C ++ OpenGL:调用gluPerspective会抛出一个未定义的引用错误?

时间:2013-02-10 00:56:31

标签: c++ opengl perspective freeglut glu

我正在使用FreeGLUT尝试使用OpenGL在C ++中创建我的第一个多维数据集。我有一个问题,每当我调用“gluPerspective”时,编译器都会抛出此错误:

build/Debug/MinGW-Windows/main.o: In function `main':
C:\Users\User\Dropbox\NetBeans Workspace\Testing/main.cpp:47: undefined reference to `gluPerspective@32'

我环顾四周,看看有没有人遇到过这个问题,什么都没发现。所以,我认为我再次忘记了一些事情。这是我调用函数的地方:

......
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(45, 1.333, 1, 1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
......

我包含freeGLUT,除了该行之外的所有其他工作。我检查了文档,看起来好像我正确使用它。我很茫然。

1 个答案:

答案 0 :(得分:2)

gluPerspective已从版本3.1的GLU(OpenGL帮助程序库)中删除。您是否正在编译仍然定义的正确库?如果没有,那么您将需要编写自己的版本并将矩阵直接传递给OpenGL。

OpenGL.org在其网站上有the gluPerspective code(此处为完整性而提供):

//matrix will receive the calculated perspective matrix.
//You would have to upload to your shader
// or use glLoadMatrixf if you aren't using shaders.
void glhPerspectivef2(float *matrix, float fovyInDegrees, float aspectRatio,
                      float znear, float zfar)
{
    float ymax, xmax;
    float temp, temp2, temp3, temp4;
    ymax = znear * tanf(fovyInDegrees * M_PI / 360.0);
    //ymin = -ymax;
    //xmin = -ymax * aspectRatio;
    xmax = ymax * aspectRatio;
    glhFrustumf2(matrix, -xmax, xmax, -ymax, ymax, znear, zfar);
}
void glhFrustumf2(float *matrix, float left, float right, float bottom, float top,
                  float znear, float zfar)
{
    float temp, temp2, temp3, temp4;
    temp = 2.0 * znear;
    temp2 = right - left;
    temp3 = top - bottom;
    temp4 = zfar - znear;
    matrix[0] = temp / temp2;
    matrix[1] = 0.0;
    matrix[2] = 0.0;
    matrix[3] = 0.0;
    matrix[4] = 0.0;
    matrix[5] = temp / temp3;
    matrix[6] = 0.0;
    matrix[7] = 0.0;
    matrix[8] = (right + left) / temp2;
    matrix[9] = (top + bottom) / temp3;
    matrix[10] = (-zfar - znear) / temp4;
    matrix[11] = -1.0;
    matrix[12] = 0.0;
    matrix[13] = 0.0;
    matrix[14] = (-temp * zfar) / temp4;
    matrix[15] = 0.0;
}