2014-12-03 72 views
0

我想OpenGL的代码::块,使用下面的链接来设置它: http://www.deannicholls.co.uk/tutorials/show/cpp_glut计划用OpenGL编译,但不运行

我试过示例文件:

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

编译不用的问题,但每次我尝试运行它,我得到:

“进程终止状态-1073741515(0分钟,2秒(s))”

我已经知道它是由行引起的:

#include <GL/glut.h> 

但我找不出它为什么会造成这种情况。有人可以帮我吗?

回答

2

我使用Windows7和我提到的网站中特别提到的“如果你使用的是Windows 7 64位,你需要把这个文件复制到'C:\ Windows \ sysWOW64'中,我没有。当我这样做了,我得到了程序运行!

+0

非常好。我喜欢看到人们解决他们的问题! – 2014-12-03 17:36:09

+0

给那个网站的作者一个屁股给我。永远不要将DLL放入操作系统路径,这将真正混淆其他程序也使用该DLL但期望不同的版本。相反,只需将该DLL放在与.exe相同的目录中(或者,如果通过Visual Studio Debugger运行它,请将该DLL的副本放在项目目录中)。 – 2014-12-03 17:49:59