Results 1 to 3 of 3

Thread: using glDrawElements() with VBO

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    6

    using glDrawElements() with VBO

    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:

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

  2. #2
    Junior Member
    Join Date
    Aug 2011
    Posts
    6

    Re: using glDrawElements() with VBO

    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.

  3. #3

    Re: using glDrawElements() with VBO

    Quote Originally Posted by ManuelP
    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.

Similar Threads

  1. Understanding glDrawElements()
    By maximus-dev in forum OpenGL ES general technical discussions
    Replies: 5
    Last Post: 11-25-2009, 01:59 PM
  2. glDrawElements usage
    By hukka in forum OpenGL ES general technical discussions
    Replies: 2
    Last Post: 05-14-2009, 03:51 AM
  3. about glDrawElements
    By freesui1984 in forum OpenGL ES general technical discussions
    Replies: 3
    Last Post: 10-10-2008, 03:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •