使用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(int button, int state, int x, int y)
{
last_mouse_x = -1;
last_mo
本文介绍了使用OpenGL实现相机控制与移动的基础知识,提供了一段简单的相机实例代码,适用于初学者学习。通过鼠标操作,可以调整相机角度和高度,代码中还包括了相机高度的限制逻辑,确保了渲染的稳定性。
订阅专栏 解锁全文
310

被折叠的 条评论
为什么被折叠?



