图形学opengl实验二-桌子的矩阵变换

在OpenGL编程基础上,通过实现实验内容,掌握OpenGL的矩阵使用,并验证课程中矩阵变换的内容:

图形学opengl实验二-桌子的矩阵变换_第1张图片

图形学opengl实验二-桌子的矩阵变换_第2张图片

#include 
float size = 0.25; //缩放
float fTranslate;
float fRotate;
float fScale = 1.0f;								// set inital scale value to 1.0f

void Draw_Leg() // This function draws one of the table leg
{
	//绘制一个桌子腿
	glPushMatrix();
	glScalef(1.0f, 1.0f, 3.0f);
	glutWireCube(1.0*size);
	glPopMatrix();

}
void Draw_Table() // This function draws a Table
{
	glPushMatrix(); //将当前矩阵保存入堆栈顶(保存当前矩阵)。
	glScalef(5.0f, 4.0f, 1.0f); //缩放函数
	glutWireCube(1 * size);  //绘制线立方体
	glPopMatrix(); //pop出来
	
	glPushMatrix();         //左下角的柱子
	glTranslatef(-1.5*size, -1 * size, -1.5*size); //尺寸是相对于中心的坐标原点,按照实验指导上的大小进行的
	Draw_Leg();
	glPopMatrix();

	glPushMatrix();         //右下角的柱子
	glTranslatef(1.5*size, -1 * size, -1.5*size);
	Draw_Leg();
	glPopMatrix();

	glPushMatrix();         //右上角的柱子
	glTranslatef(1.5*size, 1 * size, -1.5*size);
	Draw_Leg();
	glPopMatrix();

	glPushMatrix();         //左上角的柱子
	glTranslatef(-1.5*size, 1 * size, -1.5*size);
	Draw_Leg();
	glPopMatrix();

}
void reshape(int width, int height)
{
	if (height == 0)										// Prevent A Divide By Zero By
	{
		height = 1;										// Making Height Equal One
	}

	glViewport(0, 0, width, height);						// Reset The Current Viewport

	glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix
	glLoadIdentity();									// Reset The Projection Matrix

														// Calculate The Aspect Ratio Of The Window
	gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);

	glMatrixMode(GL_MODELVIEW);							// Select The Modelview Matrix
	glLoadIdentity();									// Reset The Modelview Matrix
}

void idle()
{
	glutPostRedisplay();
}

void redraw()
{
	// If want display in wireframe mode
	//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

	glClear(GL_COLOR_BUFFER_BIT); //当前缓冲区清除值,这里是清除颜色缓冲
	
	glLoadIdentity();									// Reset The Current Modelview Matrix

	glPushMatrix();
	glTranslatef(-2.0f, 0.0f, -6.0f);				// Place the triangle Left
	glTranslatef(0.0f, fTranslate, 0.0f);			// Translate in Y direction
	Draw_Table();								// Draw triangle					
	glPopMatrix();

	glPushMatrix();
	glTranslatef(0.0f, 0.0f, -6.0f);					// Place the triangle at Center
	glRotatef(fRotate, 0, 1.0f, 0);					// Rotate around Y axis
	Draw_Table();								// Draw triangle
	glPopMatrix();


	glPushMatrix();
	glTranslatef(2.0f, 0.0f, -6.0f);			// Place the triangle Right
	glScalef(fScale, fScale, fScale);		// Scale with the same value in x,y,z direction
	Draw_Table();						// Draw triangle						
	glPopMatrix();


	fTranslate += 0.005f;
	fRotate += 0.5f;
	fScale -= 0.005f;

	if (fTranslate > 0.5f) fTranslate = 0.0f;
	if (fScale < 0.5f)     fScale = 1.0f;
	glutSwapBuffers();
}

int main(int argc, char *argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
	glutInitWindowSize(640, 480);               ///////////////////////////////////////
	int windowHandle
		= glutCreateWindow("Simple GLUT App");

	glutDisplayFunc(redraw);
	glutReshapeFunc(reshape);					///////////////////////////////////////
	glutIdleFunc(idle);							///////////////////////////////////////

	glutMainLoop();

	return 0;
}



你可能感兴趣的:(图形学&图像信息处理)