• Advertisement
Sign in to follow this  

Texturing Cubes using UV Coordinates

This topic is 783 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am currently having trouble texturing a cube with UV coordinates. I have tried different configurations with the UV coordinates. With no luck. The textures renders a little but is still blurry. Not sure what the problem would be. I have a suspicion it is the uvCoord values, but I'm not sure what configuration they should be in.

const GLfloat vertices[] = {

    

       -0.5f,  0.5f, 0.0f,  // Top Left

       -0.5f, -0.5f, 0.0f,  // Bottom Left

        0.5f, -0.5f, 0.0f,  // Bottom Right

        0.5f,  0.5f, 0.0f,  // Top Right

    

      -0.5f,  0.5f, -1.0f,  // Top Left     (back)

      -0.5f, -0.5f, -1.0f,  // Bottom Left  (back)

       0.5f, -0.5f, -1.0f,  // Bottom Right (back)

       0.5f,  0.5f, -1.0f   // Top Right    (back)    

    

    };



    const GLfloat color[] = {

    

        0.0f, 0.0f, 1.0f,

        0.0f, 0.0f, 1.0f,

        0.0f, 0.0f, 1.0f,

        0.0f, 0.0f, 1.0f

    };



    const GLfloat texCoord[] = {

    

        1.0f, 1.0f, 0.0f,

        1.0f, 0.0f, 0.0f,

        0.0f, 0.0f, 0.0f,

        0.0f, 1.0f, 0.0f

    

    };



    const GLfloat uvCoord[] =

    {



        0.0f, 1.0f,  // Top Left

        0.0f, 0.0f,  // Bottom Left

        1.0f, 0.0f,  // Bottom Right

        1.0f, 1.0f,   // Top Right



        0.0f, 1.0f,  // Top Left

        0.0f, 0.0f,  // Bottom Left

        1.0f, 0.0f,  // Bottom Right

        1.0f, 1.0f,   // Top Right



        0.0f, 1.0f,  // Top Left

        0.0f, 0.0f,  // Bottom Left

        1.0f, 0.0f,  // Bottom Right

        1.0f, 1.0f,   // Top Right



        0.0f, 1.0f,  // Top Left

        0.0f, 0.0f,  // Bottom Left

        1.0f, 0.0f,  // Bottom Right

        1.0f, 1.0f,   // Top Right



        0.0f, 1.0f,  // Top Left

        0.0f, 0.0f,  // Bottom Left

        1.0f, 0.0f,  // Bottom Right

        1.0f, 1.0f,   // Top Right



        0.0f, 1.0f,  // Top Left

        0.0f, 0.0f,  // Bottom Left

        1.0f, 0.0f,  // Bottom Right

        1.0f, 1.0f   // Top Right



    };

    

    const int indices[] =

    {

        // Front

        0, 1, 3,

        1, 2, 3,

    

        // Back

        4, 5, 6,

        4, 6, 7,



        // Left

        4, 5, 1,

        1, 0, 4,



        // Right

        3, 6, 2,

        7, 6, 3,



        // Top

        7, 4, 3,

        4, 0, 3,



        // Bottom

        1, 2, 5,

        2, 5, 6



    };

 
 

        glBindBuffer(GL_ARRAY_BUFFER, VBO[3]);

        glBufferData(GL_ARRAY_BUFFER, sizeof(uvCoord), uvCoord, GL_STATIC_DRAW);

        glVertexAttribPointer(UV_POS, 2, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), nullptr);

        glEnableVertexAttribArray(UV_POS);

Vertex Shader:

#version 400 core



layout (location = 0) in vec3 position;

layout (location = 1) in vec4 color;

layout (location = 2) in vec2 texCoord;

layout (location = 3) in vec2 uvCoord;



out vec4 out_color;

out vec2 out_texCoord;

out vec2 out_uvCoord;





uniform mat4 MVP;

uniform mat4 rotation_matrix;



void main()

{

    out_color    = color;

    out_uvCoord  = uvCoord;

    out_texCoord = vec2(texCoord.x, texCoord.y);

    gl_Position  = MVP * rotation_matrix * vec4(position, 1.0f);

}

Fragment Shader:

#version 400 core



in  vec4 out_color;

out vec4 Output_Color;

in  vec2 out_texCoord;

out vec2 Output_texCoord;



in vec2 out_uvCoord;



uniform sampler2D Texture;



void main()

{

    

     Output_Color = texture(Texture, out_uvCoord);

}

Share this post


Link to post
Share on other sites
Advertisement


The textures renders a little but is still blurry. Not sure what the problem would be.

What do you mean? Post a picture of cube, and you will get definite answers. It can be filtering, texture resource dimensions and mipmap levels etc.

Share this post


Link to post
Share on other sites

One of the problems you are having is that you are using an element buffer this isn't a bad thing but i think you may misunderstand what it is doing. You have 8 vertices which you reuse to make your cube but you have 24 uv coords when glDrawElements draws lets say your first 3 triangle

 

0, 1, 3,

1, 2, 3

 

it will grab the data pointed to by your glVertexAttribPointers so this would get for element 0

-0.5f, 0.5f,  0.0f //for the position
 0.0f, 0.0f, 1.0f //for the colour
1.0f, 1.0f, 0.0f // for the texCoord
0.0f, 1.0f,  // for the uv

everytime that the element buffer says it wants element 0 this is because when you use glDrawElements it will look at the data supplied by glVertexAttribPointer and get the data from the buffer at that point. So it is never using 16 of your uv coords as they are never asked for. this is because when glDrawElements it will ask for say element 1 which would return the the 2nd lot of data. That means that if you ask for element 3 it will look at the data supplied by glVertexAttribPointer and get data it says is is related to element 3 which for

glVertexAttribPointer(UV_POS, // location
2, // size
GL_FLOAT, // data type
GL_FALSE, // normalize
3 * sizeof(GLfloat), // stride
nullptr); // where in the buffer the data starts

so in this example you have told opengl that the start of the bound GL_ARRAY_BUFFER is where the first bit of data is for UV_POS which contains 2 elements of type float which is not normalized and has a stride of 3 * sizeof(GLfloat) which is 12 bytes. so for element 0 OpenGL would get 2 floats from the beginning of the buffer. For element 1 it would get 2 floats from the beginning of the buffer + (8 bytes for the two float for element 0 + 12 bytes for the stride) so 20 bytes from the beginning of the buffer.

Edited by Gooey

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement