简体   繁体   中英

Is there a way to split one texture into an array of them using SOIL in C++?

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.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM