《计算机图形学》实验三:图形变换

本博客计算机图形学系列文章索引:

地址:《计算机图形学系列相关文章索引(持续更新)》


参考教材:《计算机图形学》 机械工业出版社 ,徐文鹏主编

源代码如下:

#include 
#include 
#include 

GLfloat rtri=0.0f;

//链接必要的库文件
#pragma comment( lib, "opengl32.lib" )	 
#pragma comment( lib, "glu32.lib" )	    
#pragma comment( lib, "glut32.lib" )	 

void Init(void)
{
	glClearColor(0.0f,0.0f,0.0f,0.0f);
	glShadeModel(GL_FLAT);
	glEnable(GL_DEPTH_TEST);
}

void Display(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glLoadIdentity();
	//平移变换函数,Z轴负半轴平移6.0f个单位
	glTranslatef(0.0f,0.0f,-6.0f);
	//旋转变换函数,以向量(1,0,0)为轴旋转rtri度
	glRotatef(rtri,1.0f,0.0f,0.0f);
	//比例变换函数,沿X轴拉伸到原来的1.5倍  
	glScalef(1.5f,1.0f,1.0f);
	glBegin(GL_TRIANGLES);
		glColor3f(1.0f,0.0f,0.0f);
		glVertex3f(0.0f,1.0f,0.0f);
		glColor3f(0.0f,1.0f,0.0f);
		glVertex3f(-1.0f,-1.0f,1.0f);
		glColor3f(0.0f,0.0f,1.0f);
		glVertex3f(1.0f,-1.0f,1.0f);

		glColor3f(1.0f,0.0f,0.0f);
		glVertex3f(0.0f,1.0f,0.0f);
		glColor3f(0.0f,0.0f,1.0f);
		glVertex3f(1.0f,-1.0f,1.0f);
		glColor3f(0.0f,1.0f,0.0f);
		glVertex3f(1.0f,-1.0f,-1.0f);

		glColor3f(1.0f,0.0f,0.0f);
		glVertex3f(0.0f,1.0f,0.0f);
		glColor3f(0.0f,1.0f,0.0f);
		glVertex3f(1.0f,-1.0f,-1.0f);
		glColor3f(0.0f,0.5f,0.5f);
		glVertex3f(-1.0f,-1.0f,-1.0f);


		glColor3f(1.0f,0.0f,0.0f);
		glVertex3f(0.0f,1.0f,0.0f);
		glColor3f(0.0f,0.0f,1.0f);
		glVertex3f(-1.0f,-1.0f,-1.0f);
		glColor3f(0.5f,0.5f,0.0f);
		glVertex3f(-1.0f,-1.0f,1.0f);
	glEnd();
	rtri += 1.0f;
	glutSwapBuffers();		
}

void ChangeSize(GLsizei width,GLsizei height)
{
	glViewport(0,0,width,height);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

}

int _tmain(int argc, _TCHAR* argv[])
{
	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
	glutInitWindowSize(640,480);
	glutInitWindowPosition(100,100);
	glutCreateWindow("实验三:变换");
	Init();
	glutDisplayFunc(Display);
	glutReshapeFunc(ChangeSize);
	glutIdleFunc(Display);
    glutMainLoop();
	return 0;
}

实验结果如下,图中是一个金字塔的形状,并且沿Z轴在不停旋转。


你可能感兴趣的:(计算机图形)