【转】openGL中的选择机制

None.gif #include < windows.h >
None.gif#include
< gl / gl.h >
None.gif#include
< gl / glu.h >
None.gif#include
< gl / glut.h >
None.gif#include
< math.h >
None.gif
None.gif
#define glRGB(x,y,z)glColor3ub((GLubyte)x,(GLubyte)y,(GLubyte)z)
None.gif
None.gif
#define SUN1
None.gif
#define MERCURY2
None.gif
#define VENUS3
None.gif
#define EARTH4
None.gif
#define MARS5
None.gif
None.gifGLfloatfAspect;
None.gif
int n;
None.gif
// Lightingvalues
ExpandedBlockStart.gifContractedBlock.gif
GLfloatwhiteLight[] = dot.gif {0.35f,0.35f,0.35f,1.0f} ;
ExpandedBlockStart.gifContractedBlock.gifGLfloatsourceLight[]
= dot.gif {0.65f,0.65f,0.65f,1.0f} ;
ExpandedBlockStart.gifContractedBlock.gifGLfloatlightPos[]
= dot.gif {0.0f,0.0f,0.0f,1.0f} ;
None.gif
None.gif
None.gif
// Calledtodrawscene
None.gif
void RenderScene( void )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
//Clearthewindowwithcurrentclearingcolor
InBlock.gif
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
InBlock.gif
InBlock.gif
//Savethematrixstateanddotherotations
InBlock.gif
glMatrixMode(GL_MODELVIEW);
InBlock.gifglPushMatrix();
InBlock.gif
InBlock.gif
//Translatethewholesceneoutandintoview
InBlock.gif
glTranslatef(0.0f,0.0f,-300.0f);
InBlock.gif
InBlock.gif
//Initializethenamesstack
InBlock.gif
glInitNames();
InBlock.gifglPushName(
0);
InBlock.gif
InBlock.gif
InBlock.gif
//Setmaterialcolor,Yellow
InBlock.gif
//Sun
InBlock.gif
glRGB(255,255,255);
InBlock.gifglLoadName(SUN);
InBlock.gifglutSolidSphere(
15.0f,15,15);
InBlock.gif
InBlock.gif
//DrawMercury
InBlock.gif
glRGB(128,0,0);
InBlock.gifglPushMatrix();
InBlock.gifglTranslatef(
24.0f,0.0f,0.0f);
InBlock.gifglLoadName(MERCURY);
InBlock.gifglutSolidSphere(
2.0f,15,15);
InBlock.gifglPopMatrix();
InBlock.gif
InBlock.gif
//DrawVenus
InBlock.gif
glPushMatrix();
InBlock.gifglRGB(
128,128,255);
InBlock.gifglTranslatef(
60.0f,0.0f,0.0f);
InBlock.gifglLoadName(VENUS);
InBlock.gifglutSolidSphere(
4.0f,15,15);
InBlock.gifglPopMatrix();
InBlock.gif
InBlock.gif
//DrawtheEarth
InBlock.gif
glPushMatrix();
InBlock.gifglRGB(
0,0,255);
InBlock.gifglTranslatef(
100.0f,0.0f,0.0f);
InBlock.gifglLoadName(EARTH);
InBlock.gifglutSolidSphere(
8.0f,15,15);
InBlock.gifglPopMatrix();
InBlock.gif
InBlock.gif
//DrawMars
InBlock.gif
glRGB(255,0,0);
InBlock.gifglPushMatrix();
InBlock.gifglTranslatef(
150.0f,0.0f,0.0f);
InBlock.gifglLoadName(MARS);
InBlock.gifglutSolidSphere(
4.0f,15,15);
InBlock.gifglPopMatrix();
InBlock.gif
InBlock.gif
InBlock.gif
//Restorethematrixstate
InBlock.gif
glPopMatrix();//Modelviewmatrix
InBlock.gif

InBlock.gifglutSwapBuffers();
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
// Presenttheinformationonwhichplanet/sunwasselectedanddisplayed
None.gif
void ProcessPlanet(GLuintid)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
switch(id)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
caseSUN:
InBlock.gifMessageBox(NULL,
"YouclickedontheSun!","Info",MB_OK|MB_ICONEXCLAMATION);
InBlock.gif
break;
InBlock.gif
InBlock.gif
caseMERCURY:
InBlock.gifMessageBox(NULL,
"YouclickedonMercury!","Info",MB_OK|MB_ICONEXCLAMATION);
InBlock.gif
break;
InBlock.gif
InBlock.gif
caseVENUS:
InBlock.gifMessageBox(NULL,
"YouclickedonVenus!","Info",MB_OK|MB_ICONEXCLAMATION);
InBlock.gif
break;
InBlock.gif
InBlock.gif
caseEARTH:
InBlock.gifMessageBox(NULL,
"YouclickedonEarth!","Info",MB_OK|MB_ICONEXCLAMATION);
InBlock.gif
break;
InBlock.gif
InBlock.gif
caseMARS:
InBlock.gifMessageBox(NULL,
"YouclickedonMars!","Info",MB_OK|MB_ICONEXCLAMATION);
InBlock.gif
break;
InBlock.gif
InBlock.gif
default:
InBlock.gifMessageBox(NULL,
"Nothingwasclickedon!","Error",MB_OK|MB_ICONEXCLAMATION);
InBlock.gif
break;
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif
// Processtheselection,whichistriggeredbyarightmouse
None.gif
// clickat(xPos,yPos).
None.gif
#define BUFFER_LENGTH64
None.gif
None.gif
// 重点段
None.gif

None.gif
void ProcessSelection( int xPos, int yPos)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
//Spaceforselectionbuffer
InBlock.gif
GLuintselectBuff[BUFFER_LENGTH];
InBlock.gif
InBlock.gif
//Hitcounterandviewportstoreage
InBlock.gif
GLinthits,viewport[4];
InBlock.gif
InBlock.gif
//Setupselectionbuffer
InBlock.gif
glSelectBuffer(BUFFER_LENGTH,selectBuff);
InBlock.gif
InBlock.gif
//Gettheviewport
InBlock.gif
glGetIntegerv(GL_VIEWPORT,viewport);
InBlock.gif
InBlock.gif
//Switchtoprojectionandsavethematrix
InBlock.gif
glMatrixMode(GL_PROJECTION);
InBlock.gifglPushMatrix();
InBlock.gif
InBlock.gif
//Changerendermode
InBlock.gif
glRenderMode(GL_SELECT);
InBlock.gif
InBlock.gif
//Establishnewclippingvolumetobeunitcubearound
InBlock.gif
//mousecursorpoint(xPos,yPos)andextendingtwopixels
InBlock.gif
//intheverticalandhorzontaldirection
InBlock.gif
glLoadIdentity();
InBlock.gifgluPickMatrix(xPos,viewport[
3]-yPos,2,2,viewport);
InBlock.gif
//Applyperspectivematrix
InBlock.gif
gluPerspective(45.0f,fAspect,1.0,425.0);
InBlock.gif
InBlock.gif
//Drawthescene
InBlock.gif
RenderScene();
InBlock.gif
InBlock.gif
//Collectthehits
InBlock.gif
hits=glRenderMode(GL_RENDER);
InBlock.gif
InBlock.gif
//Ifasinglehitoccured,displaytheinfo.
InBlock.gif
if(hits==1)
InBlock.gifProcessPlanet(selectBuff[
3]);
InBlock.gif
InBlock.gif
//Restoretheprojectionmatrix
InBlock.gif
glMatrixMode(GL_PROJECTION);
InBlock.gifglPopMatrix();
InBlock.gif
InBlock.gif
//Gobacktomodelviewfornormalrendering
InBlock.gif
glMatrixMode(GL_MODELVIEW);
InBlock.gif
InBlock.gif
//n=selectBuff[0];
InBlock.gif
//printfn;
InBlock.gif

InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
// Processthemouseclick
None.gif
void MouseCallback( int button, int state, int x, int y)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
if(button==GLUT_LEFT_BUTTON&&state==GLUT_DOWN)
InBlock.gifProcessSelection(x,y);
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
// Thisfunctiondoesanyneededinitializationontherendering
None.gif
// context.
None.gif
void SetupRC()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
//Lightvaluesandcoordinates
InBlock.gif
glEnable(GL_DEPTH_TEST);//Hiddensurfaceremoval
InBlock.gif
glFrontFace(GL_CCW);//Counterclock-wisepolygonsfaceout
InBlock.gif
glEnable(GL_CULL_FACE);//Donotcalculateinsides
InBlock.gif
InBlock.gif
//Enablelighting
InBlock.gif
glEnable(GL_LIGHTING);
InBlock.gif
InBlock.gif
//Setupandenablelight0
InBlock.gif
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,whiteLight);
InBlock.gifglLightfv(GL_LIGHT0,GL_DIFFUSE,sourceLight);
InBlock.gifglLightfv(GL_LIGHT0,GL_POSITION,lightPos);
InBlock.gifglEnable(GL_LIGHT0);
InBlock.gif
InBlock.gif
//Enablecolortracking
InBlock.gif
glEnable(GL_COLOR_MATERIAL);
InBlock.gif
InBlock.gif
//SetMaterialpropertiestofollowglColorvalues
InBlock.gif
glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
InBlock.gif
InBlock.gif
//Blackbluebackground
InBlock.gif
glClearColor(0.60f,0.60f,0.60f,1.0f);
ExpandedBlockEnd.gif}

None.gif
None.gif
void ChangeSize( int w, int h)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
//Preventadividebyzero
InBlock.gif
if(h==0)
InBlock.gifh
=1;
InBlock.gif
InBlock.gif
//SetViewporttowindowdimensions
InBlock.gif
glViewport(0,0,w,h);
InBlock.gif
InBlock.gif
//Calculateaspectratioofthewindow
InBlock.gif
fAspect=(GLfloat)w/(GLfloat)h;
InBlock.gif
InBlock.gif
//Settheperspectivecoordinatesystem
InBlock.gif
glMatrixMode(GL_PROJECTION);
InBlock.gifglLoadIdentity();
InBlock.gif
InBlock.gif
//Fieldofviewof45degrees,nearandfarplanes1.0and425
InBlock.gif
gluPerspective(45.0f,fAspect,1.0,425.0);
InBlock.gif
InBlock.gif
//Modelviewmatrixreset
InBlock.gif
glMatrixMode(GL_MODELVIEW);
InBlock.gifglLoadIdentity();
ExpandedBlockEnd.gif}

None.gif
None.gif
int main( int argc, char * argv[])
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifglutInit(
&argc,argv);
InBlock.gifglutInitDisplayMode(GLUT_DOUBLE
|GLUT_RGB|GLUT_DEPTH);
InBlock.gifglutInitWindowSize(
600,300);
InBlock.gifglutCreateWindow(
"PickaPlanet");
InBlock.gifglutReshapeFunc(ChangeSize);
InBlock.gifglutMouseFunc(MouseCallback);
InBlock.gifglutDisplayFunc(RenderScene);
InBlock.gifSetupRC();
InBlock.gifglutMainLoop();
InBlock.gif
return0;
ExpandedBlockEnd.gif}

None.gif
None.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值