source: bin/SubOperation/dx/dx_graphics.sbp@ 212

Last change on this file since 212 was 187, checked in by dai, 18 years ago

AB本体のバイナリを登録

File size: 23.4 KB
Line 
1'このファイルには、DirectGraphicsをCOMインターフェイスレベルで扱うための関数が定義されています。
2
3
4'----------------------------------------------------
5' DirectGraphicsに必要なヘッダファイルをインクルード
6'----------------------------------------------------
7#include <directx9\d3dx9.sbp>
8
9
10Dim dx_lpD3D As LPDIRECT3D9
11Dim dx_lpD3DDEV As LPDIRECT3DDEVICE9 'Direct3Dデバイスを扱うインターフェイス
12Dim dx_lpD3DXFont As LPD3DXFONT '文字列描画を扱うインターフェイス
13Dim dx_d3dpp As D3DPRESENT_PARAMETERS
14
15
16Function dx_InitD3D(hWnd As HWND, ScreenX As Long, ScreenY As Long, bWindowMode As Long) As Long
17 dx_lpD3D=Direct3DCreate9(D3D_SDK_VERSION)
18 If dx_lpD3D=0 Then
19 MessageBox(0, "Direct3D オブジェクトの生成に失敗しました。", "エラー", MB_OK or MB_ICONSTOP)
20 ExitProcess(0)
21 End If
22
23 Dim d3ddm As D3DDISPLAYMODE
24 dx_lpD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, VarPtr(d3ddm))
25
26 ZeroMemory(VarPtr(dx_d3dpp), SizeOf(D3DPRESENT_PARAMETERS))
27 dx_d3dpp.BackBufferWidth = ScreenX ' バックバッファの幅
28 dx_d3dpp.BackBufferHeight = ScreenY ' バックバッファの高さ
29 dx_d3dpp.BackBufferFormat = D3DFMT_R5G6B5 ' バックバッファのフォーマット
30 dx_d3dpp.BackBufferCount = 1 ' バックバッファの数
31 dx_d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE ' マルチサンプリングの種類
32 dx_d3dpp.MultiSampleQuality = 0 ' マルチサンプリングの品質
33 dx_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD ' スワップエフェクト
34 dx_d3dpp.hDeviceWindow = hWnd ' デバイスウインドウハンドル
35 dx_d3dpp.Windowed = bWindowMode ' ウインドウモードならTRUE
36 dx_d3dpp.EnableAutoDepthStencil = TRUE ' Zバッファの有無
37 dx_d3dpp.AutoDepthStencilFormat = D3DFMT_D16 ' Zバッファのフォーマット
38 dx_d3dpp.Flags = 0 ' 動作設定
39 dx_d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT ' リフレッシュレート
40 dx_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE ' 画面の更新間隔
41
42 Dim result As Long
43
44 'T&L HAL デバイスの生成
45 result=dx_lpD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,D3DCREATE_HARDWARE_VERTEXPROCESSING, VarPtr(dx_d3dpp), VarPtr(dx_lpD3DDEV))
46 If result Then
47 '失敗時 HAL デバイスの生成
48 result=dx_lpD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,VarPtr(dx_d3dpp), VarPtr(dx_lpD3DDEV))
49 If result Then
50 '失敗時 REF デバイスの生成
51 result=dx_lpD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_REF,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,VarPtr(dx_d3dpp), VarPtr(dx_lpD3DDEV))
52 If result Then
53 '全て失敗
54 dx_InitD3D=0
55 Exit Function
56 End If
57 End If
58 End If
59 dx_InitD3D=1
60End Function
61
62' Direct3Dフォントを生成する関数
63Function dx_InitFont() As Long As Long
64 Dim hr As DWord
65
66 '論理フォント構造体
67 Dim dfont As D3DXFONT_DESC
68 ZeroMemory(VarPtr(dfont), Len(dfont))
69 dfont.Height=16
70 dfont.Width=0
71 dfont.Weight=FW_NORMAL
72 dfont.MipLevels=0
73 dfont.CharSet=SHIFTJIS_CHARSET
74 dfont.OutputPrecision=OUT_DEFAULT_PRECIS
75 dfont.Quality=DEFAULT_QUALITY
76 dfont.PitchAndFamily=DEFAULT_PITCH
77
78 'Direct3D フォント生成
79 hr = D3DXCreateFontIndirect(dx_lpD3DDEV, VarPtr(dfont), VarPtr(dx_lpD3DXFont))
80 If hr Then dx_InitFont=0 Else dx_InitFont=1
81End Function
82
83' デバイスを生成する関数
84Function dx_Init(hWnd As HWND, ScreenX As Long, ScreenY As Long, bWindowMode As Long) As Long
85 'COMを初期化
86 CoInitialize(NULL)
87
88 'D3Dデバイスを生成
89 If dx_InitD3D(hWnd,ScreenX,ScreenY,bWindowMode)=0 Then
90 dx_Init=0
91 Exit Function
92 End If
93
94 'フォントを生成
95 If dx_InitFont()=0 Then
96 dx_Init=0
97 Exit Function
98 End If
99
100 'DirectInputを初期化
101 If dx_InitDInput()=0 Then
102 dx_Init=0
103 Exit Function
104 End If
105
106 'DirectMusicを初期化
107 If dx_InitDMusic()=0 Then
108 dx_Init=0
109 Exit Function
110 End If
111
112 dx_Init=1
113End Function
114
115' 各デバイスの終了処理
116Sub dx_Quit()
117 'DirectMusicの終了
118 dx_QuitDMusic()
119
120 'DirectInputの終了
121 dx_QuitDInput()
122
123 If dx_lpD3DXFont Then dx_lpD3DXFont->Release()
124 If dx_lpD3DDEV Then dx_lpD3DDEV->Release()
125 If dx_lpD3D Then dx_lpD3D->Release()
126
127 'COMの終了
128 CoUninitialize()
129End Sub
130
131' デバイスインターフェイスを取得するための関数
132Function dx_GetDevice() As LPDIRECT3DDEVICE9
133 dx_GetDevice=dx_lpD3DDEV
134End Function
135
136' 画面描画の開始
137Sub dx_BeginScene()
138 dx_lpD3DDEV->BeginScene()
139End Sub
140
141' 画面描画の終了
142Sub dx_EndScene()
143 dx_lpD3DDEV->EndScene()
144End Sub
145
146' バックバッファとZバッファの初期化
147Sub dx_Clear()
148 dx_lpD3DDEV->Clear(0,
149 NULL,
150 D3DCLEAR_TARGET or D3DCLEAR_ZBUFFER,
151 D3DCOLOR_XRGB(0,0,0),
152 1.0,
153 0)
154End Sub
155
156' 文字列を描画
157Sub dx_DrawText(x As Long, y As Long, psz As BytePtr, d3dColor As D3DCOLOR)
158 Dim rc As RECT
159 rc.left=x
160 rc.top=y
161 'DT_NOCLIPフラグを指定するため、rc.right及びrc.bottomは無視される
162
163 dx_lpD3DXFont->DrawText(0,
164 psz,
165 -1,
166 VarPtr(rc),
167 DT_LEFT or DT_NOCLIP or DT_EXPANDTABS,
168 d3dColor)
169End Sub
170
171' 画面を更新
172Function dx_Present() As DWord
173 dx_Present=dx_lpD3DDEV->Present(NULL,NULL,NULL,NULL)
174End Function
175
176'カリングモードの設定
177Sub dx_SetCullMode(CullMode As D3DCULL)
178 dx_lpD3DDEV->SetRenderState(D3DRS_CULLMODE,CullMode)
179End Sub
180
181'カメラの位置、角度の設定
182Sub dx_SetCamera(pVectorEye As *D3DVECTOR, pVectorAt As *D3DVECTOR, pVectorUp As *D3DVECTOR)
183 Dim mView As D3DXMATRIX
184 D3DXMatrixLookAtLH(VarPtr(mView),
185 pVectorEye As *D3DXVECTOR3, 'カメラ位置
186 pVectorAt As *D3DXVECTOR3, 'カメラ注視点
187 pVectorUp As *D3DXVECTOR3 'カメラ上方向
188 )
189 dx_lpD3DDEV->SetTransform(D3DTS_VIEW, VarPtr(mView))
190End Sub
191
192'射影変換行列の設定
193Sub dx_SetProjection(angle As Single, aspect As Single, nearclip As Single, backclip As Single)
194 Dim mProjection As D3DXMATRIX
195 D3DXMatrixPerspectiveFovLH(
196 VarPtr(mProjection),
197 angle, '視野角
198 aspect, 'アスペクト比
199 nearclip, '前方投影面
200 backclip) '後方投影面
201 dx_lpD3DDEV->SetTransform(D3DTS_PROJECTION, VarPtr(mProjection))
202End Sub
203
204' 光源の設定
205Sub dx_SetDefaultLight()
206 ' ポイントライトの設定
207 Dim PointLight As D3DLIGHT9
208 ZeroMemory(VarPtr(PointLight), SizeOf(D3DLIGHT9))
209 PointLight.Type_ = D3DLIGHT_POINT '光源の種類
210 PointLight.Diffuse.r = 0.0 'ディフューズ R
211 PointLight.Diffuse.g = 1.0 'ディフューズ G
212 PointLight.Diffuse.b = 0.0 'ディフューズ B
213 PointLight.Diffuse.a = 0.0 'ディフューズ A
214 PointLight.Specular.r = 0.1 'スペキュラー R
215 PointLight.Specular.g = 0.1 'スペキュラー G
216 PointLight.Specular.b = 0.1 'スペキュラー B
217 PointLight.Specular.a = 0.0 'スペキュラー A
218 PointLight.Ambient.r = 0.5 'アンビエント R
219 PointLight.Ambient.g = 0.5 'アンビエント G
220 PointLight.Ambient.b = 0.5 'アンビエント B
221 PointLight.Ambient.a = 0.0 'アンビエント A
222 PointLight.Position.x = -5 '光源の位置
223 PointLight.Position.y = 5
224 PointLight.Position.z = 0
225 PointLight.Range = 100.0 '有効範囲
226 PointLight.Attenuation0 = 0.0 '輝度の減衰値
227 PointLight.Attenuation1 = 1.0 '輝度の減衰値
228 PointLight.Attenuation2 = 0.0 '輝度の減衰値
229 dx_lpD3DDEV->SetLight(0, VarPtr(PointLight))
230 dx_lpD3DDEV->LightEnable(0, TRUE)
231
232 'スポットライトの設定
233 Dim SpotLight As D3DLIGHT9
234 ZeroMemory(VarPtr(SpotLight), SizeOf(D3DLIGHT9))
235 SpotLight.Type_ = D3DLIGHT_SPOT '光源の種類
236 SpotLight.Diffuse.r = 1.0 'ディフューズ R
237 SpotLight.Diffuse.g = 0.0 'ディフューズ G
238 SpotLight.Diffuse.b = 0.0 'ディフューズ B
239 SpotLight.Diffuse.a = 0.0 'ディフューズ A
240 SpotLight.Specular.r = 0.1 'スペキュラー R
241 SpotLight.Specular.g = 0.1 'スペキュラー G
242 SpotLight.Specular.b = 0.1 'スペキュラー B
243 SpotLight.Specular.a = 0.0 'スペキュラー A
244 SpotLight.Ambient.r = 0.5 'アンビエント R
245 SpotLight.Ambient.g = 0.5 'アンビエント G
246 SpotLight.Ambient.b = 0.5 'アンビエント B
247 SpotLight.Ambient.a = 0.0 'アンビエント A
248 SpotLight.Position.x = 0 '光源の位置
249 SpotLight.Position.y = 5
250 SpotLight.Position.z = 0
251 SpotLight.Direction.x = 0 '光の方向
252 SpotLight.Direction.y = -1
253 SpotLight.Direction.z = 0
254 SpotLight.Range = 100.0 '有効範囲
255 SpotLight.Falloff = 1.0 '減衰率
256 SpotLight.Attenuation0 = 0.0 '輝度の減衰値
257 SpotLight.Attenuation1 = 1.0 '輝度の減衰値
258 SpotLight.Attenuation2 = 0.0 '輝度の減衰値
259 SpotLight.Theta = 30.0 * D3DX_PI/180.0 '内部コーン角度
260 SpotLight.Phi = 60.0 * D3DX_PI/180.0 '外部コーン角度
261 dx_lpD3DDEV->SetLight(1, VarPtr(SpotLight))
262 dx_lpD3DDEV->LightEnable(1, TRUE)
263
264 'ディレクショナルライトの設定
265 Dim DirectionalLight As D3DLIGHT9
266 ZeroMemory(VarPtr(DirectionalLight), SizeOf(D3DLIGHT9))
267 DirectionalLight.Type_ = D3DLIGHT_DIRECTIONAL '光源の種類
268 DirectionalLight.Diffuse.r = 0.7 'ディフューズ R
269 DirectionalLight.Diffuse.g = 0.7 'ディフューズ G
270 DirectionalLight.Diffuse.b = 0.8 'ディフューズ B
271 DirectionalLight.Diffuse.a = 0.0 'ディフューズ A
272 DirectionalLight.Specular.r = 0.1 'スペキュラー R
273 DirectionalLight.Specular.g = 0.1 'スペキュラー G
274 DirectionalLight.Specular.b = 0.1 'スペキュラー B
275 DirectionalLight.Specular.a = 0.0 'スペキュラー A
276 DirectionalLight.Ambient.r = 0.5 'アンビエント R
277 DirectionalLight.Ambient.g = 0.5 'アンビエント G
278 DirectionalLight.Ambient.b = 0.5 'アンビエント B
279 DirectionalLight.Ambient.a = 0.0 'アンビエント A
280 DirectionalLight.Direction.x=-1 '光の方向
281 DirectionalLight.Direction.y=-1
282 DirectionalLight.Direction.z=-1
283 dx_lpD3DDEV->SetLight(2, VarPtr(DirectionalLight))
284 dx_lpD3DDEV->LightEnable(2, TRUE)
285
286 'ライトの有効化
287 dx_lpD3DDEV->SetRenderState(D3DRS_SPECULARENABLE, TRUE) 'スペキュラー
288 dx_lpD3DDEV->SetRenderState(D3DRS_LIGHTING, TRUE) 'ライト
289End Sub
290
291' ライトをオフにする関数
292Sub dx_SetLightOff()
293 dx_lpD3DDEV->SetRenderState(D3DRS_LIGHTING, FALSE)
294End Sub
295
296' デバイスの復元を行う関数
297' メモ - Alt+Tabキーでタスクが切り替わるなどのアクションでデバイスが消失した後、
298' フォーカスが戻ったときに呼び出されます。
299' 別途、OnLostDevice、OnResetDevice及びライトの設定をコーディングしなけれ
300' ばなりません。
301Function dx_RestoreDevice() As DWord
302 Dim hr As DWord
303
304 hr=dx_lpD3DDEV->TestCooperativeLevel()
305 If hr=D3D_OK Then
306 dx_RestoreDevice=hr
307 Exit Function
308 End If
309
310 If hr=D3DERR_DEVICELOST Then
311 '現在は復元不可能の場合
312 dx_RestoreDevice=hr
313 Exit Function
314 End If
315
316
317 '----------
318 ' 復元する
319 '----------
320
321 ' TODO: この位置でOnLostDevice関数で解放可能なオブジェクトを解放します。
322 dx_lpD3DXFont->OnLostDevice()
323
324 'デバイスをリセット
325 hr=dx_lpD3DDEV->Reset(VarPtr(dx_d3dpp))
326
327 ' TODO: この位置でOnResetDevice関数でリセット可能なオブジェクトをリセットします。
328 dx_lpD3DXFont->OnResetDevice()
329
330 ' TODO: この位置でライトをリセットします。
331 dx_SetDefaultLight()
332
333 dx_RestoreDevice=hr
334End Function
335
336
337'--------------------------------------
338' 頂点バッファ2Dポリゴンモデルのクラス
339'--------------------------------------
340
341Type DEFAULTVERTEX2D
342 x As Single
343 y As Single
344 z As Single
345 rhw As Single
346 dwColor As DWord
347 tu As Single
348 tv As Single
349End Type
350
351Const D3DFVF_DEFAULT2D = D3DFVF_XYZRHW or D3DFVF_DIFFUSE or D3DFVF_TEX1
352
353Class CImage2D
354 lpD3DTexture As LPDIRECT3DTEXTURE9
355 vertices[ELM(4)] As DEFAULTVERTEX2D
356
357 TextureSize As SIZE
358Public
359 'コンストラクタ
360 Sub CImage2D()
361 vertices[0].z=0
362 vertices[0].rhw=1
363 vertices[0].dwColor=D3DCOLOR_RGBA(&HFF, &HFF, &HFF, &HFF)
364 vertices[0].tu=0
365 vertices[0].tv=0
366
367 vertices[1].z=0
368 vertices[1].rhw=1
369 vertices[1].dwColor=D3DCOLOR_RGBA(&HFF, &HFF, &HFF, &HFF)
370 vertices[1].tu=1
371 vertices[1].tv=0
372
373 vertices[2].z=0
374 vertices[2].rhw=1
375 vertices[2].dwColor=D3DCOLOR_RGBA(&HFF, &HFF, &HFF, &HFF)
376 vertices[2].tu=0
377 vertices[2].tv=1
378
379 vertices[3].z=0
380 vertices[3].rhw=1
381 vertices[3].dwColor=D3DCOLOR_RGBA(&HFF, &HFF, &HFF, &HFF)
382 vertices[3].tu=1
383 vertices[3].tv=1
384
385 lpD3DTexture=0
386 End Sub
387
388 'デストラクタ
389 Sub ~CImage2D()
390 If lpD3DTexture Then lpD3DTexture->Release()
391 End Sub
392
393 Sub SetColor(dwColor As D3DCOLOR)
394 vertices[0].dwColor=dwColor
395 vertices[1].dwColor=dwColor
396 vertices[2].dwColor=dwColor
397 vertices[3].dwColor=dwColor
398 End Sub
399
400 Function SetTexture(pFileName As BytePtr, TransparentColor As D3DCOLOR) As Long
401 '-----------------------
402 ' テクスチャを読み込む
403 '-----------------------
404
405 '画像の幅、高さを取得
406 Dim hBmp As HBITMAP
407 Dim BitmapReport As BITMAP
408 hBmp=LoadImage(NULL,pFileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE)
409 If hBmp=0 Then
410 SetTexture=0
411 lpD3DTexture=0
412 Exit Function
413 End If
414 GetObject(hBmp,SizeOf(BITMAP),BitmapReport)
415 DeleteObject(hBmp)
416 TextureSize.cx=BitmapReport.bmWidth
417 TextureSize.cy=BitmapReport.bmHeight
418
419 'テクスチャーの読み込み
420 Dim hr As Long
421 hr=D3DXCreateTextureFromFileEx(dx_lpD3DDEV,
422 pFileName,
423 D3DX_DEFAULT,
424 D3DX_DEFAULT,
425 D3DX_DEFAULT,
426 0,
427 D3DFMT_UNKNOWN,
428 D3DPOOL_MANAGED,
429 D3DX_DEFAULT,
430 D3DX_DEFAULT,
431 TransparentColor,
432 NULL,
433 NULL,
434 VarPtr(lpD3DTexture))
435 If hr<>S_OK Then
436 SetTexture=0
437 lpD3DTexture=0
438 Exit Function
439 End If
440
441 SetTexture=1
442 End Function
443
444 Sub Draw(x As Single, y As Single, width As Single, height As Single)
445 'テクスチャーブレンディングの設定
446 dx_lpD3DDEV->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE)
447 dx_lpD3DDEV->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE)
448 dx_lpD3DDEV->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE)
449
450 'アルファブレンディングの設定
451 dx_lpD3DDEV->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE)
452 dx_lpD3DDEV->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA)
453 dx_lpD3DDEV->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA)
454
455 'テクスチャー設定
456 dx_lpD3DDEV->SetTexture(0, lpD3DTexture)
457
458 dx_lpD3DDEV->SetFVF(D3DFVF_DEFAULT2D) '頂点フォーマット設定
459
460 vertices[0].x=x
461 vertices[0].y=y
462 vertices[0].tu=0
463 vertices[0].tv=0
464
465 vertices[1].x=x+width
466 vertices[1].y=y
467 vertices[1].tu=1
468 vertices[1].tv=0
469
470 vertices[2].x=x+width
471 vertices[2].y=y+height
472 vertices[2].tu=1
473 vertices[2].tv=1
474
475 vertices[3].x=x
476 vertices[3].y=y+height
477 vertices[3].tu=0
478 vertices[3].tv=1
479
480 dx_lpD3DDEV->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, vertices, SizeOf(DEFAULTVERTEX2D)) 'ポリゴン描画
481
482 'アルファブレンディングの無効化
483 dx_lpD3DDEV->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE)
484 End Sub
485
486 Sub DrawStretch(x As Single, y As Single, width As Single, height As Single,
487 ImageX As Single, ImageY As Single, ImageWidth As Single, ImageHeight As Single)
488
489 'テクスチャーブレンディングの設定
490 dx_lpD3DDEV->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE)
491 dx_lpD3DDEV->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE)
492 dx_lpD3DDEV->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE)
493
494 'アルファブレンディングの設定
495 dx_lpD3DDEV->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE)
496 dx_lpD3DDEV->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA)
497 dx_lpD3DDEV->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA)
498
499 'テクスチャー設定
500 dx_lpD3DDEV->SetTexture(0, lpD3DTexture)
501
502 dx_lpD3DDEV->SetFVF(D3DFVF_DEFAULT2D) '頂点フォーマット設定
503
504 ImageX=ImageX+0.5
505 ImageY=ImageY+0.5
506
507 vertices[0].x=x
508 vertices[0].y=y
509 vertices[0].tu=ImageX/TextureSize.cx
510 vertices[0].tv=ImageY/TextureSize.cy
511
512 vertices[1].x=x+width
513 vertices[1].y=y
514 vertices[1].tu=(ImageX+ImageWidth)/TextureSize.cx
515 vertices[1].tv=ImageY/TextureSize.cy
516
517 vertices[2].x=x+width
518 vertices[2].y=y+height
519 vertices[2].tu=(ImageX+ImageWidth)/TextureSize.cx
520 vertices[2].tv=(ImageY+ImageHeight)/TextureSize.cy
521
522 vertices[3].x=x
523 vertices[3].y=y+height
524 vertices[3].tu=ImageX/TextureSize.cx
525 vertices[3].tv=(ImageY+ImageHeight)/TextureSize.cy
526
527 dx_lpD3DDEV->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, vertices, SizeOf(DEFAULTVERTEX2D)) 'ポリゴン描画
528
529 'アルファブレンディングの無効化
530 dx_lpD3DDEV->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE)
531 End Sub
532End Class
533
534
535'--------------------------------------
536' 頂点バッファ3Dポリゴンモデルのクラス
537'--------------------------------------
538
539Type DEFAULTVERTEX3D
540 x As Single
541 y As Single
542 z As Single
543 dwColor As DWord
544 tu As Single
545 tv As Single
546End Type
547
548Const D3DFVF_DEFAULT3D = D3DFVF_XYZ or D3DFVF_DIFFUSE or D3DFVF_TEX1
549
550Class CRectPolygon
551 lpD3DVB As LPDIRECT3DVERTEXBUFFER9
552 lpD3DTexture As LPDIRECT3DTEXTURE9
553 vertices[3] As DEFAULTVERTEX3D
554Public
555
556 'コンストラクタ
557 Sub CRectPolygon()
558 vertices[0].dwColor=D3DCOLOR_RGBA(&HFF, &HFF, &HFF, &HFF)
559 vertices[0].tu=0
560 vertices[0].tv=0
561
562 vertices[1].dwColor=D3DCOLOR_RGBA(&HFF, &HFF, &HFF, &HFF)
563 vertices[1].tu=1
564 vertices[1].tv=0
565
566 vertices[2].dwColor=D3DCOLOR_RGBA(&HFF, &HFF, &HFF, &HFF)
567 vertices[2].tu=0
568 vertices[2].tv=1
569
570 vertices[3].dwColor=D3DCOLOR_RGBA(&HFF, &HFF, &HFF, &HFF)
571 vertices[3].tu=1
572 vertices[3].tv=1
573
574
575 '頂点バッファの生成
576 Dim hr As DWord
577 hr = dx_lpD3DDEV->CreateVertexBuffer(SizeOf(DEFAULTVERTEX3D)*4, 0,
578 D3DFVF_DEFAULT3D, D3DPOOL_DEFAULT, VarPtr(lpD3DVB), NULL)
579 If hr Then
580 lpD3DVB=0
581 Exit Sub
582 End If
583
584 lpD3DTexture=0
585 End Sub
586
587 'デストラクタ
588 Sub ~CRectPolygon()
589 If lpD3DVB Then lpD3DVB->Release()
590 If lpD3DTexture Then lpD3DTexture->Release()
591 End Sub
592
593 Sub SetVertices(pVertices As *D3DVECTOR)
594 vertices[0].x=pVertices[0].x
595 vertices[0].y=pVertices[0].y
596 vertices[0].z=pVertices[0].z
597
598 vertices[1].x=pVertices[1].x
599 vertices[1].y=pVertices[1].y
600 vertices[1].z=pVertices[1].z
601
602 vertices[2].x=pVertices[2].x
603 vertices[2].y=pVertices[2].y
604 vertices[2].z=pVertices[2].z
605
606 vertices[3].x=pVertices[3].x
607 vertices[3].y=pVertices[3].y
608 vertices[3].z=pVertices[3].z
609
610 '頂点バッファのコピー
611 Dim hr As Long
612 Dim pBufferOfVertices As VoidPtr
613 hr = lpD3DVB->Lock(0, SizeOf(DEFAULTVERTEX3D)*4, VarPtr(pBufferOfVertices), 0)
614 If hr Then Exit Sub
615 memcpy(pBufferOfVertices, vertices, SizeOf(DEFAULTVERTEX3D)*4)
616 lpD3DVB->Unlock()
617 End Sub
618
619 Sub SetColor(dwColor As D3DCOLOR)
620 vertices[0].dwColor=dwColor
621 vertices[1].dwColor=dwColor
622 vertices[2].dwColor=dwColor
623 vertices[3].dwColor=dwColor
624 End Sub
625
626 Function SetTexture(pFileName As BytePtr, TransparentColor As D3DCOLOR) As Long
627 'テクスチャーの読み込み
628 Dim hr As Long
629 hr=D3DXCreateTextureFromFileEx(dx_lpD3DDEV,
630 pFileName,
631 D3DX_DEFAULT,
632 D3DX_DEFAULT,
633 D3DX_DEFAULT,
634 0,
635 D3DFMT_UNKNOWN,
636 D3DPOOL_MANAGED,
637 D3DX_DEFAULT,
638 D3DX_DEFAULT,
639 TransparentColor,
640 NULL,
641 NULL,
642 VarPtr(lpD3DTexture))
643 If hr<>S_OK Then
644 SetTexture=0
645 lpD3DTexture=0
646 Exit Function
647 End If
648
649 SetTexture=1
650 End Function
651
652 Sub Draw(VectorPosition As *D3DVECTOR, VectorDirection As *D3DVECTOR)
653 Dim i As Long
654
655 'ワールド変換行列の設定
656 Dim mWorld As D3DXMATRIX
657 Dim mRotX As D3DXMATRIX
658 Dim mRotY As D3DXMATRIX
659 Dim mRotZ As D3DXMATRIX
660 Dim mTrans As D3DXMATRIX
661 Dim mScale As D3DXMATRIX
662 D3DXMatrixRotationX(VarPtr(mRotX), VectorDirection->x) 'X軸回転行列
663 D3DXMatrixRotationY(VarPtr(mRotY), VectorDirection->y) 'Y軸回転行列
664 D3DXMatrixRotationZ(VarPtr(mRotZ), VectorDirection->z) 'Z軸回転行列
665
666 '平行移動行列
667 D3DXMatrixTranslation(VarPtr(mTrans),VectorPosition->x,VectorPosition->y,VectorPosition->z)
668
669 'スケーリング行列
670 D3DXMatrixScaling(VarPtr(mScale),1,1,1)
671
672 Dim mTemp As D3DXMATRIX
673 D3DXMatrixMultiply(VarPtr(mWorld),VarPtr(mRotZ),VarPtr(mRotX))
674 D3DXMatrixMultiply(VarPtr(mWorld),VarPtr(mWorld),VarPtr(mRotY))
675 D3DXMatrixMultiply(VarPtr(mWorld),VarPtr(mWorld),VarPtr(mTrans))
676 D3DXMatrixMultiply(VarPtr(mWorld),VarPtr(mWorld),VarPtr(mScale))
677
678 dx_lpD3DDEV->SetTransform(D3DTS_WORLD, VarPtr(mWorld))
679
680
681 'テクスチャーブレンディングの設定
682 dx_lpD3DDEV->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE)
683 dx_lpD3DDEV->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE)
684 dx_lpD3DDEV->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE)
685
686 'アルファブレンディングの設定
687 dx_lpD3DDEV->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE)
688 dx_lpD3DDEV->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA)
689 dx_lpD3DDEV->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA)
690
691 'テクスチャー設定
692 dx_lpD3DDEV->SetTexture(0, lpD3DTexture)
693
694 dx_lpD3DDEV->SetStreamSource(0, lpD3DVB, 0, SizeOf(DEFAULTVERTEX3D)) '頂点バッファ設定
695 dx_lpD3DDEV->SetFVF(D3DFVF_DEFAULT3D) '頂点フォーマット設定
696 dx_lpD3DDEV->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2) 'ポリゴン描画
697
698 'アルファブレンディングの無効化
699 dx_lpD3DDEV->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE)
700 End Sub
701End Class
702
703
704'-------------------------------------
705' メッシュモデル(Xファイル)のクラス
706'-------------------------------------
707Class CMeshModel
708 pD3DXMesh As LPD3DXMESH 'メッシュ
709 D3DMaterial As *D3DMATERIAL9 '質感
710 ppD3DTexture As *LPDIRECT3DTEXTURE9 'テクスチャ
711 dwNumMaterials As DWord 'マテリアル数
712Public
713 'コンストラクタ
714 Sub CMeshModel(pszFileName_xfile As BytePtr)
715 Dim i As Long
716 Dim hr As DWord
717 Dim lpD3DXBuffer As LPD3DXBUFFER
718
719 'Xファイルの読み込み
720 D3DXLoadMeshFromX(pszFileName_xfile,
721 D3DXMESH_SYSTEMMEM,
722 dx_lpD3DDEV,
723 NULL,
724 VarPtr(lpD3DXBuffer),
725 NULL,
726 VarPtr(dwNumMaterials),
727 VarPtr(pD3DXMesh))
728
729
730 '質感・テクスチャーの読み込み
731 D3DMaterial=malloc(SizeOf(D3DMATERIAL9)*dwNumMaterials)
732 ppD3DTexture=malloc(SizeOf(DWord)*dwNumMaterials)
733
734 Dim d3dxMaterials As *D3DXMATERIAL
735 d3dxMaterials = lpD3DXBuffer->GetBufferPointer()
736
737 Dim a[255] As Byte
738 For i=0 To dwNumMaterials-1
739 '質感
740 memcpy(VarPtr(D3DMaterial[i]),VarPtr(d3dxMaterials[i].MatD3D),SizeOf(D3DMATERIAL9))
741 memcpy(VarPtr(D3DMaterial[i].Ambient),VarPtr(D3DMaterial[i].Diffuse),SizeOf(D3DCOLORVALUE))
742
743 'テクスチャー
744 hr = D3DXCreateTextureFromFile(dx_lpD3DDEV,
745 d3dxMaterials[i].pTextureFilename, VarPtr(ppD3DTexture[i]))
746 If hr Then
747 ppD3DTexture[i] = NULL
748 End If
749 Next
750
751
752 'マテリアル情報の開放
753 lpD3DXBuffer->Release()
754 End Sub
755
756 'デストラクタ
757 Sub ~CMeshModel()
758 '各種メモリを解放
759 Dim i As Long
760 Dim lpTex As LPDIRECT3DTEXTURE9
761
762 If ppD3DTexture Then
763 For i=0 To dwNumMaterials-1
764 If ppD3DTexture[i] Then
765 lpTex=ppD3DTexture[i] As LPDIRECT3DTEXTURE9
766 lpTex->Release()
767 ppD3DTexture[i]=0
768 End If
769 Next
770 free(ppD3DTexture)
771 End If
772
773 If D3DMaterial Then
774 free(D3DMaterial)
775 End If
776
777 pD3DXMesh->Release()
778 End Sub
779
780 'メッシュを描画
781 Sub Draw(VectorPosition As *D3DVECTOR, VectorDirection As *D3DVECTOR)
782 Dim i As Long
783
784 'ワールド変換行列の設定
785 Dim mWorld As D3DXMATRIX
786 Dim mRotX As D3DXMATRIX
787 Dim mRotY As D3DXMATRIX
788 Dim mRotZ As D3DXMATRIX
789 Dim mTrans As D3DXMATRIX
790 Dim mScale As D3DXMATRIX
791 D3DXMatrixRotationX(VarPtr(mRotX), VectorDirection->x) 'X軸回転行列
792 D3DXMatrixRotationY(VarPtr(mRotY), VectorDirection->y) 'Y軸回転行列
793 D3DXMatrixRotationZ(VarPtr(mRotZ), VectorDirection->z) 'Z軸回転行列
794
795 '平行移動行列
796 D3DXMatrixTranslation(VarPtr(mTrans),VectorPosition->x,VectorPosition->y,VectorPosition->z)
797
798 'スケーリング行列
799 D3DXMatrixScaling(VarPtr(mScale),1,1,1)
800
801 Dim mTemp As D3DXMATRIX
802 D3DXMatrixMultiply(VarPtr(mWorld),VarPtr(mRotZ),VarPtr(mRotX))
803 D3DXMatrixMultiply(VarPtr(mWorld),VarPtr(mWorld),VarPtr(mRotY))
804 D3DXMatrixMultiply(VarPtr(mWorld),VarPtr(mWorld),VarPtr(mTrans))
805 D3DXMatrixMultiply(VarPtr(mWorld),VarPtr(mWorld),VarPtr(mScale))
806
807 dx_lpD3DDEV->SetTransform(D3DTS_WORLD, VarPtr(mWorld))
808
809
810 'Xファイルの表示
811 For i=0 To dwNumMaterials-1
812 dx_lpD3DDEV->SetMaterial(VarPtr(D3DMaterial[i]))
813 dx_lpD3DDEV->SetTexture(0, ppD3DTexture[i] As *IDirect3DBaseTexture9)
814 pD3DXMesh->DrawSubset(i)
815 Next
816 End Sub
817End Class
Note: See TracBrowser for help on using the repository browser.