内容简介:在上一篇OpenGL 深度测试基础上修改本篇主要是使用矩阵相关操作,来完成视图的变换。与之前效果如下:
在上一篇OpenGL 深度测试基础上修改 main.cpp 文件,完成此 demo 。
代码
本篇主要是使用矩阵相关操作,来完成视图的变换。与之前 demo 的区别在与,之前通过改变观察点位置来获取不同位置物体的视图。次 demo 则是直接通过矩阵变换修改物体的位置,来让观察者看到不同位置的物体。代码如下:
#include "GLMatrixStack.h"
#include "GLFrame.h"
#include "GLFrustum.h"
#include "GLBatch.h"
#include "GLGeometryTransform.h"
#include <math.h>
#include <GLUT/GLUT.h>
#include "GLTools.h"
#include "StopWatch.h"
GLShaderManager shaderManager;
GLMatrixStack modelViewMatrix;
//投影矩阵
GLFrustum viewFrustum;
GLTriangleBatch cylinderBatch;
void changeSize(int w, int h) {
glViewport(0, 0, w, h);
//设置透视投影
viewFrustum.SetPerspective(30.0f, float(w) / float(h), 2.0f, 400.0f);
}
void renderScene() {
//清理缓冲区
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
static CStopWatch rotTimer;
float yRot = rotTimer.GetElapsedSeconds() * 30;
M3DMatrix44f mTranslate, mRotate, mModelView, mModelViewProjection;
//创建平移矩阵
m3dTranslationMatrix44(mTranslate,0.0f,0.0f,-8.0f);
//创建旋转矩阵
m3dRotationMatrix44(mRotate, m3dDegToRad(yRot), 0.0f,1.0f,0.0f);
//模型矩阵 = 旋转矩阵 * 平移矩阵
m3dMatrixMultiply44(mModelView, mTranslate, mRotate);
//模型视图矩阵 = 视图矩阵 * 模型矩阵
m3dMatrixMultiply44(mModelViewProjection, viewFrustum.GetProjectionMatrix(), mModelView);
GLfloat vOrange[] = {1.0f,0.6f,0.0f,1.0f};
shaderManager.UseStockShader(GLT_SHADER_FLAT,mModelViewProjection,vOrange);
cylinderBatch.Draw();
glutSwapBuffers();
glutPostRedisplay();
}
void setupRC() {
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
shaderManager.InitializeStockShaders();
glEnable(GL_DEPTH_TEST);
gltMakeCylinder(cylinderBatch, 0.3f, 0.8f, 0.8f, 100, 1);
}
int main(int argc, char* argv[]) {
gltSetWorkingDirectory(argv[0]);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
glutInitWindowSize(600, 600);
glutCreateWindow("dowZhang");
glutReshapeFunc(changeSize);
glutDisplayFunc(renderScene);
GLenum err = glewInit();
if (GLEW_OK != err) {
return 1;
}
setupRC();
glutMainLoop();
return 0;
}
复制代码
效果如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Mobilizing Web Sites
Layon, Kristofer / 2011-12 / 266.00元
Everyone has been talking about the mobile web in recent years, and more of us are browsing the web on smartphones and similar devices than ever before. But most of what we are viewing has not yet bee......一起来看看 《Mobilizing Web Sites》 这本书的介绍吧!
CSS 压缩/解压工具
在线压缩/解压 CSS 代码
html转js在线工具
html转js在线工具