相机 glm / OpenGL 即时模式的示例

Posted

技术标签:

【中文标题】相机 glm / OpenGL 即时模式的示例【英文标题】:Example with camera glm / OpenGL immediate mode 【发布时间】:2021-01-24 18:52:16 【问题描述】:

我无法让相机转换与 glm 一起使用... 可能只是我错过了一件愚蠢的事情,但我找不到它......帮助?

  glViewport(0, 0, m_width, m_height);
  glClearColor(0.5f, 0.5f, 1.0f, 1);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  float fovy = 60.0f;
  float aspect = m_width / float(m_height);
  float znear = 0.1f;
  float zfar = 100.0f;
  glm::mat4 Mp = glm::perspective(fovy, aspect, znear, zfar);
  glMultMatrixf(&Mp[0][0]);
  
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  
  glm::vec3 eye = glm::vec3(5, 5, 5);
  glm::vec3 lookat = glm::vec3(0, 0, 0);
  glm::vec3 up = glm::vec3(0, 1, 0);
  glm::mat4 M = glm::lookAt(eye, lookat, up);
  glMultMatrixf(&M[0][0]);

  glBegin(GL_TRIANGLES);
  glColor3f(1, 1, 1);
  glVertex3f(-10,-10,-10);
  glVertex3f(10,-10,-10);
  glVertex3f(10, 10,-10);
  
  glColor3f(1, 1, 0);
  glVertex3f(-10,-10,-10);
  glVertex3f(10, 10,-10);
  glVertex3f(-10, 10,-10);
  glEnd();

这给出了以下输出: m_width = 1024, m_heigth = 768

【问题讨论】:

glm(广义线性模型)!= glm-math(GLM - OpenGL 数学) 嗯...也许我只是选择了太简单的几何形状 glm::perspective的角度参数单位为Radian:glm::perspective(glm::radians(fovy), aspect, znear, zfar); 【参考方案1】:

gluPerspective相比,glm::perspective的角度参数单位为Radian。使用glm::radians o 将度数转换为弧度:

glm::perspective(fovy, aspect, znear, zfar);

glm::perspective(glm::radians(fovy), aspect, znear, zfar);

您可以使用glLoadMatrixf,而不是glLoadIdentity,后跟glMultMatrixf。 此外,您可以使用glm::value_ptr 获取指向矩阵字段的指针:

float fovy = 60.0f;
float aspect = m_width / float(m_height);
float znear = 0.1f;
float zfar = 100.0f;
glm::mat4 Mp = glm::perspective(glm::radians(fovy), aspect, znear, zfar);

glMatrixMode(GL_PROJECTION);
glLoadMatrixf(glm::value_ptr(Mp));

glm::vec3 eye = glm::vec3(5, 5, 5);
glm::vec3 lookat = glm::vec3(0, 0, 0);
glm::vec3 up = glm::vec3(0, 1, 0);
glm::mat4 M = glm::lookAt(eye, lookat, up);

glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(glm::value_ptr(M));

【讨论】:

以上是关于相机 glm / OpenGL 即时模式的示例的主要内容,如果未能解决你的问题,请参考以下文章

OpenGL制作物面相机

OpenGL/GLSL/GLM - Skybox 像第三人称一样旋转

OpenGL - 让相机移动

OpenGL如何向上移动相机然后回到网格上

Opengl - GLM :: Ortho + GLM_COORDINATE_SYSTEM =很奇怪?

OpenGL/GLFW/GLM - 键盘输入无响应