Animating a texture in OpenGL

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Animating a texture in OpenGL

Postby corey__cakes on Sat Jan 16, 2016 1:34 pm

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.
What do you call a cow with no legs?

Ground beef.
corey__cakes
ES Beta Backer
ES Beta Backer
 
Posts: 23
Joined: Mon Dec 23, 2013 9:56 pm
Location: Your VCR.

Re: Animating a texture in OpenGL

Postby K-Bal on Sat Jan 16, 2016 2:17 pm

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].
User avatar
K-Bal
ES Beta Backer
ES Beta Backer
 
Posts: 719
Joined: Sun Mar 15, 2009 4:21 pm
Location: Germany, Aachen

Re: Animating a texture in OpenGL

Postby corey__cakes on Sun Jan 31, 2016 2:59 pm

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.
What do you call a cow with no legs?

Ground beef.
corey__cakes
ES Beta Backer
ES Beta Backer
 
Posts: 23
Joined: Mon Dec 23, 2013 9:56 pm
Location: Your VCR.

Re: Animating a texture in OpenGL

Postby K-Bal on Sun Jan 31, 2016 3:21 pm

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.
User avatar
K-Bal
ES Beta Backer
ES Beta Backer
 
Posts: 719
Joined: Sun Mar 15, 2009 4:21 pm
Location: Germany, Aachen


Return to Programming Discussion

Who is online

Users browsing this forum: No registered users and 3 guests

cron