计算机图形学——实验四 图形基本变换

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;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
计算机图形学——几何工具算法详解》PDF是一本关于计算机图形学的书籍,主要介绍了图形学中的几何工具算法。 该书详细解析了计算机图形学中的几何工具算法,涵盖了从基础知识到高级算法的内容。首先,书中介绍了计算机图形学基本概念和原理,包括图形学的历史背景、矢量、二维坐标系和三维坐标系等基础知识。然后,书中详细介绍了几何变换算法,包括平移、旋转、缩放等操作对图形的影响及其在计算机图形学中的应用。此外,还介绍了曲线和曲面的绘制算法,如贝塞尔曲线、B样条曲线和NURBS曲面等,以及相关算法的实现方法。另外,书中还详细阐述了光照和着色算法,包括光照模型、阴影生成和着色技术等。 《计算机图形学——几何工具算法详解》PDF书籍内容丰富、详细,适合计算机图形学领域的学习者和研究者阅读。通过学习该书,读者可以系统地了解计算机图形学的几何工具算法,掌握图形学中的基本概念和实际应用技术。此外,书中还提供了大量的实例和案例,有助于读者将理论知识应用到实际问题解决中。 总之,通过阅读《计算机图形学——几何工具算法详解》PDF,读者可以深入了解计算机图形学的几何工具算法,掌握相关的基本概念和实际应用技术,对计算机图形学领域有更深入的认识和理解。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值