四面旋转顶点之间的点击测试 [英] Hit testing between four rotating vertices

查看:149
本文介绍了四面旋转顶点之间的点击测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的矩形在这里我的程序上,我不得不这样做命中测试。我使用了openFrameworks但我觉得这里的问题是关系到OpenGL的主题为好。

I have a simple rectangle here in my program on which I had to do hit testing. I am using openFrameworks but I think the problem here is related to OpenGL topics as well.

public class Shape : public class ofNode {
  Shape() {
    container.setFromCenter(getPosition(), 200,100);
    setPosition(ofVec3f(365, 50, 0)); //set the position of the node
    lookAt(ofVec3f(0,0,0));
  }

  virtual void draw() {
    ofRect(container); //the 200/100 rectangle will be drawn as per the container position and dimensions
  }
  ofVec3f getTopLeft() const {
    return getTopLeft() * ofNode::getGlobalTransformMatrix();
  }

  ofVec3f getTopRight() const {}    //similar - getTopRight from container and multiply
  ofVec3f getBottomLeft() const {}  //similar
  ofVec3f getBottomRight() const {} //similar

  bool hitTest(int tx, int ty) {
    // return true if mouse pointer is inside four vertices - hit test logic working fine
  }

  private:
    ofRectagle container;
};

我的问题是在这里,我旋转和平移我的程序里面的形状:

The problem I have here is I am rotating and translating the shape inside my program:

void draw() {
  ofPushMatrix();
  ofTranslate(600,300,0);
  ofRotateY(rotate);
  shape->draw();

  ofCircle(shape->getTopLeft(), 10); //circle is drawn at the rectangle top left vertex on the screen but the coordinates while rotating and drawing do not change (as seen on cout)
  ofCircle(shape->getTopRight(), 10); //similar
  ofCircle(shape->getBottomLeft(), 10); //similar
  ofCircle(shape->getBottomRight(), 10); //similar
  ofPopmatrix();
}

在正在旋转上述变更为在吸取函数,这些点没有改变。因此,我没有合适的四个顶点的点击测试,以找出是否鼠标内部。 我如何才能在屏幕上的点的位置可以是对证的点击测试鼠标的位置?

On rotation being changed as in the above draw function, the points do not change. Hence, I do not have the appropriate four vertices for the hit test to figure out if the mouse is inside. How do I get the position of the points in the screen which can be check against the mouse position for the hit test?

推荐答案

如果你想测试是否将鼠标指针的矩形范围内,我建议你先转换成屏幕空间,并做了测试在那里,下面总体思路我在 http://stackoverflow.com/a/14424267/524368

If you want to test if the mouse pointer is within the limits of the rectangle, I suggest you first transform them into screen space and do the test there, following the general idea I gave you in http://stackoverflow.com/a/14424267/524368

我也想说,it'a关于时间停止使用OpenGL的内建矩阵操作功能。 是绝对使用没有好处它们!的一些人认为他们会是GPU加速,但事实并非如此。只要你需要这些矩阵在程序的其他方面它根本不实用滥用的OpenGL这一点。 OpenGL的毕竟不是一个数学库。并在以后的版本中,整个矩阵堆栈已退的OpenGL - 甩掉包袱

I'd also say, it'a about time to stop using OpenGL's builtin matrix manipulation functions. There is absolutely no benefit in using them! Some people think they'd be GPU accelerated, but that's not the case. As soon as you need those matrices at other points in your program it simply not practical to abuse OpenGL for this. OpenGL is not a math library after all. And in later versions the whole matrix stack has been dropped from OpenGL – good riddance.

了openFrameworks配备了一个全功能的矩阵数学库。我强烈建议你使用的的。你可以用 glLoadMatrix 饲料的OpenGL生成矩阵(固定管线)或者 glUniformMatrix (着色器管线)。

OpenFrameworks comes with a fully featured matrix math library. I strongly suggest you use that. You can feed OpenGL the generated matrices with glLoadMatrix (fixed function pipeline) or glUniformMatrix (shader pipeline).

您可以用它来实现投影功能在其他的答案概述了我。

You can use it to implement a projection function as outlined by me in that other answer.

要测试一个点位于由边缘,你可以使用一种叫做半边缘的概念定义的边界之内。说你的边缘形成星域循环。然后在循环的每个边缘你采取与边缘的点的矢量积。如果该点是由环路限定的形状内的所有交叉积将指向成相同的方向。共面四边形(投射到屏幕空间即quadrilateals)始终形成一个星域。

To test if a point lies within the boundaries defined by edges you can use a concept called "half edges". Say your edges form a star domain loop. Then for each edge in the loop you take the cross product of the point with the edge. If the point is within the shape defined by the loop all the cross products will point into the same direction. Coplanar quadrilaterals (i.e. quadrilateals projected into screen space) always form a star domain.

这篇关于四面旋转顶点之间的点击测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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