Page 1 of 1

Animating a texture in OpenGL

Posted: Sat Jan 16, 2016 11:34 am
by corey__cakes
Hello,

I have a question. What is the best way to animate a sprite from a sprite sheet loaded in as an OpenGL texture? The only ways I could think of were to change the values used in glTexCoord*() or to switch the matrix mode to GL_TEXTURE and then use glTranslate*(). What other ways are there?

Right now I am learning OpenGL by reading through the "red book." I'm using SDL to create my context, as well as for event handling, and texture loading. I mostly want to learn 2D graphics and engine development with OpenGL before I step into the 3rd dimension.

I would also appreciate any good books/websites you may know of that can teach me more about graphics programming and engine development. The OpenGL Red Book is only helpful to learning about OpenGL and not computer graphics in depth.

Thank you.

Re: Animating a texture in OpenGL

Posted: Sat Jan 16, 2016 12:17 pm
by K-Bal
You can give your shader some uniforms that define which part of the texture to use and use them to calculate the value of gl_TexCoord[0] in your shader. Manipulating the texture matrix is also possible but I find the uniform approach more straight-forward.

Edit: for newer OpenGL versions use your own varying instead of gl_TexCoord[0].

Re: Animating a texture in OpenGL

Posted: Sun Jan 31, 2016 12:59 pm
by corey__cakes
K-Bal wrote:You can give your shader some uniforms that define which part of the texture to use and use them to calculate the value of gl_TexCoord[0] in your shader. Manipulating the texture matrix is also possible but I find the uniform approach more straight-forward.
I'm not using shaders though. I'm learning OpenGL first. GLSL comes later.

Re: Animating a texture in OpenGL

Posted: Sun Jan 31, 2016 1:21 pm
by K-Bal
The two ways you describe should work, just use one of them. I don't know of more options off the top of my head.