Hello Guest

Why is opengl not using the correct data for positions

  • 4 Replies
  • 75 Views
Why is opengl not using the correct data for positions
« on: September 13, 2020, 18:19:05 »
why is opengl not using the correct data for positions when using more than 1 attribute the vertex data looks like this:
Code: [Select]
float vertices[] = {
0.5f,  0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // top right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, // bottom left
-0.5f,  0.5f, 0.0f, 0.0f, 0.5f, 0.0f // top left
};
the first three values of each vertex are position values and the last three are colour values making the vbo vao and indexbuffer looks like this:
Code: [Select]
int EBO;
EBO = glGenBuffers();

int VAO;
VAO = glGenVertexArrays();
glBindVertexArray(VAO);

int VBO = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices, GL_STATIC_DRAW);

glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
glEnableVertexAttribArray(0);

glVertexAttribPointer(1, 3, GL_FLOAT, false, 0, 0);
glEnableVertexAttribArray(1);
and the indices are this:
Code: [Select]
int indices[] = {
0, 1, 3,   //first triangle
1, 2, 3    //second triangle
};
drawing looks like this:
Code: [Select]
shader.use();
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);

the vertex and fragment shader:
Code: [Select]
//vertex
#version 400 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;

out vec3 ourColor;

void main()
{
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
ourColor = aColor;
}

//fragment
#version 400 core
out vec4 FragColor;
in vec3 ourColor;

void main()
{
FragColor = vec4(ourColor, 1.0f);
}

The result should be 2 triangles forming a quad which is coloured with the colour values given in attribute 1
but currentely it looks like this:
https://drive.google.com/file/d/1ajSlSiSjpl-Ku6C53aQXFwKtzhSdIIWi/view?usp=sharing
i also dont know where the second triangle went
when i try it without the colour values it does what its supposed to do except without color as no colour given
How do i get opengl to use the correct values

*

Offline KaiHH

  • ****
  • 277
Re: Why is opengl not using the correct data for positions
« Reply #1 on: September 13, 2020, 18:42:11 »
why is opengl not using the correct data for positions when using more than 1 attribute the vertex data looks like this:
Code: [Select]
float vertices[] = {
0.5f,  0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // top right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, // bottom left
-0.5f,  0.5f, 0.0f, 0.0f, 0.5f, 0.0f // top left
};
The problem is your calls to glVertexAttribPointer:
Code: [Select]
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, false, 0, 0);
glEnableVertexAttribArray(1);
The second-to-last argument for glVertexAttribPointer is the stride, i.e. how many bytes are between an attribute of vertex i and the same attribute of vertex i+1. The last argument is the offset to the attribute of the first vertex.
The special values 0 for stride in your case means: "The number of bytes between two same vertex attributes is only 3 floats"
This is wrong. The stride is 6 floats. So, you must use `6 * Float.BYTES` for the stride in both calls and an offset of `3 * Float.BYTES` for the offset of the second call.

Reference: https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glVertexAttribPointer.xhtml

Re: Why is opengl not using the correct data for positions
« Reply #2 on: September 13, 2020, 19:06:58 »
why is opengl not using the correct data for positions when using more than 1 attribute the vertex data looks like this:
Code: [Select]
float vertices[] = {
0.5f,  0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // top right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, // bottom left
-0.5f,  0.5f, 0.0f, 0.0f, 0.5f, 0.0f // top left
};
The problem is your calls to glVertexAttribPointer:
Code: [Select]
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, false, 0, 0);
glEnableVertexAttribArray(1);
The second-to-last argument for glVertexAttribPointer is the stride, i.e. how many bytes are between an attribute of vertex i and the same attribute of vertex i+1. The last argument is the offset to the attribute of the first vertex.
The special values 0 for stride in your case means: "The number of bytes between two same vertex attributes is only 3 floats"
This is wrong. The stride is 6 floats. So, you must use `6 * Float.BYTES` for the stride in both calls and an offset of `3 * Float.BYTES` for the offset of the second call.

Reference: https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glVertexAttribPointer.xhtml

im pretty sure i read somewhere putting 0 in the stride lets opengl figure it out but apparentely that isnt the case thanks this acctually works i do wonder how i can calculate the stride in c++ that is easy but java dont has a function like sizeof() at least none that im aware of but yeah thanks for helping me

*

Offline KaiHH

  • ****
  • 277
Re: Why is opengl not using the correct data for positions
« Reply #3 on: September 13, 2020, 19:09:32 »
im pretty sure i read somewhere putting 0 in the stride lets opengl figure it out
Using 0 for stride only works for non-interleaved buffers (i.e. one buffer = one vertex attribute). Using 0 for stride simply means: that single vertex attribute is "tightly packed", that means: No other vertex attributes are interleaved between them.
This is what the spec says:
Quote
stride
Specifies the byte offset between consecutive generic vertex attributes. If stride is 0, the generic vertex attributes are understood to be tightly packed in the array. The initial value is 0.

Re: Why is opengl not using the correct data for positions
« Reply #4 on: September 13, 2020, 19:23:01 »
im pretty sure i read somewhere putting 0 in the stride lets opengl figure it out
Using 0 for stride only works for non-interleaved buffers (i.e. one buffer = one vertex attribute). Using 0 for stride simply means: that single vertex attribute is "tightly packed", that means: No other vertex attributes are interleaved between them.
This is what the spec says:
Quote
stride
Specifies the byte offset between consecutive generic vertex attributes. If stride is 0, the generic vertex attributes are understood to be tightly packed in the array. The initial value is 0.

oooh okay thanks for explaining it to me