简体   繁体   English

OpenGL视锥体

[英]OpenGL frustum, perspective

I have the following code that takes snapshots to the framebuffer. 我有以下将快照捕获到帧缓冲区的代码。 I verified the framebuffer works correctly and the camera is facing the object correctly. 我验证了帧缓冲区可以正常工作并且相机正确地面向对象。 I used to get pictures done correctly, but it was based on faulty code, using the wrong frustum. 我过去经常正确地完成图片处理,但是它是基于错误的代码,使用了错误的视锥程序。 So I decided to start fresh (with the frustums). 所以我决定从头开始(从平头开始)。

The object is centered at the middle and is 32*32 blocks with each block 2*2, so 64 * 64. 对象位于中间,是32 * 32块,每个块2 * 2,所以64 * 64。

My distance is 100 and my viewport is 128x256. 我的距离是100,视口是128x256。 My frustum is 1 to 1000.0. 我的视锥范围是1到1000.0。

I'm relatively new to Opengl so I'm having trouble understanding the concepts of frustrums and perspectives fully. 我是Opengl的新手,所以我很难完全理解平截头概念和观点。

I do not get a picture at all. 我根本没有照片。

saveGLState();

const int nrPics = 360 / DEGREES_BETWEEN_PICTURES;
for (int i = 0; i < nrPics; i++) {

    catchFbo->bind();
    glViewport(0, 0, PICTURE_WIDTH, PICTURE_HEIGHT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    float rat = PICTURE_WIDTH / PICTURE_HEIGHT;
    glFrustum(- 1.0, + 1.0, - rat, + rat, 1.0, 1000.0);
    gluPerspective(90.0f,rat,CAPT_FRUSTRUM_NEAR,CAPT_FRUSTRUM_FAR);

    glColorMask(true, true, true, true);
    glClearColor(0,0,0,0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);
    glEnable(GL_MULTISAMPLE);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    drawScreenshot(i);
    catchFbo->release();

    QImage catchImage = catchFbo->toImage();
    catchImage.save("object/test" + QString::number(i) + ".png");
}

glDisable(GL_MULTISAMPLE);
restoreGLState();

void VoxelEditor::saveGLState()
{
    glPushAttrib(GL_ALL_ATTRIB_BITS);
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
}

void VoxelEditor::restoreGLState()
{
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);
    glPopMatrix();
    glPopAttrib();
}

EDIT: I tried using only glFrustum or glPerspective. 编辑:我尝试只使用glFrustum或glPerspective。 No luck. 没运气。

You shouldn't use both glFrustum and gluProjection. 您不应该同时使用glFrustum和gluProjection。 They both are operations which setup the projection matrix, and if you use them together you'll multiply them together and get a weird result. 它们都是设置投影矩阵的操作,如果一起使用它们,则将它们相乘会得到奇怪的结果。 Generally you'd just apply glFrustum OR gluProjection on an identity matrix, not both. 通常,您只将glFrustum gluProjection应用于单位矩阵,而不要同时应用于两者。

If that doesn't solve the problem, what are your values of NEAR, FAR, WIDTH, and HEIGHT? 如果那不能解决问题,那么您的NEAR,FAR,WIDTH和HEIGHT的值是多少? Also make sure you're not doing integer divide for your screen ratio (a common bug). 另外,请确保您没有针对屏幕比例进行整数除法(常见错误)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM