Program with opengl compiles, but doesn't run

二次信任 提交于 2021-01-29 07:50:44

问题


I wanted opengl for code::blocks and used the link below to set it up: http://www.deannicholls.co.uk/tutorials/show/cpp_glut

I tried the example file:

#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;
}

Compiling goes without problems, but everytime I try to run it I get:

"Process terminated with status -1073741515 (0 minute(s), 2 second(s))"

I already figured out that it's caused by the line:

#include <GL/glut.h>

But I could not figure out why it's causing this. Can someone help me out?


回答1:


I use Windows7 and the site I mentioned specifically stated "If you're using Windows 7 64-bit, you'll need to copy this file into 'C:\Windows\sysWOW64", which I didn't. When I did that step right I got the program to run!



来源:https://stackoverflow.com/questions/27275823/program-with-opengl-compiles-but-doesnt-run

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!