1

我试图让一个基本的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 或 glut 的东西,所以我开始评论事情以查明问题。注释掉整个 init() 函数后,我能够显示一个可爱的白框,直到我单击 X 才终止。我最终删除了 init 中的所有 glFunction,但仍然存在即时终止问题。毕竟,肇事者是这样的:

point3 points[NumPoints];

很困惑,我将 do_nothing() 写到我的 main 中(如上面所见):

void do_nothing(){
    const int NumPoints = 5000;
    point3 points[NumPoints];
    return;
}

我调用它而不是 init(),唉,即时程序终止。我不明白这么简单的事情怎么会造成如此多的痛苦。

要点3:

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(必须上课)

4

0 回答 0