我试图让一个基本的openGL程序运行。我昨天运行了这个代码,但是在另一台机器上,现在它在显示任何内容之前终止。这是我的init()函数:
void init()
{
//generate points
const int NumPoints = 5000;
point3 points[NumPoints];
point3 vertices[3] = {point3(-1.0, -1.0, 0.0),
point3(0.0, 1.0, 0.0),
point3(1.0, -1.0, 0.0)};
points[0] = point3(0.0, 0.0, 0.0);
for(int k = 1; k < NumPoints; k++)
{
int j = rand() % 3;
points[k] = (points[k-1]+vertices[j])/2.0;
}
//load shaders and use the resulting shader program
GLuint program = InitShader("shaders/vshader.glsl", "shaders/fshader.glsl");
glUseProgram( program );
//create Vertex-Array object
GLuint aBuffer;
glGenVertexArrays(1, &aBuffer);
glBindVertexArray((GLuint)&aBuffer);
//create Buffer object
GLuint buffer;
//glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(points),
points, GL_STATIC_DRAW);
//initialize the vertex position attribute from the vertex shader
GLuint loc = glGetAttribLocation( program, "vPosition");
glEnableVertexAttribArray( loc );
glVertexAttribPointer( loc, 3, GL_FLOAT, GL_FALSE, 0, 0);
glClearColor( 1.0, 1.0, 1.0, 1.0); // white background
}
这是我的主要()
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutDisplayFunc(display);
glutCreateWindow("Program 1");
glewInit();
//do_nothing();
init();
glutMainLoop();
return 0;
}
我认为我做了一些错误的opengl或过剩的东西,所以我开始评论问题以找出问题所在。整个init()函数被注释掉后,我能够显示一个可爱的白盒子,直到我点击X才终止。我最终删除了init中的所有glFunction并仍然有即时终止问题。在一切之后,行为人就是这一行:
point3 points[NumPoints];
困惑,我把do_nothing()写入了我的主要内容(如上所述):
void do_nothing(){
const int NumPoints = 5000;
point3 points[NumPoints];
return;
}
我调用了这个而不是init()和唉,即时程序终止。我不知道这么简单的事情会带来如此多的痛苦。
POINT3:
class point3
{
public:
GLfloat x;
GLfloat y;
GLfloat z;
point3();
point3(GLfloat, GLfloat, GLfloat);
~point3();
point3 operator+ (point3 param);
point3 operator- (point3 param);
point3 operator/ (GLfloat param);
point3 operator* (GLfloat param);
};
//
// constructiors and destructors
//
point3::point3(){
x = 0;
y = 0;
z = 0;
};
point3::point3(GLfloat a, GLfloat b, GLfloat c)
{
x = a;
y = b;
z = c;
}
我在Win7上使用Eclipse CDT和MinGW(必须上课)