Delphi基於OpenGL實現燃燒的火焰動畫,在OpenGL.pas 文件中,定義了所有的OpenGL 函數。本例動畫的實現主要是Delphi調用相關圖像文件來實現的。編譯本程序後,將看到一團跳動的火焰。
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
FormCreate(Sender: TObject);
010
FormPaint(Sender: TObject);
011
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
SetDCPixelFormat;
021
Drawscene (mode : GLENUM);
022
ApplyPerspective(VIEwport: TRectangle; Width, Height:
Integer
023
DPI:
);
024
protected
025
WMPaint(
var
Msg: TWMPaint); message WM_PAINT;
026
public
027
end
028
029
MainForm: TMainForm;
030
leftI, RightI,
031
TopI, BottomI,
032
zFarI, MaxDimI,
033
RatioI,Frame: GLFloat;
034
Tex:
array
[
1..8
]
of
cardinal
035
implementation
036
mmsystem;
037
{$R *.DFM}
038
TMainForm
.
Drawscene(mode : GLENUM);
//繪制圖像動畫場景
039
040
ps : TPaintStruct;
041
begin
042
BeginPaint(Handle, ps);
043
glClearcolor(
0
,
044
glClear (GL_DEPTH_BUFFER_BIT
or
GL_COLOR_BUFFER_BIT);
045
glPushMatrix;
046
gluLookAt(
2
1
047
GlBindTexture(GL_TEXTURE_2D,Tex[round(Frame)]);
048
049
glrotatef(
90
050
glscalef(
0.6
051
glBegin (GL_QUADS);
052
glTexCoord2f(
0.0
); glVertex3f(
053
1.0
); glVertex3f(-
054
,-
055
056
glEnd;
057
glPopMatrix;
058
glpopmatrix;
059
SwapBuffers(DC);
060
EndPaint(Handle, ps);
061
062
Msg: TWMPaint);
//在自定義的消息函數中用渲染模式繪制圖像動畫
063
064
drawscene (GL_RENDER);
065
066
FNTimeCallBack(uTimerID,uMessage:UINT;dwUser, dw1, dw2: DWord)
067
stdcall;
//時間回調函數,實現計時
068
069
With
MainForm
do
070
071
Frame:=Frame+
0.08
072
If
Frame>
8
then
Frame:=
073
InvalidateRect(Handle,
nil
False
074
075
076
//初始化設置OpenGL 場景
077
078
nPixelFormat:
079
pfd: TPixelFormatDescriptor;
080
081
FillChar(pfd, SizeOf(pfd),
082
with
pfd
083
nSize:=Sizeof(pfd);
084
nVersion:=
085
dwFlags:= PFD_DRAW_TO_WINDOW
086
PFD_SUPPORT_OPENGL
087
PFD_DOUBLEBUFFER;
088
iPixelType:= PFD_TYPE_RGBA;
089
cColorBits:=
24
090
cDepthBits:=
32
091
iLayerType:= PFD_MAIN_PLANE;
092
093
nPixelFormat:= ChoosePixelFormat(DC, @pfd);
094
SetPixelFormat(DC, nPixelFormat, @pfd);
095
096
ApplyPerspective(VIEwport:TRectangle;Width, Height:
097
//設置窗口隨窗口尺寸變化的位置
098
099
MaxDimI:= Width;
100
if
Height > MaxDimI
MaxDimI:= Height;
101
RatioI:= (
* VIEwport
Width +
Left - Width) / Width;
102
RightI:= RatioI * Width / (
* MaxDimI);
103
RatioI:= (Width -
Left) / Width;
104
LeftI:= -RatioI * Width / (
105
Height +
Top - Height) / Height;
106
TopI:= RatioI * Height / (
107
RatioI:= (Height -
Top) / Height;
108
BottomI:= -RatioI * Height / (
109
FNearPlane:=
72
*
* DPI / (
25.4
110
zFarI:= FNearPlane +
111
glFrustum(LeftI, RightI, BottomI, TopI, FNearPlane, zFarI);
112
glMatrixMode(GL_PROJECTION);
113
glLoadIdentity;
114
gluPerspective(
45
,Width/Height,
0.1
20000
115
glMatrixMode(GL_MODELVIEW);
116
117
118
119
120
glVIEwport(
, ClientWidth, ClIEntHeight);
121
FVIEwport
122
123
Left:=
124
Top :=
125
Width:= ClIEntWidth;
126
Height:= ClIEntHeight;
127
128
129
130
ApplyPerspective(FViewport, ClientWidth, ClIEntHeight, FDPI);
131
132
133
134
I:
integer
135
136
initopengl;
137
DC:= GetDC(Handle);
138
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
146
QO:= gluNewQuadric;
147
gluQuadricTexture(QO, GL_TRUE);
148
uTimerID:= timeSetEvent (
10
, @FNTimeCallBack,
, TIME_PERIODIC);
149
FDPI:= GetDeviceCaps(Canvas
Handle, LOGPIXELSX);
150
for
i:=
to
Tex[i]:=CreateTexture(inttostr(i)+’.jpg’);
151
152
//獲取窗口的分辨率以及應用投影方式
153
154
FDPI := GetDeviceCaps(Canvas
155
156
157
158
159
//終止計時、釋放設置的
160
OpenGL 場景和圖像占用的內存
161
162
timeKillEvent(uTimerID);
163
wglMakeCurrent(
164
wglDeleteConText(hrc);
165
ReleaseDC(Handle, DC);
166
gluDeleteQuadric (qO);
167
168
前言: 博友 "鵬" 來信
本例效果圖: 代碼文件:unit Unit1;inter
最近開始幫hr部門處理一個access數據庫,好久
環境選項最近的許多更新與通常使用的環境選項對話框(Envi
設為首頁加入收藏站內搜索網站地圖首頁編程語言 web開發