Sign in to follow this  

OpenGL 2D Animation in OpenGL

Recommended Posts

storm33229    125
I've done a little searching on the forums here, but couldn't quite find any one post that outlined how to animate 2d sprites in OpenGL. I have figured out how to clip a part of a texture and assign it to a quad. This would give me a single frame of a spritesheet, and obviously knowing how to do this would make it rather simple to make sprites animate. The trouble is that because I am using TexCoords I do not know how to select certain pixel rects of a sprite sheet to draw. For example:

glBindTexture(GL_TEXTURE_2D, tex);
glTexCoord2f(0.5, 0.5);
glVertex3f(0.25, 0.25, 0.0);
glTexCoord2f(1.0, 0.5);
glVertex3f(0.5, 0.25, 0.0);
glTexCoord2f(1.0, 1.0);
glVertex3f(0.5, 0.5, 0.0);
glTexCoord2f(0.5, 1.0);
glVertex3f(0.25, 0.5, 0.0); [font="Consolas"][size="2"]

This code will draw a texture that starts halfway through and displays the other half so:

(or at least that is how I imagine it)

Anyway using TexCoords like this does not seem to be a good way to select which part of a sprite sheet i would like to draw. Are there better ways of doing this?

Share this post

Link to post
Share on other sites
capricorn    139
[quote name='storm33229' timestamp='1312662188' post='4845528']
Anyway using TexCoords like this does not seem to be a good way to select which part of a sprite sheet i would like to draw. Are there better ways of doing this?

There may be another way through the use of texture matrix, but it is impractical.

You can easiliy convert from your pixel coordinates to texture coordinates and back: you know actual texture dimensions in pixels and your sprite locations/dimensions.
For a sprite with pixel geometry (x,y,width,height) texture coords would be (x/TextureWidth,y/TextureHeight,(x+w)/TextureWidth,(y+h)/TextureHeight). I'm sure you can quickly come up with a backwards conversion should you need one.

Share this post

Link to post
Share on other sites
JTippetts    12949
If you know the pixel locations and widths of the sprite, and the dimensions of the texture, you can easily calculate the texture coordinates. ie, given a sprite sheet sized 1024x1024, the sprite at location (256,256) of size 64x64 would be defined by the texture coordinate rectangle (0.25,0.25) - (0.3125,0.3125). It's just a matter of simple division. Each pixel in a 1024x1024 image is equalt to 1/1024 or 0.0009765625 and (1/1024)*256=0.25.

Share this post

Link to post
Share on other sites
NicoG    172
SpriteSheets are the way to go. In my 2D Engine, I use this Tool:
[list][*][url=""]SpritesheetPacker[/url][/list]Very nice and easy to use, even has a commandline tool coming with it.
Implementation for a parser of the format:

And animating is just flipping the texture-coords.
In my code, I just keep all frames in a vector, which are described in a xml document and all frames live on the same spritesheet.
Then every X seconds or frames I flip the texture-coords of the sprite and voilá, decent animation :).

Thats the code without drawing the sprite, that is done in another class since this is a derived class only. But could give a clue about how to do it.
The code should be easy to understand.
Believe me, Spritesheets really speed you up and flipping the texture-coords is the most efficient way you can have when doing pic-by-pic animations.
Thats at least what I researched.

Share this post

Link to post
Share on other sites
storm33229    125
Awesome, thank you for your help. Now I am on to learning how to NOT use Immediate mode. [img][/img]

Share this post

Link to post
Share on other sites
johnchapman    601
Another way to do it would be to load each frame of animation into layers of a 3D texture. Then to 'play' the animation you just interpolate the w texture coordinate. I'm not sure how efficient this is vs. jumping around a 2d texture but it means you can take advantage of texture filtering between frames.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By markshaw001
      Hi i am new to this forum  i wanted to ask for help from all of you i want to generate real time terrain using a 32 bit heightmap i am good at c++ and have started learning Opengl as i am very interested in making landscapes in opengl i have looked around the internet for help about this topic but i am not getting the hang of the concepts and what they are doing can some here suggests me some good resources for making terrain engine please for example like tutorials,books etc so that i can understand the whole concept of terrain generation.
    • By KarimIO
      Hey guys. I'm trying to get my application to work on my Nvidia GTX 970 desktop. It currently works on my Intel HD 3000 laptop, but on the desktop, every bind textures specifically from framebuffers, I get half a second of lag. This is done 4 times as I have three RGBA textures and one depth 32F buffer. I tried to use debugging software for the first time - RenderDoc only shows SwapBuffers() and no OGL calls, while Nvidia Nsight crashes upon execution, so neither are helpful. Without binding it runs regularly. This does not happen with non-framebuffer binds.
      GLFramebuffer::GLFramebuffer(FramebufferCreateInfo createInfo) { glGenFramebuffers(1, &fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); textures = new GLuint[createInfo.numColorTargets]; glGenTextures(createInfo.numColorTargets, textures); GLenum *DrawBuffers = new GLenum[createInfo.numColorTargets]; for (uint32_t i = 0; i < createInfo.numColorTargets; i++) { glBindTexture(GL_TEXTURE_2D, textures[i]); GLint internalFormat; GLenum format; TranslateFormats(createInfo.colorFormats[i], format, internalFormat); // returns GL_RGBA and GL_RGBA glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, createInfo.width, createInfo.height, 0, format, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); DrawBuffers[i] = GL_COLOR_ATTACHMENT0 + i; glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, textures[i], 0); } if (createInfo.depthFormat != FORMAT_DEPTH_NONE) { GLenum depthFormat; switch (createInfo.depthFormat) { case FORMAT_DEPTH_16: depthFormat = GL_DEPTH_COMPONENT16; break; case FORMAT_DEPTH_24: depthFormat = GL_DEPTH_COMPONENT24; break; case FORMAT_DEPTH_32: depthFormat = GL_DEPTH_COMPONENT32; break; case FORMAT_DEPTH_24_STENCIL_8: depthFormat = GL_DEPTH24_STENCIL8; break; case FORMAT_DEPTH_32_STENCIL_8: depthFormat = GL_DEPTH32F_STENCIL8; break; } glGenTextures(1, &depthrenderbuffer); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, createInfo.width, createInfo.height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthrenderbuffer, 0); } if (createInfo.numColorTargets > 0) glDrawBuffers(createInfo.numColorTargets, DrawBuffers); else glDrawBuffer(GL_NONE); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) std::cout << "Framebuffer Incomplete\n"; glBindFramebuffer(GL_FRAMEBUFFER, 0); width = createInfo.width; height = createInfo.height; } // ... // FBO Creation FramebufferCreateInfo gbufferCI; gbufferCI.colorFormats =; gbufferCI.depthFormat = FORMAT_DEPTH_32; gbufferCI.numColorTargets = gbufferCFs.size(); gbufferCI.width = engine.settings.resolutionX; gbufferCI.height = engine.settings.resolutionY; gbufferCI.renderPass = nullptr; gbuffer = graphicsWrapper->CreateFramebuffer(gbufferCI); // Bind glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo); // Draw here... // Bind to textures glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textures[0]); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, textures[1]); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, textures[2]); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); Here is an extract of my code. I can't think of anything else to include. I've really been butting my head into a wall trying to think of a reason but I can think of none and all my research yields nothing. Thanks in advance!
    • By Adrianensis
      Hi everyone, I've shared my 2D Game Engine source code. It's the result of 4 years working on it (and I still continue improving features ) and I want to share with the community. You can see some videos on youtube and some demo gifs on my twitter account.
      This Engine has been developed as End-of-Degree Project and it is coded in Javascript, WebGL and GLSL. The engine is written from scratch.
      This is not a professional engine but it's for learning purposes, so anyone can review the code an learn basis about graphics, physics or game engine architecture. Source code on this GitHub repository.
      I'm available for a good conversation about Game Engine / Graphics Programming
    • By C0dR
      I would like to introduce the first version of my physically based camera rendering library, written in C++, called PhysiCam.
      Physicam is an open source OpenGL C++ library, which provides physically based camera rendering and parameters. It is based on OpenGL and designed to be used as either static library or dynamic library and can be integrated in existing applications.
      The following features are implemented:
      Physically based sensor and focal length calculation Autoexposure Manual exposure Lense distortion Bloom (influenced by ISO, Shutter Speed, Sensor type etc.) Bokeh (influenced by Aperture, Sensor type and focal length) Tonemapping  
      You can find the repository at
      I would be happy about feedback, suggestions or contributions.

    • By altay
      Hi folks,
      Imagine we have 8 different light sources in our scene and want dynamic shadow map for each of them. The question is how do we generate shadow maps? Do we render the scene for each to get the depth data? If so, how about performance? Do we deal with the performance issues just by applying general methods (e.g. frustum culling)?
  • Popular Now