简体   繁体   English

有没有一种方法可以在C ++中使用SOIL将一个纹理分解为一系列纹理?

[英]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. 我在项目中使用SOIL,我需要使用单个纹理,然后使用第一个纹理的不同部分将其转换为纹理阵列。 (To use a sprite sheet). (使用精灵表)。

I'm using SDL and OpenGL by the way. 顺便说一下,我正在使用SDL和OpenGL。

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. 在现代3D api(如OpenGL)中使用Sprite Sheet的典型方法是使用纹理坐标来处理单个纹理的不同部分。 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: 例如,如果您有一个水平3帧,每个32像素乘32像素(总大小为96x32)的简单Sprite工作表,则可以使用以下代码绘制第3帧:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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