简体   繁体   English

第一人称相机,转身OPENGL

[英]first person camera, turning around OPENGL

I have a perspective camera with Obs and VRP and Up vector. 我有一个带Obs和VRP和向上矢量的透视摄像头。

I want to implement turning around to act like an observer inside my scene. 我想实现转身,像我的场景中的观察者一样。 I have already implemented forward and backward as it is only adding same amount to z coordinate to Obs and VRP (both are vec3). 我已经实现了前向和后向,因为它只是向Obs和VRP添加相同数量的z坐标(两者都是vec3)。 The problem comes when I try to implement rotate, is there any way to do it with matrix product? 当我尝试实现旋转时出现问题,是否有任何方法可以使用矩阵产品? I've tried that: 我试过了:

x = R * cos angle x = R * cos角

z = R * sin angle z = R * sin角

where R is the magnitude of vector (VRP - Obs) but it doesn't work 其中R是向量的大小(VRP-Obs),但它不起作用

Edit: i apply view and projection matrix here: 编辑:我在这里应用视图和投影矩阵:

    void MyGLWidget::projTransform(){
    //fov window, ra window (anchura / altura), near, far
    glm::mat4 Proj = glm::perspective(M_PI/1.3, 1.0, 0.2, 3000.0);
    glUniformMatrix4fv(projLoc,1,GL_FALSE, &Proj[0][0]);
}

void MyGLWidget::viewTransform(){
    //lookAt(OBS,VRP;UP)
    //glm::mat4 View = glm::lookAt(glm::vec3(0,0,10),glm::vec3(0,0,-2), glm::vec3(0,1,0));
    glm::mat4 View = glm::lookAt(glm::vec3(xObs,yObs,zObs),glm::vec3(xVRP,yVRP,zVRP), glm::vec3(0,1,0));
    glUniformMatrix4fv(viewLoc,1,GL_FALSE, &View[0][0]);
}

Assuming the Obs is the camera position and VRP is the look-at position: 假设Obs是摄像机位置,VRP是观察位置:

r = ||VRP - Obs||

                  r * cos(angle)
VRP_new = Obs + [     0          ]
                  r * sin(angle)

Since you might want to have a second rotation axis later on, I would suggest to have this parameters for the camera: A position vector t and two angles describing rotations around x and y axis [rx, ry]. 由于您可能希望稍后有第二个旋转轴,我建议为摄像机设置此参数:位置矢量t和描述围绕x轴和y轴旋转的两个角度[rx,ry]。 One can combine them in every frame to get Obs and VRP, but this is not really necessary, since the view matrix can directly be derived from this parameters: 可以在每个帧中组合它们以获得Obs和VRP,但这不是必需的,因为视图矩阵可以直接从这些参数派生:

V = T(t) * R_y(ry) * R_x(rx),

where T is a translation matrix and R_x, R_y are rotation matrices. 其中T是平移矩阵,R_x,R_y是旋转矩阵。 Depending on your notation, the view matrix might have to be inverted. 根据您的表示法,可能必须反转视图矩阵。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM