Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: Model not drawing when using Index Buffer Object

  1. #1
    Junior Member Newbie
    Join Date
    Jul 2017
    Posts
    6

    Model not drawing when using Index Buffer Object

    When I pass my index data directly in glDrawElements() everything works fine. But when I use vao and vbo the model isn't drawn at all.

    If you remove vao and vbo code and uncomment the commented it works fine.

    Below is the code

    Code :
    #include<GL\glew.h>
    #include<GL\freeglut.h>
    #include"OBJFileParser.h"
     
    GLfloat *vertexData;
    GLuint* indices;
    int size;
    ObjParser* parser = ObjParser::getObjParser();
     
    void display() {
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    	glLoadIdentity();
    	glTranslatef(0, 0, -8);
    	glDrawElements(GL_TRIANGLES,size,GL_UNSIGNED_INT, (void*)0);
     
    	//This works fine
    	//glDrawElements(GL_TRIANGLES, size, GL_UNSIGNED_INT, indices);
    	glutSwapBuffers();
    }
     
    void init() {
    	glClearColor(0, 0, 0, 0);
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
    	gluPerspective(50, 1, 5, 20);
    	glMatrixMode(GL_MODELVIEW);
    	glLoadIdentity();
     
    	//Just parsing the obj file
    	parser->parseObj("file.txt");
    	vertexData = parser->getVertexData();
    	indices = parser->getIndices();
    	size = parser->getIndicesSize();
     
    	GLuint vaoId;
    	GLuint vboId[2];
     
    	glGenVertexArrays(1, &vaoId);
    	glBindVertexArray(vaoId);
    	glGenBuffers(2, vboId);
    	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboId[0]);
    	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
     
    	glBindBuffer(GL_ARRAY_BUFFER, vboId[1]);
    	glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);
    	glVertexPointer(3, GL_FLOAT, 0, 0);
    	glEnableClientState(GL_VERTEX_ARRAY);
     
    	//This works fine
    	/*glVertexPointer(3, GL_FLOAT, 0, vertexData);
    	glEnableClientState(GL_VERTEX_ARRAY);*/
    }
     
    int main(int argc, char** argv) {
    	glutInit(&argc, argv);
    	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    	glutInitWindowSize(500, 500);
    	glutInitWindowPosition(10, 10);
    	glutCreateWindow("My Window");
    	glewInit();
    	init();
    	glutDisplayFunc(display);
    	glutMainLoop();
    	return 0;
    }

  2. #2
    Junior Member Regular Contributor
    Join Date
    Jan 2008
    Location
    phobos, mars
    Posts
    100
    I didn't look closely, but isn't sizeof(vertexData) tiny, like 4 bytes? Don't you need something like (vertex_count * sizeof(vertexData)) ??? And similar for the IBO?

    Also, my vague recollection is, for some odd reason you need to create VAOs before the IBO and VBO associated with the VAO. Looks like you do that.

    I can't imagine why setting the last argument of glDrawElements() to "indices" makes any sense.

    Always beware of the possibility it is drawing, but is not visible for some reason (outside of frustum or black color or something).

    Where are vertex attributes specified? You need to breakpoint and look at the arguments to all the data copy functions and the draw function.

    I'm probably not giving good advice here... too long since I wrote and tested my VAO/IBO/VBO code.
    Last edited by bootstrap; 08-05-2017 at 12:12 PM.

  3. #3
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,519
    Quote Originally Posted by jitendra2896 View Post
    Code :
    	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

    Code :
    	glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);
    You can't use sizeof to obtain the size of a dynamically-allocated array; it will return the size of the pointer (typically 4 bytes or 8 bytes).

    So the version using buffers is copying hardly any data to the buffers.

    Hopefully the ObjParser class returns the size of the vertex array (it wouldn't be much use if it didn't). It appears to return the size of the index array, but you aren't using it when copying data to the index buffer, only when calling glDrawElements().

  4. #4
    Junior Member Newbie
    Join Date
    Jul 2017
    Posts
    6
    Ohh!! Thanks that works.

Tags for this Thread

Posting Permissions

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