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);
}