主要使用了:
- 矩陣構(gòu)造(平移、旋轉(zhuǎn)、綜合變換)
- 模型視圖矩陣
- 三角形批次類(創(chuàng)建花托)
- 投影矩陣(透視投影)
示例程序繪制了一個在屏幕中間旋轉(zhuǎn)的線框花托。
// ModelviewProjection.cpp
// OpenGL SuperBible
// Demonstrates OpenGL the ModelviewProjection matrix
// Program by Richard S. Wright Jr.
#include <GLTools.h> // OpenGL toolkit
#include <GLMatrixStack.h>
#include <GLFrame.h>
#include <GLFrustum.h>
#include <GLGeometryTransform.h>
#include <GLBatch.h>
#include <StopWatch.h>
#include <math.h>
#ifdef __APPLE__
#include <glut/glut.h>
#else
#define FREEGLUT_STATIC
#include <GL/glut.h>
#endif
// Global view frustum class
//使用GLFrustum類來設(shè)置透視投影
GLFrustum viewFrustum;
// The shader manager
//著色器管理器
GLShaderManager shaderManager;
// The torus
//三角形批次類
GLTriangleBatch torusBatch;
// Set up the viewport and the projection matrix
//設(shè)置視圖和投影矩陣
void ChangeSize(int w, int h)
{
// Prevent a divide by zero
//防止h變?yōu)?
if(h == 0)
h = 1;
// Set Viewport to window dimensions
//設(shè)置視口窗口尺寸
glViewport(0, 0, w, h);
//SetPerspective函數(shù)的參數(shù)是一個從頂點(diǎn)方向看去的視場角度(用角度值表示)
//寬度和高度的比值和從近剪切面到原件切面的距離
viewFrustum.SetPerspective(35.0f, float(w)/float(h), 1.0f, 1000.0f);
}
// Called to draw scene
//調(diào)用場景
void RenderScene(void)
{
// Set up time based animation
//設(shè)置時間為基礎(chǔ)的動畫
static CStopWatch rotTimer;
float yRot = rotTimer.GetElapsedSeconds() * 60.0f;
// Clear the window and the depth buffer
//清除窗口和深度緩沖區(qū)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Matrix Variables
//矩陣變量
M3DMatrix44f mTranslate, mRotate, mModelview, mModelViewProjection;
// Create a translation matrix to move the torus back and into sight
//創(chuàng)建一個平移矩陣來移動這個圓環(huán)面
m3dTranslationMatrix44(mTranslate, 0.0f, 0.0f, -2.5f);
// Create a rotation matrix based on the current value of yRot
//創(chuàng)建一個基于yrot值的旋轉(zhuǎn)矩陣
m3dRotationMatrix44(mRotate, m3dDegToRad(yRot), 0.0f, 1.0f, 0.0f);
// Add the rotation to the translation, store the result in mModelView
//加上旋轉(zhuǎn)的位移,結(jié)果存儲在mmodelview
m3dMatrixMultiply44(mModelview, mTranslate, mRotate);
// Add the modelview matrix to the projection matrix,
// the final matrix is the ModelViewProjection matrix.
//添加模型視圖矩陣的投影矩陣, 最后的矩陣是modelviewprojection矩陣。
m3dMatrixMultiply44(mModelViewProjection, viewFrustum.GetProjectionMatrix(),mModelview);
// Pass this completed matrix to the shader, and render the torus
//把這個已完成的矩陣傳遞給著色,并渲染這個圓環(huán)面
GLfloat vBlack[] = { 0.0f, 0.0f, 0.0f, 1.0f };
shaderManager.UseStockShader(GLT_SHADER_FLAT, mModelViewProjection, vBlack);
torusBatch.Draw();
// Swap buffers, and immediately refresh
//交換緩沖區(qū),并立即刷新
glutSwapBuffers();
glutPostRedisplay();
}
// This function does any needed initialization on the rendering
// context.
//此函數(shù)在渲染上下文中需要初始化任何初始化。
void SetupRC()
{
// Black background
glClearColor(0.8f, 0.8f, 0.8f, 1.0f );
glEnable(GL_DEPTH_TEST);
shaderManager.InitializeStockShaders();
// This makes a torus
gltMakeTorus(torusBatch, 0.4f, 0.15f, 30, 30);
//用于控制多邊形的顯示方式
//表示物體的所有面向面用線段顯示
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
///////////////////////////////////////////////////////////////////////////////
// Main entry point for GLUT based programs
int main(int argc, char* argv[])
{
gltSetWorkingDirectory(argv[0]);//設(shè)置當(dāng)前工作目錄
glutInit(&argc, argv);//傳輸命令行參數(shù)并初始化GLUT庫
//使用雙緩沖窗口和使用RGBA顏色模式
//GLUT_DEPTH位標(biāo)志將一個深度緩沖區(qū)分配為顯示的一部分,因此我們可以執(zhí)行深度測試
// GLUT_STENCIL確保我們也會有一個可用的模板緩沖區(qū)
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
glutInitWindowSize(800, 600);//窗口大小
glutCreateWindow("ModelViewProjection Example");//窗口標(biāo)題
glutReshapeFunc(ChangeSize);//改變窗口大小的回調(diào)函數(shù)
glutDisplayFunc(RenderScene);//OpenGL渲染代碼
//初始化glew庫
GLenum err = glewInit();
if (GLEW_OK != err) {
fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
return 1;
}
SetupRC();//RC代表渲染環(huán)境(Rendering Context)
glutMainLoop();//主消息循環(huán)
return 0;
}
運(yùn)行結(jié)果:
ModelviewProjection