如何累积相机旋转?

时间:2011-05-14 10:54:17

标签: c++ camera rotation transformation openframeworks

我想知道如何累积相机旋转,所以当我再次点击屏幕时,旋转不会重置到该点击位置,但如果有意义则跟随该点的旋转

这是我的代码。这将在点击/触摸拖动事件上执行。

ofVec3f camPos      = ofVec3f(0, 0, camDistance);
ofVec3f centerPos   = ofVec3f(0, 0, 0);

static float halfWidth  = ofGetWidth()/2;
static float halfHeight = ofGetHeight()/2;

float rotX = (touch.x-halfHeight) / (halfWidth);
float rotY = (touch.y-halfHeight) / (halfHeight);

camPos.rotate( rotY * 90, ofVec3f(1, 0, 0) );
camPos.rotate( rotX * 180, ofVec3f(0, 1, 0) );
camPos += centerPos;

cam.setPosition( camPos );
cam.lookAt( centerPos );

1 个答案:

答案 0 :(得分:0)

对于初学者,你需要在某个地方存储一些旋转数据,这对于你的一个类中的成员来说是最好的,但是为了只修改你提供的代码,它们可能是静态局部变量。

ofVec3f camPos      = ofVec3f(0, 0, camDistance);
ofVec3f centerPos   = ofVec3f(0, 0, 0);

//there is no need for these to be static unless ofGetWidth
//and ofGetHeight are expensive
float halfWidth  = ofGetWidth()/2;
float halfHeight = ofGetHeight()/2;

//these accumlate all change
static float camRotX=0.0f;
static float camRotY=0.0f;

float rotX = (touch.x-halfHeight) / (halfWidth);
float rotY = (touch.y-halfHeight) / (halfHeight);

camRotX+=rotX;
camRotY+=rotY;

camPos.rotate( camRotY * 90, ofVec3f(1, 0, 0) );
camPos.rotate( camRotX * 180, ofVec3f(0, 1, 0) );
camPos += centerPos;

cam.setPosition( camPos );
cam.lookAt( centerPos );

这可能会实现您想要的鼠标点击,但它不适用于触摸事件或拖动鼠标。对于触摸事件,您希望记录触摸起始位置并将其用作旋转的基础,而不是使用屏幕中心。