views:

218

answers:

1

I'm using SOIL in my project, and I need to take in a single texture, and than convert it into an array of textures using different parts of the first texture. (To use a sprite sheet).

I'm using SDL and OpenGL by the way.

+1  A: 

The typical way to use sprite sheeting with a modern 3D api like OpenGL is to use texture coordinates to address different parts of your individual texture. While you can split it up it is much more resource friendly to use texture coordinates.

For example, if you had a simple sprite sheet with 3 frames horizontally, each 32 pixels by 32 pixels (for a total size of 96x32), you would use the following code to draw the 3rd frame:

// I assume you have bound your source texture
// This is the U coordinate's origin in texture space
float xStart = 64.0f / 96.0f;

// This is one frame width in texture space
float xIncrement = 32.0f / 96.0f;

glBegin(GL_QUADS);
  glTexCoord2f(xStart, 0);
  glVertex2f(-16.0f, 16.0f);

  glTexCoord2f(xStart, 1.0f);
  glVertex2f(-16.0f, -16.0f);

  glTexCoord2f(xStart + xIncrement, 0);
  glVertex2f(16.0f, 16.0f);

  glTexCoord2f(xStart + xIncrement, 1.0f);
  glVertex2f(16.0f, -16.0f);
glEnd();
Ron Warholic
Yeah, but I need to have height too.
William
Height works the same as width, just use the same variables I did but scale them based on your total height instead of total width.
Ron Warholic