资源描述:
《在opengl环境下绘球形》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、此程序在OpenGL环境下绘制球体以及填充的矩形。并对球体进行光照效果处理;对矩形进行运动化处理,当矩形与窗口的四边相撞时,程序中的TimerFunction(intvalue)函数可以控制其跳离窗口边缘,改变运动方向。该程序的源代码:#include"stdafx.h"#include#include//InitialsquarepositionandsizeGLfloatx1=100.0f;GLfloaty1=100.0f;GLsizeirsize=50;//Stepsizein
2、xandydirectionsGLfloatxstep=1.0f;GLfloatystep=1.0f;//KeeptrackofwindowschangingwidthandheightGLfloatwindowWidth;GLfloatwindowHeight;//CalledtodrawscenevoidRenderScene(void){glClear(GL_COLOR_BUFFER_BIT
3、GL_DEPTH_BUFFER_BIT);glColor3f(1.0,0.0,0.0);glutSolidSphere(50.0f
4、,200,200);//SetcurrentdrawingcolortoredglColor3f(1.0f,0.0f,0.0f);//DrawafilledrectanglewithcurrentcolorglRectf(x1,y1,x1+rsize,y1+rsize);//FlushdrawingcommandsglutSwapBuffers();}//CalledbyGLUTlibrarywhenidle(windownotbeing//resizedormoved)voidTimerFunction(intvalue){
5、//Reversedirectionwhenyoureachleftorrightedgeif(x1>windowWidth-rsize
6、
7、x1<-windowWidth)xstep=-xstep;//Reversedirectionwhenyoureachtoporbottomedgeif(y1>windowHeight-rsize
8、
9、y1<-windowHeight)ystep=-ystep;//Checkbounds.Thisisincasethewindowismade//smallerandtherectanglei
10、soutsidethenew//clippingvolumeif(x1>windowWidth-rsize)x1=windowWidth-rsize-1;if(y1>windowHeight-rsize)y1=windowHeight-rsize-1;//Actuallymovethesquarex1+=xstep;y1+=ystep;//RedrawthescenewithnewcoordinatesglutPostRedisplay();glutTimerFunc(33,TimerFunction,1);}//Setupt
11、herenderingstatevoidSetupRC(void){staticfloatred_light[4]={1.0,0.0,0.0,1.0};//定义红色光源staticfloatred_pos[4]={1.0,1.0,1.0,0.0};staticfloatblue_light[4]={0.0,0.0,1.0,1.0};//定义绿色光源staticfloatblue_pos[4]={-1.0,-1.0,-1.0,0.0};glLightfv(GL_LIGHT0,GL_DIFFUSE,red_light);glLig
12、htfv(GL_LIGHT0,GL_POSITION,red_pos);glLightfv(GL_LIGHT1,GL_DIFFUSE,blue_light);glLightfv(GL_LIGHT1,GL_POSITION,blue_pos);/*Enabledrawingfeaturesthatweneed...*/glEnable(GL_DEPTH_TEST);glEnable(GL_LIGHTING);glEnable(GL_LIGHT0);glEnable(GL_LIGHT1);//Setclearcolortoblue
13、glClearColor(0.5f,0.5f,1.0f,1.0f);}//CalledbyGLUTlibrarywhenthewindowhaschanagedsizevoidChangeSize(GLsizeiw,GLsizeih){//Preventadividebyze