I’m trying to render this model: https://postimg.org/image/4t4bn87pz/
Here is my result: https://postimg.org/image/etpfloya9/
I can’t figure out why it is connecting shapes that dont belong together. I separated all the data from the OBJ file and put them in arrays like…
static const GLfloat g_vertex_buffer_data[] = {
-1.557376f, 0.094970f, 0.171995f,
-1.565967f, 0.098142f, 0.171995f,
-1.557376f, 0.094970f, -0.048469f,
-1.565967f, 0.098142f, -0.048469f, ..... };
int indicies[] = {
1, 2, 3,
2, 4, 3,
3, 4, 5,
4, 6, 5,
5, 6, 7,
6, 8, 7, ..... };
static const GLfloat g_color_buffer_data[] = {
0.204205f, 0.204205f, 0.204205f,
0.204205f, 0.204205f, 0.204205f,
0.204205f, 0.204205f, 0.204205f,
0.204205f, 0.204205f, 0.204205f, ... };
static const GLfloat g_normal_buffer_data[] = {
-0.341900f, -0.939700f, -0.000000f,
0.000000f, 0.000000f, -1.000000f,
0.222000f, 0.609800f, -0.760800f, ... };
, I checked the data and it matches the OBJ file. Why would it not draw correctly? I also tried it without color buffers and it still connects things that should not be connected.
buffers:
//Vertex array object
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
//Vertex buffer
GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
//color buffer
GLuint colorbuffer;
glGenBuffers(1, &colorbuffer);
glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_color_buffer_data), g_color_buffer_data, GL_STATIC_DRAW);
//normal buffer
GLuint normalbuffer;
glGenBuffers(1, &normalbuffer);
glBindBuffer(GL_ARRAY_BUFFER, normalbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_normal_buffer_data), g_normal_buffer_data, GL_STATIC_DRAW);
//index buffer
GLuint elementbuffer;
glGenBuffers(1, &elementbuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementbuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indicies), indicies, GL_STATIC_DRAW);
rendering:
//vertex buffer
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
0, //index
3, //size
GL_FLOAT, //type
GL_FALSE, //normalized?
0, //stride
0 //array buffer offset
);
//normal buffer
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, normalbuffer);
glVertexAttribPointer(
2, //index
3, //size
GL_FLOAT, //type
GL_FALSE, //normalized?
0, //stride
0 //array buffer offset
);
//color buffer
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
glVertexAttribPointer(
1, //index
3, //size
GL_FLOAT, //type
GL_FALSE, //normalized?
0, //stride
0 //array buffer offset
);
glDrawElements(
GL_TRIANGLES, //mode
(sizeof(indicies)/sizeof(int)), //count
GL_UNSIGNED_INT, //type
(void*)0 //element array buffer offset
);
Not sure where I am going wrong, I can post more code if needed. Thanks