调用OpenGL三维绘图函数,绘制两个以上的物体(球 茶壶),变换他们的位置。 2、采用透视变换及正交变换分别对他们进行观察。 3、透视变换中变换观察空间的大小分别对他们进行观察(观测角、近远端截面距离等)。 4、变换观测点位置对他们进行观察。 5、将不同的图形观察结果显示在同一窗口的不同视口中。
时间: 2023-12-18 07:05:15 浏览: 40
下面是使用OpenGL三维绘图函数绘制两个以上的物体的代码,其中包括变换它们的位置、透视变换和正交变换观察、变换观测点位置以及在同一窗口中显示不同的图形观察结果在不同的视口中。
```c++
#include <GL/glut.h>
GLfloat position1[] = { -1.5f, 0.0f, -6.0f }; // 第一个物体的位置
GLfloat position2[] = { 1.5f, 0.0f, -6.0f }; // 第二个物体的位置
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// 绘制第一个物体(球)
glPushMatrix();
glTranslatef(position1[0], position1[1], position1[2]);
glColor3f(1.0, 0.0, 0.0); // 红色
glutSolidSphere(1.0, 50, 50);
glPopMatrix();
// 绘制第二个物体(茶壶)
glPushMatrix();
glTranslatef(position2[0], position2[1], position2[2]);
glColor3f(0.0, 1.0, 0.0); // 绿色
glutSolidTeapot(1.0);
glPopMatrix();
glFlush();
}
void reshape(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// 透视投影变换
gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
// 正交投影变换
// glOrtho(-2.0, 2.0, -2.0, 2.0, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
void keyboard(unsigned char key, int x, int y) {
switch (key) {
case 'a':
position1[0] -= 0.1f;
glutPostRedisplay();
break;
case 'd':
position1[0] += 0.1f;
glutPostRedisplay();
break;
case 'w':
position1[1] += 0.1f;
glutPostRedisplay();
break;
case 's':
position1[1] -= 0.1f;
glutPostRedisplay();
break;
case 'q':
position1[2] += 0.1f;
glutPostRedisplay();
break;
case 'e':
position1[2] -= 0.1f;
glutPostRedisplay();
break;
case 'j':
position2[0] -= 0.1f;
glutPostRedisplay();
break;
case 'l':
position2[0] += 0.1f;
glutPostRedisplay();
break;
case 'i':
position2[1] += 0.1f;
glutPostRedisplay();
break;
case 'k':
position2[1] -= 0.1f;
glutPostRedisplay();
break;
case 'u':
position2[2] += 0.1f;
glutPostRedisplay();
break;
case 'o':
position2[2] -= 0.1f;
glutPostRedisplay();
break;
}
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("OpenGL 3D Demo");
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
```
在此代码中,我们使用 `glutSolidSphere()` 和 `glutSolidTeapot()` 函数来绘制球和茶壶。我们使用 `glTranslatef()` 函数来移动它们的位置。在 `reshape()` 函数中,我们可以选择使用透视变换或正交变换来观察它们。在 `keyboard()` 函数中,我们可以使用键盘控制它们的位置。
如果要在同一窗口中显示不同的图形观察结果在不同的视口中,可以使用 `glViewport()` 函数来设置不同的视口。例如,以下代码将窗口分为四个区域,每个区域显示一个不同的视图:
```c++
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// 绘制第一个物体(球)
glPushMatrix();
glTranslatef(position1[0], position1[1], position1[2]);
glColor3f(1.0, 0.0, 0.0); // 红色
glutSolidSphere(1.0, 50, 50);
glPopMatrix();
// 绘制第二个物体(茶壶)
glPushMatrix();
glTranslatef(position2[0], position2[1], position2[2]);
glColor3f(0.0, 1.0, 0.0); // 绿色
glutSolidTeapot(1.0);
glPopMatrix();
glFlush();
}
void reshape(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
// 设置四个视口
glViewport(0, 0, w/2, h/2);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2.0, 2.0, -2.0, 2.0, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(w/2, 0, w/2, h/2);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-2.0, 2.0, -2.0, 2.0, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, h/2, w/2, h/2);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30.0, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(w/2, h/2, w/2, h/2);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(120.0, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
```
在此代码中,我们将窗口分为四个区域,并使用 `glViewport()` 函数将每个区域设置为一个视口。我们使用 `glOrtho()` 和 `glFrustum()` 函数来设置不同的投影变换。在 `display()` 函数中,我们只绘制两个物体,然后将它们的图形观察结果显示在四个视口中。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)