PDA

View Full Version : Drawing without Vertex Buffer Object



MirzaSe
09-07-2014, 07:48 PM
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?

Yandersen
09-08-2014, 03:54 AM
Any idea what I did wrong?Yep. Here is the answer:
I am trying to draw without vertex buffer objectThere must be a valid non-zero named VAO bound.

MirzaSe
09-08-2014, 04:23 AM
Yep. Here is the answer:There must be a valid non-zero named VAO bound.

I am a beginner and I wonder how to change it.

Osbios
09-08-2014, 05:56 AM
Just use a single VAO that you create/bind at the beginning of your program after creating the gl context.


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 Buffer Object confused my brain...

This is trying to bind the buffer with the ID 1 as element source. Try to change it to 0.

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,1);

MirzaSe
09-08-2014, 08:13 AM
Try to change it to 0.

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,1);

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
09-08-2014, 10:13 AM
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.
I have no idea what you are talking about.

But here is how I understand it:
You have your data defined like this:

#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:

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
09-08-2014, 12:32 PM
This is exacltly what I wanted. Thank you a lot. This was very helpful. Thank you.