OpenGL:不能使用glu.h中的函数

时间:2017-03-27 05:28:34

标签: c++ visual-studio opengl

我正在尝试在Visual Studio 2013中测试一些示例代码。

如果我只使用glut.h和gl.h中的函数,它可以成功构建:

#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <glut.h>
static int year = 0, spin = 0, day = 0;
static GLint fogMode;
const int n = 100;
const GLfloat R = 1.0f;
const GLfloat Pi = 3.1415926536f;
void init(void)
{
    GLfloat position[] = { 0.5, 0.5, 3.0, 0.0 };
    glEnable(GL_DEPTH_TEST);                          
    glLightfv(GL_LIGHT0, GL_POSITION, position);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    {
        GLfloat mat[3] = { 0.1745, 0.01175, 0.01175 };
        glMaterialfv(GL_FRONT, GL_AMBIENT, mat);
        mat[0] = 0.61424; mat[1] = 0.04136; mat[2] = 0.04136;
        glMaterialfv(GL_FRONT, GL_DIFFUSE, mat);
        mat[0] = 0.727811; mat[1] = 0.626959; mat[2] = 0.626959;
        glMaterialfv(GL_FRONT, GL_SPECULAR, mat);
        glMaterialf(GL_FRONT, GL_SHININESS, 0.6*128.0);
    }
    glEnable(GL_FOG);
    {
        GLfloat fogColor[4] = { 0.5, 0.5, 0.5, 1.0 };
        fogMode = GL_EXP;
        glFogi(GL_FOG_MODE, fogMode);
        glFogfv(GL_FOG_COLOR, fogColor);
        glFogf(GL_FOG_DENSITY, 0.35);
        glHint(GL_FOG_HINT, GL_DONT_CARE);
        glFogf(GL_FOG_START, 1.0);
        glFogf(GL_FOG_END, 5.0);
    }
    glClearColor(0.5, 0.9, 0.9, 1.0); 
}
void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(0.0, 1.0, 1.0);
    glPushMatrix(); 
    glutSolidSphere(1.0, 20, 16);   
    glRotatef(spin, 0.0, 1.0, 0.0);  
    glTranslatef(2.0, 1.0, 0.0);
    glRotatef(spin, 1.0, 0.0, 0.0); 
    glRectf(0.1, 0.1, 0.5, 0.5);
    glColor3f(0.0, 0.0, 1.0);
    glutWireSphere(0.2, 8, 8);    
    glColor3f(1.0, 0.0, 0.0);
    glTranslatef(2.0, 1.0, 0.0);
    glRotatef(2 * spin, 0.0, 1.0, 0.0);
    glutSolidSphere(0.5, 16, 8);
    glPopMatrix();
    glutSwapBuffers();
}
void spinDisplay(void)
{
    spin = spin + 2;
    if (spin > 360)
        spin = spin - 360;
    glutPostRedisplay();
}
void mouse(int button, int state, int x, int y)
{
    switch (button)
    {
    case GLUT_LEFT_BUTTON:
        if (state == GLUT_DOWN)
            glutIdleFunc(spinDisplay);
        break;
    case GLUT_MIDDLE_BUTTON:
        if (state == GLUT_DOWN)
            glutIdleFunc(NULL);
        break;
    default:
        break;
    }
}
void reshape(int w, int h)
{
    glViewport(0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    //gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 0.5, 20.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    //gluLookAt(0.0, 10.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(400, 400);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("OpengGL Programming");
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMouseFunc(mouse);
    glutMainLoop();
    return 0;
}

但是,如果我想使用来自glu.h的gluPerspective或gluLookAt,它会说:

error LNK2001:unresolved external symbol _gluPerspective@32
error LNK2001:unresolved external symbol _gluLookAt@72

我已将所需目录添加到Include目录和库目录,以及glu32.lib / glaux.lib / glut32.lib / freeglut.lib / glew32.lib / opengl32.lib /链接器下的lib,但它仍然没有工作。

抱歉英语不好,如果我没有清楚地描述问题,请告诉我。

1 个答案:

答案 0 :(得分:1)

确保您添加在:(project right click -> properties-> configuration properties->linker->input

这两个库:opengl32.lib;glu32.lib;

你喜欢:

#include <gl/GLU.h>

更多 如果你想使用过剩库和头文件:

在此处添加头文件的路径:

project right click -> properties-> configuration properties->C/C++->gneral->Additional include directories

在此处添加库路径:

project right click -> properties-> configuration properties->linker->general

在此处添加库名称:

project right click -> properties-> configuration properties->linker->input

这对我有用。