Triangle texture mapping OpenGL

2019-03-31 20:01发布

问题:

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.

Here is my code:

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?

回答1:

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.

Like this:

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
}