MirzaSe
September 7, 2014, 6:48pm
1
I am trying to draw without vertex buffer object but nothing is drawn. I do not know where I did wrong. I use Qt and OpenGL ES 2.0. This is my code:
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,1);
glEnableVertexAttribArray(m_posAttr);
glEnableVertexAttribArray(m_distLocation);
glVertexAttribPointer(m_posAttr, 2, GL_FLOAT, GL_FALSE, vertices.size()*3 * sizeof(float), &vertices[0]);
int offset = 2 * sizeof(float);
glVertexAttribPointer(m_distLocation, 1, GL_FLOAT, GL_FALSE, 0, (void*)offset);
glBindAttribLocation(m_program.programId(), m_posAttr, "posAttr" );
glBindAttribLocation(m_program.programId(), m_distLocation, "distance" );
glUniform1i(m_patternLocation, -1);
glUniform4fv(m_colorAttr, 1, vColor);
glUniform1f(m_depthAttr, 0.2f);
glDrawElements(GL_TRIANGLES,indices.size(), GL_UNSIGNED_SHORT, &indices[0] );
where vertices are vector of size 3 containing x and y coordinate and distance for each coordinate. Distance is used to do some calculations in shader.
Any idea what I did wrong?
[QUOTE=MirzaSe;1261531]Any idea what I did wrong?[/QUOTE]Yep. Here is the answer:[QUOTE=MirzaSe;1261531]I am trying to draw without vertex buffer object[/QUOTE]There must be a valid non-zero named VAO bound.
MirzaSe
September 8, 2014, 3:23am
3
I am a beginner and I wonder how to change it.
Osbios
September 8, 2014, 4:56am
4
Just use a single VAO that you create/bind at the beginning of your program after creating the gl context.
[code=“cpp”]GLuint lonelyVao;
glGenVertexArrays(1, &lonelyVao);
glBindVertexArray(lonelyVao)
Edit: never mind that, you don’t need VAOs in ES 2.0. MirzaSe use of the word VAO for Vertex B uffer Object confused my brain…
This is trying to bind the buffer with the ID 1 as element source. Try to change it to 0.
[code=“cpp”]glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,1);
MirzaSe
September 8, 2014, 7:13am
5
[QUOTE=Osbios;1261542] Try to change it to 0.
[code=“cpp”]glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,1);
[/QUOTE]
I tried, but it is the same. Thing is that I have in vertices x and y position coordinate and z is another attribute variable used for computing something in vertex shader for each position.
Osbios
September 8, 2014, 9:13am
6
I have no idea what you are talking about.
But here is how I understand it:
You have your data defined like this:
[code=“cpp”]#pragma pack(push)
#pragma pack(1)
struct xy_dist
{
float x;
float y;
float dist;
};
#pragma pack(pop)
vector<xy_dist> vertices;
vector<char> indices;
Then your vertex layout code should look like this:
[code="cpp"]glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glEnableVertexAttribArray(m_posAttr);
glEnableVertexAttribArray(m_distLocation);
int posAttrSize = sizeof(float) * 2;
int distLocationAttrSize = sizeof(float) * 1;
GLintptr posAttrStart = reinterpret_cast<GLintptr>(&vertices[0]);
GLintptr distLocationAttrStart = posAttrStart + posAttrSize;
int stride = posAttrSize + distLocationAttrSize;
glVertexAttribPointer(m_posAttr, 2, GL_FLOAT, GL_FALSE, stride, reinterpret_cast<GLvoid*>(posAttrStart));
glVertexAttribPointer(m_distLocation, 1, GL_FLOAT, GL_FALSE, stride, reinterpret_cast<GLvoid*>(distLocationAttrStart));
glBindAttribLocation(m_program.programId(), m_posAttr, "posAttr" );
glBindAttribLocation(m_program.programId(), m_distLocation, "distance" );
glUniform1i(m_patternLocation, -1);
glUniform4fv(m_colorAttr, 1, vColor);
glUniform1f(m_depthAttr, 0.2f);
glDrawElements(GL_TRIANGLES,indices.size(), GL_UNSIGNED_SHORT, &indices[0] );
In your original code the second glVertexAttribPointer still locked more like what you would use with a VBO. But it needs an exact memory address and not only the offset. Also the stride must be the same value. But I’m still not sure if you want the pos and distLocation from the same array. Because in that case you could just use a single attribute instead of two.
MirzaSe
September 8, 2014, 11:32am
7
This is exacltly what I wanted. Thank you a lot. This was very helpful. Thank you.
MirzaSe
September 8, 2014, 2:52pm
8
[QUOTE=Osbios;1261549]I have no idea what you are talking about.
But here is how I understand it:
You have your data defined like this:
[code=“cpp”]#pragma pack(push)
#pragma pack(1)
struct xy_dist
{
float x;
float y;
float dist;
};
#pragma pack(pop)
vector<xy_dist> vertices;
vector<char> indices;
Then your vertex layout code should look like this:
[code="cpp"]glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glEnableVertexAttribArray(m_posAttr);
glEnableVertexAttribArray(m_distLocation);
int posAttrSize = sizeof(float) * 2;
int distLocationAttrSize = sizeof(float) * 1;
GLintptr posAttrStart = reinterpret_cast<GLintptr>(&vertices[0]);
GLintptr distLocationAttrStart = posAttrStart + posAttrSize;
int stride = posAttrSize + distLocationAttrSize;
glVertexAttribPointer(m_posAttr, 2, GL_FLOAT, GL_FALSE, stride, reinterpret_cast<GLvoid*>(posAttrStart));
glVertexAttribPointer(m_distLocation, 1, GL_FLOAT, GL_FALSE, stride, reinterpret_cast<GLvoid*>(distLocationAttrStart));
glBindAttribLocation(m_program.programId(), m_posAttr, "posAttr" );
glBindAttribLocation(m_program.programId(), m_distLocation, "distance" );
glUniform1i(m_patternLocation, -1);
glUniform4fv(m_colorAttr, 1, vColor);
glUniform1f(m_depthAttr, 0.2f);
glDrawElements(GL_TRIANGLES,indices.size(), GL_UNSIGNED_SHORT, &indices[0] );
In your original code the second glVertexAttribPointer still locked more like what you would use with a VBO. But it needs an exact memory address and not only the offset. Also the stride must be the same value. But I’m still not sure if you want the pos and distLocation from the same array. Because in that case you could just use a single attribute instead of two.[/QUOTE] i
I have one more question. What if I want just to enable distance attribute and not send anything there?