图形学 用OpenGL实现平移、旋转、缩放
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
石正坤
一、实验目的:
1.了解OpenGL下简单图形的平移、旋转、缩放变换的编程的基本思想;
2.掌握OpenGL下简单图形的平移、旋转、缩放变换的编程的基本步骤;
二、实验原理:
在OpenGL中,可以使用下面三个函数便捷地实现简单图形平移、旋转、缩放变换的功能:
glRotatef(theta, vx, vy, vz);
glTranslatef(dx, dy, dz);
glScalef(sx,sy,sz);
三、实验内容:
// 1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "glut.h"
#include "math.h"
void display()
{
glClear( GL_COLOR_BUFFER_BIT); // Clear the frame buffer
glColor3f( 0.0, 1.0, 1.0); // Set current color to green
glBegin( GL_POLYGON); // Draw the triangle
glVertex2f( 0.0, -0.2);
glVertex2f( 0.2, 0.0);
glVertex2f( 0.0, 0.0);
glEnd();
glFlush();
}
void dsp()
{
glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BI T); // Clear the frame buffer
glColor3f( 0.0, 1.0, 1.0); // Set current color to green
display();
//-------------------------- //平移
glPushMatrix();
glTranslatef(0.5,0.5,0.0);
display();
glPopMatrix();
//-------------------------- //缩放
glPushMatrix();
glScalef(0.5,0.5,0.0);
display();
glPopMatrix();
//-------------------------- //旋转
glPushMatrix();
glRotatef(60,0.0,0.0,1.0);
display();
glPopMatrix();
}
void init()
{
glClearColor( 0.0, 0.0, 0.0, 0.0); // Set the clear color to black
// Specify the boundaries of the viewing window
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1.0, 1.0, -1.0, 1.0); // The para are: (left, right, bottom, top)
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char* argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEP TH);
glEnable(GL_DEPTH_TEST);
glutCreateWindow("simple");
glutDisplayFunc(dsp);
init();
glutMainLoop();
return 0;
}
原图:
平移:
缩放:
旋转:
四、实验总结:
1.在分别查看图像的平移、缩放、旋转中的其中一个功能时,需要将其他两个的代码注释掉,否则只会显示旋转一个功能。
2.当使用函数glScalef( sx, sy, sz)对图形作缩放操作时,
若sy为正值,则图形保持正立;而当sy为负值时,则图形就变成倒立像。