使用opengl编译的程序,但不运行

时间:2014-12-03 15:44:43

标签: opengl codeblocks

我想要opengl代码:: blocks并使用下面的链接进行设置: http://www.deannicholls.co.uk/tutorials/show/cpp_glut

我尝试了示例文件:

#include <iostream>
#include <GL/glut.h>

using namespace std;

GLfloat mat_ambient[] = {0.5, 0.5, 0.5, 1.0};
GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0};
GLfloat mat_shininess[] = {50.0};
GLfloat light_position[] = {10.0, 10.0, 10.0, 0.0};
GLfloat model_ambient[] = {1.0, 0.5, 0.5, 1.0};

void setupMaterials() {
    glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, model_ambient);
}

void changeColour(GLfloat r, GLfloat g, GLfloat b, GLfloat A) {
    model_ambient[0] = r;
    model_ambient[1] = g;
    model_ambient[2] = b;
    model_ambient[3] = A;
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, model_ambient);
}

void init(void) {
    glClearColor(0.0, 0.0, 0.0, 1.0);
    setupMaterials();

    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
    glFrontFace(GL_CCW);
    glShadeModel(GL_SMOOTH);
}

void reshape(int w, int h) {
    GLfloat fAspect;
    if(h==0) h=1;

    glViewport(0,0,w,h);

    fAspect = (GLfloat)w / (GLfloat)h;

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    gluPerspective(60, fAspect, 0.5, 100.0);
    glTranslatef(0,0,-1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void display(void) {
    int slices = 30;
    int stacks = 30;
    float radius = 0.2;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();
    changeColour(0.0, 0.0, 1.0, 1.0);
    glTranslatef(0.0, 0.0, 0.1);
    glutSolidSphere(radius, slices, stacks);
    glPopMatrix();

    glFlush();
    glutSwapBuffers();
}

void keyboard(unsigned char key, int x, int y) {
    switch (key) {
        case 27:
            exit(0); // Exit the application if 'Esc' key is pressed
    }
}

void animate() {
    glutPostRedisplay();
}

int main(int argc, char * argv[]) {
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize (800, 600);
    glutCreateWindow (argv[0]);   init();
    glutKeyboardFunc (keyboard);
    glutDisplayFunc (display);
    glutReshapeFunc (reshape);
    glutIdleFunc(animate);
    glutMainLoop();
    return 0;
}

编译没有问题,但每当我尝试运行它时,我得到:

&#34;处理终止,状态为-1073741515(0分钟,2秒(s))&#34;

我已经发现它是由线引起的:

#include <GL/glut.h>

但我无法弄清楚为什么会造成这种情况。有人可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

我使用的是Windows7,我提到的网站已经明确说明了#34;如果您使用的是Windows 7 64位,则需要将此文件复制到&#39; C:\ Windows \ sysWOW64&#34;,我没有。当我向右迈出这一步时,我就让程序运行了!