三角形纹理映射OpenGL [英] Triangle texture mapping OpenGL

查看:183
本文介绍了三角形纹理映射OpenGL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个使用Marching Cubes算法的项目,并将数据更改为3D模型.现在,我想在OpenGL中为我的3D模型使用纹理映射.我尝试过一个简单的示例,该示例将图片映射到三角形上.

I am working on a project using the Marching Cubes algorithm and changing the data into a 3D model. Now I want to use texture mapping in OpenGL for my 3D model. I have tried a simple example to begin with, which maps a picture onto a triangle.

这是我的代码:

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
} 

现在的问题是,图片无法完全映射到三角形内,并且程序会剪切三角形的图像,因此我丢失了部分数据. 如何将整个图像映射到三角形中?

Now the problem is that the picture does not map completely within the triangle, and the program cut the image for the triangle, so I am losing some part of my data. How can I map the whole image into the triangle?

推荐答案

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

If you're trying to load the entire texture as a rectangle/square, you have to make a square in OpenGL using either a quad or two triangles, and then map half of the texture to each triangle.

喜欢这个:

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
} 

这篇关于三角形纹理映射OpenGL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆