投影
- 投影分为两种:正投影与透视投影
- 正投影用于平面图形
- 透视投影用于3D图形,可以控制图形趋于人体眼睛看到事物一样
- 例如:下图透视投影中两个球的位置,从观察者看过去是一个前一个后,这种不需要开发者自己控制,OpenGL会计算好呈现出效果,是由投影矩阵来决定的
正投影代码
#include "GLTools.h"
#include "GLMatrixStack.h"
#include "GLFrame.h"
#include "GLFrustum.h"
#include "GLGeometryTransform.h"
#include "GLBatch.h"
#include
#ifdef __APPLE__
#include
#else
#define FREEGLUT_STATIC
#include
#endif
GLFrame viewFrame;
GLFrustum viewFrustum;
GLBatch tubeBatch;
GLBatch innerBatch;
//GLMatrixStack 堆栈矩阵
GLMatrixStack modelViewMatix;
GLMatrixStack projectionMatrix;
//几何变换的管道
GLGeometryTransform transformPipeline;
GLShaderManager shaderManager;
//启动demo,就会调用这个方法
void ChangeSize(int w, int h)
{
if (h == 0) {
h = 1;
}
glViewport(0, 0, w, h);
//设置正投影矩阵
viewFrustum.SetOrthographic(-130.0f, 130.0f, -130.0f, 130.0f, -130.0f, 130.f);
//获取并加载投影矩阵
projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix());
//设置变换管线以使用两个矩阵堆栈
transformPipeline.SetMatrixStacks(modelViewMatix, projectionMatrix);
}
// 召唤场景
void RenderScene(void)
{
//清屏、深度缓存
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//开启深度测试
glEnable(GL_DEPTH_TEST);
//绘制前压栈,将数据保存进去
modelViewMatix.PushMatrix(viewFrame);
GLfloat vRed[] = {1.0f, 0.0f, 0.0f, 1.0f};
GLfloat vGray[] = {0.75f, 0.75f, 0.75f, 1.0f};
//默认光源着色器
shaderManager.UseStockShader(GLT_SHADER_DEFAULT_LIGHT, transformPipeline.GetModelViewMatrix(), transformPipeline.GetProjectionMatrix(), vRed);
tubeBatch.Draw();
shaderManager.UseStockShader(GLT_SHADER_DEFAULT_LIGHT, transformPipeline.GetModelViewMatrix(), transformPipeline.GetProjectionMatrix(), vGray);
innerBatch.Draw();
//绘制完出栈,还原开始数据
modelViewMatix.PopMatrix();
glutSwapBuffers();
}
//对图形上下文初始化
void SetupRC()
{
//设置清屏颜色
glClearColor(0.0f, 0.0f, 0.75f, 1.0f );
// glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
//初始化着色器管理器
shaderManager.InitializeStockShaders();
tubeBatch.Begin(GL_QUADS, 200);
float fZ = 100.0f;
float bZ = -100.0f;
//左面板的颜色、顶点、光照数据
//颜色值
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
//光照法线
//接受3个表示坐标的值,指定一条垂直于三角形表面的法线向量
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
//顶点数据
tubeBatch.Vertex3f(-50.0f, 50.0f, 100.0f);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-50.0f, -50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-35.0f, -50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-35.0f,50.0f,fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(50.0f, 50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(35.0f, 50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(35.0f, -50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(50.0f,-50.0f,fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-35.0f, 50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-35.0f, 35.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(35.0f, 35.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(35.0f, 50.0f,fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-35.0f, -35.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-35.0f, -50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(35.0f, -50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(35.0f, -35.0f,fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 1.0f, 0.0f);
tubeBatch.Vertex3f(-50.0f, 50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 1.0f, 0.0f);
tubeBatch.Vertex3f(50.0f, 50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 1.0f, 0.0f);
tubeBatch.Vertex3f(50.0f, 50.0f, bZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 1.0f, 0.0f);
tubeBatch.Vertex3f(-50.0f,50.0f,bZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, -1.0f, 0.0f);
tubeBatch.Vertex3f(-50.0f, -50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, -1.0f, 0.0f);
tubeBatch.Vertex3f(-50.0f, -50.0f, bZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, -1.0f, 0.0f);
tubeBatch.Vertex3f(50.0f, -50.0f, bZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, -1.0f, 0.0f);
tubeBatch.Vertex3f(50.0f, -50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(1.0f, 0.0f, 0.0f);
tubeBatch.Vertex3f(50.0f, 50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(1.0f, 0.0f, 0.0f);
tubeBatch.Vertex3f(50.0f, -50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(1.0f, 0.0f, 0.0f);
tubeBatch.Vertex3f(50.0f, -50.0f, bZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(1.0f, 0.0f, 0.0f);
tubeBatch.Vertex3f(50.0f, 50.0f, bZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(-1.0f, 0.0f, 0.0f);
tubeBatch.Vertex3f(-50.0f, 50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(-1.0f, 0.0f, 0.0f);
tubeBatch.Vertex3f(-50.0f, 50.0f, bZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(-1.0f, 0.0f, 0.0f);
tubeBatch.Vertex3f(-50.0f, -50.0f, bZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(-1.0f, 0.0f, 0.0f);
tubeBatch.Vertex3f(-50.0f, -50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-50.0f, 50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-50.0f, -50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-35.0f, -50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-35.0f,50.0f,fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(50.0f, 50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(35.0f, 50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(35.0f, -50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(50.0f,-50.0f,fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-35.0f, 50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-35.0f, 35.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(35.0f, 35.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(35.0f, 50.0f,fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-35.0f, -35.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-35.0f, -50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(35.0f, -50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(35.0f, -35.0f,fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 1.0f, 0.0f);
tubeBatch.Vertex3f(-50.0f, 50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 1.0f, 0.0f);
tubeBatch.Vertex3f(50.0f, 50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 1.0f, 0.0f);
tubeBatch.Vertex3f(50.0f, 50.0f, bZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 1.0f, 0.0f);
tubeBatch.Vertex3f(-50.0f,50.0f,bZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, -1.0f, 0.0f);
tubeBatch.Vertex3f(-50.0f, -50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, -1.0f, 0.0f);
tubeBatch.Vertex3f(-50.0f, -50.0f, bZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, -1.0f, 0.0f);
tubeBatch.Vertex3f(50.0f, -50.0f, bZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, -1.0f, 0.0f);
tubeBatch.Vertex3f(50.0f, -50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(1.0f, 0.0f, 0.0f);
tubeBatch.Vertex3f(50.0f, 50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(1.0f, 0.0f, 0.0f);
tubeBatch.Vertex3f(50.0f, -50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(1.0f, 0.0f, 0.0f);
tubeBatch.Vertex3f(50.0f, -50.0f, bZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(1.0f, 0.0f, 0.0f);
tubeBatch.Vertex3f(50.0f, 50.0f, bZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(-1.0f, 0.0f, 0.0f);
tubeBatch.Vertex3f(-50.0f, 50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(-1.0f, 0.0f, 0.0f);
tubeBatch.Vertex3f(-50.0f, 50.0f, bZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(-1.0f, 0.0f, 0.0f);
tubeBatch.Vertex3f(-50.0f, -50.0f, bZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(-1.0f, 0.0f, 0.0f);
tubeBatch.Vertex3f(-50.0f, -50.0f, fZ);
tubeBatch.Normal3f(0.0f, 0.0f, -1.0f);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-35.0f,50.0f,bZ);
tubeBatch.Normal3f(0.0f, 0.0f, -1.0f);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-35.0f, -50.0f, bZ);
tubeBatch.Normal3f(0.0f, 0.0f, -1.0f);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-50.0f, -50.0f, bZ);
tubeBatch.Normal3f(0.0f, 0.0f, -1.0f);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-50.0f, 50.0f, bZ);
tubeBatch.Normal3f(0.0f, 0.0f, -1.0f);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(50.0f,-50.0f,bZ);
tubeBatch.Normal3f(0.0f, 0.0f, -1.0f);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(35.0f, -50.0f, bZ);
tubeBatch.Normal3f(0.0f, 0.0f, -1.0f);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(35.0f, 50.0f, bZ);
tubeBatch.Normal3f(0.0f, 0.0f, -1.0f);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(50.0f, 50.0f, bZ);
tubeBatch.Normal3f(0.0f, 0.0f, -1.0f);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(35.0f, 50.0f, bZ);
tubeBatch.Normal3f(0.0f, 0.0f, -1.0f);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(35.0f, 35.0f, bZ);
tubeBatch.Normal3f(0.0f, 0.0f, -1.0f);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-35.0f, 35.0f, bZ);
tubeBatch.Normal3f(0.0f, 0.0f, -1.0f);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-35.0f, 50.0f, bZ);
tubeBatch.Normal3f(0.0f, 0.0f, -1.0f);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(35.0f, -35.0f,bZ);
tubeBatch.Normal3f(0.0f, 0.0f, -1.0f);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(35.0f, -50.0f, bZ);
tubeBatch.Normal3f(0.0f, 0.0f, -1.0f);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-35.0f, -50.0f, bZ);
tubeBatch.Normal3f(0.0f, 0.0f, -1.0f);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-35.0f, -35.0f, bZ);
tubeBatch.End();
//内壁
innerBatch.Begin(GL_QUADS, 40);
innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f);
innerBatch.Normal3f(0.0f, 1.0f, 0.0f);
innerBatch.Vertex3f(-35.0f, 35.0f, fZ);
innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f);
innerBatch.Normal3f(0.0f, 1.0f, 0.0f);
innerBatch.Vertex3f(35.0f, 35.0f, fZ);
innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f);
innerBatch.Normal3f(0.0f, 1.0f, 0.0f);
innerBatch.Vertex3f(35.0f, 35.0f, bZ);
innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f);
innerBatch.Normal3f(0.0f, 1.0f, 0.0f);
innerBatch.Vertex3f(-35.0f,35.0f,bZ);
innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f);
innerBatch.Normal3f(0.0f, 1.0f, 0.0f);
innerBatch.Vertex3f(-35.0f, -35.0f, fZ);
innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f);
innerBatch.Normal3f(0.0f, 1.0f, 0.0f);
innerBatch.Vertex3f(-35.0f, -35.0f, bZ);
innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f);
innerBatch.Normal3f(0.0f, 1.0f, 0.0f);
innerBatch.Vertex3f(35.0f, -35.0f, bZ);
innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f);
innerBatch.Normal3f(0.0f, 1.0f, 0.0f);
innerBatch.Vertex3f(35.0f, -35.0f, fZ);
innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f);
innerBatch.Normal3f(1.0f, 0.0f, 0.0f);
innerBatch.Vertex3f(-35.0f, 35.0f, fZ);
innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f);
innerBatch.Normal3f(1.0f, 0.0f, 0.0f);
innerBatch.Vertex3f(-35.0f, 35.0f, bZ);
innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f);
innerBatch.Normal3f(1.0f, 0.0f, 0.0f);
innerBatch.Vertex3f(-35.0f, -35.0f, bZ);
innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f);
innerBatch.Normal3f(1.0f, 0.0f, 0.0f);
innerBatch.Vertex3f(-35.0f, -35.0f, fZ);
innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f);
innerBatch.Normal3f(-1.0f, 0.0f, 0.0f);
innerBatch.Vertex3f(35.0f, 35.0f, fZ);
innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f);
innerBatch.Normal3f(-1.0f, 0.0f, 0.0f);
innerBatch.Vertex3f(35.0f, -35.0f, fZ);
innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f);
innerBatch.Normal3f(-1.0f, 0.0f, 0.0f);
innerBatch.Vertex3f(35.0f, -35.0f, bZ);
innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f);
innerBatch.Normal3f(-1.0f, 0.0f, 0.0f);
innerBatch.Vertex3f(35.0f, 35.0f, bZ);
innerBatch.End();
}
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
viewFrame.RotateWorld(m3dDegToRad(-5.0), 1.0f, 0.0f, 0.0f);
if(key == GLUT_KEY_DOWN)
viewFrame.RotateWorld(m3dDegToRad(5.0), 1.0f, 0.0f, 0.0f);
if(key == GLUT_KEY_LEFT)
viewFrame.RotateWorld(m3dDegToRad(-5.0), 0.0f, 1.0f, 0.0f);
if(key == GLUT_KEY_RIGHT)
viewFrame.RotateWorld(m3dDegToRad(5.0), 0.0f, 1.0f, 0.0f);
//刷新窗口
glutPostRedisplay();
}
int main(int argc, char* argv[])
{
gltSetWorkingDirectory(argv[0]);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
glutInitWindowSize(800, 600);
glutCreateWindow("Orthographic Projection Example");
glutReshapeFunc(ChangeSize);
glutSpecialFunc(SpecialKeys);
glutDisplayFunc(RenderScene);
GLenum err = glewInit();
if (GLEW_OK != err) {
fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
return 1;
}
SetupRC();
glutMainLoop();
return 0;
}
- 正投影效果图
透视投影代码
和正投影代码中的区别还是有的,首先顶点位置肯定会有改变以及模式,其他的都一样
将
//设置正投影矩阵
viewFrustum.SetOrthographic(-130.0f, 130.0f, -130.0f, 130.0f, -130.0f, 130.f);
改为
//设置透视投影矩阵
viewFrustum.SetPerspective(35.0f, float(w)/float(h), 1.0f, 1000.0f);
#include "GLTools.h"
#include "GLMatrixStack.h"
#include "GLFrame.h"
#include "GLFrustum.h"
#include "GLGeometryTransform.h"
#include "GLBatch.h"
#include
#ifdef __APPLE__
#include
#else
#define FREEGLUT_STATIC
#include
#endif
GLFrame viewFrame;
GLFrustum viewFrustum;
GLBatch tubeBatch;
GLBatch innerBatch;
GLMatrixStack modelViewMatix;
GLMatrixStack projectionMatrix;
GLGeometryTransform transformPipeline;
GLShaderManager shaderManager;
void RenderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
modelViewMatix.PushMatrix(viewFrame);
GLfloat vRed[] = { 1.0f, 0.0f, 0.0f, 1.0f };
GLfloat vGray[] = { 0.75f, 0.75f, 0.75f, 1.0f };
shaderManager.UseStockShader(GLT_SHADER_DEFAULT_LIGHT, transformPipeline.GetModelViewMatrix(), transformPipeline.GetProjectionMatrix(), vRed);
tubeBatch.Draw();
shaderManager.UseStockShader(GLT_SHADER_DEFAULT_LIGHT, transformPipeline.GetModelViewMatrix(), transformPipeline.GetProjectionMatrix(), vGray);
innerBatch.Draw();
modelViewMatix.PopMatrix();
glutSwapBuffers();
}
void SetupRC()
{
glClearColor(0.0f, 0.0f, 0.75f, 1.0f );
glEnable(GL_DEPTH_TEST);
shaderManager.InitializeStockShaders();
viewFrame.MoveForward(450.0f);
tubeBatch.Begin(GL_QUADS, 200);
float fZ = 100.0f;
float bZ = -100.0f;
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-50.0f, 50.0f, 100.0f);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-50.0f, -50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-35.0f, -50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-35.0f,50.0f,fZ);
// Right Panel
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(50.0f, 50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(35.0f, 50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(35.0f, -50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(50.0f,-50.0f,fZ);
// Top Panel
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-35.0f, 50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-35.0f, 35.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(35.0f, 35.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(35.0f, 50.0f,fZ);
// Bottom Panel
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-35.0f, -35.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-35.0f, -50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(35.0f, -50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(35.0f, -35.0f,fZ);
// Top length section ////////////////////////////
// Normal points up Y axis
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 1.0f, 0.0f);
tubeBatch.Vertex3f(-50.0f, 50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 1.0f, 0.0f);
tubeBatch.Vertex3f(50.0f, 50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 1.0f, 0.0f);
tubeBatch.Vertex3f(50.0f, 50.0f, bZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 1.0f, 0.0f);
tubeBatch.Vertex3f(-50.0f,50.0f,bZ);
// Bottom section
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, -1.0f, 0.0f);
tubeBatch.Vertex3f(-50.0f, -50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, -1.0f, 0.0f);
tubeBatch.Vertex3f(-50.0f, -50.0f, bZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, -1.0f, 0.0f);
tubeBatch.Vertex3f(50.0f, -50.0f, bZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, -1.0f, 0.0f);
tubeBatch.Vertex3f(50.0f, -50.0f, fZ);
// Left section
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(1.0f, 0.0f, 0.0f);
tubeBatch.Vertex3f(50.0f, 50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(1.0f, 0.0f, 0.0f);
tubeBatch.Vertex3f(50.0f, -50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(1.0f, 0.0f, 0.0f);
tubeBatch.Vertex3f(50.0f, -50.0f, bZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(1.0f, 0.0f, 0.0f);
tubeBatch.Vertex3f(50.0f, 50.0f, bZ);
// Right Section
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(-1.0f, 0.0f, 0.0f);
tubeBatch.Vertex3f(-50.0f, 50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(-1.0f, 0.0f, 0.0f);
tubeBatch.Vertex3f(-50.0f, 50.0f, bZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(-1.0f, 0.0f, 0.0f);
tubeBatch.Vertex3f(-50.0f, -50.0f, bZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(-1.0f, 0.0f, 0.0f);
tubeBatch.Vertex3f(-50.0f, -50.0f, fZ);
// Pointing straight out Z
// Left Panel
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-50.0f, 50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-50.0f, -50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-35.0f, -50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-35.0f,50.0f,fZ);
// Right Panel
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(50.0f, 50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(35.0f, 50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(35.0f, -50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(50.0f,-50.0f,fZ);
// Top Panel
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-35.0f, 50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-35.0f, 35.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(35.0f, 35.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(35.0f, 50.0f,fZ);
// Bottom Panel
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-35.0f, -35.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-35.0f, -50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(35.0f, -50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(35.0f, -35.0f,fZ);
// Top length section ////////////////////////////
// Normal points up Y axis
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 1.0f, 0.0f);
tubeBatch.Vertex3f(-50.0f, 50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 1.0f, 0.0f);
tubeBatch.Vertex3f(50.0f, 50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 1.0f, 0.0f);
tubeBatch.Vertex3f(50.0f, 50.0f, bZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, 1.0f, 0.0f);
tubeBatch.Vertex3f(-50.0f,50.0f,bZ);
// Bottom section
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, -1.0f, 0.0f);
tubeBatch.Vertex3f(-50.0f, -50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, -1.0f, 0.0f);
tubeBatch.Vertex3f(-50.0f, -50.0f, bZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, -1.0f, 0.0f);
tubeBatch.Vertex3f(50.0f, -50.0f, bZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(0.0f, -1.0f, 0.0f);
tubeBatch.Vertex3f(50.0f, -50.0f, fZ);
// Left section
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(1.0f, 0.0f, 0.0f);
tubeBatch.Vertex3f(50.0f, 50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(1.0f, 0.0f, 0.0f);
tubeBatch.Vertex3f(50.0f, -50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(1.0f, 0.0f, 0.0f);
tubeBatch.Vertex3f(50.0f, -50.0f, bZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(1.0f, 0.0f, 0.0f);
tubeBatch.Vertex3f(50.0f, 50.0f, bZ);
// Right Section
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(-1.0f, 0.0f, 0.0f);
tubeBatch.Vertex3f(-50.0f, 50.0f, fZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(-1.0f, 0.0f, 0.0f);
tubeBatch.Vertex3f(-50.0f, 50.0f, bZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(-1.0f, 0.0f, 0.0f);
tubeBatch.Vertex3f(-50.0f, -50.0f, bZ);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Normal3f(-1.0f, 0.0f, 0.0f);
tubeBatch.Vertex3f(-50.0f, -50.0f, fZ);
// Left Panel
tubeBatch.Normal3f(0.0f, 0.0f, -1.0f);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-35.0f,50.0f,bZ);
tubeBatch.Normal3f(0.0f, 0.0f, -1.0f);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-35.0f, -50.0f, bZ);
tubeBatch.Normal3f(0.0f, 0.0f, -1.0f);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-50.0f, -50.0f, bZ);
tubeBatch.Normal3f(0.0f, 0.0f, -1.0f);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-50.0f, 50.0f, bZ);
// Right Panel
tubeBatch.Normal3f(0.0f, 0.0f, -1.0f);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(50.0f,-50.0f,bZ);
tubeBatch.Normal3f(0.0f, 0.0f, -1.0f);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(35.0f, -50.0f, bZ);
tubeBatch.Normal3f(0.0f, 0.0f, -1.0f);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(35.0f, 50.0f, bZ);
tubeBatch.Normal3f(0.0f, 0.0f, -1.0f);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(50.0f, 50.0f, bZ);
// Top Panel
tubeBatch.Normal3f(0.0f, 0.0f, -1.0f);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(35.0f, 50.0f, bZ);
tubeBatch.Normal3f(0.0f, 0.0f, -1.0f);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(35.0f, 35.0f, bZ);
tubeBatch.Normal3f(0.0f, 0.0f, -1.0f);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-35.0f, 35.0f, bZ);
tubeBatch.Normal3f(0.0f, 0.0f, -1.0f);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-35.0f, 50.0f, bZ);
// Bottom Panel
tubeBatch.Normal3f(0.0f, 0.0f, -1.0f);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(35.0f, -35.0f,bZ);
tubeBatch.Normal3f(0.0f, 0.0f, -1.0f);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(35.0f, -50.0f, bZ);
tubeBatch.Normal3f(0.0f, 0.0f, -1.0f);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-35.0f, -50.0f, bZ);
tubeBatch.Normal3f(0.0f, 0.0f, -1.0f);
tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f);
tubeBatch.Vertex3f(-35.0f, -35.0f, bZ);
tubeBatch.End();
innerBatch.Begin(GL_QUADS, 40);
// Insides /////////////////////////////
// Normal points up Y axis
innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f);
innerBatch.Normal3f(0.0f, 1.0f, 0.0f);
innerBatch.Vertex3f(-35.0f, 35.0f, fZ);
innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f);
innerBatch.Normal3f(0.0f, 1.0f, 0.0f);
innerBatch.Vertex3f(35.0f, 35.0f, fZ);
innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f);
innerBatch.Normal3f(0.0f, 1.0f, 0.0f);
innerBatch.Vertex3f(35.0f, 35.0f, bZ);
innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f);
innerBatch.Normal3f(0.0f, 1.0f, 0.0f);
innerBatch.Vertex3f(-35.0f,35.0f,bZ);
// Bottom section
innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f);
innerBatch.Normal3f(0.0f, 1.0f, 0.0f);
innerBatch.Vertex3f(-35.0f, -35.0f, fZ);
innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f);
innerBatch.Normal3f(0.0f, 1.0f, 0.0f);
innerBatch.Vertex3f(-35.0f, -35.0f, bZ);
innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f);
innerBatch.Normal3f(0.0f, 1.0f, 0.0f);
innerBatch.Vertex3f(35.0f, -35.0f, bZ);
innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f);
innerBatch.Normal3f(0.0f, 1.0f, 0.0f);
innerBatch.Vertex3f(35.0f, -35.0f, fZ);
// Left section
innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f);
innerBatch.Normal3f(1.0f, 0.0f, 0.0f);
innerBatch.Vertex3f(-35.0f, 35.0f, fZ);
innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f);
innerBatch.Normal3f(1.0f, 0.0f, 0.0f);
innerBatch.Vertex3f(-35.0f, 35.0f, bZ);
innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f);
innerBatch.Normal3f(1.0f, 0.0f, 0.0f);
innerBatch.Vertex3f(-35.0f, -35.0f, bZ);
innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f);
innerBatch.Normal3f(1.0f, 0.0f, 0.0f);
innerBatch.Vertex3f(-35.0f, -35.0f, fZ);
// Right Section
innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f);
innerBatch.Normal3f(-1.0f, 0.0f, 0.0f);
innerBatch.Vertex3f(35.0f, 35.0f, fZ);
innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f);
innerBatch.Normal3f(-1.0f, 0.0f, 0.0f);
innerBatch.Vertex3f(35.0f, -35.0f, fZ);
innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f);
innerBatch.Normal3f(-1.0f, 0.0f, 0.0f);
innerBatch.Vertex3f(35.0f, -35.0f, bZ);
innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f);
innerBatch.Normal3f(-1.0f, 0.0f, 0.0f);
innerBatch.Vertex3f(35.0f, 35.0f, bZ);
innerBatch.End();
}
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
viewFrame.RotateWorld(m3dDegToRad(-5.0), 1.0f, 0.0f, 0.0f);
if(key == GLUT_KEY_DOWN)
viewFrame.RotateWorld(m3dDegToRad(5.0), 1.0f, 0.0f, 0.0f);
if(key == GLUT_KEY_LEFT)
viewFrame.RotateWorld(m3dDegToRad(-5.0), 0.0f, 1.0f, 0.0f);
if(key == GLUT_KEY_RIGHT)
viewFrame.RotateWorld(m3dDegToRad(5.0), 0.0f, 1.0f, 0.0f);
// Refresh the Window
glutPostRedisplay();
}
void ChangeSize(int w, int h)
{
if(h == 0)
h = 1;
glViewport(0, 0, w, h);
//设置正投影矩阵
// viewFrustum.SetOrthographic(-130.0f, 130.0f, -130.0f, 130.0f, -130.0f, 130.0f);
viewFrustum.SetPerspective(35.0f, float(w)/float(h), 1.0f, 1000.0f);
projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix());
transformPipeline.SetMatrixStacks(modelViewMatix, projectionMatrix);
}
int main(int argc, char* argv[])
{
gltSetWorkingDirectory(argv[0]);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
glutInitWindowSize(800, 600);
glutCreateWindow("Perspective Projection Example");
glutReshapeFunc(ChangeSize);
glutSpecialFunc(SpecialKeys);
glutDisplayFunc(RenderScene);
GLenum err = glewInit();
if (GLEW_OK != err) {
fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
return 1;
}
SetupRC();
glutMainLoop();
return 0;
}
效果如下,会比正投影效果更好,更偏于真实性