1.通过二维几何变换的数学模型,编写平移、旋转、放缩、对称变换的变换矩阵。
2.理解矩阵堆栈、图形变换函数的原理,掌握其用法。
// 1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <gl/glut.h>
float fTranslate;
float fRotate;
float fScale = 1.0f; //set initial scale value to 1.0f
bool bPersp = false;
bool bAnim = false;
bool bWire = false;
bool cAnim = false;
int wHeight = 0;
int wWidth = 0;
//todo
// hint: some additional parameters may needed here when you operate the teapot
int c_x = 0, c_y = 0,c_z = 5;
float cRotate;
void Draw_Leg()
{
glScalef(1,1,3);
glutSolidCube(1.0);
}
void Draw_Scene()
{
glPushMatrix();
glTranslatef(c_x,c_y,c_z);
glRotatef(90,1,0,0);
//hint:when operate the teapot,you may need to change some parameters
if(cAnim){
glRotatef(cRotate,0,1,0);
}
glutSolidTeapot(1);
glPopMatrix();
glPushMatrix();
glTranslatef(0,0,3.5);
glScalef(5,4,1);
glutSolidCube(1.0);
glPopMatrix();
glPushMatrix();
glTranslatef(1.5,1,1.5);
Draw_Leg();
glPopMatrix();
glPushMatrix();
glTranslatef(-1.5,1,1.5);
Draw_Leg();
glPopMatrix();
glPushMatrix();
glTranslatef(1.5,-1,1.5);
Draw_Leg();
glPopMatrix();
glPushMatrix();
glTranslatef(-1.5,-1,1.5);
Draw_Leg();
glPopMatrix();
}
//更新视图
int v_x = 0, v_y = 0;
void updateView(int width, int height)
{
glViewport(v_x,v_y,width,height); //reset the current viewport
glMatrixMode(GL_PROJECTION); //select the projection matrix
glLoadIdentity(); //reset the projection matrix
float whRatio = (GLfloat)width/(GLfloat)height;
if(bPersp){
//hint: when 'p' operation, use FUNCTION gluPerspective
gluPerspective(20.0, whRatio, 1.0, 20.0);
//glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 20.0);
}
else{
glOrtho(-3,3,-3,3,-100,100);
}
glMatrixMode(GL_MODELVIEW); //select the modelview matrix
}
void reshape(int width, int height)
{
if(height == 0) //prevent a divide by zero by
{
height = 1; //making height equal one
}
wHeight = height;
wWidth = width;
updateView(wWidth, wHeight);
}
void idle()
{
glutPostRedisplay();
}
float eye[] = {0,0,8};
float center[] = {0,0,0};
//hint: you may need another ARRAY when you operate the teapot
void key(unsigned char k, int x, int y)
{
switch(k)
{
case 27:
case 'q':{ break;}
case 'p':{bPersp = !bPersp; updateView(wWidth, wHeight); break;}
case ' ':{bAnim = !bAnim; break;}
case 'o':{bWire = !bWire; break;}
//hint eye[] and center[] are the keys to solve the problems
case 'a':{
v_x-=2; updateView(wWidth, wHeight);
// eye[0]+=2; updateView(wWidth, wHeight);
break;
}
case 'd':{
v_x+=2; updateView(wWidth, wHeight);
break;
}
case 'w':{
v_y+=2; updateView(wWidth, wHeight);
break;
}
case 's':{
v_y-=2; updateView(wWidth, wHeight);
break;
}
case 'z':{
eye[2] = 8;
break;
}
case 'c':{
eye[2] = -8;
break;
}
//茶壶相关操作
case 'i':{
c_z += 1;
break;
}
case 'j':{
c_x -= 1;
break;
}
case 'k':{
c_z -= 1;
break;
}
case 'l':{
c_x += 1;
break;
}
case 'e':{
cAnim = !cAnim;
break;
}
}
}
void redraw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//场景(0,0,0)的视点中心(0,5,50),V轴向上
gluLookAt(eye[0], eye[1], eye[2], center[0],center[1],center[2],0,1,0);
if(bWire){
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
else{
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
}
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
GLfloat white[] = {1.0,1.0,1.0,1.0};
GLfloat light_pos[] = {5,5,5,1};
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
glLightfv(GL_LIGHT0, GL_AMBIENT, white);
glEnable(GL_LIGHT0);
//glTranslatef(0.0f,0.0f,-6.0f); //place the triangle at center
glRotatef(fRotate, 0 ,1.0f , 0); //rotata around Y axis
glRotatef(-90,1,0,0);
glScalef(0.2,0.2,0.2);
Draw_Scene(); //Draw Scene
if(bAnim) {fRotate += 0.5f;cRotate += 0.5f;}
//hint: when you want to rotate the teapot, you may like to add another line here =)
if(cAnim) {cRotate += 0.5f;}
glutSwapBuffers();
}
int main(int argc , char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(500,500);
int windowHandle = glutCreateWindow("你的名字");
glutDisplayFunc(redraw);
glutReshapeFunc(reshape);
glutKeyboardFunc(key);
glutIdleFunc(idle);
glutMainLoop();
return 0;
}