实验三 OpenGL几何变换
一、实验目的和要求
1. 理解掌握一个OpenGL程序平移、旋转、缩放变换的方法。
二、实验内容及主要步骤代码
(1)阅读实验原理,运行示范实验代码,掌握OpenGL程序平移、旋转、缩放变换的方法;
(2)根据示范代码,尝试完成实验作业;
(3)形成以下图形,如下图所示。
主要步骤代码:
(1)示范实验源代码
voiddrawSquare(void){
glBegin(GL_POLYGON);
glVertex2f(-1.0f,-1.0f);
glVertex2f(1.0f,-1.0f);
glVertex2f(1.0f,1.0f);
glVertex2f(-1.0f,1.0f);
glEnd();
}
void myDraw(void){
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glPushMatrix();
glTranslatef(0.0f,2.0f,0.0f);
glScalef(3.0,0.5,1.0);
glColor3f(1.0,0.0,0.0);
drawSquare();
glPopMatrix();
glPushMatrix();
glTranslatef(-3.0,0.0,0.0);
glPushMatrix();
glRotatef(45.0,0.0,0.0,1.0);
glColor3f(0.0,1.0,0.0);
drawSquare();
glPopMatrix();
glTranslatef(3.0,0.0,0.0);
glPushMatrix();
glRotatef(45.0,0.0,0.0,1.0);
glColor3f(0.0,0.7,0.0);
drawSquare();
glPopMatrix();
glTranslatef(3.0,0.0,0.0);
glPushMatrix();
glRotatef(45.0,0.0,0.0,1.0);
glColor3f(0.0,0.4,0.0);
drawSquare();
glPopMatrix();
glPopMatrix();
glTranslatef(0.0,-3.0,0.0);
glScalef(4.0,1.5,1.0);
glColor3f(0.0,0.0,1.0);
drawSquare();
glFlush();
}
三、实验作业
1.根据示范程序,能否在示范代码基础上修改形成以下图形?如下图所示,写出主要的步骤和代码。(不需要将整个程序都写下来)
答:
(1)源代码:
voiddrawDiamond(){
glBegin(GL_POLYGON);
glVertex2f(0.0f,0.0f);
glVertex2f(1.0f,2.0f);
glVertex2f(0.0f,4.0f);
glVertex2f(-1.0f,2.0f);
glEnd();
}
voidmyDisplay(){
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glColor3f(1.0,0.0,0.0);
drawDiamond();
glRotatef(120.0,0.0,0.0,1.0);
glColor3f(0.0,1.0,0.0);
drawDiamond();
glRotatef(120.0,0.0,0.0,1.0);
glColor3f(0.0,0.0,1.0);
drawDiamond();
glFlush();
}
(2)运行结果: