Delphi 自制的游戲素材:OpenGL三維旋轉星體代碼。在Delphi中,OpenGL可以作為自身動態運行時間庫(RTL)的一部分,以pas單元的形式放在“……\Borland\Delphi5\Source\Rtl\Win”目錄下,可以象使用其他單元文件一樣方便地使用。OpenGL的繪圖環境與GDI(Graphic Devic Interface,圖形設備接口)的繪圖環境不同。因此各自的使用方法也不一樣。本實例的Opengl繪圖步驟:
1.獲取顯示設備的句柄,這個句柄可以用GetDC開獲取。
2.設置象素格式(pixelFormat)。該步驟先用ChoosePixelFormat選取預先根據顯示設備設置好的象素格式,然後用SetPixelFormat來設置象素格式 。
3.建立新的OpenGL Rendering Context(HGLRC),這個參數相當於GDI中繪圖設備的HDC,兩者的調用方法也很相似。如本例中的hrc:=wglCreateContext(Canvas.Handle),在創建了這樣一個HRC之後,在設備上繪圖時只要將這個HRC通過wglMakeCurrent(Canvas.Handle, hrc)設置為當前的繪圖環境即可。
4.設置視口和透視投影坐標。Opengl是一個三維繪圖系統,所有繪制的圖形都是建立在三維空間內的,因此要設置透視投影坐標,以便能觀察繪制的圖形效果。
經過了上述四個步驟之後,就可以在指定的設備上畫圖了。本例中的畫圖操作在是由夾在glbegin()…glEnd中的函數體中的操作完成的。
本示例的代碼運行效果如下圖:
在本例中,並沒有使用Delphi自帶的Opengl.pas文件,而是引入了Mike Lischke針對具體應用情況重新編寫的OpenGL.pas單元。主要原因是該單元已經將初始化設置和渲染設置已經很好地封閉了,可以非常方便而簡潔地調用。還將常用的幾何實體變換封裝成Geometry.pas單元。有了這兩個單元文件,在設計OpenGL游戲時,可以將主要的精力放在游戲的思路和總體規劃中,而不是花在各種環境的設置和實體模型的創建和變換中。本例也不打算對這兩個單元進行說明了,只介紹如何調用它們實現本例的形體旋轉效果。特別要說明的是必須將新的OpenGL.pas和Geometry.pas和工程文件放在同一目錄中。
另外,本例創建的是控制台程序,而不是標准的win32程序。因此,一個重要的技術就是如何在控制台環境下實現窗口的創建和銷毀等效果。
001
program
Project2;
002
{$APPTYPE CONSOLE}
003
uses
Windows, Messages,OpenGL;
004
var
005
hParentWnd: hWnd;
// 窗口句柄
006
h_DC: hDC;
// 設備環境的句柄
007
h_RC: hGLRC;
// 渲染環境的句柄
008
WndClass: TWndClass;
// 窗口類型
009
iMessage: TMsg;
// 屏幕的長度和寬度
010
PWndWidth, PWndHeight:
Integer
;
// 旋轉的星體
011
Angle:
Integer
;
012
// 處理父窗口消息的方法
013
function
MainWndProc(hWnd:hWnd; iMessage: uInt; wParam: wParam; lParam: lParam): lResult; export; stdcall;
014
begin
015
Result :=
0
;
016
case
iMessage
of
017
WM_DESTROY: PostQuitMessage(
0
);
// 通知窗口程序即將退出
018
else
019
Result := DefWindowProc(hWnd, iMessage, wParam, lParam);
020
end
;
021
end
;
022
// 創建自定義的窗口
023
procedure
PrepareWindow;
024
begin
025
if
(hPrevInst =
0
)
then
026
begin
027
WndClass
.
style := CS_HREDRAW
or
CS_VREDRAW;
028
WndClass
.
lpfnWndProc := @MainWndProc;
// 指向處理窗口消息的過程
029
WndClass
.
cbClsExtra :=
0
;
030
WndClass
.
cbWndExtra :=
0
;
031
WndClass
.
hInstance := GetModuleHandle(
nil
);
032
WndClass
.
hIcon :=
0
;
033
WndClass
.
hCursor := LOADCURSOR(
0
, IDC_ARROW);
034
WndClass
.
hBrBackground :=
0
;
//opengl場景不需要背景
035
WndClass
.
lpszMenuName :=
nil
;
036
WndClass
.
lpszClassName :=
'glClass'
;
037
if
RegisterClass(WndClass) =
0
then
038
begin
039
MessageBox(hParentWnd,
'窗口類型不能被注冊!'
,
nil
, MB_OK
or
MB_ICONSTOP
or
MB_TASKMODAL);
040
Halt;
041
end
;
042
end
;
043
PWndWidth := Round(GetSystemMetrics(SM_CXSCREEN)/
2
);
//設置演示窗口的寬度
044
PWndHeight := Round(GetSystemMetrics(SM_CYSCREEN)/
2
);
// 設置嚴實窗口的長度
045
hParentWnd := CreateWindow(
'glClass'
,
'Delphi中演示OPENGL動畫'
, WS_SYSMENU,
0
,
0
, PWndWidth, PWndHeight,
0
,
0
, hInstance,
nil
);
046
end
;
047
// 繪制場景
048
procedure
DrawScene;
049
begin
050
Angle := Angle+Random(
20
);
051
glClear(GL_DEPTH_BUFFER_BIT
or
GL_COLOR_BUFFER_BIT);
// 清除幀緩沖區,為繪制新的幀做准備
052
glPushMatrix;
// 保存當前矩陣
053
glTranslatef(Random(
2
)/
10
, Random(
2
)/
10
,Random(
2
)-
6
);
054
{將視點後移使全部的場景可見,前面兩個數值分別是X,Y的坐標值,最後的數值決定對象大小}
055
// 轉動對象的視點,後面三個數字表示旋轉幅度,值越大幅度越大
056
glRotatef(Angle,
1
,
1
,
1
);
057
// glRotatef(Angle,0,0,0); //很強的視覺沖擊效果
058
//以下是定義組成星體的點元素
059
glBegin(GL_TRIANGLES);
060
glNormal3f(-
0.74116
, -
0.579615
,
0.338716
);
061
glVertex3f(-
0.475525
, -
0.142578
,
0.154755
);
062
glVertex3f(-
0.293854
, -
0.451569
,
0.0235291
);
063
glVertex3f(-
0.344055
, -
0.147278
,
0.434387
);
064
glEnd;
065
glBegin(GL_TRIANGLES);
066
glNormal3f(-
0.410835
, -
0.910633
, -
0.0442978
);
067
glVertex3f(-
0.293854
, -
0.451569
,
0.0235291
);
068
glVertex3f(-
0.0111389
, -
0.565186
, -
0.262848
);
069
glVertex3f(
0.168762
, -
0.656647
, -
0.0511475
);
070
glEnd;
071
glBegin(GL_TRIANGLES);
072
glNormal3f(
0.569974
, -
0.805453
,
0.162401
);
073
glVertex3f(
0.168762
, -
0.656647
, -
0.0511475
);
074
glVertex3f(
0.417633
, -
0.475372
, -
0.0255432
);
075
glVertex3f(
0.253418
, -
0.528687
,
0.286377
);
076
glEnd;
077
glBegin(GL_TRIANGLES);
078
glNormal3f(
0.949311
, -
0.3094
, -
0.0554997
);
079
glVertex3f(
0.417633
, -
0.475372
, -
0.0255432
);
080
glVertex3f(
0.471161
, -
0.290833
, -
0.138733
);
081
glVertex3f(
0.531952
, -
0.152008
,
0.127167
);
082
glEnd;
083
glBegin(GL_TRIANGLES);
084
glNormal3f(
0.9759
,
0.0404478
,
0.214439
);
085
glVertex3f(
0.531952
, -
0.152008
,
0.127167
);
086
glVertex3f(
0.533997
,
0.20929
,
0.0497131
);
087
glVertex3f(
0.472351
,
0.0888977
,
0.352966
);
088
glEnd;
089
glBegin(GL_TRIANGLES);
090
glNormal3f(
0.672389
,
0.730092
, -
0.121896
);
091
glVertex3f(
0.533997
,
0.20929
,
0.0497131
);
092
glVertex3f(
0.425964
,
0.25
, -
0.302368
);
093
glVertex3f(
0.306183
,
0.404633
, -
0.0369263
);
094
glEnd;
095
glBegin(GL_TRIANGLES);
096
glNormal3f(
0.292631
,
0.954815
, -
0.051914
);
097
glVertex3f(
0.306183
,
0.404633
, -
0.0369263
);
098
glVertex3f(
0.0134583
,
0.491821
, -
0.083374
);
099
glVertex3f(
0.131439
,
0.474701
,
0.266785
);
100
glEnd;
101
glBegin(GL_TRIANGLES);
102
glNormal3f(-
0.29624
,
0.930867
, -
0.21384
);
103
glVertex3f(
0.0134583
,
0.491821
, -
0.083374
);
104
glVertex3f(-
0.131439
,
0.40448
, -
0.262848
);
105
glVertex3f(-
0.293854
,
0.415649
,
0.0107727
);
106
glEnd;
107
glBegin(GL_TRIANGLES);
108
glNormal3f(-
0.704095
,
0.703107
,
0.0994509
);
109
glVertex3f(-
0.475525
,
0.224731
,
0.0743408
);
110
glVertex3f(-
0.344055
,
0.324249
,
0.301544
);
111
glVertex3f(-
0.293854
,
0.415649
,
0.0107727
);
112
glEnd;
113
glBegin(GL_TRIANGLES);
114
glNormal3f(-
0.986932
, -
0.0344614
, -
0.157411
);
115
glVertex3f(-
0.425293
, -
0.0452881
, -
0.181488
);
116
glVertex3f(-
0.475525
, -
0.142578
,
0.154755
);
117
glVertex3f(-
0.475525
,
0.224731
,
0.0743408
);
118
glEnd;
119
glBegin(GL_TRIANGLES);
120
glNormal3f(-
0.319147
, -
0.514182
, -
0.796091
);
121
glVertex3f(-
0.267548
, -
0.15448
, -
0.425323
);
122
glVertex3f(
0.191071
, -
0.337128
, -
0.491211
);
123
glVertex3f(-
0.0111389
, -
0.565186
, -
0.262848
);
124
glEnd;
125
glBegin(GL_TRIANGLES);
126
glNormal3f(-
0.156126
, -
0.100546
,
0.982606
);
127
glVertex3f(-
0.317749
, -
0.0470581
,
0.448822
);
128
glVertex3f(-
0.344055
, -
0.147278
,
0.434387
);
129
glVertex3f(-
0.0487366
, -
0.464478
,
0.448853
);
130
glEnd;
131
glBegin(GL_TRIANGLES);
132
glNormal3f(
0.77365
, -
0.250923
, -
0.58181
);
133
glVertex3f(
0.191071
, -
0.337128
, -
0.491211
);
134
glVertex3f(
0.312347
,
0
, -
0.475342
);
135
glVertex3f(
0.471161
, -
0.290833
, -
0.138733
);
136
glEnd;
137
glBegin(GL_TRIANGLES);
138
glNormal3f(
0.376298
, -
0.37407
,
0.847627
);
139
glVertex3f(-
0.0487366
, -
0.464478
,
0.448853
);
140
glVertex3f(
0.253418
, -
0.528687
,
0.286377
);
141
glVertex3f(
0.212646
, -
0.201538
,
0.448853
);
142
glEnd;
143
glBegin(GL_TRIANGLES);
144
glNormal3f(
0.511356
,
0.31956
, -
0.797744
);
145
glVertex3f(
0.312347
,
0
, -
0.475342
);
146
glVertex3f(
0.131378
,
0.249969
, -
0.491211
);
147
glVertex3f(
0.425964
,
0.25
, -
0.302368
);
148
glEnd;
149
glBegin(GL_TRIANGLES);
150
glNormal3f(
0.407016
, -
0.0631076
,
0.911238
);
151
glVertex3f(
0.212646
, -
0.201538
,
0.448853
);
152
glVertex3f(
0.472351
,
0.0888977
,
0.352966
);
153
glVertex3f(
0.212646
,
0.267761
,
0.481354
);
154
glEnd;
155
glBegin(GL_TRIANGLES);
156
glNormal3f(-
0.270432
,
0.623807
, -
0.733302
);
157
glVertex3f(
0.131378
,
0.249969
, -
0.491211
);
158
glVertex3f(-
0.267548
,
0.15448
, -
0.425323
);
159
glVertex3f(-
0.131439
,
0.40448
, -
0.262848
);
160
glEnd;
161
glBegin(GL_TRIANGLES);
162
glNormal3f(
0.187651
,
0.74153
,
0.644143
);
163
glVertex3f(
0.212646
,
0.267761
,
0.481354
);
164
glVertex3f(
0.131439
,
0.474701
,
0.266785
);
165
glVertex3f(-
0.0811768
,
0.360779
,
0.459869
);
166
glEnd;
167
glBegin(GL_TRIANGLES);
168
glNormal3f(-
0.839618
,
0
, -
0.543177
);
169
glVertex3f(-
0.425293
, -
0.0452881
, -
0.181488
);
170
glVertex3f(-
0.267548
,
0.15448
, -
0.425323
);
171
glVertex3f(-
0.267548
, -
0.15448
, -
0.425323
);
172
glEnd;
173
glBegin(GL_TRIANGLES);
174
glNormal3f(-
0.523449
,
0.281854
,
0.804089
);
175
glVertex3f(-
0.344055
,
0.324249
,
0.301544
);
176
glVertex3f(-
0.317749
, -
0.0470581
,
0.448822
);
177
glVertex3f(-
0.0811768
,
0.360779
,
0.459869
);
178
glEnd;
179
glBegin(GL_TRIANGLES);
180
glNormal3f(-
0.889618
, -
0.190557
,
0.415053
);
181
glVertex3f(-
0.447205
,
0.0761719
,
0.315887
);
182
glVertex3f(-
0.475525
, -
0.142578
,
0.154755
);
183
glVertex3f(-
0.344055
, -
0.147278
,
0.434387
);
184
glEnd;
185
glBegin(GL_TRIANGLES);
186
glNormal3f(-
0.672801
, -
0.631467
,
0.385471
);
187
glVertex3f(
0.00344849
, -
0.631836
,
0.247131
);
188
glVertex3f(-
0.344055
, -
0.147278
,
0.434387
);
189
glVertex3f(-
0.293854
, -
0.451569
,
0.0235291
);
190
glEnd;
191
glBegin(GL_TRIANGLES);
192
glNormal3f(-
0.879473
, -
0.438944
, -
0.183999
);
193
glVertex3f(-
0.361786
, -
0.280945
, -
0.0588074
);
194
glVertex3f(-
0.293854
, -
0.451569
,
0.0235291
);
195
glVertex3f(-
0.475525
, -
0.142578
,
0.154755
);
196
glEnd;
197
glBegin(GL_TRIANGLES);
198
glNormal3f(-
0.703044
, -
0.516037
, -
0.489321
);
199
glVertex3f(-
0.361786
, -
0.280945
, -
0.0588074
);
200
glVertex3f(-
0.0111389
, -
0.565186
, -
0.262848
);
201
glVertex3f(-
0.293854
, -
0.451569
,
0.0235291
);
202
glEnd;
203
glBegin(GL_TRIANGLES);
204
glNormal3f(
0.17191
, -
0.842719
, -
0.510169
);
205
glVertex3f(
0.282532
, -
0.491699
, -
0.285278
);
206
glVertex3f(
0.168762
, -
0.656647
, -
0.0511475
);
207
glVertex3f(-
0.0111389
, -
0.565186
, -
0.262848
);
208
glEnd;
209
glBegin(GL_TRIANGLES);
210
glNormal3f(-
0.42147
, -
0.892735
, -
0.159332
);
211
glVertex3f(
0.00344849
, -
0.631836
,
0.247131
);
212
glVertex3f(-
0.293854
, -
0.451569
,
0.0235291
);
213
glVertex3f(
0.168762
, -
0.656647
, -
0.0511475
);
214
glEnd;
215
glBegin(GL_TRIANGLES);
216
glNormal3f(
0.333159
, -
0.906305
,
0.260032
);
217
glVertex3f(
0.00344849
, -
0.631836
,
0.247131
);
218
glVertex3f(
0.168762
, -
0.656647
, -
0.0511475
);
219
glVertex3f(
0.253418
, -
0.528687
,
0.286377
);
220
glEnd;
221
glBegin(GL_TRIANGLES);
222
glNormal3f(
0.764525
, -
0.567613
,
0.305478
);
223
glVertex3f(
0.473358
, -
0.170807
,
0.400909
);
224
glVertex3f(
0.253418
, -
0.528687
,
0.286377
);
225
glVertex3f(
0.417633
, -
0.475372
, -
0.0255432
);
226
glEnd;
227
glBegin(GL_TRIANGLES);
228
glNormal3f(
0.586159
, -
0.768494
, -
0.256583
);
229
glVertex3f(
0.282532
, -
0.491699
, -
0.285278
);
230
glVertex3f(
0.417633
, -
0.475372
, -
0.0255432
);
231
glVertex3f(
0.168762
, -
0.656647
, -
0.0511475
);
232
glEnd;
233
glBegin(GL_TRIANGLES);
234
glNormal3f(-
0.705977
, -
0.593585
, -
0.386334
);
235
glVertex3f(-
0.361786
, -
0.280945
, -
0.0588074
);
236
glVertex3f(-
0.267548
, -
0.15448
, -
0.425323
);
237
glVertex3f(-
0.0111389
, -
0.565186
, -
0.262848
);
238
glEnd;
239
glBegin(GL_TRIANGLES);
240
glNormal3f(
0.141872
, -
0.760405
, -
0.633764
);
241
glVertex3f(
0.282532
, -
0.491699
, -
0.285278
);
242
glVertex3f(-
0.0111389
, -
0.565186
, -
0.262848
);
243
glVertex3f(
0.191071
, -
0.337128
, -
0.491211
);
244
glEnd;
245
glBegin(GL_TRIANGLES);
246
glNormal3f(-
0.27854
, -
0.381434
, -
0.881433
);
247
glVertex3f(
0.00231934
,
0
, -
0.577454
);
248
glVertex3f(
0.191071
, -
0.337128
, -
0.491211
);
249
glVertex3f(-
0.267548
, -
0.15448
, -
0.425323
);
250
glEnd;
251
glBegin(GL_TRIANGLES);
252
glNormal3f(-
0.679685
,
0.0732876
,
0.729834
);
253
glVertex3f(-
0.447205
,
0.0761719
,
0.315887
);
254
glVertex3f(-
0.344055
, -
0.147278
,
0.434387
);
255
glVertex3f(-
0.317749
, -
0.0470581
,
0.448822
);
256
glEnd;
257
glBegin(GL_TRIANGLES);
258
glNormal3f(-
0.694999
, -
0.631368
,
0.34402
);
259
glVertex3f(
0.00344849
, -
0.631836
,
0.247131
);
260
glVertex3f(-
0.0487366
, -
0.464478
,
0.448853
);
261
glVertex3f(-
0.344055
, -
0.147278
,
0.434387
);
262
glEnd;
263
glBegin(GL_TRIANGLES);
264
glNormal3f(-
0.17093
, -
0.110087
,
0.979114
);
265
glVertex3f(-
0.0549011
,
0
,
0.5
);
266
glVertex3f(-
0.317749
, -
0.0470581
,
0.448822
);
267
glVertex3f(-
0.0487366
, -
0.464478
,
0.448853
);
268
glEnd;
269
glBegin(GL_TRIANGLES);
270
glNormal3f(
0.796179
, -
0.46695
, -
0.384781
);
271
glVertex3f(
0.282532
, -
0.491699
, -
0.285278
);
272
glVertex3f(
0.471161
, -
0.290833
, -
0.138733
);
273
glVertex3f(
0.417633
, -
0.475372
, -
0.0255432
);
274
glEnd;
275
glBegin(GL_TRIANGLES);
276
glNormal3f(
0.968681
, -
0.225635
, -
0.103661
);
277
glVertex3f(
0.525299
, -
0.042511
, -
0.17334
);
278
glVertex3f(
0.531952
, -
0.152008
,
0.127167
);
279
glVertex3f(
0.471161
, -
0.290833
, -
0.138733
);
280
glEnd;
281
glBegin(GL_TRIANGLES);
282
glNormal3f(
0.902522
, -
0.39741
,
0.16589
);
283
glVertex3f(
0.473358
, -
0.170807
,
0.400909
);
284
glVertex3f(
0.417633
, -
0.475372
, -
0.0255432
);
285
glVertex3f(
0.531952
, -
0.152008
,
0.127167
);
286
glEnd;
287
glBegin(GL_TRIANGLES);
288
glNormal3f(
0.976342
,
0.0429096
,
0.21193
);
289
glVertex3f(
0.473358
, -
0.170807
,
0.400909
);
290
glVertex3f(
0.531952
, -
0.152008
,
0.127167
);
291
glVertex3f(
0.472351
,
0.0888977
,
0.352966
);
292
glEnd;
293
glBegin(GL_TRIANGLES);
294
glNormal3f(
0.877521
,
0.357037
,
0.320127
);
295
glVertex3f(
0.408844
,
0.328003
,
0.260376
);
296
glVertex3f(
0.472351
,
0.0888977
,
0.352966
);
297
glVertex3f(
0.533997
,
0.20929
,
0.0497131
);
298
glEnd;
299
glBegin(GL_TRIANGLES);
300
glNormal3f(
0.999592
, -
0.0112823
, -
0.0262407
);
301
glVertex3f(
0.525299
, -
0.042511
, -
0.17334
);
302
glVertex3f(
0.533997
,
0.20929
,
0.0497131
);
303
glVertex3f(
0.531952
, -
0.152008
,
0.127167
);
304
glEnd;
305
glBegin(GL_TRIANGLES);
306
glNormal3f(
0.764909
, -
0.303992
, -
0.567894
);
307
glVertex3f(
0.282532
, -
0.491699
, -
0.285278
);
308
glVertex3f(
0.191071
, -
0.337128
, -
0.491211
);
309
glVertex3f(
0.471161
, -
0.290833
, -
0.138733
);
310
glEnd;
311
glBegin(GL_TRIANGLES);
312
glNormal3f(
0.774426
, -
0.249842
, -
0.581243
);
313
glVertex3f(
0.525299
, -
0.042511
, -
0.17334
);
314
glVertex3f(
0.471161
, -
0.290833
, -
0.138733
);
315
glVertex3f(
0.312347
,
0
, -
0.475342
);
316
glEnd;
317
glBegin(GL_TRIANGLES);
318
glNormal3f(
0.312115
, -
0.0676723
, -
0.947631
);
319
glVertex3f(
0.00231934
,
0
, -
0.577454
);
320
glVertex3f(
0.312347
,
0
, -
0.475342
);
321
glVertex3f(
0.191071
, -
0.337128
, -
0.491211
);
322
glEnd;
323
glBegin(GL_TRIANGLES);
324
glNormal3f(
0.197739
, -
0.72867
,
0.655698
);
325
glVertex3f(
0.00344849
, -
0.631836
,
0.247131
);
326
glVertex3f(
0.253418
, -
0.528687
,
0.286377
);
327
glVertex3f(-
0.0487366
, -
0.464478
,
0.448853
);
328
glEnd;
329
glBegin(GL_TRIANGLES);
330
glNormal3f(
0.211591
, -
0.413455
,
0.885598
);
331
glVertex3f(
0.473358
, -
0.170807
,
0.400909
);
332
glVertex3f(
0.212646
, -
0.201538
,
0.448853
);
333
glVertex3f(
0.253418
, -
0.528687
,
0.286377
);
334
glEnd;
335
glBegin(GL_TRIANGLES);
336
glNormal3f(
0.108039
, -
0.107399
,
0.988328
);
337
glVertex3f(-
0.0549011
,
0
,
0.5
);
338
glVertex3f(-
0.0487366
, -
0.464478
,
0.448853
);
339
glVertex3f(
0.212646
, -
0.201538
,
0.448853
);
340
glEnd;
341
glBegin(GL_TRIANGLES);
342
glNormal3f(
0.942466
,
0.202839
, -
0.265731
);
343
glVertex3f(
0.525299
, -
0.042511
, -
0.17334
);
344
glVertex3f(
0.425964
,
0.25
, -
0.302368
);
345
glVertex3f(
0.533997
,
0.20929
,
0.0497131
);
346
glEnd;
347
glBegin(GL_TRIANGLES);
348
glNormal3f(
0.540665
,
0.809814
, -
0.227778
);
349
glVertex3f(
0.170563
,
0.425323
, -
0.285278
);
350
glVertex3f(
0.306183
,
0.404633
, -
0.0369263
);
351
glVertex3f(
0.425964
,
0.25
, -
0.302368
);
352
glEnd;
353
glBegin(GL_TRIANGLES);
354
glNormal3f(
0.657824
,
0.75244
, -
0.0332111
);
355
glVertex3f(
0.408844
,
0.328003
,
0.260376
);
356
glVertex3f(
0.533997
,
0.20929
,
0.0497131
);
357
glVertex3f(
0.306183
,
0.404633
, -
0.0369263
);
358
glEnd;
359
glBegin(GL_TRIANGLES);
360
glNormal3f(
0.467657
,
0.881464
,
0.0657114
);
361
glVertex3f(
0.408844
,
0.328003
,
0.260376
);
362
glVertex3f(
0.306183
,
0.404633
, -
0.0369263
);
363
glVertex3f(
0.131439
,
0.474701
,
0.266785
);
364
glEnd;
365
glBegin(GL_TRIANGLES);
366
glNormal3f(
0.036579
,
0.998664
,
0.036503
);
367
glVertex3f(-
0.138153
,
0.486145
,
0.223846
);
368
glVertex3f(
0.131439
,
0.474701
,
0.266785
);
369
glVertex3f(
0.0134583
,
0.491821
, -
0.083374
);
370
glEnd;
371
glBegin(GL_TRIANGLES);
372
glNormal3f(
0.296505
,
0.951448
, -
0.0826477
);
373
glVertex3f(
0.170563
,
0.425323
, -
0.285278
);
374
glVertex3f(
0.0134583
,
0.491821
, -
0.083374
);
375
glVertex3f(
0.306183
,
0.404633
, -
0.0369263
);
376
glEnd;
377
glBegin(GL_TRIANGLES);
378
glNormal3f(
0.818651
,
0.0249238
, -
0.57375
);
379
glVertex3f(
0.525299
, -
0.042511
, -
0.17334
);
380
glVertex3f(
0.312347
,
0
, -
0.475342
);
381
glVertex3f(
0.425964
,
0.25
, -
0.302368
);
382
glEnd;
383
glBegin(GL_TRIANGLES);
384
glNormal3f(
0.407482
,
0.655568
, -
0.635759
);
385
glVertex3f(
0.170563
,
0.425323
, -
0.285278
);
386
glVertex3f(
0.425964
,
0.25
, -
0.302368
);
387
glVertex3f(
0.131378
,
0.249969
, -
0.491211
);
388
glEnd;
389
glBegin(GL_TRIANGLES);
390
glNormal3f(
0.3086
,
0.163933
, -
0.936959
);
391
glVertex3f(
0.00231934
,
0
, -
0.577454
);
392
glVertex3f(
0.131378
,
0.249969
, -
0.491211
);
393
glVertex3f(
0.312347
,
0
, -
0.475342
);
394
glEnd;
395
glBegin(GL_TRIANGLES);
396
glNormal3f(
0.157363
,
0.179867
,
0.971022
);
397
glVertex3f(
0.473358
, -
0.170807
,
0.400909
);
398
glVertex3f(
0.472351
,
0.0888977
,
0.352966
);
399
glVertex3f(
0.212646
, -
0.201538
,
0.448853
);
400
glEnd;
401
glBegin(GL_TRIANGLES);
402
glNormal3f(
0.618173
,
0.421188
,
0.663674
);
403
glVertex3f(
0.408844
,
0.328003
,
0.260376
);
404
glVertex3f(
0.212646
,
0.267761
,
0.481354
);
405
glVertex3f(
0.472351
,
0.0888977
,
0.352966
);
406
glEnd;
407
glBegin(GL_TRIANGLES);
408
glNormal3f(
0.137357
, -
0.0684344
,
0.988155
);
409
glVertex3f(-
0.0549011
,
0
,
0.5
);
410
glVertex3f(
0.212646
, -
0.201538
,
0.448853
);
411
glVertex3f(
0.212646
,
0.267761
,
0.481354
);
412
glEnd;
413
glBegin(GL_TRIANGLES);
414
glNormal3f(-
0.898457
, -
0.367331
, -
0.240507
);
415
glVertex3f(-
0.361786
, -
0.280945
, -
0.0588074
);
416
glVertex3f(-
0.475525
, -
0.142578
,
0.154755
);
417
glVertex3f(-
0.425293
, -
0.0452881
, -
0.181488
);
418
glEnd;
419
glBegin(GL_TRIANGLES);
420
glNormal3f(-
0.990518
,
0.0293809
,
0.134204
);
421
glVertex3f(-
0.447205
,
0.0761719
,
0.315887
);
422
glVertex3f(-
0.475525
,
0.224731
,
0.0743408
);
423
glVertex3f(-
0.475525
, -
0.142578
,
0.154755
);
424
glEnd;
425
glBegin(GL_TRIANGLES);
426
glNormal3f(-
0.930333
,
0.145751
, -
0.336506
);
427
glVertex3f(-
0.361786
,
0.262848
, -
0.223602
);
428
glVertex3f(-
0.425293
, -
0.0452881
, -
0.181488
);
429
glVertex3f(-
0.475525
,
0.224731
,
0.0743408
);
430
glEnd;
431
glBegin(GL_TRIANGLES);
432
glNormal3f(-
0.0915225
,
0.922481
, -
0.375037
);
433
glVertex3f(
0.170563
,
0.425323
, -
0.285278
);
434
glVertex3f(-
0.131439
,
0.40448
, -
0.262848
);
435
glVertex3f(
0.0134583
,
0.491821
, -
0.083374
);
436
glEnd;
437
glBegin(GL_TRIANGLES);
438
glNormal3f(-
0.533345
,
0.770975
, -
0.348053
);
439
glVertex3f(-
0.361786
,
0.262848
, -
0.223602
);
440
glVertex3f(-
0.293854
,
0.415649
,
0.0107727
);
441
glVertex3f(-
0.131439
,
0.40448
, -
0.262848
);
442
glEnd;
443
glBegin(GL_TRIANGLES);
444
glNormal3f(-
0.272513
,
0.955032
, -
0.116838
);
445
glVertex3f(-
0.138153
,
0.486145
,
0.223846
);
446
glVertex3f(
0.0134583
,
0.491821
, -
0.083374
);
447
glVertex3f(-
0.293854
,
0.415649
,
0.0107727
);
448
glEnd;
449
glBegin(GL_TRIANGLES);
450
glNormal3f(-
0.863373
,
0.378291
,
0.333889
);
451
glVertex3f(-
0.447205
,
0.0761719
,
0.315887
);
452
glVertex3f(-
0.344055
,
0.324249
,
0.301544
);
453
glVertex3f(-
0.475525
,
0.224731
,
0.0743408
);
454
glEnd;
455
glBegin(GL_TRIANGLES);
456
glNormal3f(-
0.574288
,
0.804112
,
0.153612
);
457
glVertex3f(-
0.138153
,
0.486145
,
0.223846
);
458
glVertex3f(-
0.293854
,
0.415649
,
0.0107727
);
459
glVertex3f(-
0.344055
,
0.324249
,
0.301544
);
460
glEnd;
461
glBegin(GL_TRIANGLES);
462
glNormal3f(-
0.742204
,
0.639143
, -
0.201568
);
463
glVertex3f(-
0.361786
,
0.262848
, -
0.223602
);
464
glVertex3f(-
0.475525
,
0.224731
,
0.0743408
);
465
glVertex3f(-
0.293854
,
0.415649
,
0.0107727
);
466
glEnd;
467
glBegin(GL_TRIANGLES);
468
glNormal3f(-
0.100014
,
0.766867
, -
0.633966
);
469
glVertex3f(
0.170563
,
0.425323
, -
0.285278
);
470
glVertex3f(
0.131378
,
0.249969
, -
0.491211
);
471
glVertex3f(-
0.131439
,
0.40448
, -
0.262848
);
472
glEnd;
473
glBegin(GL_TRIANGLES);
474
glNormal3f(-
0.496295
,
0.646655
, -
0.579248
);
475
glVertex3f(-
0.361786
,
0.262848
, -
0.223602
);
476
glVertex3f(-
0.131439
,
0.40448
, -
0.262848
);
477
glVertex3f(-
0.267548
,
0.15448
, -
0.425323
);
478
glEnd;
479
glBegin(GL_TRIANGLES);
480
glNormal3f(-
0.245954
,
0.427173
, -
0.870075
);
481
glVertex3f(
0.00231934
,
0
, -
0.577454
);
482
glVertex3f(-
0.267548
,
0.15448
, -
0.425323
);
483
glVertex3f(
0.131378
,
0.249969
, -
0.491211
);
484
glEnd;
485
glBegin(GL_TRIANGLES);
486
glNormal3f(-
0.838044
, -
0.412032
, -
0.357647
);
487
glVertex3f(-
0.361786
, -
0.280945
, -
0.0588074
);
488
glVertex3f(-
0.425293
, -
0.0452881
, -
0.181488
);
489
glVertex3f(-
0.267548
, -
0.15448
, -
0.425323
);
490
glEnd;
491
glBegin(GL_TRIANGLES);
492
glNormal3f(-
0.49107
,
0
, -
0.87112
);
493
glVertex3f(
0.00231934
,
0
, -
0.577454
);
494
glVertex3f(-
0.267548
, -
0.15448
, -
0.425323
);
495
glVertex3f(-
0.267548
,
0.15448
, -
0.425323
);
496
glEnd;
497
glBegin(GL_TRIANGLES);
498
glNormal3f(-
0.874574
,
0.115899
, -
0.470838
);
499
glVertex3f(-
0.361786
,
0.262848
, -
0.223602
);
500
glVertex3f(-
0.267548
,
0.15448
, -
0.425323
);
501
glVertex3f(-
0.425293
, -
0.0452881
, -
0.181488
);
502
glEnd;
503
glBegin(GL_TRIANGLES);
504
glNormal3f(
0.399242
,
0.730775
,
0.553691
);
505
glVertex3f(
0.408844
,
0.328003
,
0.260376
);
506
glVertex3f(
0.131439
,
0.474701
,
0.266785
);
507
glVertex3f(
0.212646
,
0.267761
,
0.481354
);
508
glEnd;
509
glBegin(GL_TRIANGLES);
510
glNormal3f(-
0.0385133
,
0.878612
,
0.475982
);
511
glVertex3f(-
0.138153
,
0.486145
,
0.223846
);
512
glVertex3f(-
0.0811768
,
0.360779
,
0.459869
);
513
glVertex3f(
0.131439
,
0.474701
,
0.266785
);
514
glEnd;
515
glBegin(GL_TRIANGLES);
516
glNormal3f(-
0.0385461
,
0.107696
,
0.993436
);
517
glVertex3f(-
0.0549011
,
0
,
0.5
);
518
glVertex3f(
0.212646
,
0.267761
,
0.481354
);
519
glVertex3f(-
0.0811768
,
0.360779
,
0.459869
);
520
glEnd;
521
glBegin(GL_TRIANGLES);
522
glNormal3f(-
0.549513
,
0.274117
,
0.789237
);
523
glVertex3f(-
0.447205
,
0.0761719
,
0.315887
);
524
glVertex3f(-
0.317749
, -
0.0470581
,
0.448822
);
525
glVertex3f(-
0.344055
,
0.324249
,
0.301544
);
526
glEnd;
527
glBegin(GL_TRIANGLES);
528
glNormal3f(-
0.206353
,
0.0933145
,
0.974018
);
529
glVertex3f(-
0.0549011
,
0
,
0.5
);
530
glVertex3f(-
0.0811768
,
0.360779
,
0.459869
);
531
glVertex3f(-
0.317749
, -
0.0470581
,
0.448822
);
532
glEnd;
533
glBegin(GL_TRIANGLES);
534
glNormal3f(-
0.408821
,
0.761405
,
0.503119
);
535
glVertex3f(-
0.138153
,
0.486145
,
0.223846
);
536
glVertex3f(-
0.344055
,
0.324249
,
0.301544
);
537
glVertex3f(-
0.0811768
,
0.360779
,
0.459869
);
538
glEnd;
539
SwapBuffers(h_DC);
// 交換幀緩沖區
540
glPopMatrix;
// 處理當前矩陣
541
end
;
// 置於先前保存的矩陣之後
542
// 正式繪制Opengl場景
543
procedure
PrepareScene;
544
begin
545
glEnable(GL_DEPTH_TEST);
546
glEnable(GL_CULL_FACE);
547
glEnable(GL_NORMALIZE);
548
glEnable(GL_LIGHTING);
549
glEnable(GL_LIGHT0);
550
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
551
glMatrixMode(GL_PROJECTION);
552
glVIEwPort(
0
,
0
, PWndWidth, PWndHeight);
// 設置渲染的環境
553
gluPerspective(
60
,
400
/
300
,
1
,
100
);
// 設置其余的場景范圍
554
glMatrixMode(GL_MODELVIEW);
555
angle :=
0
;
556
end
;
557
begin
558
PrepareWindow;
559
h_DC := GetDC(hParentWnd);
// 從創建的窗口中獲得一個句柄
560
InitOpenGL;
// 激活動態鏈接庫
561
h_RC :=CreateRenderingContext(h_DC, [opDoubleBuffered],
0
,
0
);
// 設置Opengl的渲染環境
562
ActivateRenderingContext(h_DC, h_RC);
// 激活設備句柄
563
PrepareScene;
564
ShowWindow(hParentWnd, SW_SHOW);
// 使窗口可見
565
566
// 下列消息使這個程序循環執行
567
repeat
568
PeekMessage(iMessage,
0
,
0
,
0
, PM_REMOVE);
// 解釋消息
569
TranslateMessage(iMessage);
570
DispatchMessage(iMessage);
// 給主窗口過程分派消息
571
DrawScene;
572
573
until
(iMessage
.
message = WM_QUIT);
574
// 退出演示後的相關處理
575
DeactivateRenderingContext;
576
DestroyRenderingContext(h_RC);
577
ReleaseDC(hParentWnd, h_DC);
578
UnRegisterClass(
'glClass'
, hInstance);
579
CloSEOpenGL;
580
DestroyWindow(hParentWnd);
581
end
.
本例實現的只是一個角狀星體在窗口中飄忽不定地旋轉著,沒有別的事件發生。在下面的實例中,將實現一個更加復雜的OpenGL游戲,裡面包含有背景貼圖、幾何對象加載和聲音素材的加入等功能。
目前在編制OpenGL游戲中,除使用Delphi自帶的OpenGL.pas單元文件外,很多時候都是使用一些更加方便快捷的OpenGL單元文件,比如本例就使用了Mike Lischke寫的OpenGL.pas。.這對於簡化OpenGL游戲的開發,節約時間和精力都是非常重要的。當然,如果有足夠的實力,最好是自己編寫一個適合自己的OpenGL.pas,但是一般情況下是沒有這個必要的。因為,在游戲開發過程中,進度是一個很重要的衡量指標。而且網絡是一個非常重要的資源,可能別人已經有人寫好了你想要的文件了。其實,在網絡上不斷的搜索中也可以學到不少東西。