Ubuntu 下QT中使用gluLookAt
时间: 2024-03-17 17:43:38 浏览: 129
在 Ubuntu 下使用 Qt 进行 OpenGL 编程,需要在 Qt 项目中添加以下依赖项:`QT += opengl`。然后在 Qt 程序中可以直接调用 OpenGL 的 API,包括 `gluLookAt` 函数。
下面是一个简单的例子:
```c++
#include <QtOpenGL/QGLWidget>
#include <GL/glu.h>
class MyGLWidget : public QGLWidget
{
protected:
void initializeGL()
{
// 设置清屏颜色为白色
glClearColor(1.0, 1.0, 1.0, 0.0);
}
void paintGL()
{
// 清屏
glClear(GL_COLOR_BUFFER_BIT);
// 设置视口
glViewport(0, 0, width(), height());
// 设置投影矩阵
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat)width()/(GLfloat)height(), 0.1, 100.0);
// 设置模型视图矩阵
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 5.0, // 相机位置
0.0, 0.0, 0.0, // 目标位置
0.0, 1.0, 0.0); // 上向量
// 绘制图形
glBegin(GL_TRIANGLES);
glVertex3f(-1.0, -1.0, 0.0);
glVertex3f( 1.0, -1.0, 0.0);
glVertex3f( 0.0, 1.0, 0.0);
glEnd();
}
};
```
在 Ubuntu 中,如果没有安装 OpenGL 库,需要先安装。可以使用以下命令安装 OpenGL 开发包:
```bash
sudo apt-get install mesa-common-dev libglu1-mesa-dev freeglut3-dev
```
安装完成后,就可以在 Ubuntu 中使用 Qt 进行 OpenGL 编程了。
阅读全文
相关推荐


















