OpenGL switching between ortho and perspecive problems

三世轮回 提交于 2019-12-11 06:09:59

问题


I've integrated a model into OpenGL and implemented some keyboard functions for example to switch between orthografic and perspective view. Also you can zoom in and out and change the camera view. Now what i've recognize is after switching to orthografic and then back to perspective the model gets a lot of issues. First one: If i now change the horizontal camera view in the perspective view the model gets lost after a time (after pressing the camera view key often). And the second issue is that i can't really zoom in and out in the orthografic view. I tried to scale my model because i thought it could be too big but this didn't helped. And I don't know why the orthografic function creates this issue in the perspective view because without ever pressing the ortho key everything works fine in the perspective view.

//change to perspective
case 'p':
    if (!(perspective)) {
        projection = glm::perspective(zoom, 1.0f, 0.1f, 100.0f);
        perspective = true;
    }
    break;

//change to ortho
case 'o':
    if (perspective) {
        projection = glm::ortho(-100.0f, 100.0f, -100.0f, 100.0f, -1000.0f, 1000.0f);
        perspective = false;
    }
    break;

// zoom out 
case '-':
    if (zoom <= 3.0236f) {
        zoom += 0.1f;
        if (perspective) { 
            projection = glm::perspective(zoom, 1.0f, 0.1f, 1000.0f);
        }
        else {
            projection = glm::ortho(-0.1f - zoom, 0.1f + zoom, -0.1f - zoom, 0.1f + zoom, -10000.0f, 10000.0f);
        }
    }
    break;

The same for zoom in with changed prefixes

I hope somebody can help thank you

if you need more details or more code please let me know

Edit: For the first Problem the code where i'm clearing the depth buffer in the display function

void display()
{
       ... defined camera settings code here ....


    glClear(GL_DEPTH_BUFFER_BIT);
    glClearBufferfv(GL_COLOR, 0, color);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture);

    // set shader param and calculate matrices
    modelView = view * modelView * model; 
    ...
    glDrawArrays(GL_TRIANGLES, 0, obj.vertices.size());

    glutSwapBuffers(); 
}

what I also recognized now is that the model in the perspective view only gets lost when using the camera view changes directly after switching from the ortho view to the perspective view. If I first press zoom in/out after changing to perspective and then using camera view changes everything works fine


回答1:


try using a greater range of scale for your ortho matrix,

//change to ortho
case 'o':
    if (perspective) { 
        float min = -pow(10, zoom);
        float max = pow(10, zoom);
        projection = glm::ortho(min, max, min, max, -1000.0f, 1000.0f);
        perspective = false;
    }
    break;

Use the same code when you zoom in / out, or better still, factor it out into a subroutine. Also, consider whether you need such huge ranges for zNear and zFar. Try with smaller numbers.

Perhaps post your first problem as a separate question



来源:https://stackoverflow.com/questions/44710262/opengl-switching-between-ortho-and-perspecive-problems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!