相机 glm / OpenGL 即时模式示例

Example with camera glm / OpenGL immediate mode

我无法让相机变换与 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

gluPerspective, the unit of the angle argument of glm::perspective is Radian. Use glm::radians 相比 o 从度数到弧度的转换:

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

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

而不是 glLoadIdentity followed by glMultMatrixf you can use glLoadMatrixf
此外,您可以使用 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));