程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> Delphi基於OpenGL實現燃燒的火焰動畫特效

Delphi基於OpenGL實現燃燒的火焰動畫特效

編輯:Delphi

Delphi基於OpenGL實現燃燒的火焰動畫,在OpenGL.pas 文件中,定義了所有的OpenGL 函數。本例動畫的實現主要是Delphi調用相關圖像文件來實現的。編譯本程序後,將看到一團跳動的火焰。

vIEw source print? 001 unit Unit1; 002 interface 003 uses 004 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, 005 OpenGL,StdCtrls,Utils; 006 type 007 TMainForm = class(TForm) 008 procedure FormReSize(Sender: TObject); 009 procedure FormCreate(Sender: TObject); 010 procedure FormPaint(Sender: TObject); 011 procedure FormDestroy(Sender: TObject); 012 private 013 DC: HDC; 014 hrc: HGLRC; 015 FDPI: Cardinal; 016 FNearPlane: Single; 017 FVIEwport : TRectangle; 018 uTimerId : uint; 019 QO: gluQuadricObj; 020 procedure SetDCPixelFormat; 021 procedure Drawscene (mode : GLENUM); 022 procedure ApplyPerspective(VIEwport: TRectangle; Width, Height: Integer; 023 DPI: Cardinal); 024 protected 025 procedure WMPaint(var Msg: TWMPaint); message WM_PAINT; 026 public 027 end; 028 var 029 MainForm: TMainForm; 030 leftI, RightI, 031 TopI, BottomI, 032 zFarI, MaxDimI, 033 RatioI,Frame: GLFloat; 034 Tex: array[1..8of cardinal; 035 implementation 036 uses mmsystem; 037 {$R *.DFM} 038 procedure TMainForm.Drawscene(mode : GLENUM); //繪制圖像動畫場景 039 var 040 ps : TPaintStruct; 041 begin 042 BeginPaint(Handle, ps); 043 glClearcolor(0,0,0,0); 044 glClear (GL_DEPTH_BUFFER_BIT or GL_COLOR_BUFFER_BIT); 045 glPushMatrix; 046 gluLookAt(2,0,0,0,0,00,0,1); 047 GlBindTexture(GL_TEXTURE_2D,Tex[round(Frame)]); 048 glPushMatrix; 049 glrotatef(90,0,1,0); 050 glscalef(1,0.6,1); 051 glBegin (GL_QUADS); 052 glTexCoord2f(0.0,0.0); glVertex3f(1,1,0); 053 glTexCoord2f(0.0,1.0); glVertex3f(-1,1,0); 054 glTexCoord2f(1.0,1.0); glVertex3f(-1,-1,0); 055 glTexCoord2f(1.0,0.0); glVertex3f(1,-1,0); 056 glEnd; 057 glPopMatrix; 058 glpopmatrix; 059 SwapBuffers(DC); 060 EndPaint(Handle, ps); 061 end; 062 procedure TMainForm.WMPaint(var Msg: TWMPaint);//在自定義的消息函數中用渲染模式繪制圖像動畫 063 begin 064 drawscene (GL_RENDER); 065 end; 066 procedure FNTimeCallBack(uTimerID,uMessage:UINT;dwUser, dw1, dw2: DWord) 067 stdcall;//時間回調函數,實現計時 068 begin 069 With MainForm do 070 begin 071 Frame:=Frame+0.08; 072 If Frame>8 then Frame:=1; 073 InvalidateRect(Handle, nilFalse); 074 end; 075 end; 076 procedure TMainForm.SetDCPixelFormat; //初始化設置OpenGL 場景 077 var 078 nPixelFormat:Integer; 079 pfd: TPixelFormatDescriptor; 080 begin 081 FillChar(pfd, SizeOf(pfd), 0); 082 with pfd do begin 083 nSize:=Sizeof(pfd); 084 nVersion:=1; 085 dwFlags:= PFD_DRAW_TO_WINDOW or 086 PFD_SUPPORT_OPENGL or 087 PFD_DOUBLEBUFFER; 088 iPixelType:= PFD_TYPE_RGBA; 089 cColorBits:= 24; 090 cDepthBits:= 32; 091 iLayerType:= PFD_MAIN_PLANE; 092 end; 093 nPixelFormat:= ChoosePixelFormat(DC, @pfd); 094 SetPixelFormat(DC, nPixelFormat, @pfd); 095 end; 096 procedure TMainForm.ApplyPerspective(VIEwport:TRectangle;Width, Height: Integer; 097 DPI: Cardinal);//設置窗口隨窗口尺寸變化的位置 098 begin 099 MaxDimI:= Width; 100 if Height > MaxDimI then MaxDimI:= Height; 101 RatioI:= (2 * VIEwport.Width + 2 * VIEwport.Left - Width) / Width; 102 RightI:= RatioI * Width / (2 * MaxDimI); 103 RatioI:= (Width - 2 * VIEwport.Left) / Width; 104 LeftI:= -RatioI * Width / (2 * MaxDimI); 105 RatioI:= (2 * VIEwport.Height + 2 * VIEwport.Top - Height) / Height; 106 TopI:= RatioI * Height / (2 * MaxDimI); 107 RatioI:= (Height - 2 * VIEwport.Top) / Height; 108 BottomI:= -RatioI * Height / (2 * MaxDimI); 109 FNearPlane:= 72 2 * DPI / (25.4 * MaxDimI); 110 zFarI:= FNearPlane + 72; 111 glFrustum(LeftI, RightI, BottomI, TopI, FNearPlane, zFarI); 112 glMatrixMode(GL_PROJECTION); 113 glLoadIdentity; 114 gluPerspective(45,Width/Height, 0.120000); 115 glMatrixMode(GL_MODELVIEW); 116 InvalidateRect(Handle, nilFalse); 117 end; 118 procedure TMainForm.FormReSize(Sender: TObject);//設置窗口隨窗口尺寸變化的位置 119 begin 120 glVIEwport(00, ClientWidth, ClIEntHeight); 121 with FVIEwport do 122 begin 123 Left:= 0; 124 Top := 0; 125 Width:= ClIEntWidth; 126 Height:= ClIEntHeight; 127 end; 128 glMatrixMode(GL_PROJECTION); 129 glLoadIdentity; 130 ApplyPerspective(FViewport, ClientWidth, ClIEntHeight, FDPI); 131 end; 132 procedure TMainForm.FormCreate(Sender: TObject);//初始化設置OpenGL 場景 133 var 134 I:integer; 135 begin 136 initopengl; 137 DC:= GetDC(Handle); 138 SetDCPixelFormat; 139 hrc:= wglCreateConText(DC); 140 wglMakeCurrent(DC, hrc); 141 glEnable(GL_DEPTH_TEST); 142 glenable(GL_Texture_2d); 143 glenable(gl_blend); 144 glblendfunc(GL_ONE,GL_ONE); 145 glMatrixMode(GL_PROJECTION); 146 QO:= gluNewQuadric; 147 gluQuadricTexture(QO, GL_TRUE); 148 uTimerID:= timeSetEvent (100, @FNTimeCallBack, 0, TIME_PERIODIC); 149 FDPI:= GetDeviceCaps(Canvas.Handle, LOGPIXELSX); 150 for i:=1 to 8 do Tex[i]:=CreateTexture(inttostr(i)+’.jpg’); 151 end; 152 procedure TMainForm.FormPaint(Sender: TObject); //獲取窗口的分辨率以及應用投影方式 153 begin 154 FDPI := GetDeviceCaps(Canvas.Handle, LOGPIXELSX); 155 glMatrixMode(GL_PROJECTION); 156 glLoadIdentity; 157 ApplyPerspective(FViewport, ClientWidth, ClIEntHeight, FDPI); 158 end; 159 procedure TMainForm.FormDestroy(Sender: TObject); //終止計時、釋放設置的 160 OpenGL 場景和圖像占用的內存 161 begin 162 timeKillEvent(uTimerID); 163 wglMakeCurrent(00); 164 wglDeleteConText(hrc); 165 ReleaseDC(Handle, DC); 166 gluDeleteQuadric (qO); 167 end; 168 end.
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved