3 #include "mof/private/GraphicsDeviceImpl.hpp"
4 #include "mof/ConsoleIO.hpp"
7 #include "mof/private/VertexFVF.hpp"
8 #include "mof/Material.hpp"
9 #include "mof/private/TextureImpl.hpp"
10 #include "mof/Window.hpp"
18 LPDIRECT3DDEVICE9 m_pDevice = NULL;
19 LPDIRECT3D9 m_pD3D = NULL;
20 D3DPRESENT_PARAMETERS* m_pParam = NULL;
21 IDirect3DStateBlock9* m_pNormalBlendingBlock = NULL;
22 IDirect3DStateBlock9* m_pAddBlendingBlock = NULL;
23 IDirect3DStateBlock9* m_pAlphaBlendingBlock = NULL;
24 int m_currentStateBlock;
25 mof::Matrix3D m_worldTransform;
26 mof::Matrix3D m_viewTransform;
27 mof::Matrix3D m_projectionTransform;
29 bool m_flagDeviceLost;
33 inline bool isOK(HRESULT hr){
40 template void GraphicsDevice::drawVertexArray( const VertexXYZRHWCUV& , const VertexXYZRHWCUV& , PRIMITIVE_TYPE );
41 template void GraphicsDevice::drawVertexArray( const VertexXYZRHWC& , const VertexXYZRHWC& , PRIMITIVE_TYPE);
42 template void GraphicsDevice::drawVertexArray( const VertexXYZCUV& , const VertexXYZCUV& , PRIMITIVE_TYPE);
43 template void GraphicsDevice::drawVertexArray( const VertexXYZNUV& , const VertexXYZNUV& , PRIMITIVE_TYPE);
44 template void GraphicsDevice::drawVertexArray( const VertexXYZC& , const VertexXYZC& , PRIMITIVE_TYPE);
48 //TODO
\8aÖ
\90\94\95ª
\8a\84
49 void GraphicsDevice::initialize( const mof::Window& window , int width , int height , bool fullscreen )
53 m_hWnd = window.getHandler();
57 if( NULL == ( m_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
59 throw std::runtime_error( "Failed --- Direct3DCreate9" );
62 m_pParam = new D3DPRESENT_PARAMETERS;
63 ZeroMemory( m_pParam, sizeof(D3DPRESENT_PARAMETERS) );
64 m_pParam->Windowed = (fullscreen)? FALSE : TRUE; //TRUE=Window,FALSE=FULLSCREEN
65 m_pParam->BackBufferCount = 1;
66 m_pParam->PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
70 //
\83t
\83\8b\83X
\83N
\83\8a\81[
\83\93\8e\9e\82Í
\83o
\83b
\83N
\83o
\83b
\83t
\83@
\82Ì
\95\9d\81A
\8d\82\82³
\82ð
\8dÄ
\8ew
\92è
71 m_pParam->BackBufferWidth = width; //
\95\9d
72 m_pParam->BackBufferHeight = height; //
\8d\82\82³
73 m_pParam->BackBufferFormat = D3DFMT_X8R8G8B8; //16bit
74 //m_pParam->BackBufferFormat = D3DFMT_R5G6B5; //16bit
75 m_pParam->FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
79 m_pParam->BackBufferFormat = D3DFMT_UNKNOWN; //
\8c»
\8dÝ
\82Ì
\89æ
\96Ê
\83\82\81[
\83h
\82ð
\97\98\97p
81 m_pParam->SwapEffect = D3DSWAPEFFECT_DISCARD;
82 // Z
\83o
\83b
\83t
\83@
\82Ì
\8e©
\93®
\8dì
\90¬
83 m_pParam->EnableAutoDepthStencil = 1;
84 m_pParam->AutoDepthStencilFormat = D3DFMT_D16;
97 0 , D3DDEVTYPE_HAL , m_hWnd ,
98 D3DCREATE_HARDWARE_VERTEXPROCESSING , m_pParam , &m_pDevice
111 0 , D3DDEVTYPE_HAL , m_hWnd ,
112 D3DCREATE_SOFTWARE_VERTEXPROCESSING , m_pParam , &(m_pDevice)
124 0 , D3DDEVTYPE_REF , m_hWnd ,
125 D3DCREATE_HARDWARE_VERTEXPROCESSING , m_pParam , &m_pDevice
132 throw std::runtime_error(std::string("Failed --- CreateDevice"));
137 setViewport( mof::Rectangle<int>(0 , 0 , width , height) );
140 m_pDevice->SetSamplerState(0 , D3DSAMP_MAGFILTER , D3DTEXF_LINEAR ); //D3DTEXF_POINT
141 m_pDevice->SetSamplerState(0 , D3DSAMP_MINFILTER , D3DTEXF_LINEAR ); //D3DTEXF_POINT
144 m_pDevice->BeginStateBlock( );
145 m_pDevice->SetRenderState( D3DRS_ALPHABLENDENABLE , TRUE );
146 m_pDevice->SetRenderState( D3DRS_SRCBLEND,D3DBLEND_SRCALPHA );
147 m_pDevice->SetRenderState( D3DRS_DESTBLEND,D3DBLEND_ONE );
148 m_pDevice->SetTextureStageState( 0 , D3DTSS_ALPHAOP,D3DTOP_MODULATE ); //ARG1
\82ÆARG2
\82Ì
\83¿
\92l
\82ð
\8fæ
\8eZ
\82µ
\82Ä
\83¿
\92l
\82ð
\8eæ
\93¾
\82µ
\82Ü
\82·
\81B
149 m_pDevice->SetTextureStageState( 0 , D3DTSS_ALPHAARG1,D3DTA_DIFFUSE ); //
\83e
\83N
\83X
\83`
\83\83\82Ì
\83¿
\92l
150 m_pDevice->SetTextureStageState( 0 , D3DTSS_ALPHAARG2,D3DTA_TEXTURE ); //
\92¸
\93_
\82Ì
\83¿
151 m_pDevice->SetTextureStageState( 0 , D3DTSS_COLOROP,D3DTOP_MODULATE ); //ARG1
\82ÆARG2
\82Ì
\83J
\83\89\81[
\82Ì
\92l
\82ð
\8fæ
\8eZ
\82µ
\82Ü
\82·
\81B
152 m_pDevice->SetTextureStageState( 0 , D3DTSS_COLORARG1,D3DTA_TEXTURE ); //
\83e
\83N
\83X
\83`
\83\83\82Ì
\83J
\83\89\81[
153 m_pDevice->SetTextureStageState( 0 , D3DTSS_COLORARG2,D3DTA_DIFFUSE ); //
\92¸
\93_
\82Ì
\83J
\83\89\81[*/
154 m_pDevice->EndStateBlock( &m_pAddBlendingBlock );
156 m_pDevice->BeginStateBlock( );
157 m_pDevice->SetRenderState( D3DRS_ALPHABLENDENABLE , TRUE );
158 m_pDevice->SetRenderState( D3DRS_SRCBLEND , D3DBLEND_SRCALPHA );
159 m_pDevice->SetRenderState( D3DRS_DESTBLEND , D3DBLEND_INVSRCALPHA );
160 m_pDevice->SetTextureStageState( 0 ,D3DTSS_ALPHAOP , D3DTOP_SELECTARG2 ); //ARG1
\82ÆARG2
\82Ì
\83¿
\92l
\82ð
\8fæ
\8eZ
\82µ
\82Ä
\83¿
\92l
\82ð
\8eæ
\93¾
\82µ
\82Ü
\82·
\81B
161 m_pDevice->SetTextureStageState( 0 ,D3DTSS_ALPHAARG1 , D3DTA_DIFFUSE ); //
\83e
\83N
\83X
\83`
\83\83\82Ì
\83¿
\92l
162 m_pDevice->SetTextureStageState( 0 ,D3DTSS_ALPHAARG2 , D3DTA_TEXTURE ); //
\92¸
\93_
\82Ì
\83¿
\92l
163 m_pDevice->SetTextureStageState( 0 ,D3DTSS_COLOROP , D3DTOP_MODULATE ); //ARG1
\82ÆARG2
\82Ì
\83J
\83\89\81[
\82Ì
\92l
\82ð
\8fæ
\8eZ
\82µ
\82Ü
\82·
\81B
164 m_pDevice->SetTextureStageState( 0 ,D3DTSS_COLORARG1 , D3DTA_DIFFUSE ); //
\83e
\83N
\83X
\83`
\83\83\82Ì
\83J
\83\89\81[
165 m_pDevice->SetTextureStageState( 0 ,D3DTSS_COLORARG2 , D3DTA_TEXTURE ); //
\92¸
\93_
\82Ì
\83J
\83\89\81[*/
166 m_pDevice->EndStateBlock( &m_pNormalBlendingBlock );
168 m_pDevice->BeginStateBlock( );
169 m_pDevice->SetRenderState( D3DRS_ALPHABLENDENABLE , TRUE );
170 m_pDevice->SetRenderState( D3DRS_SRCBLEND , D3DBLEND_SRCALPHA );
171 m_pDevice->SetRenderState( D3DRS_DESTBLEND , D3DBLEND_INVSRCALPHA );
172 m_pDevice->SetTextureStageState( 0,D3DTSS_ALPHAOP , D3DTOP_MODULATE ); //ARG1
\82ÆARG2
\82Ì
\83¿
\92l
\82ð
\8fæ
\8eZ
\82µ
\82Ä
\83¿
\92l
\82ð
\8eæ
\93¾
\82µ
\82Ü
\82·
\81B
173 m_pDevice->SetTextureStageState( 0,D3DTSS_ALPHAARG1 , D3DTA_DIFFUSE ); //
\83e
\83N
\83X
\83`
\83\83\82Ì
\83¿
\92l
174 m_pDevice->SetTextureStageState( 0,D3DTSS_ALPHAARG2 , D3DTA_TEXTURE ); //
\92¸
\93_
\82Ì
\83¿
\92l
175 m_pDevice->SetTextureStageState( 0,D3DTSS_COLOROP , D3DTOP_MODULATE ); //ARG1
\82ÆARG2
\82Ì
\83J
\83\89\81[
\82Ì
\92l
\82ð
\8fæ
\8eZ
\82µ
\82Ü
\82·
\81B
176 m_pDevice->SetTextureStageState( 0,D3DTSS_COLORARG1 , D3DTA_DIFFUSE ); //
\83e
\83N
\83X
\83`
\83\83\82Ì
\83J
\83\89\81[
177 m_pDevice->SetTextureStageState( 0,D3DTSS_COLORARG2 , D3DTA_TEXTURE ); //
\92¸
\93_
\82Ì
\83J
\83\89\81[*/
178 m_pDevice->EndStateBlock( &m_pAlphaBlendingBlock );
180 setProjectionTransform(0.1f , 100);
181 } // function initialize
185 void GraphicsDevice::finalize( )
187 m_pAddBlendingBlock->Release();
188 m_pNormalBlendingBlock->Release();
189 m_pAlphaBlendingBlock->Release();
190 if(m_pDevice != NULL)m_pDevice->Release();
191 if(m_pD3D != NULL)m_pD3D->Release();
198 void GraphicsDevice::beginScene( )
202 if(true == m_flagDeviceLost)
204 Sleep(100); // 0.1
\95b
\91Ò
\82Â
205 if(/*m_flagActive == true &&*/ m_pDevice->TestCooperativeLevel() == D3DERR_DEVICENOTRESET )
207 //---
\83f
\83o
\83C
\83X
\95\9c\8bA
\82ð
\8e\8e\82Ý
\82é
209 if( FAILED( m_pDevice->Reset( m_pParam) ) )return;
211 //
\83f
\83o
\83C
\83X
\82Ì
\95\9c\8bA
\82É
\90¬
\8c÷
212 m_flagDeviceLost = false;
213 //---
\83X
\83e
\81[
\83g
\82ð
\95\9c\8bA
214 DEBUG_PRINT( "DeviceRecovered!" );
221 if( !isOK( m_pDevice->BeginScene( ) ) )
223 throw std::runtime_error("Failed --- BeginScene");
227 } // function beginScene
232 void GraphicsDevice::endScene( )
235 if( !isOK( m_pDevice->EndScene() ) )
237 throw std::runtime_error("Failed --- EndScene");
240 //
\83o
\83b
\83N
\83o
\83b
\83t
\83@
\82©
\82ç
\83v
\83\89\83C
\83}
\83\8a\83o
\83b
\83t
\83@
\82Ö
\93]
\91\97
241 HRESULT hr = m_pDevice->Present( NULL, NULL, NULL, NULL );
243 if( hr == D3DERR_DEVICELOST && false == m_flagDeviceLost )
245 DEBUG_PRINT( "DeviceLost!" );
246 m_flagDeviceLost = true;
247 //
\83\8a\83\
\81[
\83X
\82Ì
\89ð
\95ú
\8f\88\97\9d
252 if( !isOK( hr ) )throw std::runtime_error("Present");
255 } // function endScene
260 void GraphicsDevice::setWorldTransform( const mof::Matrix3D& matrix )
262 m_worldTransform = matrix;
264 for(int i = 0 ; i < 4 ; ++i ){
265 for(int j = 0 ; j < 4 ; ++j){
266 mat(i , j) = matrix.at(i , j);
269 m_pDevice->SetTransform( D3DTS_WORLD, &mat );
274 void GraphicsDevice::setProjectionTransform( real min , real max )
277 D3DXMatrixPerspectiveFovLH
279 &matProj, D3DX_PI / 4.0f,
280 static_cast<float>(getViewportWidth()) / static_cast<float>(getViewportHeight()) , min, max
283 mof::Matrix3D::Array arry;
284 for(int i = 0 ; i < 4 ; ++i ){
285 for(int j = 0 ; j < 4 ; ++j){
286 arry.elements[i][j] = matProj( i , j );
289 m_projectionTransform = mof::Matrix3D( arry );
290 m_pDevice->SetTransform( D3DTS_PROJECTION , &matProj );
296 void GraphicsDevice::setViewTransform( const mof::Matrix3D& matrix )
298 m_viewTransform = matrix;
300 for(int i = 0 ; i < 4 ; ++i ){
301 for(int j = 0 ; j < 4 ; ++j){
302 mat(i , j) = matrix.at(i , j);
305 m_pDevice->SetTransform( D3DTS_VIEW , &mat );
311 const mof::Matrix3D& GraphicsDevice::getWorldTransform( )
313 return m_worldTransform;
318 const mof::Matrix3D& GraphicsDevice::getProjectionTransform()
320 return m_projectionTransform;
325 const mof::Matrix3D& GraphicsDevice::getViewTransform()
327 return m_viewTransform;
332 void GraphicsDevice::setZBuffer( bool available )
334 if( available )m_pDevice->SetRenderState( D3DRS_ZENABLE, D3DZB_TRUE );
335 else m_pDevice->SetRenderState( D3DRS_ZENABLE , D3DZB_FALSE );
340 void GraphicsDevice::lightEnable( bool available )
342 m_pDevice->SetRenderState( D3DRS_LIGHTING , available );
347 void GraphicsDevice::clearZBuffer()
349 m_pDevice->Clear( 0 , NULL , D3DCLEAR_ZBUFFER , 0 , 1.0f, 0 );
354 void GraphicsDevice::clearRenderTarget( mof::Color color )
356 m_pDevice->Clear( 0 , 0 , D3DCLEAR_TARGET , color , 0 , 0 );
361 void GraphicsDevice::setAlphaBlendingMode( const int flag )
363 if(flag == m_currentStateBlock )return;
365 if( flag == mof::GraphicsDevice::BLENDING_MODE_ADD ){
366 m_pAddBlendingBlock->Apply();
368 else if(flag == mof::GraphicsDevice::BLENDING_MODE_BASIC)
370 m_pNormalBlendingBlock->Apply();
372 else if( flag == mof::GraphicsDevice::BLENDING_MODE_ALPHA )
374 m_pAlphaBlendingBlock->Apply();
377 m_currentStateBlock = flag;
382 Vector2D GraphicsDevice::to2DPosition( const mof::Vector3D& position )
384 mof::Matrix3D matrix = m_worldTransform;
385 matrix = m_viewTransform * m_projectionTransform;
386 int hWidth = getViewportWidth() / 2;
387 int hHeight = getViewportHeight() / 2;
388 mof::Vector3D tmpPosition = position * matrix;
392 static_cast<int>(tmpPosition.x * hWidth + hWidth ) ,
393 static_cast<int>(-tmpPosition.y * hHeight + hHeight)
398 void GraphicsDevice::setViewport( const mof::Rectangle<int>& area )
403 vp.Width = area.getWidth( );
404 vp.Height = area.getHeight( );
407 if( FAILED( m_pDevice->SetViewport( &vp ) ) )
409 throw std::runtime_error( "Failed --- SetViewport" );
413 int GraphicsDevice::getViewportWidth( )
416 if( FAILED(m_pDevice->GetViewport( &vp ) ) )
418 throw std::runtime_error("Failed --- getViewportWidth");
424 int GraphicsDevice::getViewportHeight() {
426 if( FAILED( m_pDevice->GetViewport( &vp ) ) )
428 throw std::runtime_error("Failed --- getViewportHeight");
437 void GraphicsDevice::drawVertexArray( const T& front , const T& last , mof::PRIMITIVE_TYPE type )
440 if(&last < &front)return;
441 int length = &last - &front + 1; //
\92¸
\93_
\90\94
443 m_pDevice->SetFVF( mof::getFVF<T>() );
445 if(type == mof::PRIMITIVE_TYPE_TRIANGLESTRIP)
447 hr = m_pDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP , length > 3 ? length - 2 : 1 , (const void*)&front , sizeof(T) );
449 else if( type == PRIMITIVE_TYPE_TRIANGLELIST )
451 hr = m_pDevice->DrawPrimitiveUP( D3DPT_TRIANGLELIST , length / 3 , (const void*)&front , sizeof(T) );
453 else if( type == mof::PRIMITIVE_TYPE_LINELIST )
455 hr = m_pDevice->DrawPrimitiveUP(D3DPT_LINELIST , length / 2 , (const void*)&front , sizeof(T) );
460 throw std::runtime_error("Failed -- DrawPrimitiveUP");
463 } // function drawVertexArray
467 void GraphicsDevice::setMaterial(const mof::Material& material){
470 memcpy(static_cast<void*>(&mat) , static_cast<const void*>(&material) , sizeof(D3DMATERIAL9));
472 if( FAILED( m_pDevice->SetMaterial( &mat ) ) )
474 throw std::runtime_error("Faild --- SetMaterial");
479 void GraphicsDevice::setTexture( const mof::Texture* pTexture )
481 if( pTexture == NULL )m_pDevice->SetTexture( 0 , NULL );
482 else m_pDevice->SetTexture( 0 , pTexture->m_pImpl->pTexture );
486 mof::Rectangle<int> GraphicsDevice::getClientRegion()
491 GetWindowRect(m_hWnd , &rect);
495 ScreenToClient(m_hWnd , &point);
496 int beginX = rect.left - point.x;
497 int beginY = rect.top - point.y;
498 return mof::Rectangle<int>(beginX , beginY , beginX + m_width , beginY + m_height);
501 LPDIRECT3DDEVICE9 GraphicsDevice::getRawDevice( )
506 } // namespace GraphicsDevice