PDA

View Full Version : Rotate a 2D Texture 1 step at a time please!



KraftDinner
10-12-2004, 06:12 PM
I have the following code and want to add 2D texture mapping rotation. (Rotate the texture not the model)

This code displays a texture map in quadrant zero of the display area... ie top right quadrant.

My goal is to paste this texture onto a 2x2 polygon centered about the origin. Rotate the texture but have the polygon remain fixed at the origin with an image of the texture map rotating within this polygon... pixels outside of the texture map should be blue (as that is the border color of the texture map)



#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>

#define iWidth (16+2)
#define iHeight (16+2)

static GLubyte image[iHeight][iWidth][3];
static GLuint texName;

void makeImage(void)
{
int s, t;
for (s=0; s < 18; ++s)
{
for (t=0; t < 18; ++t)
{
image[t][s][0] = 0;
image[t][s][1] = 0;
image[t][s][2] = 255;
}
}
for (s=1; s < 17; s++)
{
for (t=1; t < 17; t++)
{
image[t][s][0] = (GLubyte) ((s-1) * 17);
image[t][s][1] = (GLubyte) ((t-1) * 17);
image[t][s][2] = (GLubyte) (1);
}
}
}

void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);

makeImage();

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

glGenTextures(1, &amp;texName);
glBindTexture(GL_TEXTURE_2D, texName);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, iWidth, iHeight, 1, GL_RGB, GL_UNSIGNED_BYTE, image);
glEnable(GL_TEXTURE_2D);
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


glBegin(GL_QUADS);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(1.0, 0.0, 0.0);
glVertex3f(1.0, 1.0, 0.0);
glVertex3f(0.0, 1.0, 0.0);
glEnd();

glFlush();
glutSwapBuffers();
}

void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 30.0);
glTranslatef(0.0,0.0,-3.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
}
}

int main(int argc, char** argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(250, 250);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutKeyboardFunc (keyboard);
glutMainLoop();
return 0;
}

KraftDinner
10-12-2004, 06:28 PM
I transated the texture and the polygon now to have their centers at the origin.


void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glTranslatef(+0.5, +0.5, 0.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glBegin(GL_QUADS);
glVertex3f(-0.5, -0.5, 0.0);
glVertex3f( 0.5, -0.5, 0.0);
glVertex3f( 0.5, 0.5, 0.0);
glVertex3f(-0.5, 0.5, 0.0);
glEnd();

glFlush();
glutSwapBuffers();

}

KraftDinner
10-12-2004, 07:05 PM
But if I need to rotate the texture map (about the z axis) then it will no loger fit in the 1x1 square I've defined. So I try a 2x2 square centered about the origin.



glBegin(GL_QUADS);
glVertex3f(-1.0, -1.0, 0.0);
glVertex3f( 1.0, -1.0, 0.0);
glVertex3f( 1.0, 1.0, 0.0);
glVertex3f(-1.0, 1.0, 0.0);
glEnd();I tried this but get strange results... (The texture is centered in the 2x2 square... I see a single blue border but the rest of the square is random colors) I expected the texture to be in the center of the square with a blue box around it. (Because I clamped to border)

Dol AI with 3D Bone system
10-12-2004, 08:58 PM
You can use glBindTexture to animated textures in the scene, but you must have 2 images...

KraftDinner
10-13-2004, 07:24 AM
Well I'm at work now on a different computer...
and I'm getting different results.

At work on my G5 I am unable to get the blue border at all around my image. Any idea why?

I've changed the display code to rotate the texture and it does indeed rotate... (but no blue border)


void display(void)
{
static float r=0.0;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glTranslatef(+0.5, +0.5, 0.0);
glRotatef(r, 0.0, 0.0, 1.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glBegin(GL_QUADS);
glVertex3f(-1.0, -1.0, 0.0);
glVertex3f( 1.0, -1.0, 0.0);
glVertex3f( 1.0, 1.0, 0.0);
glVertex3f(-1.0, 1.0, 0.0);
glEnd();

glFlush();
glutSwapBuffers();

r = r + .05;
}I am curious as to why I didn't need to define the S and T planes
like:


float planeS[]={1.0f, 0.0f, 0.0f, 1.0f);
float planeT[]={0.0f, 1.0f, 0.0f, 1.0f);Nor did I seem to need:

glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGenfv(GL_S, GL_EYE_PLANE, planeS);
glTexGenfv(GL_T, GL_EYE_PLANE, planeT);When are these necessary?
Shouldn't the planes be multiplied by some matrix in order for them to actually do something?

Dol AI with 3D Bone system
10-13-2004, 08:03 AM
Ah ha you must use :-

glBegin(GL_TRIANGLES);
glTexCoord2f(U, V); glVertex3f(x1, y1, z1);
glTexCoord2f(U, V); glVertex3f(x1, y1, z1);
glTexCoord2f(U, V); glVertex3f(x1, y1, z1);
glEnd;

to transform or rotation the textures that sure you try this...

U := X Coordinate Textures
V := Y Coordinate Textures

KraftDinner
10-13-2004, 08:32 AM
No I do not wish to specify the texture coordinates.
I want opengl to automatically generate them for me.

Dol AI with 3D Bone system
10-13-2004, 08:47 AM
You try call the TEXGEN to all scene and change parameter to animation texture into frame loop.

use glTexGenfv

KraftDinner
10-13-2004, 08:54 AM
Yes.
Please re-read the last question.
It says that it works.. but that I didn't need to call these functions...
Many thanks.
;)

Dol AI with 3D Bone system
10-13-2004, 09:05 AM
Why you not build the inner matrix?

this:-
glPushMatrix();
...Draw triangle
glPushMatrix();
...TexGen and transform textures here
glPopMatrix();
glPopMatrix();

KraftDinner
10-13-2004, 11:19 AM
An example please of what you mean.

Dol AI with 3D Bone system
10-13-2004, 11:35 AM
I'm not sure for this but I think the surely pixel position on the surface matrix.