Search code examples
opengl3dtexturestexture-mapping

Texture Mapping a Pyramid


I am attempting to texture a pyramid that I drew. I am trying to map a brick texture that has the same orientation on all sides, however all the texture coordinates I've tried thus far leave me with the correct orientation on some faces of the pyramid and a flipped orientation on other sides. The image attached shows what I'm referring to, I am trying to get all faces of the pyramid to have the "correct" orientation as shown in the image

enter image description here

I assumed this is an issue with my texture coordinates.

Here are the vertices and texture coords I'm working with:

GLfloat verts[] = {
        // Vertex Positions    // texture coords 
         0.5f,  0.5f, 0.0f,     1.0f, 1.0f, // Top Right Vertex 
         0.5f, -0.5f, 0.0f,     1.0f, 0.0f, // Bottom Right Vertex 

        -0.5f, -0.5f, 0.0f,      0.0f, 0.0f, // Bottom Left Vertex 
        -0.5f,  0.5f, 0.0f,      0.0f, 1.0f, // Top Left Vertex 

        
        0.0f, 0.0f, 1.0f,        0.5f, 0.5f// Pyramid top vertex 
    };

Solution

  • You might have to use multiple texture coordinates for each vertex. The texture coordinates for the base would be:

    float basetexcoords[] = {
    1.0f, 1.0f,
    1.0f, 0.0f,
    0.0f, 0.0f,
    0.0f, 1.0f
    };
    

    The texture coordinates for each side would be:

    float sidetexcoords[] = {
    0.5f, 0.5f,
    1.0f, 0.0f,
    0.0f, 0.0f
    };
    

    A possible way to do it is to repeat the same vertex positions in your verts array. While not very memory-efficient and rather bad it is a simple solution.

    Here's how it can look like:

    float verts[] = {
    -0.5f, -0.5f, 0.0f, 0.0f, 0.0f,
    -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
    0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
    0.5f, 0.5f, 0.0f, 1.0f, 1.0f,
    -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
    0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
    0.0f, 0.0f, 1.0f, 0.5f, 0.5f,
    -0.5f, -0.5f, 0.0f, 0.0f, 0.0f,
    0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
    0.0f, 0.0f, 1.0f, 0.5f, 0.5f,
    0.5f, -0.5f, 0.0f, 0.0f, 0.0f,
    0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
    0.0f, 0.0f, 1.0f, 0.5f, 0.5f,
    0.5f, 0.5f, 0.0f, 0.0f, 0.0f,
    -0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
    0.0f, 0.0f, 1.0f, 0.5f, 0.5f,
    -0.5f, 0.5f, 0.0f, 0.0f, 0.0f,
    -0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
    };