OpenGL在正视和透视问题之间切换 [英] OpenGL switching between ortho and perspecive problems

查看:297
本文介绍了OpenGL在正视和透视问题之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将模型集成到OpenGL中,并实现了一些键盘功能,例如在正视图和透视图之间切换.您也可以放大和缩小并更改相机视图.现在,我认识到的是切换到同种异体移植然后回到透视模型后,模型出现了很多问题.第一个:如果我现在在透视图中更改水平相机视图,则该模型会在一段时间后丢失(经常按下相机视图键之后).第二个问题是,我无法真正在正交视图中进行放大和缩小.我试图扩展模型,因为我认为它可能太大,但这并没有帮助.而且我不知道为什么同位移植功能会在透视图中产生此问题,因为无需按正交键,一切就可以在透视图中正常工作.

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

希望有人能帮助您

如果您需要更多详细信息或更多代码,请告诉我

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

对于第一个问题,我在显示功能中清除深度缓冲区的代码

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

推荐答案

尝试使用更大范围的正交矩阵

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;

放大/缩小时使用相同的代码,或者更好的是将其分解为子例程.另外,请考虑zNear和zFar是否需要如此大的范围.尝试使用较小的数字.

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

这篇关于OpenGL在正视和透视问题之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆