0
\$\begingroup\$

I'm going through open.gl tutorials. I'm at the point where I want to apply textures. The tutorial first introduces texture coordinates to the vertices[] array, and then adding the corresponding in, out and uniform variables accordingly on each shader. The issue I'm having with is OpenGL cannot interpret the vertex attributes correctly with the given stride and offset parameters when I change the structure of the vertices[] array, as seen on the second half of the code below.

float vertices[] =
{//   X      Y     R     G     B
    -0.5f, +0.5f, 1.0f, 0.0f, 0.0f, // Top-left
    +0.5f, +0.5f, 0.0f, 1.0f, 0.0f, // Top-right
    +0.5f, -0.5f, 0.0f, 0.0f, 1.0f, // Bottom-right
    -0.5f, -0.5f, 1.0f, 1.0f, 1.0f  // Bottom-left
};

GLuint elements[] = {
    0, 1, 2,
    2, 3, 0
};

GLint posAttrib = glGetAttribLocation(_shaderProgram.program, "position");  // handle to vert shader argument
glEnableVertexAttribArray(posAttrib);
glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 
    5 * sizeof(float), 0); // how to interpret input array
// position, vec2, type, normalize mode, stride, offset             

GLint colAttrib = glGetAttribLocation(_shaderProgram.program, "color");
glEnableVertexAttribArray(colAttrib);
glVertexAttribPointer(colAttrib, 3, GL_FLOAT, GL_FALSE,
    5 * sizeof(float), (void*)(2 * sizeof(float)));

 // Draw call
 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

vert.glsl

#version 150

in vec2 position;
in vec3 color;

out vec3 Color;

uniform mat4 model;
uniform mat4 view;
uniform mat4 proj;

void main()
{
    Color = color;
    gl_Position = proj * view * model * vec4(position, 0.0, 1.0);
}

frag.glsl

#version 150

in vec3 Color;

out vec4 outColor;

uniform sampler2D tex;

void main()
{
    outColor = vec4(Color, 1.0);
}

enter image description here


However, when I want to introduce texture coordinates in the vertices[] array like this, I get weird shapes. OpenGL for some reason cannot interpret the vertices[] array correctly.

float vertices[] =                      
{//   X      Y       R     G     B       S     T
    -0.5f, +0.5f,   1.0f, 0.0f, 0.0f,   0.0f, 0.0f      // Top-left
    +0.5f, +0.5f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f      // Top-right
    +0.5f, -0.5f,   0.0f, 0.0f, 1.0f,   1.0f, 1.0f      // Bottom-right
    -0.5f, -0.5f,   1.0f, 1.0f, 1.0f,   0.0f, 1.0f      // Bottom-left
};

GLuint elements[] = {
    0, 1, 2,
    2, 3, 0
};

GLint posAttrib = glGetAttribLocation(_shaderProgram.program, "position");  // handle to vert shader argument
glEnableVertexAttribArray(posAttrib);
glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 
    7 * sizeof(float), 0); // how to interpret input array
                // position, vec2, type, normalize mode, stride, offset

GLint colAttrib = glGetAttribLocation(_shaderProgram.program, "color");
glEnableVertexAttribArray(colAttrib);
glVertexAttribPointer(colAttrib, 3, GL_FLOAT, GL_FALSE,
    7 * sizeof(float), (void*)(2 * sizeof(float)));

GLint texAttrib = glGetAttribLocation(_shaderProgram.program, "texcoords");
glEnableVertexAttribArray(texAttrib);
glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE,
    7 * sizeof(float), (void*)(5 * sizeof(float)));

 // Draw call (same as before)
 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

vert.glsl

#version 150

in vec2 position;
in vec3 color;
in vec2 texcoords;

out vec3 Color;
out vec2 TexCoord;

uniform mat4 model;
uniform mat4 view;
uniform mat4 proj;

void main()
{
    Color = color;
    TexCoord = texcoords;

    gl_Position = proj * view * model * vec4(position, 0.0, 1.0);
}

frag.glsl

#version 150

in vec3 Color;
in vec2 TexCoord;

out vec4 outColor;

uniform sampler2D tex;

void main()
{
    // i know the pixel color computation doesn't make sense here, as I also 
    // do not provide the texture uniform variable. But I'm aware of the 
    // glsl compiler optimizations stripping away unused attributes, so here 
    // I use it in a meaningless way so that its not optimized out. I just 
    // want to make sure the interpretation of the vertex attributes are 
    // correct, so that I can start getting the texture right on the correct geometry.
    outColor = vec4(Color, 1.0) + texture(tex, TexCoord);
}

enter image description here


From the output I get, I conclude that OpenGL somehow having hard time interpreting the vertex array with the given attribute stride and offset parameters. What causes this - what am I missing - and how do I get this right?

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

As /u/slime73 pointed out, you are missing , after your T coordinates.

float vertices[] =                      
{//   X      Y       R     G     B       S     T
    -0.5f, +0.5f,   1.0f, 0.0f, 0.0f,   0.0f, 0.0f     // Top-left
    +0.5f, +0.5f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f      // Top-right
    +0.5f, -0.5f,   0.0f, 0.0f, 1.0f,   1.0f, 1.0f      // Bottom-right
    -0.5f, -0.5f,   1.0f, 1.0f, 1.0f,   0.0f, 1.0f      // Bottom-left
};

should be

float vertices[] =                      
{//   X      Y       R     G     B       S     T
    -0.5f, +0.5f,   1.0f, 0.0f, 0.0f,   0.0f, 0.0f,     // Top-left
    +0.5f, +0.5f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f,     // Top-right
    +0.5f, -0.5f,   0.0f, 0.0f, 1.0f,   1.0f, 1.0f,     // Bottom-right
    -0.5f, -0.5f,   1.0f, 1.0f, 1.0f,   0.0f, 1.0f,     // Bottom-left
};                                           //   ^ commas.
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .