使用鼠标选择缩放OpenGL场景



我正在尝试用鼠标根据屏幕上选定的区域实现opengl场景的缩放。

我的目标是使用户能够使用鼠标缩放2D opengl世界的任何部分。而且他应该能够放大几倍。

努力做到这一点。

绘图使用:

    glViewport(fullscreen)
    gluOrtho2D()
    ...drawing...

尝试在gluOrtho2D中更改世界坐标,但似乎不可能缩放几次…

所以我试图为glScalef和glTranslatef…

也许有人试过这样做,可以提供一些建议?

如何使用glLoadIdentity将矩阵重置为相同的值。每次你调用一个OpenGL的矩阵操作函数时,它都会在当前活动矩阵堆栈的顶部进行一次就地乘法运算。

典型的显示函数应该是这样的(伪代码)

display:
    glClear(…)
    # just to illustrate that you can mix multiple projection
    # setups throughout rendering a single frame
    foreach l in layers:
        glViewport(l→viewport)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        l→setup_projection() # for example glOrtho
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
        l→setup_view() # for example gluLookAt
        foreach m in models:
            glMatrixMode(GL_MODELVIEW)
            # make a copy of the current modelview so that we can
            # restore it after the model has been drawn, by pushing onto the stack
            glPushMatrix()
            setup_model_transformation()
            draw_model()
            glMatrixMode(GL_MODELVIEW)
            # restore to the previous matrix by poping it from the stack
            glPopMatrix()

请注意,这使用了已弃用的(即旧的和从现代OpenGL中删除的)矩阵堆栈。现代OpenGL程序做自己的矩阵数学,只是加载相关的,有效的矩阵到所谓的着色器统一为每个模型绘制。

取得了一些进展…

    displayedMapCoords[0] = mapCenterPoint[0] - sceneSize * aspectRatio;
    displayedMapCoords[1] = mapCenterPoint[0] + sceneSize * aspectRatio;
    displayedMapCoords[2] = mapCenterPoint[1] - sceneSize;
    displayedMapCoords[3] = mapCenterPoint[1] + sceneSize;
    ...
    gluOrtho2D( displayedMapCoords[0], displayedMapCoords[1],
                displayedMapCoords[2], displayedMapCoords[3] ); 
    glScalef(mapScale, mapScale, 0);
    ...

当用鼠标选择一个矩形时,我计算比例因子和新的中心点:

    mapScale = (float) CfgManager::Instance()->getScreenHeight() / selectedAreaSize;
    mapCenterPoint[0] = -(float) mapScale * (CfgManager::Instance()->getScreenWidth()/2 - zoomArea[0] - selectedAreaSize / 2) * sceneSize / CfgManager::Instance()->getScreenHeight();
    mapCenterPoint[1] = (float) mapScale * (CfgManager::Instance()->getScreenHeight()/2 - zoomArea[1] - selectedAreaSize / 2) * sceneSize / CfgManager::Instance()->getScreenHeight();

这段代码工作得很好…但只有一次。我希望在缩放后能够选择另一个区域。计算比例系数在这里不是问题,但是计算新的中心点位置就不那么容易了:/

最新更新