【问题标题】:getting a region from texture atlas with opengl using soil使用土壤从opengl的纹理图集中获取区域
【发布时间】:2017-10-27 21:10:17
【问题描述】:

我想使用 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);

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

这是图片:

【问题讨论】:

  • 但是你如何计算分数呢?我的意思是,如果我想要例如第一行中的第二个图块,我怎么知道 glTexCoord2f 的参数?

标签: c++ opengl mapping textures soil


【解决方案1】:

纹理坐标将几何体的顶点(点)映射到纹理图像中的一个点。因此,它指定了纹理的哪一部分放置在几何体的特定部分上,并与纹理参数(参见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();

【讨论】:

    猜你喜欢
    • 2014-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-03
    • 2014-12-30
    相关资源
    最近更新 更多