PDA

View Full Version : Scrolling textures



foniks
05-10-2003, 12:19 AM
I have a texture on a single quad, which is repeated across the surface of the quad.

I want to scroll the texture across the surface of the quad (for a background in a 2D game), but I can't work out how to manipulate the texture coordinates correctly.

Anyone got any examples or suggestions?

Cheers...

yaro_dup1
05-10-2003, 01:30 AM
Hi,

Scrolling is very simple.
For example if You want to scroll the texture with X coordinates You have to change the lower coordinates from 0.0f to 1.0f and higher from tex_count to texcount+1.0f.

Here is some source of code:

while (game_is_running)
{
for (float i = 0.0f; i < 1.0f; i += 0.05f)
{
glBindTexture(GL_TEXTURE_2D, texArray[id])
glBegin(GL_QUADS);
glTexCoord2f(i, 0); glVertex2f(x1, y1);
glTexCoord2f(i, ycount); glVertex2f(x1, y2);
glTexCoord2f(xcount + i, ycount); glVertex2f(x2, y2);
glTexCoord2f(xcount + i, 0); glVertex2f(x2, y1);
glEnd();
}

}

This is backward scrolling, there might be some mistakes cause I writted it online but I think You'll manage to fix it and use it.

Cheers,
yaro

Obli
05-10-2003, 01:43 AM
This works fine if you have few vertices. Another way to do this is to change MatrixMode to TEXTURE and do some rotate/translate as you want.

This will affect all the vertices without the need to update them. Unluckly, some video cards (up to GeForce1 I guess) will not like this and will give you less performance.

For what you need to do, I guess glYaro got the best idea, however the thing I've said will probably come in handy in the future...

Will you show us your work? I would like to play it a bit!

EDIT: ah, remember to go back to identity matrix for TEXTURE and to MatrixMode MODELVIEW or averything will be messed up...

[This message has been edited by Obli (edited 05-10-2003).]

foniks
05-10-2003, 03:05 AM
Thanks guys http://www.opengl.org/discussion_boards/ubb/smile.gif

Both solutions work well.


Will you show us your work? I would like to play it a bit!

Absolutely http://www.opengl.org/discussion_boards/ubb/wink.gif It may take a while, but when I have a nice finished product I will be sure to post a link.