OpenGL第十一课Word格式.docx
- 文档编号:3964899
- 上传时间:2023-05-02
- 格式:DOCX
- 页数:39
- 大小:35.99KB
OpenGL第十一课Word格式.docx
《OpenGL第十一课Word格式.docx》由会员分享,可在线阅读,更多相关《OpenGL第十一课Word格式.docx(39页珍藏版)》请在冰点文库上搜索。
每3帧一次看起来很不错,变量hold将存放一个用来对旗形波浪进行光滑的浮点数。
这几行添加在程序头部,位于最后一行#include之后、GLuinttexture[1]之前的位置。
floatpoints[45][45][3];
//Points网格顶点数组
intwiggle_count=0;
//指定旗形波浪的运动速度
GLfloathold;
//临时变量
然后下移至LoadGLTextures()子过程。
本课中使用的纹理文件名是Tim.bmp。
找到LoadBMP("
Data/NeHe.bmp"
)这一句,并用LoadBMP("
Data/Tim.bmp"
)替换它。
if(TextureImage[0]=LoadBMP("
))//载入位图
接着在InitGL()函数的尾部returnTRUE之前,添加如下的代码。
glPolygonMode(GL_BACK,GL_FILL);
//后表面完全填充
glPolygonMode(GL_FRONT,GL_LINE);
//前表面使用线条绘制
上面的代码指定使用完全填充模式来填充多边形区域的背面(译者:
或者叫做后表面吧)。
相反,多边形的正面(译者:
前表面)则使用轮廓线填充了。
这些方式完全取决于您的个人喜好。
并且与多边形的方位或者顶点的方向有关。
详情请参考红宝书(RedBook)。
这里我顺便推销一本推动我学习OpenGL的好书—Addison-Wesley出版的《Programmer'
sGuidetoOpenGL》。
个人以为这是学习OpenGL的无价之宝。
接着上面的代码并在returnTRUE这一句之前,添加如下的几行。
//沿X平面循环
for(intx=0;
x<
45;
x++)
{
//沿Y平面循环
for(inty=0;
y<
y++)
{
//向表面添加波浪效果
points[x][y][0]=float((x/5.0f)-4.5f);
points[x][y][1]=float((y/5.0f)-4.5f);
points[x][y][2]=float(sin((((x/5.0f)*40.0f)/360.0f)*3.141592654*2.0f));
}
}
这里感谢GrahamGibbons关于使用整数循环变量消除波浪间的脉冲锯齿的建议。
上面的两个循环初始化网格上的点。
使用整数循环可以消除由于浮点运算取整造成的脉冲锯齿的出现。
我们将x和y变量都除以5,再减去4.5。
这样使得我们的波浪可以“居中”(译者:
这样计算所得结果将落在区间[-4.5,4.5]之间)。
点[x][y][2]最后的值就是一个sine函数计算的结果。
Sin()函数需要一个弧度参变量。
将float_x乘以40.0f,得到角度值。
然后除以360.0f再乘以PI,乘以2,就转换为弧度了。
接着我将彻底重写DrawGLScene函数。
intDrawGLScene(GLvoid)//绘制我们的GL场景
intx,y;
//循环变量
floatfloat_x,float_y,float_xb,float_yb;
//用来将旗形的波浪分割成很小的四边形
我们使用不同的变量来控制循环。
下面的代码中大多数变量除了用来控制循环和存储临时变量之外并没有什么别的用处。
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//清除屏幕和深度缓冲
glLoadIdentity();
//重置当前的模型观察矩阵
glTranslatef(0.0f,0.0f,-12.0f);
//移入屏幕12个单位
glRotatef(xrot,1.0f,0.0f,0.0f);
//绕X轴旋转
glRotatef(yrot,0.0f,1.0f,0.0f);
//绕Y轴旋转
glRotatef(zrot,0.0f,0.0f,1.0f);
//绕Z轴旋转
glBindTexture(GL_TEXTURE_2D,texture[0]);
//选择纹理
正如您所见,上面的代码和第六课的很类似,唯一的区别就是我将场景挪的离镜头更远了一些。
glBegin(GL_QUADS);
//四边形绘制开始
for(x=0;
x<
44;
x++)//沿X平面0-44循环(45点)
for(y=0;
y<
y++)//沿Y平面0-44循环(45点)
接着开始使用循环进行多边形绘制。
这里使用整型可以避免我以前所用的int()强制类型转换。
float_x=float(x)/44.0f;
//生成X浮点值
float_y=float(y)/44.0f;
//生成Y浮点值
float_xb=float(x+1)/44.0f;
//X浮点值+0.0227f
float_yb=float(y+1)/44.0f;
//Y浮点值+0.0227f
上面我们使用4个变量来存放纹理坐标。
每个多边形(网格之间的四边形)分别映射了纹理的1/44×
1/44部分。
循环首先确定左下顶点的值,然后我们据此得到其他三点的值。
glTexCoord2f(float_x,float_y);
//第一个纹理坐标(左下角)
glVertex3f(points[x][y][0],points[x][y][1],points[x][y][2]);
glTexCoord2f(float_x,float_yb);
//第二个纹理坐标(左上角)
glVertex3f(points[x][y+1][0],points[x][y+1][1],points[x][y+1][2]);
glTexCoord2f(float_xb,float_yb);
//第三个纹理坐标(右上角)
glVertex3f(points[x+1][y+1][0],points[x+1][y+1][1],points[x+1][y+1][2]);
glTexCoord2f(float_xb,float_y);
//第四个纹理坐标(右下角)
glVertex3f(points[x+1][y][0],points[x+1][y][1],points[x+1][y][2]);
glEnd();
//四边形绘制结束
上面几行使用glTexCoord2f()和glVertex3f()载入数据。
提醒一点:
四边形是逆时针绘制的。
这就是说,您开始所见到的表面是背面。
后表面完全填充了,前表面由线条组成。
如果您按顺时针顺序绘制的话,您初始时见到的可能是前表面。
也就是说您将看到网格型的纹理效果而不是完全填充的。
if(wiggle_count==2)//用来降低波浪速度(每隔2帧一次)
每绘制两次场景,循环一次sine值,以产生运动效果。
45;
y++)//沿Y平面循环
hold=points[0][y][2];
//存储当前左侧波浪值
for(x=0;
x++)//沿X平面循环
//当前波浪值等于其右侧的波浪值
points[x][y][2]=points[x+1][y][2];
points[44][y][2]=hold;
//刚才的值成为最左侧的波浪值
wiggle_count=0;
//计数器清零
wiggle_count++;
//计数器加一
上面所作的事情是先存储每一行的第一个值,然后将波浪左移一下,是图象产生波浪。
存储的数值挪到末端以产生一个永无尽头的波浪纹理效果。
然后重置计数器wiggle_count以保持动画的进行。
上面的代码由NeHe(2000年2月)修改过,以消除波浪间出现的细小锯齿。
xrot+=0.3f;
//X轴旋转
yrot+=0.2f;
//Y轴旋转
zrot+=0.4f;
//Z轴旋转
returnTRUE;
//返回
标准的NeHe旋转增量。
现在编译并运行程序,您将看到一个漂亮的位图波浪。
除了嘘声一片之外,我不敢确信大家的反应。
但我希望大家能从这一课中学到点什么。
如果您有任何问题或者需要澄清的地方,请随便联络我。
感谢大家。
Lesson11
Wellgreetingsall.Forthoseofyouthatwanttoseewhatwearedoinghere,youcancheckitoutattheendofmydemo/hackWorthless!
IamboscoandIwilldomybesttoteachyouguyshowtodotheanimated,sine-wavepicture.ThistutorialisbasedonNeHe'
stutorial#6andyoushouldhaveatleastthatmuchknowledge.YoushoulddownloadthesourcepackageandplacethebitmapI'
veincludedinadirectorycalleddatawhereyoursourcecodeis.Oruseyourowntextureifit'
sanappropriatesizetobeusedasatexturewithOpenGL.
Firstthingsfirst.OpenTutorial#6inVisualC++andaddthefollowingincludestatementrightaftertheother#includestatements.The#includebelowallowsustoworkwithcomplexmathsuchassineandcosine.
//ForTheSin()Function
We'
llusethearraypointstostoretheindividualx,y&
zcoordinatesofourgrid.Thegridis45pointsby45points,whichinturnmakes44quadsx44quads.wiggle_countwillbeusedtokeeptrackofhowfastthetexturewaves.Everythreeframeslooksprettygood,andthevariableholdwillstoreafloatingpointvaluetosmoothoutthewavingoftheflag.Theselinescanbeaddedatthetopoftheprogram,somewhereunderthelast#includeline,andbeforetheGLuinttexture[1]line.
//TheArrayForThePointsOnTheGridOfOur"
Wave"
//CounterUsedToControlHowFastFlagWaves
//TemporarilyHoldsAFloatingPointValue
MovedownthetheLoadGLTextures()procedure.WewanttousethetexturecalledTim.bmp.FindLoadBMP("
)andreplaceitwithLoadBMP("
).
))//LoadTheBitmap
NowaddthefollowingcodetothebottomoftheInitGL()functionbeforereturnTRUE.
//BackFaceIsFilledIn
//FrontFaceIsDrawnWithLines
Thesesimplyspecifythatwewantbackfacingpolygonstobefilledcompletelyandthatwewantfrontfacingpolygonstobeoutlinedonly.Mostlypersonalpreferenceatthispoint.Hastodowiththeorientationofthepolygonorthedirectionofthevertices.SeetheRedBookformoreinformationonthis.Incidentally,whileI'
matit,letmeplugthebookbysayingit'
soneofthedrivingforcesbehindmelearningOpenGL,nottomentionNeHe'
ssite!
ThanksNeHe.BuyTheProgrammer'
sGuidetoOpenGLfromAddison-Wesley.It'
saninvaluableresourceasfarasI'
mconcerned.Ok,backtothetutorial.Rightbelowthecodeabove,andabovereturnTRUE,addthefollowinglines.
//LoopThroughTheXPlane
//LoopThroughTheYPlane
//ApplyTheWaveToOurMesh
ThankstoGrahamGibbonsforsuggestinganintegerlooptogetridofthespikeintheripple.
Thetwoloopsaboveinitializethepointsonourgrid.Iinitializevariablesinmylooptolocalizetheminmymindasmerelyloopvariables.Notsureit'
skosher.Weuseintegerloopstopreventoddgraphicalglitchesthatappearwhenfloatingpointcalculationsareused.Wedividethexandyvariablesby5(i.e.45/9=5)andsubtract4.5fromeachofthemtocenterthe"
wave"
.Thesameeffectcouldbeaccomplishedwithatranslate,butIpreferthismethod.
Thefinalvaluepoints[x][y][2]statementisoursinevalue.Thesin()functionrequiresradians.Wetakeourdegreevalue,whichisourfloat_xmultipliedby40.0f.Oncewehavethat,toconverttoradianswetakethedegree,divideby360.0f,multiplybypi,oranapproximationandthenmultiplyby2.0f.
I'
mgoingtore-writetheDrawGLScenefunctionfromscratchsocleanitoutanditreplacewiththefollowingcode.
intDrawGLScene(GLvoid)//DrawOurGLScene
//LoopVariables
//UsedToBreakTheFlagIntoTinyQuads
Differentvariablesusedforcontrollingtheloops.Seethecodebelowbutmostoftheseserveno"
specific"
purposeotherthancontrollingloopsandstoringtemporaryvalues.
//ClearTheScreenAndDepthBuffer
//ResetTheCurrentMatrix
//Translate17UnitsIntoTheScreen
//RotateOnTheXAxis
//RotateOnTheYAxis
//RotateOnTheZAxis
//SelectOurTexture
You'
veseenallofthisbeforeaswell.Sameasintutorial#6exceptImerelypushmyscenebackawayfromthecameraabitmore.
//StartDrawingOurQuads
x++)//LoopThroughTheXPlane0-44(45Points)
y++)//LoopThroughTheYPlane0-44(45Points)
Merelystartsthelooptodrawourpolygons.Iuseintegersheretokeepfromhavingtousetheint()functionasIdidearliertogetthearrayreferencereturnedasaninteger.
//CreateAFloatingPointXValue
//CreateAFloatingPointYValue
//CreateAFloatingPointYValue+0.0227f
Weusethefourvariablesaboveforthetexturecoordinates.Eachofourpolygons(s
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- OpenGL 第十一课 第十一
![提示](https://static.bingdoc.com/images/bang_tan.gif)