• R/O
  • SSH
  • HTTPS

Commit

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

X operations(XOPS)に非常に近いFPSゲームを制作・リメイクし、成果物をオープンソースとして公開することを目的としたプロジェクトです。


Commit MetaInfo

Revision95 (tree)
Time2015-06-28 18:52:02
Authorxops-mikan

Log Message

グラフィックスで用いるDirectXとOpenGLをソースファイルごと分割

Change Summary

Incremental Difference

--- trunk/d3dgraphics.cpp (revision 94)
+++ trunk/d3dgraphics.cpp (nonexistent)
@@ -1,3726 +0,0 @@
1-//! @file d3dgraphics.cpp
2-//! @brief D3DGraphicsクラスの定義
3-
4-//--------------------------------------------------------------------------------
5-//
6-// OpenXOPS
7-// Copyright (c) 2014-2015, OpenXOPS Project / [-_-;](mikan) All rights reserved.
8-//
9-// Redistribution and use in source and binary forms, with or without
10-// modification, are permitted provided that the following conditions are met:
11-// * Redistributions of source code must retain the above copyright notice,
12-// this list of conditions and the following disclaimer.
13-// * Redistributions in binary form must reproduce the above copyright notice,
14-// this list of conditions and the following disclaimer in the documentation
15-// and/or other materials provided with the distribution.
16-// * Neither the name of the OpenXOPS Project nor the names of its contributors
17-// may be used to endorse or promote products derived from this software
18-// without specific prior written permission.
19-//
20-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21-// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22-// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23-// DISCLAIMED. IN NO EVENT SHALL OpenXOPS Project BE LIABLE FOR ANY
24-// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25-// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26-// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27-// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29-// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30-//--------------------------------------------------------------------------------
31-
32-// ***** OpenGL core only *****
33-//
34-// libjpeg
35-// Copyright (C) 1991-2014, Thomas G. Lane, Guido Vollbeding.
36-// this software is based in part on the work of the Independent JPEG Group
37-//
38-// zlib
39-// Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler
40-//
41-// libpng
42-// Copyright (c) 1998-2014 Glenn Randers-Pehrson
43-//
44-// ****************************
45-
46-#include "d3dgraphics.h"
47-
48-#ifndef GRAPHICS_OPENGL
49-
50-//! @brief コンストラクタ
51-D3DGraphics::D3DGraphics()
52-{
53- pD3D = NULL;
54- pd3dDevice = NULL;
55- aspect = 1.0f;
56- fullscreenflag = false;
57- for(int i=0; i<MAX_MODEL; i++){
58- pmesh[i] = NULL;
59- }
60- for(int i=0; i<MAX_TEXTURE; i++){
61- ptextures[i] = NULL;
62- }
63-
64- blockdata = NULL;
65- for(int i=0; i<TOTAL_BLOCKTEXTURE; i++){
66- mapTextureID[i] = -1;
67- }
68-
69- StartRenderFlag = false;
70-
71- //ptextsprite = NULL;
72- pxmsfont = NULL;
73- TextureFont = -1;
74-}
75-
76-//! @brief ディストラクタ
77-D3DGraphics::~D3DGraphics()
78-{
79- //リソース解放
80- CleanupD3Dresource();
81-
82- if( pd3dDevice != NULL ) pd3dDevice->Release();
83- if( pD3D != NULL ) pD3D->Release();
84-
85-#ifdef ENABLE_DEBUGLOG
86- //ログに出力
87- OutputLog.WriteLog(LOG_CLEANUP, "グラフィック", "DirectX");
88-#endif
89-}
90-
91-//! @brief 初期化@n
92-//! (DirectX 9)
93-//! @param WindowCtrl WindowControlクラスのポインタ
94-//! @param TextureFontFilename 使用するテクスチャフォントのファイル名
95-//! @param fullscreen false:ウィンドウ表示 true:フルスクリーン用表示
96-//! @return 成功:0 失敗:1
97-int D3DGraphics::InitD3D(WindowControl *WindowCtrl, char *TextureFontFilename, bool fullscreen)
98-{
99-#ifdef ENABLE_DEBUGLOG
100- //ログに出力
101- OutputLog.WriteLog(LOG_INIT, "グラフィック", "DirectX");
102-#endif
103-
104- D3DPRESENT_PARAMETERS d3dpp;
105- RECT rec;
106-
107- GetClientRect(WindowCtrl->GethWnd(), &rec);
108-
109- fullscreenflag = fullscreen;
110-
111- //D3D9の作成
112- pD3D = Direct3DCreate9(D3D_SDK_VERSION);
113- if( pD3D == NULL ){
114- return 1;
115- }
116-
117- //D3Dデバイスの作成
118- ZeroMemory(&d3dpp, sizeof(d3dpp));
119- if( fullscreenflag == false ){
120- d3dpp.Windowed = TRUE;
121- d3dpp.BackBufferWidth = rec.right;
122- d3dpp.BackBufferHeight = rec.bottom;
123- d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
124- d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
125- d3dpp.EnableAutoDepthStencil = TRUE;
126- d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
127- d3dpp.FullScreen_RefreshRateInHz = 0;
128- }
129- else{
130- D3DDISPLAYMODE dispmode;
131- pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &dispmode);
132-
133- d3dpp.Windowed = FALSE;
134- d3dpp.BackBufferWidth = rec.right;
135- d3dpp.BackBufferHeight = rec.bottom;
136- d3dpp.BackBufferFormat = dispmode.Format;
137- d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
138- d3dpp.EnableAutoDepthStencil = TRUE;
139- d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
140- d3dpp.FullScreen_RefreshRateInHz = dispmode.RefreshRate;
141- }
142-
143- if( FAILED( pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, WindowCtrl->GethWnd(), D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &pd3dDevice) ) ){
144- if( FAILED( pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, WindowCtrl->GethWnd(), D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pd3dDevice) ) ){
145- return 1;
146- }
147- }
148-
149-#ifdef ENABLE_DEBUGLOG
150- //ログに出力
151- OutputLog.WriteLog(LOG_COMPLETE, "", "");
152-#endif
153-
154- //テクスチャフォント用画像のファイル名を設定
155- strcpy(TextureFontFname, TextureFontFilename);
156-
157- //描画関係の詳細な設定
158- if( InitSubset() != 0){
159- return 1;
160- }
161-
162-
163- //アスペクト比を設定
164- aspect = (float)rec.right / (float)rec.bottom;
165-
166- //マウスカーソルを消す
167- //ShowCursor(FALSE);
168-
169-
170- float aspecth, prx, pry, r;
171- aspecth = (float)SCREEN_WIDTH/SCREEN_HEIGHT;
172-
173- //HUD_myweapon [奥行き, 縦, 横]
174-
175- //HUD_A 現在持っている武器を表示する座標
176- prx = DegreeToRadian(-39) * aspecth /2;
177- pry = DegreeToRadian(-55) /2;
178- r = 7.5f;
179- HUD_myweapon_x[0] = cos(pry)*r;
180- HUD_myweapon_y[0] = sin(pry)*r;
181- HUD_myweapon_z[0] = sin(prx)*r;
182-
183- //HUD_A 予備の武器を表示する座標
184- prx = DegreeToRadian(-52) * aspecth /2;
185- pry = DegreeToRadian(-60) /2;
186- r = 16.0f;
187- HUD_myweapon_x[1] = cos(pry)*r;
188- HUD_myweapon_y[1] = sin(pry)*r;
189- HUD_myweapon_z[1] = sin(prx)*r;
190-
191- return 0;
192-}
193-
194-//! @brief リセット@n
195-//! (ウィンドウ最小化からの復帰 など)
196-//! @param WindowCtrl WindowControlクラスのポインタ
197-//! @return 成功:0 待ち:1 失敗:2
198-int D3DGraphics::ResetD3D(WindowControl *WindowCtrl)
199-{
200- //フォーカスを失っているなら待たせる
201- if( pd3dDevice->TestCooperativeLevel() == D3DERR_DEVICELOST ){
202- return 1;
203- }
204-
205-#ifdef ENABLE_DEBUGLOG
206- //ログに出力
207- OutputLog.WriteLog(LOG_INIT, "グラフィック", "DirectX(リセット)");
208-#endif
209-
210- //リソース解放
211- CleanupD3Dresource();
212-
213- D3DPRESENT_PARAMETERS d3dpp;
214- RECT rec;
215-
216- GetClientRect(WindowCtrl->GethWnd(), &rec);
217-
218- //D3Dデバイスの作成
219- ZeroMemory(&d3dpp, sizeof(d3dpp));
220- if( fullscreenflag == false ){
221- d3dpp.Windowed = TRUE;
222- d3dpp.BackBufferWidth = rec.right;
223- d3dpp.BackBufferHeight = rec.bottom;
224- d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
225- d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
226- d3dpp.EnableAutoDepthStencil = TRUE;
227- d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
228- d3dpp.FullScreen_RefreshRateInHz = 0;
229- }
230- else{
231- D3DDISPLAYMODE dispmode;
232- pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &dispmode);
233-
234- d3dpp.Windowed = FALSE;
235- d3dpp.BackBufferWidth = rec.right;
236- d3dpp.BackBufferHeight = rec.bottom;
237- d3dpp.BackBufferFormat = dispmode.Format;
238- d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
239- d3dpp.EnableAutoDepthStencil = TRUE;
240- d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
241- d3dpp.FullScreen_RefreshRateInHz = dispmode.RefreshRate;
242- }
243-
244- if( FAILED( pd3dDevice->Reset(&d3dpp) ) ){
245- return 2;
246- }
247-
248- //描画関係の詳細な設定
249- if( InitSubset() != 0){
250- return 2;
251- }
252-
253-#ifdef ENABLE_DEBUGLOG
254- //ログに出力
255- OutputLog.WriteLog(LOG_COMPLETE, "", "");
256-#endif
257-
258- return 0;
259-}
260-
261-//! @brief 描画関係の細部設定
262-//! @attention 初期化時に1度だけ実行してください。
263-int D3DGraphics::InitSubset()
264-{
265- //ライト
266- //pd3dDevice->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_ARGB(0,255,255,255) );
267- pd3dDevice->LightEnable(0, FALSE);
268- pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
269-
270- //フォグ
271- float fog_st = 100;
272- float fog_end = 800;
273- pd3dDevice->SetRenderState(D3DRS_FOGENABLE, TRUE);
274- pd3dDevice->SetRenderState(D3DRS_FOGCOLOR, D3DCOLOR_RGBA(0, 0, 0, 0));
275- pd3dDevice->SetRenderState(D3DRS_FOGTABLEMODE, D3DFOG_LINEAR);
276- pd3dDevice->SetRenderState(D3DRS_FOGVERTEXMODE, D3DFOG_NONE);
277- pd3dDevice->SetRenderState(D3DRS_FOGSTART,*(DWORD*)(&fog_st));
278- pd3dDevice->SetRenderState(D3DRS_FOGEND, *(DWORD*)(&fog_end));
279-
280- // テクスチャフィルタを使う
281- pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
282- pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
283- pd3dDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
284-
285- //ミップマップの詳細レベル (LOD) バイアスを指定する。
286- float LODBias = -0.2f;
287- pd3dDevice->SetSamplerState(0, D3DSAMP_MIPMAPLODBIAS, *((LPDWORD)(&LODBias)) );
288-
289- //アルファ・ブレンディングを行う
290- pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
291-
292- //透過処理を行う
293- pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
294- pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
295-
296- //アルファテストに対応しているかチェック
297- D3DCAPS9 Caps;
298- pd3dDevice->GetDeviceCaps(&Caps);
299- if( Caps.AlphaCmpCaps & D3DPCMPCAPS_GREATEREQUAL ){
300- //アルファテスト設定
301- // 完全に透明なピクセルは描画しない
302- pd3dDevice->SetRenderState(D3DRS_ALPHAREF, (DWORD)0x00000001);
303- pd3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);
304- pd3dDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL);
305- }
306-
307- //深度バッファ比較関数
308- pd3dDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
309- pd3dDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESS);
310-
311- //ポリゴンの裏・表
312- pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
313-
314-
315- //テキストスプライト初期化
316- if( FAILED( D3DXCreateSprite( pd3dDevice, &ptextsprite ) ) ){
317- return 1;
318- }
319- //フォント名:MS ゴシック サイズ:18
320- HRESULT hr = D3DXCreateFont( pd3dDevice, -18, 0, FW_NORMAL, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
321- DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "MS ゴシック", &pxmsfont);
322- if( FAILED(hr) ) return 1;
323-
324- //テクスチャフォント用画像を取得
325- TextureFont = LoadTexture(TextureFontFname, true, false);
326- return 0;
327-}
328-
329-//! @brief デバイスのリソースを解放
330-void D3DGraphics::CleanupD3Dresource()
331-{
332- if( TextureFont != -1 ){ CleanupTexture(TextureFont); }
333- if( pxmsfont != NULL ){
334- pxmsfont->Release();
335- pxmsfont = NULL;
336- }
337- if( ptextsprite != NULL ){ ptextsprite->Release(); }
338-
339- CleanupMapdata();
340-
341- for(int i=0; i<MAX_MODEL; i++){
342- CleanupModel(i);
343- }
344- for(int i=0; i<MAX_TEXTURE; i++){
345- CleanupTexture(i);
346- }
347-}
348-
349-//! @brief モデルファイルを読み込む(.x)
350-//! @param filename ファイル名
351-//! @return 成功:モデル認識番号(0以上) 失敗:-1
352-int D3DGraphics::LoadModel(char* filename)
353-{
354-#ifdef ENABLE_DEBUGLOG
355- //ログに出力
356- OutputLog.WriteLog(LOG_LOAD, "モデル", filename);
357-#endif
358-
359- int id = -1;
360-
361- //空いている要素を探す
362- for(int i=0; i<MAX_MODEL; i++){
363- if( pmesh[i] == NULL ){
364- id = i;
365- break;
366- }
367- }
368- if( id == -1 ){ return -1; }
369-
370- LPD3DXBUFFER pD3DXMtrlBuffer;
371-
372-#ifdef PATH_DELIMITER_SLASH
373- //パス区切り文字を変換
374- filename = ChangePathDelimiter(filename);
375-#endif
376-
377- //.xファイルをバッファーに読み込む
378- if( FAILED( D3DXLoadMeshFromX( filename, D3DXMESH_SYSTEMMEM, pd3dDevice, NULL,
379- &pD3DXMtrlBuffer, NULL, &nummaterials[id], &pmesh[id] ) ) ) {
380- return -1;
381- }
382-
383- //マテリアル情報を取得
384- D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();
385- int num = nummaterials[id];
386- pmaterials[id] = new D3DMATERIAL9[num];
387- if( pmaterials[id] == NULL ) return -1;
388-
389- //構造体に代入
390- for( int i=0; i<num; i=i+1 ){
391- pmaterials[id][i] = d3dxMaterials[i].MatD3D;
392- pmaterials[id][i].Ambient = pmaterials[id][i].Diffuse;
393- }
394-
395- //バッファを開放
396- pD3DXMtrlBuffer->Release();
397-
398-#ifdef ENABLE_DEBUGLOG
399- //ログに出力
400- OutputLog.WriteLog(LOG_COMPLETE, "", id);
401-#endif
402- return id;
403-}
404-
405-//! @brief モデルファイルの中間データを作成(モーフィング)
406-//! @param idA モデルAの認識番号
407-//! @param idB モデルBの認識番号
408-//! @return 成功:新しいモデル認識番号(0以上) 失敗:-1
409-//! @attention モデルAとモデルBは、頂点数・ポリゴン数・インデックスが同じである必要があります。
410-//! @attention それぞれのモデルデータが正しくないか 頂点数が異なる場合、実行に失敗します。
411-int D3DGraphics::MorphingModel(int idA, int idB)
412-{
413-#ifdef ENABLE_DEBUGLOG
414- char str[128];
415- sprintf(str, "中間データ作成  ID:%d and %d", idA, idB);
416-
417- //ログに出力
418- OutputLog.WriteLog(LOG_LOAD, "モデル", str);
419-#endif
420-
421- //データが正しいか調べる
422- if( (idA < 0)||((MAX_MODEL -1) < idA) ){ return -1; }
423- if( pmesh[idA] == NULL ){ return -1; }
424- if( (idB < 0)||((MAX_MODEL -1) < idB) ){ return -1; }
425- if( pmesh[idB] == NULL ){ return -1; }
426-
427- int idN = -1;
428- int numvA, numvB;
429- LPDIRECT3DVERTEXBUFFER9 pvbA, pvbB, pvbN;
430- D3DXVECTOR3 *pVerticesA, *pVerticesB, *pVerticesN;
431- int FVFsize;
432-
433- //空いている要素を探す
434- for(int i=0; i<MAX_MODEL; i++){
435- if( pmesh[i] == NULL ){
436- idN = i;
437- break;
438- }
439- }
440- if( idN == -1 ){ return -1; }
441-
442- //頂点数を取得
443- numvA = pmesh[idA]->GetNumVertices();
444- numvB = pmesh[idB]->GetNumVertices();
445-
446- //頂点数が同じかどうか調べる
447- if( numvA != numvB ){ return -1; }
448-
449- //頂点データをコピー(実質的に領域確保用のダミー)
450- if( pmesh[idA]->CloneMeshFVF(pmesh[idA]->GetOptions(), pmesh[idA]->GetFVF(), pd3dDevice, &pmesh[idN]) != D3D_OK ){
451- return -1;
452- }
453-
454- //マテリアル情報をコピー
455- int num = nummaterials[idA];
456- nummaterials[idN] = nummaterials[idA];
457- pmaterials[idN] = new D3DMATERIAL9[num];
458- if( pmaterials[idN] == NULL ) return -1;
459- for( int i=0; i<num; i=i+1 ){
460- pmaterials[idN][i] = pmaterials[idA][i];
461- }
462-
463- //バッファーを取得
464- pmesh[idA]->GetVertexBuffer(&pvbA);
465- pmesh[idB]->GetVertexBuffer(&pvbB);
466- pmesh[idN]->GetVertexBuffer(&pvbN);
467-
468- //1頂点当たりのバイト数取得
469- FVFsize = D3DXGetFVFVertexSize(pmesh[idN]->GetFVF());
470-
471- //各頂点を読み出し計算
472- for(int i=0; i<numvA; i++){
473- pvbA->Lock(i*FVFsize, sizeof(D3DXVECTOR3), (void**)&pVerticesA, D3DLOCK_READONLY);
474- pvbB->Lock(i*FVFsize, sizeof(D3DXVECTOR3), (void**)&pVerticesB, D3DLOCK_READONLY);
475- pvbN->Lock(i*FVFsize, sizeof(D3DXVECTOR3), (void**)&pVerticesN, 0);
476-
477- //平均化
478- pVerticesN->x = (pVerticesA->x + pVerticesB->x)/2;
479- pVerticesN->y = (pVerticesA->y + pVerticesB->y)/2;
480- pVerticesN->z = (pVerticesA->z + pVerticesB->z)/2;
481-
482- pvbA->Unlock();
483- pvbB->Unlock();
484- pvbN->Unlock();
485- }
486-
487-#ifdef ENABLE_DEBUGLOG
488- //ログに出力
489- OutputLog.WriteLog(LOG_COMPLETE, "", idN);
490-#endif
491- return idN;
492-}
493-
494-//! @brief モデルファイルを解放
495-//! @param id モデル認識番号
496-void D3DGraphics::CleanupModel(int id)
497-{
498- if( (id < 0)||((MAX_MODEL -1) < id) ){ return; }
499- if( pmesh[id] != NULL ){
500- delete [] pmaterials[id];
501-
502- pmesh[id]->Release();
503- pmesh[id] = NULL;
504-
505-#ifdef ENABLE_DEBUGLOG
506- //ログに出力
507- OutputLog.WriteLog(LOG_CLEANUP, "モデル", id);
508-#endif
509- }
510-}
511-
512-//! @brief テクスチャを読み込む
513-//! @param filename ファイル名
514-//! @param texturefont テクスチャフォントフラグ
515-//! @param BlackTransparent 黒を透過する
516-//! @return 成功:テクスチャ認識番号(0以上) 失敗:-1
517-int D3DGraphics::LoadTexture(char* filename, bool texturefont, bool BlackTransparent)
518-{
519- int id = -1;
520- D3DXIMAGE_INFO info;
521- int MipLevels;
522-
523-#ifdef ENABLE_DEBUGLOG
524- //ログに出力
525- OutputLog.WriteLog(LOG_LOAD, "テクスチャ", filename);
526-#endif
527-
528- //空いている認識番号を探す
529- for(int i=0; i<MAX_TEXTURE; i++){
530- if( ptextures[i] == NULL ){
531- id = i;
532- break;
533- }
534- }
535- if( id == -1 ){ return -1; }
536-
537-#ifdef PATH_DELIMITER_SLASH
538- //パス区切り文字を変換
539- filename = ChangePathDelimiter(filename);
540-#endif
541-
542- //ファイル情報を取得
543- if( D3DXGetImageInfoFromFile(filename, &info) != D3D_OK ){ return -1; }
544-
545- //ミップマップレベルを設定
546- if( texturefont == true ){
547- MipLevels = 1;
548- }
549- else{
550- MipLevels = 4;//D3DX_DEFAULT;
551- }
552-
553- //テクスチャを読み込む
554- if( BlackTransparent == false ){
555- if( FAILED( D3DXCreateTextureFromFileEx(pd3dDevice, filename, info.Width, info.Height, MipLevels, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0x00000000, NULL, NULL, &ptextures[id]) ) ) {
556- return -1;
557- }
558- }
559- else{
560- if( FAILED( D3DXCreateTextureFromFileEx(pd3dDevice, filename, info.Width, info.Height, MipLevels, 0, D3DFMT_A1R5G5B5, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_ARGB(255, 0, 0, 0), NULL, NULL, &ptextures[id]) ) ) {
561- return -1;
562- }
563- }
564-
565-#ifdef ENABLE_DEBUGLOG
566- //ログに出力
567- OutputLog.WriteLog(LOG_COMPLETE, "", id);
568-#endif
569- return id;
570-}
571-
572-//! @brief テクスチャのサイズを取得
573-//! @param id テクスチャ認識番号
574-//! @param width 幅を受け取るポインタ
575-//! @param height 高さを受け取るポインタ
576-//! @return 成功:0 失敗:1
577-//! @attention サーフェイスのサイズを取得します。GPUにロードされたサイズであり、テクスチャ(現物)と異なる場合があります。
578-int D3DGraphics::GetTextureSize(int id, int *width, int *height)
579-{
580- //無効な認識番号が指定されていたら、処理せず返す。
581- if( id == -1 ){ return 1; }
582- if( ptextures[id] == NULL ){ return 1; }
583-
584- IDirect3DSurface9 *surface;
585- D3DSURFACE_DESC desc;
586-
587- //サーフェイスを取得
588- ptextures[id]->GetSurfaceLevel(0, &surface);
589-
590- //幅と高さを取得
591- surface->GetDesc(&desc);
592- *width = desc.Width;
593- *height = desc.Height;
594-
595- //サーフェイスを開放
596- surface->Release();
597-
598- return 0;
599-}
600-
601-//! @brief テクスチャを解放
602-//! @param id テクスチャ認識番号
603-void D3DGraphics::CleanupTexture(int id)
604-{
605- if( (id < 0)||((MAX_TEXTURE -1) < id) ){ return; }
606- if( ptextures[id] != NULL ){
607- ptextures[id]->Release();
608- ptextures[id] = NULL;
609-
610-#ifdef ENABLE_DEBUGLOG
611- //ログに出力
612- OutputLog.WriteLog(LOG_CLEANUP, "テクスチャ", id);
613-#endif
614- }
615-}
616-
617-//! @brief 全ての描画処理を開始
618-//! @return 成功:0 失敗:1
619-//! @attention 描画処理の最初に呼び出す必要があります。
620-int D3DGraphics::StartRender()
621-{
622- if( StartRenderFlag == true ){ return 1; }
623-
624- //領域を初期化
625- pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
626-
627- if( SUCCEEDED( pd3dDevice->BeginScene() ) ){
628- //Zバッファ初期化
629- pd3dDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
630-
631- //座標ゼロ地点にワールド変換行列
632- ResetWorldTransform();
633-
634- //描画中のフラグを立てる
635- StartRenderFlag = true;
636- return 0;
637- }
638-
639- return 1;
640-}
641-
642-//! @brief 全ての描画処理を終了
643-//! @return 成功:false 失敗:true
644-//! @attention 描画処理の最後に呼び出す必要があります。
645-bool D3DGraphics::EndRender()
646-{
647- //描画中なら終了
648- if( StartRenderFlag == true ){
649- pd3dDevice->EndScene();
650- }
651-
652- HRESULT hr = pd3dDevice->Present(NULL, NULL, NULL, NULL);
653-
654- //フラグを false に
655- StartRenderFlag = false;
656-
657- if( hr == D3DERR_DEVICELOST ){
658- return true;
659- }
660- return false;
661-}
662-
663-//! @brief Zバッファをリセット
664-void D3DGraphics::ResetZbuffer()
665-{
666- //Zバッファを一度無効にし、初期化後、再度有効に
667- pd3dDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
668- pd3dDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
669- pd3dDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
670-}
671-
672-//! @brief ワールド空間を原点(0,0,0)に戻す など
673-void D3DGraphics::ResetWorldTransform()
674-{
675- D3DXMATRIX matWorld;
676- D3DXMatrixIdentity(&matWorld);
677- pd3dDevice->SetTransform(D3DTS_WORLD, &matWorld);
678-}
679-
680-//! @brief ワールド空間の座標・角度・拡大率を設定
681-//! @param x X座標
682-//! @param y Y座標
683-//! @param z Z座標
684-//! @param rx 横軸角度
685-//! @param ry 縦軸角度
686-//! @param size 拡大率
687-void D3DGraphics::SetWorldTransform(float x, float y, float z, float rx, float ry, float size)
688-{
689- SetWorldTransform(x, y, z, rx, ry, 0.0f, size);
690-}
691-
692-//! @brief ワールド空間の座標・角度・拡大率を設定
693-//! @param x X座標
694-//! @param y Y座標
695-//! @param z Z座標
696-//! @param rx 横軸角度
697-//! @param ry1 縦軸角度
698-//! @param ry2 縦軸角度
699-//! @param size 拡大率
700-void D3DGraphics::SetWorldTransform(float x, float y, float z, float rx, float ry1, float ry2, float size)
701-{
702- D3DXMATRIX matWorld;
703- D3DXMATRIX matWorld1, matWorld2, matWorld3, matWorld4, matWorld5;
704-
705- //行列を作成
706- D3DXMatrixTranslation(&matWorld1, x, y, z);
707- D3DXMatrixRotationY(&matWorld2, rx);
708- D3DXMatrixRotationX(&matWorld3, ry1);
709- D3DXMatrixRotationZ(&matWorld4, ry2);
710- D3DXMatrixScaling(&matWorld5, size, size, size);
711-
712- //計算
713- matWorld = matWorld5 * matWorld4 * matWorld3 * matWorld2 * matWorld1;
714-
715- //適用
716- pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
717-}
718-
719-//! @brief ワールド空間の座標・角度・拡大率を設定(エフェクト用)
720-//! @param x X座標
721-//! @param y Y座標
722-//! @param z Z座標
723-//! @param rx 横軸角度
724-//! @param ry 縦軸角度
725-//! @param rt 回転角度
726-//! @param size 拡大率
727-void D3DGraphics::SetWorldTransformEffect(float x, float y, float z, float rx, float ry, float rt, float size)
728-{
729- D3DXMATRIX matWorld;
730- D3DXMATRIX matWorld1, matWorld2, matWorld3, matWorld4, matWorld5;
731-
732- //行列を作成
733- D3DXMatrixTranslation(&matWorld1, x, y, z);
734- D3DXMatrixRotationY(&matWorld2, rx);
735- D3DXMatrixRotationZ(&matWorld3, ry);
736- D3DXMatrixRotationX(&matWorld4, rt);
737- D3DXMatrixScaling(&matWorld5, size, size, size);
738-
739- //計算
740- matWorld = matWorld5 * matWorld4 * matWorld3 * matWorld2 * matWorld1;
741-
742- //適用
743- pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
744-}
745-
746-//! @brief ワールド空間を人が武器を持つ場所に設定
747-//! @param x X座標
748-//! @param y Y座標
749-//! @param z Z座標
750-//! @param mx 手元を原点にした モデルのX座標
751-//! @param my 手元を原点にした モデルのY座標
752-//! @param mz 手元を原点にした モデルのZ座標
753-//! @param rx 横軸角度
754-//! @param ry 縦軸角度
755-//! @param size 拡大率
756-void D3DGraphics::SetWorldTransformHumanWeapon(float x, float y, float z, float mx, float my, float mz, float rx, float ry, float size)
757-{
758- D3DXMATRIX matWorld;
759- D3DXMATRIX matWorld1, matWorld2, matWorld3, matWorld4, matWorld5;
760-
761- //行列を作成
762- D3DXMatrixTranslation(&matWorld1, x, y, z);
763- D3DXMatrixRotationY(&matWorld2, rx);
764- D3DXMatrixRotationX(&matWorld3, ry);
765- D3DXMatrixTranslation(&matWorld4, mx, my, mz);
766- D3DXMatrixScaling(&matWorld5, size, size, size);
767-
768- //計算
769- matWorld = matWorld5 * matWorld4 * matWorld3 * matWorld2 * matWorld1;
770-
771- //適用
772- pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
773-}
774-
775-//! @brief ワールド空間を所持している武器を表示する場所に設定
776-//! @param rotation 武器を回転させる
777-//! @param camera_x カメラのX座標
778-//! @param camera_y カメラのY座標
779-//! @param camera_z カメラのZ座標
780-//! @param camera_rx カメラの横軸角度
781-//! @param camera_ry カメラの縦軸角度
782-//! @param rx 武器のの縦軸角度
783-//! @param size 表示サイズ
784-//! @note rotation・・ true:現在持っている武器です。 false:予備の武器です。(rx は無視されます)
785-//! @todo 位置やサイズの微調整
786-void D3DGraphics::SetWorldTransformPlayerWeapon(bool rotation, float camera_x, float camera_y, float camera_z, float camera_rx, float camera_ry, float rx, float size)
787-{
788- D3DXMATRIX matWorld;
789- D3DXMATRIX matWorld1, matWorld2, matWorld3, matWorld4, matWorld5, matWorld6;
790-
791- size = size * 0.3f;
792-
793- //行列を作成
794- D3DXMatrixTranslation(&matWorld1, camera_x, camera_y, camera_z);
795- D3DXMatrixRotationY(&matWorld2, camera_rx *-1);
796- D3DXMatrixRotationZ(&matWorld3, camera_ry);
797- // matWorld4 = [奥行き, 縦, 横]
798- if( rotation == true ){
799- D3DXMatrixTranslation(&matWorld4, HUD_myweapon_x[0], HUD_myweapon_y[0], HUD_myweapon_z[0]);
800- D3DXMatrixRotationY(&matWorld5, rx);
801- }
802- else{
803- D3DXMatrixTranslation(&matWorld4, HUD_myweapon_x[1], HUD_myweapon_y[1], HUD_myweapon_z[1]);
804- D3DXMatrixRotationY(&matWorld5, D3DX_PI);
805- }
806- D3DXMatrixScaling(&matWorld6, size, size, size);
807-
808- //計算
809- matWorld = matWorld6 * matWorld5 * matWorld4 * matWorld3 * matWorld2 * matWorld1;
810-
811- //適用
812- pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
813-}
814-
815-//! @brief ワールド空間の座標を取得
816-//! @param *x x軸を受け取るポインタ
817-//! @param *y y軸を受け取るポインタ
818-//! @param *z z軸を受け取るポインタ
819-void D3DGraphics::GetWorldTransformPos(float *x, float *y, float *z)
820-{
821- D3DXMATRIX matWorld;
822- pd3dDevice->GetTransform( D3DTS_WORLD, &matWorld );
823- *x = matWorld._41;
824- *y = matWorld._42;
825- *z = matWorld._43;
826-}
827-
828-//! @brief フォグを設定
829-//! @param skynumber 空の番号
830-void D3DGraphics::SetFog(int skynumber)
831-{
832- D3DCOLOR skycolor;
833-
834- //空の番号により色を決定
835- switch(skynumber){
836- case 1: skycolor = D3DCOLOR_RGBA(64, 64+16, 64, 0); break;
837- case 2: skycolor = D3DCOLOR_RGBA(16, 16, 16, 0); break;
838- case 3: skycolor = D3DCOLOR_RGBA(0, 16, 32, 0); break;
839- case 4: skycolor = D3DCOLOR_RGBA(32, 16, 16, 0); break;
840- case 5: skycolor = D3DCOLOR_RGBA(64, 32, 32, 0); break;
841- default: skycolor = D3DCOLOR_RGBA(0, 0, 0, 0); break;
842- }
843-
844- //フォグを設定
845- pd3dDevice->SetRenderState(D3DRS_FOGCOLOR, skycolor);
846-}
847-
848-//! @brief カメラ(視点)を設定
849-//! @param camera_x カメラのX座標
850-//! @param camera_y カメラのY座標
851-//! @param camera_z カメラのZ座標
852-//! @param camera_rx カメラの横軸角度
853-//! @param camera_ry カメラの縦軸角度
854-//! @param viewangle 視野角
855-void D3DGraphics::SetCamera(float camera_x, float camera_y, float camera_z, float camera_rx, float camera_ry, float viewangle)
856-{
857- float vUpVecF;
858- D3DXMATRIX matWorld;
859- D3DXMATRIXA16 matView;
860-
861- //camera_ryを -PI〜PI の間に正規化
862- for(; camera_ry>D3DX_PI; camera_ry -= D3DX_PI*2){}
863- for(; camera_ry<D3DX_PI*-1; camera_ry += D3DX_PI*2){}
864-
865- //カメラの向きを決定
866- if( fabs(camera_ry) < D3DX_PI/2 ){
867- vUpVecF = 1.0f;
868- }
869- else{
870- vUpVecF = -1.0f;
871- }
872-
873- D3DXMatrixIdentity(&matWorld);
874- pd3dDevice->SetTransform(D3DTS_WORLD, &matWorld);
875-
876- //カメラ座標
877- D3DXVECTOR3 vEyePt( camera_x, camera_y, camera_z );
878- D3DXVECTOR3 vLookatPt( cos(camera_rx)*cos(camera_ry) + camera_x, sin(camera_ry) + camera_y, sin(camera_rx)*cos(camera_ry) + camera_z );
879- D3DXVECTOR3 vUpVec( 0.0f, vUpVecF, 0.0f );
880- D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
881- pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
882-
883- //カメラ設定(射影変換行列)viewangle
884- D3DXMATRIXA16 matProj;
885- D3DXMatrixPerspectiveFovLH( &matProj, viewangle, aspect, CLIPPINGPLANE_NEAR, CLIPPINGPLANE_FAR);
886- pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProj);
887-}
888-
889-//! @brief マップデータを取り込む
890-//! @param in_blockdata ブロックデータ
891-//! @param directory ブロックデータが存在するディレクトリ
892-void D3DGraphics::LoadMapdata(BlockDataInterface* in_blockdata, char *directory)
893-{
894- //ブロックデータが指定されていなければ、処理しない。
895- if( in_blockdata == NULL ){ return; }
896-
897- char fname[MAX_PATH];
898- char fnamefull[MAX_PATH];
899- //int bs;
900- struct blockdata data;
901- int vID[4];
902- int uvID[4];
903-
904- //クラスを設定
905- blockdata = in_blockdata;
906-
907- //テクスチャ読み込み
908- for(int i=0; i<TOTAL_BLOCKTEXTURE; i++){
909- //テクスチャ名を取得
910- blockdata->GetTexture(fname, i);
911-
912- if( strcmp(fname, "") == 0 ){ //指定されていなければ、処理しない
913- mapTextureID[i] = -1;
914- }
915- else{
916- //「ディレクトリ+ファイル名」を生成し、読み込む
917- strcpy(fnamefull, directory);
918- strcat(fnamefull, fname);
919- mapTextureID[i] = LoadTexture(fnamefull, false, false);
920- }
921- }
922-
923-#ifdef ENABLE_DEBUGLOG
924- //ログに出力
925- OutputLog.WriteLog(LOG_LOAD, "マップ", "(頂点データ)");
926-#endif
927-
928-#ifdef BLOCKDATA_GPUMEMORY
929- VERTEXTXTA* pVertices;
930-
931- //ブロック数を取得
932- bs = blockdata->GetTotaldatas();
933-
934- //ブロック数分のバッファーを作成
935- pd3dDevice->CreateVertexBuffer(bs*6*4*sizeof(VERTEXTXTA),0,D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1,D3DPOOL_DEFAULT,&g_pVB,NULL);
936-
937- for(int i=0; i<bs; i++){
938- //データを取得
939- blockdata->Getdata(&data, i);
940-
941- for(int j=0; j<6; j++){
942- //面の頂点データの関連付けを取得
943- blockdataface(j, &vID[0], &uvID[0]);
944-
945- //GPUをロック(1面分)
946- g_pVB->Lock((i*6+j)*4*sizeof(VERTEXTXTA), 4*sizeof(VERTEXTXTA), (void**)&pVertices, 0);
947-
948- //頂点座標・UV座標・色を設定
949- pVertices[0].position = D3DXVECTOR3( data.x[ vID[1] ], data.y[ vID[1] ], data.z[ vID[1] ] );
950- pVertices[0].tu = data.material[j].u[ uvID[1] ];
951- pVertices[0].tv = data.material[j].v[ uvID[1] ];
952- pVertices[1].position = D3DXVECTOR3( data.x[ vID[2] ], data.y[ vID[2] ], data.z[ vID[2] ] );
953- pVertices[1].tu = data.material[j].u[ uvID[2] ];
954- pVertices[1].tv = data.material[j].v[ uvID[2] ];
955- pVertices[2].position = D3DXVECTOR3( data.x[ vID[0] ], data.y[ vID[0] ], data.z[ vID[0] ] );
956- pVertices[2].tu = data.material[j].u[ uvID[0] ];
957- pVertices[2].tv = data.material[j].v[ uvID[0] ];
958- pVertices[3].position = D3DXVECTOR3( data.x[ vID[3] ], data.y[ vID[3] ], data.z[ vID[3] ] );
959- pVertices[3].tu = data.material[j].u[ uvID[3] ];
960- pVertices[3].tv = data.material[j].v[ uvID[3] ];
961- for(int k=0; k<4; k++){
962- pVertices[k].color = D3DCOLOR_COLORVALUE(data.material[j].shadow, data.material[j].shadow, data.material[j].shadow, 1.0f);
963- }
964-
965- //GPUのロックを解除
966- g_pVB->Unlock();
967- }
968- }
969-#else
970- //ブロック数を取得
971- bs = blockdata->GetTotaldatas();
972-
973- for(int i=0; i<bs; i++){
974- //データを取得
975- blockdata->Getdata(&data, i);
976-
977- for(int j=0; j<6; j++){
978- //面の頂点データの関連付けを取得
979- blockdataface(j, vID, uvID);
980-
981- //頂点座標・UV座標・色を設定
982- g_pVertices[i][j][0].position = D3DXVECTOR3( data.x[ vID[1] ], data.y[ vID[1] ], data.z[ vID[1] ] );
983- g_pVertices[i][j][0].tu = data.material[j].u[ uvID[1] ];
984- g_pVertices[i][j][0].tv = data.material[j].v[ uvID[1] ];
985- g_pVertices[i][j][1].position = D3DXVECTOR3( data.x[ vID[2] ], data.y[ vID[2] ], data.z[ vID[2] ] );
986- g_pVertices[i][j][1].tu = data.material[j].u[ uvID[2] ];
987- g_pVertices[i][j][1].tv = data.material[j].v[ uvID[2] ];
988- g_pVertices[i][j][2].position = D3DXVECTOR3( data.x[ vID[0] ], data.y[ vID[0] ], data.z[ vID[0] ] );
989- g_pVertices[i][j][2].tu = data.material[j].u[ uvID[0] ];
990- g_pVertices[i][j][2].tv = data.material[j].v[ uvID[0] ];
991- g_pVertices[i][j][3].position = D3DXVECTOR3( data.x[ vID[3] ], data.y[ vID[3] ], data.z[ vID[3] ] );
992- g_pVertices[i][j][3].tu = data.material[j].u[ uvID[3] ];
993- g_pVertices[i][j][3].tv = data.material[j].v[ uvID[3] ];
994- for(int k=0; k<4; k++){
995- g_pVertices[i][j][k].color = D3DCOLOR_COLORVALUE(data.material[j].shadow, data.material[j].shadow, data.material[j].shadow, 1.0f);
996- }
997- }
998- }
999-#endif
1000-
1001-#ifdef ENABLE_DEBUGLOG
1002- //ログに出力
1003- OutputLog.WriteLog(LOG_COMPLETE, "", "");
1004-#endif
1005-}
1006-
1007-//! @brief マップデータを描画
1008-//! @param wireframe ワイヤーフレーム表示
1009-void D3DGraphics::DrawMapdata(bool wireframe)
1010-{
1011- //ブロックデータが読み込まれていなければ、処理しない。
1012- if( blockdata == NULL ){ return; }
1013-
1014- struct blockdata data;
1015- int textureID;
1016-
1017- if( wireframe == true ){
1018- //ワイヤーフレーム表示
1019- for(int i=0; i<bs; i++){
1020- blockdata->Getdata(&data, i);
1021- Drawline(data.x[0], data.y[0], data.z[0], data.x[1], data.y[1], data.z[1]);
1022- Drawline(data.x[1], data.y[1], data.z[1], data.x[2], data.y[2], data.z[2]);
1023- Drawline(data.x[2], data.y[2], data.z[2], data.x[3], data.y[3], data.z[3]);
1024- Drawline(data.x[3], data.y[3], data.z[3], data.x[0], data.y[0], data.z[0]);
1025- Drawline(data.x[4], data.y[4], data.z[4], data.x[5], data.y[5], data.z[5]);
1026- Drawline(data.x[5], data.y[5], data.z[5], data.x[6], data.y[6], data.z[6]);
1027- Drawline(data.x[6], data.y[6], data.z[6], data.x[7], data.y[7], data.z[7]);
1028- Drawline(data.x[7], data.y[7], data.z[7], data.x[4], data.y[4], data.z[4]);
1029- Drawline(data.x[0], data.y[0], data.z[0], data.x[4], data.y[4], data.z[4]);
1030- Drawline(data.x[1], data.y[1], data.z[1], data.x[5], data.y[5], data.z[5]);
1031- Drawline(data.x[2], data.y[2], data.z[2], data.x[6], data.y[6], data.z[6]);
1032- Drawline(data.x[3], data.y[3], data.z[3], data.x[7], data.y[7], data.z[7]);
1033- }
1034- return;
1035- }
1036-
1037- //深度バッファ比較関数を設定
1038- //pd3dDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESS);
1039-
1040-
1041-#ifdef BLOCKDATA_GPUMEMORY
1042- //データ設定
1043- pd3dDevice->SetStreamSource(0,g_pVB,0,sizeof(VERTEXTXTA));
1044-
1045- for(textureID=0; textureID<TOTAL_BLOCKTEXTURE; textureID++){
1046- //テクスチャが正常に読み込めていなければ設定
1047- if( mapTextureID[textureID] == -1 ){
1048- pd3dDevice->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE);
1049- pd3dDevice->SetTexture(0, NULL);
1050- }
1051- else if( ptextures[ mapTextureID[textureID] ] == NULL ){
1052- pd3dDevice->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE);
1053- pd3dDevice->SetTexture(0, NULL);
1054- }
1055- else{
1056- pd3dDevice->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1);
1057- pd3dDevice->SetTexture(0, ptextures[mapTextureID[textureID]] );
1058- }
1059-
1060- for(int i=0; i<bs; i++){
1061- //データ取得
1062- blockdata->Getdata(&data, i);
1063-
1064- for(int j=0; j<6; j++){
1065- //テクスチャ認識番号を取得
1066- int ID = data.material[j].textureID;
1067-
1068- if( textureID == ID ){
1069- //面を描画
1070- pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, (i*6+j)*4, 2);
1071- }
1072- }
1073- }
1074- }
1075-#else
1076- //データを設定
1077- pd3dDevice->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1);
1078-
1079- for(textureID=0; textureID<TOTAL_BLOCKTEXTURE; textureID++){
1080- //テクスチャが正常に読み込めていなければ設定
1081- if( mapTextureID[textureID] == -1 ){
1082- pd3dDevice->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE);
1083- pd3dDevice->SetTexture(0, NULL);
1084- }
1085- else if( ptextures[ mapTextureID[textureID] ] == NULL ){
1086- pd3dDevice->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE);
1087- pd3dDevice->SetTexture(0, NULL);
1088- }
1089- else{
1090- pd3dDevice->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1);
1091- pd3dDevice->SetTexture(0, ptextures[mapTextureID[textureID]] );
1092- }
1093-
1094- for(int i=0; i<bs; i++){
1095- //データ取得
1096- blockdata->Getdata(&data, i);
1097-
1098- for(int j=0; j<6; j++){
1099- //テクスチャ認識番号を取得
1100- int ID = data.material[j].textureID;
1101-
1102- if( textureID == ID ){
1103- //面を描画
1104- pd3dDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, g_pVertices[i][j], sizeof(VERTEXTXTA));
1105- }
1106- }
1107- }
1108- }
1109-#endif
1110-
1111- //深度バッファ比較関数を元に戻す
1112- //pd3dDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESSEQUAL);
1113-}
1114-
1115-//! @brief マップテクスチャを取得
1116-//! @param id テクスチャ番号
1117-//! @return テクスチャ認識番号(失敗:-1)
1118-int D3DGraphics::GetMapTextureID(int id)
1119-{
1120- if( (id < 0)||((TOTAL_BLOCKTEXTURE -1) < id ) ){ return -1; }
1121- return mapTextureID[id];
1122-}
1123-
1124-//! @brief マップデータを解放
1125-void D3DGraphics::CleanupMapdata()
1126-{
1127- //テクスチャを開放
1128- for(int i=0; i<TOTAL_BLOCKTEXTURE; i++){
1129- CleanupTexture(mapTextureID[i]);
1130- }
1131-
1132-#ifdef BLOCKDATA_GPUMEMORY
1133- //頂点データ解放
1134- if( g_pVB != NULL ){
1135- g_pVB->Release();
1136- g_pVB = NULL;
1137- }
1138-#endif
1139- bs = 0;
1140-
1141- blockdata = NULL;
1142-
1143-#ifdef ENABLE_DEBUGLOG
1144- //ログに出力
1145- OutputLog.WriteLog(LOG_CLEANUP, "マップ", "(頂点データ)");
1146-#endif
1147-}
1148-
1149-//! @brief モデルファイルを描画
1150-//! @param id_model モデル認識番号
1151-//! @param id_texture テクスチャ認識番号
1152-void D3DGraphics::RenderModel(int id_model, int id_texture)
1153-{
1154- //無効な引数が設定されていれば失敗
1155- if( id_model == -1 ){ return; }
1156- //if( id_texture == -1 ){ return; }
1157-
1158- //指定したモデルが初期化されていなければ失敗
1159- if( pmesh[id_model] == NULL) return;
1160-
1161- //描画
1162- for(int i=0; i<(signed)nummaterials[id_model]; i=i+1){
1163- pd3dDevice->SetMaterial( &pmaterials[id_model][i] );
1164- if( id_texture == -1 ){
1165- pd3dDevice->SetTexture(0, NULL);
1166- }
1167- else if( ptextures[id_texture] == NULL ){
1168- pd3dDevice->SetTexture(0, NULL);
1169- }
1170- else{
1171- pd3dDevice->SetTexture( 0, ptextures[id_texture] );
1172- }
1173- pmesh[id_model]->DrawSubset(i);
1174- }
1175-}
1176-
1177-//! @brief 板を描画
1178-//! @param id_texture テクスチャ認識番号
1179-//! @param alpha 透明度 (0.0〜1.0 0.0:完全透明)
1180-void D3DGraphics::RenderBoard(int id_texture, float alpha)
1181-{
1182- //テクスチャが設定されていなければ、処理しない。
1183- if( id_texture == -1 ){ return; }
1184-
1185- VERTEXTXTA BoardVertices[4];
1186-
1187- //頂点座標・UV座標・色/透明度を設定
1188- BoardVertices[0].position = D3DXVECTOR3(0.0f, 0.5f, -0.5f);
1189- BoardVertices[0].tu = 1.0f;
1190- BoardVertices[0].tv = 0.0f;
1191- BoardVertices[1].position = D3DXVECTOR3(0.0f, -0.5f, -0.5f);
1192- BoardVertices[1].tu = 1.0f;
1193- BoardVertices[1].tv = 1.0f;
1194- BoardVertices[2].position = D3DXVECTOR3(0.0f, 0.5f, 0.5f);
1195- BoardVertices[2].tu = 0.0f;
1196- BoardVertices[2].tv = 0.0f;
1197- BoardVertices[3].position = D3DXVECTOR3(0.0f, -0.5f, 0.5f);
1198- BoardVertices[3].tu = 0.0f;
1199- BoardVertices[3].tv = 1.0f;
1200- for(int i=0; i<4; i++){
1201- BoardVertices[i].color = D3DCOLOR_COLORVALUE(1.0f, 1.0f, 1.0f, alpha);
1202- }
1203-
1204- //アルファブレンドを設定
1205- pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
1206- pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
1207- pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
1208-
1209- //テクスチャとデータ形式を設定し描画
1210- pd3dDevice->SetTexture(0, ptextures[id_texture]);
1211- pd3dDevice->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1);
1212- pd3dDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, BoardVertices, sizeof(VERTEXTXTA));
1213-
1214- //アルファブレンドを元に戻す
1215- pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1216- pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
1217-}
1218-
1219-//! @brief 画面の明るさを設定
1220-//! @param Width 幅
1221-//! @param Height 高さ
1222-//! @param Brightness 画面の明るさ (0 で不変、1 以上で明るさの度合い)
1223-void D3DGraphics::ScreenBrightness(int Width, int Height, int Brightness)
1224-{
1225- //明るさ不変なら処理しない(軽量化)
1226- if( Brightness == 0 ){ return; }
1227-
1228- //透明度を設定し、描画
1229- float alpha = 0.02f * Brightness;
1230- Draw2DBox(0, 0, Width, Height, D3DCOLOR_COLORVALUE(1.0f,1.0f,1.0f,alpha));
1231-}
1232-
1233-//! @brief 【デバック用】中心線描画
1234-void D3DGraphics::Centerline()
1235-{
1236- ResetWorldTransform();
1237- Drawline(100.0f, 0.0f, 0.0f, -100.0f, 0.0f, 0.0f);
1238- Drawline(0.0f, 100.0f, 0.0f, 0.0f, -100.0f, 0.0f);
1239- Drawline(0.0f, 0.0f, 100.0f, 0.0f, 0.0f, -100.0f);
1240-}
1241-
1242-//! @brief 【デバック用】緑線描画
1243-void D3DGraphics::Drawline(float x1, float y1, float z1, float x2, float y2, float z2)
1244-{
1245- VERTEXTXTA mv[2];
1246-
1247- mv[0].position = D3DXVECTOR3(x1, y1, z1);
1248- mv[1].position = D3DXVECTOR3(x2, y2, z2);
1249- for(int i=0; i<2; i++){
1250- mv[i].color = 0xFF00FF00;
1251- mv[i].tu = 0.0f;
1252- mv[i].tv = 0.0f;
1253- }
1254-
1255- pd3dDevice->SetTexture(0, NULL);
1256- pd3dDevice->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1);
1257- pd3dDevice->DrawPrimitiveUP(D3DPT_LINELIST, 1, mv, sizeof(VERTEXTXTA));
1258-}
1259-
1260-//! @brief 2D システムフォントによるテキスト描画を開始
1261-//! @attention DirectXの ID3DXSprite を初期化しています。
1262-void D3DGraphics::Start2DMSFontTextRender()
1263-{
1264- ptextsprite->Begin(D3DXSPRITE_ALPHABLEND);
1265-}
1266-
1267-//! @brief 文字を描画(システムフォント使用)
1268-//! @param x x座標
1269-//! @param y y座標
1270-//! @param str 文字列 (改行コード:可)
1271-//! @param color 色
1272-//! @warning <b>描画は非常に低速です。</b>画面内で何度も呼び出すとパフォーマンスに影響します。
1273-//! @warning「改行コードを活用し一度に描画する」「日本語が必要ない文字はテクスチャフォントを活用する」などの対応を講じてください。
1274-//! @attention DirectXの ID3DXSprite を使用し、システムフォントで描画ています。
1275-//! @attention フォントの種類やサイズは固定です。 文字を二重に重ねて立体感を出さないと見にくくなります。
1276-void D3DGraphics::Draw2DMSFontText(int x, int y, char *str, int color)
1277-{
1278- //if( ptextsprite == NULL ){ return; }
1279-
1280- //テキストスプライト初期化
1281- Start2DMSFontTextRender();
1282-
1283- //基準座標を設定
1284- D3DXMATRIX matWorld;
1285- D3DXMatrixIdentity(&matWorld);
1286- ptextsprite->SetTransform(&matWorld);
1287-
1288- //文字をを描画
1289- RECT rc = {x, y, 0, 0};
1290- pxmsfont->DrawText(ptextsprite, str, -1, &rc, DT_NOCLIP, color);
1291-
1292- //テキストスプライト解放
1293- End2DMSFontTextRender();
1294-}
1295-
1296-//! @brief 文字を中央揃えで描画(システムフォント使用)
1297-//! @param x x座標
1298-//! @param y y座標
1299-//! @param w 横の大きさ
1300-//! @param h 縦の大きさ
1301-//! @param str 文字列 (改行コード:可)
1302-//! @param color 色
1303-//! @warning <b>描画は非常に低速です。</b>画面内で何度も呼び出すとパフォーマンスに影響します。
1304-//! @warning「改行コードを活用し一度に描画する」「日本語が必要ない文字はテクスチャフォントを活用する」などの対応を講じてください。
1305-//! @attention DirectXの ID3DXSprite を使用し、システムフォントで描画ています。
1306-//! @attention フォントの種類やサイズは固定です。 文字を二重に重ねて立体感を出さないと見にくくなります。
1307-void D3DGraphics::Draw2DMSFontTextCenter(int x, int y, int w, int h, char *str, int color)
1308-{
1309- //if( ptextsprite == NULL ){ return; }
1310-
1311- //テキストスプライト初期化
1312- Start2DMSFontTextRender();
1313-
1314- //基準座標を設定
1315- D3DXMATRIX matWorld;
1316- D3DXMatrixIdentity(&matWorld);
1317- ptextsprite->SetTransform(&matWorld);
1318-
1319- //文字をを描画
1320- RECT rc = {x, y, x+w, y+h};
1321- pxmsfont->DrawText(ptextsprite, str, -1, &rc, DT_CENTER, color);
1322-
1323- //テキストスプライト解放
1324- End2DMSFontTextRender();
1325-}
1326-
1327-//! @brief 2D システムフォントによるテキスト描画を終了
1328-//! @attention DirectXの ID3DXSprite を解放しています。
1329-void D3DGraphics::End2DMSFontTextRender()
1330-{
1331- ptextsprite->End();
1332-}
1333-
1334-//! @brief 2D描画用設定
1335-void D3DGraphics::Start2DRender()
1336-{
1337- pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
1338- pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
1339- pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
1340-
1341- //深度バッファ比較関数を設定
1342- pd3dDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1343-}
1344-
1345-//! @brief 文字を描画(テクスチャフォント使用)
1346-//! @param x x座標
1347-//! @param y y座標
1348-//! @param str 文字列 (改行コード:<b>不可</b>)
1349-//! @param color 色
1350-//! @param fontwidth 一文字の幅
1351-//! @param fontheight 一文字の高さ
1352-//! @attention 文字を二重に重ねて立体感を出さないと見にくくなります。
1353-void D3DGraphics::Draw2DTextureFontText(int x, int y, char *str, int color, int fontwidth, int fontheight)
1354-{
1355- //テクスチャフォントの取得に失敗していれば、処理しない
1356- if( TextureFont == -1 ){ return; }
1357-
1358- //2D描画用設定を適用
1359- Start2DRender();
1360-
1361- int w;
1362- float font_u, font_v;
1363- float t_u, t_v;
1364- TLVERTX pBoxVertices[4];
1365-
1366- //1文字のUV座標を計算
1367- font_u = 1.0f / 16;
1368- font_v = 1.0f / 16;
1369-
1370- //ワールド座標を原点に戻す
1371- ResetWorldTransform();
1372-
1373- //テクスチャをフォントテクスチャに設定
1374- pd3dDevice->SetTexture( 0, ptextures[TextureFont] );
1375-
1376- //データ形式を設定
1377- pd3dDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1);
1378-
1379- // 与えられた文字数分ループ
1380- for(int i=0; i<(int)strlen(str); i++){
1381- //UV座標を計算
1382- w = str[i];
1383- if( w < 0 ){ w += 256; }
1384- t_u = (w % 16) * font_u;
1385- t_v = (w / 16) * font_v;
1386-
1387- //頂点座標・UV座標・色を設定
1388- pBoxVertices[0].x = (float)x + i*fontwidth;
1389- pBoxVertices[0].y = (float)y;
1390- pBoxVertices[0].tu = t_u;
1391- pBoxVertices[0].tv = t_v;
1392- pBoxVertices[1].x = (float)x + fontwidth + i*fontwidth;
1393- pBoxVertices[1].y = (float)y;
1394- pBoxVertices[1].tu = t_u + font_u;
1395- pBoxVertices[1].tv = t_v;
1396- pBoxVertices[2].x = (float)x + i*fontwidth;
1397- pBoxVertices[2].y = (float)y + fontheight;
1398- pBoxVertices[2].tu = t_u;
1399- pBoxVertices[2].tv = t_v + font_v;
1400- pBoxVertices[3].x = (float)x + fontwidth + i*fontwidth;
1401- pBoxVertices[3].y = (float)y + fontheight;
1402- pBoxVertices[3].tu = t_u + font_u;
1403- pBoxVertices[3].tv = t_v + font_v;
1404- for(int j=0; j<4; j++){
1405- pBoxVertices[j].z = 0.0f;
1406- pBoxVertices[j].rhw = 1.0f;
1407- pBoxVertices[j].color = color;
1408- }
1409-
1410- //描画
1411- pd3dDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, pBoxVertices, sizeof(TLVERTX));
1412- }
1413-
1414- //2D描画用設定を解除
1415- End2DRender();
1416-}
1417-
1418-//! @brief 線を描画
1419-//! @param x1 始点の x座標
1420-//! @param y1 始点の y座標
1421-//! @param x2 終点の x座標
1422-//! @param y2 終点の y座標
1423-//! @param color 色
1424-void D3DGraphics::Draw2DLine(int x1, int y1, int x2, int y2, int color)
1425-{
1426- TLVERTX pLineVertices[2];
1427-
1428- //2D描画用設定を適用
1429- Start2DRender();
1430-
1431- //ワールド座標を原点に戻す
1432- ResetWorldTransform();
1433-
1434- //頂点座標と色などを設定
1435- pLineVertices[0].x = (float)x1;
1436- pLineVertices[0].y = (float)y1;
1437- pLineVertices[1].x = (float)x2;
1438- pLineVertices[1].y = (float)y2;
1439- for(int i=0; i<2; i++){
1440- pLineVertices[i].z = 0.0f;
1441- pLineVertices[i].rhw = 1.0f;
1442- pLineVertices[i].color = color;
1443- pLineVertices[i].tu = 0.0f;
1444- pLineVertices[i].tv = 0.0f;
1445- }
1446-
1447- pd3dDevice->SetTexture(0, NULL);
1448-
1449- //データ形式を設定し、描画。
1450- pd3dDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1);
1451- pd3dDevice->DrawPrimitiveUP(D3DPT_LINELIST, 1, pLineVertices, sizeof(TLVERTX));
1452-
1453- //2D描画用設定を解除
1454- End2DRender();
1455-}
1456-
1457-//! @brief 円(16角形)を描画
1458-//! @param x 中心の x座標
1459-//! @param y 中心の y座標
1460-//! @param r 半径
1461-//! @param color 色
1462-void D3DGraphics::Draw2DCycle(int x, int y, int r, int color)
1463-{
1464- TLVERTX pLineVertices[16+1];
1465-
1466- //2D描画用設定を適用
1467- Start2DRender();
1468-
1469- //ワールド座標を原点に戻す
1470- ResetWorldTransform();
1471-
1472- //頂点座標と色などを設定
1473- for(int i=0; i<16+1; i++){
1474- pLineVertices[i].x = (float)x + cos(DegreeToRadian((360.0f/16.0f)) * i) * r;
1475- pLineVertices[i].y = (float)y + sin(DegreeToRadian((360.0f/16.0f)) * i) * r;
1476-
1477- pLineVertices[i].z = 0.0f;
1478- pLineVertices[i].rhw = 1.0f;
1479- pLineVertices[i].color = color;
1480- pLineVertices[i].tu = 0.0f;
1481- pLineVertices[i].tv = 0.0f;
1482- }
1483-
1484- pd3dDevice->SetTexture(0, NULL);
1485-
1486- //データ形式を設定し、描画。
1487- pd3dDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1);
1488- pd3dDevice->DrawPrimitiveUP(D3DPT_LINESTRIP, 16, pLineVertices, sizeof(TLVERTX));
1489-
1490- //2D描画用設定を解除
1491- End2DRender();
1492-}
1493-
1494-//! @brief 四角形を描画
1495-//! @param x1 左上の x座標
1496-//! @param y1 左上の y座標
1497-//! @param x2 右下の x座標
1498-//! @param y2 右下の y座標
1499-//! @param color 色
1500-void D3DGraphics::Draw2DBox(int x1, int y1, int x2, int y2, int color)
1501-{
1502- TLVERTX pBoxVertices[4];
1503-
1504- //2D描画用設定を適用
1505- Start2DRender();
1506-
1507- //ワールド座標を原点に戻す
1508- ResetWorldTransform();
1509-
1510- //頂点座標と色などを設定
1511- pBoxVertices[0].x = (float)x1;
1512- pBoxVertices[0].y = (float)y1;
1513- pBoxVertices[1].x = (float)x2;
1514- pBoxVertices[1].y = (float)y1;
1515- pBoxVertices[2].x = (float)x1;
1516- pBoxVertices[2].y = (float)y2;
1517- pBoxVertices[3].x = (float)x2;
1518- pBoxVertices[3].y = (float)y2;
1519- for(int i=0; i<4; i++){
1520- pBoxVertices[i].z = 0.0f;
1521- pBoxVertices[i].rhw = 1.0f;
1522- pBoxVertices[i].color = color;
1523- pBoxVertices[i].tu = 0.0f;
1524- pBoxVertices[i].tv = 0.0f;
1525- }
1526-
1527- pd3dDevice->SetTexture(0, NULL);
1528-
1529- //データ形式を設定し、描画。
1530- pd3dDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1);
1531- pd3dDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, pBoxVertices, sizeof(TLVERTX));
1532-
1533- //2D描画用設定を解除
1534- End2DRender();
1535-}
1536-
1537-//! @brief 画像を描画
1538-//! @param x x座標
1539-//! @param y y座標
1540-//! @param id テクスチャ認識番号
1541-//! @param width 幅
1542-//! @param height 高さ
1543-//! @param alpha 透明度(0.0〜1.0)
1544-void D3DGraphics::Draw2DTexture(int x, int y, int id, int width, int height, float alpha)
1545-{
1546- //無効なテクスチャ番号を指定されていれば処理しない
1547- if( id == -1 ){ return; }
1548-
1549- TLVERTX pBoxVertices[4];
1550-
1551- //2D描画用設定を適用
1552- Start2DRender();
1553-
1554- //ワールド座標を原点に戻す
1555- ResetWorldTransform();
1556-
1557- //頂点座標・UV座標・色を設定
1558- pBoxVertices[0].x = (float)x;
1559- pBoxVertices[0].y = (float)y;
1560- pBoxVertices[0].tu = 0.0f;
1561- pBoxVertices[0].tv = 0.0f;
1562- pBoxVertices[1].x = (float)x + width;
1563- pBoxVertices[1].y = (float)y;
1564- pBoxVertices[1].tu = 1.0f;
1565- pBoxVertices[1].tv = 0.0f;
1566- pBoxVertices[2].x = (float)x;
1567- pBoxVertices[2].y = (float)y + height;
1568- pBoxVertices[2].tu = 0.0f;
1569- pBoxVertices[2].tv = 1.0f;
1570- pBoxVertices[3].x = (float)x + width;
1571- pBoxVertices[3].y = (float)y + height;
1572- pBoxVertices[3].tu = 1.0f;
1573- pBoxVertices[3].tv = 1.0f;
1574- for(int i=0; i<4; i++){
1575- pBoxVertices[i].z = 0.0f;
1576- pBoxVertices[i].rhw = 1.0f;
1577- pBoxVertices[i].color = D3DCOLOR_COLORVALUE(1.0f,1.0f,1.0f,alpha);
1578- }
1579-
1580- //テクスチャとデータ形式を設定し、描画
1581- pd3dDevice->SetTexture( 0, ptextures[id] );
1582- pd3dDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1);
1583- pd3dDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, pBoxVertices, sizeof(TLVERTX));
1584-
1585- //2D描画用設定を解除
1586- End2DRender();
1587-}
1588-
1589-//! @brief 2D描画用設定を解除
1590-void D3DGraphics::End2DRender()
1591-{
1592- pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1593- pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
1594-
1595- //深度バッファ比較関数を元に戻す
1596- pd3dDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESSEQUAL);
1597-}
1598-
1599-//! @brief 画面のスクリーンショットを保存
1600-//! @param filename ファイル名
1601-//! @return 成功:true 失敗:false
1602-bool D3DGraphics::SaveScreenShot(char* filename)
1603-{
1604- LPDIRECT3DSURFACE9 pSurface = NULL;
1605- HRESULT hr;
1606-
1607- //サーフェースを作成し、画面を取得
1608- pd3dDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &pSurface);
1609-
1610- //サーフェイスを画像に出力
1611- hr = D3DXSaveSurfaceToFile(filename, D3DXIFF_BMP, pSurface, NULL, NULL);
1612-
1613- //解放
1614- pSurface->Release();
1615-
1616- if( hr == D3D_OK ){
1617- return true;
1618- }
1619- return false;
1620-}
1621-
1622-//! @brief カラーコードを取得
1623-//! @param red 赤(0.0f〜1.0f)
1624-//! @param green 緑(0.0f〜1.0f)
1625-//! @param blue 青(0.0f〜1.0f)
1626-//! @param alpha 透明度(0.0f〜1.0f)
1627-//! @return カラーコード
1628-int D3DGraphics::GetColorCode(float red, float green, float blue, float alpha)
1629-{
1630- return D3DCOLOR_COLORVALUE(red, green, blue, alpha);
1631-}
1632-
1633-#else //GRAPHICS_OPENGL
1634-
1635-//! @brief コンストラクタ
1636-D3DGraphics::D3DGraphics()
1637-{
1638- hGLRC = NULL;
1639- width = 0;
1640- height = 0;
1641- SystemFont = NULL;
1642- now_SystemFontUStr = new WCHAR [1];
1643- now_SystemFontUStr[0] = NULL;
1644- SystemFontListIdx = 0;
1645- SystemFontListIdxSize = 0;
1646- now_textureid = -1;
1647-
1648- camera_x = 0.0f;
1649- camera_y = 0.0f;
1650- camera_z = 0.0f;
1651- camera_rx = 0.0f;
1652- camera_ry = 0.0f;
1653- viewangle = 0.0f;
1654-
1655- blockdata = NULL;
1656- for(int i=0; i<TOTAL_BLOCKTEXTURE; i++){
1657- mapTextureID[i] = -1;
1658- }
1659-
1660- TextureFont = -1;
1661-}
1662-
1663-//! @brief ディストラクタ
1664-D3DGraphics::~D3DGraphics()
1665-{
1666- for(int i=0; i<MAX_MODEL; i++){
1667- CleanupModel(i);
1668- }
1669- for(int i=0; i<MAX_TEXTURE; i++){
1670- CleanupTexture(i);
1671- }
1672-
1673- if( SystemFont != NULL ){
1674- DeleteObject(SystemFont);
1675- }
1676- if( now_SystemFontUStr != NULL ){
1677- delete [] now_SystemFontUStr;
1678- }
1679- if( SystemFontListIdx != 0 ){
1680- glDeleteLists(SystemFontListIdx, SystemFontListIdxSize);
1681- }
1682-
1683- if( hGLRC != NULL ){ wglDeleteContext(hGLRC); }
1684-
1685-#ifdef ENABLE_DEBUGLOG
1686- //ログに出力
1687- OutputLog.WriteLog(LOG_CLEANUP, "グラフィック", "OpenGL");
1688-#endif
1689-
1690- //libjpeg解放
1691- jpeg_destroy_decompress(&cinfo);
1692-}
1693-
1694-//! @brief 初期化@n
1695-//! (OpenGL 1.1)
1696-//! @param WindowCtrl WindowControlクラスのポインタ
1697-//! @param TextureFontFilename 使用するテクスチャフォントのファイル名
1698-//! @param fullscreen false:ウィンドウ表示 true:フルスクリーン用表示
1699-//! @return 成功:0 失敗:1
1700-int D3DGraphics::InitD3D(WindowControl *WindowCtrl, char *TextureFontFilename, bool fullscreen)
1701-{
1702-#ifdef ENABLE_DEBUGLOG
1703- //ログに出力
1704- OutputLog.WriteLog(LOG_INIT, "グラフィック", "OpenGL");
1705-#endif
1706-
1707- hWnd = WindowCtrl->GethWnd();
1708-
1709- RECT prc;
1710- GetClientRect(hWnd, &prc);
1711- width = prc.right;
1712- height = prc.bottom;
1713-
1714- //フルスクリーン化
1715- if( fullscreen == true ){
1716- DEVMODE devmode;
1717- ZeroMemory(&devmode, sizeof(devmode));
1718- devmode.dmSize = sizeof(devmode);
1719- devmode.dmPelsWidth = width;
1720- devmode.dmPelsHeight = height;
1721- devmode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
1722-
1723- if( ChangeDisplaySettings(&devmode, CDS_TEST) != DISP_CHANGE_SUCCESSFUL ){
1724- return 1;
1725- }
1726- ChangeDisplaySettings(&devmode, CDS_FULLSCREEN);
1727- }
1728-
1729-
1730-
1731- HDC hDC;
1732- int pfdID;
1733- BOOL bResult;
1734-
1735- //ピクセルフォーマット
1736- static PIXELFORMATDESCRIPTOR pfd = {
1737- sizeof (PIXELFORMATDESCRIPTOR),
1738- 1,
1739- PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL,
1740- PFD_TYPE_RGBA,
1741- 24,
1742- 0, 0, 0,
1743- 0, 0, 0,
1744- 0, 0,
1745- 0, 0, 0, 0, 0,
1746- 32,
1747- 0,
1748- 0,
1749- PFD_MAIN_PLANE,
1750- 0,
1751- 0,
1752- 0,
1753- 0
1754- };
1755-
1756- //デバイスコンテキスト取得
1757- hDC = GetDC(hWnd);
1758-
1759- //ピクセルフォーマットを取得
1760- pfdID = ChoosePixelFormat(hDC, &pfd);
1761- if (pfdID == 0) { return 1; }
1762-
1763- //ピクセルフォーマットを指定
1764- bResult = SetPixelFormat(hDC, pfdID, &pfd);
1765- if (bResult == FALSE) { return 1; }
1766-
1767- //コンテキストを指定
1768- hGLRC = wglCreateContext(hDC);
1769- if (hGLRC == NULL) { return 1; }
1770-
1771- //デバイスコンテキスト解放
1772- ReleaseDC(hWnd, hDC);
1773-
1774- //システムフォント用意
1775- //フォント名:MS ゴシック サイズ:18
1776- SystemFont = CreateFont(18, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, SHIFTJIS_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, "MS ゴシック");
1777-
1778-#ifdef ENABLE_DEBUGLOG
1779- //ログに出力
1780- OutputLog.WriteLog(LOG_COMPLETE, "", "");
1781-#endif
1782-
1783- //テクスチャフォント用画像のファイル名を設定
1784- strcpy(TextureFontFname, TextureFontFilename);
1785-
1786- //テクスチャフォント用画像を取得
1787- TextureFont = LoadTexture(TextureFontFname, true, false);
1788-
1789-
1790- float aspecth, prx, pry, r;
1791- aspecth = (float)SCREEN_WIDTH/SCREEN_HEIGHT;
1792-
1793- //HUD_myweapon [奥行き, 縦, 横]
1794-
1795- //HUD_A 現在持っている武器を表示する座標
1796- prx = (float)M_PI/180*-39 * aspecth /2;
1797- pry = (float)M_PI/180*-55 /2;
1798- r = 7.5f;
1799- HUD_myweapon_x[0] = cos(pry)*r;
1800- HUD_myweapon_y[0] = sin(pry)*r;
1801- HUD_myweapon_z[0] = sin(prx)*r;
1802-
1803- //HUD_A 予備の武器を表示する座標
1804- prx = (float)M_PI/180*-52 * aspecth /2;
1805- pry = (float)M_PI/180*-60 /2;
1806- r = 16.0f;
1807- HUD_myweapon_x[1] = cos(pry)*r;
1808- HUD_myweapon_y[1] = sin(pry)*r;
1809- HUD_myweapon_z[1] = sin(prx)*r;
1810-
1811-
1812- //libjpeg初期化
1813- cinfo.err = jpeg_std_error(&jerr);
1814- jpeg_create_decompress(&cinfo);
1815-
1816- return 0;
1817-}
1818-
1819-//! @brief リセット@n
1820-//! (ウィンドウ最小化からの復帰 など)
1821-//! @param WindowCtrl WindowControlクラスのポインタ
1822-//! @return 成功:0 待ち:1 失敗:2
1823-int D3DGraphics::ResetD3D(WindowControl *WindowCtrl)
1824-{
1825-#ifdef ENABLE_DEBUGLOG
1826- //ログに出力
1827- OutputLog.WriteLog(LOG_INIT, "グラフィック", "OpenGL(リセット)");
1828-#endif
1829-
1830- hWnd = WindowCtrl->GethWnd();
1831-
1832-#ifdef ENABLE_DEBUGLOG
1833- //ログに出力
1834- OutputLog.WriteLog(LOG_ERROR, "", "");
1835-#endif
1836-
1837- return 2;
1838-}
1839-
1840-//! @brief モデルファイルを読み込む(.x)
1841-//! @param filename ファイル名
1842-//! @return 成功:モデル認識番号(0以上) 失敗:-1
1843-int D3DGraphics::LoadModel(char* filename)
1844-{
1845-#ifdef ENABLE_DEBUGLOG
1846- //ログに出力
1847- OutputLog.WriteLog(LOG_LOAD, "モデル", filename);
1848-#endif
1849-
1850- int id = -1;
1851- FILE *fp;
1852- char buf[256];
1853- char str[256];
1854-
1855- int vertexs = 0;
1856- MODELVDATA *vertex = NULL;
1857- int polygons = 0;
1858- int *index = NULL;
1859- int in_vertexs, in_polygons;
1860- MODELVDATA *old_vertex;
1861- int *old_index;
1862-
1863- char stroks[] = " ;,"; //区切る文字列
1864-
1865- //空いている認識番号を探す
1866- for(int i=0; i<MAX_TEXTURE; i++){
1867- if( pmodel[i].useflag == false ){
1868- id = i;
1869- break;
1870- }
1871- }
1872- if( id == -1 ){ return -1; }
1873-
1874- //ファイルを読み込む
1875- fp = fopen(filename, "r");
1876- if( fp == NULL ){
1877- return -1; //ファイルが読めない
1878- }
1879-
1880- //マジックコード取得
1881- fgets(buf, 256, fp);
1882- buf[ strlen("xof 0302txt") ] = 0x00;
1883- if( strcmp(buf, "xof 0302txt") != 0 ){
1884- fclose( fp );
1885- return -1; //Xファイルでない
1886- }
1887-
1888- while( fgets(buf, 256, fp) != NULL ){
1889- strcpy(str, buf);
1890- str[ strlen("Mesh") ] = 0x00;
1891- if( strcmp(str, "Mesh") == 0 ){
1892-
1893- fgets(buf, 256, fp);
1894- in_vertexs = atoi(buf);
1895-
1896- if( vertexs == 0 ){
1897- //1つ目のメッシュデータならば、領域を作成するだけ。
1898- vertex = new MODELVDATA [in_vertexs];
1899- }
1900- else{
1901- //2つ目の以降なら、領域を確保し直してコピーし、古い領域は削除。
1902- old_vertex = vertex;
1903- vertex = new MODELVDATA [vertexs+in_vertexs];
1904- memcpy(vertex, old_vertex, sizeof(MODELVDATA)*vertexs);
1905- delete [] old_vertex;
1906- }
1907-
1908- for(int i=0; i<in_vertexs; i++){
1909- fgets(buf, 256, fp);
1910- vertex[i+vertexs].x = (float)atof( strtok(buf, stroks) ) * -1;
1911- vertex[i+vertexs].y = (float)atof( strtok(NULL, stroks) );
1912- vertex[i+vertexs].z = (float)atof( strtok(NULL, stroks) );
1913- }
1914-
1915- fgets(buf, 256, fp);
1916-
1917- fgets(buf, 256, fp);
1918- in_polygons = atoi(buf);
1919-
1920- if( polygons == 0 ){
1921- //1つ目のインデックスデータならば、領域を作成するだけ。
1922- index = new int [in_polygons*5];
1923- }
1924- else{
1925- //2つ目の以降なら、領域を確保し直してコピーし、古い領域は削除。
1926- old_index = index;
1927- index = new int [(polygons+in_polygons)*5];
1928- memcpy(index, old_index, sizeof(int)*polygons*5);
1929- delete [] old_index;
1930- }
1931-
1932- for(int i=0; i<in_polygons; i++){
1933- fgets(buf, 256, fp);
1934- index[(i+polygons)*5] = atoi( strtok(buf, stroks) );
1935- for(int j=0; j<index[(i+polygons)*5]; j++){
1936- index[(i+polygons)*5 + j + 1] = atoi( strtok(NULL, stroks) ) + vertexs;
1937- }
1938- }
1939-
1940- while( fgets(buf, 256, fp) != NULL ){
1941- strcpy(str, buf);
1942- str[ strlen(" MeshTextureCoords") ] = 0x00;
1943- if( strcmp(str, " MeshTextureCoords") == 0 ){
1944-
1945- fgets(buf, 256, fp);
1946- if( atoi(buf) != in_vertexs ){ break; }
1947-
1948- for(int i=0; i<in_vertexs; i++){
1949- fgets(buf, 256, fp);
1950- vertex[i+vertexs].u = (float)atof( strtok(buf, stroks) );
1951- vertex[i+vertexs].v = (float)atof( strtok(NULL, stroks) );
1952- }
1953-
1954- break;
1955- }
1956- }
1957-
1958- vertexs += in_vertexs;
1959- polygons += in_polygons;
1960- }
1961- }
1962-
1963- //ファイルハンドルを解放
1964- fclose( fp );
1965-
1966- float *VertexAry = new float [polygons*6*3];
1967- float *ColorAry = new float [polygons*6*4];
1968- float *TexCoordAry = new float [polygons*6*2];
1969- int vid;
1970- int cnt = 0;
1971-
1972- for(int i=0; i<polygons; i++){
1973- if( index[i*5] == 3 ){
1974- //三角形
1975- vid = index[i*5+1];
1976- VertexAry[0 + cnt*3] = vertex[vid].x;
1977- VertexAry[1 + cnt*3] = vertex[vid].y;
1978- VertexAry[2 + cnt*3] = vertex[vid].z;
1979- TexCoordAry[0 + cnt*2] = vertex[vid].u;
1980- TexCoordAry[1 + cnt*2] = vertex[vid].v;
1981-
1982- VertexAry[3 + cnt*3] = vertex[vid].x;
1983- VertexAry[4 + cnt*3] = vertex[vid].y;
1984- VertexAry[5 + cnt*3] = vertex[vid].z;
1985- TexCoordAry[2 + cnt*2] = vertex[vid].u;
1986- TexCoordAry[3 + cnt*2] = vertex[vid].v;
1987-
1988- vid = index[i*5+3];
1989- VertexAry[6 + cnt*3] = vertex[vid].x;
1990- VertexAry[7 + cnt*3] = vertex[vid].y;
1991- VertexAry[8 + cnt*3] = vertex[vid].z;
1992- TexCoordAry[4 + cnt*2] = vertex[vid].u;
1993- TexCoordAry[5 + cnt*2] = vertex[vid].v;
1994-
1995- vid = index[i*5+2];
1996- VertexAry[9 + cnt*3] = vertex[vid].x;
1997- VertexAry[10 + cnt*3] = vertex[vid].y;
1998- VertexAry[11 + cnt*3] = vertex[vid].z;
1999- TexCoordAry[6 + cnt*2] = vertex[vid].u;
2000- TexCoordAry[7 + cnt*2] = vertex[vid].v;
2001-
2002- VertexAry[12 + cnt*3] = vertex[vid].x;
2003- VertexAry[13 + cnt*3] = vertex[vid].y;
2004- VertexAry[14 + cnt*3] = vertex[vid].z;
2005- TexCoordAry[8 + cnt*2] = vertex[vid].u;
2006- TexCoordAry[9 + cnt*2] = vertex[vid].v;
2007-
2008- VertexAry[15 + cnt*3] = vertex[vid].x;
2009- VertexAry[16 + cnt*3] = vertex[vid].y;
2010- VertexAry[17 + cnt*3] = vertex[vid].z;
2011- TexCoordAry[10 + cnt*2] = vertex[vid].u;
2012- TexCoordAry[11 + cnt*2] = vertex[vid].v;
2013-
2014- cnt += 6;
2015- }
2016- else{
2017- //四角形
2018- vid = index[i*5+1];
2019- VertexAry[0 + cnt*3] = vertex[vid].x;
2020- VertexAry[1 + cnt*3] = vertex[vid].y;
2021- VertexAry[2 + cnt*3] = vertex[vid].z;
2022- TexCoordAry[0 + cnt*2] = vertex[vid].u;
2023- TexCoordAry[1 + cnt*2] = vertex[vid].v;
2024-
2025- VertexAry[3 + cnt*3] = vertex[vid].x;
2026- VertexAry[4 + cnt*3] = vertex[vid].y;
2027- VertexAry[5 + cnt*3] = vertex[vid].z;
2028- TexCoordAry[2 + cnt*2] = vertex[vid].u;
2029- TexCoordAry[3 + cnt*2] = vertex[vid].v;
2030-
2031- vid = index[i*5+4];
2032- VertexAry[6 + cnt*3] = vertex[vid].x;
2033- VertexAry[7 + cnt*3] = vertex[vid].y;
2034- VertexAry[8 + cnt*3] = vertex[vid].z;
2035- TexCoordAry[4 + cnt*2] = vertex[vid].u;
2036- TexCoordAry[5 + cnt*2] = vertex[vid].v;
2037-
2038- vid = index[i*5+2];
2039- VertexAry[9 + cnt*3] = vertex[vid].x;
2040- VertexAry[10 + cnt*3] = vertex[vid].y;
2041- VertexAry[11 + cnt*3] = vertex[vid].z;
2042- TexCoordAry[6 + cnt*2] = vertex[vid].u;
2043- TexCoordAry[7 + cnt*2] = vertex[vid].v;
2044-
2045- vid = index[i*5+3];
2046- VertexAry[12 + cnt*3] = vertex[vid].x;
2047- VertexAry[13 + cnt*3] = vertex[vid].y;
2048- VertexAry[14 + cnt*3] = vertex[vid].z;
2049- TexCoordAry[8 + cnt*2] = vertex[vid].u;
2050- TexCoordAry[9 + cnt*2] = vertex[vid].v;
2051-
2052- VertexAry[15 + cnt*3] = vertex[vid].x;
2053- VertexAry[16 + cnt*3] = vertex[vid].y;
2054- VertexAry[17 + cnt*3] = vertex[vid].z;
2055- TexCoordAry[10 + cnt*2] = vertex[vid].u;
2056- TexCoordAry[11 + cnt*2] = vertex[vid].v;
2057-
2058- cnt += 6;
2059- }
2060- }
2061-
2062- //色情報配列を用意
2063- ColorAry[0] = 1.0f;
2064- ColorAry[1] = 1.0f;
2065- ColorAry[2] = 1.0f;
2066- ColorAry[3] = 1.0f;
2067- for(int i=1; i<cnt; i++){
2068- memcpy(&(ColorAry[i*4]), ColorAry, sizeof(float)*4);
2069- }
2070-
2071- delete [] vertex;
2072- delete [] index;
2073-
2074- pmodel[id].useflag = true;
2075- pmodel[id].polygons = polygons;
2076- pmodel[id].VertexAry = VertexAry;
2077- pmodel[id].ColorAry = ColorAry;
2078- pmodel[id].TexCoordAry = TexCoordAry;
2079-
2080-#ifdef ENABLE_DEBUGLOG
2081- //ログに出力
2082- OutputLog.WriteLog(LOG_COMPLETE, "", id);
2083-#endif
2084- return id;
2085-}
2086-
2087-//! @brief モデルファイルの中間データを作成(モーフィング)
2088-//! @param idA モデルAの認識番号
2089-//! @param idB モデルBの認識番号
2090-//! @return 成功:新しいモデル認識番号(0以上) 失敗:-1
2091-//! @attention モデルAとモデルBは、頂点数・ポリゴン数・インデックスが同じである必要があります。
2092-//! @attention それぞれのモデルデータが正しくないか 頂点数が異なる場合、実行に失敗します。
2093-int D3DGraphics::MorphingModel(int idA, int idB)
2094-{
2095-#ifdef ENABLE_DEBUGLOG
2096- char str[128];
2097- sprintf(str, "中間データ作成  ID:%d and %d", idA, idB);
2098-
2099- //ログに出力
2100- OutputLog.WriteLog(LOG_LOAD, "モデル", str);
2101-#endif
2102-
2103- /*
2104- //データが正しいか調べる
2105- if( (idA < 0)||((MAX_MODEL -1) < idA) ){ return -1; }
2106- if( pmodel[idA].useflag == false ){ return; }
2107- if( (idB < 0)||((MAX_MODEL -1) < idB) ){ return -1; }
2108- if( pmodel[idB].useflag == false ){ return; }
2109-
2110- int id = -1;
2111-
2112- //空いている認識番号を探す
2113- for(int i=0; i<MAX_TEXTURE; i++){
2114- if( pmodel[i].useflag == false ){
2115- id = i;
2116- break;
2117- }
2118- }
2119- if( id == -1 ){ return -1; }
2120-
2121- return -1;
2122- */
2123-
2124-#ifdef ENABLE_DEBUGLOG
2125- //ログに出力
2126- OutputLog.WriteLog(LOG_ERROR, "", "");
2127-#endif
2128-
2129- return idA;
2130-}
2131-
2132-//! @brief モデルファイルを解放
2133-//! @param id モデル認識番号
2134-void D3DGraphics::CleanupModel(int id)
2135-{
2136- if( (id < 0)||((MAX_MODEL -1) < id) ){ return; }
2137- if( pmodel[id].useflag == false ){ return; }
2138-
2139- delete pmodel[id].VertexAry;
2140- delete pmodel[id].ColorAry;
2141- delete pmodel[id].TexCoordAry;
2142- pmodel[id].useflag = false;
2143-
2144-#ifdef ENABLE_DEBUGLOG
2145- //ログに出力
2146- OutputLog.WriteLog(LOG_CLEANUP, "モデル", id);
2147-#endif
2148-}
2149-
2150-//! @brief テクスチャを読み込む
2151-//! @param filename ファイル名
2152-//! @param texturefont テクスチャフォントフラグ
2153-//! @param BlackTransparent 黒を透過する
2154-//! @return 成功:テクスチャ認識番号(0以上) 失敗:-1
2155-int D3DGraphics::LoadTexture(char* filename, bool texturefont, bool BlackTransparent)
2156-{
2157- int id = -1;
2158- char filename2[MAX_PATH];
2159- int format = 0;
2160-
2161-#ifdef ENABLE_DEBUGLOG
2162- //ログに出力
2163- OutputLog.WriteLog(LOG_LOAD, "テクスチャ", filename);
2164-#endif
2165-
2166- //空いている認識番号を探す
2167- for(int i=0; i<MAX_TEXTURE; i++){
2168- if( ptextures[i].useflag == false ){
2169- id = i;
2170- break;
2171- }
2172- }
2173- if( id == -1 ){ return -1; }
2174-
2175- //ファイル名を小文字へ変換(拡張子判定用)
2176- for(int i=0; i<strlen(filename); i++){
2177- filename2[i] = (char)tolower(filename[i]);
2178- }
2179- filename2[ strlen(filename) ] = NULL;
2180-
2181- //拡張子でファイルフォーマットを判定
2182- for(int i=strlen(filename2)-1; i>0; i--){
2183- if( filename2[i] == '.' ){
2184- if( strcmp(&(filename2[i]), ".bmp") == 0 ){
2185- format = 1;
2186- }
2187- if( strcmp(&(filename2[i]), ".dds") == 0 ){
2188- format = 2;
2189- }
2190- if( strcmp(&(filename2[i]), ".jpeg") == 0 ){
2191- format = 3;
2192- }
2193- if( strcmp(&(filename2[i]), ".jpg") == 0 ){
2194- format = 3;
2195- }
2196- if( strcmp(&(filename2[i]), ".png") == 0 ){
2197- format = 4;
2198- }
2199- break;
2200- }
2201- }
2202-
2203- //対応してないフォーマット
2204- if( format == 0 ){ return -1; }
2205-
2206- if( format == 1 ){ // .bmp
2207- if( LoadBMPTexture(filename, BlackTransparent, &(ptextures[id])) == false ){
2208- return -1;
2209- }
2210- }
2211- if( format == 2 ){ // .dds
2212- if( LoadDDSTexture(filename, BlackTransparent, &(ptextures[id])) == false ){
2213- return -1;
2214- }
2215- }
2216- if( format == 3 ){ // .jpeg
2217- if( LoadJPEGTexture(filename, BlackTransparent, &(ptextures[id])) == false ){
2218- return -1;
2219- }
2220- }
2221- if( format == 4 ){ // .png
2222- if( LoadPNGTexture(filename, BlackTransparent, &(ptextures[id])) == false ){
2223- return -1;
2224- }
2225- }
2226-
2227-
2228- //テクスチャ有効
2229- glEnable(GL_TEXTURE_2D);
2230-
2231- HDC hDC;
2232- hDC = GetDC(hWnd);
2233- wglMakeCurrent(hDC, hGLRC);
2234- glGenTextures(1 , &(textureobjname[id]));
2235- ReleaseDC(hWnd, hDC);
2236-
2237- glBindTexture(GL_TEXTURE_2D, textureobjname[id]);
2238-
2239- //OpenGLにセット
2240- int width = ptextures[id].width;
2241- int height = ptextures[id].height;
2242- unsigned char *data = ptextures[id].data;
2243- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
2244-
2245- //ミップマップ設定
2246- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2247- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2248-
2249- //乗算合成
2250- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2251-
2252- //テクスチャ無効
2253- glDisable(GL_TEXTURE_2D);
2254-
2255-#ifdef ENABLE_DEBUGLOG
2256- //ログに出力
2257- OutputLog.WriteLog(LOG_COMPLETE, "", id);
2258-#endif
2259-
2260- return id;
2261-
2262-
2263- /*
2264- unsigned char *data = new unsigned char [16*4];
2265-
2266- data[0*4 + 0] = 255; data[0*4 + 1] = 255; data[0*4 + 2] = 255; data[0*4 + 3] = 255;
2267- data[1*4 + 0] = 0; data[1*4 + 1] = 0; data[1*4 + 2] = 0; data[1*4 + 3] = 255;
2268- data[2*4 + 0] = 255; data[2*4 + 1] = 255; data[2*4 + 2] = 255; data[2*4 + 3] = 255;
2269- data[3*4 + 0] = 0; data[3*4 + 1] = 0; data[3*4 + 2] = 0; data[3*4 + 3] = 255;
2270- data[4*4 + 0] = 255; data[4*4 + 1] = 0; data[4*4 + 2] = 0; data[4*4 + 3] = 255;
2271- data[5*4 + 0] = 0; data[5*4 + 1] = 255; data[5*4 + 2] = 0; data[5*4 + 3] = 255;
2272- data[6*4 + 0] = 0; data[6*4 + 1] = 0; data[6*4 + 2] = 255; data[6*4 + 3] = 255;
2273- data[7*4 + 0] = 0; data[7*4 + 1] = 0; data[7*4 + 2] = 0; data[7*4 + 3] = 255;
2274- data[8*4 + 0] = 128; data[8*4 + 1] = 0; data[8*4 + 2] = 0; data[8*4 + 3] = 255;
2275- data[9*4 + 0] = 0; data[9*4 + 1] = 128; data[9*4 + 2] = 0; data[9*4 + 3] = 255;
2276- data[10*4 + 0] = 0; data[10*4 + 1] = 0; data[10*4 + 2] = 128; data[10*4 + 3] = 255;
2277- data[11*4 + 0] = 0; data[11*4 + 1] = 0; data[11*4 + 2] = 0; data[11*4 + 3] = 255;
2278- data[12*4 + 0] = 255; data[12*4 + 1] = 255; data[12*4 + 2] = 0; data[12*4 + 3] = 255;
2279- data[13*4 + 0] = 255; data[13*4 + 1] = 0; data[13*4 + 2] = 255; data[13*4 + 3] = 255;
2280- data[14*4 + 0] = 0; data[14*4 + 1] = 255; data[14*4 + 2] = 255; data[14*4 + 3] = 255;
2281- data[15*4 + 0] = 255; data[15*4 + 1] = 255; data[15*4 + 2] = 255; data[15*4 + 3] = 255;
2282-
2283- ptextures[id].data = data;
2284- ptextures[id].width = 4;
2285- ptextures[id].height = 4;
2286-
2287- ptextures[id].useflag = true;
2288-
2289- return id;
2290- */
2291-}
2292-
2293-//! @brief BMPファイルをを読み込む
2294-//! @param filename ファイル名
2295-//! @param BlackTransparent 黒を透過する
2296-//! @param ptexture 受け取るTEXTUREDATA構造体のポインタ
2297-//! @return 成功:true 失敗:false
2298-//! @attention 通常は LoadTexture()関数 から呼びだすこと。
2299-bool D3DGraphics::LoadBMPTexture(char* filename, bool BlackTransparent, TEXTUREDATA *ptexture)
2300-{
2301- FILE *fp;
2302- unsigned char header[54];
2303- unsigned int dataPos;
2304- unsigned int width, height;
2305- unsigned int index;
2306-
2307- //ファイルを読み込む
2308- fp = fopen(filename, "rb");
2309- if( fp == NULL ){
2310- return false; //ファイルが読めない
2311- }
2312-
2313- //ヘッダーを読む
2314- fread(header, 1, 54, fp);
2315-
2316- if( (header[0x00] != 'B')||(header[0x01] != 'M') ){
2317- fclose(fp);
2318- return false; //.bmpではない
2319- }
2320-
2321- // バイト配列から整数を読み込む
2322- dataPos = *(int*)&(header[0x0E]) + 14;
2323- width = *(int*)&(header[0x12]);
2324- height = *(int*)&(header[0x16]);
2325- index = *(int*)&(header[0x1C]);
2326-
2327- //実データの先頭まで移動
2328- fseek(fp, dataPos, SEEK_SET);
2329-
2330- unsigned char *data = new unsigned char [width*height*4];
2331-
2332- //各ピクセル8ビットなら、256色パレットモード
2333- if( index == 8 ){
2334- unsigned char pixel;
2335- unsigned char *pallet = new unsigned char [256*4];
2336- fread(pallet, 1, 256*4, fp);
2337-
2338- for(int h=height-1; h>=0; h--){
2339- for(int w=0; w<width; w++){
2340- fread(&pixel, 1, 1, fp);
2341-
2342- data[(h*width+w)*4 + 0] = pallet[pixel*4 + 2];
2343- data[(h*width+w)*4 + 1] = pallet[pixel*4 + 1];
2344- data[(h*width+w)*4 + 2] = pallet[pixel*4 + 0];
2345- data[(h*width+w)*4 + 3] = 255;
2346-
2347- if( BlackTransparent == true ){
2348- //黒ならば透過する
2349- if( (data[(h*width+w)*4 + 0] == 0)&&(data[(h*width+w)*4 + 1] == 0)&&(data[(h*width+w)*4 + 2] == 0) ){
2350- data[(h*width+w)*4 + 3] = 0;
2351- }
2352- }
2353- }
2354- }
2355-
2356- delete []pallet;
2357- }
2358-
2359- //各ピクセル24ビットなら、フルカラー
2360- if( index == 24 ){
2361- unsigned char pixel[3];
2362- for(int h=height-1; h>=0; h--){
2363- for(int w=0; w<width; w++){
2364- fread(&pixel, 1, 3, fp);
2365-
2366- data[(h*width+w)*4 + 0] = pixel[2];
2367- data[(h*width+w)*4 + 1] = pixel[1];
2368- data[(h*width+w)*4 + 2] = pixel[0];
2369- data[(h*width+w)*4 + 3] = 255;
2370-
2371- if( BlackTransparent == true ){
2372- //黒ならば透過する
2373- if( (data[(h*width+w)*4 + 0] == 0)&&(data[(h*width+w)*4 + 1] == 0)&&(data[(h*width+w)*4 + 2] == 0) ){
2374- data[(h*width+w)*4 + 3] = 0;
2375- }
2376- }
2377- }
2378- }
2379- }
2380-
2381- //ファイルハンドルを解放
2382- fclose( fp );
2383-
2384- //構造体に代入
2385- ptexture->data = data;
2386- ptexture->width = width;
2387- ptexture->height = height;
2388-
2389- ptexture->useflag = true;
2390-
2391- return true;
2392-}
2393-
2394-//! @brief DDSファイルをを読み込む
2395-//! @param filename ファイル名
2396-//! @param BlackTransparent 黒を透過する
2397-//! @param ptexture 受け取るTEXTUREDATA構造体のポインタ
2398-//! @return 成功:true 失敗:false
2399-//! @attention 通常は LoadTexture()関数 から呼びだすこと。
2400-bool D3DGraphics::LoadDDSTexture(char* filename, bool BlackTransparent, TEXTUREDATA *ptexture)
2401-{
2402- FILE *fp;
2403- unsigned char header[124+4];
2404- unsigned int width, height;
2405- unsigned int index;
2406-
2407- //ファイルを読み込む
2408- fp = fopen(filename, "rb");
2409- if( fp == NULL ){
2410- return false; //ファイルが読めない
2411- }
2412-
2413- //ヘッダーを読む
2414- fread(header, 1, 124+4, fp);
2415-
2416- if( (header[0x00] != 'D')||(header[0x01] != 'D')||(header[0x02] != 'S')||(header[0x03] != ' ') ){
2417- fclose(fp);
2418- return false; //.ddsではない
2419- }
2420-
2421- // バイト配列から整数を読み込む
2422- width = *(int*)&(header[0x10]);
2423- height = *(int*)&(header[0x0C]);
2424- index = *(int*)&(header[0x58]);
2425-
2426- if( (index != 16)&&(index != 32) ){
2427- fclose(fp);
2428- return false; //対応してないフォーマット
2429- }
2430-
2431- unsigned char *data = new unsigned char [width*height*4];
2432-
2433- for(int h=0; h<height; h++){
2434- for(int w=0; w<width; w++){
2435- unsigned char pixel[4];
2436- fread(&pixel, 1, index/8, fp);
2437-
2438- if( index == 16 ){ //各ピクセル16ビット
2439- data[(h*width+w)*4 + 0] = (pixel[1]<<4)&0xF0;
2440- data[(h*width+w)*4 + 1] = pixel[0]&0xF0;
2441- data[(h*width+w)*4 + 2] = (pixel[0]<<4)&0xF0;
2442- data[(h*width+w)*4 + 3] = pixel[1]&0xF0;
2443- }
2444- if( index == 32 ){ //各ピクセル32ビット
2445- data[(h*width+w)*4 + 0] = pixel[2];
2446- data[(h*width+w)*4 + 1] = pixel[1];
2447- data[(h*width+w)*4 + 2] = pixel[0];
2448- data[(h*width+w)*4 + 3] = pixel[3];
2449- }
2450-
2451- if( BlackTransparent == true ){
2452- //黒ならば透過する
2453- if( (data[(h*width+w)*4 + 0] == 0)&&(data[(h*width+w)*4 + 1] == 0)&&(data[(h*width+w)*4 + 2] == 0) ){
2454- data[(h*width+w)*4 + 3] = 0;
2455- }
2456- }
2457- }
2458- }
2459-
2460- //ファイルハンドルを解放
2461- fclose( fp );
2462-
2463- //構造体に代入
2464- ptexture->data = data;
2465- ptexture->width = width;
2466- ptexture->height = height;
2467-
2468- ptexture->useflag = true;
2469-
2470- return true;
2471-}
2472-
2473-//! @brief JPEGファイルをを読み込む
2474-//! @param filename ファイル名
2475-//! @param BlackTransparent 黒を透過する
2476-//! @param ptexture 受け取るTEXTUREDATA構造体のポインタ
2477-//! @return 成功:true 失敗:false
2478-//! @attention 通常は LoadTexture()関数 から呼びだすこと。
2479-bool D3DGraphics::LoadJPEGTexture(char* filename, bool BlackTransparent, TEXTUREDATA *ptexture)
2480-{
2481- FILE *fp;
2482- JSAMPARRAY img;
2483- unsigned int width, height;
2484-
2485- //ファイルを読み込む
2486- fp = fopen(filename, "rb");
2487- if( fp == NULL ){
2488- return false; //ファイルが読めない
2489- }
2490- jpeg_stdio_src(&cinfo, fp);
2491-
2492- //パラメータの設定
2493- jpeg_read_header(&cinfo, true);
2494-
2495- //展開開始
2496- jpeg_start_decompress(&cinfo);
2497-
2498- //領域確保
2499- img = (JSAMPARRAY)new JSAMPROW [cinfo.output_height];
2500- for(int i=0; i<cinfo.output_height; i++){
2501- img[i] = (JSAMPROW)new JSAMPLE [cinfo.output_width * cinfo.out_color_components];
2502- }
2503-
2504- //展開
2505- while( cinfo.output_scanline < cinfo.output_height ) {
2506- jpeg_read_scanlines(&cinfo, img + cinfo.output_scanline, cinfo.output_height - cinfo.output_scanline);
2507- }
2508-
2509- //展開終了
2510- jpeg_finish_decompress(&cinfo);
2511-
2512-
2513- //ファイルハンドルを解放
2514- fclose( fp );
2515-
2516-
2517- // バイト配列から整数を読み込む
2518- width = (int)cinfo.output_width;
2519- height = (int)cinfo.output_height;
2520-
2521- unsigned char *data = new unsigned char [width*height*4];
2522-
2523- for(int h=0; h<height; h++){
2524- //1ライン分取得
2525- JSAMPROW p = img[h];
2526-
2527- for(int w=0; w<width; w++){
2528- data[(h*width+w)*4 + 0] = p[w*3 + 0];
2529- data[(h*width+w)*4 + 1] = p[w*3 + 1];
2530- data[(h*width+w)*4 + 2] = p[w*3 + 2];
2531- data[(h*width+w)*4 + 3] = 255;
2532-
2533- if( BlackTransparent == true ){
2534- //黒ならば透過する
2535- if( (data[(h*width+w)*4 + 0] == 0)&&(data[(h*width+w)*4 + 1] == 0)&&(data[(h*width+w)*4 + 2] == 0) ){
2536- data[(h*width+w)*4 + 3] = 0;
2537- }
2538- }
2539-
2540- }
2541- }
2542-
2543- //領域解放
2544- for(int i=0; i<cinfo.output_height; i++){
2545- delete [] img[i];
2546- }
2547- delete [] img;
2548-
2549- //構造体に代入
2550- ptexture->data = data;
2551- ptexture->width = width;
2552- ptexture->height = height;
2553-
2554- ptexture->useflag = true;
2555-
2556- return true;
2557-}
2558-
2559-//! @brief PNGファイルをを読み込む
2560-//! @param filename ファイル名
2561-//! @param BlackTransparent 黒を透過する
2562-//! @param ptexture 受け取るTEXTUREDATA構造体のポインタ
2563-//! @return 成功:true 失敗:false
2564-//! @attention 通常は LoadTexture()関数 から呼びだすこと。
2565-bool D3DGraphics::LoadPNGTexture(char* filename, bool BlackTransparent, TEXTUREDATA *ptexture)
2566-{
2567- FILE *fp;
2568- png_byte sig[4];
2569- png_structp pPng;
2570- png_infop pInfo;
2571- unsigned int width, height;
2572-
2573- //ファイルを読み込む
2574- fp = fopen(filename, "rb");
2575- if( fp == NULL ){
2576- return false; //ファイルが読めない
2577- }
2578-
2579- //PNGファイルか判定
2580- fread(sig, 4, 1, fp);
2581- if( png_sig_cmp(sig, 0, 4) != 0 ){
2582- fclose(fp);
2583- return false; //.pngではない
2584- }
2585-
2586- //構造体を初期化
2587- pPng = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2588- pInfo = png_create_info_struct(pPng);
2589-
2590- //情報を取得
2591- png_init_io(pPng, fp);
2592- png_set_sig_bytes(pPng, 4);
2593- png_read_info(pPng, pInfo);
2594-
2595- //テクスチャの大きさを取得
2596- width = png_get_image_width(pPng, pInfo);
2597- height = png_get_image_height(pPng, pInfo);
2598-
2599- //インターレス方式か判定
2600- if( png_get_interlace_type(pPng, pInfo) != PNG_INTERLACE_NONE ){
2601- png_destroy_read_struct(&pPng, &pInfo, (png_infopp)NULL);
2602- fclose(fp);
2603- return false; //インターレスには対応しない。
2604- }
2605-
2606- //アルファチャンネルを初期化
2607- png_set_add_alpha(pPng, 0xff, PNG_FILLER_AFTER);
2608-
2609- // tRNSチャンクがあれば、アルファチャンネルに変換
2610- if (png_get_valid(pPng, pInfo, PNG_INFO_tRNS)) {
2611- png_set_tRNS_to_alpha(pPng);
2612- }
2613-
2614- unsigned char *data = new unsigned char [width*height*4];
2615-
2616- //1ライン分の作業領域を確保
2617- png_bytep buf = new png_byte[width*4];
2618-
2619- for(int h=0; h<height; h++){
2620- //1ライン分取得
2621- png_read_row(pPng,buf,NULL);
2622-
2623- for(int w=0; w<width; w++){
2624- data[(h*width+w)*4 + 0] = buf[w*4 + 0];
2625- data[(h*width+w)*4 + 1] = buf[w*4 + 1];
2626- data[(h*width+w)*4 + 2] = buf[w*4 + 2];
2627- data[(h*width+w)*4 + 3] = buf[w*4 + 3];
2628-
2629- if( BlackTransparent == true ){
2630- //黒ならば透過する
2631- if( (data[(h*width+w)*4 + 0] == 0)&&(data[(h*width+w)*4 + 1] == 0)&&(data[(h*width+w)*4 + 2] == 0) ){
2632- data[(h*width+w)*4 + 3] = 0;
2633- }
2634- }
2635- }
2636- }
2637-
2638- //1ライン分の作業領域を解放
2639- delete [] buf;
2640-
2641- //解放
2642- png_read_end(pPng, NULL);
2643- png_destroy_read_struct(&pPng, &pInfo, (png_infopp)NULL);
2644-
2645- //ファイルハンドルを解放
2646- fclose( fp );
2647-
2648- //構造体に代入
2649- ptexture->data = data;
2650- ptexture->width = width;
2651- ptexture->height = height;
2652-
2653- ptexture->useflag = true;
2654-
2655- return true;
2656-}
2657-
2658-//! @brief テクスチャのサイズを取得
2659-//! @param id テクスチャ認識番号
2660-//! @param width 幅を受け取るポインタ
2661-//! @param height 高さを受け取るポインタ
2662-//! @return 成功:0 失敗:1
2663-//! @attention サーフェイスのサイズを取得します。GPUにロードされたサイズであり、テクスチャ(現物)と異なる場合があります。
2664-int D3DGraphics::GetTextureSize(int id, int *width, int *height)
2665-{
2666- //無効な認識番号が指定されていたら、処理せず返す。
2667- if( id == -1 ){ return 1; }
2668- if( ptextures[id].useflag == false ){ return 1; }
2669-
2670- *width = ptextures[id].width;
2671- *height = ptextures[id].height;
2672-
2673- return 0;
2674-}
2675-
2676-//! @brief テクスチャを指定
2677-//! @param TextureID テクスチャ認識番号
2678-void D3DGraphics::SetTexture(int TextureID)
2679-{
2680- if( now_textureid == TextureID ){
2681- return;
2682- }
2683-
2684- //OpenGLにセット
2685- glBindTexture(GL_TEXTURE_2D, textureobjname[TextureID]);
2686-
2687- now_textureid = TextureID;
2688-}
2689-
2690-//! @brief テクスチャを解放
2691-//! @param id テクスチャ認識番号
2692-void D3DGraphics::CleanupTexture(int id)
2693-{
2694- if( (id < 0)||((MAX_TEXTURE -1) < id) ){ return; }
2695- if( ptextures[id].useflag == false ){ return; }
2696-
2697- delete ptextures[id].data;
2698- glDeleteTextures(1 , &(textureobjname[id]));
2699- ptextures[id].useflag = false;
2700-
2701-#ifdef ENABLE_DEBUGLOG
2702- //ログに出力
2703- OutputLog.WriteLog(LOG_CLEANUP, "テクスチャ", id);
2704-#endif
2705-}
2706-
2707-//! @brief 全ての描画処理を開始
2708-//! @return 成功:0 失敗:1
2709-//! @attention 描画処理の最初に呼び出す必要があります。
2710-int D3DGraphics::StartRender()
2711-{
2712- HDC hDC;
2713-
2714- hDC = BeginPaint(hWnd, &Paint_ps);
2715-
2716- //コンテキストを指定
2717- wglMakeCurrent(hDC, hGLRC);
2718-
2719- //初期化
2720- glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
2721- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2722-
2723- glEnable(GL_DEPTH_TEST);
2724- glEnable(GL_CULL_FACE);
2725-
2726- //混合処理(透過有効化)
2727- glEnable(GL_BLEND);
2728- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2729-
2730- //アルファテスト
2731- glAlphaFunc(GL_GREATER, 0.0f);
2732- glEnable(GL_ALPHA_TEST);
2733-
2734- return 0;
2735-}
2736-
2737-//! @brief 全ての描画処理を終了
2738-//! @return 成功:false 失敗:true
2739-//! @attention 描画処理の最後に呼び出す必要があります。
2740-bool D3DGraphics::EndRender()
2741-{
2742- glFlush();
2743-
2744- wglMakeCurrent(NULL, NULL);
2745-
2746- EndPaint(hWnd, &Paint_ps);
2747-
2748- return false;
2749-}
2750-
2751-//! @brief Zバッファをリセット
2752-void D3DGraphics::ResetZbuffer()
2753-{
2754- glClear(GL_DEPTH_BUFFER_BIT);
2755-}
2756-
2757-//! @brief ワールド空間を原点(0,0,0)に戻す など
2758-void D3DGraphics::ResetWorldTransform()
2759-{
2760- glMatrixMode(GL_PROJECTION);
2761- glLoadIdentity();
2762- gluPerspective(viewangle*(180.0f/(float)M_PI), (float)width/height, CLIPPINGPLANE_NEAR, CLIPPINGPLANE_FAR);
2763-
2764- glMatrixMode(GL_MODELVIEW);
2765- glLoadIdentity();
2766- gluLookAt(camera_x*-1, camera_y, camera_z, camera_x*-1 + cos(camera_rx*-1 + (float)M_PI)*cos(camera_ry), camera_y + sin(camera_ry), camera_z + sin(camera_rx*-1 + (float)M_PI)*cos(camera_ry), 0.0f, 1.0f, 0.0f);
2767-}
2768-
2769-//! @brief ワールド空間の座標・角度・拡大率を設定
2770-//! @param x X座標
2771-//! @param y Y座標
2772-//! @param z Z座標
2773-//! @param rx 横軸角度
2774-//! @param ry 縦軸角度
2775-//! @param size 拡大率
2776-void D3DGraphics::SetWorldTransform(float x, float y, float z, float rx, float ry, float size)
2777-{
2778- SetWorldTransform(x, y, z, rx, ry, 0.0f, size);
2779-}
2780-
2781-//! @brief ワールド空間の座標・角度・拡大率を設定
2782-//! @param x X座標
2783-//! @param y Y座標
2784-//! @param z Z座標
2785-//! @param rx 横軸角度
2786-//! @param ry1 縦軸角度
2787-//! @param ry2 縦軸角度
2788-//! @param size 拡大率
2789-void D3DGraphics::SetWorldTransform(float x, float y, float z, float rx, float ry1, float ry2, float size)
2790-{
2791- ResetWorldTransform();
2792-
2793- glMatrixMode(GL_MODELVIEW);
2794-
2795- glTranslatef(x*-1, y, z);
2796- glRotatef(rx*-1*(180.0f/(float)M_PI), 0.0f, 1.0f, 0.0f);
2797- glRotatef(ry1*(180.0f/(float)M_PI), 1.0f, 0.0f, 0.0f);
2798- glRotatef(ry2*-1*(180.0f/(float)M_PI), 0.0f, 0.0f, 1.0f);
2799- glScalef(size, size, size);
2800-}
2801-
2802-//! @brief ワールド空間の座標・角度・拡大率を設定(エフェクト用)
2803-//! @param x X座標
2804-//! @param y Y座標
2805-//! @param z Z座標
2806-//! @param rx 横軸角度
2807-//! @param ry 縦軸角度
2808-//! @param rt 回転角度
2809-//! @param size 拡大率
2810-void D3DGraphics::SetWorldTransformEffect(float x, float y, float z, float rx, float ry, float rt, float size)
2811-{
2812- ResetWorldTransform();
2813-
2814- glMatrixMode(GL_MODELVIEW);
2815-
2816- glTranslatef(x*-1, y, z);
2817- glRotatef(rx*-1*(180.0f/(float)M_PI), 0.0f, 1.0f, 0.0f);
2818- glRotatef(ry*-1*(180.0f/(float)M_PI), 0.0f, 0.0f, 1.0f);
2819- glRotatef(rt*(180.0f/(float)M_PI), 1.0f, 0.0f, 0.0f);
2820- glScalef(size, size, size);
2821-}
2822-
2823-//! @brief ワールド空間を人が武器を持つ場所に設定
2824-//! @param x X座標
2825-//! @param y Y座標
2826-//! @param z Z座標
2827-//! @param mx 手元を原点にした モデルのX座標
2828-//! @param my 手元を原点にした モデルのY座標
2829-//! @param mz 手元を原点にした モデルのZ座標
2830-//! @param rx 横軸角度
2831-//! @param ry 縦軸角度
2832-//! @param size 拡大率
2833-void D3DGraphics::SetWorldTransformHumanWeapon(float x, float y, float z, float mx, float my, float mz, float rx, float ry, float size)
2834-{
2835- ResetWorldTransform();
2836-
2837- glMatrixMode(GL_MODELVIEW);
2838-
2839- glTranslatef(x*-1, y, z);
2840- glRotatef(rx*-1*(180.0f/(float)M_PI), 0.0f, 1.0f, 0.0f);
2841- glRotatef(ry*(180.0f/(float)M_PI), 1.0f, 0.0f, 0.0f);
2842- glTranslatef(mx*-1, my, mz);
2843- glScalef(size, size, size);
2844-}
2845-
2846-//! @brief ワールド空間を所持している武器を表示する場所に設定
2847-//! @param rotation 武器を回転させる
2848-//! @param camera_x カメラのX座標
2849-//! @param camera_y カメラのY座標
2850-//! @param camera_z カメラのZ座標
2851-//! @param camera_rx カメラの横軸角度
2852-//! @param camera_ry カメラの縦軸角度
2853-//! @param rx 武器のの縦軸角度
2854-//! @param size 表示サイズ
2855-//! @note rotation・・ true:現在持っている武器です。 false:予備の武器です。(rx は無視されます)
2856-//! @todo 位置やサイズの微調整
2857-void D3DGraphics::SetWorldTransformPlayerWeapon(bool rotation, float camera_x, float camera_y, float camera_z, float camera_rx, float camera_ry, float rx, float size)
2858-{
2859- size = size * 0.3f;
2860-
2861- ResetWorldTransform();
2862-
2863- glMatrixMode(GL_MODELVIEW);
2864-
2865- glTranslatef(camera_x*-1, camera_y, camera_z);
2866- glRotatef(camera_rx*(180.0f/(float)M_PI), 0.0f, 1.0f, 0.0f);
2867- glRotatef(camera_ry*-1*(180.0f/(float)M_PI), 0.0f, 0.0f, 1.0f);
2868-
2869- if( rotation == true ){
2870- glTranslatef(HUD_myweapon_x[0]*-1, HUD_myweapon_y[0], HUD_myweapon_z[0]);
2871- glRotatef(rx*-1*(180.0f/(float)M_PI), 0.0f, 1.0f, 0.0f);
2872- }
2873- else{
2874- glTranslatef(HUD_myweapon_x[1]*-1, HUD_myweapon_y[1], HUD_myweapon_z[1]);
2875- glRotatef(180, 0.0f, 1.0f, 0.0f);
2876- }
2877- glScalef(size, size, size);
2878-}
2879-
2880-//! @brief ワールド空間の座標を取得
2881-//! @param *x x軸を受け取るポインタ
2882-//! @param *y y軸を受け取るポインタ
2883-//! @param *z z軸を受け取るポインタ
2884-void D3DGraphics::GetWorldTransformPos(float *x, float *y, float *z)
2885-{
2886- GLfloat model[16];
2887- glMatrixMode(GL_MODELVIEW);
2888- glGetFloatv(GL_MODELVIEW_MATRIX, model);
2889- *x = model[12];
2890- *y = model[13];
2891- *z = model[14];
2892-}
2893-
2894-//! @brief フォグを設定
2895-//! @param skynumber 空の番号
2896-void D3DGraphics::SetFog(int skynumber)
2897-{
2898- float fogColor[4];
2899-
2900- //空の番号により色を決定
2901- switch(skynumber){
2902- case 1: fogColor[0] = 0.25f; fogColor[1] = 0.25f+0.0625; fogColor[2] = 0.25f; fogColor[3] = 1.0f; break;
2903- case 2: fogColor[0] = 0.0625; fogColor[1] = 0.0625; fogColor[2] = 0.0625; fogColor[3] = 1.0f; break;
2904- case 3: fogColor[0] = 0.0f; fogColor[1] = 0.0625; fogColor[2] = 0.125; fogColor[3] = 1.0f; break;
2905- case 4: fogColor[0] = 0.125; fogColor[1] = 0.0625; fogColor[2] = 0.0625; fogColor[3] = 1.0f; break;
2906- case 5: fogColor[0] = 0.25f; fogColor[1] = 0.125; fogColor[2] = 0.125; fogColor[3] = 1.0f; break;
2907- default: fogColor[0] = 0.0f; fogColor[1] = 0.0f; fogColor[2] = 0.0f; fogColor[3] = 1.0f; break;
2908- }
2909-
2910- float fog_st = 100;
2911- float fog_end = 800;
2912- glFogi(GL_FOG_MODE, GL_LINEAR);
2913- glFogfv(GL_FOG_COLOR, fogColor);
2914- glHint(GL_FOG_HINT, GL_NICEST);
2915- glFogf(GL_FOG_START, fog_st);
2916- glFogf(GL_FOG_END, fog_end);
2917-
2918- glEnable(GL_FOG);
2919-}
2920-
2921-//! @brief カメラ(視点)を設定
2922-//! @param in_camera_x カメラのX座標
2923-//! @param in_camera_y カメラのY座標
2924-//! @param in_camera_z カメラのZ座標
2925-//! @param in_camera_rx カメラの横軸角度
2926-//! @param in_camera_ry カメラの縦軸角度
2927-//! @param in_viewangle 視野角
2928-void D3DGraphics::SetCamera(float in_camera_x, float in_camera_y, float in_camera_z, float in_camera_rx, float in_camera_ry, float in_viewangle)
2929-{
2930- glViewport(0, 0, width, height);
2931-
2932- camera_x = in_camera_x;
2933- camera_y = in_camera_y;
2934- camera_z = in_camera_z;
2935- camera_rx = in_camera_rx;
2936- camera_ry = in_camera_ry;
2937- viewangle = in_viewangle;
2938-
2939- ResetWorldTransform();
2940-}
2941-
2942-//! @brief マップデータを取り込む
2943-//! @param in_blockdata ブロックデータ
2944-//! @param directory ブロックデータが存在するディレクトリ
2945-void D3DGraphics::LoadMapdata(BlockDataInterface* in_blockdata, char *directory)
2946-{
2947- //ブロックデータが指定されていなければ、処理しない。
2948- if( in_blockdata == NULL ){ return; }
2949-
2950- char fname[MAX_PATH];
2951- char fnamefull[MAX_PATH];
2952-
2953- //クラスを設定
2954- blockdata = in_blockdata;
2955-
2956- //ブロック数を取得
2957- bs = blockdata->GetTotaldatas();
2958-
2959- //テクスチャ読み込み
2960- for(int i=0; i<TOTAL_BLOCKTEXTURE; i++){
2961- //テクスチャ名を取得
2962- blockdata->GetTexture(fname, i);
2963-
2964- if( strcmp(fname, "") == 0 ){ //指定されていなければ、処理しない
2965- mapTextureID[i] = -1;
2966- }
2967- else{
2968- //「ディレクトリ+ファイル名」を生成し、読み込む
2969- strcpy(fnamefull, directory);
2970- strcat(fnamefull, fname);
2971- mapTextureID[i] = LoadTexture(fnamefull, false, false);
2972- }
2973- }
2974-}
2975-
2976-//! @brief マップデータを描画
2977-//! @param wireframe ワイヤーフレーム表示
2978-void D3DGraphics::DrawMapdata(bool wireframe)
2979-{
2980- //ブロックデータが読み込まれていなければ、処理しない。
2981- if( blockdata == NULL ){ return; }
2982-
2983- struct blockdata data;
2984- int textureID;
2985- int vID[4];
2986- int uvID[4];
2987- float *VertexAry = new float [bs*6 * 6*3];
2988- float *ColorAry = new float [bs*6 * 6*4];
2989- float *TexCoordAry = new float [bs*6 * 6*2];
2990-
2991- if( wireframe == true ){
2992- //ワイヤーフレーム表示
2993- for(int i=0; i<bs; i++){
2994- blockdata->Getdata(&data, i);
2995- Drawline(data.x[0], data.y[0], data.z[0], data.x[1], data.y[1], data.z[1]);
2996- Drawline(data.x[1], data.y[1], data.z[1], data.x[2], data.y[2], data.z[2]);
2997- Drawline(data.x[2], data.y[2], data.z[2], data.x[3], data.y[3], data.z[3]);
2998- Drawline(data.x[3], data.y[3], data.z[3], data.x[0], data.y[0], data.z[0]);
2999- Drawline(data.x[4], data.y[4], data.z[4], data.x[5], data.y[5], data.z[5]);
3000- Drawline(data.x[5], data.y[5], data.z[5], data.x[6], data.y[6], data.z[6]);
3001- Drawline(data.x[6], data.y[6], data.z[6], data.x[7], data.y[7], data.z[7]);
3002- Drawline(data.x[7], data.y[7], data.z[7], data.x[4], data.y[4], data.z[4]);
3003- Drawline(data.x[0], data.y[0], data.z[0], data.x[4], data.y[4], data.z[4]);
3004- Drawline(data.x[1], data.y[1], data.z[1], data.x[5], data.y[5], data.z[5]);
3005- Drawline(data.x[2], data.y[2], data.z[2], data.x[6], data.y[6], data.z[6]);
3006- Drawline(data.x[3], data.y[3], data.z[3], data.x[7], data.y[7], data.z[7]);
3007- }
3008- return;
3009- }
3010-
3011- //配列有効化
3012- glEnableClientState(GL_VERTEX_ARRAY);
3013- glEnableClientState(GL_COLOR_ARRAY);
3014- glEnableClientState(GL_TEXTURE_COORD_ARRAY);
3015-
3016- for(textureID=0; textureID<TOTAL_BLOCKTEXTURE; textureID++){
3017- int cnt = 0;
3018-
3019- //テクスチャが正常に読み込めていなければ設定
3020- if( mapTextureID[textureID] == -1 ){
3021- //テクスチャ無効
3022- glDisable(GL_TEXTURE_2D);
3023- }
3024- else if( ptextures[ mapTextureID[textureID] ].useflag == false ){
3025- //テクスチャ無効
3026- glDisable(GL_TEXTURE_2D);
3027- }
3028- else{
3029- //テクスチャ有効
3030- glEnable(GL_TEXTURE_2D);
3031-
3032- //テクスチャをセット
3033- SetTexture(mapTextureID[textureID]);
3034- }
3035-
3036- for(int i=0; i<bs; i++){
3037- //データ取得
3038- blockdata->Getdata(&data, i);
3039-
3040- for(int j=0; j<6; j++){
3041- //テクスチャ認識番号を取得
3042- int ID = data.material[j].textureID;
3043-
3044- if( textureID == ID ){
3045- //面の頂点データの関連付けを取得
3046- blockdataface(j, &vID[0], &uvID[0]);
3047-
3048- //頂点配列を用意
3049- VertexAry[0 + cnt*18] = data.x[ vID[1] ]*-1; VertexAry[1 + cnt*18] = data.y[ vID[1] ]; VertexAry[2 + cnt*18] = data.z[ vID[1] ];
3050- VertexAry[3 + cnt*18] = data.x[ vID[1] ]*-1; VertexAry[4 + cnt*18] = data.y[ vID[1] ]; VertexAry[5 + cnt*18] = data.z[ vID[1] ];
3051- VertexAry[6 + cnt*18] = data.x[ vID[0] ]*-1; VertexAry[7 + cnt*18] = data.y[ vID[0] ]; VertexAry[8 + cnt*18] = data.z[ vID[0] ];
3052- VertexAry[9 + cnt*18] = data.x[ vID[2] ]*-1; VertexAry[10 + cnt*18] = data.y[ vID[2] ]; VertexAry[11 + cnt*18] = data.z[ vID[2] ];
3053- VertexAry[12 + cnt*18] = data.x[ vID[3] ]*-1; VertexAry[13 + cnt*18] = data.y[ vID[3] ]; VertexAry[14 + cnt*18] = data.z[ vID[3] ];
3054- VertexAry[15 + cnt*18] = data.x[ vID[3] ]*-1; VertexAry[16 + cnt*18] = data.y[ vID[3] ]; VertexAry[17 + cnt*18] = data.z[ vID[3] ];
3055-
3056- //色情報配列を用意
3057- ColorAry[0 + cnt*24] = data.material[j].shadow;
3058- ColorAry[1 + cnt*24] = data.material[j].shadow;
3059- ColorAry[2 + cnt*24] = data.material[j].shadow;
3060- ColorAry[3 + cnt*24] = 1.0f;
3061- for(int i=1; i<6; i++){
3062- memcpy(&(ColorAry[i*4 + cnt*24]), ColorAry, sizeof(float)*4);
3063- }
3064-
3065- //UV座標配列を用意
3066- TexCoordAry[0 + cnt*12] = data.material[j].u[ uvID[1] ]; TexCoordAry[1 + cnt*12] = data.material[j].v[ uvID[1] ];
3067- TexCoordAry[2 + cnt*12] = data.material[j].u[ uvID[1] ]; TexCoordAry[3 + cnt*12] = data.material[j].v[ uvID[1] ];
3068- TexCoordAry[4 + cnt*12] = data.material[j].u[ uvID[0] ]; TexCoordAry[5 + cnt*12] = data.material[j].v[ uvID[0] ];
3069- TexCoordAry[6 + cnt*12] = data.material[j].u[ uvID[2] ]; TexCoordAry[7 + cnt*12] = data.material[j].v[ uvID[2] ];
3070- TexCoordAry[8 + cnt*12] = data.material[j].u[ uvID[3] ]; TexCoordAry[9 + cnt*12] = data.material[j].v[ uvID[3] ];
3071- TexCoordAry[10 + cnt*12] = data.material[j].u[ uvID[3] ]; TexCoordAry[11 + cnt*12] = data.material[j].v[ uvID[3] ];
3072-
3073- cnt += 1;
3074- }
3075- }
3076- }
3077-
3078- //描画
3079- glVertexPointer(3, GL_FLOAT, 0, VertexAry);
3080- glColorPointer(4, GL_FLOAT, 0, ColorAry);
3081- glTexCoordPointer(2, GL_FLOAT, 0, TexCoordAry);
3082- glDrawArrays(GL_TRIANGLE_STRIP, 1, 6*cnt-2);
3083- }
3084-
3085- //配列無効化
3086- glDisableClientState(GL_VERTEX_ARRAY);
3087- glDisableClientState(GL_COLOR_ARRAY);
3088- glDisableClientState(GL_TEXTURE_COORD_ARRAY);
3089-
3090- delete [] VertexAry;
3091- delete [] ColorAry;
3092- delete [] TexCoordAry;
3093-}
3094-
3095-//! @brief マップテクスチャを取得
3096-//! @param id テクスチャ番号
3097-//! @return テクスチャ認識番号(失敗:-1)
3098-int D3DGraphics::GetMapTextureID(int id)
3099-{
3100- if( (id < 0)||((TOTAL_BLOCKTEXTURE -1) < id ) ){ return -1; }
3101- return mapTextureID[id];
3102-}
3103-
3104-//! @brief マップデータを解放
3105-void D3DGraphics::CleanupMapdata()
3106-{
3107- //テクスチャを開放
3108- for(int i=0; i<TOTAL_BLOCKTEXTURE; i++){
3109- CleanupTexture(mapTextureID[i]);
3110- }
3111-
3112- bs = 0;
3113-
3114- blockdata = NULL;
3115-}
3116-
3117-//! @brief モデルファイルを描画
3118-//! @param id_model モデル認識番号
3119-//! @param id_texture テクスチャ認識番号
3120-void D3DGraphics::RenderModel(int id_model, int id_texture)
3121-{
3122- //無効な引数が設定されていれば失敗
3123- if( id_model == -1 ){ return; }
3124- if( pmodel[id_model].useflag == false ){ return; }
3125-
3126- //テクスチャが正常に読み込めていなければ設定
3127- if( id_texture == -1 ){
3128- //テクスチャ無効
3129- glDisable(GL_TEXTURE_2D);
3130- }
3131- else if( ptextures[id_texture].useflag == false ){
3132- //テクスチャ無効
3133- glDisable(GL_TEXTURE_2D);
3134- }
3135- else{
3136- //テクスチャ有効
3137- glEnable(GL_TEXTURE_2D);
3138-
3139- //テクスチャをセット
3140- SetTexture(id_texture);
3141- }
3142-
3143- //配列有効化
3144- glEnableClientState(GL_VERTEX_ARRAY);
3145- glEnableClientState(GL_COLOR_ARRAY);
3146- glEnableClientState(GL_TEXTURE_COORD_ARRAY);
3147-
3148- //描画
3149- glVertexPointer(3, GL_FLOAT, 0, pmodel[id_model].VertexAry);
3150- glColorPointer(4, GL_FLOAT, 0, pmodel[id_model].ColorAry);
3151- glTexCoordPointer(2, GL_FLOAT, 0, pmodel[id_model].TexCoordAry);
3152- glDrawArrays(GL_TRIANGLE_STRIP, 1, pmodel[id_model].polygons*6-2);
3153-
3154- //配列無効化
3155- glDisableClientState(GL_VERTEX_ARRAY);
3156- glDisableClientState(GL_COLOR_ARRAY);
3157- glDisableClientState(GL_TEXTURE_COORD_ARRAY);
3158-
3159- /*
3160- Drawline(1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f);
3161- Drawline(0.0f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f);
3162- Drawline(0.0f, 0.0f, 1.0f, 0.0f, 0.0f, -1.0f);
3163- */
3164-}
3165-
3166-//! @brief 板を描画
3167-//! @param id_texture テクスチャ認識番号
3168-//! @param alpha 透明度 (0.0〜1.0 0.0:完全透明)
3169-void D3DGraphics::RenderBoard(int id_texture, float alpha)
3170-{
3171- //テクスチャが設定されていなければ、処理しない。
3172- if( id_texture == -1 ){ return; }
3173- if( ptextures[id_texture].useflag == false ){ return; }
3174-
3175- float VertexAry[4*3];
3176- float ColorAry[4*4];
3177- float TexCoordAry[4*2];
3178-
3179- //テクスチャ有効
3180- glEnable(GL_TEXTURE_2D);
3181-
3182- //テクスチャをセット
3183- SetTexture(id_texture);
3184-
3185- //頂点配列を用意
3186- VertexAry[0] = 0.0f; VertexAry[1] = 0.5f; VertexAry[2] = 0.5f;
3187- VertexAry[3] = 0.0f; VertexAry[4] = -0.5f; VertexAry[5] = 0.5f;
3188- VertexAry[6] = 0.0f; VertexAry[7] = 0.5f; VertexAry[8] = -0.5f;
3189- VertexAry[9] = 0.0f; VertexAry[10] = -0.5f; VertexAry[11] = -0.5f;
3190-
3191- //色情報配列を用意
3192- ColorAry[0] = 1.0f;
3193- ColorAry[1] = 1.0f;
3194- ColorAry[2] = 1.0f;
3195- ColorAry[3] = alpha;
3196- for(int i=1; i<4; i++){
3197- memcpy(&(ColorAry[i*4]), ColorAry, sizeof(float)*4);
3198- }
3199-
3200- //UV座標配列を用意
3201- TexCoordAry[0] = 1.0f; TexCoordAry[1] = 0.0f;
3202- TexCoordAry[2] = 1.0f; TexCoordAry[3] = 1.0f;
3203- TexCoordAry[4] = 0.0f; TexCoordAry[5] = 0.0f;
3204- TexCoordAry[6] = 0.0f; TexCoordAry[7] = 1.0f;
3205-
3206- //配列有効化
3207- glEnableClientState(GL_VERTEX_ARRAY);
3208- glEnableClientState(GL_COLOR_ARRAY);
3209- glEnableClientState(GL_TEXTURE_COORD_ARRAY);
3210-
3211- //描画
3212- glVertexPointer(3, GL_FLOAT, 0, VertexAry);
3213- glColorPointer(4, GL_FLOAT, 0, ColorAry);
3214- glTexCoordPointer(2, GL_FLOAT, 0, TexCoordAry);
3215- glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
3216-
3217- //配列無効化
3218- glDisableClientState(GL_VERTEX_ARRAY);
3219- glDisableClientState(GL_COLOR_ARRAY);
3220- glDisableClientState(GL_TEXTURE_COORD_ARRAY);
3221-}
3222-
3223-//! @brief 画面の明るさを設定
3224-//! @param Width 幅
3225-//! @param Height 高さ
3226-//! @param Brightness 画面の明るさ (0 で不変、1 以上で明るさの度合い)
3227-void D3DGraphics::ScreenBrightness(int Width, int Height, int Brightness)
3228-{
3229- //明るさ不変なら処理しない(軽量化)
3230- if( Brightness == 0 ){ return; }
3231-
3232- //透明度を設定し、描画
3233- float alpha = 0.02f * Brightness;
3234- Draw2DBox(0, 0, Width, Height, GetColorCode(1.0f,1.0f,1.0f,alpha));
3235-}
3236-
3237-//! @brief 【デバック用】中心線描画
3238-void D3DGraphics::Centerline()
3239-{
3240- ResetWorldTransform();
3241- Drawline(100.0f, 0.0f, 0.0f, -100.0f, 0.0f, 0.0f);
3242- Drawline(0.0f, 100.0f, 0.0f, 0.0f, -100.0f, 0.0f);
3243- Drawline(0.0f, 0.0f, 100.0f, 0.0f, 0.0f, -100.0f);
3244-}
3245-
3246-//! @brief 【デバック用】緑線描画
3247-void D3DGraphics::Drawline(float x1, float y1, float z1, float x2, float y2, float z2)
3248-{
3249- float VertexAry[2*3];
3250- unsigned char ColorAry[2*4];
3251-
3252- //テクスチャ無効
3253- glDisable(GL_TEXTURE_2D);
3254-
3255- //頂点配列を用意
3256- VertexAry[0] = (float)x1*-1; VertexAry[1] = (float)y1; VertexAry[2] = (float)z1;
3257- VertexAry[3] = (float)x2*-1; VertexAry[4] = (float)y2; VertexAry[5] = (float)z2;
3258-
3259- //色情報配列を用意
3260- ColorAry[0] = 0;
3261- ColorAry[1] = 255;
3262- ColorAry[2] = 0;
3263- ColorAry[3] = 255;
3264- memcpy(&(ColorAry[4]), ColorAry, sizeof(unsigned char)*4);
3265-
3266- //配列有効化
3267- glEnableClientState(GL_VERTEX_ARRAY);
3268- glEnableClientState(GL_COLOR_ARRAY);
3269-
3270- //描画
3271- glVertexPointer(3, GL_FLOAT, 0, VertexAry);
3272- glColorPointer(4, GL_UNSIGNED_BYTE, 0, ColorAry);
3273- glDrawArrays(GL_LINE_STRIP, 0, 2);
3274-
3275- //配列無効化
3276- glDisableClientState(GL_VERTEX_ARRAY);
3277- glDisableClientState(GL_COLOR_ARRAY);
3278-}
3279-
3280-//! @brief 文字を描画(システムフォント使用)
3281-//! @param x x座標
3282-//! @param y y座標
3283-//! @param str 文字列 (改行コード:可)
3284-//! @param color 色
3285-//! @warning <b>描画は非常に低速です。</b>画面内で何度も呼び出すとパフォーマンスに影響します。
3286-//! @warning「改行コードを活用し一度に描画する」「日本語が必要ない文字はテクスチャフォントを活用する」などの対応を講じてください。
3287-//! @attention フォントの種類やサイズは固定です。 文字を二重に重ねて立体感を出さないと見にくくなります。
3288-//! @todo 文字を二重に重ねると、上下関係が正しく処理されない。
3289-//! @todo 1文字目が欠ける場合がある。
3290-void D3DGraphics::Draw2DMSFontText(int x, int y, char *str, int color)
3291-{
3292- int len = strlen(str);
3293- WCHAR *ustr;
3294-
3295- Start2DRender();
3296-
3297- //テクスチャ無効
3298- glDisable(GL_TEXTURE_2D);
3299-
3300- //Unicode文字列へ変換
3301- ustr = new WCHAR [len+1];
3302- MultiByteToWideChar(CP_ACP, 0, str, -1, ustr, len + 1);
3303-
3304- //新たな文字列なら、リソースを作り直す
3305- if( lstrcmpW(ustr, now_SystemFontUStr) != 0 ){
3306- GLuint listIdx;
3307- HDC hDC;
3308-
3309- //古いデータを削除
3310- glDeleteLists(SystemFontListIdx, SystemFontListIdxSize);
3311- delete [] now_SystemFontUStr;
3312-
3313- //デバイスコンテキスト設定
3314- hDC = GetDC(hWnd);
3315- wglMakeCurrent(hDC, hGLRC);
3316- SelectObject(hDC, SystemFont);
3317-
3318- //ディスプレイリストを作成
3319- listIdx = glGenLists(len);
3320- for(int i=0; i<lstrlenW(ustr); i++){
3321- wglUseFontBitmapsW(hDC, ustr[i], 1, listIdx+i);
3322- }
3323-
3324- //デバイスコンテキスト廃棄
3325- ReleaseDC(hWnd, hDC);
3326-
3327- //設定を記録
3328- now_SystemFontUStr = new WCHAR [len+1];
3329- lstrcpyW(now_SystemFontUStr, ustr);
3330- SystemFontListIdx = listIdx;
3331- SystemFontListIdxSize = len;
3332- }
3333-
3334- //座標と色を設定
3335- glBitmap(0, 0, 0, 0, 10, 0, NULL);
3336- glRasterPos2i(x, y);
3337- glColor4ub((color>>24)&0xFF, (color>>16)&0xFF, (color>>8)&0xFF, color&0xFF);
3338-
3339- for(int i=0; i<lstrlenW(ustr); i++){
3340- if( ustr[i] == '\n' ){
3341- //改行する
3342- y += 19;
3343- glRasterPos2i(x, y);
3344- }
3345- else{
3346- //ディスプレイリスト描画
3347- glCallList(SystemFontListIdx + i);
3348- }
3349- }
3350-
3351- //Unicode文字列の廃棄
3352- delete [] ustr;
3353-
3354- End2DRender();
3355-}
3356-
3357-//! @brief 文字を中央揃えで描画(システムフォント使用)
3358-//! @param x x座標
3359-//! @param y y座標
3360-//! @param w 横の大きさ
3361-//! @param h 縦の大きさ
3362-//! @param str 文字列 (改行コード:可)
3363-//! @param color 色
3364-//! @warning <b>正しく中央揃えになりません。</b>
3365-void D3DGraphics::Draw2DMSFontTextCenter(int x, int y, int w, int h, char *str, int color)
3366-{
3367- Draw2DMSFontText(x, y, str, color);
3368-}
3369-
3370-//! @brief 2D描画用設定
3371-void D3DGraphics::Start2DRender()
3372-{
3373- glMatrixMode(GL_PROJECTION);
3374- glPushMatrix();
3375- glLoadIdentity();
3376- glOrtho(0, width, height, 0, -1, 1);
3377- glMatrixMode(GL_MODELVIEW);
3378- glPushMatrix();
3379- glLoadIdentity();
3380- gluLookAt(0, 0, 1, 0, 0, 0, 0, 1, 0);
3381-
3382- glDisable(GL_DEPTH_TEST);
3383- glDisable(GL_CULL_FACE);
3384-}
3385-
3386-//! @brief 文字を描画(テクスチャフォント使用)
3387-//! @param x x座標
3388-//! @param y y座標
3389-//! @param str 文字列 (改行コード:<b>不可</b>)
3390-//! @param color 色
3391-//! @param fontwidth 一文字の幅
3392-//! @param fontheight 一文字の高さ
3393-//! @attention 文字を二重に重ねて立体感を出さないと見にくくなります。
3394-void D3DGraphics::Draw2DTextureFontText(int x, int y, char *str, int color, int fontwidth, int fontheight)
3395-{
3396- //テクスチャフォントの取得に失敗していれば、処理しない
3397- if( TextureFont == -1 ){ return; }
3398-
3399- int strlens = (int)strlen(str);
3400-
3401- float *VertexAry = new float [strlens*6*2];
3402- unsigned char *ColorAry = new unsigned char [strlens*6*4];
3403- float *TexCoordAry = new float [strlens*6*2];
3404-
3405- //2D描画用設定を適用
3406- Start2DRender();
3407-
3408- int w;
3409- float font_u, font_v;
3410- float t_u, t_v;
3411-
3412- //1文字のUV座標を計算
3413- font_u = 1.0f / 16;
3414- font_v = 1.0f / 16;
3415-
3416- //テクスチャ有効
3417- glEnable(GL_TEXTURE_2D);
3418-
3419- //テクスチャをセット
3420- SetTexture(TextureFont);
3421-
3422- //配列有効化
3423- glEnableClientState(GL_VERTEX_ARRAY);
3424- glEnableClientState(GL_COLOR_ARRAY);
3425- glEnableClientState(GL_TEXTURE_COORD_ARRAY);
3426-
3427- //色情報配列を用意
3428- ColorAry[0] = (color>>24)&0xFF;
3429- ColorAry[1] = (color>>16)&0xFF;
3430- ColorAry[2] = (color>>8)&0xFF;
3431- ColorAry[3] = color&0xFF;
3432- for(int i=1; i<strlens*6; i++){
3433- memcpy(&(ColorAry[i*4]), ColorAry, sizeof(unsigned char)*4);
3434- }
3435-
3436- // 与えられた文字数分ループ
3437- for(int i=0; i<strlens; i++){
3438- //UV座標を計算
3439- w = str[i];
3440- if( w < 0 ){ w += 256; }
3441- t_u = (w % 16) * font_u;
3442- t_v = (w / 16) * font_v;
3443-
3444- VertexAry[0 + i*12] = (float)x + i*fontwidth; VertexAry[1 + i*12] = (float)y;
3445- VertexAry[2 + i*12] = (float)x + i*fontwidth; VertexAry[3 + i*12] = (float)y;
3446- VertexAry[4 + i*12] = (float)x + fontwidth + i*fontwidth; VertexAry[5 + i*12] = (float)y;
3447- VertexAry[6 + i*12] = (float)x + i*fontwidth; VertexAry[7 + i*12] = (float)y + fontheight;
3448- VertexAry[8 + i*12] = (float)x + fontwidth + i*fontwidth; VertexAry[9 + i*12] = (float)y + fontheight;
3449- VertexAry[10 + i*12] = (float)x + fontwidth + i*fontwidth; VertexAry[11 + i*12] = (float)y + fontheight;
3450- TexCoordAry[0 + i*12] = t_u; TexCoordAry[1 + i*12] = t_v;
3451- TexCoordAry[2 + i*12] = t_u; TexCoordAry[3 + i*12] = t_v;
3452- TexCoordAry[4 + i*12] = t_u + font_u; TexCoordAry[5 + i*12] = t_v;
3453- TexCoordAry[6 + i*12] = t_u; TexCoordAry[7 + i*12] = t_v + font_v;
3454- TexCoordAry[8 + i*12] = t_u + font_u; TexCoordAry[9 + i*12] = t_v + font_v;
3455- TexCoordAry[10 + i*12] = t_u + font_u; TexCoordAry[11 + i*12] = t_v + font_v;
3456- }
3457-
3458- //描画
3459- glVertexPointer(2, GL_FLOAT, 0, VertexAry);
3460- glColorPointer(4, GL_UNSIGNED_BYTE, 0, ColorAry);
3461- glTexCoordPointer(2, GL_FLOAT, 0, TexCoordAry);
3462- glDrawArrays(GL_TRIANGLE_STRIP, 1, strlens*6-2);
3463-
3464- //配列無効化
3465- glDisableClientState(GL_VERTEX_ARRAY);
3466- glDisableClientState(GL_COLOR_ARRAY);
3467- glDisableClientState(GL_TEXTURE_COORD_ARRAY);
3468-
3469- //2D描画用設定を解除
3470- End2DRender();
3471-
3472- delete [] VertexAry;
3473- delete [] ColorAry;
3474- delete [] TexCoordAry;
3475-}
3476-
3477-//! @brief 線を描画
3478-//! @param x1 始点の x座標
3479-//! @param y1 始点の y座標
3480-//! @param x2 終点の x座標
3481-//! @param y2 終点の y座標
3482-//! @param color 色
3483-void D3DGraphics::Draw2DLine(int x1, int y1, int x2, int y2, int color)
3484-{
3485- float VertexAry[2*2];
3486- unsigned char ColorAry[2*4];
3487-
3488- //2D描画用設定を適用
3489- Start2DRender();
3490-
3491- //テクスチャ無効
3492- glDisable(GL_TEXTURE_2D);
3493-
3494- //頂点配列を用意
3495- VertexAry[0] = (float)x1; VertexAry[1] = (float)y1;
3496- VertexAry[2] = (float)x2; VertexAry[3] = (float)y2;
3497-
3498- //色情報配列を用意
3499- ColorAry[0] = (color>>24)&0xFF;
3500- ColorAry[1] = (color>>16)&0xFF;
3501- ColorAry[2] = (color>>8)&0xFF;
3502- ColorAry[3] = color&0xFF;
3503- memcpy(&(ColorAry[4]), ColorAry, sizeof(unsigned char)*4);
3504-
3505- //配列有効化
3506- glEnableClientState(GL_VERTEX_ARRAY);
3507- glEnableClientState(GL_COLOR_ARRAY);
3508-
3509- //描画
3510- glVertexPointer(2, GL_FLOAT, 0, VertexAry);
3511- glColorPointer(4, GL_UNSIGNED_BYTE, 0, ColorAry);
3512- glDrawArrays(GL_LINE_STRIP, 0, 2);
3513-
3514- //配列無効化
3515- glDisableClientState(GL_VERTEX_ARRAY);
3516- glDisableClientState(GL_COLOR_ARRAY);
3517-
3518- //2D描画用設定を解除
3519- End2DRender();
3520-}
3521-
3522-//! @brief 円(16角形)を描画
3523-//! @param x 中心の x座標
3524-//! @param y 中心の y座標
3525-//! @param r 半径
3526-//! @param color 色
3527-void D3DGraphics::Draw2DCycle(int x, int y, int r, int color)
3528-{
3529- float VertexAry[(16+1)*2];
3530- unsigned char ColorAry[(16+1)*4];
3531-
3532- //2D描画用設定を適用
3533- Start2DRender();
3534-
3535- //テクスチャ無効
3536- glDisable(GL_TEXTURE_2D);
3537-
3538- //頂点座標を設定
3539- for(int i=0; i<16+1; i++){
3540- float x2, y2;
3541- x2 = (float)x + cos((float)M_PI*2/16 * i) * r;
3542- y2 = (float)y + sin((float)M_PI*2/16 * i) * r;
3543- VertexAry[i*2] = x2; VertexAry[i*2+1] = y2;
3544- }
3545-
3546- //色情報配列を用意
3547- ColorAry[0] = (color>>24)&0xFF;
3548- ColorAry[1] = (color>>16)&0xFF;
3549- ColorAry[2] = (color>>8)&0xFF;
3550- ColorAry[3] = color&0xFF;
3551- for(int i=1; i<16+1; i++){
3552- memcpy(&(ColorAry[i*4]), ColorAry, sizeof(unsigned char)*4);
3553- }
3554-
3555- //配列有効化
3556- glEnableClientState(GL_VERTEX_ARRAY);
3557- glEnableClientState(GL_COLOR_ARRAY);
3558-
3559- //描画
3560- glVertexPointer(2, GL_FLOAT, 0, VertexAry);
3561- glColorPointer(4, GL_UNSIGNED_BYTE, 0, ColorAry);
3562- glDrawArrays(GL_LINE_STRIP, 0, 16+1);
3563-
3564- //配列無効化
3565- glDisableClientState(GL_VERTEX_ARRAY);
3566- glDisableClientState(GL_COLOR_ARRAY);
3567-
3568- //2D描画用設定を解除
3569- End2DRender();
3570-}
3571-
3572-//! @brief 四角形を描画
3573-//! @param x1 左上の x座標
3574-//! @param y1 左上の y座標
3575-//! @param x2 右下の x座標
3576-//! @param y2 右下の y座標
3577-//! @param color 色
3578-void D3DGraphics::Draw2DBox(int x1, int y1, int x2, int y2, int color)
3579-{
3580- float VertexAry[4*2];
3581- unsigned char ColorAry[4*4];
3582-
3583- //2D描画用設定を適用
3584- Start2DRender();
3585-
3586- //テクスチャ無効
3587- glDisable(GL_TEXTURE_2D);
3588-
3589- //頂点配列を用意
3590- VertexAry[0] = (float)x1; VertexAry[1] = (float)y1;
3591- VertexAry[2] = (float)x2; VertexAry[3] = (float)y1;
3592- VertexAry[4] = (float)x1; VertexAry[5] = (float)y2;
3593- VertexAry[6] = (float)x2; VertexAry[7] = (float)y2;
3594-
3595- //色情報配列を用意
3596- ColorAry[0] = (color>>24)&0xFF;
3597- ColorAry[1] = (color>>16)&0xFF;
3598- ColorAry[2] = (color>>8)&0xFF;
3599- ColorAry[3] = color&0xFF;
3600- for(int i=1; i<4; i++){
3601- memcpy(&(ColorAry[i*4]), ColorAry, sizeof(unsigned char)*4);
3602- }
3603-
3604- //配列有効化
3605- glEnableClientState(GL_VERTEX_ARRAY);
3606- glEnableClientState(GL_COLOR_ARRAY);
3607-
3608- //描画
3609- glVertexPointer(2, GL_FLOAT, 0, VertexAry);
3610- glColorPointer(4, GL_UNSIGNED_BYTE, 0, ColorAry);
3611- glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
3612-
3613- //配列無効化
3614- glDisableClientState(GL_VERTEX_ARRAY);
3615- glDisableClientState(GL_COLOR_ARRAY);
3616-
3617- //2D描画用設定を解除
3618- End2DRender();
3619-}
3620-
3621-//! @brief 画像を描画
3622-//! @param x x座標
3623-//! @param y y座標
3624-//! @param id テクスチャ認識番号
3625-//! @param width 幅
3626-//! @param height 高さ
3627-//! @param alpha 透明度(0.0〜1.0)
3628-void D3DGraphics::Draw2DTexture(int x, int y, int id, int width, int height, float alpha)
3629-{
3630- //無効なテクスチャ番号を指定されていれば処理しない
3631- if( id == -1 ){ return; }
3632-
3633- float VertexAry[4*2];
3634- float ColorAry[4*4];
3635- float TexCoordAry[4*2];
3636-
3637- //2D描画用設定を適用
3638- Start2DRender();
3639-
3640- //テクスチャ有効
3641- glEnable(GL_TEXTURE_2D);
3642-
3643- //テクスチャをセット
3644- SetTexture(id);
3645-
3646- //頂点配列を用意
3647- VertexAry[0] = (float)x; VertexAry[1] = (float)y;
3648- VertexAry[2] = (float)x+width; VertexAry[3] = (float)y;
3649- VertexAry[4] = (float)x; VertexAry[5] = (float)y+height;
3650- VertexAry[6] = (float)x+width; VertexAry[7] = (float)y+height;
3651-
3652- //色情報配列を用意
3653- ColorAry[0] = 1.0f;
3654- ColorAry[1] = 1.0f;
3655- ColorAry[2] = 1.0f;
3656- ColorAry[3] = alpha;
3657- for(int i=1; i<4; i++){
3658- memcpy(&(ColorAry[i*4]), ColorAry, sizeof(float)*4);
3659- }
3660-
3661- //UV座標配列を用意
3662- TexCoordAry[0] = 0.0f; TexCoordAry[1] = 0.0f;
3663- TexCoordAry[2] = 1.0f; TexCoordAry[3] = 0.0f;
3664- TexCoordAry[4] = 0.0f; TexCoordAry[5] = 1.0f;
3665- TexCoordAry[6] = 1.0f; TexCoordAry[7] = 1.0f;
3666-
3667- //配列有効化
3668- glEnableClientState(GL_VERTEX_ARRAY);
3669- glEnableClientState(GL_COLOR_ARRAY);
3670- glEnableClientState(GL_TEXTURE_COORD_ARRAY);
3671-
3672- //描画
3673- glVertexPointer(2, GL_FLOAT, 0, VertexAry);
3674- glColorPointer(4, GL_FLOAT, 0, ColorAry);
3675- glTexCoordPointer(2, GL_FLOAT, 0, TexCoordAry);
3676- glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
3677-
3678- //配列無効化
3679- glDisableClientState(GL_VERTEX_ARRAY);
3680- glDisableClientState(GL_COLOR_ARRAY);
3681- glDisableClientState(GL_TEXTURE_COORD_ARRAY);
3682-
3683- //2D描画用設定を解除
3684- End2DRender();
3685-}
3686-
3687-//! @brief 2D描画用設定を解除
3688-void D3DGraphics::End2DRender()
3689-{
3690- glMatrixMode(GL_PROJECTION);
3691- glLoadIdentity();
3692- glPopMatrix();
3693- glMatrixMode(GL_MODELVIEW);
3694- glLoadIdentity();
3695- glPopMatrix();
3696-
3697- glEnable(GL_DEPTH_TEST);
3698- glEnable(GL_CULL_FACE);
3699-}
3700-
3701-//! @brief 画面のスクリーンショットを保存
3702-//! @param filename ファイル名
3703-//! @return 成功:true 失敗:false
3704-bool D3DGraphics::SaveScreenShot(char* filename)
3705-{
3706- return false;
3707-}
3708-
3709-//! @brief カラーコードを取得
3710-//! @param red 赤(0.0f〜1.0f)
3711-//! @param green 緑(0.0f〜1.0f)
3712-//! @param blue 青(0.0f〜1.0f)
3713-//! @param alpha 透明度(0.0f〜1.0f)
3714-//! @return カラーコード
3715-int D3DGraphics::GetColorCode(float red, float green, float blue, float alpha)
3716-{
3717- unsigned char red2, green2, blue2, alpha2;
3718- red2 = (unsigned char)(red*255);
3719- green2 = (unsigned char)(green*255);
3720- blue2 = (unsigned char)(blue*255);
3721- alpha2 = (unsigned char)(alpha*255);
3722-
3723- return (red2 << 24) | (green2 << 16) | (blue2 << 8) | alpha2;
3724-}
3725-
3726-#endif //GRAPHICS_OPENGL
\ No newline at end of file
--- trunk/d3dgraphics-directx.cpp (nonexistent)
+++ trunk/d3dgraphics-directx.cpp (revision 95)
@@ -0,0 +1,1619 @@
1+//! @file d3dgraphics-directx.cpp
2+//! @brief D3DGraphicsクラスの定義(DirectX版)
3+
4+//--------------------------------------------------------------------------------
5+//
6+// OpenXOPS
7+// Copyright (c) 2014-2015, OpenXOPS Project / [-_-;](mikan) All rights reserved.
8+//
9+// Redistribution and use in source and binary forms, with or without
10+// modification, are permitted provided that the following conditions are met:
11+// * Redistributions of source code must retain the above copyright notice,
12+// this list of conditions and the following disclaimer.
13+// * Redistributions in binary form must reproduce the above copyright notice,
14+// this list of conditions and the following disclaimer in the documentation
15+// and/or other materials provided with the distribution.
16+// * Neither the name of the OpenXOPS Project nor the names of its contributors
17+// may be used to endorse or promote products derived from this software
18+// without specific prior written permission.
19+//
20+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+// DISCLAIMED. IN NO EVENT SHALL OpenXOPS Project BE LIABLE FOR ANY
24+// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26+// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27+// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+//--------------------------------------------------------------------------------
31+
32+#include "d3dgraphics.h"
33+
34+#ifndef GRAPHICS_OPENGL
35+
36+//! @brief コンストラクタ
37+D3DGraphics::D3DGraphics()
38+{
39+ pD3D = NULL;
40+ pd3dDevice = NULL;
41+ aspect = 1.0f;
42+ fullscreenflag = false;
43+ for(int i=0; i<MAX_MODEL; i++){
44+ pmesh[i] = NULL;
45+ }
46+ for(int i=0; i<MAX_TEXTURE; i++){
47+ ptextures[i] = NULL;
48+ }
49+
50+ blockdata = NULL;
51+ for(int i=0; i<TOTAL_BLOCKTEXTURE; i++){
52+ mapTextureID[i] = -1;
53+ }
54+
55+ StartRenderFlag = false;
56+
57+ //ptextsprite = NULL;
58+ pxmsfont = NULL;
59+ TextureFont = -1;
60+}
61+
62+//! @brief ディストラクタ
63+D3DGraphics::~D3DGraphics()
64+{
65+ //リソース解放
66+ CleanupD3Dresource();
67+
68+ if( pd3dDevice != NULL ) pd3dDevice->Release();
69+ if( pD3D != NULL ) pD3D->Release();
70+
71+#ifdef ENABLE_DEBUGLOG
72+ //ログに出力
73+ OutputLog.WriteLog(LOG_CLEANUP, "グラフィック", "DirectX");
74+#endif
75+}
76+
77+//! @brief 初期化@n
78+//! (DirectX 9)
79+//! @param WindowCtrl WindowControlクラスのポインタ
80+//! @param TextureFontFilename 使用するテクスチャフォントのファイル名
81+//! @param fullscreen false:ウィンドウ表示 true:フルスクリーン用表示
82+//! @return 成功:0 失敗:1
83+int D3DGraphics::InitD3D(WindowControl *WindowCtrl, char *TextureFontFilename, bool fullscreen)
84+{
85+#ifdef ENABLE_DEBUGLOG
86+ //ログに出力
87+ OutputLog.WriteLog(LOG_INIT, "グラフィック", "DirectX");
88+#endif
89+
90+ D3DPRESENT_PARAMETERS d3dpp;
91+ RECT rec;
92+
93+ GetClientRect(WindowCtrl->GethWnd(), &rec);
94+
95+ fullscreenflag = fullscreen;
96+
97+ //D3D9の作成
98+ pD3D = Direct3DCreate9(D3D_SDK_VERSION);
99+ if( pD3D == NULL ){
100+ return 1;
101+ }
102+
103+ //D3Dデバイスの作成
104+ ZeroMemory(&d3dpp, sizeof(d3dpp));
105+ if( fullscreenflag == false ){
106+ d3dpp.Windowed = TRUE;
107+ d3dpp.BackBufferWidth = rec.right;
108+ d3dpp.BackBufferHeight = rec.bottom;
109+ d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
110+ d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
111+ d3dpp.EnableAutoDepthStencil = TRUE;
112+ d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
113+ d3dpp.FullScreen_RefreshRateInHz = 0;
114+ }
115+ else{
116+ D3DDISPLAYMODE dispmode;
117+ pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &dispmode);
118+
119+ d3dpp.Windowed = FALSE;
120+ d3dpp.BackBufferWidth = rec.right;
121+ d3dpp.BackBufferHeight = rec.bottom;
122+ d3dpp.BackBufferFormat = dispmode.Format;
123+ d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
124+ d3dpp.EnableAutoDepthStencil = TRUE;
125+ d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
126+ d3dpp.FullScreen_RefreshRateInHz = dispmode.RefreshRate;
127+ }
128+
129+ if( FAILED( pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, WindowCtrl->GethWnd(), D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &pd3dDevice) ) ){
130+ if( FAILED( pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, WindowCtrl->GethWnd(), D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pd3dDevice) ) ){
131+ return 1;
132+ }
133+ }
134+
135+#ifdef ENABLE_DEBUGLOG
136+ //ログに出力
137+ OutputLog.WriteLog(LOG_COMPLETE, "", "");
138+#endif
139+
140+ //テクスチャフォント用画像のファイル名を設定
141+ strcpy(TextureFontFname, TextureFontFilename);
142+
143+ //描画関係の詳細な設定
144+ if( InitSubset() != 0){
145+ return 1;
146+ }
147+
148+
149+ //アスペクト比を設定
150+ aspect = (float)rec.right / (float)rec.bottom;
151+
152+ //マウスカーソルを消す
153+ //ShowCursor(FALSE);
154+
155+
156+ float aspecth, prx, pry, r;
157+ aspecth = (float)SCREEN_WIDTH/SCREEN_HEIGHT;
158+
159+ //HUD_myweapon [奥行き, 縦, 横]
160+
161+ //HUD_A 現在持っている武器を表示する座標
162+ prx = DegreeToRadian(-39) * aspecth /2;
163+ pry = DegreeToRadian(-55) /2;
164+ r = 7.5f;
165+ HUD_myweapon_x[0] = cos(pry)*r;
166+ HUD_myweapon_y[0] = sin(pry)*r;
167+ HUD_myweapon_z[0] = sin(prx)*r;
168+
169+ //HUD_A 予備の武器を表示する座標
170+ prx = DegreeToRadian(-52) * aspecth /2;
171+ pry = DegreeToRadian(-60) /2;
172+ r = 16.0f;
173+ HUD_myweapon_x[1] = cos(pry)*r;
174+ HUD_myweapon_y[1] = sin(pry)*r;
175+ HUD_myweapon_z[1] = sin(prx)*r;
176+
177+ return 0;
178+}
179+
180+//! @brief リセット@n
181+//! (ウィンドウ最小化からの復帰 など)
182+//! @param WindowCtrl WindowControlクラスのポインタ
183+//! @return 成功:0 待ち:1 失敗:2
184+int D3DGraphics::ResetD3D(WindowControl *WindowCtrl)
185+{
186+ //フォーカスを失っているなら待たせる
187+ if( pd3dDevice->TestCooperativeLevel() == D3DERR_DEVICELOST ){
188+ return 1;
189+ }
190+
191+#ifdef ENABLE_DEBUGLOG
192+ //ログに出力
193+ OutputLog.WriteLog(LOG_INIT, "グラフィック", "DirectX(リセット)");
194+#endif
195+
196+ //リソース解放
197+ CleanupD3Dresource();
198+
199+ D3DPRESENT_PARAMETERS d3dpp;
200+ RECT rec;
201+
202+ GetClientRect(WindowCtrl->GethWnd(), &rec);
203+
204+ //D3Dデバイスの作成
205+ ZeroMemory(&d3dpp, sizeof(d3dpp));
206+ if( fullscreenflag == false ){
207+ d3dpp.Windowed = TRUE;
208+ d3dpp.BackBufferWidth = rec.right;
209+ d3dpp.BackBufferHeight = rec.bottom;
210+ d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
211+ d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
212+ d3dpp.EnableAutoDepthStencil = TRUE;
213+ d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
214+ d3dpp.FullScreen_RefreshRateInHz = 0;
215+ }
216+ else{
217+ D3DDISPLAYMODE dispmode;
218+ pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &dispmode);
219+
220+ d3dpp.Windowed = FALSE;
221+ d3dpp.BackBufferWidth = rec.right;
222+ d3dpp.BackBufferHeight = rec.bottom;
223+ d3dpp.BackBufferFormat = dispmode.Format;
224+ d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
225+ d3dpp.EnableAutoDepthStencil = TRUE;
226+ d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
227+ d3dpp.FullScreen_RefreshRateInHz = dispmode.RefreshRate;
228+ }
229+
230+ if( FAILED( pd3dDevice->Reset(&d3dpp) ) ){
231+ return 2;
232+ }
233+
234+ //描画関係の詳細な設定
235+ if( InitSubset() != 0){
236+ return 2;
237+ }
238+
239+#ifdef ENABLE_DEBUGLOG
240+ //ログに出力
241+ OutputLog.WriteLog(LOG_COMPLETE, "", "");
242+#endif
243+
244+ return 0;
245+}
246+
247+//! @brief 描画関係の細部設定
248+//! @attention 初期化時に1度だけ実行してください。
249+int D3DGraphics::InitSubset()
250+{
251+ //ライト
252+ //pd3dDevice->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_ARGB(0,255,255,255) );
253+ pd3dDevice->LightEnable(0, FALSE);
254+ pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
255+
256+ //フォグ
257+ float fog_st = 100;
258+ float fog_end = 800;
259+ pd3dDevice->SetRenderState(D3DRS_FOGENABLE, TRUE);
260+ pd3dDevice->SetRenderState(D3DRS_FOGCOLOR, D3DCOLOR_RGBA(0, 0, 0, 0));
261+ pd3dDevice->SetRenderState(D3DRS_FOGTABLEMODE, D3DFOG_LINEAR);
262+ pd3dDevice->SetRenderState(D3DRS_FOGVERTEXMODE, D3DFOG_NONE);
263+ pd3dDevice->SetRenderState(D3DRS_FOGSTART,*(DWORD*)(&fog_st));
264+ pd3dDevice->SetRenderState(D3DRS_FOGEND, *(DWORD*)(&fog_end));
265+
266+ // テクスチャフィルタを使う
267+ pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
268+ pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
269+ pd3dDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
270+
271+ //ミップマップの詳細レベル (LOD) バイアスを指定する。
272+ float LODBias = -0.2f;
273+ pd3dDevice->SetSamplerState(0, D3DSAMP_MIPMAPLODBIAS, *((LPDWORD)(&LODBias)) );
274+
275+ //アルファ・ブレンディングを行う
276+ pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
277+
278+ //透過処理を行う
279+ pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
280+ pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
281+
282+ //アルファテストに対応しているかチェック
283+ D3DCAPS9 Caps;
284+ pd3dDevice->GetDeviceCaps(&Caps);
285+ if( Caps.AlphaCmpCaps & D3DPCMPCAPS_GREATEREQUAL ){
286+ //アルファテスト設定
287+ // 完全に透明なピクセルは描画しない
288+ pd3dDevice->SetRenderState(D3DRS_ALPHAREF, (DWORD)0x00000001);
289+ pd3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);
290+ pd3dDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL);
291+ }
292+
293+ //深度バッファ比較関数
294+ pd3dDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
295+ pd3dDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESS);
296+
297+ //ポリゴンの裏・表
298+ pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
299+
300+
301+ //テキストスプライト初期化
302+ if( FAILED( D3DXCreateSprite( pd3dDevice, &ptextsprite ) ) ){
303+ return 1;
304+ }
305+ //フォント名:MS ゴシック サイズ:18
306+ HRESULT hr = D3DXCreateFont( pd3dDevice, -18, 0, FW_NORMAL, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
307+ DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "MS ゴシック", &pxmsfont);
308+ if( FAILED(hr) ) return 1;
309+
310+ //テクスチャフォント用画像を取得
311+ TextureFont = LoadTexture(TextureFontFname, true, false);
312+ return 0;
313+}
314+
315+//! @brief デバイスのリソースを解放
316+void D3DGraphics::CleanupD3Dresource()
317+{
318+ if( TextureFont != -1 ){ CleanupTexture(TextureFont); }
319+ if( pxmsfont != NULL ){
320+ pxmsfont->Release();
321+ pxmsfont = NULL;
322+ }
323+ if( ptextsprite != NULL ){ ptextsprite->Release(); }
324+
325+ CleanupMapdata();
326+
327+ for(int i=0; i<MAX_MODEL; i++){
328+ CleanupModel(i);
329+ }
330+ for(int i=0; i<MAX_TEXTURE; i++){
331+ CleanupTexture(i);
332+ }
333+}
334+
335+//! @brief モデルファイルを読み込む(.x)
336+//! @param filename ファイル名
337+//! @return 成功:モデル認識番号(0以上) 失敗:-1
338+int D3DGraphics::LoadModel(char* filename)
339+{
340+#ifdef ENABLE_DEBUGLOG
341+ //ログに出力
342+ OutputLog.WriteLog(LOG_LOAD, "モデル", filename);
343+#endif
344+
345+ int id = -1;
346+
347+ //空いている要素を探す
348+ for(int i=0; i<MAX_MODEL; i++){
349+ if( pmesh[i] == NULL ){
350+ id = i;
351+ break;
352+ }
353+ }
354+ if( id == -1 ){ return -1; }
355+
356+ LPD3DXBUFFER pD3DXMtrlBuffer;
357+
358+#ifdef PATH_DELIMITER_SLASH
359+ //パス区切り文字を変換
360+ filename = ChangePathDelimiter(filename);
361+#endif
362+
363+ //.xファイルをバッファーに読み込む
364+ if( FAILED( D3DXLoadMeshFromX( filename, D3DXMESH_SYSTEMMEM, pd3dDevice, NULL,
365+ &pD3DXMtrlBuffer, NULL, &nummaterials[id], &pmesh[id] ) ) ) {
366+ return -1;
367+ }
368+
369+ //マテリアル情報を取得
370+ D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();
371+ int num = nummaterials[id];
372+ pmaterials[id] = new D3DMATERIAL9[num];
373+ if( pmaterials[id] == NULL ) return -1;
374+
375+ //構造体に代入
376+ for( int i=0; i<num; i=i+1 ){
377+ pmaterials[id][i] = d3dxMaterials[i].MatD3D;
378+ pmaterials[id][i].Ambient = pmaterials[id][i].Diffuse;
379+ }
380+
381+ //バッファを開放
382+ pD3DXMtrlBuffer->Release();
383+
384+#ifdef ENABLE_DEBUGLOG
385+ //ログに出力
386+ OutputLog.WriteLog(LOG_COMPLETE, "", id);
387+#endif
388+ return id;
389+}
390+
391+//! @brief モデルファイルの中間データを作成(モーフィング)
392+//! @param idA モデルAの認識番号
393+//! @param idB モデルBの認識番号
394+//! @return 成功:新しいモデル認識番号(0以上) 失敗:-1
395+//! @attention モデルAとモデルBは、頂点数・ポリゴン数・インデックスが同じである必要があります。
396+//! @attention それぞれのモデルデータが正しくないか 頂点数が異なる場合、実行に失敗します。
397+int D3DGraphics::MorphingModel(int idA, int idB)
398+{
399+#ifdef ENABLE_DEBUGLOG
400+ char str[128];
401+ sprintf(str, "中間データ作成  ID:%d and %d", idA, idB);
402+
403+ //ログに出力
404+ OutputLog.WriteLog(LOG_LOAD, "モデル", str);
405+#endif
406+
407+ //データが正しいか調べる
408+ if( (idA < 0)||((MAX_MODEL -1) < idA) ){ return -1; }
409+ if( pmesh[idA] == NULL ){ return -1; }
410+ if( (idB < 0)||((MAX_MODEL -1) < idB) ){ return -1; }
411+ if( pmesh[idB] == NULL ){ return -1; }
412+
413+ int idN = -1;
414+ int numvA, numvB;
415+ LPDIRECT3DVERTEXBUFFER9 pvbA, pvbB, pvbN;
416+ D3DXVECTOR3 *pVerticesA, *pVerticesB, *pVerticesN;
417+ int FVFsize;
418+
419+ //空いている要素を探す
420+ for(int i=0; i<MAX_MODEL; i++){
421+ if( pmesh[i] == NULL ){
422+ idN = i;
423+ break;
424+ }
425+ }
426+ if( idN == -1 ){ return -1; }
427+
428+ //頂点数を取得
429+ numvA = pmesh[idA]->GetNumVertices();
430+ numvB = pmesh[idB]->GetNumVertices();
431+
432+ //頂点数が同じかどうか調べる
433+ if( numvA != numvB ){ return -1; }
434+
435+ //頂点データをコピー(実質的に領域確保用のダミー)
436+ if( pmesh[idA]->CloneMeshFVF(pmesh[idA]->GetOptions(), pmesh[idA]->GetFVF(), pd3dDevice, &pmesh[idN]) != D3D_OK ){
437+ return -1;
438+ }
439+
440+ //マテリアル情報をコピー
441+ int num = nummaterials[idA];
442+ nummaterials[idN] = nummaterials[idA];
443+ pmaterials[idN] = new D3DMATERIAL9[num];
444+ if( pmaterials[idN] == NULL ) return -1;
445+ for( int i=0; i<num; i=i+1 ){
446+ pmaterials[idN][i] = pmaterials[idA][i];
447+ }
448+
449+ //バッファーを取得
450+ pmesh[idA]->GetVertexBuffer(&pvbA);
451+ pmesh[idB]->GetVertexBuffer(&pvbB);
452+ pmesh[idN]->GetVertexBuffer(&pvbN);
453+
454+ //1頂点当たりのバイト数取得
455+ FVFsize = D3DXGetFVFVertexSize(pmesh[idN]->GetFVF());
456+
457+ //各頂点を読み出し計算
458+ for(int i=0; i<numvA; i++){
459+ pvbA->Lock(i*FVFsize, sizeof(D3DXVECTOR3), (void**)&pVerticesA, D3DLOCK_READONLY);
460+ pvbB->Lock(i*FVFsize, sizeof(D3DXVECTOR3), (void**)&pVerticesB, D3DLOCK_READONLY);
461+ pvbN->Lock(i*FVFsize, sizeof(D3DXVECTOR3), (void**)&pVerticesN, 0);
462+
463+ //平均化
464+ pVerticesN->x = (pVerticesA->x + pVerticesB->x)/2;
465+ pVerticesN->y = (pVerticesA->y + pVerticesB->y)/2;
466+ pVerticesN->z = (pVerticesA->z + pVerticesB->z)/2;
467+
468+ pvbA->Unlock();
469+ pvbB->Unlock();
470+ pvbN->Unlock();
471+ }
472+
473+#ifdef ENABLE_DEBUGLOG
474+ //ログに出力
475+ OutputLog.WriteLog(LOG_COMPLETE, "", idN);
476+#endif
477+ return idN;
478+}
479+
480+//! @brief モデルファイルを解放
481+//! @param id モデル認識番号
482+void D3DGraphics::CleanupModel(int id)
483+{
484+ if( (id < 0)||((MAX_MODEL -1) < id) ){ return; }
485+ if( pmesh[id] != NULL ){
486+ delete [] pmaterials[id];
487+
488+ pmesh[id]->Release();
489+ pmesh[id] = NULL;
490+
491+#ifdef ENABLE_DEBUGLOG
492+ //ログに出力
493+ OutputLog.WriteLog(LOG_CLEANUP, "モデル", id);
494+#endif
495+ }
496+}
497+
498+//! @brief テクスチャを読み込む
499+//! @param filename ファイル名
500+//! @param texturefont テクスチャフォントフラグ
501+//! @param BlackTransparent 黒を透過する
502+//! @return 成功:テクスチャ認識番号(0以上) 失敗:-1
503+int D3DGraphics::LoadTexture(char* filename, bool texturefont, bool BlackTransparent)
504+{
505+ int id = -1;
506+ D3DXIMAGE_INFO info;
507+ int MipLevels;
508+
509+#ifdef ENABLE_DEBUGLOG
510+ //ログに出力
511+ OutputLog.WriteLog(LOG_LOAD, "テクスチャ", filename);
512+#endif
513+
514+ //空いている認識番号を探す
515+ for(int i=0; i<MAX_TEXTURE; i++){
516+ if( ptextures[i] == NULL ){
517+ id = i;
518+ break;
519+ }
520+ }
521+ if( id == -1 ){ return -1; }
522+
523+#ifdef PATH_DELIMITER_SLASH
524+ //パス区切り文字を変換
525+ filename = ChangePathDelimiter(filename);
526+#endif
527+
528+ //ファイル情報を取得
529+ if( D3DXGetImageInfoFromFile(filename, &info) != D3D_OK ){ return -1; }
530+
531+ //ミップマップレベルを設定
532+ if( texturefont == true ){
533+ MipLevels = 1;
534+ }
535+ else{
536+ MipLevels = 4;//D3DX_DEFAULT;
537+ }
538+
539+ //テクスチャを読み込む
540+ if( BlackTransparent == false ){
541+ if( FAILED( D3DXCreateTextureFromFileEx(pd3dDevice, filename, info.Width, info.Height, MipLevels, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0x00000000, NULL, NULL, &ptextures[id]) ) ) {
542+ return -1;
543+ }
544+ }
545+ else{
546+ if( FAILED( D3DXCreateTextureFromFileEx(pd3dDevice, filename, info.Width, info.Height, MipLevels, 0, D3DFMT_A1R5G5B5, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_ARGB(255, 0, 0, 0), NULL, NULL, &ptextures[id]) ) ) {
547+ return -1;
548+ }
549+ }
550+
551+#ifdef ENABLE_DEBUGLOG
552+ //ログに出力
553+ OutputLog.WriteLog(LOG_COMPLETE, "", id);
554+#endif
555+ return id;
556+}
557+
558+//! @brief テクスチャのサイズを取得
559+//! @param id テクスチャ認識番号
560+//! @param width 幅を受け取るポインタ
561+//! @param height 高さを受け取るポインタ
562+//! @return 成功:0 失敗:1
563+//! @attention サーフェイスのサイズを取得します。GPUにロードされたサイズであり、テクスチャ(現物)と異なる場合があります。
564+int D3DGraphics::GetTextureSize(int id, int *width, int *height)
565+{
566+ //無効な認識番号が指定されていたら、処理せず返す。
567+ if( id == -1 ){ return 1; }
568+ if( ptextures[id] == NULL ){ return 1; }
569+
570+ IDirect3DSurface9 *surface;
571+ D3DSURFACE_DESC desc;
572+
573+ //サーフェイスを取得
574+ ptextures[id]->GetSurfaceLevel(0, &surface);
575+
576+ //幅と高さを取得
577+ surface->GetDesc(&desc);
578+ *width = desc.Width;
579+ *height = desc.Height;
580+
581+ //サーフェイスを開放
582+ surface->Release();
583+
584+ return 0;
585+}
586+
587+//! @brief テクスチャを解放
588+//! @param id テクスチャ認識番号
589+void D3DGraphics::CleanupTexture(int id)
590+{
591+ if( (id < 0)||((MAX_TEXTURE -1) < id) ){ return; }
592+ if( ptextures[id] != NULL ){
593+ ptextures[id]->Release();
594+ ptextures[id] = NULL;
595+
596+#ifdef ENABLE_DEBUGLOG
597+ //ログに出力
598+ OutputLog.WriteLog(LOG_CLEANUP, "テクスチャ", id);
599+#endif
600+ }
601+}
602+
603+//! @brief 全ての描画処理を開始
604+//! @return 成功:0 失敗:1
605+//! @attention 描画処理の最初に呼び出す必要があります。
606+int D3DGraphics::StartRender()
607+{
608+ if( StartRenderFlag == true ){ return 1; }
609+
610+ //領域を初期化
611+ pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
612+
613+ if( SUCCEEDED( pd3dDevice->BeginScene() ) ){
614+ //Zバッファ初期化
615+ pd3dDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
616+
617+ //座標ゼロ地点にワールド変換行列
618+ ResetWorldTransform();
619+
620+ //描画中のフラグを立てる
621+ StartRenderFlag = true;
622+ return 0;
623+ }
624+
625+ return 1;
626+}
627+
628+//! @brief 全ての描画処理を終了
629+//! @return 成功:false 失敗:true
630+//! @attention 描画処理の最後に呼び出す必要があります。
631+bool D3DGraphics::EndRender()
632+{
633+ //描画中なら終了
634+ if( StartRenderFlag == true ){
635+ pd3dDevice->EndScene();
636+ }
637+
638+ HRESULT hr = pd3dDevice->Present(NULL, NULL, NULL, NULL);
639+
640+ //フラグを false に
641+ StartRenderFlag = false;
642+
643+ if( hr == D3DERR_DEVICELOST ){
644+ return true;
645+ }
646+ return false;
647+}
648+
649+//! @brief Zバッファをリセット
650+void D3DGraphics::ResetZbuffer()
651+{
652+ //Zバッファを一度無効にし、初期化後、再度有効に
653+ pd3dDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
654+ pd3dDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
655+ pd3dDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
656+}
657+
658+//! @brief ワールド空間を原点(0,0,0)に戻す など
659+void D3DGraphics::ResetWorldTransform()
660+{
661+ D3DXMATRIX matWorld;
662+ D3DXMatrixIdentity(&matWorld);
663+ pd3dDevice->SetTransform(D3DTS_WORLD, &matWorld);
664+}
665+
666+//! @brief ワールド空間の座標・角度・拡大率を設定
667+//! @param x X座標
668+//! @param y Y座標
669+//! @param z Z座標
670+//! @param rx 横軸角度
671+//! @param ry 縦軸角度
672+//! @param size 拡大率
673+void D3DGraphics::SetWorldTransform(float x, float y, float z, float rx, float ry, float size)
674+{
675+ SetWorldTransform(x, y, z, rx, ry, 0.0f, size);
676+}
677+
678+//! @brief ワールド空間の座標・角度・拡大率を設定
679+//! @param x X座標
680+//! @param y Y座標
681+//! @param z Z座標
682+//! @param rx 横軸角度
683+//! @param ry1 縦軸角度
684+//! @param ry2 縦軸角度
685+//! @param size 拡大率
686+void D3DGraphics::SetWorldTransform(float x, float y, float z, float rx, float ry1, float ry2, float size)
687+{
688+ D3DXMATRIX matWorld;
689+ D3DXMATRIX matWorld1, matWorld2, matWorld3, matWorld4, matWorld5;
690+
691+ //行列を作成
692+ D3DXMatrixTranslation(&matWorld1, x, y, z);
693+ D3DXMatrixRotationY(&matWorld2, rx);
694+ D3DXMatrixRotationX(&matWorld3, ry1);
695+ D3DXMatrixRotationZ(&matWorld4, ry2);
696+ D3DXMatrixScaling(&matWorld5, size, size, size);
697+
698+ //計算
699+ matWorld = matWorld5 * matWorld4 * matWorld3 * matWorld2 * matWorld1;
700+
701+ //適用
702+ pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
703+}
704+
705+//! @brief ワールド空間の座標・角度・拡大率を設定(エフェクト用)
706+//! @param x X座標
707+//! @param y Y座標
708+//! @param z Z座標
709+//! @param rx 横軸角度
710+//! @param ry 縦軸角度
711+//! @param rt 回転角度
712+//! @param size 拡大率
713+void D3DGraphics::SetWorldTransformEffect(float x, float y, float z, float rx, float ry, float rt, float size)
714+{
715+ D3DXMATRIX matWorld;
716+ D3DXMATRIX matWorld1, matWorld2, matWorld3, matWorld4, matWorld5;
717+
718+ //行列を作成
719+ D3DXMatrixTranslation(&matWorld1, x, y, z);
720+ D3DXMatrixRotationY(&matWorld2, rx);
721+ D3DXMatrixRotationZ(&matWorld3, ry);
722+ D3DXMatrixRotationX(&matWorld4, rt);
723+ D3DXMatrixScaling(&matWorld5, size, size, size);
724+
725+ //計算
726+ matWorld = matWorld5 * matWorld4 * matWorld3 * matWorld2 * matWorld1;
727+
728+ //適用
729+ pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
730+}
731+
732+//! @brief ワールド空間を人が武器を持つ場所に設定
733+//! @param x X座標
734+//! @param y Y座標
735+//! @param z Z座標
736+//! @param mx 手元を原点にした モデルのX座標
737+//! @param my 手元を原点にした モデルのY座標
738+//! @param mz 手元を原点にした モデルのZ座標
739+//! @param rx 横軸角度
740+//! @param ry 縦軸角度
741+//! @param size 拡大率
742+void D3DGraphics::SetWorldTransformHumanWeapon(float x, float y, float z, float mx, float my, float mz, float rx, float ry, float size)
743+{
744+ D3DXMATRIX matWorld;
745+ D3DXMATRIX matWorld1, matWorld2, matWorld3, matWorld4, matWorld5;
746+
747+ //行列を作成
748+ D3DXMatrixTranslation(&matWorld1, x, y, z);
749+ D3DXMatrixRotationY(&matWorld2, rx);
750+ D3DXMatrixRotationX(&matWorld3, ry);
751+ D3DXMatrixTranslation(&matWorld4, mx, my, mz);
752+ D3DXMatrixScaling(&matWorld5, size, size, size);
753+
754+ //計算
755+ matWorld = matWorld5 * matWorld4 * matWorld3 * matWorld2 * matWorld1;
756+
757+ //適用
758+ pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
759+}
760+
761+//! @brief ワールド空間を所持している武器を表示する場所に設定
762+//! @param rotation 武器を回転させる
763+//! @param camera_x カメラのX座標
764+//! @param camera_y カメラのY座標
765+//! @param camera_z カメラのZ座標
766+//! @param camera_rx カメラの横軸角度
767+//! @param camera_ry カメラの縦軸角度
768+//! @param rx 武器のの縦軸角度
769+//! @param size 表示サイズ
770+//! @note rotation・・ true:現在持っている武器です。 false:予備の武器です。(rx は無視されます)
771+//! @todo 位置やサイズの微調整
772+void D3DGraphics::SetWorldTransformPlayerWeapon(bool rotation, float camera_x, float camera_y, float camera_z, float camera_rx, float camera_ry, float rx, float size)
773+{
774+ D3DXMATRIX matWorld;
775+ D3DXMATRIX matWorld1, matWorld2, matWorld3, matWorld4, matWorld5, matWorld6;
776+
777+ size = size * 0.3f;
778+
779+ //行列を作成
780+ D3DXMatrixTranslation(&matWorld1, camera_x, camera_y, camera_z);
781+ D3DXMatrixRotationY(&matWorld2, camera_rx *-1);
782+ D3DXMatrixRotationZ(&matWorld3, camera_ry);
783+ // matWorld4 = [奥行き, 縦, 横]
784+ if( rotation == true ){
785+ D3DXMatrixTranslation(&matWorld4, HUD_myweapon_x[0], HUD_myweapon_y[0], HUD_myweapon_z[0]);
786+ D3DXMatrixRotationY(&matWorld5, rx);
787+ }
788+ else{
789+ D3DXMatrixTranslation(&matWorld4, HUD_myweapon_x[1], HUD_myweapon_y[1], HUD_myweapon_z[1]);
790+ D3DXMatrixRotationY(&matWorld5, D3DX_PI);
791+ }
792+ D3DXMatrixScaling(&matWorld6, size, size, size);
793+
794+ //計算
795+ matWorld = matWorld6 * matWorld5 * matWorld4 * matWorld3 * matWorld2 * matWorld1;
796+
797+ //適用
798+ pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
799+}
800+
801+//! @brief ワールド空間の座標を取得
802+//! @param *x x軸を受け取るポインタ
803+//! @param *y y軸を受け取るポインタ
804+//! @param *z z軸を受け取るポインタ
805+void D3DGraphics::GetWorldTransformPos(float *x, float *y, float *z)
806+{
807+ D3DXMATRIX matWorld;
808+ pd3dDevice->GetTransform( D3DTS_WORLD, &matWorld );
809+ *x = matWorld._41;
810+ *y = matWorld._42;
811+ *z = matWorld._43;
812+}
813+
814+//! @brief フォグを設定
815+//! @param skynumber 空の番号
816+void D3DGraphics::SetFog(int skynumber)
817+{
818+ D3DCOLOR skycolor;
819+
820+ //空の番号により色を決定
821+ switch(skynumber){
822+ case 1: skycolor = D3DCOLOR_RGBA(64, 64+16, 64, 0); break;
823+ case 2: skycolor = D3DCOLOR_RGBA(16, 16, 16, 0); break;
824+ case 3: skycolor = D3DCOLOR_RGBA(0, 16, 32, 0); break;
825+ case 4: skycolor = D3DCOLOR_RGBA(32, 16, 16, 0); break;
826+ case 5: skycolor = D3DCOLOR_RGBA(64, 32, 32, 0); break;
827+ default: skycolor = D3DCOLOR_RGBA(0, 0, 0, 0); break;
828+ }
829+
830+ //フォグを設定
831+ pd3dDevice->SetRenderState(D3DRS_FOGCOLOR, skycolor);
832+}
833+
834+//! @brief カメラ(視点)を設定
835+//! @param camera_x カメラのX座標
836+//! @param camera_y カメラのY座標
837+//! @param camera_z カメラのZ座標
838+//! @param camera_rx カメラの横軸角度
839+//! @param camera_ry カメラの縦軸角度
840+//! @param viewangle 視野角
841+void D3DGraphics::SetCamera(float camera_x, float camera_y, float camera_z, float camera_rx, float camera_ry, float viewangle)
842+{
843+ float vUpVecF;
844+ D3DXMATRIX matWorld;
845+ D3DXMATRIXA16 matView;
846+
847+ //camera_ryを -PI〜PI の間に正規化
848+ for(; camera_ry>D3DX_PI; camera_ry -= D3DX_PI*2){}
849+ for(; camera_ry<D3DX_PI*-1; camera_ry += D3DX_PI*2){}
850+
851+ //カメラの向きを決定
852+ if( fabs(camera_ry) < D3DX_PI/2 ){
853+ vUpVecF = 1.0f;
854+ }
855+ else{
856+ vUpVecF = -1.0f;
857+ }
858+
859+ D3DXMatrixIdentity(&matWorld);
860+ pd3dDevice->SetTransform(D3DTS_WORLD, &matWorld);
861+
862+ //カメラ座標
863+ D3DXVECTOR3 vEyePt( camera_x, camera_y, camera_z );
864+ D3DXVECTOR3 vLookatPt( cos(camera_rx)*cos(camera_ry) + camera_x, sin(camera_ry) + camera_y, sin(camera_rx)*cos(camera_ry) + camera_z );
865+ D3DXVECTOR3 vUpVec( 0.0f, vUpVecF, 0.0f );
866+ D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
867+ pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
868+
869+ //カメラ設定(射影変換行列)viewangle
870+ D3DXMATRIXA16 matProj;
871+ D3DXMatrixPerspectiveFovLH( &matProj, viewangle, aspect, CLIPPINGPLANE_NEAR, CLIPPINGPLANE_FAR);
872+ pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProj);
873+}
874+
875+//! @brief マップデータを取り込む
876+//! @param in_blockdata ブロックデータ
877+//! @param directory ブロックデータが存在するディレクトリ
878+void D3DGraphics::LoadMapdata(BlockDataInterface* in_blockdata, char *directory)
879+{
880+ //ブロックデータが指定されていなければ、処理しない。
881+ if( in_blockdata == NULL ){ return; }
882+
883+ char fname[MAX_PATH];
884+ char fnamefull[MAX_PATH];
885+ //int bs;
886+ struct blockdata data;
887+ int vID[4];
888+ int uvID[4];
889+
890+ //クラスを設定
891+ blockdata = in_blockdata;
892+
893+ //テクスチャ読み込み
894+ for(int i=0; i<TOTAL_BLOCKTEXTURE; i++){
895+ //テクスチャ名を取得
896+ blockdata->GetTexture(fname, i);
897+
898+ if( strcmp(fname, "") == 0 ){ //指定されていなければ、処理しない
899+ mapTextureID[i] = -1;
900+ }
901+ else{
902+ //「ディレクトリ+ファイル名」を生成し、読み込む
903+ strcpy(fnamefull, directory);
904+ strcat(fnamefull, fname);
905+ mapTextureID[i] = LoadTexture(fnamefull, false, false);
906+ }
907+ }
908+
909+#ifdef ENABLE_DEBUGLOG
910+ //ログに出力
911+ OutputLog.WriteLog(LOG_LOAD, "マップ", "(頂点データ)");
912+#endif
913+
914+#ifdef BLOCKDATA_GPUMEMORY
915+ VERTEXTXTA* pVertices;
916+
917+ //ブロック数を取得
918+ bs = blockdata->GetTotaldatas();
919+
920+ //ブロック数分のバッファーを作成
921+ pd3dDevice->CreateVertexBuffer(bs*6*4*sizeof(VERTEXTXTA),0,D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1,D3DPOOL_DEFAULT,&g_pVB,NULL);
922+
923+ for(int i=0; i<bs; i++){
924+ //データを取得
925+ blockdata->Getdata(&data, i);
926+
927+ for(int j=0; j<6; j++){
928+ //面の頂点データの関連付けを取得
929+ blockdataface(j, &vID[0], &uvID[0]);
930+
931+ //GPUをロック(1面分)
932+ g_pVB->Lock((i*6+j)*4*sizeof(VERTEXTXTA), 4*sizeof(VERTEXTXTA), (void**)&pVertices, 0);
933+
934+ //頂点座標・UV座標・色を設定
935+ pVertices[0].position = D3DXVECTOR3( data.x[ vID[1] ], data.y[ vID[1] ], data.z[ vID[1] ] );
936+ pVertices[0].tu = data.material[j].u[ uvID[1] ];
937+ pVertices[0].tv = data.material[j].v[ uvID[1] ];
938+ pVertices[1].position = D3DXVECTOR3( data.x[ vID[2] ], data.y[ vID[2] ], data.z[ vID[2] ] );
939+ pVertices[1].tu = data.material[j].u[ uvID[2] ];
940+ pVertices[1].tv = data.material[j].v[ uvID[2] ];
941+ pVertices[2].position = D3DXVECTOR3( data.x[ vID[0] ], data.y[ vID[0] ], data.z[ vID[0] ] );
942+ pVertices[2].tu = data.material[j].u[ uvID[0] ];
943+ pVertices[2].tv = data.material[j].v[ uvID[0] ];
944+ pVertices[3].position = D3DXVECTOR3( data.x[ vID[3] ], data.y[ vID[3] ], data.z[ vID[3] ] );
945+ pVertices[3].tu = data.material[j].u[ uvID[3] ];
946+ pVertices[3].tv = data.material[j].v[ uvID[3] ];
947+ for(int k=0; k<4; k++){
948+ pVertices[k].color = D3DCOLOR_COLORVALUE(data.material[j].shadow, data.material[j].shadow, data.material[j].shadow, 1.0f);
949+ }
950+
951+ //GPUのロックを解除
952+ g_pVB->Unlock();
953+ }
954+ }
955+#else
956+ //ブロック数を取得
957+ bs = blockdata->GetTotaldatas();
958+
959+ for(int i=0; i<bs; i++){
960+ //データを取得
961+ blockdata->Getdata(&data, i);
962+
963+ for(int j=0; j<6; j++){
964+ //面の頂点データの関連付けを取得
965+ blockdataface(j, vID, uvID);
966+
967+ //頂点座標・UV座標・色を設定
968+ g_pVertices[i][j][0].position = D3DXVECTOR3( data.x[ vID[1] ], data.y[ vID[1] ], data.z[ vID[1] ] );
969+ g_pVertices[i][j][0].tu = data.material[j].u[ uvID[1] ];
970+ g_pVertices[i][j][0].tv = data.material[j].v[ uvID[1] ];
971+ g_pVertices[i][j][1].position = D3DXVECTOR3( data.x[ vID[2] ], data.y[ vID[2] ], data.z[ vID[2] ] );
972+ g_pVertices[i][j][1].tu = data.material[j].u[ uvID[2] ];
973+ g_pVertices[i][j][1].tv = data.material[j].v[ uvID[2] ];
974+ g_pVertices[i][j][2].position = D3DXVECTOR3( data.x[ vID[0] ], data.y[ vID[0] ], data.z[ vID[0] ] );
975+ g_pVertices[i][j][2].tu = data.material[j].u[ uvID[0] ];
976+ g_pVertices[i][j][2].tv = data.material[j].v[ uvID[0] ];
977+ g_pVertices[i][j][3].position = D3DXVECTOR3( data.x[ vID[3] ], data.y[ vID[3] ], data.z[ vID[3] ] );
978+ g_pVertices[i][j][3].tu = data.material[j].u[ uvID[3] ];
979+ g_pVertices[i][j][3].tv = data.material[j].v[ uvID[3] ];
980+ for(int k=0; k<4; k++){
981+ g_pVertices[i][j][k].color = D3DCOLOR_COLORVALUE(data.material[j].shadow, data.material[j].shadow, data.material[j].shadow, 1.0f);
982+ }
983+ }
984+ }
985+#endif
986+
987+#ifdef ENABLE_DEBUGLOG
988+ //ログに出力
989+ OutputLog.WriteLog(LOG_COMPLETE, "", "");
990+#endif
991+}
992+
993+//! @brief マップデータを描画
994+//! @param wireframe ワイヤーフレーム表示
995+void D3DGraphics::DrawMapdata(bool wireframe)
996+{
997+ //ブロックデータが読み込まれていなければ、処理しない。
998+ if( blockdata == NULL ){ return; }
999+
1000+ struct blockdata data;
1001+ int textureID;
1002+
1003+ if( wireframe == true ){
1004+ //ワイヤーフレーム表示
1005+ for(int i=0; i<bs; i++){
1006+ blockdata->Getdata(&data, i);
1007+ Drawline(data.x[0], data.y[0], data.z[0], data.x[1], data.y[1], data.z[1]);
1008+ Drawline(data.x[1], data.y[1], data.z[1], data.x[2], data.y[2], data.z[2]);
1009+ Drawline(data.x[2], data.y[2], data.z[2], data.x[3], data.y[3], data.z[3]);
1010+ Drawline(data.x[3], data.y[3], data.z[3], data.x[0], data.y[0], data.z[0]);
1011+ Drawline(data.x[4], data.y[4], data.z[4], data.x[5], data.y[5], data.z[5]);
1012+ Drawline(data.x[5], data.y[5], data.z[5], data.x[6], data.y[6], data.z[6]);
1013+ Drawline(data.x[6], data.y[6], data.z[6], data.x[7], data.y[7], data.z[7]);
1014+ Drawline(data.x[7], data.y[7], data.z[7], data.x[4], data.y[4], data.z[4]);
1015+ Drawline(data.x[0], data.y[0], data.z[0], data.x[4], data.y[4], data.z[4]);
1016+ Drawline(data.x[1], data.y[1], data.z[1], data.x[5], data.y[5], data.z[5]);
1017+ Drawline(data.x[2], data.y[2], data.z[2], data.x[6], data.y[6], data.z[6]);
1018+ Drawline(data.x[3], data.y[3], data.z[3], data.x[7], data.y[7], data.z[7]);
1019+ }
1020+ return;
1021+ }
1022+
1023+ //深度バッファ比較関数を設定
1024+ //pd3dDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESS);
1025+
1026+
1027+#ifdef BLOCKDATA_GPUMEMORY
1028+ //データ設定
1029+ pd3dDevice->SetStreamSource(0,g_pVB,0,sizeof(VERTEXTXTA));
1030+
1031+ for(textureID=0; textureID<TOTAL_BLOCKTEXTURE; textureID++){
1032+ //テクスチャが正常に読み込めていなければ設定
1033+ if( mapTextureID[textureID] == -1 ){
1034+ pd3dDevice->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE);
1035+ pd3dDevice->SetTexture(0, NULL);
1036+ }
1037+ else if( ptextures[ mapTextureID[textureID] ] == NULL ){
1038+ pd3dDevice->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE);
1039+ pd3dDevice->SetTexture(0, NULL);
1040+ }
1041+ else{
1042+ pd3dDevice->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1);
1043+ pd3dDevice->SetTexture(0, ptextures[mapTextureID[textureID]] );
1044+ }
1045+
1046+ for(int i=0; i<bs; i++){
1047+ //データ取得
1048+ blockdata->Getdata(&data, i);
1049+
1050+ for(int j=0; j<6; j++){
1051+ //テクスチャ認識番号を取得
1052+ int ID = data.material[j].textureID;
1053+
1054+ if( textureID == ID ){
1055+ //面を描画
1056+ pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, (i*6+j)*4, 2);
1057+ }
1058+ }
1059+ }
1060+ }
1061+#else
1062+ //データを設定
1063+ pd3dDevice->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1);
1064+
1065+ for(textureID=0; textureID<TOTAL_BLOCKTEXTURE; textureID++){
1066+ //テクスチャが正常に読み込めていなければ設定
1067+ if( mapTextureID[textureID] == -1 ){
1068+ pd3dDevice->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE);
1069+ pd3dDevice->SetTexture(0, NULL);
1070+ }
1071+ else if( ptextures[ mapTextureID[textureID] ] == NULL ){
1072+ pd3dDevice->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE);
1073+ pd3dDevice->SetTexture(0, NULL);
1074+ }
1075+ else{
1076+ pd3dDevice->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1);
1077+ pd3dDevice->SetTexture(0, ptextures[mapTextureID[textureID]] );
1078+ }
1079+
1080+ for(int i=0; i<bs; i++){
1081+ //データ取得
1082+ blockdata->Getdata(&data, i);
1083+
1084+ for(int j=0; j<6; j++){
1085+ //テクスチャ認識番号を取得
1086+ int ID = data.material[j].textureID;
1087+
1088+ if( textureID == ID ){
1089+ //面を描画
1090+ pd3dDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, g_pVertices[i][j], sizeof(VERTEXTXTA));
1091+ }
1092+ }
1093+ }
1094+ }
1095+#endif
1096+
1097+ //深度バッファ比較関数を元に戻す
1098+ //pd3dDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESSEQUAL);
1099+}
1100+
1101+//! @brief マップテクスチャを取得
1102+//! @param id テクスチャ番号
1103+//! @return テクスチャ認識番号(失敗:-1)
1104+int D3DGraphics::GetMapTextureID(int id)
1105+{
1106+ if( (id < 0)||((TOTAL_BLOCKTEXTURE -1) < id ) ){ return -1; }
1107+ return mapTextureID[id];
1108+}
1109+
1110+//! @brief マップデータを解放
1111+void D3DGraphics::CleanupMapdata()
1112+{
1113+ //テクスチャを開放
1114+ for(int i=0; i<TOTAL_BLOCKTEXTURE; i++){
1115+ CleanupTexture(mapTextureID[i]);
1116+ }
1117+
1118+#ifdef BLOCKDATA_GPUMEMORY
1119+ //頂点データ解放
1120+ if( g_pVB != NULL ){
1121+ g_pVB->Release();
1122+ g_pVB = NULL;
1123+ }
1124+#endif
1125+ bs = 0;
1126+
1127+ blockdata = NULL;
1128+
1129+#ifdef ENABLE_DEBUGLOG
1130+ //ログに出力
1131+ OutputLog.WriteLog(LOG_CLEANUP, "マップ", "(頂点データ)");
1132+#endif
1133+}
1134+
1135+//! @brief モデルファイルを描画
1136+//! @param id_model モデル認識番号
1137+//! @param id_texture テクスチャ認識番号
1138+void D3DGraphics::RenderModel(int id_model, int id_texture)
1139+{
1140+ //無効な引数が設定されていれば失敗
1141+ if( id_model == -1 ){ return; }
1142+ //if( id_texture == -1 ){ return; }
1143+
1144+ //指定したモデルが初期化されていなければ失敗
1145+ if( pmesh[id_model] == NULL) return;
1146+
1147+ //描画
1148+ for(int i=0; i<(signed)nummaterials[id_model]; i=i+1){
1149+ pd3dDevice->SetMaterial( &pmaterials[id_model][i] );
1150+ if( id_texture == -1 ){
1151+ pd3dDevice->SetTexture(0, NULL);
1152+ }
1153+ else if( ptextures[id_texture] == NULL ){
1154+ pd3dDevice->SetTexture(0, NULL);
1155+ }
1156+ else{
1157+ pd3dDevice->SetTexture( 0, ptextures[id_texture] );
1158+ }
1159+ pmesh[id_model]->DrawSubset(i);
1160+ }
1161+}
1162+
1163+//! @brief 板を描画
1164+//! @param id_texture テクスチャ認識番号
1165+//! @param alpha 透明度 (0.0〜1.0 0.0:完全透明)
1166+void D3DGraphics::RenderBoard(int id_texture, float alpha)
1167+{
1168+ //テクスチャが設定されていなければ、処理しない。
1169+ if( id_texture == -1 ){ return; }
1170+
1171+ VERTEXTXTA BoardVertices[4];
1172+
1173+ //頂点座標・UV座標・色/透明度を設定
1174+ BoardVertices[0].position = D3DXVECTOR3(0.0f, 0.5f, -0.5f);
1175+ BoardVertices[0].tu = 1.0f;
1176+ BoardVertices[0].tv = 0.0f;
1177+ BoardVertices[1].position = D3DXVECTOR3(0.0f, -0.5f, -0.5f);
1178+ BoardVertices[1].tu = 1.0f;
1179+ BoardVertices[1].tv = 1.0f;
1180+ BoardVertices[2].position = D3DXVECTOR3(0.0f, 0.5f, 0.5f);
1181+ BoardVertices[2].tu = 0.0f;
1182+ BoardVertices[2].tv = 0.0f;
1183+ BoardVertices[3].position = D3DXVECTOR3(0.0f, -0.5f, 0.5f);
1184+ BoardVertices[3].tu = 0.0f;
1185+ BoardVertices[3].tv = 1.0f;
1186+ for(int i=0; i<4; i++){
1187+ BoardVertices[i].color = D3DCOLOR_COLORVALUE(1.0f, 1.0f, 1.0f, alpha);
1188+ }
1189+
1190+ //アルファブレンドを設定
1191+ pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
1192+ pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
1193+ pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
1194+
1195+ //テクスチャとデータ形式を設定し描画
1196+ pd3dDevice->SetTexture(0, ptextures[id_texture]);
1197+ pd3dDevice->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1);
1198+ pd3dDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, BoardVertices, sizeof(VERTEXTXTA));
1199+
1200+ //アルファブレンドを元に戻す
1201+ pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1202+ pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
1203+}
1204+
1205+//! @brief 画面の明るさを設定
1206+//! @param Width 幅
1207+//! @param Height 高さ
1208+//! @param Brightness 画面の明るさ (0 で不変、1 以上で明るさの度合い)
1209+void D3DGraphics::ScreenBrightness(int Width, int Height, int Brightness)
1210+{
1211+ //明るさ不変なら処理しない(軽量化)
1212+ if( Brightness == 0 ){ return; }
1213+
1214+ //透明度を設定し、描画
1215+ float alpha = 0.02f * Brightness;
1216+ Draw2DBox(0, 0, Width, Height, D3DCOLOR_COLORVALUE(1.0f,1.0f,1.0f,alpha));
1217+}
1218+
1219+//! @brief 【デバック用】中心線描画
1220+void D3DGraphics::Centerline()
1221+{
1222+ ResetWorldTransform();
1223+ Drawline(100.0f, 0.0f, 0.0f, -100.0f, 0.0f, 0.0f);
1224+ Drawline(0.0f, 100.0f, 0.0f, 0.0f, -100.0f, 0.0f);
1225+ Drawline(0.0f, 0.0f, 100.0f, 0.0f, 0.0f, -100.0f);
1226+}
1227+
1228+//! @brief 【デバック用】緑線描画
1229+void D3DGraphics::Drawline(float x1, float y1, float z1, float x2, float y2, float z2)
1230+{
1231+ VERTEXTXTA mv[2];
1232+
1233+ mv[0].position = D3DXVECTOR3(x1, y1, z1);
1234+ mv[1].position = D3DXVECTOR3(x2, y2, z2);
1235+ for(int i=0; i<2; i++){
1236+ mv[i].color = 0xFF00FF00;
1237+ mv[i].tu = 0.0f;
1238+ mv[i].tv = 0.0f;
1239+ }
1240+
1241+ pd3dDevice->SetTexture(0, NULL);
1242+ pd3dDevice->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1);
1243+ pd3dDevice->DrawPrimitiveUP(D3DPT_LINELIST, 1, mv, sizeof(VERTEXTXTA));
1244+}
1245+
1246+//! @brief 2D システムフォントによるテキスト描画を開始
1247+//! @attention DirectXの ID3DXSprite を初期化しています。
1248+void D3DGraphics::Start2DMSFontTextRender()
1249+{
1250+ ptextsprite->Begin(D3DXSPRITE_ALPHABLEND);
1251+}
1252+
1253+//! @brief 文字を描画(システムフォント使用)
1254+//! @param x x座標
1255+//! @param y y座標
1256+//! @param str 文字列 (改行コード:可)
1257+//! @param color 色
1258+//! @warning <b>描画は非常に低速です。</b>画面内で何度も呼び出すとパフォーマンスに影響します。
1259+//! @warning「改行コードを活用し一度に描画する」「日本語が必要ない文字はテクスチャフォントを活用する」などの対応を講じてください。
1260+//! @attention DirectXの ID3DXSprite を使用し、システムフォントで描画ています。
1261+//! @attention フォントの種類やサイズは固定です。 文字を二重に重ねて立体感を出さないと見にくくなります。
1262+void D3DGraphics::Draw2DMSFontText(int x, int y, char *str, int color)
1263+{
1264+ //if( ptextsprite == NULL ){ return; }
1265+
1266+ //テキストスプライト初期化
1267+ Start2DMSFontTextRender();
1268+
1269+ //基準座標を設定
1270+ D3DXMATRIX matWorld;
1271+ D3DXMatrixIdentity(&matWorld);
1272+ ptextsprite->SetTransform(&matWorld);
1273+
1274+ //文字をを描画
1275+ RECT rc = {x, y, 0, 0};
1276+ pxmsfont->DrawText(ptextsprite, str, -1, &rc, DT_NOCLIP, color);
1277+
1278+ //テキストスプライト解放
1279+ End2DMSFontTextRender();
1280+}
1281+
1282+//! @brief 文字を中央揃えで描画(システムフォント使用)
1283+//! @param x x座標
1284+//! @param y y座標
1285+//! @param w 横の大きさ
1286+//! @param h 縦の大きさ
1287+//! @param str 文字列 (改行コード:可)
1288+//! @param color 色
1289+//! @warning <b>描画は非常に低速です。</b>画面内で何度も呼び出すとパフォーマンスに影響します。
1290+//! @warning「改行コードを活用し一度に描画する」「日本語が必要ない文字はテクスチャフォントを活用する」などの対応を講じてください。
1291+//! @attention DirectXの ID3DXSprite を使用し、システムフォントで描画ています。
1292+//! @attention フォントの種類やサイズは固定です。 文字を二重に重ねて立体感を出さないと見にくくなります。
1293+void D3DGraphics::Draw2DMSFontTextCenter(int x, int y, int w, int h, char *str, int color)
1294+{
1295+ //if( ptextsprite == NULL ){ return; }
1296+
1297+ //テキストスプライト初期化
1298+ Start2DMSFontTextRender();
1299+
1300+ //基準座標を設定
1301+ D3DXMATRIX matWorld;
1302+ D3DXMatrixIdentity(&matWorld);
1303+ ptextsprite->SetTransform(&matWorld);
1304+
1305+ //文字をを描画
1306+ RECT rc = {x, y, x+w, y+h};
1307+ pxmsfont->DrawText(ptextsprite, str, -1, &rc, DT_CENTER, color);
1308+
1309+ //テキストスプライト解放
1310+ End2DMSFontTextRender();
1311+}
1312+
1313+//! @brief 2D システムフォントによるテキスト描画を終了
1314+//! @attention DirectXの ID3DXSprite を解放しています。
1315+void D3DGraphics::End2DMSFontTextRender()
1316+{
1317+ ptextsprite->End();
1318+}
1319+
1320+//! @brief 2D描画用設定
1321+void D3DGraphics::Start2DRender()
1322+{
1323+ pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
1324+ pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
1325+ pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
1326+
1327+ //深度バッファ比較関数を設定
1328+ pd3dDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1329+}
1330+
1331+//! @brief 文字を描画(テクスチャフォント使用)
1332+//! @param x x座標
1333+//! @param y y座標
1334+//! @param str 文字列 (改行コード:<b>不可</b>)
1335+//! @param color 色
1336+//! @param fontwidth 一文字の幅
1337+//! @param fontheight 一文字の高さ
1338+//! @attention 文字を二重に重ねて立体感を出さないと見にくくなります。
1339+void D3DGraphics::Draw2DTextureFontText(int x, int y, char *str, int color, int fontwidth, int fontheight)
1340+{
1341+ //テクスチャフォントの取得に失敗していれば、処理しない
1342+ if( TextureFont == -1 ){ return; }
1343+
1344+ //2D描画用設定を適用
1345+ Start2DRender();
1346+
1347+ int w;
1348+ float font_u, font_v;
1349+ float t_u, t_v;
1350+ TLVERTX pBoxVertices[4];
1351+
1352+ //1文字のUV座標を計算
1353+ font_u = 1.0f / 16;
1354+ font_v = 1.0f / 16;
1355+
1356+ //ワールド座標を原点に戻す
1357+ ResetWorldTransform();
1358+
1359+ //テクスチャをフォントテクスチャに設定
1360+ pd3dDevice->SetTexture( 0, ptextures[TextureFont] );
1361+
1362+ //データ形式を設定
1363+ pd3dDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1);
1364+
1365+ // 与えられた文字数分ループ
1366+ for(int i=0; i<(int)strlen(str); i++){
1367+ //UV座標を計算
1368+ w = str[i];
1369+ if( w < 0 ){ w += 256; }
1370+ t_u = (w % 16) * font_u;
1371+ t_v = (w / 16) * font_v;
1372+
1373+ //頂点座標・UV座標・色を設定
1374+ pBoxVertices[0].x = (float)x + i*fontwidth;
1375+ pBoxVertices[0].y = (float)y;
1376+ pBoxVertices[0].tu = t_u;
1377+ pBoxVertices[0].tv = t_v;
1378+ pBoxVertices[1].x = (float)x + fontwidth + i*fontwidth;
1379+ pBoxVertices[1].y = (float)y;
1380+ pBoxVertices[1].tu = t_u + font_u;
1381+ pBoxVertices[1].tv = t_v;
1382+ pBoxVertices[2].x = (float)x + i*fontwidth;
1383+ pBoxVertices[2].y = (float)y + fontheight;
1384+ pBoxVertices[2].tu = t_u;
1385+ pBoxVertices[2].tv = t_v + font_v;
1386+ pBoxVertices[3].x = (float)x + fontwidth + i*fontwidth;
1387+ pBoxVertices[3].y = (float)y + fontheight;
1388+ pBoxVertices[3].tu = t_u + font_u;
1389+ pBoxVertices[3].tv = t_v + font_v;
1390+ for(int j=0; j<4; j++){
1391+ pBoxVertices[j].z = 0.0f;
1392+ pBoxVertices[j].rhw = 1.0f;
1393+ pBoxVertices[j].color = color;
1394+ }
1395+
1396+ //描画
1397+ pd3dDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, pBoxVertices, sizeof(TLVERTX));
1398+ }
1399+
1400+ //2D描画用設定を解除
1401+ End2DRender();
1402+}
1403+
1404+//! @brief 線を描画
1405+//! @param x1 始点の x座標
1406+//! @param y1 始点の y座標
1407+//! @param x2 終点の x座標
1408+//! @param y2 終点の y座標
1409+//! @param color 色
1410+void D3DGraphics::Draw2DLine(int x1, int y1, int x2, int y2, int color)
1411+{
1412+ TLVERTX pLineVertices[2];
1413+
1414+ //2D描画用設定を適用
1415+ Start2DRender();
1416+
1417+ //ワールド座標を原点に戻す
1418+ ResetWorldTransform();
1419+
1420+ //頂点座標と色などを設定
1421+ pLineVertices[0].x = (float)x1;
1422+ pLineVertices[0].y = (float)y1;
1423+ pLineVertices[1].x = (float)x2;
1424+ pLineVertices[1].y = (float)y2;
1425+ for(int i=0; i<2; i++){
1426+ pLineVertices[i].z = 0.0f;
1427+ pLineVertices[i].rhw = 1.0f;
1428+ pLineVertices[i].color = color;
1429+ pLineVertices[i].tu = 0.0f;
1430+ pLineVertices[i].tv = 0.0f;
1431+ }
1432+
1433+ pd3dDevice->SetTexture(0, NULL);
1434+
1435+ //データ形式を設定し、描画。
1436+ pd3dDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1);
1437+ pd3dDevice->DrawPrimitiveUP(D3DPT_LINELIST, 1, pLineVertices, sizeof(TLVERTX));
1438+
1439+ //2D描画用設定を解除
1440+ End2DRender();
1441+}
1442+
1443+//! @brief 円(16角形)を描画
1444+//! @param x 中心の x座標
1445+//! @param y 中心の y座標
1446+//! @param r 半径
1447+//! @param color 色
1448+void D3DGraphics::Draw2DCycle(int x, int y, int r, int color)
1449+{
1450+ TLVERTX pLineVertices[16+1];
1451+
1452+ //2D描画用設定を適用
1453+ Start2DRender();
1454+
1455+ //ワールド座標を原点に戻す
1456+ ResetWorldTransform();
1457+
1458+ //頂点座標と色などを設定
1459+ for(int i=0; i<16+1; i++){
1460+ pLineVertices[i].x = (float)x + cos(DegreeToRadian((360.0f/16.0f)) * i) * r;
1461+ pLineVertices[i].y = (float)y + sin(DegreeToRadian((360.0f/16.0f)) * i) * r;
1462+
1463+ pLineVertices[i].z = 0.0f;
1464+ pLineVertices[i].rhw = 1.0f;
1465+ pLineVertices[i].color = color;
1466+ pLineVertices[i].tu = 0.0f;
1467+ pLineVertices[i].tv = 0.0f;
1468+ }
1469+
1470+ pd3dDevice->SetTexture(0, NULL);
1471+
1472+ //データ形式を設定し、描画。
1473+ pd3dDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1);
1474+ pd3dDevice->DrawPrimitiveUP(D3DPT_LINESTRIP, 16, pLineVertices, sizeof(TLVERTX));
1475+
1476+ //2D描画用設定を解除
1477+ End2DRender();
1478+}
1479+
1480+//! @brief 四角形を描画
1481+//! @param x1 左上の x座標
1482+//! @param y1 左上の y座標
1483+//! @param x2 右下の x座標
1484+//! @param y2 右下の y座標
1485+//! @param color 色
1486+void D3DGraphics::Draw2DBox(int x1, int y1, int x2, int y2, int color)
1487+{
1488+ TLVERTX pBoxVertices[4];
1489+
1490+ //2D描画用設定を適用
1491+ Start2DRender();
1492+
1493+ //ワールド座標を原点に戻す
1494+ ResetWorldTransform();
1495+
1496+ //頂点座標と色などを設定
1497+ pBoxVertices[0].x = (float)x1;
1498+ pBoxVertices[0].y = (float)y1;
1499+ pBoxVertices[1].x = (float)x2;
1500+ pBoxVertices[1].y = (float)y1;
1501+ pBoxVertices[2].x = (float)x1;
1502+ pBoxVertices[2].y = (float)y2;
1503+ pBoxVertices[3].x = (float)x2;
1504+ pBoxVertices[3].y = (float)y2;
1505+ for(int i=0; i<4; i++){
1506+ pBoxVertices[i].z = 0.0f;
1507+ pBoxVertices[i].rhw = 1.0f;
1508+ pBoxVertices[i].color = color;
1509+ pBoxVertices[i].tu = 0.0f;
1510+ pBoxVertices[i].tv = 0.0f;
1511+ }
1512+
1513+ pd3dDevice->SetTexture(0, NULL);
1514+
1515+ //データ形式を設定し、描画。
1516+ pd3dDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1);
1517+ pd3dDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, pBoxVertices, sizeof(TLVERTX));
1518+
1519+ //2D描画用設定を解除
1520+ End2DRender();
1521+}
1522+
1523+//! @brief 画像を描画
1524+//! @param x x座標
1525+//! @param y y座標
1526+//! @param id テクスチャ認識番号
1527+//! @param width 幅
1528+//! @param height 高さ
1529+//! @param alpha 透明度(0.0〜1.0)
1530+void D3DGraphics::Draw2DTexture(int x, int y, int id, int width, int height, float alpha)
1531+{
1532+ //無効なテクスチャ番号を指定されていれば処理しない
1533+ if( id == -1 ){ return; }
1534+
1535+ TLVERTX pBoxVertices[4];
1536+
1537+ //2D描画用設定を適用
1538+ Start2DRender();
1539+
1540+ //ワールド座標を原点に戻す
1541+ ResetWorldTransform();
1542+
1543+ //頂点座標・UV座標・色を設定
1544+ pBoxVertices[0].x = (float)x;
1545+ pBoxVertices[0].y = (float)y;
1546+ pBoxVertices[0].tu = 0.0f;
1547+ pBoxVertices[0].tv = 0.0f;
1548+ pBoxVertices[1].x = (float)x + width;
1549+ pBoxVertices[1].y = (float)y;
1550+ pBoxVertices[1].tu = 1.0f;
1551+ pBoxVertices[1].tv = 0.0f;
1552+ pBoxVertices[2].x = (float)x;
1553+ pBoxVertices[2].y = (float)y + height;
1554+ pBoxVertices[2].tu = 0.0f;
1555+ pBoxVertices[2].tv = 1.0f;
1556+ pBoxVertices[3].x = (float)x + width;
1557+ pBoxVertices[3].y = (float)y + height;
1558+ pBoxVertices[3].tu = 1.0f;
1559+ pBoxVertices[3].tv = 1.0f;
1560+ for(int i=0; i<4; i++){
1561+ pBoxVertices[i].z = 0.0f;
1562+ pBoxVertices[i].rhw = 1.0f;
1563+ pBoxVertices[i].color = D3DCOLOR_COLORVALUE(1.0f,1.0f,1.0f,alpha);
1564+ }
1565+
1566+ //テクスチャとデータ形式を設定し、描画
1567+ pd3dDevice->SetTexture( 0, ptextures[id] );
1568+ pd3dDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1);
1569+ pd3dDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, pBoxVertices, sizeof(TLVERTX));
1570+
1571+ //2D描画用設定を解除
1572+ End2DRender();
1573+}
1574+
1575+//! @brief 2D描画用設定を解除
1576+void D3DGraphics::End2DRender()
1577+{
1578+ pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1579+ pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
1580+
1581+ //深度バッファ比較関数を元に戻す
1582+ pd3dDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESSEQUAL);
1583+}
1584+
1585+//! @brief 画面のスクリーンショットを保存
1586+//! @param filename ファイル名
1587+//! @return 成功:true 失敗:false
1588+bool D3DGraphics::SaveScreenShot(char* filename)
1589+{
1590+ LPDIRECT3DSURFACE9 pSurface = NULL;
1591+ HRESULT hr;
1592+
1593+ //サーフェースを作成し、画面を取得
1594+ pd3dDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &pSurface);
1595+
1596+ //サーフェイスを画像に出力
1597+ hr = D3DXSaveSurfaceToFile(filename, D3DXIFF_BMP, pSurface, NULL, NULL);
1598+
1599+ //解放
1600+ pSurface->Release();
1601+
1602+ if( hr == D3D_OK ){
1603+ return true;
1604+ }
1605+ return false;
1606+}
1607+
1608+//! @brief カラーコードを取得
1609+//! @param red 赤(0.0f〜1.0f)
1610+//! @param green 緑(0.0f〜1.0f)
1611+//! @param blue 青(0.0f〜1.0f)
1612+//! @param alpha 透明度(0.0f〜1.0f)
1613+//! @return カラーコード
1614+int D3DGraphics::GetColorCode(float red, float green, float blue, float alpha)
1615+{
1616+ return D3DCOLOR_COLORVALUE(red, green, blue, alpha);
1617+}
1618+
1619+#endif //GRAPHICS_OPENGL
\ No newline at end of file
--- trunk/d3dgraphics-opengl.cpp (nonexistent)
+++ trunk/d3dgraphics-opengl.cpp (revision 95)
@@ -0,0 +1,2141 @@
1+//! @file d3dgraphics-opengl.cpp
2+//! @brief D3DGraphicsクラスの定義(OpenGL版)
3+
4+//--------------------------------------------------------------------------------
5+//
6+// OpenXOPS
7+// Copyright (c) 2014-2015, OpenXOPS Project / [-_-;](mikan) All rights reserved.
8+//
9+// Redistribution and use in source and binary forms, with or without
10+// modification, are permitted provided that the following conditions are met:
11+// * Redistributions of source code must retain the above copyright notice,
12+// this list of conditions and the following disclaimer.
13+// * Redistributions in binary form must reproduce the above copyright notice,
14+// this list of conditions and the following disclaimer in the documentation
15+// and/or other materials provided with the distribution.
16+// * Neither the name of the OpenXOPS Project nor the names of its contributors
17+// may be used to endorse or promote products derived from this software
18+// without specific prior written permission.
19+//
20+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+// DISCLAIMED. IN NO EVENT SHALL OpenXOPS Project BE LIABLE FOR ANY
24+// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26+// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27+// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+//--------------------------------------------------------------------------------
31+
32+// ***** OpenGL core only *****
33+//
34+// libjpeg
35+// Copyright (C) 1991-2014, Thomas G. Lane, Guido Vollbeding.
36+// this software is based in part on the work of the Independent JPEG Group
37+//
38+// zlib
39+// Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler
40+//
41+// libpng
42+// Copyright (c) 1998-2014 Glenn Randers-Pehrson
43+//
44+// ****************************
45+
46+#include "d3dgraphics.h"
47+
48+#ifdef GRAPHICS_OPENGL
49+
50+//! @brief コンストラクタ
51+D3DGraphics::D3DGraphics()
52+{
53+ hGLRC = NULL;
54+ width = 0;
55+ height = 0;
56+ SystemFont = NULL;
57+ now_SystemFontUStr = new WCHAR [1];
58+ now_SystemFontUStr[0] = NULL;
59+ SystemFontListIdx = 0;
60+ SystemFontListIdxSize = 0;
61+ now_textureid = -1;
62+
63+ camera_x = 0.0f;
64+ camera_y = 0.0f;
65+ camera_z = 0.0f;
66+ camera_rx = 0.0f;
67+ camera_ry = 0.0f;
68+ viewangle = 0.0f;
69+
70+ blockdata = NULL;
71+ for(int i=0; i<TOTAL_BLOCKTEXTURE; i++){
72+ mapTextureID[i] = -1;
73+ }
74+
75+ TextureFont = -1;
76+}
77+
78+//! @brief ディストラクタ
79+D3DGraphics::~D3DGraphics()
80+{
81+ for(int i=0; i<MAX_MODEL; i++){
82+ CleanupModel(i);
83+ }
84+ for(int i=0; i<MAX_TEXTURE; i++){
85+ CleanupTexture(i);
86+ }
87+
88+ if( SystemFont != NULL ){
89+ DeleteObject(SystemFont);
90+ }
91+ if( now_SystemFontUStr != NULL ){
92+ delete [] now_SystemFontUStr;
93+ }
94+ if( SystemFontListIdx != 0 ){
95+ glDeleteLists(SystemFontListIdx, SystemFontListIdxSize);
96+ }
97+
98+ if( hGLRC != NULL ){ wglDeleteContext(hGLRC); }
99+
100+#ifdef ENABLE_DEBUGLOG
101+ //ログに出力
102+ OutputLog.WriteLog(LOG_CLEANUP, "グラフィック", "OpenGL");
103+#endif
104+
105+ //libjpeg解放
106+ jpeg_destroy_decompress(&cinfo);
107+}
108+
109+//! @brief 初期化@n
110+//! (OpenGL 1.1)
111+//! @param WindowCtrl WindowControlクラスのポインタ
112+//! @param TextureFontFilename 使用するテクスチャフォントのファイル名
113+//! @param fullscreen false:ウィンドウ表示 true:フルスクリーン用表示
114+//! @return 成功:0 失敗:1
115+int D3DGraphics::InitD3D(WindowControl *WindowCtrl, char *TextureFontFilename, bool fullscreen)
116+{
117+#ifdef ENABLE_DEBUGLOG
118+ //ログに出力
119+ OutputLog.WriteLog(LOG_INIT, "グラフィック", "OpenGL");
120+#endif
121+
122+ hWnd = WindowCtrl->GethWnd();
123+
124+ RECT prc;
125+ GetClientRect(hWnd, &prc);
126+ width = prc.right;
127+ height = prc.bottom;
128+
129+ //フルスクリーン化
130+ if( fullscreen == true ){
131+ DEVMODE devmode;
132+ ZeroMemory(&devmode, sizeof(devmode));
133+ devmode.dmSize = sizeof(devmode);
134+ devmode.dmPelsWidth = width;
135+ devmode.dmPelsHeight = height;
136+ devmode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
137+
138+ if( ChangeDisplaySettings(&devmode, CDS_TEST) != DISP_CHANGE_SUCCESSFUL ){
139+ return 1;
140+ }
141+ ChangeDisplaySettings(&devmode, CDS_FULLSCREEN);
142+ }
143+
144+
145+
146+ HDC hDC;
147+ int pfdID;
148+ BOOL bResult;
149+
150+ //ピクセルフォーマット
151+ static PIXELFORMATDESCRIPTOR pfd = {
152+ sizeof (PIXELFORMATDESCRIPTOR),
153+ 1,
154+ PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL,
155+ PFD_TYPE_RGBA,
156+ 24,
157+ 0, 0, 0,
158+ 0, 0, 0,
159+ 0, 0,
160+ 0, 0, 0, 0, 0,
161+ 32,
162+ 0,
163+ 0,
164+ PFD_MAIN_PLANE,
165+ 0,
166+ 0,
167+ 0,
168+ 0
169+ };
170+
171+ //デバイスコンテキスト取得
172+ hDC = GetDC(hWnd);
173+
174+ //ピクセルフォーマットを取得
175+ pfdID = ChoosePixelFormat(hDC, &pfd);
176+ if (pfdID == 0) { return 1; }
177+
178+ //ピクセルフォーマットを指定
179+ bResult = SetPixelFormat(hDC, pfdID, &pfd);
180+ if (bResult == FALSE) { return 1; }
181+
182+ //コンテキストを指定
183+ hGLRC = wglCreateContext(hDC);
184+ if (hGLRC == NULL) { return 1; }
185+
186+ //デバイスコンテキスト解放
187+ ReleaseDC(hWnd, hDC);
188+
189+ //システムフォント用意
190+ //フォント名:MS ゴシック サイズ:18
191+ SystemFont = CreateFont(18, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, SHIFTJIS_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, "MS ゴシック");
192+
193+#ifdef ENABLE_DEBUGLOG
194+ //ログに出力
195+ OutputLog.WriteLog(LOG_COMPLETE, "", "");
196+#endif
197+
198+ //テクスチャフォント用画像のファイル名を設定
199+ strcpy(TextureFontFname, TextureFontFilename);
200+
201+ //テクスチャフォント用画像を取得
202+ TextureFont = LoadTexture(TextureFontFname, true, false);
203+
204+
205+ float aspecth, prx, pry, r;
206+ aspecth = (float)SCREEN_WIDTH/SCREEN_HEIGHT;
207+
208+ //HUD_myweapon [奥行き, 縦, 横]
209+
210+ //HUD_A 現在持っている武器を表示する座標
211+ prx = (float)M_PI/180*-39 * aspecth /2;
212+ pry = (float)M_PI/180*-55 /2;
213+ r = 7.5f;
214+ HUD_myweapon_x[0] = cos(pry)*r;
215+ HUD_myweapon_y[0] = sin(pry)*r;
216+ HUD_myweapon_z[0] = sin(prx)*r;
217+
218+ //HUD_A 予備の武器を表示する座標
219+ prx = (float)M_PI/180*-52 * aspecth /2;
220+ pry = (float)M_PI/180*-60 /2;
221+ r = 16.0f;
222+ HUD_myweapon_x[1] = cos(pry)*r;
223+ HUD_myweapon_y[1] = sin(pry)*r;
224+ HUD_myweapon_z[1] = sin(prx)*r;
225+
226+
227+ //libjpeg初期化
228+ cinfo.err = jpeg_std_error(&jerr);
229+ jpeg_create_decompress(&cinfo);
230+
231+ return 0;
232+}
233+
234+//! @brief リセット@n
235+//! (ウィンドウ最小化からの復帰 など)
236+//! @param WindowCtrl WindowControlクラスのポインタ
237+//! @return 成功:0 待ち:1 失敗:2
238+int D3DGraphics::ResetD3D(WindowControl *WindowCtrl)
239+{
240+#ifdef ENABLE_DEBUGLOG
241+ //ログに出力
242+ OutputLog.WriteLog(LOG_INIT, "グラフィック", "OpenGL(リセット)");
243+#endif
244+
245+ hWnd = WindowCtrl->GethWnd();
246+
247+#ifdef ENABLE_DEBUGLOG
248+ //ログに出力
249+ OutputLog.WriteLog(LOG_ERROR, "", "");
250+#endif
251+
252+ return 2;
253+}
254+
255+//! @brief モデルファイルを読み込む(.x)
256+//! @param filename ファイル名
257+//! @return 成功:モデル認識番号(0以上) 失敗:-1
258+int D3DGraphics::LoadModel(char* filename)
259+{
260+#ifdef ENABLE_DEBUGLOG
261+ //ログに出力
262+ OutputLog.WriteLog(LOG_LOAD, "モデル", filename);
263+#endif
264+
265+ int id = -1;
266+ FILE *fp;
267+ char buf[256];
268+ char str[256];
269+
270+ int vertexs = 0;
271+ MODELVDATA *vertex = NULL;
272+ int polygons = 0;
273+ int *index = NULL;
274+ int in_vertexs, in_polygons;
275+ MODELVDATA *old_vertex;
276+ int *old_index;
277+
278+ char stroks[] = " ;,"; //区切る文字列
279+
280+ //空いている認識番号を探す
281+ for(int i=0; i<MAX_TEXTURE; i++){
282+ if( pmodel[i].useflag == false ){
283+ id = i;
284+ break;
285+ }
286+ }
287+ if( id == -1 ){ return -1; }
288+
289+ //ファイルを読み込む
290+ fp = fopen(filename, "r");
291+ if( fp == NULL ){
292+ return -1; //ファイルが読めない
293+ }
294+
295+ //マジックコード取得
296+ fgets(buf, 256, fp);
297+ buf[ strlen("xof 0302txt") ] = 0x00;
298+ if( strcmp(buf, "xof 0302txt") != 0 ){
299+ fclose( fp );
300+ return -1; //Xファイルでない
301+ }
302+
303+ while( fgets(buf, 256, fp) != NULL ){
304+ strcpy(str, buf);
305+ str[ strlen("Mesh") ] = 0x00;
306+ if( strcmp(str, "Mesh") == 0 ){
307+
308+ fgets(buf, 256, fp);
309+ in_vertexs = atoi(buf);
310+
311+ if( vertexs == 0 ){
312+ //1つ目のメッシュデータならば、領域を作成するだけ。
313+ vertex = new MODELVDATA [in_vertexs];
314+ }
315+ else{
316+ //2つ目の以降なら、領域を確保し直してコピーし、古い領域は削除。
317+ old_vertex = vertex;
318+ vertex = new MODELVDATA [vertexs+in_vertexs];
319+ memcpy(vertex, old_vertex, sizeof(MODELVDATA)*vertexs);
320+ delete [] old_vertex;
321+ }
322+
323+ for(int i=0; i<in_vertexs; i++){
324+ fgets(buf, 256, fp);
325+ vertex[i+vertexs].x = (float)atof( strtok(buf, stroks) ) * -1;
326+ vertex[i+vertexs].y = (float)atof( strtok(NULL, stroks) );
327+ vertex[i+vertexs].z = (float)atof( strtok(NULL, stroks) );
328+ }
329+
330+ fgets(buf, 256, fp);
331+
332+ fgets(buf, 256, fp);
333+ in_polygons = atoi(buf);
334+
335+ if( polygons == 0 ){
336+ //1つ目のインデックスデータならば、領域を作成するだけ。
337+ index = new int [in_polygons*5];
338+ }
339+ else{
340+ //2つ目の以降なら、領域を確保し直してコピーし、古い領域は削除。
341+ old_index = index;
342+ index = new int [(polygons+in_polygons)*5];
343+ memcpy(index, old_index, sizeof(int)*polygons*5);
344+ delete [] old_index;
345+ }
346+
347+ for(int i=0; i<in_polygons; i++){
348+ fgets(buf, 256, fp);
349+ index[(i+polygons)*5] = atoi( strtok(buf, stroks) );
350+ for(int j=0; j<index[(i+polygons)*5]; j++){
351+ index[(i+polygons)*5 + j + 1] = atoi( strtok(NULL, stroks) ) + vertexs;
352+ }
353+ }
354+
355+ while( fgets(buf, 256, fp) != NULL ){
356+ strcpy(str, buf);
357+ str[ strlen(" MeshTextureCoords") ] = 0x00;
358+ if( strcmp(str, " MeshTextureCoords") == 0 ){
359+
360+ fgets(buf, 256, fp);
361+ if( atoi(buf) != in_vertexs ){ break; }
362+
363+ for(int i=0; i<in_vertexs; i++){
364+ fgets(buf, 256, fp);
365+ vertex[i+vertexs].u = (float)atof( strtok(buf, stroks) );
366+ vertex[i+vertexs].v = (float)atof( strtok(NULL, stroks) );
367+ }
368+
369+ break;
370+ }
371+ }
372+
373+ vertexs += in_vertexs;
374+ polygons += in_polygons;
375+ }
376+ }
377+
378+ //ファイルハンドルを解放
379+ fclose( fp );
380+
381+ float *VertexAry = new float [polygons*6*3];
382+ float *ColorAry = new float [polygons*6*4];
383+ float *TexCoordAry = new float [polygons*6*2];
384+ int vid;
385+ int cnt = 0;
386+
387+ for(int i=0; i<polygons; i++){
388+ if( index[i*5] == 3 ){
389+ //三角形
390+ vid = index[i*5+1];
391+ VertexAry[0 + cnt*3] = vertex[vid].x;
392+ VertexAry[1 + cnt*3] = vertex[vid].y;
393+ VertexAry[2 + cnt*3] = vertex[vid].z;
394+ TexCoordAry[0 + cnt*2] = vertex[vid].u;
395+ TexCoordAry[1 + cnt*2] = vertex[vid].v;
396+
397+ VertexAry[3 + cnt*3] = vertex[vid].x;
398+ VertexAry[4 + cnt*3] = vertex[vid].y;
399+ VertexAry[5 + cnt*3] = vertex[vid].z;
400+ TexCoordAry[2 + cnt*2] = vertex[vid].u;
401+ TexCoordAry[3 + cnt*2] = vertex[vid].v;
402+
403+ vid = index[i*5+3];
404+ VertexAry[6 + cnt*3] = vertex[vid].x;
405+ VertexAry[7 + cnt*3] = vertex[vid].y;
406+ VertexAry[8 + cnt*3] = vertex[vid].z;
407+ TexCoordAry[4 + cnt*2] = vertex[vid].u;
408+ TexCoordAry[5 + cnt*2] = vertex[vid].v;
409+
410+ vid = index[i*5+2];
411+ VertexAry[9 + cnt*3] = vertex[vid].x;
412+ VertexAry[10 + cnt*3] = vertex[vid].y;
413+ VertexAry[11 + cnt*3] = vertex[vid].z;
414+ TexCoordAry[6 + cnt*2] = vertex[vid].u;
415+ TexCoordAry[7 + cnt*2] = vertex[vid].v;
416+
417+ VertexAry[12 + cnt*3] = vertex[vid].x;
418+ VertexAry[13 + cnt*3] = vertex[vid].y;
419+ VertexAry[14 + cnt*3] = vertex[vid].z;
420+ TexCoordAry[8 + cnt*2] = vertex[vid].u;
421+ TexCoordAry[9 + cnt*2] = vertex[vid].v;
422+
423+ VertexAry[15 + cnt*3] = vertex[vid].x;
424+ VertexAry[16 + cnt*3] = vertex[vid].y;
425+ VertexAry[17 + cnt*3] = vertex[vid].z;
426+ TexCoordAry[10 + cnt*2] = vertex[vid].u;
427+ TexCoordAry[11 + cnt*2] = vertex[vid].v;
428+
429+ cnt += 6;
430+ }
431+ else{
432+ //四角形
433+ vid = index[i*5+1];
434+ VertexAry[0 + cnt*3] = vertex[vid].x;
435+ VertexAry[1 + cnt*3] = vertex[vid].y;
436+ VertexAry[2 + cnt*3] = vertex[vid].z;
437+ TexCoordAry[0 + cnt*2] = vertex[vid].u;
438+ TexCoordAry[1 + cnt*2] = vertex[vid].v;
439+
440+ VertexAry[3 + cnt*3] = vertex[vid].x;
441+ VertexAry[4 + cnt*3] = vertex[vid].y;
442+ VertexAry[5 + cnt*3] = vertex[vid].z;
443+ TexCoordAry[2 + cnt*2] = vertex[vid].u;
444+ TexCoordAry[3 + cnt*2] = vertex[vid].v;
445+
446+ vid = index[i*5+4];
447+ VertexAry[6 + cnt*3] = vertex[vid].x;
448+ VertexAry[7 + cnt*3] = vertex[vid].y;
449+ VertexAry[8 + cnt*3] = vertex[vid].z;
450+ TexCoordAry[4 + cnt*2] = vertex[vid].u;
451+ TexCoordAry[5 + cnt*2] = vertex[vid].v;
452+
453+ vid = index[i*5+2];
454+ VertexAry[9 + cnt*3] = vertex[vid].x;
455+ VertexAry[10 + cnt*3] = vertex[vid].y;
456+ VertexAry[11 + cnt*3] = vertex[vid].z;
457+ TexCoordAry[6 + cnt*2] = vertex[vid].u;
458+ TexCoordAry[7 + cnt*2] = vertex[vid].v;
459+
460+ vid = index[i*5+3];
461+ VertexAry[12 + cnt*3] = vertex[vid].x;
462+ VertexAry[13 + cnt*3] = vertex[vid].y;
463+ VertexAry[14 + cnt*3] = vertex[vid].z;
464+ TexCoordAry[8 + cnt*2] = vertex[vid].u;
465+ TexCoordAry[9 + cnt*2] = vertex[vid].v;
466+
467+ VertexAry[15 + cnt*3] = vertex[vid].x;
468+ VertexAry[16 + cnt*3] = vertex[vid].y;
469+ VertexAry[17 + cnt*3] = vertex[vid].z;
470+ TexCoordAry[10 + cnt*2] = vertex[vid].u;
471+ TexCoordAry[11 + cnt*2] = vertex[vid].v;
472+
473+ cnt += 6;
474+ }
475+ }
476+
477+ //色情報配列を用意
478+ ColorAry[0] = 1.0f;
479+ ColorAry[1] = 1.0f;
480+ ColorAry[2] = 1.0f;
481+ ColorAry[3] = 1.0f;
482+ for(int i=1; i<cnt; i++){
483+ memcpy(&(ColorAry[i*4]), ColorAry, sizeof(float)*4);
484+ }
485+
486+ delete [] vertex;
487+ delete [] index;
488+
489+ pmodel[id].useflag = true;
490+ pmodel[id].polygons = polygons;
491+ pmodel[id].VertexAry = VertexAry;
492+ pmodel[id].ColorAry = ColorAry;
493+ pmodel[id].TexCoordAry = TexCoordAry;
494+
495+#ifdef ENABLE_DEBUGLOG
496+ //ログに出力
497+ OutputLog.WriteLog(LOG_COMPLETE, "", id);
498+#endif
499+ return id;
500+}
501+
502+//! @brief モデルファイルの中間データを作成(モーフィング)
503+//! @param idA モデルAの認識番号
504+//! @param idB モデルBの認識番号
505+//! @return 成功:新しいモデル認識番号(0以上) 失敗:-1
506+//! @attention モデルAとモデルBは、頂点数・ポリゴン数・インデックスが同じである必要があります。
507+//! @attention それぞれのモデルデータが正しくないか 頂点数が異なる場合、実行に失敗します。
508+int D3DGraphics::MorphingModel(int idA, int idB)
509+{
510+#ifdef ENABLE_DEBUGLOG
511+ char str[128];
512+ sprintf(str, "中間データ作成  ID:%d and %d", idA, idB);
513+
514+ //ログに出力
515+ OutputLog.WriteLog(LOG_LOAD, "モデル", str);
516+#endif
517+
518+ /*
519+ //データが正しいか調べる
520+ if( (idA < 0)||((MAX_MODEL -1) < idA) ){ return -1; }
521+ if( pmodel[idA].useflag == false ){ return; }
522+ if( (idB < 0)||((MAX_MODEL -1) < idB) ){ return -1; }
523+ if( pmodel[idB].useflag == false ){ return; }
524+
525+ int id = -1;
526+
527+ //空いている認識番号を探す
528+ for(int i=0; i<MAX_TEXTURE; i++){
529+ if( pmodel[i].useflag == false ){
530+ id = i;
531+ break;
532+ }
533+ }
534+ if( id == -1 ){ return -1; }
535+
536+ return -1;
537+ */
538+
539+#ifdef ENABLE_DEBUGLOG
540+ //ログに出力
541+ OutputLog.WriteLog(LOG_ERROR, "", "");
542+#endif
543+
544+ return idA;
545+}
546+
547+//! @brief モデルファイルを解放
548+//! @param id モデル認識番号
549+void D3DGraphics::CleanupModel(int id)
550+{
551+ if( (id < 0)||((MAX_MODEL -1) < id) ){ return; }
552+ if( pmodel[id].useflag == false ){ return; }
553+
554+ delete pmodel[id].VertexAry;
555+ delete pmodel[id].ColorAry;
556+ delete pmodel[id].TexCoordAry;
557+ pmodel[id].useflag = false;
558+
559+#ifdef ENABLE_DEBUGLOG
560+ //ログに出力
561+ OutputLog.WriteLog(LOG_CLEANUP, "モデル", id);
562+#endif
563+}
564+
565+//! @brief テクスチャを読み込む
566+//! @param filename ファイル名
567+//! @param texturefont テクスチャフォントフラグ
568+//! @param BlackTransparent 黒を透過する
569+//! @return 成功:テクスチャ認識番号(0以上) 失敗:-1
570+int D3DGraphics::LoadTexture(char* filename, bool texturefont, bool BlackTransparent)
571+{
572+ int id = -1;
573+ char filename2[MAX_PATH];
574+ int format = 0;
575+
576+#ifdef ENABLE_DEBUGLOG
577+ //ログに出力
578+ OutputLog.WriteLog(LOG_LOAD, "テクスチャ", filename);
579+#endif
580+
581+ //空いている認識番号を探す
582+ for(int i=0; i<MAX_TEXTURE; i++){
583+ if( ptextures[i].useflag == false ){
584+ id = i;
585+ break;
586+ }
587+ }
588+ if( id == -1 ){ return -1; }
589+
590+ //ファイル名を小文字へ変換(拡張子判定用)
591+ for(int i=0; i<strlen(filename); i++){
592+ filename2[i] = (char)tolower(filename[i]);
593+ }
594+ filename2[ strlen(filename) ] = NULL;
595+
596+ //拡張子でファイルフォーマットを判定
597+ for(int i=strlen(filename2)-1; i>0; i--){
598+ if( filename2[i] == '.' ){
599+ if( strcmp(&(filename2[i]), ".bmp") == 0 ){
600+ format = 1;
601+ }
602+ if( strcmp(&(filename2[i]), ".dds") == 0 ){
603+ format = 2;
604+ }
605+ if( strcmp(&(filename2[i]), ".jpeg") == 0 ){
606+ format = 3;
607+ }
608+ if( strcmp(&(filename2[i]), ".jpg") == 0 ){
609+ format = 3;
610+ }
611+ if( strcmp(&(filename2[i]), ".png") == 0 ){
612+ format = 4;
613+ }
614+ break;
615+ }
616+ }
617+
618+ //対応してないフォーマット
619+ if( format == 0 ){ return -1; }
620+
621+ if( format == 1 ){ // .bmp
622+ if( LoadBMPTexture(filename, BlackTransparent, &(ptextures[id])) == false ){
623+ return -1;
624+ }
625+ }
626+ if( format == 2 ){ // .dds
627+ if( LoadDDSTexture(filename, BlackTransparent, &(ptextures[id])) == false ){
628+ return -1;
629+ }
630+ }
631+ if( format == 3 ){ // .jpeg
632+ if( LoadJPEGTexture(filename, BlackTransparent, &(ptextures[id])) == false ){
633+ return -1;
634+ }
635+ }
636+ if( format == 4 ){ // .png
637+ if( LoadPNGTexture(filename, BlackTransparent, &(ptextures[id])) == false ){
638+ return -1;
639+ }
640+ }
641+
642+
643+ //テクスチャ有効
644+ glEnable(GL_TEXTURE_2D);
645+
646+ HDC hDC;
647+ hDC = GetDC(hWnd);
648+ wglMakeCurrent(hDC, hGLRC);
649+ glGenTextures(1 , &(textureobjname[id]));
650+ ReleaseDC(hWnd, hDC);
651+
652+ glBindTexture(GL_TEXTURE_2D, textureobjname[id]);
653+
654+ //OpenGLにセット
655+ int width = ptextures[id].width;
656+ int height = ptextures[id].height;
657+ unsigned char *data = ptextures[id].data;
658+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
659+
660+ //ミップマップ設定
661+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
662+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
663+
664+ //乗算合成
665+ glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
666+
667+ //テクスチャ無効
668+ glDisable(GL_TEXTURE_2D);
669+
670+#ifdef ENABLE_DEBUGLOG
671+ //ログに出力
672+ OutputLog.WriteLog(LOG_COMPLETE, "", id);
673+#endif
674+
675+ return id;
676+
677+
678+ /*
679+ unsigned char *data = new unsigned char [16*4];
680+
681+ data[0*4 + 0] = 255; data[0*4 + 1] = 255; data[0*4 + 2] = 255; data[0*4 + 3] = 255;
682+ data[1*4 + 0] = 0; data[1*4 + 1] = 0; data[1*4 + 2] = 0; data[1*4 + 3] = 255;
683+ data[2*4 + 0] = 255; data[2*4 + 1] = 255; data[2*4 + 2] = 255; data[2*4 + 3] = 255;
684+ data[3*4 + 0] = 0; data[3*4 + 1] = 0; data[3*4 + 2] = 0; data[3*4 + 3] = 255;
685+ data[4*4 + 0] = 255; data[4*4 + 1] = 0; data[4*4 + 2] = 0; data[4*4 + 3] = 255;
686+ data[5*4 + 0] = 0; data[5*4 + 1] = 255; data[5*4 + 2] = 0; data[5*4 + 3] = 255;
687+ data[6*4 + 0] = 0; data[6*4 + 1] = 0; data[6*4 + 2] = 255; data[6*4 + 3] = 255;
688+ data[7*4 + 0] = 0; data[7*4 + 1] = 0; data[7*4 + 2] = 0; data[7*4 + 3] = 255;
689+ data[8*4 + 0] = 128; data[8*4 + 1] = 0; data[8*4 + 2] = 0; data[8*4 + 3] = 255;
690+ data[9*4 + 0] = 0; data[9*4 + 1] = 128; data[9*4 + 2] = 0; data[9*4 + 3] = 255;
691+ data[10*4 + 0] = 0; data[10*4 + 1] = 0; data[10*4 + 2] = 128; data[10*4 + 3] = 255;
692+ data[11*4 + 0] = 0; data[11*4 + 1] = 0; data[11*4 + 2] = 0; data[11*4 + 3] = 255;
693+ data[12*4 + 0] = 255; data[12*4 + 1] = 255; data[12*4 + 2] = 0; data[12*4 + 3] = 255;
694+ data[13*4 + 0] = 255; data[13*4 + 1] = 0; data[13*4 + 2] = 255; data[13*4 + 3] = 255;
695+ data[14*4 + 0] = 0; data[14*4 + 1] = 255; data[14*4 + 2] = 255; data[14*4 + 3] = 255;
696+ data[15*4 + 0] = 255; data[15*4 + 1] = 255; data[15*4 + 2] = 255; data[15*4 + 3] = 255;
697+
698+ ptextures[id].data = data;
699+ ptextures[id].width = 4;
700+ ptextures[id].height = 4;
701+
702+ ptextures[id].useflag = true;
703+
704+ return id;
705+ */
706+}
707+
708+//! @brief BMPファイルをを読み込む
709+//! @param filename ファイル名
710+//! @param BlackTransparent 黒を透過する
711+//! @param ptexture 受け取るTEXTUREDATA構造体のポインタ
712+//! @return 成功:true 失敗:false
713+//! @attention 通常は LoadTexture()関数 から呼びだすこと。
714+bool D3DGraphics::LoadBMPTexture(char* filename, bool BlackTransparent, TEXTUREDATA *ptexture)
715+{
716+ FILE *fp;
717+ unsigned char header[54];
718+ unsigned int dataPos;
719+ unsigned int width, height;
720+ unsigned int index;
721+
722+ //ファイルを読み込む
723+ fp = fopen(filename, "rb");
724+ if( fp == NULL ){
725+ return false; //ファイルが読めない
726+ }
727+
728+ //ヘッダーを読む
729+ fread(header, 1, 54, fp);
730+
731+ if( (header[0x00] != 'B')||(header[0x01] != 'M') ){
732+ fclose(fp);
733+ return false; //.bmpではない
734+ }
735+
736+ // バイト配列から整数を読み込む
737+ dataPos = *(int*)&(header[0x0E]) + 14;
738+ width = *(int*)&(header[0x12]);
739+ height = *(int*)&(header[0x16]);
740+ index = *(int*)&(header[0x1C]);
741+
742+ //実データの先頭まで移動
743+ fseek(fp, dataPos, SEEK_SET);
744+
745+ unsigned char *data = new unsigned char [width*height*4];
746+
747+ //各ピクセル8ビットなら、256色パレットモード
748+ if( index == 8 ){
749+ unsigned char pixel;
750+ unsigned char *pallet = new unsigned char [256*4];
751+ fread(pallet, 1, 256*4, fp);
752+
753+ for(int h=height-1; h>=0; h--){
754+ for(int w=0; w<width; w++){
755+ fread(&pixel, 1, 1, fp);
756+
757+ data[(h*width+w)*4 + 0] = pallet[pixel*4 + 2];
758+ data[(h*width+w)*4 + 1] = pallet[pixel*4 + 1];
759+ data[(h*width+w)*4 + 2] = pallet[pixel*4 + 0];
760+ data[(h*width+w)*4 + 3] = 255;
761+
762+ if( BlackTransparent == true ){
763+ //黒ならば透過する
764+ if( (data[(h*width+w)*4 + 0] == 0)&&(data[(h*width+w)*4 + 1] == 0)&&(data[(h*width+w)*4 + 2] == 0) ){
765+ data[(h*width+w)*4 + 3] = 0;
766+ }
767+ }
768+ }
769+ }
770+
771+ delete []pallet;
772+ }
773+
774+ //各ピクセル24ビットなら、フルカラー
775+ if( index == 24 ){
776+ unsigned char pixel[3];
777+ for(int h=height-1; h>=0; h--){
778+ for(int w=0; w<width; w++){
779+ fread(&pixel, 1, 3, fp);
780+
781+ data[(h*width+w)*4 + 0] = pixel[2];
782+ data[(h*width+w)*4 + 1] = pixel[1];
783+ data[(h*width+w)*4 + 2] = pixel[0];
784+ data[(h*width+w)*4 + 3] = 255;
785+
786+ if( BlackTransparent == true ){
787+ //黒ならば透過する
788+ if( (data[(h*width+w)*4 + 0] == 0)&&(data[(h*width+w)*4 + 1] == 0)&&(data[(h*width+w)*4 + 2] == 0) ){
789+ data[(h*width+w)*4 + 3] = 0;
790+ }
791+ }
792+ }
793+ }
794+ }
795+
796+ //ファイルハンドルを解放
797+ fclose( fp );
798+
799+ //構造体に代入
800+ ptexture->data = data;
801+ ptexture->width = width;
802+ ptexture->height = height;
803+
804+ ptexture->useflag = true;
805+
806+ return true;
807+}
808+
809+//! @brief DDSファイルをを読み込む
810+//! @param filename ファイル名
811+//! @param BlackTransparent 黒を透過する
812+//! @param ptexture 受け取るTEXTUREDATA構造体のポインタ
813+//! @return 成功:true 失敗:false
814+//! @attention 通常は LoadTexture()関数 から呼びだすこと。
815+bool D3DGraphics::LoadDDSTexture(char* filename, bool BlackTransparent, TEXTUREDATA *ptexture)
816+{
817+ FILE *fp;
818+ unsigned char header[124+4];
819+ unsigned int width, height;
820+ unsigned int index;
821+
822+ //ファイルを読み込む
823+ fp = fopen(filename, "rb");
824+ if( fp == NULL ){
825+ return false; //ファイルが読めない
826+ }
827+
828+ //ヘッダーを読む
829+ fread(header, 1, 124+4, fp);
830+
831+ if( (header[0x00] != 'D')||(header[0x01] != 'D')||(header[0x02] != 'S')||(header[0x03] != ' ') ){
832+ fclose(fp);
833+ return false; //.ddsではない
834+ }
835+
836+ // バイト配列から整数を読み込む
837+ width = *(int*)&(header[0x10]);
838+ height = *(int*)&(header[0x0C]);
839+ index = *(int*)&(header[0x58]);
840+
841+ if( (index != 16)&&(index != 32) ){
842+ fclose(fp);
843+ return false; //対応してないフォーマット
844+ }
845+
846+ unsigned char *data = new unsigned char [width*height*4];
847+
848+ for(int h=0; h<height; h++){
849+ for(int w=0; w<width; w++){
850+ unsigned char pixel[4];
851+ fread(&pixel, 1, index/8, fp);
852+
853+ if( index == 16 ){ //各ピクセル16ビット
854+ data[(h*width+w)*4 + 0] = (pixel[1]<<4)&0xF0;
855+ data[(h*width+w)*4 + 1] = pixel[0]&0xF0;
856+ data[(h*width+w)*4 + 2] = (pixel[0]<<4)&0xF0;
857+ data[(h*width+w)*4 + 3] = pixel[1]&0xF0;
858+ }
859+ if( index == 32 ){ //各ピクセル32ビット
860+ data[(h*width+w)*4 + 0] = pixel[2];
861+ data[(h*width+w)*4 + 1] = pixel[1];
862+ data[(h*width+w)*4 + 2] = pixel[0];
863+ data[(h*width+w)*4 + 3] = pixel[3];
864+ }
865+
866+ if( BlackTransparent == true ){
867+ //黒ならば透過する
868+ if( (data[(h*width+w)*4 + 0] == 0)&&(data[(h*width+w)*4 + 1] == 0)&&(data[(h*width+w)*4 + 2] == 0) ){
869+ data[(h*width+w)*4 + 3] = 0;
870+ }
871+ }
872+ }
873+ }
874+
875+ //ファイルハンドルを解放
876+ fclose( fp );
877+
878+ //構造体に代入
879+ ptexture->data = data;
880+ ptexture->width = width;
881+ ptexture->height = height;
882+
883+ ptexture->useflag = true;
884+
885+ return true;
886+}
887+
888+//! @brief JPEGファイルをを読み込む
889+//! @param filename ファイル名
890+//! @param BlackTransparent 黒を透過する
891+//! @param ptexture 受け取るTEXTUREDATA構造体のポインタ
892+//! @return 成功:true 失敗:false
893+//! @attention 通常は LoadTexture()関数 から呼びだすこと。
894+bool D3DGraphics::LoadJPEGTexture(char* filename, bool BlackTransparent, TEXTUREDATA *ptexture)
895+{
896+ FILE *fp;
897+ JSAMPARRAY img;
898+ unsigned int width, height;
899+
900+ //ファイルを読み込む
901+ fp = fopen(filename, "rb");
902+ if( fp == NULL ){
903+ return false; //ファイルが読めない
904+ }
905+ jpeg_stdio_src(&cinfo, fp);
906+
907+ //パラメータの設定
908+ jpeg_read_header(&cinfo, true);
909+
910+ //展開開始
911+ jpeg_start_decompress(&cinfo);
912+
913+ //領域確保
914+ img = (JSAMPARRAY)new JSAMPROW [cinfo.output_height];
915+ for(int i=0; i<cinfo.output_height; i++){
916+ img[i] = (JSAMPROW)new JSAMPLE [cinfo.output_width * cinfo.out_color_components];
917+ }
918+
919+ //展開
920+ while( cinfo.output_scanline < cinfo.output_height ) {
921+ jpeg_read_scanlines(&cinfo, img + cinfo.output_scanline, cinfo.output_height - cinfo.output_scanline);
922+ }
923+
924+ //展開終了
925+ jpeg_finish_decompress(&cinfo);
926+
927+
928+ //ファイルハンドルを解放
929+ fclose( fp );
930+
931+
932+ // バイト配列から整数を読み込む
933+ width = (int)cinfo.output_width;
934+ height = (int)cinfo.output_height;
935+
936+ unsigned char *data = new unsigned char [width*height*4];
937+
938+ for(int h=0; h<height; h++){
939+ //1ライン分取得
940+ JSAMPROW p = img[h];
941+
942+ for(int w=0; w<width; w++){
943+ data[(h*width+w)*4 + 0] = p[w*3 + 0];
944+ data[(h*width+w)*4 + 1] = p[w*3 + 1];
945+ data[(h*width+w)*4 + 2] = p[w*3 + 2];
946+ data[(h*width+w)*4 + 3] = 255;
947+
948+ if( BlackTransparent == true ){
949+ //黒ならば透過する
950+ if( (data[(h*width+w)*4 + 0] == 0)&&(data[(h*width+w)*4 + 1] == 0)&&(data[(h*width+w)*4 + 2] == 0) ){
951+ data[(h*width+w)*4 + 3] = 0;
952+ }
953+ }
954+
955+ }
956+ }
957+
958+ //領域解放
959+ for(int i=0; i<cinfo.output_height; i++){
960+ delete [] img[i];
961+ }
962+ delete [] img;
963+
964+ //構造体に代入
965+ ptexture->data = data;
966+ ptexture->width = width;
967+ ptexture->height = height;
968+
969+ ptexture->useflag = true;
970+
971+ return true;
972+}
973+
974+//! @brief PNGファイルをを読み込む
975+//! @param filename ファイル名
976+//! @param BlackTransparent 黒を透過する
977+//! @param ptexture 受け取るTEXTUREDATA構造体のポインタ
978+//! @return 成功:true 失敗:false
979+//! @attention 通常は LoadTexture()関数 から呼びだすこと。
980+bool D3DGraphics::LoadPNGTexture(char* filename, bool BlackTransparent, TEXTUREDATA *ptexture)
981+{
982+ FILE *fp;
983+ png_byte sig[4];
984+ png_structp pPng;
985+ png_infop pInfo;
986+ unsigned int width, height;
987+
988+ //ファイルを読み込む
989+ fp = fopen(filename, "rb");
990+ if( fp == NULL ){
991+ return false; //ファイルが読めない
992+ }
993+
994+ //PNGファイルか判定
995+ fread(sig, 4, 1, fp);
996+ if( png_sig_cmp(sig, 0, 4) != 0 ){
997+ fclose(fp);
998+ return false; //.pngではない
999+ }
1000+
1001+ //構造体を初期化
1002+ pPng = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1003+ pInfo = png_create_info_struct(pPng);
1004+
1005+ //情報を取得
1006+ png_init_io(pPng, fp);
1007+ png_set_sig_bytes(pPng, 4);
1008+ png_read_info(pPng, pInfo);
1009+
1010+ //テクスチャの大きさを取得
1011+ width = png_get_image_width(pPng, pInfo);
1012+ height = png_get_image_height(pPng, pInfo);
1013+
1014+ //インターレス方式か判定
1015+ if( png_get_interlace_type(pPng, pInfo) != PNG_INTERLACE_NONE ){
1016+ png_destroy_read_struct(&pPng, &pInfo, (png_infopp)NULL);
1017+ fclose(fp);
1018+ return false; //インターレスには対応しない。
1019+ }
1020+
1021+ //アルファチャンネルを初期化
1022+ png_set_add_alpha(pPng, 0xff, PNG_FILLER_AFTER);
1023+
1024+ // tRNSチャンクがあれば、アルファチャンネルに変換
1025+ if (png_get_valid(pPng, pInfo, PNG_INFO_tRNS)) {
1026+ png_set_tRNS_to_alpha(pPng);
1027+ }
1028+
1029+ unsigned char *data = new unsigned char [width*height*4];
1030+
1031+ //1ライン分の作業領域を確保
1032+ png_bytep buf = new png_byte[width*4];
1033+
1034+ for(int h=0; h<height; h++){
1035+ //1ライン分取得
1036+ png_read_row(pPng,buf,NULL);
1037+
1038+ for(int w=0; w<width; w++){
1039+ data[(h*width+w)*4 + 0] = buf[w*4 + 0];
1040+ data[(h*width+w)*4 + 1] = buf[w*4 + 1];
1041+ data[(h*width+w)*4 + 2] = buf[w*4 + 2];
1042+ data[(h*width+w)*4 + 3] = buf[w*4 + 3];
1043+
1044+ if( BlackTransparent == true ){
1045+ //黒ならば透過する
1046+ if( (data[(h*width+w)*4 + 0] == 0)&&(data[(h*width+w)*4 + 1] == 0)&&(data[(h*width+w)*4 + 2] == 0) ){
1047+ data[(h*width+w)*4 + 3] = 0;
1048+ }
1049+ }
1050+ }
1051+ }
1052+
1053+ //1ライン分の作業領域を解放
1054+ delete [] buf;
1055+
1056+ //解放
1057+ png_read_end(pPng, NULL);
1058+ png_destroy_read_struct(&pPng, &pInfo, (png_infopp)NULL);
1059+
1060+ //ファイルハンドルを解放
1061+ fclose( fp );
1062+
1063+ //構造体に代入
1064+ ptexture->data = data;
1065+ ptexture->width = width;
1066+ ptexture->height = height;
1067+
1068+ ptexture->useflag = true;
1069+
1070+ return true;
1071+}
1072+
1073+//! @brief テクスチャのサイズを取得
1074+//! @param id テクスチャ認識番号
1075+//! @param width 幅を受け取るポインタ
1076+//! @param height 高さを受け取るポインタ
1077+//! @return 成功:0 失敗:1
1078+//! @attention サーフェイスのサイズを取得します。GPUにロードされたサイズであり、テクスチャ(現物)と異なる場合があります。
1079+int D3DGraphics::GetTextureSize(int id, int *width, int *height)
1080+{
1081+ //無効な認識番号が指定されていたら、処理せず返す。
1082+ if( id == -1 ){ return 1; }
1083+ if( ptextures[id].useflag == false ){ return 1; }
1084+
1085+ *width = ptextures[id].width;
1086+ *height = ptextures[id].height;
1087+
1088+ return 0;
1089+}
1090+
1091+//! @brief テクスチャを指定
1092+//! @param TextureID テクスチャ認識番号
1093+void D3DGraphics::SetTexture(int TextureID)
1094+{
1095+ if( now_textureid == TextureID ){
1096+ return;
1097+ }
1098+
1099+ //OpenGLにセット
1100+ glBindTexture(GL_TEXTURE_2D, textureobjname[TextureID]);
1101+
1102+ now_textureid = TextureID;
1103+}
1104+
1105+//! @brief テクスチャを解放
1106+//! @param id テクスチャ認識番号
1107+void D3DGraphics::CleanupTexture(int id)
1108+{
1109+ if( (id < 0)||((MAX_TEXTURE -1) < id) ){ return; }
1110+ if( ptextures[id].useflag == false ){ return; }
1111+
1112+ delete ptextures[id].data;
1113+ glDeleteTextures(1 , &(textureobjname[id]));
1114+ ptextures[id].useflag = false;
1115+
1116+#ifdef ENABLE_DEBUGLOG
1117+ //ログに出力
1118+ OutputLog.WriteLog(LOG_CLEANUP, "テクスチャ", id);
1119+#endif
1120+}
1121+
1122+//! @brief 全ての描画処理を開始
1123+//! @return 成功:0 失敗:1
1124+//! @attention 描画処理の最初に呼び出す必要があります。
1125+int D3DGraphics::StartRender()
1126+{
1127+ HDC hDC;
1128+
1129+ hDC = BeginPaint(hWnd, &Paint_ps);
1130+
1131+ //コンテキストを指定
1132+ wglMakeCurrent(hDC, hGLRC);
1133+
1134+ //初期化
1135+ glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
1136+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1137+
1138+ glEnable(GL_DEPTH_TEST);
1139+ glEnable(GL_CULL_FACE);
1140+
1141+ //混合処理(透過有効化)
1142+ glEnable(GL_BLEND);
1143+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1144+
1145+ //アルファテスト
1146+ glAlphaFunc(GL_GREATER, 0.0f);
1147+ glEnable(GL_ALPHA_TEST);
1148+
1149+ return 0;
1150+}
1151+
1152+//! @brief 全ての描画処理を終了
1153+//! @return 成功:false 失敗:true
1154+//! @attention 描画処理の最後に呼び出す必要があります。
1155+bool D3DGraphics::EndRender()
1156+{
1157+ glFlush();
1158+
1159+ wglMakeCurrent(NULL, NULL);
1160+
1161+ EndPaint(hWnd, &Paint_ps);
1162+
1163+ return false;
1164+}
1165+
1166+//! @brief Zバッファをリセット
1167+void D3DGraphics::ResetZbuffer()
1168+{
1169+ glClear(GL_DEPTH_BUFFER_BIT);
1170+}
1171+
1172+//! @brief ワールド空間を原点(0,0,0)に戻す など
1173+void D3DGraphics::ResetWorldTransform()
1174+{
1175+ glMatrixMode(GL_PROJECTION);
1176+ glLoadIdentity();
1177+ gluPerspective(viewangle*(180.0f/(float)M_PI), (float)width/height, CLIPPINGPLANE_NEAR, CLIPPINGPLANE_FAR);
1178+
1179+ glMatrixMode(GL_MODELVIEW);
1180+ glLoadIdentity();
1181+ gluLookAt(camera_x*-1, camera_y, camera_z, camera_x*-1 + cos(camera_rx*-1 + (float)M_PI)*cos(camera_ry), camera_y + sin(camera_ry), camera_z + sin(camera_rx*-1 + (float)M_PI)*cos(camera_ry), 0.0f, 1.0f, 0.0f);
1182+}
1183+
1184+//! @brief ワールド空間の座標・角度・拡大率を設定
1185+//! @param x X座標
1186+//! @param y Y座標
1187+//! @param z Z座標
1188+//! @param rx 横軸角度
1189+//! @param ry 縦軸角度
1190+//! @param size 拡大率
1191+void D3DGraphics::SetWorldTransform(float x, float y, float z, float rx, float ry, float size)
1192+{
1193+ SetWorldTransform(x, y, z, rx, ry, 0.0f, size);
1194+}
1195+
1196+//! @brief ワールド空間の座標・角度・拡大率を設定
1197+//! @param x X座標
1198+//! @param y Y座標
1199+//! @param z Z座標
1200+//! @param rx 横軸角度
1201+//! @param ry1 縦軸角度
1202+//! @param ry2 縦軸角度
1203+//! @param size 拡大率
1204+void D3DGraphics::SetWorldTransform(float x, float y, float z, float rx, float ry1, float ry2, float size)
1205+{
1206+ ResetWorldTransform();
1207+
1208+ glMatrixMode(GL_MODELVIEW);
1209+
1210+ glTranslatef(x*-1, y, z);
1211+ glRotatef(rx*-1*(180.0f/(float)M_PI), 0.0f, 1.0f, 0.0f);
1212+ glRotatef(ry1*(180.0f/(float)M_PI), 1.0f, 0.0f, 0.0f);
1213+ glRotatef(ry2*-1*(180.0f/(float)M_PI), 0.0f, 0.0f, 1.0f);
1214+ glScalef(size, size, size);
1215+}
1216+
1217+//! @brief ワールド空間の座標・角度・拡大率を設定(エフェクト用)
1218+//! @param x X座標
1219+//! @param y Y座標
1220+//! @param z Z座標
1221+//! @param rx 横軸角度
1222+//! @param ry 縦軸角度
1223+//! @param rt 回転角度
1224+//! @param size 拡大率
1225+void D3DGraphics::SetWorldTransformEffect(float x, float y, float z, float rx, float ry, float rt, float size)
1226+{
1227+ ResetWorldTransform();
1228+
1229+ glMatrixMode(GL_MODELVIEW);
1230+
1231+ glTranslatef(x*-1, y, z);
1232+ glRotatef(rx*-1*(180.0f/(float)M_PI), 0.0f, 1.0f, 0.0f);
1233+ glRotatef(ry*-1*(180.0f/(float)M_PI), 0.0f, 0.0f, 1.0f);
1234+ glRotatef(rt*(180.0f/(float)M_PI), 1.0f, 0.0f, 0.0f);
1235+ glScalef(size, size, size);
1236+}
1237+
1238+//! @brief ワールド空間を人が武器を持つ場所に設定
1239+//! @param x X座標
1240+//! @param y Y座標
1241+//! @param z Z座標
1242+//! @param mx 手元を原点にした モデルのX座標
1243+//! @param my 手元を原点にした モデルのY座標
1244+//! @param mz 手元を原点にした モデルのZ座標
1245+//! @param rx 横軸角度
1246+//! @param ry 縦軸角度
1247+//! @param size 拡大率
1248+void D3DGraphics::SetWorldTransformHumanWeapon(float x, float y, float z, float mx, float my, float mz, float rx, float ry, float size)
1249+{
1250+ ResetWorldTransform();
1251+
1252+ glMatrixMode(GL_MODELVIEW);
1253+
1254+ glTranslatef(x*-1, y, z);
1255+ glRotatef(rx*-1*(180.0f/(float)M_PI), 0.0f, 1.0f, 0.0f);
1256+ glRotatef(ry*(180.0f/(float)M_PI), 1.0f, 0.0f, 0.0f);
1257+ glTranslatef(mx*-1, my, mz);
1258+ glScalef(size, size, size);
1259+}
1260+
1261+//! @brief ワールド空間を所持している武器を表示する場所に設定
1262+//! @param rotation 武器を回転させる
1263+//! @param camera_x カメラのX座標
1264+//! @param camera_y カメラのY座標
1265+//! @param camera_z カメラのZ座標
1266+//! @param camera_rx カメラの横軸角度
1267+//! @param camera_ry カメラの縦軸角度
1268+//! @param rx 武器のの縦軸角度
1269+//! @param size 表示サイズ
1270+//! @note rotation・・ true:現在持っている武器です。 false:予備の武器です。(rx は無視されます)
1271+//! @todo 位置やサイズの微調整
1272+void D3DGraphics::SetWorldTransformPlayerWeapon(bool rotation, float camera_x, float camera_y, float camera_z, float camera_rx, float camera_ry, float rx, float size)
1273+{
1274+ size = size * 0.3f;
1275+
1276+ ResetWorldTransform();
1277+
1278+ glMatrixMode(GL_MODELVIEW);
1279+
1280+ glTranslatef(camera_x*-1, camera_y, camera_z);
1281+ glRotatef(camera_rx*(180.0f/(float)M_PI), 0.0f, 1.0f, 0.0f);
1282+ glRotatef(camera_ry*-1*(180.0f/(float)M_PI), 0.0f, 0.0f, 1.0f);
1283+
1284+ if( rotation == true ){
1285+ glTranslatef(HUD_myweapon_x[0]*-1, HUD_myweapon_y[0], HUD_myweapon_z[0]);
1286+ glRotatef(rx*-1*(180.0f/(float)M_PI), 0.0f, 1.0f, 0.0f);
1287+ }
1288+ else{
1289+ glTranslatef(HUD_myweapon_x[1]*-1, HUD_myweapon_y[1], HUD_myweapon_z[1]);
1290+ glRotatef(180, 0.0f, 1.0f, 0.0f);
1291+ }
1292+ glScalef(size, size, size);
1293+}
1294+
1295+//! @brief ワールド空間の座標を取得
1296+//! @param *x x軸を受け取るポインタ
1297+//! @param *y y軸を受け取るポインタ
1298+//! @param *z z軸を受け取るポインタ
1299+void D3DGraphics::GetWorldTransformPos(float *x, float *y, float *z)
1300+{
1301+ GLfloat model[16];
1302+ glMatrixMode(GL_MODELVIEW);
1303+ glGetFloatv(GL_MODELVIEW_MATRIX, model);
1304+ *x = model[12];
1305+ *y = model[13];
1306+ *z = model[14];
1307+}
1308+
1309+//! @brief フォグを設定
1310+//! @param skynumber 空の番号
1311+void D3DGraphics::SetFog(int skynumber)
1312+{
1313+ float fogColor[4];
1314+
1315+ //空の番号により色を決定
1316+ switch(skynumber){
1317+ case 1: fogColor[0] = 0.25f; fogColor[1] = 0.25f+0.0625; fogColor[2] = 0.25f; fogColor[3] = 1.0f; break;
1318+ case 2: fogColor[0] = 0.0625; fogColor[1] = 0.0625; fogColor[2] = 0.0625; fogColor[3] = 1.0f; break;
1319+ case 3: fogColor[0] = 0.0f; fogColor[1] = 0.0625; fogColor[2] = 0.125; fogColor[3] = 1.0f; break;
1320+ case 4: fogColor[0] = 0.125; fogColor[1] = 0.0625; fogColor[2] = 0.0625; fogColor[3] = 1.0f; break;
1321+ case 5: fogColor[0] = 0.25f; fogColor[1] = 0.125; fogColor[2] = 0.125; fogColor[3] = 1.0f; break;
1322+ default: fogColor[0] = 0.0f; fogColor[1] = 0.0f; fogColor[2] = 0.0f; fogColor[3] = 1.0f; break;
1323+ }
1324+
1325+ float fog_st = 100;
1326+ float fog_end = 800;
1327+ glFogi(GL_FOG_MODE, GL_LINEAR);
1328+ glFogfv(GL_FOG_COLOR, fogColor);
1329+ glHint(GL_FOG_HINT, GL_NICEST);
1330+ glFogf(GL_FOG_START, fog_st);
1331+ glFogf(GL_FOG_END, fog_end);
1332+
1333+ glEnable(GL_FOG);
1334+}
1335+
1336+//! @brief カメラ(視点)を設定
1337+//! @param in_camera_x カメラのX座標
1338+//! @param in_camera_y カメラのY座標
1339+//! @param in_camera_z カメラのZ座標
1340+//! @param in_camera_rx カメラの横軸角度
1341+//! @param in_camera_ry カメラの縦軸角度
1342+//! @param in_viewangle 視野角
1343+void D3DGraphics::SetCamera(float in_camera_x, float in_camera_y, float in_camera_z, float in_camera_rx, float in_camera_ry, float in_viewangle)
1344+{
1345+ glViewport(0, 0, width, height);
1346+
1347+ camera_x = in_camera_x;
1348+ camera_y = in_camera_y;
1349+ camera_z = in_camera_z;
1350+ camera_rx = in_camera_rx;
1351+ camera_ry = in_camera_ry;
1352+ viewangle = in_viewangle;
1353+
1354+ ResetWorldTransform();
1355+}
1356+
1357+//! @brief マップデータを取り込む
1358+//! @param in_blockdata ブロックデータ
1359+//! @param directory ブロックデータが存在するディレクトリ
1360+void D3DGraphics::LoadMapdata(BlockDataInterface* in_blockdata, char *directory)
1361+{
1362+ //ブロックデータが指定されていなければ、処理しない。
1363+ if( in_blockdata == NULL ){ return; }
1364+
1365+ char fname[MAX_PATH];
1366+ char fnamefull[MAX_PATH];
1367+
1368+ //クラスを設定
1369+ blockdata = in_blockdata;
1370+
1371+ //ブロック数を取得
1372+ bs = blockdata->GetTotaldatas();
1373+
1374+ //テクスチャ読み込み
1375+ for(int i=0; i<TOTAL_BLOCKTEXTURE; i++){
1376+ //テクスチャ名を取得
1377+ blockdata->GetTexture(fname, i);
1378+
1379+ if( strcmp(fname, "") == 0 ){ //指定されていなければ、処理しない
1380+ mapTextureID[i] = -1;
1381+ }
1382+ else{
1383+ //「ディレクトリ+ファイル名」を生成し、読み込む
1384+ strcpy(fnamefull, directory);
1385+ strcat(fnamefull, fname);
1386+ mapTextureID[i] = LoadTexture(fnamefull, false, false);
1387+ }
1388+ }
1389+}
1390+
1391+//! @brief マップデータを描画
1392+//! @param wireframe ワイヤーフレーム表示
1393+void D3DGraphics::DrawMapdata(bool wireframe)
1394+{
1395+ //ブロックデータが読み込まれていなければ、処理しない。
1396+ if( blockdata == NULL ){ return; }
1397+
1398+ struct blockdata data;
1399+ int textureID;
1400+ int vID[4];
1401+ int uvID[4];
1402+ float *VertexAry = new float [bs*6 * 6*3];
1403+ float *ColorAry = new float [bs*6 * 6*4];
1404+ float *TexCoordAry = new float [bs*6 * 6*2];
1405+
1406+ if( wireframe == true ){
1407+ //ワイヤーフレーム表示
1408+ for(int i=0; i<bs; i++){
1409+ blockdata->Getdata(&data, i);
1410+ Drawline(data.x[0], data.y[0], data.z[0], data.x[1], data.y[1], data.z[1]);
1411+ Drawline(data.x[1], data.y[1], data.z[1], data.x[2], data.y[2], data.z[2]);
1412+ Drawline(data.x[2], data.y[2], data.z[2], data.x[3], data.y[3], data.z[3]);
1413+ Drawline(data.x[3], data.y[3], data.z[3], data.x[0], data.y[0], data.z[0]);
1414+ Drawline(data.x[4], data.y[4], data.z[4], data.x[5], data.y[5], data.z[5]);
1415+ Drawline(data.x[5], data.y[5], data.z[5], data.x[6], data.y[6], data.z[6]);
1416+ Drawline(data.x[6], data.y[6], data.z[6], data.x[7], data.y[7], data.z[7]);
1417+ Drawline(data.x[7], data.y[7], data.z[7], data.x[4], data.y[4], data.z[4]);
1418+ Drawline(data.x[0], data.y[0], data.z[0], data.x[4], data.y[4], data.z[4]);
1419+ Drawline(data.x[1], data.y[1], data.z[1], data.x[5], data.y[5], data.z[5]);
1420+ Drawline(data.x[2], data.y[2], data.z[2], data.x[6], data.y[6], data.z[6]);
1421+ Drawline(data.x[3], data.y[3], data.z[3], data.x[7], data.y[7], data.z[7]);
1422+ }
1423+ return;
1424+ }
1425+
1426+ //配列有効化
1427+ glEnableClientState(GL_VERTEX_ARRAY);
1428+ glEnableClientState(GL_COLOR_ARRAY);
1429+ glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1430+
1431+ for(textureID=0; textureID<TOTAL_BLOCKTEXTURE; textureID++){
1432+ int cnt = 0;
1433+
1434+ //テクスチャが正常に読み込めていなければ設定
1435+ if( mapTextureID[textureID] == -1 ){
1436+ //テクスチャ無効
1437+ glDisable(GL_TEXTURE_2D);
1438+ }
1439+ else if( ptextures[ mapTextureID[textureID] ].useflag == false ){
1440+ //テクスチャ無効
1441+ glDisable(GL_TEXTURE_2D);
1442+ }
1443+ else{
1444+ //テクスチャ有効
1445+ glEnable(GL_TEXTURE_2D);
1446+
1447+ //テクスチャをセット
1448+ SetTexture(mapTextureID[textureID]);
1449+ }
1450+
1451+ for(int i=0; i<bs; i++){
1452+ //データ取得
1453+ blockdata->Getdata(&data, i);
1454+
1455+ for(int j=0; j<6; j++){
1456+ //テクスチャ認識番号を取得
1457+ int ID = data.material[j].textureID;
1458+
1459+ if( textureID == ID ){
1460+ //面の頂点データの関連付けを取得
1461+ blockdataface(j, &vID[0], &uvID[0]);
1462+
1463+ //頂点配列を用意
1464+ VertexAry[0 + cnt*18] = data.x[ vID[1] ]*-1; VertexAry[1 + cnt*18] = data.y[ vID[1] ]; VertexAry[2 + cnt*18] = data.z[ vID[1] ];
1465+ VertexAry[3 + cnt*18] = data.x[ vID[1] ]*-1; VertexAry[4 + cnt*18] = data.y[ vID[1] ]; VertexAry[5 + cnt*18] = data.z[ vID[1] ];
1466+ VertexAry[6 + cnt*18] = data.x[ vID[0] ]*-1; VertexAry[7 + cnt*18] = data.y[ vID[0] ]; VertexAry[8 + cnt*18] = data.z[ vID[0] ];
1467+ VertexAry[9 + cnt*18] = data.x[ vID[2] ]*-1; VertexAry[10 + cnt*18] = data.y[ vID[2] ]; VertexAry[11 + cnt*18] = data.z[ vID[2] ];
1468+ VertexAry[12 + cnt*18] = data.x[ vID[3] ]*-1; VertexAry[13 + cnt*18] = data.y[ vID[3] ]; VertexAry[14 + cnt*18] = data.z[ vID[3] ];
1469+ VertexAry[15 + cnt*18] = data.x[ vID[3] ]*-1; VertexAry[16 + cnt*18] = data.y[ vID[3] ]; VertexAry[17 + cnt*18] = data.z[ vID[3] ];
1470+
1471+ //色情報配列を用意
1472+ ColorAry[0 + cnt*24] = data.material[j].shadow;
1473+ ColorAry[1 + cnt*24] = data.material[j].shadow;
1474+ ColorAry[2 + cnt*24] = data.material[j].shadow;
1475+ ColorAry[3 + cnt*24] = 1.0f;
1476+ for(int i=1; i<6; i++){
1477+ memcpy(&(ColorAry[i*4 + cnt*24]), ColorAry, sizeof(float)*4);
1478+ }
1479+
1480+ //UV座標配列を用意
1481+ TexCoordAry[0 + cnt*12] = data.material[j].u[ uvID[1] ]; TexCoordAry[1 + cnt*12] = data.material[j].v[ uvID[1] ];
1482+ TexCoordAry[2 + cnt*12] = data.material[j].u[ uvID[1] ]; TexCoordAry[3 + cnt*12] = data.material[j].v[ uvID[1] ];
1483+ TexCoordAry[4 + cnt*12] = data.material[j].u[ uvID[0] ]; TexCoordAry[5 + cnt*12] = data.material[j].v[ uvID[0] ];
1484+ TexCoordAry[6 + cnt*12] = data.material[j].u[ uvID[2] ]; TexCoordAry[7 + cnt*12] = data.material[j].v[ uvID[2] ];
1485+ TexCoordAry[8 + cnt*12] = data.material[j].u[ uvID[3] ]; TexCoordAry[9 + cnt*12] = data.material[j].v[ uvID[3] ];
1486+ TexCoordAry[10 + cnt*12] = data.material[j].u[ uvID[3] ]; TexCoordAry[11 + cnt*12] = data.material[j].v[ uvID[3] ];
1487+
1488+ cnt += 1;
1489+ }
1490+ }
1491+ }
1492+
1493+ //描画
1494+ glVertexPointer(3, GL_FLOAT, 0, VertexAry);
1495+ glColorPointer(4, GL_FLOAT, 0, ColorAry);
1496+ glTexCoordPointer(2, GL_FLOAT, 0, TexCoordAry);
1497+ glDrawArrays(GL_TRIANGLE_STRIP, 1, 6*cnt-2);
1498+ }
1499+
1500+ //配列無効化
1501+ glDisableClientState(GL_VERTEX_ARRAY);
1502+ glDisableClientState(GL_COLOR_ARRAY);
1503+ glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1504+
1505+ delete [] VertexAry;
1506+ delete [] ColorAry;
1507+ delete [] TexCoordAry;
1508+}
1509+
1510+//! @brief マップテクスチャを取得
1511+//! @param id テクスチャ番号
1512+//! @return テクスチャ認識番号(失敗:-1)
1513+int D3DGraphics::GetMapTextureID(int id)
1514+{
1515+ if( (id < 0)||((TOTAL_BLOCKTEXTURE -1) < id ) ){ return -1; }
1516+ return mapTextureID[id];
1517+}
1518+
1519+//! @brief マップデータを解放
1520+void D3DGraphics::CleanupMapdata()
1521+{
1522+ //テクスチャを開放
1523+ for(int i=0; i<TOTAL_BLOCKTEXTURE; i++){
1524+ CleanupTexture(mapTextureID[i]);
1525+ }
1526+
1527+ bs = 0;
1528+
1529+ blockdata = NULL;
1530+}
1531+
1532+//! @brief モデルファイルを描画
1533+//! @param id_model モデル認識番号
1534+//! @param id_texture テクスチャ認識番号
1535+void D3DGraphics::RenderModel(int id_model, int id_texture)
1536+{
1537+ //無効な引数が設定されていれば失敗
1538+ if( id_model == -1 ){ return; }
1539+ if( pmodel[id_model].useflag == false ){ return; }
1540+
1541+ //テクスチャが正常に読み込めていなければ設定
1542+ if( id_texture == -1 ){
1543+ //テクスチャ無効
1544+ glDisable(GL_TEXTURE_2D);
1545+ }
1546+ else if( ptextures[id_texture].useflag == false ){
1547+ //テクスチャ無効
1548+ glDisable(GL_TEXTURE_2D);
1549+ }
1550+ else{
1551+ //テクスチャ有効
1552+ glEnable(GL_TEXTURE_2D);
1553+
1554+ //テクスチャをセット
1555+ SetTexture(id_texture);
1556+ }
1557+
1558+ //配列有効化
1559+ glEnableClientState(GL_VERTEX_ARRAY);
1560+ glEnableClientState(GL_COLOR_ARRAY);
1561+ glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1562+
1563+ //描画
1564+ glVertexPointer(3, GL_FLOAT, 0, pmodel[id_model].VertexAry);
1565+ glColorPointer(4, GL_FLOAT, 0, pmodel[id_model].ColorAry);
1566+ glTexCoordPointer(2, GL_FLOAT, 0, pmodel[id_model].TexCoordAry);
1567+ glDrawArrays(GL_TRIANGLE_STRIP, 1, pmodel[id_model].polygons*6-2);
1568+
1569+ //配列無効化
1570+ glDisableClientState(GL_VERTEX_ARRAY);
1571+ glDisableClientState(GL_COLOR_ARRAY);
1572+ glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1573+
1574+ /*
1575+ Drawline(1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f);
1576+ Drawline(0.0f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f);
1577+ Drawline(0.0f, 0.0f, 1.0f, 0.0f, 0.0f, -1.0f);
1578+ */
1579+}
1580+
1581+//! @brief 板を描画
1582+//! @param id_texture テクスチャ認識番号
1583+//! @param alpha 透明度 (0.0〜1.0 0.0:完全透明)
1584+void D3DGraphics::RenderBoard(int id_texture, float alpha)
1585+{
1586+ //テクスチャが設定されていなければ、処理しない。
1587+ if( id_texture == -1 ){ return; }
1588+ if( ptextures[id_texture].useflag == false ){ return; }
1589+
1590+ float VertexAry[4*3];
1591+ float ColorAry[4*4];
1592+ float TexCoordAry[4*2];
1593+
1594+ //テクスチャ有効
1595+ glEnable(GL_TEXTURE_2D);
1596+
1597+ //テクスチャをセット
1598+ SetTexture(id_texture);
1599+
1600+ //頂点配列を用意
1601+ VertexAry[0] = 0.0f; VertexAry[1] = 0.5f; VertexAry[2] = 0.5f;
1602+ VertexAry[3] = 0.0f; VertexAry[4] = -0.5f; VertexAry[5] = 0.5f;
1603+ VertexAry[6] = 0.0f; VertexAry[7] = 0.5f; VertexAry[8] = -0.5f;
1604+ VertexAry[9] = 0.0f; VertexAry[10] = -0.5f; VertexAry[11] = -0.5f;
1605+
1606+ //色情報配列を用意
1607+ ColorAry[0] = 1.0f;
1608+ ColorAry[1] = 1.0f;
1609+ ColorAry[2] = 1.0f;
1610+ ColorAry[3] = alpha;
1611+ for(int i=1; i<4; i++){
1612+ memcpy(&(ColorAry[i*4]), ColorAry, sizeof(float)*4);
1613+ }
1614+
1615+ //UV座標配列を用意
1616+ TexCoordAry[0] = 1.0f; TexCoordAry[1] = 0.0f;
1617+ TexCoordAry[2] = 1.0f; TexCoordAry[3] = 1.0f;
1618+ TexCoordAry[4] = 0.0f; TexCoordAry[5] = 0.0f;
1619+ TexCoordAry[6] = 0.0f; TexCoordAry[7] = 1.0f;
1620+
1621+ //配列有効化
1622+ glEnableClientState(GL_VERTEX_ARRAY);
1623+ glEnableClientState(GL_COLOR_ARRAY);
1624+ glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1625+
1626+ //描画
1627+ glVertexPointer(3, GL_FLOAT, 0, VertexAry);
1628+ glColorPointer(4, GL_FLOAT, 0, ColorAry);
1629+ glTexCoordPointer(2, GL_FLOAT, 0, TexCoordAry);
1630+ glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1631+
1632+ //配列無効化
1633+ glDisableClientState(GL_VERTEX_ARRAY);
1634+ glDisableClientState(GL_COLOR_ARRAY);
1635+ glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1636+}
1637+
1638+//! @brief 画面の明るさを設定
1639+//! @param Width 幅
1640+//! @param Height 高さ
1641+//! @param Brightness 画面の明るさ (0 で不変、1 以上で明るさの度合い)
1642+void D3DGraphics::ScreenBrightness(int Width, int Height, int Brightness)
1643+{
1644+ //明るさ不変なら処理しない(軽量化)
1645+ if( Brightness == 0 ){ return; }
1646+
1647+ //透明度を設定し、描画
1648+ float alpha = 0.02f * Brightness;
1649+ Draw2DBox(0, 0, Width, Height, GetColorCode(1.0f,1.0f,1.0f,alpha));
1650+}
1651+
1652+//! @brief 【デバック用】中心線描画
1653+void D3DGraphics::Centerline()
1654+{
1655+ ResetWorldTransform();
1656+ Drawline(100.0f, 0.0f, 0.0f, -100.0f, 0.0f, 0.0f);
1657+ Drawline(0.0f, 100.0f, 0.0f, 0.0f, -100.0f, 0.0f);
1658+ Drawline(0.0f, 0.0f, 100.0f, 0.0f, 0.0f, -100.0f);
1659+}
1660+
1661+//! @brief 【デバック用】緑線描画
1662+void D3DGraphics::Drawline(float x1, float y1, float z1, float x2, float y2, float z2)
1663+{
1664+ float VertexAry[2*3];
1665+ unsigned char ColorAry[2*4];
1666+
1667+ //テクスチャ無効
1668+ glDisable(GL_TEXTURE_2D);
1669+
1670+ //頂点配列を用意
1671+ VertexAry[0] = (float)x1*-1; VertexAry[1] = (float)y1; VertexAry[2] = (float)z1;
1672+ VertexAry[3] = (float)x2*-1; VertexAry[4] = (float)y2; VertexAry[5] = (float)z2;
1673+
1674+ //色情報配列を用意
1675+ ColorAry[0] = 0;
1676+ ColorAry[1] = 255;
1677+ ColorAry[2] = 0;
1678+ ColorAry[3] = 255;
1679+ memcpy(&(ColorAry[4]), ColorAry, sizeof(unsigned char)*4);
1680+
1681+ //配列有効化
1682+ glEnableClientState(GL_VERTEX_ARRAY);
1683+ glEnableClientState(GL_COLOR_ARRAY);
1684+
1685+ //描画
1686+ glVertexPointer(3, GL_FLOAT, 0, VertexAry);
1687+ glColorPointer(4, GL_UNSIGNED_BYTE, 0, ColorAry);
1688+ glDrawArrays(GL_LINE_STRIP, 0, 2);
1689+
1690+ //配列無効化
1691+ glDisableClientState(GL_VERTEX_ARRAY);
1692+ glDisableClientState(GL_COLOR_ARRAY);
1693+}
1694+
1695+//! @brief 文字を描画(システムフォント使用)
1696+//! @param x x座標
1697+//! @param y y座標
1698+//! @param str 文字列 (改行コード:可)
1699+//! @param color 色
1700+//! @warning <b>描画は非常に低速です。</b>画面内で何度も呼び出すとパフォーマンスに影響します。
1701+//! @warning「改行コードを活用し一度に描画する」「日本語が必要ない文字はテクスチャフォントを活用する」などの対応を講じてください。
1702+//! @attention フォントの種類やサイズは固定です。 文字を二重に重ねて立体感を出さないと見にくくなります。
1703+//! @todo 文字を二重に重ねると、上下関係が正しく処理されない。
1704+//! @todo 1文字目が欠ける場合がある。
1705+void D3DGraphics::Draw2DMSFontText(int x, int y, char *str, int color)
1706+{
1707+ int len = strlen(str);
1708+ WCHAR *ustr;
1709+
1710+ Start2DRender();
1711+
1712+ //テクスチャ無効
1713+ glDisable(GL_TEXTURE_2D);
1714+
1715+ //Unicode文字列へ変換
1716+ ustr = new WCHAR [len+1];
1717+ MultiByteToWideChar(CP_ACP, 0, str, -1, ustr, len + 1);
1718+
1719+ //新たな文字列なら、リソースを作り直す
1720+ if( lstrcmpW(ustr, now_SystemFontUStr) != 0 ){
1721+ GLuint listIdx;
1722+ HDC hDC;
1723+
1724+ //古いデータを削除
1725+ glDeleteLists(SystemFontListIdx, SystemFontListIdxSize);
1726+ delete [] now_SystemFontUStr;
1727+
1728+ //デバイスコンテキスト設定
1729+ hDC = GetDC(hWnd);
1730+ wglMakeCurrent(hDC, hGLRC);
1731+ SelectObject(hDC, SystemFont);
1732+
1733+ //ディスプレイリストを作成
1734+ listIdx = glGenLists(len);
1735+ for(int i=0; i<lstrlenW(ustr); i++){
1736+ wglUseFontBitmapsW(hDC, ustr[i], 1, listIdx+i);
1737+ }
1738+
1739+ //デバイスコンテキスト廃棄
1740+ ReleaseDC(hWnd, hDC);
1741+
1742+ //設定を記録
1743+ now_SystemFontUStr = new WCHAR [len+1];
1744+ lstrcpyW(now_SystemFontUStr, ustr);
1745+ SystemFontListIdx = listIdx;
1746+ SystemFontListIdxSize = len;
1747+ }
1748+
1749+ //座標と色を設定
1750+ glBitmap(0, 0, 0, 0, 10, 0, NULL);
1751+ glRasterPos2i(x, y);
1752+ glColor4ub((color>>24)&0xFF, (color>>16)&0xFF, (color>>8)&0xFF, color&0xFF);
1753+
1754+ for(int i=0; i<lstrlenW(ustr); i++){
1755+ if( ustr[i] == '\n' ){
1756+ //改行する
1757+ y += 19;
1758+ glRasterPos2i(x, y);
1759+ }
1760+ else{
1761+ //ディスプレイリスト描画
1762+ glCallList(SystemFontListIdx + i);
1763+ }
1764+ }
1765+
1766+ //Unicode文字列の廃棄
1767+ delete [] ustr;
1768+
1769+ End2DRender();
1770+}
1771+
1772+//! @brief 文字を中央揃えで描画(システムフォント使用)
1773+//! @param x x座標
1774+//! @param y y座標
1775+//! @param w 横の大きさ
1776+//! @param h 縦の大きさ
1777+//! @param str 文字列 (改行コード:可)
1778+//! @param color 色
1779+//! @warning <b>正しく中央揃えになりません。</b>
1780+void D3DGraphics::Draw2DMSFontTextCenter(int x, int y, int w, int h, char *str, int color)
1781+{
1782+ Draw2DMSFontText(x, y, str, color);
1783+}
1784+
1785+//! @brief 2D描画用設定
1786+void D3DGraphics::Start2DRender()
1787+{
1788+ glMatrixMode(GL_PROJECTION);
1789+ glPushMatrix();
1790+ glLoadIdentity();
1791+ glOrtho(0, width, height, 0, -1, 1);
1792+ glMatrixMode(GL_MODELVIEW);
1793+ glPushMatrix();
1794+ glLoadIdentity();
1795+ gluLookAt(0, 0, 1, 0, 0, 0, 0, 1, 0);
1796+
1797+ glDisable(GL_DEPTH_TEST);
1798+ glDisable(GL_CULL_FACE);
1799+}
1800+
1801+//! @brief 文字を描画(テクスチャフォント使用)
1802+//! @param x x座標
1803+//! @param y y座標
1804+//! @param str 文字列 (改行コード:<b>不可</b>)
1805+//! @param color 色
1806+//! @param fontwidth 一文字の幅
1807+//! @param fontheight 一文字の高さ
1808+//! @attention 文字を二重に重ねて立体感を出さないと見にくくなります。
1809+void D3DGraphics::Draw2DTextureFontText(int x, int y, char *str, int color, int fontwidth, int fontheight)
1810+{
1811+ //テクスチャフォントの取得に失敗していれば、処理しない
1812+ if( TextureFont == -1 ){ return; }
1813+
1814+ int strlens = (int)strlen(str);
1815+
1816+ float *VertexAry = new float [strlens*6*2];
1817+ unsigned char *ColorAry = new unsigned char [strlens*6*4];
1818+ float *TexCoordAry = new float [strlens*6*2];
1819+
1820+ //2D描画用設定を適用
1821+ Start2DRender();
1822+
1823+ int w;
1824+ float font_u, font_v;
1825+ float t_u, t_v;
1826+
1827+ //1文字のUV座標を計算
1828+ font_u = 1.0f / 16;
1829+ font_v = 1.0f / 16;
1830+
1831+ //テクスチャ有効
1832+ glEnable(GL_TEXTURE_2D);
1833+
1834+ //テクスチャをセット
1835+ SetTexture(TextureFont);
1836+
1837+ //配列有効化
1838+ glEnableClientState(GL_VERTEX_ARRAY);
1839+ glEnableClientState(GL_COLOR_ARRAY);
1840+ glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1841+
1842+ //色情報配列を用意
1843+ ColorAry[0] = (color>>24)&0xFF;
1844+ ColorAry[1] = (color>>16)&0xFF;
1845+ ColorAry[2] = (color>>8)&0xFF;
1846+ ColorAry[3] = color&0xFF;
1847+ for(int i=1; i<strlens*6; i++){
1848+ memcpy(&(ColorAry[i*4]), ColorAry, sizeof(unsigned char)*4);
1849+ }
1850+
1851+ // 与えられた文字数分ループ
1852+ for(int i=0; i<strlens; i++){
1853+ //UV座標を計算
1854+ w = str[i];
1855+ if( w < 0 ){ w += 256; }
1856+ t_u = (w % 16) * font_u;
1857+ t_v = (w / 16) * font_v;
1858+
1859+ VertexAry[0 + i*12] = (float)x + i*fontwidth; VertexAry[1 + i*12] = (float)y;
1860+ VertexAry[2 + i*12] = (float)x + i*fontwidth; VertexAry[3 + i*12] = (float)y;
1861+ VertexAry[4 + i*12] = (float)x + fontwidth + i*fontwidth; VertexAry[5 + i*12] = (float)y;
1862+ VertexAry[6 + i*12] = (float)x + i*fontwidth; VertexAry[7 + i*12] = (float)y + fontheight;
1863+ VertexAry[8 + i*12] = (float)x + fontwidth + i*fontwidth; VertexAry[9 + i*12] = (float)y + fontheight;
1864+ VertexAry[10 + i*12] = (float)x + fontwidth + i*fontwidth; VertexAry[11 + i*12] = (float)y + fontheight;
1865+ TexCoordAry[0 + i*12] = t_u; TexCoordAry[1 + i*12] = t_v;
1866+ TexCoordAry[2 + i*12] = t_u; TexCoordAry[3 + i*12] = t_v;
1867+ TexCoordAry[4 + i*12] = t_u + font_u; TexCoordAry[5 + i*12] = t_v;
1868+ TexCoordAry[6 + i*12] = t_u; TexCoordAry[7 + i*12] = t_v + font_v;
1869+ TexCoordAry[8 + i*12] = t_u + font_u; TexCoordAry[9 + i*12] = t_v + font_v;
1870+ TexCoordAry[10 + i*12] = t_u + font_u; TexCoordAry[11 + i*12] = t_v + font_v;
1871+ }
1872+
1873+ //描画
1874+ glVertexPointer(2, GL_FLOAT, 0, VertexAry);
1875+ glColorPointer(4, GL_UNSIGNED_BYTE, 0, ColorAry);
1876+ glTexCoordPointer(2, GL_FLOAT, 0, TexCoordAry);
1877+ glDrawArrays(GL_TRIANGLE_STRIP, 1, strlens*6-2);
1878+
1879+ //配列無効化
1880+ glDisableClientState(GL_VERTEX_ARRAY);
1881+ glDisableClientState(GL_COLOR_ARRAY);
1882+ glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1883+
1884+ //2D描画用設定を解除
1885+ End2DRender();
1886+
1887+ delete [] VertexAry;
1888+ delete [] ColorAry;
1889+ delete [] TexCoordAry;
1890+}
1891+
1892+//! @brief 線を描画
1893+//! @param x1 始点の x座標
1894+//! @param y1 始点の y座標
1895+//! @param x2 終点の x座標
1896+//! @param y2 終点の y座標
1897+//! @param color 色
1898+void D3DGraphics::Draw2DLine(int x1, int y1, int x2, int y2, int color)
1899+{
1900+ float VertexAry[2*2];
1901+ unsigned char ColorAry[2*4];
1902+
1903+ //2D描画用設定を適用
1904+ Start2DRender();
1905+
1906+ //テクスチャ無効
1907+ glDisable(GL_TEXTURE_2D);
1908+
1909+ //頂点配列を用意
1910+ VertexAry[0] = (float)x1; VertexAry[1] = (float)y1;
1911+ VertexAry[2] = (float)x2; VertexAry[3] = (float)y2;
1912+
1913+ //色情報配列を用意
1914+ ColorAry[0] = (color>>24)&0xFF;
1915+ ColorAry[1] = (color>>16)&0xFF;
1916+ ColorAry[2] = (color>>8)&0xFF;
1917+ ColorAry[3] = color&0xFF;
1918+ memcpy(&(ColorAry[4]), ColorAry, sizeof(unsigned char)*4);
1919+
1920+ //配列有効化
1921+ glEnableClientState(GL_VERTEX_ARRAY);
1922+ glEnableClientState(GL_COLOR_ARRAY);
1923+
1924+ //描画
1925+ glVertexPointer(2, GL_FLOAT, 0, VertexAry);
1926+ glColorPointer(4, GL_UNSIGNED_BYTE, 0, ColorAry);
1927+ glDrawArrays(GL_LINE_STRIP, 0, 2);
1928+
1929+ //配列無効化
1930+ glDisableClientState(GL_VERTEX_ARRAY);
1931+ glDisableClientState(GL_COLOR_ARRAY);
1932+
1933+ //2D描画用設定を解除
1934+ End2DRender();
1935+}
1936+
1937+//! @brief 円(16角形)を描画
1938+//! @param x 中心の x座標
1939+//! @param y 中心の y座標
1940+//! @param r 半径
1941+//! @param color 色
1942+void D3DGraphics::Draw2DCycle(int x, int y, int r, int color)
1943+{
1944+ float VertexAry[(16+1)*2];
1945+ unsigned char ColorAry[(16+1)*4];
1946+
1947+ //2D描画用設定を適用
1948+ Start2DRender();
1949+
1950+ //テクスチャ無効
1951+ glDisable(GL_TEXTURE_2D);
1952+
1953+ //頂点座標を設定
1954+ for(int i=0; i<16+1; i++){
1955+ float x2, y2;
1956+ x2 = (float)x + cos((float)M_PI*2/16 * i) * r;
1957+ y2 = (float)y + sin((float)M_PI*2/16 * i) * r;
1958+ VertexAry[i*2] = x2; VertexAry[i*2+1] = y2;
1959+ }
1960+
1961+ //色情報配列を用意
1962+ ColorAry[0] = (color>>24)&0xFF;
1963+ ColorAry[1] = (color>>16)&0xFF;
1964+ ColorAry[2] = (color>>8)&0xFF;
1965+ ColorAry[3] = color&0xFF;
1966+ for(int i=1; i<16+1; i++){
1967+ memcpy(&(ColorAry[i*4]), ColorAry, sizeof(unsigned char)*4);
1968+ }
1969+
1970+ //配列有効化
1971+ glEnableClientState(GL_VERTEX_ARRAY);
1972+ glEnableClientState(GL_COLOR_ARRAY);
1973+
1974+ //描画
1975+ glVertexPointer(2, GL_FLOAT, 0, VertexAry);
1976+ glColorPointer(4, GL_UNSIGNED_BYTE, 0, ColorAry);
1977+ glDrawArrays(GL_LINE_STRIP, 0, 16+1);
1978+
1979+ //配列無効化
1980+ glDisableClientState(GL_VERTEX_ARRAY);
1981+ glDisableClientState(GL_COLOR_ARRAY);
1982+
1983+ //2D描画用設定を解除
1984+ End2DRender();
1985+}
1986+
1987+//! @brief 四角形を描画
1988+//! @param x1 左上の x座標
1989+//! @param y1 左上の y座標
1990+//! @param x2 右下の x座標
1991+//! @param y2 右下の y座標
1992+//! @param color 色
1993+void D3DGraphics::Draw2DBox(int x1, int y1, int x2, int y2, int color)
1994+{
1995+ float VertexAry[4*2];
1996+ unsigned char ColorAry[4*4];
1997+
1998+ //2D描画用設定を適用
1999+ Start2DRender();
2000+
2001+ //テクスチャ無効
2002+ glDisable(GL_TEXTURE_2D);
2003+
2004+ //頂点配列を用意
2005+ VertexAry[0] = (float)x1; VertexAry[1] = (float)y1;
2006+ VertexAry[2] = (float)x2; VertexAry[3] = (float)y1;
2007+ VertexAry[4] = (float)x1; VertexAry[5] = (float)y2;
2008+ VertexAry[6] = (float)x2; VertexAry[7] = (float)y2;
2009+
2010+ //色情報配列を用意
2011+ ColorAry[0] = (color>>24)&0xFF;
2012+ ColorAry[1] = (color>>16)&0xFF;
2013+ ColorAry[2] = (color>>8)&0xFF;
2014+ ColorAry[3] = color&0xFF;
2015+ for(int i=1; i<4; i++){
2016+ memcpy(&(ColorAry[i*4]), ColorAry, sizeof(unsigned char)*4);
2017+ }
2018+
2019+ //配列有効化
2020+ glEnableClientState(GL_VERTEX_ARRAY);
2021+ glEnableClientState(GL_COLOR_ARRAY);
2022+
2023+ //描画
2024+ glVertexPointer(2, GL_FLOAT, 0, VertexAry);
2025+ glColorPointer(4, GL_UNSIGNED_BYTE, 0, ColorAry);
2026+ glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
2027+
2028+ //配列無効化
2029+ glDisableClientState(GL_VERTEX_ARRAY);
2030+ glDisableClientState(GL_COLOR_ARRAY);
2031+
2032+ //2D描画用設定を解除
2033+ End2DRender();
2034+}
2035+
2036+//! @brief 画像を描画
2037+//! @param x x座標
2038+//! @param y y座標
2039+//! @param id テクスチャ認識番号
2040+//! @param width 幅
2041+//! @param height 高さ
2042+//! @param alpha 透明度(0.0〜1.0)
2043+void D3DGraphics::Draw2DTexture(int x, int y, int id, int width, int height, float alpha)
2044+{
2045+ //無効なテクスチャ番号を指定されていれば処理しない
2046+ if( id == -1 ){ return; }
2047+
2048+ float VertexAry[4*2];
2049+ float ColorAry[4*4];
2050+ float TexCoordAry[4*2];
2051+
2052+ //2D描画用設定を適用
2053+ Start2DRender();
2054+
2055+ //テクスチャ有効
2056+ glEnable(GL_TEXTURE_2D);
2057+
2058+ //テクスチャをセット
2059+ SetTexture(id);
2060+
2061+ //頂点配列を用意
2062+ VertexAry[0] = (float)x; VertexAry[1] = (float)y;
2063+ VertexAry[2] = (float)x+width; VertexAry[3] = (float)y;
2064+ VertexAry[4] = (float)x; VertexAry[5] = (float)y+height;
2065+ VertexAry[6] = (float)x+width; VertexAry[7] = (float)y+height;
2066+
2067+ //色情報配列を用意
2068+ ColorAry[0] = 1.0f;
2069+ ColorAry[1] = 1.0f;
2070+ ColorAry[2] = 1.0f;
2071+ ColorAry[3] = alpha;
2072+ for(int i=1; i<4; i++){
2073+ memcpy(&(ColorAry[i*4]), ColorAry, sizeof(float)*4);
2074+ }
2075+
2076+ //UV座標配列を用意
2077+ TexCoordAry[0] = 0.0f; TexCoordAry[1] = 0.0f;
2078+ TexCoordAry[2] = 1.0f; TexCoordAry[3] = 0.0f;
2079+ TexCoordAry[4] = 0.0f; TexCoordAry[5] = 1.0f;
2080+ TexCoordAry[6] = 1.0f; TexCoordAry[7] = 1.0f;
2081+
2082+ //配列有効化
2083+ glEnableClientState(GL_VERTEX_ARRAY);
2084+ glEnableClientState(GL_COLOR_ARRAY);
2085+ glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2086+
2087+ //描画
2088+ glVertexPointer(2, GL_FLOAT, 0, VertexAry);
2089+ glColorPointer(4, GL_FLOAT, 0, ColorAry);
2090+ glTexCoordPointer(2, GL_FLOAT, 0, TexCoordAry);
2091+ glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
2092+
2093+ //配列無効化
2094+ glDisableClientState(GL_VERTEX_ARRAY);
2095+ glDisableClientState(GL_COLOR_ARRAY);
2096+ glDisableClientState(GL_TEXTURE_COORD_ARRAY);
2097+
2098+ //2D描画用設定を解除
2099+ End2DRender();
2100+}
2101+
2102+//! @brief 2D描画用設定を解除
2103+void D3DGraphics::End2DRender()
2104+{
2105+ glMatrixMode(GL_PROJECTION);
2106+ glLoadIdentity();
2107+ glPopMatrix();
2108+ glMatrixMode(GL_MODELVIEW);
2109+ glLoadIdentity();
2110+ glPopMatrix();
2111+
2112+ glEnable(GL_DEPTH_TEST);
2113+ glEnable(GL_CULL_FACE);
2114+}
2115+
2116+//! @brief 画面のスクリーンショットを保存
2117+//! @param filename ファイル名
2118+//! @return 成功:true 失敗:false
2119+bool D3DGraphics::SaveScreenShot(char* filename)
2120+{
2121+ return false;
2122+}
2123+
2124+//! @brief カラーコードを取得
2125+//! @param red 赤(0.0f〜1.0f)
2126+//! @param green 緑(0.0f〜1.0f)
2127+//! @param blue 青(0.0f〜1.0f)
2128+//! @param alpha 透明度(0.0f〜1.0f)
2129+//! @return カラーコード
2130+int D3DGraphics::GetColorCode(float red, float green, float blue, float alpha)
2131+{
2132+ unsigned char red2, green2, blue2, alpha2;
2133+ red2 = (unsigned char)(red*255);
2134+ green2 = (unsigned char)(green*255);
2135+ blue2 = (unsigned char)(blue*255);
2136+ alpha2 = (unsigned char)(alpha*255);
2137+
2138+ return (red2 << 24) | (green2 << 16) | (blue2 << 8) | alpha2;
2139+}
2140+
2141+#endif //GRAPHICS_OPENGL
\ No newline at end of file