OpenGL 入门教程(六) 旋转加平移

// SphereWorld.cpp
// OpenGL SuperBible
// New and improved (performance) sphere world
// Program by Richard S. Wright Jr.


#include <GLTools.h>
#include <GLShaderManager.h>
#include <GLFrustum.h>
#include <GLBatch.h>
#include <GLFrame.h>
#include <GLMatrixStack.h>
#include <GLGeometryTransform.h>
#include <StopWatch.h>


#include <math.h>
#include <stdio.h>


#ifdef __APPLE__
#include <glut/glut.h>
#else
#define FREEGLUT_STATIC
#include <GL/glut.h>
#endif




GLShaderManager		shaderManager;			// Shader Manager
GLMatrixStack		modelViewMatrix;		// Modelview Matrix
GLMatrixStack		projectionMatrix;		// Projection Matrix
GLFrustum			viewFrustum;			// View Frustum
GLGeometryTransform	transformPipeline;		// Geometry Transform Pipeline


GLTriangleBatch		torusBatch;
GLBatch				floorBatch;
GLTriangleBatch     sphereBatch;
GLFrame             cameraFrame;


//
// This function does any needed initialization on the rendering
// context. 
void SetupRC()
{
	// Initialze Shader Manager
	shaderManager.InitializeStockShaders();


	glEnable(GL_DEPTH_TEST);
	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);


	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);


	// This makes a torus
	gltMakeTorus(torusBatch, 0.4f, 0.15f, 30, 30);


	// This make a sphere
	gltMakeSphere(sphereBatch, 0.1f, 26, 13);


	floorBatch.Begin(GL_LINES, 324);
	for (GLfloat x = -20.0; x <= 20.0f; x += 0.5) {
		floorBatch.Vertex3f(x, -0.55f, 20.0f);
		floorBatch.Vertex3f(x, -0.55f, -20.0f);


		floorBatch.Vertex3f(20.0f, -0.55f, x);
		floorBatch.Vertex3f(-20.0f, -0.55f, x);
	}
	floorBatch.End();
}




///
// Screen changes size or is initialized
void ChangeSize(int nWidth, int nHeight)
{
	glViewport(0, 0, nWidth, nHeight);


	// Create the projection matrix, and load it on the projection matrix stack
	viewFrustum.SetPerspective(35.0f, float(nWidth) / float(nHeight), 1.0f, 100.0f);
	projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix());


	// Set the transformation pipeline to use the two matrix stacks 
	transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix);
}




// Respond to arrow keys by moving the camera frame of reference
void SpecialKeys(int key, int x, int y)
{
	float linear = 0.1f;
	float angular = float(m3dDegToRad(5.0f));


	if (key == GLUT_KEY_UP)
		cameraFrame.MoveForward(linear);


	if (key == GLUT_KEY_DOWN)
		cameraFrame.MoveForward(-linear);


	if (key == GLUT_KEY_LEFT)
		cameraFrame.RotateWorld(angular, 0.0f, 1.0f, 0.0f);


	if (key == GLUT_KEY_RIGHT)
		cameraFrame.RotateWorld(-angular, 0.0f, 1.0f, 0.0f);
}


// Called to draw scene
void RenderScene(void)
{
	// Color values
	static GLfloat vFloorColor[] = { 0.0f, 1.0f, 0.0f, 1.0f };
	static GLfloat vTorusColor[] = { 1.0f, 0.0f, 0.0f, 1.0f };
	static GLfloat vSphereColor[] = { 0.0f, 0.0f, 1.0f, 1.0f };


	// Time Based animation
	static CStopWatch	rotTimer;
	float yRot = rotTimer.GetElapsedSeconds() * 60.0f;


	// Clear the color and depth buffers
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


	/*
	Stack:modelViewMatrix
	*/
	// Save the current modelview matrix (the identity matrix)
	modelViewMatrix.PushMatrix();


	/*
	Stack:
	top:modelViewMatrix
		modelViewMatrix
	*/
	M3DMatrix44f mCamera;
	cameraFrame.GetCameraMatrix(mCamera);//设置摄像机视口矩阵
	modelViewMatrix.PushMatrix(mCamera);//模型视图矩阵关联摄像机矩阵


	// Draw the ground
	shaderManager.UseStockShader(GLT_SHADER_FLAT,
		transformPipeline.GetModelViewProjectionMatrix(),
		vFloorColor);
	floorBatch.Draw();


	// Draw the spinning Torus
	modelViewMatrix.Translate(0.0f, 0.0f, -2.5f);//模型视图矩阵向后平移


	/*
	Stack:
	top:modelViewMatrix + Z轴平移-2.5f
	modelViewMatrix
	modelViewMatrix
	*/
	// Save the Translation
	modelViewMatrix.PushMatrix();


	/*
	Stack:
	top:modelViewMatrix + Z轴平移-2.5f + 绕Y轴旋转
	modelViewMatrix
	modelViewMatrix
	*/
	// Apply a rotation and draw the torus
	modelViewMatrix.Rotate(yRot, 0.0f, 1.0f, 0.0f);//绕着y轴旋转
	shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(),
		vTorusColor);
	torusBatch.Draw();
	/*
	Stack:
	top:modelViewMatrix
		modelViewMatrix
	*/
	modelViewMatrix.PopMatrix(); // "Erase" the Rotation from before
	/*
	Stack:
	top:modelViewMatrix + 绕Y轴旋转 + 平移0.8
	modelViewMatrix
	*/
	// Apply another rotation, followed by a translation, then draw the sphere
	
	modelViewMatrix.Rotate(yRot * -2.0f, 0.0f, 0.1f, 0.0f);//绕着y轴旋转并平移,原点为(0,0,0)
	//如果把旋转放到平移的后面(下面)执行,将绕着Y轴旋转,原点为(0,8,0,0)
	modelViewMatrix.Translate(0.8f, 0.0f, 0.0f);//浮点数指的是比例
	shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(),
		vSphereColor);
	sphereBatch.Draw();


	// Restore the previous modleview matrix (the identity matrix)
	/*
	Stack:
	top:modelViewMatrix
	*/
	modelViewMatrix.PopMatrix();
	/*
	Stack:
	top:(其他)
	*/
	modelViewMatrix.PopMatrix();
	// Do the buffer Swap
	glutSwapBuffers();


	// Tell GLUT to do it again
	glutPostRedisplay();
}








int main(int argc, char* argv[])
{
	gltSetWorkingDirectory(argv[0]);


	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(800, 600);


	glutCreateWindow("OpenGL SphereWorld");


	glutSpecialFunc(SpecialKeys);
	glutReshapeFunc(ChangeSize);
	glutDisplayFunc(RenderScene);


	GLenum err = glewInit();
	if (GLEW_OK != err) {
		fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
		return 1;
	}




	SetupRC();
	glutMainLoop();
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值