//***************************************************************************** // 2Dポリゴン //***************************************************************************** //------------------------------------------------------ // 固定機能版 //------------------------------------------------------ void iexPolygon::Render2D( LPTLVERTEX v, int Num, LPIEX2DOBJ lpObj, u32 dwFlags ) { LPDEVICE lpDevice = iexSystem::Device; if( lpObj ) iexRenderState::Set( dwFlags & 0x0F, NULL, lpObj->GetTexture() ); else iexRenderState::Set( dwFlags & 0x0F, NULL, NULL ); // レンダリング lpDevice->SetFVF(D3DFVF_TLVERTEX); lpDevice->DrawPrimitiveUP( D3DPT_TRIANGLESTRIP, Num, v, sizeof(TLVERTEX) ); }
//------------------------------------------------------ // シェーダー版 //------------------------------------------------------ void iexPolygon::Render2D( LPTLVERTEX v, int Num, LPIEX2DOBJ lpObj, iexShader* shader, char* name ) { LPDEVICE lpDevice = iexSystem::Device; // シェーダーの適用 u32 pass = shader->Begine(name); lpDevice->SetFVF(D3DFVF_TLVERTEX); if( lpObj ) shader->SetTexture( lpObj->GetTexture() ); for( u32 p=0 ; p<pass ; p++ ) { shader->BeginePass(p); shader->CommitChanges(); // レンダリング lpDevice->DrawPrimitiveUP( D3DPT_TRIANGLESTRIP, Num, v, sizeof(TLVERTEX) ); shader->EndPass(); } shader->End(); }
//------------------------------------------------------ // シェーダー版 //------------------------------------------------------ void iexPolygon::Render3D( LPLVERTEX lpVertex, int Num, LPIEX2DOBJ lpObj, iexShader* shader, char* name ) { LPDEVICE lpDevice = iexSystem::Device; // シェーダーの適用 u32 pass = shader->Begine(name); // ローカル-射影変換行列 Matrix m = matView * matProjection; shader->SetMatrix( &m ); lpDevice->SetFVF(D3DFVF_LVERTEX); if( lpObj ) shader->SetTexture( lpObj->GetTexture() ); for( u32 p=0 ; p<pass ; p++ ) { shader->BeginePass(p); shader->CommitChanges(); // レンダリング lpDevice->DrawPrimitiveUP( D3DPT_TRIANGLESTRIP, Num, lpVertex, sizeof(LVERTEX) ); shader->EndPass(); } shader->End(); }
//***************************************************************************** // 3Dポリゴン //***************************************************************************** //------------------------------------------------------ // 固定機能版 //------------------------------------------------------ void iexPolygon::Render3D( LPLVERTEX lpVertex, int Num, iex2DObj* lpObj, u32 dwFlags ) { LPDEVICE lpDevice = iexSystem::Device; // ワールド行列設定 Matrix mat( 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 ); lpDevice->SetTransform( D3DTS_WORLD, (D3DMATRIX*)&mat ); // レンダーステート更新 if( lpObj ) iexRenderState::Set( dwFlags & 0x0F, NULL, lpObj->GetTexture() ); else iexRenderState::Set( dwFlags & 0x0F, NULL, NULL ); lpDevice->SetRenderState( D3DRS_LIGHTING, FALSE ); // レンダリング lpDevice->SetFVF(D3DFVF_LVERTEX); lpDevice->DrawPrimitiveUP( D3DPT_TRIANGLESTRIP, Num, lpVertex, sizeof(LVERTEX) ); lpDevice->SetRenderState( D3DRS_LIGHTING, TRUE ); }
//--------------------------------------------------- void PrepareFor3DDrawing( LPDEVICE pDevice, int viewport_width, int viewport_height, float fov_in_degrees, float near_clip, float far_clip, DISX_VECTOR3* pvEye, DISX_VECTOR3* pvLookat, DISX_VECTOR3* pvUp ) { // This function sets up DirectX up for 3D rendering. // Only call it once per frame, as it is VERY slow. // INPUTS: // pDevice a pointer to the D3D device // viewport_width the width of the client area of the window // viewport_height the height of the client area of the window // fov_in_degrees the field of view, in degrees // near_clip the distance to the near clip plane; should be > 0! // far_clip the distance to the far clip plane // eye the eyepoint coordinates, in world space // lookat the point toward which the eye is looking, in world space // up a vector indicating which dir. is up; usually <0,1,0> // // What this function does NOT do: // 1. set the current texture (SetTexture) // 2. set up the texture stages for texturing (SetTextureStageState) // 3. set the current vertex format (SetVertexShader) // 4. set up the world matrix (SetTransform(D3DTS_WORLD, &my_world_matrix)) // set up render state to some nice defaults: { // some defaults pDevice->SetRenderState( DISRS_ZENABLE, TRUE ); pDevice->SetRenderState( DISRS_ZWRITEENABLE, TRUE ); pDevice->SetRenderState( DISRS_ZFUNC, DIS_CMP_LESSEQUAL ); pDevice->SetRenderState( DISRS_CULLMODE, DIS_CULL_NONE ); pDevice->SetRenderState( DISRS_CLIPPING, TRUE ); pDevice->SetRenderState( DISRS_LIGHTING, FALSE ); pDevice->SetRenderState( DISRS_COLORVERTEX, TRUE ); pDevice->SetRenderState( DISRS_SHADEMODE, DIS_SHADE_GOURAUD ); pDevice->SetRenderState( DISRS_FILLMODE, DIS_FILL_SOLID ); pDevice->SetRenderState( DISRS_ALPHABLENDENABLE, FALSE ); // turn fog off pDevice->SetRenderState( DISRS_FOGENABLE, FALSE ); pDevice->SetRenderState( DISRS_RANGEFOGENABLE, FALSE ); // turn on high-quality bilinear interpolations pDevice->SetSamplerState(0, DIS_SAMP_MAGFILTER, DIS_TEXF_LINEAR); pDevice->SetSamplerState(1, DIS_SAMP_MAGFILTER, DIS_TEXF_LINEAR); pDevice->SetSamplerState(0, DIS_SAMP_MINFILTER, DIS_TEXF_LINEAR); pDevice->SetSamplerState(1, DIS_SAMP_MINFILTER, DIS_TEXF_LINEAR); pDevice->SetSamplerState(0, DIS_SAMP_MIPFILTER, DIS_TEXF_LINEAR); pDevice->SetSamplerState(1, DIS_SAMP_MIPFILTER, DIS_TEXF_LINEAR); } // set up view & projection matrices (but not the world matrix!) { // if the window is not square, instead of distorting the scene, // clip it so that the longer dimension of the window has the // regular FOV, and the shorter dimension has a reduced FOV. float fov_x = fov_in_degrees * 3.1415927f/180.0f; float fov_y = fov_in_degrees * 3.1415927f/180.0f; float aspect = (float)viewport_height / (float)viewport_width; if (aspect < 1) fov_y *= aspect; else fov_x /= aspect; if (near_clip < 0.1f) near_clip = 0.1f; if (far_clip < near_clip + 1.0f) far_clip = near_clip + 1.0f; DISX_MATRIX proj; MakeProjectionMatrix(&proj, near_clip, far_clip, fov_x, fov_y); pDevice->SetTransform(DIS_TS_PROJECTION, &proj); DISX_MATRIX view; DISX_MatrixLookAtLH(&view, pvEye, pvLookat, pvUp); pDevice->SetTransform(DIS_TS_VIEW, &view); // Optimization note: "You can minimize the number of required calculations // by concatenating your world and view matrices into a world-view matrix // that you set as the world matrix, and then setting the view matrix // to the identity." //D3DXMatrixMultiply(&world, &world, &view); //D3DXMatrixIdentity(&view); } }
void PrepareFor2DDrawing(LPDEVICE pDevice) { // New 2D drawing area will have x,y coords in the range <-1,-1> .. <1,1> // +--------+ Y=-1 // | | // | screen | Z=0: front of scene // | | Z=1: back of scene // +--------+ Y=1 // X=-1 X=1 // NOTE: After calling this, be sure to then call (at least): // 1. SetVertexShader() // 2. SetTexture(), if you need it // before rendering primitives! // Also, be sure your sprites have a z coordinate of 0. pDevice->SetRenderState( DISRS_ZENABLE, TRUE ); pDevice->SetRenderState( DISRS_ZWRITEENABLE, TRUE ); pDevice->SetRenderState( DISRS_ZFUNC, D3DCMP_LESSEQUAL ); pDevice->SetRenderState( DISRS_SHADEMODE, D3DSHADE_FLAT ); pDevice->SetRenderState( DISRS_FILLMODE, D3DFILL_SOLID ); pDevice->SetRenderState( DISRS_FOGENABLE, FALSE ); pDevice->SetRenderState( DISRS_CULLMODE, D3DCULL_NONE ); pDevice->SetRenderState( DISRS_CLIPPING, TRUE ); pDevice->SetRenderState( DISRS_LIGHTING, FALSE ); pDevice->SetRenderState( DISRS_ALPHABLENDENABLE, FALSE ); pDevice->SetRenderState( DISRS_LOCALVIEWER, FALSE ); pDevice->SetTexture(0, NULL); pDevice->SetTexture(1, NULL); pDevice->SetSamplerState(0, DIS_SAMP_MAGFILTER, D3DTEXF_POINT);//D3DTEXF_LINEAR); pDevice->SetSamplerState(1, DIS_SAMP_MAGFILTER, D3DTEXF_POINT);//D3DTEXF_LINEAR); pDevice->SetTextureStageState(0, DIS_TSS_TEXTURETRANSFORMFLAGS, DIS_TTFF_DISABLE); pDevice->SetTextureStageState(1, DIS_TSS_TEXTURETRANSFORMFLAGS, DIS_TTFF_DISABLE); pDevice->SetTextureStageState(0, DIS_TSS_COLOROP, DIS_TOP_MODULATE ); pDevice->SetTextureStageState(0, DIS_TSS_COLORARG1, DIS_TA_TEXTURE ); pDevice->SetTextureStageState(0, DIS_TSS_COLORARG2, DIS_TA_CURRENT ); pDevice->SetTextureStageState(1, DIS_TSS_COLOROP, DIS_TOP_DISABLE ); pDevice->SetTextureStageState(0, DIS_TSS_ALPHAOP, DIS_TOP_SELECTARG1 ); pDevice->SetTextureStageState(0, DIS_TSS_ALPHAARG1, DIS_TA_DIFFUSE ); pDevice->SetTextureStageState(1, DIS_TSS_ALPHAOP, DIS_TOP_DISABLE ); pDevice->SetRenderState( DISRS_ALPHABLENDENABLE, FALSE ); // set up for 2D drawing: { DISX_MATRIX Ortho2D; DISX_MATRIX Identity; DISX_MatrixOrthoLH(&Ortho2D, 2.0f, -2.0f, 0.f, 1.0f); //DISX_MatrixPerspectiveLH(&Ortho2D, 1280.0f, 1024.0f, 0.1f, 100.0f); DISX_MatrixIdentity(&Identity); pDevice->SetTransform(DIS_TS_PROJECTION, &Ortho2D); pDevice->SetTransform(DIS_TS_WORLD, &Identity); pDevice->SetTransform(DIS_TS_VIEW, &Identity); } }