Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
646 views
in Technique[技术] by (71.8m points)

glm math - Example with camera glm / OpenGL immediate mode

I can't get the camera transforms to work with glm... Probably just a silly thing I missed but I just can't find it... help?

  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();

This gives the following output: m_width = 1024, m_heigth = 768

enter image description here

question from:https://stackoverflow.com/questions/65874625/example-with-camera-glm-opengl-immediate-mode

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Compared to gluPerspective, the unit of the angle argument of glm::perspective is Radian. Use glm::radians o convert from degrees to radians:

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

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

Instead of glLoadIdentity followed by glMultMatrixf you can use glLoadMatrixf.
In addition, you can use glm::value_ptr to get a pointer to the matrix fields:

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));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...