在 OpenGL 中移动相机
我正在研究用 OpenGL 渲染地形。 我的代码如下:
void Render_Terrain(int k)
{
GLfloat angle = (GLfloat) (k/40 % 360);
//PROJECTION
glm::mat4 Projection = glm::perspective(45.0f, 1.0f, 0.1f, 100.0f);
//VIEW
glm::mat4 View = glm::mat4(1.);
//ROTATION
//View = glm::rotate(View, angle * -0.1f, glm::vec3(1.f, 0.f, 0.f));
//View = glm::rotate(View, angle * 0.2f, glm::vec3(0.f, 1.f, 0.f));
//View = glm::rotate(View, angle * 0.9f, glm::vec3(0.f, 0.f, 1.f));
View = glm::translate(View, glm::vec3(0.f,0.f, -4.0f)); // x, y, z position ?
//MODEL
glm::mat4 Model = glm::mat4(1.0);
glm::mat4 MVP = Projection * View * Model;
glUniformMatrix4fv(glGetUniformLocation(shaderprogram, "MVP_matrix"), 1, GL_FALSE, glm::value_ptr(MVP));
//Transfer additional information to the vertex shader
glm::mat4 MV = Model * View;
glUniformMatrix4fv(glGetUniformLocation(shaderprogram, "MV_matrix"), 1, GL_FALSE, glm::value_ptr(MV));
glClearColor(0.0, 0.0, 0.0, 1.0);
glDrawArrays(GL_LINE_STRIP, terrain_start, terrain_end );
}
我可以绕 X、Y、Z 轴旋转,缩放地形,但我找不到移动相机的方法。我正在使用 OpenGL 3+,而且我对图形有点陌生。
I am working on rendering a terrain in OpenGL.
My code is the following:
void Render_Terrain(int k)
{
GLfloat angle = (GLfloat) (k/40 % 360);
//PROJECTION
glm::mat4 Projection = glm::perspective(45.0f, 1.0f, 0.1f, 100.0f);
//VIEW
glm::mat4 View = glm::mat4(1.);
//ROTATION
//View = glm::rotate(View, angle * -0.1f, glm::vec3(1.f, 0.f, 0.f));
//View = glm::rotate(View, angle * 0.2f, glm::vec3(0.f, 1.f, 0.f));
//View = glm::rotate(View, angle * 0.9f, glm::vec3(0.f, 0.f, 1.f));
View = glm::translate(View, glm::vec3(0.f,0.f, -4.0f)); // x, y, z position ?
//MODEL
glm::mat4 Model = glm::mat4(1.0);
glm::mat4 MVP = Projection * View * Model;
glUniformMatrix4fv(glGetUniformLocation(shaderprogram, "MVP_matrix"), 1, GL_FALSE, glm::value_ptr(MVP));
//Transfer additional information to the vertex shader
glm::mat4 MV = Model * View;
glUniformMatrix4fv(glGetUniformLocation(shaderprogram, "MV_matrix"), 1, GL_FALSE, glm::value_ptr(MV));
glClearColor(0.0, 0.0, 0.0, 1.0);
glDrawArrays(GL_LINE_STRIP, terrain_start, terrain_end );
}
I can do a rotation around the X,Y,Z axis, scale my terrain but I can't find a way to move the camera. I am using OpenGL 3+ and I am kinda new to graphics.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
移动相机的最佳方法是使用 gluLookAt(),它模拟相机移动,因为相机无法移动。该函数有 9 个参数。前 3 个是眼睛的 XYZ 坐标,也是相机所在的位置。后 3 个参数是中心的 XYZ 坐标,即相机从眼睛看到的点。它始终是屏幕的中心。第三个 3 个参数是从眼睛垂直向上指向的 UP 向量的 XYZ 坐标。通过操纵这 3 个 XYZ 坐标,您可以模拟任何您想要的相机运动。
查看此链接。
更多详细信息:
- 例如,如果您想要围绕一个对象旋转,您可以围绕向上向量旋转您的眼睛。
- 如果您想向前或向后移动,您可以添加或减去眼睛以及中心点。
-如果你想向左或向右倾斜相机,你可以围绕你的观看向量旋转你的向上向量,其中你的观看向量是中心-眼睛。
gluLookAt 在已弃用的固定函数管道上运行,因此您应该使用 glm::lookAt 代替。
The best way to move the camera would be through the use of gluLookAt(), it simulates camera movement since the camera cannot be moved whatsoever. The function takes 9 parameters. The first 3 are the XYZ coordinates of the eye which is where the camera is exactly located. The second 3 parameters are the XYZ coordinates of the center which is the point the camera is looking at from the eye. It is always going to be the center of the screen. The third 3 parameters are the XYZ coordinates of the UP vector which points vertically upwards from the eye. Through manipulating those 3 XYZ coordinates you can simulate any camera movement you want.
Check out this link.
Further details:
-If you want for example to rotate around an object you rotate your eye around the up vector.
-If you want to move forward or backwards you add or subtract to the eye as well as the center points.
-If you want to tilt the camera left or right you rotate your up vector around your look vector where your look vector is center - eye.
gluLookAt operates on the deprecated fixed function pipeline, so you should use glm::lookAt instead.
您当前正在使用常量向量进行翻译。在注释掉的代码中(我假设您用来测试旋转),您使用
angle
来调整旋转。您应该有一个类似的翻译变量。然后,您可以将glm::translate
调用更改为:并获取翻译。
您可能应该向 Render_Terrain 传递多个参数,因为平移和旋转至少需要六个参数。
You are currently using a constant vector for translation. In the commented out code (which I assume you were using to test rotation), you use
angle
to adjust the rotation. You should have a similar variable for translation. Then, you can change theglm::translate
call to:and get translation.
You should probably pass in more than one parameter into
Render_Terrain
, as translation and rotation need at least six parameters.在 OpenGL 中,相机始终位于 (0, 0, 0)。您需要将矩阵模式设置为 GL_MODELVIEW,然后使用 glTranslate、glRotate、glLoadMatrix 等修改或设置模型/视图矩阵,以使相机看起来已移动。如果您使用的是 GLU,则可以使用 gluLookAt 将相机指向特定方向。
In OpenGL the camera is always at (0, 0, 0). You need to set the matrix mode to GL_MODELVIEW, and then modify or set the model/view matrix using things like glTranslate, glRotate, glLoadMatrix, etc. in order to make it appear that the camera has moved. If you're using GLU, you can use gluLookAt to point the camera in a particular direction.