【问题标题】:Triangle texture mapping OpenGL三角形纹理映射OpenGL
【发布时间】:2011-11-24 15:52:30
【问题描述】:

我正在使用 Marching Cubes 算法开展一个项目,并将数据更改为 3D 模型。现在我想在 OpenGL 中为我的 3D 模型使用纹理映射。我首先尝试了一个简单的示例,它将图片映射到三角形上。

这是我的代码:

int DrawGLScene(GLvoid)    // Here's Where We Do All The Drawing
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
    glLoadIdentity();                            // Reset The Current Matrix
    glTranslatef(1.0f,0.0f,-6.0f);               // Move Into The Screen 5 Units
    glRotatef(xrot,1.0f,0.0f,0.0f);              // Rotate On The X Axis
    glRotatef(yrot,0.0f,1.0f,0.0f);              // Rotate On The Y Axis
    glRotatef(zrot,0.0f,0.0f,1.0f);              // Rotate On The Z Axis
    glBindTexture(GL_TEXTURE_2D, texture[0]);    // Select Our Texture
    glBegin(GL_TRIANGLES);

    glTexCoord2f(0, 0); glVertex3f( -2,  0, -2 );
    glTexCoord2f(1, 0); glVertex3f(  2,  0, -2 );
    glTexCoord2f(0.5, 1); glVertex3f(  0,  2, -2 );

    glEnd();

    xrot+=0.3f;                             // X Axis Rotation
    yrot+=0.2f;                             // Y Axis Rotation
    zrot+=0.4f;                             // Z Axis Rotation
    return true;                            // Keep Going
} 

现在的问题是图片没有完全映射在三角形内,程序剪切了三角形的图像,所以我丢失了部分数据。 如何将整个图像映射到三角形中?

【问题讨论】:

  • 我不确定我是否在关注你。您已经创建了一个带有纹理坐标的三角形。纹理本身是一个矩形。所以你或多或少地从你的纹理中切割出一个三角形。问题是什么?你想达到什么目的?也许你可以举例说明?
  • 继续@Bart 所说的,您不能将所有矩形纹理映射到三角形而不会出现严重的扭曲或明显的无纹理空间。只需再添加一个纹理坐标和一个新顶点,您就可以绘制整个纹理。

标签: opengl mapping textures


【解决方案1】:

如果您尝试将整个纹理加载为矩形/正方形,则必须在 OpenGL 中使用一个四边形或两个三角形制作一个正方形,然后将一半纹理映射到每个三角形。

像这样:

int DrawGLScene(GLvoid)    // Here's Where We Do All The Drawing
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
    glLoadIdentity();                            // Reset The Current Matrix
    glTranslatef(1.0f,0.0f,-6.0f);               // Move Into The Screen 5 Units
    glRotatef(xrot,1.0f,0.0f,0.0f);              // Rotate On The X Axis
    glRotatef(yrot,0.0f,1.0f,0.0f);              // Rotate On The Y Axis
    glRotatef(zrot,0.0f,0.0f,1.0f);              // Rotate On The Z Axis
    glBindTexture(GL_TEXTURE_2D, texture[0]);    // Select Our Texture
    glBegin(GL_TRIANGLES);

    // first triangle, bottom left half
    glTexCoord2f(0, 0); glVertex3f( -2,  0, -2 );
    glTexCoord2f(1, 0); glVertex3f(  2,  0, -2 );
    glTexCoord2f(0, 1); glVertex3f( -2,  2, -2 );

    // second triangle, top right half
    glTexCoord2f(1, 0); glVertex3f(  2,  0, -2 );
    glTexCoord2f(0, 1); glVertex3f( -2,  2, -2 );
    glTexCoord2f(1, 1); glVertex3f(  2,  2, -2 );

    glEnd();

    xrot+=0.3f;                             // X Axis Rotation
    yrot+=0.2f;                             // Y Axis Rotation
    zrot+=0.4f;                             // Z Axis Rotation
    return true;                            // Keep Going
} 

【讨论】:

    猜你喜欢
    • 2014-07-08
    • 2013-08-14
    • 2011-04-04
    • 1970-01-01
    • 2014-10-05
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多