【问题标题】:C++ OpenGL: Calling gluPerspective throws an undefined reference error?C++ OpenGL:调用 gluPerspective 会引发未定义的引用错误?
【发布时间】:2013-02-10 00:56:31
【问题描述】:

我正在使用 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,除此行之外,其他一切都有效。我检查了文档,似乎我正确使用了它。我很茫然。

【问题讨论】:

  • 在我回答你得到的问题之前:为什么要使用固定功能管道? FFP 已弃用。
  • 我只是想在进入着色器之类的东西之前先做一个基本的工作测试。

标签: c++ opengl perspective freeglut glu


【解决方案1】:

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

【讨论】:

  • 可能是前者。后者根本没有帮助。但我正在使用最新版本的 FreeGLUT 和 OpenGL 3.3.0 进行编译。我该如何解决这个问题?
  • 我想我会四处寻找。 OpenGL 是否还有矩阵类,还是我必须自己制作?
  • @MrDoctorProfessorTyler - 它仍然有矩阵类,我找到了代码。
  • 谢谢!这是一个非常大的帮助:D
  • gluPerspective never 根本不是 OpenGL 的一部分。它是 GLU 的一部分,一个配套的帮助库,随 OpenGL-1.x 一起发布,但不属于 OpenGL-1.x
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-15
  • 1970-01-01
相关资源
最近更新 更多