opengl定义三维图形,实现三维三视图实验代码
时间: 2023-12-20 15:06:04 浏览: 150
基于opengl的图形学 实验源代码
5星 · 资源好评率100%
下面是一个使用 OpenGL 实现三维三视图的代码示例,包含了正交投影和透视投影两种方式:
```c++
#include <GL/glut.h>
// 窗口大小
const int WINDOW_WIDTH = 600;
const int WINDOW_HEIGHT = 600;
// 三维坐标系范围
const double X_MIN = -10.0;
const double X_MAX = 10.0;
const double Y_MIN = -10.0;
const double Y_MAX = 10.0;
const double Z_MIN = -10.0;
const double Z_MAX = 10.0;
// 三个视图窗口的位置和大小
const int VIEWPORT1_X = 20;
const int VIEWPORT1_Y = 20;
const int VIEWPORT1_WIDTH = 200;
const int VIEWPORT1_HEIGHT = 200;
const int VIEWPORT2_X = 240;
const int VIEWPORT2_Y = 20;
const int VIEWPORT2_WIDTH = 200;
const int VIEWPORT2_HEIGHT = 200;
const int VIEWPORT3_X = 460;
const int VIEWPORT3_Y = 20;
const int VIEWPORT3_WIDTH = 200;
const int VIEWPORT3_HEIGHT = 200;
// 三维模型的顶点坐标
GLfloat vertices[][3] = {
{-1.0, -1.0, -1.0},
{1.0, -1.0, -1.0},
{1.0, 1.0, -1.0},
{-1.0, 1.0, -1.0},
{-1.0, -1.0, 1.0},
{1.0, -1.0, 1.0},
{1.0, 1.0, 1.0},
{-1.0, 1.0, 1.0}
};
// 三维模型的面
GLint faces[][4] = {
{0, 1, 2, 3},
{1, 5, 6, 2},
{5, 4, 7, 6},
{4, 0, 3, 7},
{0, 4, 5, 1},
{3, 2, 6, 7}
};
// 初始化 OpenGL
void init(void) {
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, 1.0, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
}
// 绘制三维模型
void drawModel(void) {
int i;
glBegin(GL_QUADS);
for (i = 0; i < 6; i++) {
glNormal3fv(&vertices[faces[i][0]][0]);
glVertex3fv(&vertices[faces[i][0]][0]);
glNormal3fv(&vertices[faces[i][1]][0]);
glVertex3fv(&vertices[faces[i][1]][0]);
glNormal3fv(&vertices[faces[i][2]][0]);
glVertex3fv(&vertices[faces[i][2]][0]);
glNormal3fv(&vertices[faces[i][3]][0]);
glVertex3fv(&vertices[faces[i][3]][0]);
}
glEnd();
}
// 绘制三个视图窗口
void drawViewport1(void) {
glViewport(VIEWPORT1_X, VIEWPORT1_Y, VIEWPORT1_WIDTH, VIEWPORT1_HEIGHT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(X_MIN, X_MAX, Y_MIN, Y_MAX, Z_MIN, Z_MAX);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
drawModel();
}
void drawViewport2(void) {
glViewport(VIEWPORT2_X, VIEWPORT2_Y, VIEWPORT2_WIDTH, VIEWPORT2_HEIGHT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(X_MIN, X_MAX, Y_MIN, Y_MAX, Z_MIN, Z_MAX);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
drawModel();
}
void drawViewport3(void) {
glViewport(VIEWPORT3_X, VIEWPORT3_Y, VIEWPORT3_WIDTH, VIEWPORT3_HEIGHT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(X_MIN, X_MAX, Y_MIN, Y_MAX, Z_MIN, Z_MAX);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0);
drawModel();
}
// 绘制场景
void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawViewport1();
drawViewport2();
drawViewport3();
glutSwapBuffers();
}
// 窗口大小改变时的回调函数
void reshape(int width, int height) {
glViewport(0, 0, width, height);
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutCreateWindow("3D Viewport Demo");
init();
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
```
运行该程序后,会弹出一个窗口,其中包含了三个视图窗口,分别显示了三个视图的效果。可以通过修改 `gluPerspective` 或 `glOrtho` 函数的参数来调整投影方式和视角大小,从而得到不同的效果。
阅读全文