c++ - glDrawArrays() draws one more unwanted object automatically -


i'm trying learn modern opengl this site, i'm on vertex arrays. know can minimise opengl function calls using gldrawarrays instead of glvertex**() each vertex.

from point both functions should result in same output, i'm getting additional drawing didn't make through gldrawarrays().

here code (vertices , display() )

float triangles[]={ 0.0f, 5.0f, 0.0f,  -5.0f, -5.0f, -4.0f,  5.0f, -5.0f, -4.0f,  0.0f, 5.0f, 0.0f,  -5.0f, -5.0f, 4.0f,  5.0f, -5.0f, 4.0f,  0.0f, 5.0f, 0.0f,  -5.0f, -5.0f, -4.0f,  -5.0f, -5.0f, 4.0f,  0.0f, 5.0f, 0.0f,  5.0f, -5.0f, -4.0f,  5.0f, -5.0f, 4.0f,}; void display(void) { glclear (gl_color_buffer_bit | gl_depth_buffer_bit); glcolor3f(0.1f, 1.0f, 0.0f); glenableclientstate(gl_vertex_array); glvertexpointer(3, gl_float, 0, triangles); gldrawarrays(gl_triangles, 0, sizeof(triangles)); gldisableclientstate(gl_vertex_array); glflush (); // try commneting glflush() } 

and when run program, creates 3d triangle programmed, 2d square shape (which did not program). it?? dont know how opengl creates object without programmer's help?

your problem 3rd argument of gldrawarrays():

gldrawarrays(gl_triangles, 0, sizeof(triangles)); 

the value needs number of vertices, while passing size in bytes.

since have 12 vertices, argument value should 12. if want calculate data definition, each vertex consists of 3 array element, have divide total size 3 times size of array element:

gldrawarrays(gl_triangles, 0, sizeof(triangles) / (3 * sizeof(triangles[0])); 

Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -