《opengl+delphi-鼠标拾取》由会员分享,可在线阅读,更多相关《opengl+delphi-鼠标拾取(28页珍藏版)》请在金锄头文库上搜索。
1、opengl+delphi 鼠标拾取 opengl+delphi 鼠标拾取/-/ OBJECT PICKING TUTORIAL/ Author: Ben Humphrey -/ Delphi Converter: David DavesLord Caouette -/ OpenGL Template Author: Jan Horn - http:/www.sulaco.co.za/-program OpenGLApp;usesWindows,Messages,OpenGL,Textures;constWND_TITLE = OpenGL App by Jan Horn;FPS_TIMER
2、= 1; / Timer to calculate FPSFPS_INTERVAL = 1000; / Calculate FPS every 1000 msSUN = 100;/ This is the object ID for the SUNEARTH = 101;/ This is the object ID for the EARTHPLUTO= 102;/ This is the object ID for PLUTOvarh_Wnd : HWND; / Global window handleh_DC : HDC; / Global device contexth_RC : HG
3、LRC; / OpenGL rendering contextkeys : Array0.255 of Boolean; / Holds keystrokesFPSCount : Integer = 0; / Counter for FPSElapsedTime : cardinal; / Elapsed time between frames/ TexturesSunTex : glUInt;EarthTex: glUInt;PlutoTex: glUInt;/ User variablespObj: GLUQuadricObj;SunRotation : single = 90;/ Thi
4、s holds our suns current rotationEarthRotation : single = 90;/ This holds our earths current rotationPlutoRotation : single = 90;/ This holds plutos current rotationg_Fullscreen: boolean;$R *.RESprocedure glBindTexture(target: GLenum; texture: GLuint); stdcall; external opengl32;- Function to conver
5、t int to string. (No sysutils = smaller EXE) -function IntToStr(Num : Integer) : String; / using SysUtils increase file size by 100KbeginStr(Num, result);end;- Function to draw the actual scene -procedure glDraw();beginglClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);/ Clear The Screen And The De
6、pth BufferglLoadIdentity();/ Reset The matrix/ We make our position a bit high and back to view the whole scene/ Position View Up VectorgluLookAt(0, 3, 6, 0, 0, 0, 0, 1, 0);/ This determines where the cameras position and view isglInitNames();/ This clears the name stack so we always start with 0 na
7、mes./ This next line is important. If you dont push on at least ONE name,/ The selection wont work. Instead of glLoadName()/glEnd() you can use/ glPushName(TheID) and glPopName(); Then you dont need to glPushName(0);glPushNameopengl+delphi 鼠标拾取(0);/ This starts off the first object in the stackpObj
8、:= gluNewQuadric();/ Get a new Quadric off the stackgluQuadricTexture(pObj, true);/ This turns on texture coordinates for our Quadrics/ Bind the sun texture to the sun quadraticglBindTexture(GL_TEXTURE_2D, SunTex);/ Bind the Sun texture to the sun/ Below we call glLoadName(). We need to pass in an I
9、D that we can check later that will/ be associated with the polygons drawn next. Here is how it works. We call glLoadName()/ and pass an ID. Then we draw any primitives or shapes, then we call glEnd() which/ stops assigning polys to that object name. We now have a group of polygons that are/ given a
10、n ID. Our ID SUN now refers to the sun Quadric we draw below.glLoadName(SUN);/ Push on our SUN label (IMPORTANT)/ If we use glLoadName(), then we need to end it with glEnd(). There is a problem/ though with some video cards that MUST have a glBegin()/glEnd() between/ the calls to glLoadName()/glEnd(
11、). This is strange but some cards grind/ to a 2 FPS speed (VoodooCards). Since we are using Quadrics there is/ no glBegin()/glEnd() being called explicitly, so we need to fake it./ *Remember, you only have to do this if you are using Quadrics.* So, to fix/ this we just put a empty glBegin()/glEnd()
12、statement between each object ID passed in.glBegin(GL_LINES);glEnd(); / Here we push on a new matrix so we dont affect any other quadrics./ We first translate the quadric to the origin (0, 0, 0), Then we rotate it/ about the Y axis. This gives it the spinning effect. Then we draw the/ largest of the
13、 spheres. This represents the sun with its texture map.glPushMatrix();/ Push on a new matrix scopeglTranslatef(0, 0, 0);/ Translate this sphere to the leftglRotatef(SunRotation, 0, 1.0, 0);/ Rotate the sphere around the Y axis to make it spingluSphere(pObj, 0.5, 20, 20);/ Draw the sunwith a radius o
14、f 0.5glPopMatrix();/ End the current scope of this matrix/ Now that we drew the sun, we want to end our Object name. We call glPopName()/ to do that. Now nothing else will be associated with the SUN ID.glEnd();/ Stop assigning polygons to the SUN label (IMPORTANT)/ Next, we want to bind the Earth texture to our Earth sphereglBi