PDA

View Full Version : Model not drawing when using Index Buffer Object



jitendra2896
08-04-2017, 02:22 PM
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


#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;
}

bootstrap
08-05-2017, 10:46 AM
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.

GClements
08-05-2017, 12:46 PM
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);




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().

jitendra2896
08-06-2017, 02:04 AM
Ohh!! Thanks that works.