Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm trying to animate a texture to scroll a static screen for a television, however I'm having some issues.

Just translating within the texture matrix animates all textures in the scene which is obviously a problem. However when trying to push and pop the matrix the texture matrix seems to keep getting reset. So rather than a scrolling texture, the texture just stays at the same translation.

Here's the code snippet.

glMatrixMode(GL_TEXTURE);

glPushMatrix();

glTranslatef(0, 0.03 * dt, 0);

glBindTexture(GL_TEXTURE_2D, staticTexture);

RepeatTexture();

glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glNormal3f(0, 0, 1);
glVertex3f(0.49, 0.205, 0.461);
glTexCoord2f(0, 1);
glNormal3f(0, 0, 1);
glVertex3f(0.49, 1.204, 0.461);
glTexCoord2f(1, 1);
glNormal3f(0, 0, 1);
glVertex3f(-.80, 1.204, 0.461);
glTexCoord2f(1, 0);
glNormal3f(0, 0, 1);
glVertex3f(-.80, 0.205, 0.461);
glEnd();

glPopMatrix();

glMatrixMode(GL_MODELVIEW);
share|improve this question

3 Answers 3

up vote 1 down vote accepted

Your problem is that you're doing this:

glPushMatrix();
glTranslatef(0, 0.03 * dt, 0);
// (...)
glPopMatrix();

The offset created by the glTranslatef() command only exists between the glPushMatrix and the glPopMatrix; it doesn't retain its values from previous frames; the offset goes away forever as soon as the matrix is popped. So assuming that dt remains constant at a value of 1/30 (for 30fps), every single frame you draw effectively uses the same single 0.03 * 0.033 offset from the texture coordinates specified on the mesh. Which is why it doesn't appear to scroll.

In order to make it scroll, you want to make the texture be offset by a different amount on each frame. One easy way to do this would be to do glTranslate(0, 0.03 * absolute_time, 0);, where 'absolute_time' is the number of seconds since the game started to run. Or any other continuously-upward-moving counter. That way, the translation will continue to increase as the game runs, instead of holding still.

share|improve this answer
    
Thank you! Works perfectly now. –  Alex L Apr 24 at 1:53

There's an easy solution - don't translate with a matrix.

Since you're using immediate mode, it's an incredibly easy matter to simply offset the texture coords however much you'd like.

EG, glTexCoord2f(1, 0.03 * dt);

This will, of course, not be so easily done when you learn about the OpenGL Core Profile, which is highly recommended to be used. But until then, there's a very easy solution right there.

share|improve this answer

have you tried to assign a separate var for each texture?

also what is RepeatTexture(); calling?

i think that may be the source of your problems

share|improve this answer
    
I'm unsure what you mean by assigning a separate value for each texture. In regards to the RepeatTexture(), it's just to save space, it sets the texture to repeat vertically and horizontally. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); Not calling the function doesn't change anything. –  Alex L Apr 23 at 17:22
    
I'm fairly sure glTexparamateri belongs in the texture initialization, not every bind call. –  mcmonkey4eva Apr 23 at 17:49
    
Was not aware of that, moved the repeat texture function to the Init() –  Alex L Apr 24 at 1:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.