2013-05-06 91 views
0

我'嘗試使用OpenGL 3.3+,所以我可以再後來,使其球體繪製二十面體,但我不斷收到這個錯誤在VS2010快遞:
生成球

Stack around the variable '_vertices' was corrupted

這裏驗證碼:

#include "Angel.h" 

int CurrentWidth = 800, 
CurrentHeight = 600, 
WindowHandle = 0; 

unsigned FrameCount = 0; 
/*Default values used to draw the 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 to draw the 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 up the points which i need to draw from the start*/ 
    GLfloat _vertices[60]; 
    for (int i = 0; i < 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); 
    } 
} 

注:我只把圖紙和不是萬能的,而且功能不使用指數陣列會得出一些奇怪的東西。謝謝

回答

1

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

應該

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

否則,你會出界和陣列外修改內存,因此,陣列周圍堆被損壞。

+0

我向你致謝:) – user2353966 2013-05-08 19:36:25

+0

非常歡迎 – 2013-05-08 19:38:14

1

你正在寫出數組邊界。 _vertices是一個60 GLfloat s的數組,但您正在訪問範圍0 .. 3*59 + 2