Drawing multiple objects with glDrawArrays - c++

UPDATE: Upon further investigation, it seems to be switching the program that causes the 2nd object to not be drawn. I don't know why. Both objects use essentially the same GLSL.
I have a hunch I'm not using buffers right. I have a cube and a prism defined. If I comment out the prism's draw call, the cube draws. otherwise only the prism draws. What am I missing here
Draw Code:
glUseProgram(cubeProgram);
glBindBuffer(GL_ARRAY_BUFFER, vertexArrayBuffers[0]);
glVertexAttribPointer(cVPos, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(cVPos);
glBindBuffer(GL_ARRAY_BUFFER, vertexArrayBuffers[1]);
glVertexAttribPointer(cNormID, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(cNormID);
glDrawArrays(GL_TRIANGLES, 0, 36);
glUseProgram(priProgram);
glBindBuffer(GL_ARRAY_BUFFER, vertexArrayBuffers[2]);
glVertexAttribPointer(pVPos, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(pVPos);
glBindBuffer(GL_ARRAY_BUFFER, vertexArrayBuffers[3]);
glVertexAttribPointer(pNormID, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(pNormID);
glDrawArrays(GL_TRIANGLES, 0, 64);
VBO Creation:
glGenBuffers(4, vertexArrayBuffers);
glBindBuffer(GL_ARRAY_BUFFER, vertexArrayBuffers[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVerts), cubeVerts, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vertexArrayBuffers[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(cubeNorms), cubeNorms, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vertexArrayBuffers[2]);
glBufferData(GL_ARRAY_BUFFER, sizeof(priVerts), priVerts, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vertexArrayBuffers[3]);
glBufferData(GL_ARRAY_BUFFER, sizeof(priNorms), priNorms, GL_STATIC_DRAW);
Let me know if there's more code needed.

So it was my GLSL causing the problem. I was calling a function to set variables for the shader and one of them seems to have unset. I recalled the function after drawing the first object and they both draw. Hooray!

Related

Proper way to use indexed VBO

I am working on a simple 3D Engine. I currently have a working setup with multiple VAO's which I can switch between during the render loop, but they all are not using index buffers.
I'm now trying to add a new VAO composed of 4 VBO's: vert position, color, normal and indices.
Everything compiles and runs but the drawing calls to the second VAO (with indexed vertices) do not render. I'm sure there is a problem with my setup somewhere, so I've added this code which includes all the VAO and VBO generations, calls, and uses. Does anything in this code seem wrong, and is this the correct way to set it all up?
VAO1 has 3 buffers: position, color, normals
VAO2 has 3 buffers: position, color, normals and vertex indices
//Initalize vaos and vbos
GLuint vao1, vbo1[3];
GLuint vao2, vbo2[4];
//Generate Vertex arrays:
glGenVertexArrays(1, &vao1);
glGenVertexArrays(1, &vao2);
//Generate Buffers:
glGenBuffers(3, vbo1);
glGenBuffers(4, vbo2);
//Initalize Bufferdata vectors:
vector<GLfloat> VertPosBuffer1Vector;
vector<GLfloat> VertNormalBuffer1Vector;
vector<GLfloat> VertColorBuffer1Vector;
vector<GLfloat> VertPosBuffer2Vector;
vector<GLfloat> VertNormalBuffer2Vector;
vector<GLfloat> VertColorBuffer2Vector;
vector<GLuint> VertIndexBuffer2Vector;
//Fill Buffers:
//(not included but all vectors are filled with data)
//VAO 1
glBindVertexArray(vao1);
//Vertex position buffer:
glBindBuffer(GL_ARRAY_BUFFER, vbo1[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*VertPosBuffer1Vector.size(), &VertPosBuffer1Vector[0], GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(0);
//Vertex color buffer:
glBindBuffer(GL_ARRAY_BUFFER, vbo1[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*VertColorBuffer1Vector.size(), &VertColorBuffer1Vector[0], GL_STATIC_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(1);
//Vertex normal buffer:
glBindBuffer(GL_ARRAY_BUFFER, vbo1[2]);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*VertNormalBuffer1Vector.size(), &VertNormalBuffer1Vector[0], GL_STATIC_DRAW);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(2);
//VAO 2
glBindVertexArray(vao2);
//Vertex position buffer:
glBindBuffer(GL_ARRAY_BUFFER, vbo2[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*VertPosBuffer2Vector.size(), &VertPosBuffer2Vector[0], GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(0);
//Vertex color buffer:
glBindBuffer(GL_ARRAY_BUFFER, vbo2[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*VertColorBuffer2Vector.size(), &VertColorBuffer2Vector[0], GL_STATIC_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(1);
//Vertex normal buffer:
glBindBuffer(GL_ARRAY_BUFFER, vbo2[2]);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*VertNormalBuffer2Vector.size(), &VertNormalBuffer2Vector[0], GL_STATIC_DRAW);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(2);
//Vertex index buffer:
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo2[3]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint)*VertIndexBuffer2Vector.size(), &VertIndexBuffer2Vector[0], GL_STATIC_DRAW);
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(3);
//unbind vao
glBindVertexArray(0);
//bind first vao
glBindVertexArray(vao1);
and
//RENDERLOOP
//render objects from vao1 using:
glDrawArrays(GL_TRIANGLES, start, size);
//switch vao
glBindVertexArray(0);
glBindVertexArray(vao2);
//render objects from vao2 using:
glDrawElements(
GL_TRIANGLES,
start,
GL_UNSIGNED_INT,
(void*)0
);
I have checked that the data in my buffers are correct.
Is it correct that the shader doesn't take in any information of indices? The shader will be the same as if I didn't use an index buffer?
Thank you
The indices are not a vertex attribute. So what you need to do is remove these two lines:
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(3);
I also noticed that you are using the variable "start" as the count argument for glDrawElements. I don't know the values of start and size, but I assume you should use "size" as the second argument in glDrawElements.

Drawing multiple objects in OpenGL with different buffers

With OpenGL shaders, I want to render two objects. Each is defined by a set of vertex positions and vertex indices for the triangles. When I make my buffers, I use the following code:
// Object 1 vertex positions
glBindBuffer(GL_ARRAY_BUFFER, object1_vertex_buffer);
glBufferData(GL_ARRAY_BUFFER, object1_vertices_size, object1_vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
// Object 1 vertex indices
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object1_index_buffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, object1_indices_size, object1_indices, GL_STATIC_DRAW);
// Object 2 vertex positions
glBindBuffer(GL_ARRAY_BUFFER, object2_vertex_buffer);
glBufferData(GL_ARRAY_BUFFER, object2_vertices_size, object2_vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
// Object 2 vertex indices
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object2_index_buffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, object2_indices_size, object2_indices, GL_STATIC_DRAW);
And then when I render my scene, I use the following code:
// Object 1
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object1_index_buffer);
glDrawElements(GL_TRIANGLES, object1_num_indices, GL_UNSIGNED_INT, (void*)0);
// Object 2
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object2_index_buffer);
glDrawElements(GL_TRIANGLES, object2_num_indices, GL_UNSIGNED_INT, (void*)0);
However, this results in only object 2 being drawn. What am I doing wrong?
If you have openGL 3.3+ you should use a VAO:
glBindVertexArray(vao1);
// Object 1 vertex positions
glBindBuffer(GL_ARRAY_BUFFER, object1_vertex_buffer);
glBufferData(GL_ARRAY_BUFFER, object1_vertices_size, object1_vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
// Object 1 vertex indices
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object1_index_buffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, object1_indices_size, object1_indices, GL_STATIC_DRAW);
glBindVertexArray(vao2);
// Object 2 vertex positions
glBindBuffer(GL_ARRAY_BUFFER, object2_vertex_buffer);
glBufferData(GL_ARRAY_BUFFER, object2_vertices_size, object2_vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
// Object 2 vertex indices
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object2_index_buffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, object2_indices_size, object2_indices, GL_STATIC_DRAW);
and then you only need to bind the right vao when drawing:
// Object 1
glBindVertexArray(vao1);
glDrawElements(GL_TRIANGLES, object1_num_indices, GL_UNSIGNED_INT, (void*)0);
// Object 2
glBindVertexArray(vao2);
glDrawElements(GL_TRIANGLES, object2_num_indices, GL_UNSIGNED_INT, (void*)0);
otherwise you will need to repeat the bind and glVertexAttribPointer calls between draws:
// Object 1
glBindBuffer(GL_ARRAY_BUFFER, object1_vertex_buffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object1_index_buffer);
glDrawElements(GL_TRIANGLES, object1_num_indices, GL_UNSIGNED_INT, (void*)0);
// Object 2
glBindBuffer(GL_ARRAY_BUFFER, object2_vertex_buffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object2_index_buffer);
glDrawElements(GL_TRIANGLES, object2_num_indices, GL_UNSIGNED_INT, (void*)0);
If this is all the code present, then there are several problems:
One has to enable vertex attributes by calling glEnableVertexAttribArray.
In the drawing code you are only rebinding the index buffer, but not the vertex buffer. Both, glBindBuffer(GL_ARRAY_BUFFER,... and glVertexAttribPointer change the current state, so you are overriding the first objects setting with the second ones. If you really want to work without VAOs, you'll have to bind the correct buffer before each draw call and update the glVertexAttribPointer
There is no VAO in use (VAOs are mandatory when working in Core profile but I would recommend using them also in compatibility mode)

Should I use the same VBO for passing different vertex attributes? Or should I use 2?

I know the glVertexAttribPointer will use the values from the VBO that was bound when it was called. But can you buffer twice onto the same object? Would it replace what was in? Or can you clear a buffer? I don't know if this approach is correct:
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO); // shared VBO
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(posLoc);
glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 3*sizeof(GLfloat),(GLvoid*)0);
glBufferData(GL_ARRAY_BUFFER, sizeof(colours), colours, GL_STATIC_DRAW);
glEnableVertexAttribArray(colLoc);
glVertexAttribPointer(colLoc, 4, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat),(GLvoid*)0);
glBindBuffer(GL_ARRAY_VERTEX, 0);
glBindVertexArray(0);
Or if I should be using 2 VBOs for buffering the data. What would happen if you call the glBufferData function twice to the same bound vertex array object? This is the other way I would think of for doing this:
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO1); // VBO for vertices
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(posLoc);
glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 3*sizeof(GLfloat),(GLvoid*)0);
glBindBuffer(GL_ARRAY_BUFFER, VBO2); // VBO for colours
glBufferData(GL_ARRAY_BUFFER, sizeof(colours), colours, GL_STATIC_DRAW);
glEnableVertexAttribArray(colLoc);
glVertexAttribPointer(colLoc, 4, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat),(GLvoid*)0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
The top example won't work as the second glBufferData call will overwrite all of the buffer space in the second one. To properly do that, you have to use the stride and pointer arguments properly, so that the data is interleaved. It's easier (and cleaner imo) to just have multiple VBO's, each storing a separate set of data.

Opengl 2.0 with multiple VAO

I was trying to setup a 2d screen with two layers of drawing
1) a grid made of white solid lines
2) tiles made of colored square
here are my opengl codes:
setup grid
//glGenBuffers(1, &gridVAO);
glGenVertexArrays(1, &gridVAO); // **updated**
glBindVertexArray(gridVAO);
glGenBuffers(2, gridVBO);
glBindBuffer(GL_ARRAY_BUFFER, gridVBO[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(gridPos), gridPos, GL_STATIC_DRAW);
glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, gridVBO[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(gridColors), gridColors, GL_STATIC_DRAW);
glVertexAttribPointer(vColor, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
setup tile
// glGenBuffers(1, &tileVAO);
glGenVertexArrays(1, &tileVAO); //**updated**
glBindVertexArray(tileVAO);
glGenBuffers(2, tileVBO);
glBindBuffer(GL_ARRAY_BUFFER, tileVBO[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(tilePos), tilePos, GL_STATIC_DRAW);
glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, tileVBO[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(tileColors), tileColors, GL_STATIC_DRAW);
glVertexAttribPointer(vColor, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
draw tiles
glBindVertexArray(tileVAO);
glDrawArrays(GL_TRIANGLES, 0, TILE_COUNT_X * TILE_COUNT_Y * 6);
glBindVertexArray(0);
draw grid
glBindVertexArray(gridVAO);
glDrawArrays(GL_LINES, 0, girdVertexCount);
glBindVertexArray(0);
by keeping only setup+draw grid produces this:
by keeping only setup+draw tiles produces this:
The VAOs are working properly by themselves.
However, magic begins here:
If I do the combination of
setup grid
setup tiles
// draw tiles
draw grid
It produces this:
Looks like the draw grid codes pick up buffers from tile's VAO. Help Please :D
update:
after corrected to glGenVertexArrays the same problem still happens
but i found both VAOs are 0
cout<<"gridVAO "<<gridVAO<<endl;
cout<<"tileVAO "<<tileVAO<<endl;
output:
gridVAO 0
tileVAO 0

VBO verses Vertex Array, vertex normals issue in VBO

I don't know what's wrong here, everything is working fine except normals.
When I use vertex array, model looks perfect but when I switch to VBO, model looks worse because of vertex normals. I spent a lot of time to fix it but do not know what's wrong. VBO generation seems perfect. but still do not know.
any idea?
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
void InitVBO(){
glGenBuffers(1, &vboNormID);
glBindBuffer(GL_ARRAY_BUFFER, vboNormID);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLpoint)*nb_Vertices, NULL, GL_DYNAMIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(GLpoint)*nb_Vertices, VertNormals);
glNormalPointer(GL_FLOAT, sizeof(GLpoint), BUFFER_OFFSET(12));
glGenBuffers(1, &vboVertID);
glBindBuffer(GL_ARRAY_BUFFER, vboVertID);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLpoint)*nb_Vertices, NULL, GL_DYNAMIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(GLpoint)*nb_Vertices, p_VERTICES);
glVertexPointer(3, GL_FLOAT, sizeof(GLpoint), BUFFER_OFFSET(0));
glGenBuffers(1, &indexVBOID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBOID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLFace)*nb_Faces, NULL, GL_DYNAMIC_DRAW);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, sizeof(GLFace)*nb_Faces, p_indices);
//glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLFace)*nb_Faces, p_indices, GL_DYNAMIC_DRAW);}
Rendering code is followed. VBO + Vertex Array
Vertex array is working perfect. I can see the perfect shape of model with vertex normals, but with VBO there is some issue with vertex normals. I think I am doing something wrong with BUFFER_OFFSET(12).
void RenderTringularModel(GLvoid){
if(VertNormals && !MESH_SMOOTH)
{
glBindBuffer(GL_ARRAY_BUFFER, vboNormID);
glBindBuffer(GL_ARRAY_BUFFER, vboVertID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBOID);
glPushClientAttrib( GL_CLIENT_VERTEX_ARRAY_BIT );
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glNormalPointer(GL_FLOAT, sizeof(GLpoint), BUFFER_OFFSET(12));
glVertexPointer(3, GL_FLOAT, sizeof(GLpoint), BUFFER_OFFSET(0));
glDrawElements(GL_TRIANGLES, nb_Faces*3, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glPopClientAttrib();
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
else
{
//glShadeModel (GL_FLAT);
glPushClientAttrib( GL_CLIENT_VERTEX_ARRAY_BIT );
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(GLpoint), p_VERTICES);
glNormalPointer(GL_FLOAT, sizeof(GLpoint), VertNormals);
glDrawElements(GL_TRIANGLES, 3*nb_Faces, GL_UNSIGNED_INT, p_indices);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glPopClientAttrib();
}
}
UPDATE 1:
I think you are talking about InitVBO.. I was wrong at that point. How about this...But it is also not working.. actually the issue it.. in many ways i tried to bind the vertex normals in InitVBO().. but result comes the same every time.
glGenBuffers(1, &vboVertID);
glBindBuffer(GL_ARRAY_BUFFER, vboVertID);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLpoint)*nb_Vertices, NULL, GL_DYNAMIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(GLpoint)*nb_Vertices, p_VERTICES);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(GLpoint)*nb_Vertices, sizeof(GLpoint)*nb_Vertices, VertNormals);
glVertexPointer(3, GL_FLOAT, sizeof(GLpoint), BUFFER_OFFSET(0));
glNormalPointer(GL_FLOAT, sizeof(GLpoint), BUFFER_OFFSET(12));
Update 2:
Ok ... i don't bind the vertex normals (vboNormID). I am discarding it. is it ok now ? Now it should work.. Actually i have done everything in that way.. to bind the vertex normals with vboVertID and with vboNormID etc etc.. but not every single mathod is working.. give me any suggestion by analyzing this code.
void RenderTringularModel(GLvoid){
if(VertNormals && !MESH_SMOOTH)
{
glBindBuffer(GL_ARRAY_BUFFER, vboVertID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBOID);
glPushClientAttrib( GL_CLIENT_VERTEX_ARRAY_BIT );
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glNormalPointer(GL_FLOAT, sizeof(GLpoint), BUFFER_OFFSET(12));
glVertexPointer(3, GL_FLOAT, sizeof(GLpoint), BUFFER_OFFSET(0));
glDrawElements(GL_TRIANGLES, nb_Faces*3, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
Update 3:
I don't know who told me to use BUFFER_OFFSET()... just crap.. I have fixed it now, now working fine without this stupid BUFFER_OFFSET()..
Now the fixed code it really simple as follows:
void InitVBO(){
glGenBuffers(1, &vboVertID);
glBindBuffer(GL_ARRAY_BUFFER, vboVertID);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLpoint)*nb_Vertices, p_VERTICES, GL_DYNAMIC_DRAW);
glGenBuffers(1, &vboNormID);
glBindBuffer(GL_ARRAY_BUFFER, vboNormID);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLpoint)*nb_Vertices, VertNormals, GL_DYNAMIC_DRAW);
glGenBuffers(1, &indexVBOID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBOID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLFace)*nb_Faces, p_indices, GL_DYNAMIC_DRAW);}
void RenderTringularModel(GLpoint *P, GLpoint *Vn, GLFace *T, int nbF){
glPushClientAttrib( GL_CLIENT_VERTEX_ARRAY_BIT );
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, vboVertID);
glVertexPointer(3, GL_FLOAT, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, vboNormID);
glNormalPointer(GL_FLOAT, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBOID);
glDrawElements(GL_TRIANGLES, nb_Faces*3, GL_UNSIGNED_INT, 0);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glPopClientAttrib();}
Now guys kindly guide me how can I update these buffers in real-time. I mean to say I need to update the whole data in real-time, normals, vertices, triangles.
for example: after subdivision of triangles, i would need to update the triangles buffer with vertices and normals too.. and another thing is, during cutting simulation of model, i need to update the vertices position and normals, so i would need to update the buffer at that point too.. so tell what do i do for this kind of issue ? how can i make it up ? do i need to delete the buffers and regenerate again ? if yes then how ?