c++ - gluProject() 的奇怪结果

标签 c++ opengl 3d 2d glu

gluProject 函数 (OpenGL) 有问题。 我想将一个简单的 3D 点从对象空间转换到屏幕空间。 我的代码如下:

int main(){
    GLdouble mvmatrix[16];
    GLdouble projmatrix[16];
    GLint viewport[4];
    GLdouble winx;
    GLdouble winy;
    GLdouble winz;
    glGetDoublev(GL_MODELVIEW_MATRIX, mvmatrix);
    glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);
    glGetIntegerv(GL_VIEWPORT, viewport);
    gluProject(0.0, 0.0, 0.0, mvmatrix, projmatrix, viewport, &winx, &winy, &winz);
    std::cout << winx << winy;
    getchar();
    getchar();
    return 0;
}

输出是:

-1.71799e+009 -1.71799e+009

这是一个奇怪的结果,对我来说没有意义。 有谁知道出了什么问题?我没有在网上找到任何东西。

最佳答案

Does anyone know what went wrong?

您还没有创建 GL context并在使用 glGetDoublev()glGetIntegerv() 之前使其成为当前版本:

In order for any OpenGL commands to work, a context must be current; all OpenGL commands affect the state of whichever context is current. The current context is a thread-local variable, so a single process can have several threads, each of which has its own current context. However, a single context cannot be current in multiple threads at the same time.

您可以使用 ( Free ) GLUT (在 other things (not exhaustive) 中)创建一个窗口和关联的 GL 上下文:

#include <GL/glut.h>
#include <iostream>

int main(int argc, char **argv)
{
    // create context & make it current
    glutInit( &argc, argv );
    glutInitWindowSize( 200, 200 );
    glutInitDisplayMode( GLUT_RGBA );
    glutCreateWindow( "GLUT" );

    GLdouble mvmatrix[16];
    GLdouble projmatrix[16];
    GLint viewport[4];
    GLdouble winx;
    GLdouble winy;
    GLdouble winz;
    glGetDoublev( GL_MODELVIEW_MATRIX, mvmatrix );
    glGetDoublev( GL_PROJECTION_MATRIX, projmatrix );
    glGetIntegerv( GL_VIEWPORT, viewport );
    gluProject
        (
        0.0, 0.0, 0.0, 
        mvmatrix, projmatrix, viewport,
        &winx, &winy, &winz
        );
    std::cout << winx << " " << winy << std::endl;
    return 0;
}

输出:

100 100

关于c++ - gluProject() 的奇怪结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33233195/

相关文章:

c++ - 使用立即模式绘制纹理的 OpenGL 替代方案?

python - 访问 3D numpy 数组的切片

c++ - 如何使用 C++ 使用 3d 点数组绘制多边形

c++ - 在对的 vector 中按关键字查找对

c++ - if 语句内满足的条件的索引

具有可变大小和可变类型的 C++ 容器

c++ - assert(true) 警告签名/未签名不匹配

opengl - OpenGL/OpenGLES 中的帧缓冲区纹理行为

OpenGL 计算阶段与其他阶段

3d - 球体上的纹理错误