使用OpenGL实现相机的控制与移动是OpenGL基础学习中的一个重要环节。本文将为大家分享一份简单的OpenGL相机实例代码,供大家学习和参考。
代码如下:
#include <gl/glut.h>
float camera_angle = 0.0f;
float camera_height = 0.0f;
float camera_distance = 1.0f;
int last_mouse_x = -1;
int last_mouse_y = -1;
void mouse_motion(int x, int y)
{
if (last_mouse_x != -1 && last_mouse_y != -1)
{
camera_angle += (x - last_mouse_x) * 0.5f;
camera_height += (y - last_mouse_y) * 0.5f;
if (camera_height > 89.0f)
{
camera_height = 89.0f;
}
if (camera_height < -89.0f)
{
camera_height = -89.0f;
}
}
last_mouse_x = x;
last_mouse_y = y;
}
void mouse_button