四个旋转顶点之间的命中测试 [英] Hit testing between four rotating vertices

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

问题描述

我的程序中有一个简单的矩形,我必须对其进行命中测试.我正在使用 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();
}

在上述draw函数中改变旋转时,点不会改变.因此,我没有合适的四个顶点用于命中测试来确定鼠标是否在里面.如何获取屏幕中可以根据鼠标位置进行检查以进行命中测试的点的位置?

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?

推荐答案

如果你想测试鼠标指针是否在矩形的范围内,我建议你先把它们转换成屏幕空间并在那里做测试,如下我在 https://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 https://stackoverflow.com/a/14424267/524368

我还要说,是时候停止使用 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(固定函数管道)或 glUniformMatrix(着色器管道)向 OpenGL 提供生成的矩阵.

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.

要测试点是否位于边定义的边界内,您可以使用称为半边"的概念.假设您的边形成了一个 星域 循环.然后对于循环中的每条边,您取点与边的叉积.如果点在循环定义的形状内,则所有叉积都将指向同一方向.共面四边形(即投影到屏幕空间的四边形)总是形成一个星域.

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天全站免登陆