创建一个围绕对象 OpenGL C++旋转的相机
Create a camera that rotates around the object OpenGL C++
我在本教程结束时做练习:链接
但是,我坚持"创建围绕对象旋转的相机"
( position = ObjectCenter + ( radius * cos(time), height, radius * sin(time) ) );
我使用上面提供的代码来创建以下旋转
GLfloat radius = 10.0f;
position = glm::vec3(0.0f,0.0f,0.0f) + glm::vec3(radius * cos(glfwGetTime()), 0.0f, radius * sin(glfwGetTime()));
使用它,场景以 10 的距离围绕 (0,0,0) 旋转,我认为这是立方体的中心,但事实并非如此,因此当场景旋转时,立方体每隔一段时间就会闪过窗口。
那么,我究竟如何找到对象中心呢?当我绘制立方体时,我无法从代码中理解,我告诉它将其居中。
作为参考,初始位置 vec 为
glm::vec3 position = glm::vec3( 0, 0, 5 );
最简单的方法是在控件中添加以下内容.cpp并在controls.hpp中声明它
glm::vec3 *getPosition(){
return &position;
}
void setPosition(glm::vec3 var){
position = var;
}
然后,您只需在主循环的开头声明以下变量:
float rotCamera = 0.0f;
glm::vec3 origPos = *getPosition();
并在主循环中紧跟在 glfwPollEvents(); 之后添加以下行:
// Swap buffers
glfwSwapBuffers(window);
glfwPollEvents();
//To rotate the camera through the object
if (glfwGetKey( window, GLFW_KEY_C ) == GLFW_PRESS){
rotCamera += 0.01;
glm::vec3 rot(cos(rotCamera) * 10.0 - origPos.x, 0, sin(rotCamera) * 10.0 - origPos.z);
setPosition(origPos + rot);
}
最后,如果您希望相机始终朝立方体看,则必须将 lookAt 从.cpp控件更改为始终看 (0,0,0):
ViewMatrix = glm::lookAt(position, // Camera is here
glm::vec3( 0, 0, 0 ), //To look always to the cube
up // Head is up (set to 0,-1,0 to look upside-down)
);
相关文章:
- 无法使用VAO和EBO(openGL)绘制多个对象
- 如何在OpenGL中正确旋转和缩放对象?
- 创建 OpenGL 对象期间出错
- 在类中渲染OpenGL对象
- OpenGL 对象在移动一段距离后振动
- 我的3D OpenGL对象围绕世界来源,而不是本地空间来源.我在做什么错或误会
- 使用纹理,法线和索引列表从VBO绘制OpenGL对象的问题
- 为什么我的 OpenGL 对象相对于最后一个绘制的对象绘制
- OpenGl 对象不显示,着色器相关
- OpenGL对象在PC上以正常速度旋转,但在笔记本电脑上非常快
- OpenGL-对象转换和VBO
- OpenGL对象包装器中的自动绑定
- Opengl对象拉伸
- 用于OpenGL对象的RAII包装器
- 在架构x86_64中找不到openGL对象
- Opengl对象加载器与纹理
- OpenGL对象自动在圆中移动
- OpenGL对象创建
- openGl对象在特定位置消失
- OpenGl -对象在旋转180度后反转