简体   繁体   中英

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.

My distance is 100 and my viewport is 128x256. My frustum is 1 to 1000.0.

I'm relatively new to Opengl so I'm having trouble understanding the concepts of frustrums and perspectives fully.

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. No luck.

You shouldn't use both glFrustum and 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.

If that doesn't solve the problem, what are your values of NEAR, FAR, WIDTH, and HEIGHT? Also make sure you're not doing integer divide for your screen ratio (a common bug).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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