我的程序中有一个简单的矩形,我必须在上面进行命中测试。我正在使用 openFrameworks,但我认为这里的问题也与 OpenGL 主题有关。
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;
};
我在这里遇到的问题是我在程序中旋转和平移形状:
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
函数中改变旋转时,点不会改变。因此,我没有合适的四个顶点来进行命中测试来确定鼠标是否在里面。如何获取屏幕中点的位置,这些点可以根据鼠标位置进行点击测试?