PDA

View Full Version : using glDrawElements() with VBO



ManuelP
08-25-2011, 08:32 AM
Hello,

i want to render simple primatives in a view port using glDrawElements on VBO, but for hours i am now stuck with an empty viewport, where nothing is drawn at all.

I dont receive any error messages or have any other problems, but the viewport simple stays empty.

The test code i am using now looks similar to this:


#Used Shaders
const char* defaultFragmentShader = "\
varying lowp vec3 primaryColor;\
void main (void)\
{\
gl_FragColor = vec4(1.0, 0.0, 0.0, 0.0);\
}";

const char* defaultVertexShader = "\
attribute highp vec4 position;\
attribute lowp vec3 color;\
uniform mediump mat4 matrix;\
varying vec3 primaryColor; \
void main(void)\
{\
primaryColor = color;\
gl_Position = matrix * position;\
}";

#Shader program is created, linked, and referenced to the attributes and uniforms are acquired after linking

#Prepare Viewport
glViewport(this->x, this->y, this->width, this->height);
glScissor(this->x, this->y, this->width, this->height);
glClearColor(this->red, this->green, this->blue, this->alpha);
glClear(GL_COLOR_BUFFER_BIT);

#set shader program
glUseProgram(this->program);

//Generate Buffer index
glGenBuffers(3, (GLuint*) this->bufferIndex);
if(glIsBuffer(this->bufferIndex[0]) == false) throw "Could not generate Vertex Buffer!";
if(glIsBuffer(this->bufferIndex[1]) == false) throw "Could not generate Color Buffer!";
if(glIsBuffer(this->bufferIndex[2]) == false) throw "Could not generate Index Buffer!";

#Load vertex position, color and index information into the buffers ,
glBindBuffer(GL_ARRAY_BUFFER, this->bufferIndex[0]); //Set first index to be Array Buffer (sets it also to the active VertexBuffer)
if (!OGLESTools::TestEGLError("glBindBuffer")){ throw "Binding VOB failed!"; }
glBufferData(GL_ARRAY_BUFFER, this->shaderProgram->getVertexSize() * sizeof(GL_FLOAT) * this->nVertices, this->vertices, GL_STATIC_DRAW); //Loads numVertices from *vertexSource into the active VertexBuffer
if (!OGLESTools::TestEGLError("glBufferData")){ throw "Loading VOB failed!"; }
glVertexAttribPointer( this->shaderProgram->getVertexLocation() , this->shaderProgram->getVertexSize() , GL_FLOAT, GL_FALSE, 0, 0 );

glBindBuffer(GL_ARRAY_BUFFER, this->bufferIndex[1]); //Set first index to be Array Buffer (sets it also to the active VertexBuffer)
if (!OGLESTools::TestEGLError("glBindBuffer")){ throw "Binding VOB failed!"; }
glBufferData(GL_ARRAY_BUFFER, this->shaderProgram->getColorSize() * sizeof(GL_FLOAT) * this->nVertices, this->color, GL_STATIC_DRAW); //Loads numVertices from *vertexSource into the active VertexBuffer
if (!OGLESTools::TestEGLError("glBufferData")){ throw "Loading VOB failed!"; }
glVertexAttribPointer( this->shaderProgram->getColorLocation() , this->shaderProgram->getColorSize() , GL_FLOAT, GL_FALSE, 0, 0 );

//Prepare Vertex Index Buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->bufferIndex[2]); //Set first index to be Array Buffer (sets it also to the active VertexBuffer)
if (!OGLESTools::TestEGLError("glBindBuffer")){ throw "Binding VOB failed!"; }
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLushort) * this->nIndexes, this->indexes, GL_STATIC_DRAW); //Load the vertex index information
if (!OGLESTools::TestEGLError("glBufferData")){ throw "Loading VOB failed!"; }

#enable the Vertex Position and Color buffer

glEnableVertexAttribArray( this->shaderProgram->getVertexLocation() );
if (!OGLESTools::TestEGLError("glEnableVertexAttribArray")){ throw "glEnableVertexAttribArray failed!"; }

//glBindBuffer(GL_ARRAY_BUFFER, this->bufferIndex[1]); //Set first index to be Array Buffer (sets it also to the active VertexBuffer)
// if (!OGLESTools::TestEGLError("glBindBuffer")){ throw "Binding VOB failed!"; }
glEnableVertexAttribArray( this->shaderProgram->getColorLocation() );
if (!OGLESTools::TestEGLError("glEnableVertexAttribArray")){ throw "glEnableVertexAttribArray failed!"; }

#Call glDrawElements ( there are three vertex defined, with position,color, index )
glDrawElements(GL_LINE_LOOP, 3, GL_UNSIGNED_SHORT, (void*) 0);

glDisableVertexAttribArray( this->shaderProgram->getVertexLocation() );
glDisableVertexAttribArray( this->shaderProgram->getColorLocation() );

#Flip Buffers
eglSwapBuffers(eglDisplay, eglSurface);


Does this look alright? Anyone has an idea what could be wrong?
Thanks!

ManuelP
08-26-2011, 07:40 AM
Hi!

the problem i had had nothing to do with the GL code, but with an pthread specific setup i am using in my program.

I do have another problem with code though.

I wrote the program in this way, that each graphic object has its data structure, storing the vertex information in local buffers, and having references to VBO. The vertex information is changed (number of vertixes and their attributes quite often (can be more often than the object is drawn on the screen), and if such update happened between the rendering of two frames, i update the information in the VBOs by calling glBufferData() and glVertexAttribPointer() to update the VBOs.

The problem is if i render a "view" frames without updating the VBOs (because there has been no change in the graphic object) i get segment faults or display errors. Do the VBOs get corrupted somehow? Are they only valid for one rendering?

I hope someone has an answer to that cause i guess loading data into the VBOs for each frame is quite slow.

abhijeet9888
05-10-2012, 12:15 AM
Hi!

the problem i had had nothing to do with the GL code, but with an pthread specific setup i am using in my program.

I do have another problem with code though.

I wrote the program in this way, that each graphic object has its data structure, storing the vertex information in local buffers, and having references to VBO. The vertex information is changed (number of vertixes and their attributes quite often (can be more often than the object is drawn on the screen), and if such update happened between the rendering of two frames, i update the information in the VBOs by calling glBufferData() and glVertexAttribPointer() to update the VBOs.

The problem is if i render a "view" frames without updating the VBOs (because there has been no change in the graphic object) i get segment faults or display errors. Do the VBOs get corrupted somehow? Are they only valid for one rendering?

I hope someone has an answer to that cause i guess loading data into the VBOs for each frame is quite slow.

Hi
If you want to update VBO data why not use glBufferSubData ??
This API will update specific values (or entire array as well).
With this API there wont be any need to change vertex information in local buffers.
AFAIK VBO are not limited to one rendering. Even if you delete a VBO you can always reuse them for storing same or different attribute/indices.