
最終效果
#include "GLTools.h"
#include "GLShaderManager.h"
#include "GLFrustum.h"
#include "GLBatch.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; // 著色器管理器
GLMatrixStack modelViewMatrix; // 模型視圖矩陣
GLMatrixStack projectionMatrix; // 投影矩陣
GLFrustum viewFrustum; // 視景體
GLGeometryTransform transformPipeline; // 幾何圖形變換管道
GLTriangleBatch lSphereBatch; //大球
GLTriangleBatch sSphereBatch; //小球
GLBatch floorBatch; //地板
//角色幀 照相機(jī)角色幀
GLFrame cameraFrame;
//隨機(jī)位置的球
#define NUM_SPHERES 50
GLFrame spheres[NUM_SPHERES];
void SetupRC() {
//設(shè)置clear color
glClearColor(0.0, 0.0, 0.0, 1.0);
//初始化固定管線
shaderManager.InitializeStockShaders();
//設(shè)置管線的模型視圖矩陣和投影矩陣引用
transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix);
//按一個(gè)方塊一個(gè)方塊的添加地板頂點(diǎn)
floorBatch.Begin(GL_LINES, 324);
for (GLfloat idx = -20.0; idx <= 20.0; idx+=0.5) {
floorBatch.Vertex3f(idx, -0.55, 20.0);
floorBatch.Vertex3f(idx, -0.55, -20.0);
floorBatch.Vertex3f(20, -0.55, idx);
floorBatch.Vertex3f(-20, -0.55, idx);
}
floorBatch.End();
//開(kāi)啟深度測(cè)試
glEnable(GL_DEPTH_TEST);
//初始化大球頂點(diǎn)數(shù)據(jù)
gltMakeSphere(lSphereBatch, 0.4f, 40, 80);
//初始化小球頂點(diǎn)數(shù)據(jù)
gltMakeSphere(sSphereBatch, 0.1f, 26, 13);
//初始化小球隨機(jī)位置
for (int idx = 0; idx < NUM_SPHERES; idx++) {
GLfloat x = ((GLfloat)((rand() % 400) - 200 ) * 0.1f);
GLfloat z = ((GLfloat)((rand() % 400) - 200 ) * 0.1f);
spheres[idx].SetOrigin(x, 0.0, z);
}
}
//進(jìn)行調(diào)用以繪制場(chǎng)景
void RenderScene(void) {
//地板、大球、小球顏色
static GLfloat vFloorColor[] = { 0.0f, 1.0f, 0.0f, 1.0f};
static GLfloat vLargeSphereColor[] = { 1.0f, 0.0f, 0.0f, 1.0f };
static GLfloat vSmallSphereColor[] = { 0.0f, 0.0f, 1.0f, 1.0f};
//點(diǎn)光源位置
M3DVector4f vLightPos = {0.0f, 10.0f, 5.0f, 1.0f};
//利用一個(gè)計(jì)時(shí)器獲取時(shí)間的增量
static CStopWatch rotTimer;
GLfloat yRot = rotTimer.GetElapsedSeconds() * 60.0;
//清空顏色和深度緩沖
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//入棧相機(jī)矩陣
M3DMatrix44f mCamera;
cameraFrame.GetCameraMatrix(mCamera);
modelViewMatrix.PushMatrix(mCamera);
//繪制地板
shaderManager.UseStockShader(GLT_SHADER_FLAT,
transformPipeline.GetModelViewProjectionMatrix(),
vFloorColor);
floorBatch.Draw();
//模型視圖矩陣向z軸負(fù)方向移動(dòng)3.0
modelViewMatrix.Translate(0.0, 0.0, -3.0);
//繪制小球
for (int idx = 0; idx < NUM_SPHERES; idx++) {
//模型視圖矩陣入棧
modelViewMatrix.PushMatrix();
//移動(dòng)小球到設(shè)定的位置
modelViewMatrix.MultMatrix(spheres[idx]);
//繪制小球
shaderManager.UseStockShader(GLT_SHADER_POINT_LIGHT_DIFF,
transformPipeline.GetModelViewMatrix(),
transformPipeline.GetProjectionMatrix(),
vLightPos,
vSmallSphereColor);
sSphereBatch.Draw();
//模型視圖矩陣出棧
modelViewMatrix.PopMatrix();
}
//模型視圖矩陣入棧
modelViewMatrix.PushMatrix();
//模型視圖矩陣入陣?yán)@y旋轉(zhuǎn)yRot度,完成自轉(zhuǎn)
modelViewMatrix.Rotate(yRot, 0.0f, 1.0f, 0.0f);
//繪制大球
shaderManager.UseStockShader(GLT_SHADER_POINT_LIGHT_DIFF,
transformPipeline.GetModelViewMatrix(),
transformPipeline.GetProjectionMatrix(),
vLightPos,
vLargeSphereColor);
lSphereBatch.Draw();
//模型視圖矩陣出棧
modelViewMatrix.PopMatrix();
//模型視圖矩陣入陣?yán)@y旋轉(zhuǎn)yRot * -1.5度
modelViewMatrix.Rotate(yRot * -1.5, 0.0, 1.0, 0.0);
//模型視圖矩陣入陣向x軸正方向移動(dòng)0.8
modelViewMatrix.Translate(0.8, 0.0, 0.0);
//還是利用點(diǎn)光源繪制公轉(zhuǎn)的小球
shaderManager.UseStockShader(GLT_SHADER_POINT_LIGHT_DIFF,
transformPipeline.GetModelViewMatrix(),
transformPipeline.GetProjectionMatrix(),
vLightPos,
vSmallSphereColor);
sSphereBatch.Draw();
//模型視圖矩陣出棧
modelViewMatrix.PopMatrix();
//交換緩沖幀
glutSwapBuffers();
//繪制下一幀
glutPostRedisplay();
}
//屏幕更改大小或已初始化
void ChangeSize(int nWidth, int nHeight) {
//設(shè)置視口
glViewport(0, 0, nWidth, nHeight);
//輔助計(jì)算投影矩陣
viewFrustum.SetPerspective(35.0, float(nWidth) / float(nHeight), 1.0, 100.0);
//將計(jì)算出的投影矩陣,加載到投影矩陣堆棧
projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix());
//模型矩陣還原為單位陣
modelViewMatrix.LoadIdentity();
}
//上下左右按鍵處理
void SpeacialKeys(int key,int x,int y){
//前后移動(dòng)步長(zhǎng)
float linear = 0.1;
//左右旋轉(zhuǎn)角度
float angular = float(m3dDegToRad(3.0));
switch (key) {
case GLUT_KEY_UP:
//相機(jī)前進(jìn)
cameraFrame.MoveForward(linear);
break;
case GLUT_KEY_DOWN:
//相機(jī)后退
cameraFrame.MoveForward(-linear);
break;
case GLUT_KEY_LEFT:
//相機(jī)繞z軸旋轉(zhuǎn)angular度
cameraFrame.RotateWorld(angular, 0.0, 1.0, 0.0);
break;
case GLUT_KEY_RIGHT:
//相機(jī)繞z軸旋轉(zhuǎn)-angular度
cameraFrame.RotateWorld(-angular, 0.0, 1.0, 0.0);
break;
default:
break;
}
}
int main(int argc, char* argv[])
{
gltSetWorkingDirectory(argv[0]);
//初始化配置
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_STENCIL);
//初始化窗口
glutInitWindowSize(800, 800);
glutCreateWindow("OpenGL SphereWorld");
//設(shè)置回調(diào)函數(shù)
glutReshapeFunc(ChangeSize);
glutSpecialFunc(SpeacialKeys);
glutDisplayFunc(RenderScene);
//設(shè)置GLEW
GLenum err = glewInit();
if (GLEW_OK != err) {
fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
return 1;
}
//初始化場(chǎng)景信息
SetupRC();
//開(kāi)啟loop
glutMainLoop();
return 0;
}