PDA

View Full Version : Texture and Environmental Mapping ?



Andrew
12-11-2000, 11:18 AM
I am trying to set an image (.rgb) as a background of a window, and have a dynamic texture mapped 3D object (torus) rotate in front of it.

I have a QUAD set up the same size as my widow, but it is rotating with the torus, infact, it is `slicing' the torus right through the middle.

I have a feeling that I am rotating everything in the window, or the viewpoint, instead of just the torus.

Does anyone know how to fix this?

I have a snip-it of code below.

Any help would be greatly appreciated.
Andrew.


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

glBegin(GL_QUADS) ;
glTexCoord2f(0.0,0.0) ; glVertex2f(-15.0,-15.0) ;
glTexCoord2f(0.0,1.0) ; glVertex2f(-15.0, 15.0) ;
glTexCoord2f(1.0,1.0) ; glVertex2f( 15.0, 15.0) ;
glTexCoord2f(1.0,0.0) ; glVertex2f( 15.0,-15.0) ;
glEnd() ;

glBindTexture(GL_TEXTURE_2D, textures[0]);
glEnable(GL_TEXTURE_2D);

glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
glEnable(GL_CULL_FACE);

glRotatef(5.0, 1.0, 1.0, 1.0);
glutSolidTorus(2.0, 5.0, 50, 50);

glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
glDisable(GL_CULL_FACE);

glutSwapBuffers();
}

Bob
12-12-2000, 12:40 AM
I don't think you get the whole concept with OpenGL's matrices. They are not automatically resetored each frame, this is something you have to do yourself (with glLoadIdentity()). Your program doesn't (or at least you don't show it) reset the modelview matrix, so each frame you make another rotation which is added to matrix from the previous frame.

If you look at it closely, you will notice that the quad is 5.0 degrees after the torus in it's rotation. This is because you update the matrix between the two objects.

To solve this, you need a static variable that holds the angle of rotation, and then use this variable to rotate the object with a new matrix each frame.



void display(void)
{
static float angle = 0.0f;
angle += 5.0f;
glLoadIdentity();
glBegin(GL_QUADS);
...
glEnd();
glRotatef(angle, 1.0f, 1.0f, 1.0f);
glutSolidTorus();
}

... in semi-pseudocode.