C ++ OpenGL用鼠标拖动多个对象 [英] C++ OpenGL dragging multiple objects with mouse

查看:172
本文介绍了C ++ OpenGL用鼠标拖动多个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想知道有人会如何拖4种不同的东西 openGL中的对象.我有非常简单的代码来绘制这些对象:

just wondering how someone would go about dragging 4 different objects in openGL. I have very simple code to draw these objects:

glPushMatrix();
glTranslatef(mouse_x, mouse_y, 0);
glutSolidIcosahedron();
glPopMatrix();

glPushMatrix();
glTranslatef(mouse_x2, mouse_y2, 0);
glutSolidIcosahedron();
glPopMatrix();

glPushMatrix();
glTranslatef(mouse_x3, mouse_y3, 0);
glutSolidIcosahedron();
glPopMatrix();

glPushMatrix();
glTranslatef(mouse_x4, mouse_y4, 0);
glutSolidIcosahedron();
glPopMatrix();

我了解如何移动对象,但是我想学习如何拖放这些对象中的任何一个. 我一直在研究名称栈"和选择模式",但这让我感到困惑. 我也知道必须要有某种glutMouseFunc. 这只是我不解的每种形状的选择.

I understand how to move an object, but I want to learn how to drag and drop any one of these objects. I've been researching about the Name Stack and Selection Mode, but it just confused the hell out of me. And I also know about having to have some sort of glutMouseFunc. It's just the selection of each shape I'm puzzled on.

推荐答案

您需要做的第一件事是单击按钮时捕获鼠标在屏幕上的位置.有很多方法可以做到这一点,但我认为这超出了这个问题的范围.当屏幕显示X,Y坐标时,必须检测是否选择了任何对象,以及该对象是哪个.有两种可能的方法.您可以跟踪每个对象的边界矩形位置(在屏幕空间中),并且测试光标是否在这些矩形中的任何一个内将非常简单.或者,您可以通过世界空间中的光标位置从眼睛投射光线,并检查该光线与每个对象的相交.

First thing that you need to do is capturing the position of mouse on the screen when the button is clicked. There are plenty of ways to do it but I believe it's outside of the scope of this question. When you have screen X,Y coords you must detect if any object is selected and which one it is. There are two possible approaches. You can either keep track of a bounding rectangle positions of each object (in screen space) and the test if the cursor is inside any of those rectangles will be quite simple. Or you can cast a ray from eye through cursor position in world space and check intersection of this ray with each object.

第二种方法对于3D图形更具通用性,但是您似乎只使用X和Y坐标,因此您不必担心对象的Z顺序.

The second approach is more versatile for 3D graphics but you seem to be using only X and Y coords so you don't need to worry about Z order of objects.

在第一个解决方案的情况下,主要问题是:如何知道您的对象在屏幕上的大小. glutSolidIcosahedron()渲染半径为1的对象.要计算其屏幕半径,可以使用一些矩阵数学,也可以使用简单的三角函数.您将需要知道相机到绘图平面的距离(我相信您在渲染之前使用了glTranslatef(0,0,X).X是您的距离),您还需要知道相机的视角.您将其设置在投影矩阵中.现在拿一张纸,画一个角锥,在距离X处与一个平面相交,知道一个对象的半径为1,您可以轻松地计算出它占据屏幕的面积. (我将把这个计算留给您)

In case of the first solution the main problem is: how to know how big is your object on the screen. glutSolidIcosahedron() renders an object of radius 1. To calculate it's screen radius you can either use some matrix math or in that case a simple trigonometry. You will need to know the distance from camera to the drawing plane (I believe you're using some glTranslatef(0,0,X) before you render. X is your distance) You also need to know the view angle of the camera. You set it in projection matrix. Now take a piece of paper, draw a cone of angle alpha, intersecting a plane at distance X and knowing that an object has radius 1 you can easily calculate how large area of the screen it occupies. (I'll leave this calculation for you)

现在,如果您知道屏幕上的半径,只需测试单击位置到每个对象的距离即可.如果距离小于半径,则将其选中.如果一个以上的对象通过了此测试,只需选择其中的第一个即可.

Now if you know the radius on screen, simply test the distance from your click position to each object. if the distance is below radius it's selected. If more than one object passes this test just select first one of them.

这篇关于C ++ OpenGL用鼠标拖动多个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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