【计算机图形学】实验3《图形变换》

实验3图形变换

一、实验目的

学习图形的基本几何变换

二、实验内容

1. 投影变换;

2. 视口变换;

3. 旋转、平移和缩放。


运行结果

点击w键使地球逆时针自转:

点击a键使月球逆时针旋转:

点击d键使月球顺时针旋转:

点击f键左移飞船:

点击h键放大飞船:

点击j键右移飞船:

点击g键缩小飞船:

点击s键使地球顺时针自转:

源代码

#include <GL/glut.h>

#include <stdlib.h>

#include <iostream>

using namespace std;



void init();

void display();

void shape(int w, int h);

void reshape();

void DrawEarth();

void DrawMoon();

void DrawSpaceship();

void position();

void keyboard (unsigned char key, int x, int y);



static int moon = 0, earth = 0; //旋转

static GLfloat move_SP = 0.0; //平移量

static GLfloat size_SP = 1.0; //缩放量



void init() {

glClearColor (0.0, 0.0, 0.0, 0.0);

glShadeModel (GL_FLAT);

}

void DrawSpaceship() {

glPushMatrix();

glTranslatef(move_SP, 0, 0);

glScalef(size_SP, size_SP, 1); //缩放,参数含义(x轴倍数,y轴倍数,z轴倍数)

glColor3f(1.0, 1.0, 0.0); //绘制颜色RGB:黄色

glRectf(-0.02, -0.02, 0.02, 0.02); //绘制矩形

glPopMatrix();

}



void display() {

glClear (GL_COLOR_BUFFER_BIT);

position();

DrawSpaceship();

glutSwapBuffers();

}



void reshape (int w, int h) {

glViewport (0, 0, (GLsizei) w, (GLsizei) h);

glMatrixMode (GL_PROJECTION);

glLoadIdentity ();

gluPerspective(60.0, (GLfloat) w / (GLfloat) h, 1.0, 20.0);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

gluLookAt ( 0.0, 0.0, 5.0,

            0.0, 0.0, 0.0,

            0.0, 1.0, 0.0);

}



void DrawEarth() {

glPushMatrix();

glColor3f (0.0, 0.5, 1.0);

glRotatef ((GLfloat) earth, 0.0, 1.0, 0.0);

glutWireSphere(1.0, 100, 20);

glPopMatrix();

}



void DrawMoon() {

glPushMatrix();

glRotatef ((GLfloat) moon, 0.0, 1.0, 0.0);  //旋转

glTranslatef (2.0, 0.0, 0.0);  //平移

glColor3f (0.9, 0.9, 0.9);

glutSolidSphere(0.2, 80, 8);

glPopMatrix();

}



void position() {

if (moon <= 180) {

//cout << "--------1月后-----------" << endl;

DrawMoon();

DrawEarth();

} else {

//cout << "-----------2月前---------------" << endl;

DrawEarth();

DrawMoon();

}

}



void keyboard (unsigned char key, int x, int y) {

switch (key) {

case 'w': //地球自转-逆时针(从北向南看)

earth = (earth + 10) % 360;



glutPostRedisplay();

break;

case 's': //地球自转-顺时针

earth = (earth - 10 + 360) % 360;

glutPostRedisplay();

break;

case 'a': //月球公转-逆时针

moon = (moon + 5) % 360;

glutPostRedisplay();



break;

case 'd': //月球公转-顺时针

moon = (moon - 5 + 360) % 360;

glutPostRedisplay();

break;

case 'j': //飞船平移-向右

move_SP += 0.15;

glutPostRedisplay();

break;

case 'f': //飞船平移-向左

move_SP -= 0.15;

glutPostRedisplay();

break;

case 'h': //飞船缩放-变大

size_SP += 0.1;

glutPostRedisplay();

break;

case 'g': //飞船缩放-变小

size_SP -= 0.1;

glutPostRedisplay();

break;

case 27:

exit(0);

break;

default:

break;

}

//cout << "earth=" << earth << ", moon=" << moon << endl;

}



int main(int argc, char** argv) {

cout << "飞船(黄点),地球(蓝色),月球(白色)" << endl;

cout << "w键地球逆时针自转,s键地球顺时针自转;a键月球逆时针公转,d键月球顺时针公转。" << endl;

cout << "f键飞船左移,j键飞船右移,g键飞船变小,h键飞船变大。" << endl;

glutInit(&argc, argv);

glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);

glutInitWindowSize (1000, 1000);

glutInitWindowPosition (100, 100);

glutCreateWindow ("3图形变换-地月");

init();

glutDisplayFunc(display);

glutReshapeFunc(reshape);

glutKeyboardFunc(keyboard);

glutMainLoop();

return 0;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值