我正在尝试使用 OpenGL 3.3+ 绘制一个二十面体,所以我稍后可能会细分以使其成为一个球体,但我在 VS2010 Express 中不断收到此错误:
变量“_vertices”周围的堆栈已损坏
这是代码:
#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);
}
}
注意:我只放了绘图功能而不是所有东西,如果不使用索引数组,它会画一些奇怪的东西。谢谢