使用土壤从带有opengl的纹理图集中获取区域

getting a region from texture atlas with opengl using soil

我想使用 soil 使用 opengl 将纹理图集的不同区域映射到立方体的不同侧面。到目前为止,我设法将单个图像映射到立方体的一侧:

int LoadTexture(const char*);

int texID;

void DrawCube(float width)
{
    float wd2 = width / 2;
    glColor3f(0, 0, 1);
    glBegin(GL_QUADS);

    //front
    glTexCoord2f(0, 1);
    glVertex3f(-wd2, -wd2, wd2);
    glTexCoord2f(1, 1);
    glVertex3f(wd2, -wd2, wd2);
    glTexCoord2f(1, 0);

    glVertex3f(wd2, wd2, wd2);
    glTexCoord2f(0, 0);

    glVertex3f(-wd2, wd2, wd2);

    //left side..
    //right side..
    //back..
    //top..
    //bottom..

    glEnd();
}

int LoadTexture(const char* tex) {
    texID = SOIL_load_OGL_texture(tex, 4, 0, 0);
    if (!texID) {
        cout << "Texture not loaded!\n";

    }
    return texID;
}

并且在 init 函数中:

glEnable(GL_TEXTURE_2D);
texID = LoadTexture("sas.jpg");
glBindTexture(GL_TEXTURE_2D, texID);

但我的问题是如何只获得整个纹理的一个精灵?

图片如下:

纹理坐标将几何体的顶点(点)映射到纹理图像中的一个点。从而指定纹理的哪一部分放置在几何体的特定部分上,并与纹理参数(参见glTexParameter)一起指定纹理如何包裹几何体。
一般情况下,贴图左下点用贴图坐标(0, 0)寻址,贴图右上点用(1, 1)寻址。

您的纹理由 8 列和 4 行的图块组成。要将单个纹理块放置在四边形上,必须以相同的方式拆分纹理坐标。单个图块的角是这样处理的:

float tiles_U = 8.0f;
float tiles_V = 4.0f;
float index_U = .... ; // index of the column in [0, tiles_U-1];
float index_V = .... ; // index of the row in [0, tiles_V-1];

float left_U  = index_U        / tiles_U;
float right_U = (index_U+1.0f) / tiles_U;

float top_V    = (tiles_V - index_V)        / tiles_V;
float bottom_V = (tiles_V - index_V - 1.0f) / tiles_V;

像这样将其应用于您的代码:

float wd2 = width / 2;
glColor3f(0, 0, 1);
glBegin(GL_QUADS);

glTexCoord2f( left_U, top_V );
glVertex3f(-wd2, -wd2, wd2);

glTexCoord2f( right_U, top_V );
glVertex3f(wd2, -wd2, wd2);

glTexCoord2f( right_U, bottom_V );
glVertex3f(wd2, wd2, wd2);

glTexCoord2f( left_U, bottom_V );
glVertex3f(-wd2, wd2, wd2);

glEnd();