使用鼠标选择缩放OpenGL场景
Zoom OpenGL scene using mouse selection
我正在尝试用鼠标根据屏幕上选定的区域实现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();
这段代码工作得很好…但只有一次。我希望在缩放后能够选择另一个区域。计算比例系数在这里不是问题,但是计算新的中心点位置就不那么容易了:/
相关文章:
- 如何在 3D OpenGL 场景上绘制 SDL 2D 矩形?
- 在两个不同的位置渲染 OpenGL 场景
- 在动画OpenGL场景中绘制
- OpenGL异步场景渲染需要几秒钟
- OpenGL C++围绕场景中心旋转对象
- 将鼠标事件发送到虚拟现实OpenGL场景中的QtQuick窗口
- Qt OpengL-旋转三维固定场景
- 使用注入的DLL从外部应用程序中渲染OpenGL场景
- 仅缩放场景中的一个对象OpenGL 3.0
- QMatrix4x4 模型视图投影 OpenGL 无法获取要渲染的场景
- 在 OpenGL 中仅绘制场景中的选定项目
- Opengl 在移动的 3D 场景中绘制 2D 矩形
- OpenGL ES 2.0 几个场景
- 在 xz 平面 opengl 中绘制场景
- 将 3D 模型加载到 OpenGL 场景中
- OpenGL场景渲染为黑色与阴影映射
- 添加纹理到QT OpenGL场景图形
- OpenGL场景下Qt qml应用
- 使用鼠标选择缩放OpenGL场景
- 如何在OpenGL场景中绘制透明橡皮筋