c++ - Most basic working vbo example -


i want know simplest method using vbo's in opengl... have tried running few examples work clouded other information thats making confusing me... @ moment have

gluint vboid = 0; const int trisize = (m_tris.size()/2)*3;//m_tris index array verts , normals glfloat* vertices = new glfloat[trisize]; glfloat* normals = new glfloat[trisize];  int j=0; (int i=0; i<m_tris.size(); i+=2) {      normals[j] = m_normals[m_tris[i+1]*3];     vertices[j++] = m_vertices[m_tris[i]*3];     normals[j] = m_normals[m_tris[i+1]*3+1];     vertices[j++] = m_vertices[m_tris[i]*3+1];     normals[j] = m_normals[m_tris[i+1]*3+2];     vertices[j++] = m_vertices[m_tris[i]*3+2]; } //im pretty sure loop right used before display mesh correctly without vbo's using glvertex3f  glgenbuffersarb(1, &vboid); glbindbufferarb(gl_array_buffer_arb, vboid); glbufferdataarb(gl_array_buffer_arb, sizeof(vertices)+sizeof(normals), 0, gl_static_draw_arb); glbuffersubdataarb(gl_array_buffer_arb, 0, sizeof(vertices), vertices); glbuffersubdataarb(gl_array_buffer_arb, sizeof(vertices), sizeof(normals), normals);  glvertexpointer(sizeof(vertices), gl_float, 3, 0); glnormalpointer(gl_float, 3, (void*)sizeof(vertices)); 

in render method have

gldrawarrays(gl_triangles, 0, this->gettrinum()); //0 vboid? 

also have method runs once...

glenableclientstate(gl_vertex_array); glenableclientstate(gl_normal_array); 

when try run code "exc_bad_access"

any advice on im doing wrong... or simple implementation of vbo helpful

what doing looks right, except sizeof(vertices) gives size of address, not size of array. so, when call glbufferdata, allocating 8 bytes , when call glbuffersubdata, fill 8 bytes first 4 bytes of vertices array , first 4 bytes of normals array. then, when go call gldrawarrays, first indice causes array index out of bounds exception (hence exc_bad_access).

// data gluint geometry_array = 0; gluint indice_array = 0;  glfloat *geometry; gluint *indices; 

you can initialization differently, interleave data 32 bytes per vertice array. way choose set array works, long use glvertexpointer, glnormalpointer, , gltexcoordpointer correctly.

// initialize  geometry = new glfloat[8*num_geometry]; indices = new gluint[num_indices];  /* fill geometry: 0, 1, 2 = vertex_xyz   *                3, 4, 5 = normal_xyz  *                6, 7 = tex_coord_uv  */  glgenbuffers(1, &geometry_array); glbindbuffer(gl_array_buffer, geometry_array); glbufferdata(gl_array_buffer, sizeof(glfloat)*8*num_geometry, null, gl_dynamic_draw); glbuffersubdata(gl_array_buffer, 0, sizeof(glfloat)*8*num_geometry, geometry);  glgenbuffers(1, &indice_array); glbindbuffer(gl_element_array_buffer, indice_array); glbufferdata(gl_element_array_buffer, sizeof(gluint)*num_indices, null, gl_static_draw); glbuffersubdata(gl_element_array_buffer, 0, sizeof(gluint)*num_indices, indices); 

the render function 5 steps. first, bind buffers opengl knows find geometry , indice data. second, enable various client states each type of data. if leave 1 out, type of data not rendered, , if leave of them out, entire object not rendered. third, have tell opengl each type of data contained in array. in case 32 byte geometry data starts 12 bytes of vertex data, starting offset should 0 or null. since each vertex-normal-texcoord combo 32 bytes, step size. "sizeof(glfloat)8" says 32 bytes offset null, there vertice xyz group. in case of normals, each normal xyz group 12 bytes after offset vertice xyz group, hence "(float)(sizeof(glfloat)*3)" starting offset. again, step size 32 bytes. fourth, have tell draw of triangles associated indice array. finally, should disable client states, in case want use different method of rendering.

//render // step 1 glbindbuffer(gl_array_buffer, geometry_array); glbindbuffer(gl_element_array_buffer, indice_array);  // step 2 glenableclientstate(gl_texture_coord_array); glenableclientstate(gl_normal_array); glenableclientstate(gl_vertex_array);  // step 3 gltexcoordpointer(3, gl_float, sizeof(glfloat)*8, (float*)(sizeof(glfloat)*5)); glnormalpointer(gl_float, sizeof(glfloat)*8, (float*)(sizeof(glfloat)*3)); glvertexpointer(3, gl_float, sizeof(glfloat)*8, null);  // step 4 gldrawelements(gl_triangles, num_indices, gl_unsigned_int, null);  // step 5 gldisableclientstate(gl_vertex_array); gldisableclientstate(gl_normal_array); gldisableclientstate(gl_texture_coord_array); 

Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -