0
\$\begingroup\$

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);
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
0
1
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
3
  • \$\begingroup\$ 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. \$\endgroup\$
    – Alex L
    Apr 23 '15 at 17:22
  • \$\begingroup\$ I'm fairly sure glTexparamateri belongs in the texture initialization, not every bind call. \$\endgroup\$ Apr 23 '15 at 17:49
  • \$\begingroup\$ Was not aware of that, moved the repeat texture function to the Init() \$\endgroup\$
    – Alex L
    Apr 24 '15 at 1:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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