c++ - Generating a Sphere -


i'am trying draw icosahedron using opengl 3.3+ may subdivided later on make sphere, keep getting error in vs2010 express:

stack around variable '_vertices' corrupted

here code:

#include "angel.h"  int currentwidth = 800, currentheight = 600, windowhandle = 0;  unsigned framecount = 0; /*default values used draw object*/ float x = 0.525731112119133606f; float z = 0.850650808352039932f;  gluint   vertexshaderid,   fragmentshaderid,   programid,   vaoid,   vboid,   colorbufferid,   indexbufferid;  const glchar* vertexshader = {   "#version 400\n"\    "layout(location=0) in vec4 in_position;\n"\   "layout(location=1) in vec4 in_color;\n"\   "out vec4 ex_color;\n"\    "void main(void)\n"\   "{\n"\   " gl_position = in_position;\n"\   " ex_color = in_color;\n"\   "}\n"  };  const glchar* fragmentshader = {    "#version 400\n"\     "in vec4 ex_color;\n"\    "out vec4 out_color;\n"\     "void main(void)\n"\    "{\n"\    " out_color = vec4( 0.0, 0.0, 1.0, 1.0 );\n"\    "}\n" };  void initwindow(int argc, char* argv[]) {   glutinit(&argc, argv);    glutinitcontextversion(4, 0);   glutinitcontextflags(glut_forward_compatible);   glutinitcontextprofile(glut_core_profile);    glutsetoption(     glut_action_on_window_close,     glut_action_glutmainloop_returns   );    glutinitwindowsize(currentwidth, currentheight);    glutinitdisplaymode(glut_depth | glut_double | glut_rgba);    windowhandle = glutcreatewindow(window_title_prefix);    if(windowhandle < 1) {      exit(exit_failure);   }    glutreshapefunc(resizefunction);   glutdisplayfunc(renderfunction);   glutidlefunc(idlefunction);   gluttimerfunc(0, timerfunction, 0);   glutclosefunc(cleanup); }  void renderfunction(void) {   ++framecount;    glclear(gl_color_buffer_bit | gl_depth_buffer_bit);    gldrawelements(gl_triangles, 60, gl_unsigned_byte, (glvoid*)0);    glutswapbuffers();   glutpostredisplay(); }  void createvbo(void) {   /*starting points*/   glfloat vertices[][4] =     {{-x,0.0f,z, 1.0}, {x,0.0f,z, 1.0}, {-x,0.0f,-z, 1.0}, {x,0.0f,-z, 1.0},     {0.0f,z,x, 1.0}, {0.0f,z,-x, 1.0}, {0.0f,-z,x, 1.0}, {0.0f,-z,-x, 1.0},     {z,x,0.0f, 1.0}, {-z,x,0.0f, 1.0}, {z,-x,0.0f, 1.0}, {-z,-x,0.0f, 1.0}};    /*incdices on how draw object using triangles*/   glubyte indices[] =     {1,4,0,     4,9,0,    4,5,9,    8,5,4,    1,8,4,      1,10,8,    10,3,8,   8,3,5,    3,2,5,    3,7,2,      3,10,7,    10,6,7,   6,11,7,   6,0,11,   6,1,0,      10,1,6,    11,0,9,   2,11,9,   5,2,9,    11,2,7};    /*filling points need draw start*/   glfloat _vertices[60];   (int = 0; < 60; ++i) {       _vertices[(3*i)+0] = vertices[indices[i]][0];       _vertices[(3*i)+1] = vertices[indices[i]][1];       _vertices[(3*i)+2] = vertices[indices[i]][2];   }   /*end of nodes generation*/   glenum errorcheckvalue = glgeterror();<br/>   glgenvertexarrays(1, &vaoid);   glbindvertexarray(vaoid);<br/>   glgenbuffers(1, &vboid);   glbindbuffer(gl_array_buffer, vboid);   glbufferdata(gl_array_buffer, sizeof(_vertices), _vertices, gl_static_draw);   glvertexattribpointer(0, 4, gl_float, gl_false, 0, 0);   glenablevertexattribarray(0);    glgenbuffers(1, &indexbufferid);   glbindbuffer(gl_element_array_buffer, indexbufferid);   glbufferdata(gl_element_array_buffer, sizeof(indices), indices, gl_static_draw);    errorcheckvalue = glgeterror();   if(errorcheckvalue != gl_no_error)   {      exit(-1);   } } 

note: put function of drawing , not everything,and without using indices array draw weird stuff. thanks

for (int = 0; < 60; ++i) {

should be

for (int = 0; < 20; ++i) {

otherwise, you're going out of bounds , modifying memory outside of array, hence, stack around array being corrupted.


Comments