
##SetupRC()
//4.设置大球模型GLfloat fRadius, GLint iSlices, GLint iStacks初始化球体的三角形批次,后面参数依次是,球半径,片段数,堆叠数
gltMakeSphere(torusBatch, 0.4, 80, 40);
##RenderScene ()
//4.获取光源位置
M3DVector4f vLightPos = {0.0f,10.0f,5.0f,1.0f};
//5.使得大球位置平移(3.0)向屏幕里面
modelViewMatrix.Translate(0.0f, 0.0f, -3.0f);
//6.压栈(复制栈顶)
modelViewMatrix.PushMatrix();
//7.大球自转
modelViewMatrix.Rotate(yRot, 0.0f, 1.0f, 0.0f);
//8.指定合适的着色器(点光源着色器)
shaderManager.UseStockShader(GLT_SHADER_POINT_LIGHT_DIFF, transformPipeline.GetModelViewMatrix(),
transformPipeline.GetProjectionMatrix(), vLightPos, vTorusColor);
torusBatch.Draw();
//9.绘制完毕则Pop
modelViewMatrix.PopMatrix();
#include "StopWatch.h"
计时器
//基于动画时间
static CStopWatch rotTimer;
float yRot = rotTimer.GetElapsedSeconds()*60.0f;
哈哈,画大球的时候怎么画成了椭圆球。
找了半天没找到哪弄错了,在很多大牛的帮助下找到原因了
//创建投影矩阵
viewFrustum.SetPerspective(35.0f, float(w/h), 1.0f, 100.0f);
float(w/h) int 相除结果是int类型了,需要拿float接
然后接着画多个小球
##SetupRC()
// 6设置随机小球
for (int i=0; i<NUM_SPHERES; i++) {
GLfloat x = ((GLfloat)((rand()%400) -200) *0.1f);
GLfloat z = ((GLfloat)((rand() % 400)-200)*0.1f);
//在y方向,将球体设置为0.0的位置,这使得它们看起来是漂浮在眼睛的高度
spheres[i].SetOrigin(x,0.0,z);
}
##RenderScene ()
//12.画小球
for (int i = 0; i < NUM_SPHERES; i++) {
modelViewMatrix.PushMatrix();
modelViewMatrix.MultMatrix(spheres[i]);
shaderManager.UseStockShader(GLT_SHADER_POINT_LIGHT_DIFF, transformPipeline.GetModelViewMatrix(),
transformPipeline.GetProjectionMatrix(), vLightPos, vSphereColor);
sphereBatch.Draw();
modelViewMatrix.PopMatrix();
}
然后画一个会转的小球
//5. 设置小球球模型
##SetupRC()
gltMakeSphere(sphereBatch, 0.1f, 26, 13);
##RenderScene ()
//13. 让一个小篮球围绕大球公众自转
modelViewMatrix.Rotate(yRot * -2.0f, 0.0f, 1.0f, 0.0f);
modelViewMatrix.Translate(0.8f, 0.0f, 0.0f);
shaderManager.UseStockShader(GLT_SHADER_FLAT,transformPipeline.GetModelViewProjectionMatrix(),vSphereColor);
sphereBatch.Draw();
//最后一个物体可以不加push和pop
画到这运行发现屏幕乱窜,大球不见了。
原因是没有加入观察者。
//加入观察者
M3DMatrix44f mCamera;
cameraFrame.GetCameraMatrix(mCamera);
modelViewMatrix.PushMatrix(mCamera);
好了球完美的绕着大球转动了。
代码提取地址
链接: https://pan.baidu.com/s/1d2TEEuiHOoRYhdNl4ZUgPA 提取码: 2trh
网友评论