我正在尝试使用OpenGL 3.3+绘制一个二十面体,所以我可以稍后再细分为一个球体,但我在VS2010 Express中一直收到这个错误:
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);
}
}
注意:我只提供绘图功能,而不是所有内容,而且如果不使用Indices数组,它会绘制一些奇怪的东西。感谢
答案 0 :(得分:1)
你正在写出数组边界。 _vertices
是一个包含60 GLfloat
个数组的数组,但您可以在0 .. 3*59 + 2
范围内访问它。
答案 1 :(得分:1)
for (int i = 0; i < 60; ++i) {
应该是
for (int i = 0; i < 20; ++i) {
否则,你将走出界限并修改数组外部的内存,因此,数组周围的堆栈正在被破坏。