PickDemo (OpenGL演示)

2008-02-23 07:21:55来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

分  类:绘图
语  种:简体中文
编辑器:Delphi7
平  台:Win9x,Win2k/NT,WinXP
作品源代码: 本地下载
软件或演示: -
代码大小: 128.3K
软件大小: -

试着用鼠标拖动三维空间里的一条直线(或其端点)...
本演示程序希望能为OpenGL爱好者带来些什么,也希望OpenGL高手们给予评价。

glProject和glUnProject是一对被众多OpenGL书籍所"遗忘"的函数,本例程专为其而作。

program PickDemo;

uses
Windows, Messages, GL,GLu;

const
WND_TITLE = 'OpenGL Pick Demo by inRm';
BUFSIZE = 32; // Size of selection buffer.
type
gl1f = glfloat;
gl3f = array [0..2] of glfloat;
gl4f = array [0..3] of glfloat;
var
h_Wnd : HWND; // Global window handle
h_DC : HDC; // Global device context
h_RC : HGLRC; // OpenGL rendering context
winW : integer=800; // window size
winH : integer=600;
keys : Array[0..255] of Boolean; // Holds keystrokes
viewP :TviewPortArray;
modeM,projM :T16DArray;
// User vaiables
Ldown,Rdown :boolean;
hit,eye : Integer;
zPos :Tpoint;
x0, y0 :Integer; // mouse movement
rx, ry :single;
// Object position
obj :array[1..2] of gl3f; //对象的空间坐标
Pos :array[1..2] of gl3f; //对象的屏幕坐标
{------------------------------------------------------------------}
{ Function to convert int to string. }
{------------------------------------------------------------------}
function IntToStr(Num : Integer) : String;
begin
Str(Num, result);
end;
function Vert( x,y,z :gl1f) :gl3f;
begin
result[0]:=x; result[1]:=y; result[2]:=z;
end;
function Point(X, Y: Integer): TPoint;
begin
Result.X:= X; Result.Y:= Y;
end;
procedure setAxisList;
begin
glNewList(1,GL_COMPILE); //坐标线
glColor3f(1,0.5,0.5); glLineWidth (1);//绘图直线时笔的宽度
glBegin(GL_LINE_STRIP);
glColor3f(1,0,1); glVertex3f( 0,0,0); glVertex3f( 4,0,0);
glColor3f(0,1,0); glVertex3f( 0,0,0); glVertex3f( 0,3,0);
glColor3f(0,0,1); glVertex3f( 0,0,0); glVertex3f( 0,0,2);
glEnd;
glEndList;
end;
{------------------------------------------------------------------}
{ Function to draw the actual scene }
{------------------------------------------------------------------}
procedure glDraw( Mode :integer);
var h :integer;
begin
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The View
glTranslatef(0,0,-eye);

glRotatef( ry, 1,0,0);
glRotatef( rx, 0,0,1);
glColor3f(0, 0.1, 0.6);
glCallList(1);
glPointSize( 6); glLineWidth(2);
if mode = GL_SELECT then glLoadName(1);
glBegin(GL_POINTS); glColor3f(1, 1, 0);
if hit=1 then glColor3f(1, 0, 0) else glColor3f(1, 1, 0);
glVertex3fv( @obj[1]); //画第一点
glEnd;
if mode = GL_SELECT then glLoadName(2);
glBegin(GL_POINTS); glColor3f(1, 1, 0);
if hit=2 then glColor3f(1, 0, 0) else glColor3f(1, 1, 0);
glVertex3fv( @obj[2]); //画第二点
glEnd;
if mode = GL_SELECT then glLoadName(3);
glBegin( GL_LINES);
if hit=3 then glColor3f(1, 0, 0) else glColor3f(1, 0.5, 0.5);
glVertex3fv( @obj[1]); glVertex3fv( @obj[2]); //连接线
glEnd;
if mode = GL_SELECT then glLoadName(0);
glCallList(1); //画坐标轴线
if Ldown and(hit>0) then begin //画坐标指示线
glColor3f( 0.5,0.5,0.5);
glEnable( GL_LINE_STIPPLE); glLineStipple(1,$4444);
if hit<3 then h:=hit else h:=1;
glBegin( GL_LINE_STRIP);
glVertex3fv( @obj[h]); glVertex3f( obj[h,0],obj[h,1],0);
glVertex3f( obj[h,0],0,0); glVertex3f( 0,0,0);
glVertex3f( 0,obj[h,1],0); glVertex3f( obj[h,0],obj[h,1],0);
glEnd;
glDisable( GL_LINE_STIPPLE);
end;
end;
{------------------------------------------------------------------}
{ Initialise OpenGL }
{------------------------------------------------------------------}
procedure glInit();
begin
glClearColor(0.0, 0.0, 0.0, 0.0); // Black Background
// glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading
glClearDepth(1.0); // Depth Buffer Setup
glDepthFunc(GL_LESS); // The Type Of Depth Test To Do
glEnable(GL_DEPTH_TEST); // Enable Depth Buffer

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); //Realy Nice perspective calculations
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //控制混合方式
glEnable(GL_BLEND); //混合(透明)使能
glEnable(GL_line_smooth);
glEnable(GL_POINT_SMOOTH);
setAxisList;
rx:= -125; ry:= -65; //视角
eye:= 10; //景深
obj[1]:=Vert(-1, 2, 0.8);
obj[2]:=Vert( 2,-1,-0.5);
end;
{------------------------------------------------------------------}
{ Handle window resize }
{------------------------------------------------------------------}
procedure glResizeWnd(Width, Height : Integer);

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:OpenGL 屏幕保护 (演示)

下一篇:嵌入汇编得到CPU的ID号码的演示