// called before rendering void Effect::startEffect() { if (!m_pEffect) return; m_clipPlaneChanged = false; // if a clipping plane is specified, it must be converted to projections space // the plane is currently assumed to be in world space. if (gRenderer.getClipPlaneEnable()) { D3DXMATRIX View, Projection,ViewProj; D3DXPLANE projPlane, worldPlane; //D3DXVECTOR4 projClipPlane; gRenderer.getClipPlane(&m_oldClipPlane); worldPlane.a = m_oldClipPlane.a; worldPlane.b = m_oldClipPlane.b; worldPlane.c = m_oldClipPlane.c; worldPlane.d = m_oldClipPlane.d; // get all matrices from device pD3DDevice->GetTransform(D3DTS_VIEW, & View); pD3DDevice->GetTransform(D3DTS_PROJECTION, & Projection); // multiply them ViewProj = View * Projection; //worldPlane.y *= -1; D3DXPlaneNormalize(&worldPlane, &worldPlane); D3DXMatrixInverse(&ViewProj, NULL ,&ViewProj); D3DXMatrixTranspose(&ViewProj ,&ViewProj); D3DXPlaneTransform(&projPlane, &worldPlane, &ViewProj); D3DXPlaneNormalize(&projPlane, &projPlane); Plane passIn(projPlane.a, projPlane.b, projPlane.c, projPlane.d); gRenderer.setClipPlane(passIn); m_clipPlaneChanged = true; } m_pEffect->Begin(0,0); m_pEffect->BeginPass(0); m_pEffect->CommitChanges(); }
HRESULT KGraphicsEngine::GetPickRay(D3DXVECTOR3 * RayOrig,D3DXVECTOR3* RayDir ) { D3DXVECTOR3 vPickRayDir; D3DXVECTOR3 vPickRayOrig; D3DXMATRIX matProj; g_pd3dDevice->GetTransform( D3DTS_PROJECTION, &matProj ); POINT ptCursor; GetCursorPos( &ptCursor ); //ScreenToClient( g_hRenderWnd, &ptCursor ); //ptCursor.x -= m_RenderWindowRect.left; //ptCursor.y += m_RenderWindowRect.top; D3DXVECTOR3 v; // Compute the vector of the pick ray in screen space ::GetWindowRect(g_hRenderWnd,&m_RenderWindowRect); float Width = (float)( m_RenderWindowRect.right - m_RenderWindowRect.left); float Height = (float)( m_RenderWindowRect.bottom - m_RenderWindowRect.top); float X = (float)(ptCursor.x - m_RenderWindowRect.left); float Y = (float)(ptCursor.y - m_RenderWindowRect.top ); v.x =(float) ( ( ( 2.0f * X ) / Width ) - 1.0f) / matProj._11; v.y =(float) -( ( ( 2.0f * Y ) / Height ) - 1.0f) / matProj._22; v.z = 1.0f; // Get the inverse view matrix D3DXMATRIX matView, m; g_pd3dDevice->GetTransform( D3DTS_VIEW, &matView ); D3DXMatrixInverse( &m, NULL, &matView ); // Transform the screen space pick ray into 3D space vPickRayDir.x = v.x*m._11 + v.y*m._21 + v.z*m._31; vPickRayDir.y = v.x*m._12 + v.y*m._22 + v.z*m._32; vPickRayDir.z = v.x*m._13 + v.y*m._23 + v.z*m._33; vPickRayOrig.x = m._41 ; vPickRayOrig.y = m._42 ; vPickRayOrig.z = m._43 ; *RayOrig = vPickRayOrig; *RayDir = vPickRayDir ; return S_OK; }
type_result HierarchySubReferer::_render( D3DMATERIAL9& material ) { _initializeMesh(); // main: // DX9 바인딩: DX9& dx9 = getDependent(); if( ! &dx9) { ALERT_ERROR(" : 디바이스 바인딩 실패로 중지"); return RESULT_TYPE_ERROR; } LPDIRECT3DDEVICE9 device = dx9.getDevice(); // 월드좌표 갱신: D3DXMATRIX& world = getWorldMatrix(); D3DXMATRIX origin; device->GetTransform(D3DTS_WORLD, &origin); device->SetTransform(D3DTS_WORLD, &world); device->SetRenderState(D3DRS_AMBIENT, 0xffffffff); //device->SetTexture(0, NULL); device->SetMaterial(&material); device->SetTexture(0, 0); if(_ball) { _ball->DrawSubset(0); } // 자식과 선을 잇기: device->SetTransform(D3DTS_WORLD, &origin); _searchModuleSet(getConnector().getModuleCodeSetKey().getValue(), &ThisClass::_renderLineBetweenChild); // 월드 좌표 복귀: return RESULT_SUCCESS; }
void RenderTree() { // Disable lighting g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE) ; g_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE ); g_pd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA ); g_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA ); // Get current world matrix D3DXMATRIX matCurrentWorld ; g_pd3dDevice->GetTransform(D3DTS_WORLD, &matCurrentWorld) ; // Set world matrix for billboard g_pd3dDevice->SetTransform(D3DTS_WORLD, &g_matBillboardWorld) ; // Set texture g_pd3dDevice->SetTexture(0, g_pTexture) ; g_pd3dDevice->SetStreamSource(0, g_pVB, 0, sizeof(Vertex)) ; g_pd3dDevice->SetFVF(Vertex_FVF) ; g_pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID) ; g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2) ; // Restore world matrix g_pd3dDevice->SetTransform(D3DTS_WORLD, &matCurrentWorld) ; g_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE ); }
//----------------------------------------------------------------------------- HRESULT CDXUTMeshFrame::Render( LPDIRECT3DDEVICE9 pd3dDevice, bool bDrawOpaqueSubsets, bool bDrawAlphaSubsets, D3DXMATRIX* pmatWorldMatrix ) { // For pure devices, specify the world transform. If the world transform is not // specified on pure devices, this function will fail. D3DXMATRIX matSavedWorld, matWorld; if ( NULL == pmatWorldMatrix ) pd3dDevice->GetTransform( D3DTS_WORLD, &matSavedWorld ); else matSavedWorld = *pmatWorldMatrix; D3DXMatrixMultiply( &matWorld, &m_mat, &matSavedWorld ); pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld ); if( m_pMesh ) m_pMesh->Render( pd3dDevice, bDrawOpaqueSubsets, bDrawAlphaSubsets ); if( m_pChild ) m_pChild->Render( pd3dDevice, bDrawOpaqueSubsets, bDrawAlphaSubsets, &matWorld ); pd3dDevice->SetTransform( D3DTS_WORLD, &matSavedWorld ); if( m_pNext ) m_pNext->Render( pd3dDevice, bDrawOpaqueSubsets, bDrawAlphaSubsets, &matSavedWorld ); return S_OK; }
void Draw( int iMeshIndex ) const { const MeshInfo& meshInfo = m_vMeshInfo[iMeshIndex]; if( meshInfo.bNeedsTextureMatrixScale ) { // Kill the texture translation. // XXX: Change me to scale the translation by the TextureTranslationScale of the first vertex. RageMatrix m; g_pd3dDevice->GetTransform( D3DTS_TEXTURE0, (D3DMATRIX*)&m ); m.m[2][0] = 0; m.m[2][1] = 0; g_pd3dDevice->SetTransform( D3DTS_TEXTURE0, (D3DMATRIX*)&m ); } g_pd3dDevice->SetFVF( D3DFVF_RageModelVertex ); g_pd3dDevice->DrawIndexedPrimitiveUP( D3DPT_TRIANGLELIST, // PrimitiveType meshInfo.iVertexStart, // MinIndex meshInfo.iVertexCount, // NumVertices meshInfo.iTriangleCount, // PrimitiveCount, &m_vTriangles[0]+meshInfo.iTriangleStart,// pIndexData, D3DFMT_INDEX16, // IndexDataFormat, &m_vVertex[0], // pVertexStreamZeroData, sizeof(m_vVertex[0]) // VertexStreamZeroStride ); }
void COverlayQuadsDX::Render(SRenderState &state) { if (m_count == 0) return; D3DXMATRIX orig; LPDIRECT3DDEVICE9 device = g_Windowing.Get3DDevice(); device->GetTransform(D3DTS_WORLD, &orig); D3DXMATRIX world = orig; D3DXMATRIX trans, scale; D3DXMatrixTranslation(&trans, state.x - 0.5f , state.y - 0.5f , 0.0f); D3DXMatrixScaling (&scale, state.width , state.height , 1.0f); D3DXMatrixMultiply(&world, &world, &scale); D3DXMatrixMultiply(&world, &world, &trans); device->SetTransform(D3DTS_WORLD, &world); device->SetTexture( 0, m_texture.Get() ); device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); device->SetSamplerState(0, D3DSAMP_ADDRESSU , D3DTADDRESS_CLAMP); device->SetSamplerState(0, D3DSAMP_ADDRESSV , D3DTADDRESS_CLAMP); device->SetTextureStageState(0, D3DTSS_COLOROP , D3DTOP_SELECTARG1 ); device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_DIFFUSE ); device->SetTextureStageState(0, D3DTSS_ALPHAOP , D3DTOP_MODULATE ); device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE ); device->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE ); device->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE ); device->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE ); device->SetRenderState( D3DRS_LIGHTING , FALSE ); device->SetRenderState( D3DRS_ZENABLE , FALSE ); device->SetRenderState( D3DRS_FOGENABLE, FALSE ); device->SetRenderState( D3DRS_ALPHATESTENABLE, TRUE ); device->SetRenderState( D3DRS_ALPHAREF , 0 ); device->SetRenderState( D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL ); device->SetRenderState( D3DRS_FILLMODE , D3DFILL_SOLID ); device->SetRenderState( D3DRS_CULLMODE , D3DCULL_NONE ); device->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE ); device->SetRenderState( D3DRS_SRCBLEND , D3DBLEND_SRCALPHA ); device->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA ); device->SetFVF(m_fvf); device->SetStreamSource(0, m_vertex.Get(), 0, sizeof(VERTEX)); device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, m_count*2); device->SetTexture(0, NULL); device->SetTransform(D3DTS_WORLD, &orig); }
HRESULT HookIDirect3DDevice9::GetTransform(LPVOID _this, D3DTRANSFORMSTATETYPE State, D3DMATRIX* pMatrix) { LOG_API(); return pD3Dev->GetTransform(State, pMatrix); }
void CLcMdl2D::Render() { DWORD dMnLgt; LPDIRECT3DDEVICE9 pDev = (LPDIRECT3DDEVICE9)LcDev_GetD3Device(); pDev->GetRenderState( D3DRS_LIGHTING, &dMnLgt); pDev->SetRenderState( D3DRS_LIGHTING, FALSE); pDev->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE); pDev->SetRenderState(D3DRS_ALPHAREF, 0x40); pDev->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL); pDev->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); pDev->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); pDev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); pDev->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); pDev->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); pDev->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR); pDev->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE ); pDev->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE ); pDev->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE ); pDev->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE ); pDev->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE ); pDev->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE ); D3DXMATRIX mtWld; D3DXMATRIX mtViw; D3DXMATRIX mtViwI; pDev->GetTransform(D3DTS_VIEW, &mtViw); D3DXMatrixInverse(&mtViwI, NULL, &mtViw); mtViwI._41 = 0; mtViwI._42 = 0; mtViwI._43 = 0; mtWld = mtViwI * m_mtWld; pDev->SetTransform(D3DTS_WORLD, &mtWld); PDTX pTex = (PDTX)m_pTex->GetPointer(); pDev->SetTexture(0, pTex); pDev->SetFVF(VtxDUV1::FVF); pDev->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, m_pVtx, sizeof(VtxDUV1)); pDev->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE); pDev->SetRenderState( D3DRS_ALPHATESTENABLE, FALSE); pDev->SetRenderState( D3DRS_ZWRITEENABLE, TRUE); pDev->SetRenderState( D3DRS_LIGHTING, dMnLgt); LcD3D_SetWorldIdentity(pDev); }
RAY CPicking::CalcPickingRay(LPDIRECT3DDEVICE9 _device, HWND _hWnd, int x, int y) { RAY ray; RECT rt; GetClientRect(_hWnd, &rt); float px = 0.0f; float py = 0.0f; //get viewport D3DVIEWPORT9 vp; _device->GetViewport(&vp); //get projection D3DXMATRIX proj; _device->GetTransform(D3DTS_PROJECTION, &proj); //px = ( ((2.0f*x) / vp.Width) - 1.0f ) / proj(0,0); //py = ( ((-2.0f*y) / vp.Height) + 1.0f ) / proj(1,1); px = ( ((2.0f*x) / vp.Width) - 1.0f ); py = ( ((-2.0f*y) / vp.Height) + 1.0f ); px /= proj._11; py /= proj._22; ray.origin = D3DXVECTOR3(0.0f, 0.0f, 0.0f); ray.direction = D3DXVECTOR3(px, py, 1.0f); D3DXMATRIX view; _device->GetTransform(D3DTS_VIEW, &view); D3DXMATRIX viewInverse; D3DXMatrixInverse(&viewInverse, 0, &view); // 광선의 원점을 변환한다 w = 1 D3DXVec3TransformCoord(&ray.origin, &ray.origin, &viewInverse); // 광선의 방향을 변환한다 w = 0 D3DXVec3TransformNormal(&ray.direction, &ray.direction, &viewInverse); // 방향을 정규화 D3DXVec3Normalize(&ray.direction, &ray.direction); return ray; }
/// \param matrixName Name of matrix you want to set in effect file. void Effect::setWorldViewProjMatrixFromDevice(const std::string &matrixName) { if (!m_pEffect) return; D3DXMATRIX World,View, Projection,WorldViewProj; // get all matrices from device pD3DDevice->GetTransform(D3DTS_WORLD, & World); pD3DDevice->GetTransform(D3DTS_VIEW, & View); pD3DDevice->GetTransform(D3DTS_PROJECTION, & Projection); // multiply them WorldViewProj = World * View * Projection; // pass them to effect file m_pEffect->SetMatrix(matrixName.c_str(), &WorldViewProj); }
// 使用DirectX9來繪圖 void RenderFrameDX9(void) { LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9(); D3DMATRIX pMatrix[3]={0}; device->GetTransform(D3DTS_WORLD , &pMatrix[0] ); device->GetTransform(D3DTS_VIEW , &pMatrix[1] ); device->GetTransform(D3DTS_PROJECTION , &pMatrix[2] ); //D3DXMatrixOrthoLH(&matProject, width, height, Znear, Zfar); device->SetRenderState( D3DRS_LIGHTING, FALSE ); device->Clear( 0, NULL, // 清除整個畫面 D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, // 清除顏色跟Z Buffer D3DCOLOR_ARGB(0, 0, 0, 0), // 設定要把顏色清成黑色 1.0f, // 設定要把Z值清為1, 也就是離鏡頭最遠. 0 // 設定要把Stencil buffer清為0, 在這沒差. ); // 計算出一個可以轉換到鏡頭座標系的矩陣 Matrix4x4 view_matrix = GutMatrixLookAtRH(g_eye, g_lookat, g_up); // 設定鏡頭轉換矩陣 // 因為記憶體排列方法相同, 可以直接把Matrix4x4轉型成D3DMATRIX. device->SetTransform(D3DTS_VIEW, (D3DMATRIX *) &view_matrix); // 計算出一個使用非平行投影的矩陣 Matrix4x4 perspective_matrix = GutMatrixPerspectiveRH_DirectX(90.0f, 1.0f, 1.0f, 100.0f); // 設定視角轉換矩陣 device->SetTransform(D3DTS_PROJECTION, (D3DMATRIX *)&perspective_matrix); // 開始下繪圖指令 device->BeginScene(); // 設定資料格式 device->SetFVF(D3DFVF_XYZ); // 畫出金字塔的8條邊線 device->DrawPrimitiveUP(D3DPT_LINELIST, 8, g_vertices, sizeof(Vector4)); // 宣告所有的繪圖指令都下完了 device->EndScene(); // 呈現出背景backbuffer的畫面 device->Present( NULL, NULL, NULL, NULL ); }
void initEffect() { ID3DXBuffer* errorBuffer = 0; D3DXCreateEffectFromFile( device, L"effect.fx", NULL, // CONST D3DXMACRO* pDefines, NULL, // LPD3DXINCLUDE pInclude, D3DXSHADER_USE_LEGACY_D3DX9_31_DLL, NULL, // LPD3DXEFFECTPOOL pPool, &effect, &errorBuffer); if(errorBuffer) { MessageBoxA(hMainWnd, (char*)errorBuffer->GetBufferPointer(), 0, 0); errorBuffer->Release(); terminate(); } D3DXMATRIX W, V, P, Result; D3DXMatrixIdentity(&Result); device->GetTransform(D3DTS_WORLD, &W); device->GetTransform(D3DTS_VIEW, &V); device->GetTransform(D3DTS_PROJECTION, &P); D3DXMatrixMultiply(&Result, &W, &V); D3DXMatrixMultiply(&Result, &Result, &P); effect->SetMatrix(effect->GetParameterByName(0, "WorldViewProj"), &Result); effect->SetTechnique(effect->GetTechnique(0)); auto hr = effect->SetTexture(effect->GetParameterByName(NULL, "Overlay"), overlayTexture); hr |= effect->SetTexture(effect->GetParameterByName(NULL, "Base"), particleTexture); hr |= effect->SetTexture(effect->GetParameterByName(NULL, "PreRender"), renderTexture); if(hr != 0) { MessageBox(hMainWnd, L"Unable to set effect textures.", L"", MB_ICONHAND); } }
/// \param matrixName Name of matrix you want to set in the effect file. void Effect::setWorldMatrix(const std::string &matrixName) { if (!m_pEffect) return; D3DXMATRIX world; // get all matrices from device pD3DDevice->GetTransform(D3DTS_WORLD, &world); m_pEffect->SetMatrix(matrixName.c_str(), &world); }
void CFrustum::ViewToWorld(LPDIRECT3DDEVICE9 pGraphicDev , _bool bMakPlane) //영역 변환 { memcpy(m_vConvert_Point, m_vOriginal_Point, sizeof(_vec3) * 8); _matrix matView; pGraphicDev->GetTransform(D3DTS_VIEW, &matView); D3DXMatrixInverse(&matView, NULL, &matView); for (_int i = 0; i < 8; ++i) D3DXVec3TransformCoord(&m_vConvert_Point[i], &m_vConvert_Point[i], &matView); if(bMakPlane == true) Make_Plane(); }
/** * モデルを描画する。 * プラットフォームごとの固有設定も行う。 * モデルが設定されてない場合は何もしない。 * @param gl */ void LAppModel::draw() { if (live2DModel == NULL)return; // 座標変換退避 D3DXMATRIXA16 buf ; g_pD3DDevice->GetTransform(D3DTS_WORLD, &buf);//World座標を取得 // モデルの変換を適用 float* tr = modelMatrix->getArray() ;//float[16] g_pD3DDevice->MultiplyTransform( D3DTS_WORLD , (D3DXMATRIXA16*)tr ) ; // Live2Dを描画 live2DModel->draw(); g_pD3DDevice->SetTransform(D3DTS_WORLD, &buf);//変換を復元 }
INT CLcXSkinIns::FrameMove() { LPDIRECT3DDEVICE9 pDev = (LPDIRECT3DDEVICE9)LcDev_GetD3Device(); m_dTimeCur += m_fElapse; // For Sort...(optional) D3DXMATRIX mtViwI; pDev->GetTransform( D3DTS_VIEW, &mtViwI ); D3DXMatrixInverse(&mtViwI, NULL, &mtViwI); D3DXVECTOR3 vcCam = D3DXVECTOR3(mtViwI._41, mtViwI._42, mtViwI._43); D3DXVECTOR3 vcZ = D3DXVECTOR3(mtViwI._31, mtViwI._32, mtViwI._33); D3DXVECTOR3 vcTmp = m_vcTrn - vcCam; m_fStlSrtR = D3DXVec3Dot(&vcZ, &vcTmp); return 0; }
HRESULT CFrustum::Init_Frustum(LPDIRECT3DDEVICE9 pGraphicDev) //8개의 평면 생성과 영역 변환 { m_pGraphicDev = pGraphicDev; m_vOriginal_Point[0] = _vec3(-1.f , 1.f , 0.f); m_vOriginal_Point[1] = _vec3(1.f , 1.f , 0.f); m_vOriginal_Point[2] = _vec3(1.f , -1.f , 0.f); m_vOriginal_Point[3] = _vec3(-1.f , -1.f , 0.f); m_vOriginal_Point[4] = _vec3(-1.f , 1.f , 1.f); m_vOriginal_Point[5] = _vec3(1.f , 1.f , 1.f); m_vOriginal_Point[6] = _vec3(1.f , -1.f , 1.f); m_vOriginal_Point[7] = _vec3(-1.f , -1.f , 1.f); _matrix matProj; pGraphicDev->GetTransform(D3DTS_PROJECTION , &matProj); D3DXMatrixInverse(&matProj , NULL , &matProj); for(_int i = 0 ; i < 8; ++i) D3DXVec3TransformCoord(&m_vOriginal_Point[i] , &m_vOriginal_Point[i] , &matProj); return NULL; }
//描画 void Vertices::Draw(LPDIRECT3DDEVICE9 pD3DDevice, D3DXMATRIX *pmtrx) { if (!pD3DDevice || !pVertexBuffer) return; D3DXMATRIX mtrxOld; //ワールド変換行列保存用 pD3DDevice->GetTransform(D3DTS_WORLD, &mtrxOld); //ワールド行列を保存 pD3DDevice->SetTransform(D3DTS_WORLD, pmtrx); //デバイスにマトリックスを設定 pD3DDevice->SetStreamSource(0, pVertexBuffer, 0, sizeof(D3DVERTEX)); //頂点バッファをデバイスのストリームにセット if (pIndexBuffer) //インデックスバッファが存在する場合 { pD3DDevice->SetIndices(pIndexBuffer); //インデックスバッファをセット pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, vectorD3DVERTEX.size(), 0, Primitives); //頂点インデックスありで描画 } else { //存在しない場合 pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, Primitives); //頂点インデックスなしで描画 } pD3DDevice->SetTransform(D3DTS_WORLD, &mtrxOld); //デバイスのワールド変換行列を復元 return; }
//----------------------------------------------------------------------------- HRESULT CDXUTMeshFile::Render( LPDIRECT3DDEVICE9 pd3dDevice, D3DXMATRIX* pmatWorldMatrix ) { // For pure devices, specify the world transform. If the world transform is not // specified on pure devices, this function will fail. // Set up the world transformation D3DXMATRIX matSavedWorld, matWorld; if ( NULL == pmatWorldMatrix ) pd3dDevice->GetTransform( D3DTS_WORLD, &matSavedWorld ); else matSavedWorld = *pmatWorldMatrix; D3DXMatrixMultiply( &matWorld, &matSavedWorld, &m_mat ); pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld ); // Render opaque subsets in the meshes if( m_pChild ) m_pChild->Render( pd3dDevice, TRUE, FALSE, &matWorld ); // Enable alpha blending pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE ); pd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA ); pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA ); // Render alpha subsets in the meshes if( m_pChild ) m_pChild->Render( pd3dDevice, FALSE, TRUE, &matWorld ); // Restore state pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE ); pd3dDevice->SetTransform( D3DTS_WORLD, &matSavedWorld ); return S_OK; }
//============================================================================= // 描画処理 //============================================================================= void CMeshField::Draw(int pTexture) { D3DXMATRIX mtxWorld; D3DXMATRIX mtxScl,mtxRot,mtxTranslate; SetMtxView(CManager::GetCamera()->GetMtxView()); // 視錐台 作成 FRUSTUM sFrustum; // セットアップする関数 SetupFOVClipPlanes(VIEW_ANGLE,VIEW_ASPECT,VIEW_NEAR_Z,VIEW_FAR_Z,sFrustum); //頂点バッファの中身を埋める VERTEX_3D *pVtx; // 頂点データの範囲をロックし、頂点バッファへのポインタを取得 m_pD3DVtxBuff->Lock(0, 0, (void**)&pVtx, 0); for(int nCntVtxZ = 0; nCntVtxZ < (m_nNumBlockZ + 1); nCntVtxZ++) { for(int nCntVtxX = 0; nCntVtxX < (m_nNumBlockX + 1); nCntVtxX++) { VECTOR3 pos; // 頂点座標の設定 pos.x=pVtx[nCntVtxZ * (m_nNumBlockX + 1) + nCntVtxX].vtx.x; pos.z=pVtx[nCntVtxZ * (m_nNumBlockX + 1) + nCntVtxX].vtx.z; pos.y=pVtx[nCntVtxZ * (m_nNumBlockX + 1) + nCntVtxX].vtx.y; MATRIX4x4 matrix; matrix._11 = m_MtxView._11; matrix._12 = m_MtxView._12; matrix._13 = m_MtxView._13; matrix._14 = m_MtxView._14; matrix._21 = m_MtxView._21; matrix._22 = m_MtxView._22; matrix._23 = m_MtxView._23; matrix._24 = m_MtxView._24; matrix._31 = m_MtxView._31; matrix._32 = m_MtxView._32; matrix._33 = m_MtxView._33; matrix._34 = m_MtxView._34; matrix._41 = m_MtxView._41; matrix._42 = m_MtxView._42; matrix._43 = m_MtxView._43; matrix._44 = m_MtxView._44; // ??? if(!MeshFOVCheck(&pos,8,sFrustum,matrix)) //if( 判定用関数 ) // 視錐台から、外れていたら { continue; // 描画しないで、次のモデルへ } } } // 頂点データをアンロックする m_pD3DVtxBuff->Unlock(); LPDIRECT3DDEVICE9 pDevice = CManager::GetDevice(); // ワールドマトリックスの初期化 D3DXMatrixIdentity(&m_mtxWorld); // 回転を反映 D3DXMatrixRotationYawPitchRoll(&mtxRot, m_Rot.y, m_Rot.x, m_Rot.z); D3DXMatrixMultiply(&m_mtxWorld, &m_mtxWorld, &mtxRot); // 移動を反映 D3DXMatrixTranslation(&mtxTranslate, m_Pos.x, m_Pos.y, m_Pos.z); D3DXMatrixMultiply(&m_mtxWorld, &m_mtxWorld, &mtxTranslate); if (m_bTransParent) { pDevice->SetRenderState(D3DRS_ZENABLE, FALSE); // Zバッファを使用しない } else { pDevice->SetRenderState(D3DRS_ZENABLE, TRUE); // Zバッファを使用する } //ワールド行列変数 D3DXMATRIX world, view, proj, rot, pos, mtxParent; D3DXMATRIX invWorld; //ビュー行列 D3DXVECTOR3 eye(0.0f, 50.0f, -100.0f); D3DXVECTOR3 at(0.0f, 20.0f, 0.0f); D3DXVECTOR3 up(0.0f, 1.0f, 0.0f); D3DXVECTOR3 lightVec(0.20f, 0.0f, 0.80f); D3DXCOLOR lightDiffuse(0.8f, 0.8f, 0.8f, 1.0f); D3DXCOLOR lightDiffuse2(0.8f, 0.4f, 0.4f, 1.0f); D3DXCOLOR lightAmbient(0.5f, 0.5f, 0.5f, 1.0f); CCamera *pCamera; // カメラを取得 pCamera = CManager::GetCamera(); eye = pCamera->GetPosP(); at = pCamera->GetPosR(); up = pCamera->GetVecUp(); pDevice->GetTransform(D3DTS_WORLD, &mtxParent); //ワールド行列 D3DXMatrixIdentity(&m_mtxWorld); D3DXMatrixRotationYawPitchRoll(&rot, m_Rot.y, m_Rot.x, m_Rot.z); D3DXMatrixMultiply(&m_mtxWorld, &m_mtxWorld, &rot); D3DXMatrixTranslation(&pos, m_Pos.x, m_Pos.y, m_Pos.z); D3DXMatrixMultiply(&m_mtxWorld, &m_mtxWorld, &pos); D3DXMatrixMultiply(&m_mtxWorld, &m_mtxWorld, &mtxParent); D3DXMatrixInverse(&invWorld, NULL, &m_mtxWorld); D3DXVec3TransformCoord(&lightVec, &lightVec, &invWorld); D3DXMatrixLookAtLH(&view, &eye, &at, &up); D3DXMatrixPerspectiveFovLH(&proj, D3DXToRadian(45), 960.0f / 540.0f, 10.0f, 100000.0f); D3DXMATRIX wvp = m_mtxWorld*view*proj; D3DXVECTOR3 cameraVec = at - eye; D3DXVec3Normalize(&cameraVec, &cameraVec); D3DXVec3Normalize(&lightVec, &lightVec); D3DXVECTOR3 playerPos = CGame::GetPlayer(0)->GetPos(); // ワールドマトリックスの設定 pDevice->SetTransform(D3DTS_WORLD, &m_mtxWorld); // 頂点バッファをレンダリングパイプラインに設定 pDevice->SetStreamSource(0, m_pD3DVtxBuff, 0, sizeof(VERTEX_3D)); // インデックスバッファをレンダリングパイプラインに設定 pDevice->SetIndices(m_pD3DIndexBuff); // 頂点フォーマットの設定 pDevice->SetFVF(FVF_VERTEX_3D); if (pTexture == NULL) { pDevice->SetRenderState(D3DRS_FOGENABLE, TRUE); pDevice->SetRenderState(D3DRS_FOGCOLOR, D3DCOLOR_XRGB(255, 255, 255)); _vsc[1]->SetMatrix(pDevice, "world", &m_mtxWorld); _vsc[1]->SetMatrix(pDevice, "gWvp", &wvp); _vsc[1]->SetFloatArray(pDevice, "LightDir", (float*)&lightVec, 3); _vsc[1]->SetVector(pDevice, "LightDiffuse", (D3DXVECTOR4*)&lightDiffuse); _vsc[1]->SetVector(pDevice, "LightDiffuse2", (D3DXVECTOR4*)&lightDiffuse2); _vsc[1]->SetVector(pDevice, "LightAmbient", (D3DXVECTOR4*)&lightAmbient); _vsc[1]->SetFloatArray(pDevice, "CameraVec", (float*)&cameraVec, 3); _vsc[1]->SetFloatArray(pDevice, "PlayerPos", (float*)&playerPos, 3); _vsc[1]->SetFloatArray(pDevice, "Pos", (float*)&eye, 3); // テクスチャの設定 pDevice->SetTexture(0, CTexture::GetTex(m_texid)); unsigned int s0 = _psc->GetSamplerIndex("texSampler"); pDevice->SetTexture(s0, CTexture::GetTex(m_texid)); pDevice->SetVertexShader(_vs[1]); } else { _vsc[0]->SetMatrix(pDevice, "world", &m_mtxWorld); _vsc[0]->SetMatrix(pDevice, "gWvp", &wvp); _vsc[0]->SetFloatArray(pDevice, "LightDir", (float*)&lightVec, 3); _vsc[0]->SetVector(pDevice, "LightDiffuse", (D3DXVECTOR4*)&lightDiffuse); _vsc[0]->SetVector(pDevice, "LightDiffuse2", (D3DXVECTOR4*)&lightDiffuse2); _vsc[0]->SetVector(pDevice, "LightAmbient", (D3DXVECTOR4*)&lightAmbient); _vsc[0]->SetFloatArray(pDevice, "CameraVec", (float*)&cameraVec, 3); _vsc[0]->SetFloatArray(pDevice, "PlayerPos", (float*)&playerPos, 3); _vsc[0]->SetFloatArray(pDevice, "Pos", (float*)&m_Pos, 3); // テクスチャの設定 pDevice->SetTexture(0, CTexture::GetTex(pTexture)); unsigned int s0 = _psc->GetSamplerIndex("shadowSampler"); pDevice->SetTexture(s0, CTexture::GetTex(pTexture)); pDevice->SetVertexShader(_vs[0]); } pDevice->SetPixelShader(_ps); // マテリアルの設定 pDevice->SetMaterial(&m_material); // ポリゴンの描画 pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLESTRIP, 0, 0, m_nNumVertex, 0, m_nNumPolygon); pDevice->SetRenderState(D3DRS_FOGENABLE, FALSE); pDevice->SetRenderState(D3DRS_ZENABLE, TRUE); // Zバッファを使用する pDevice->SetVertexShader(nullptr); pDevice->SetPixelShader(nullptr); }
INT CLcAcmIns::FrameMove() { INT hr = 0; INT iNgeo = m_pLcHead->iNgeo; INT i = 0; INT dFrmB = 0; // Begin Frame INT dFrmE = 0; // End Frame if(!m_bAnima) return 0; if(NULL == m_pFrame || m_pFrame->empty()) { dFrmB = m_pLcHead->nFrmF ; dFrmE = m_pLcHead->nFrmL ; } else { vector<_Tframe >::iterator p = m_pFrame->begin() + m_nActM; dFrmB = p->nB; dFrmE = p->nE; } // 현재 프레임 계산 : Frame = FrameSpeed(n/s) * Time(s) + Begin Frame // 시간 단위: 초 m_dFrmCur = m_pLcHead->nFrmS * m_dTimeCur + dFrmB; // End Frame보다 크면 재조정 if(m_dFrmCur>= dFrmE) { // 나머지 시간을 현재시간으로 설정 FLOAT t = FLOAT(dFrmE-dFrmB) /m_pLcHead->nFrmS; m_dTimeCur = m_dTimeCur - t; m_dFrmCur = m_pLcHead->nFrmS * m_dTimeCur + dFrmB; hr = 1; // Return 값을 애니메이션 한 번 수행 것으로한다. } // 1. 현재 프레임, 다음 프레임, 비중을 구한다. // 시간에서 프레임을 구한다. INT nFrmC = INT(m_dFrmCur); // End Frame FLOAT fFrmW = m_dFrmCur - nFrmC; // 소수점은 비중이 된다. INT nFrmN = nFrmC+1; // Next Frame // // End Frame보다 크면 재조정 // if(1 == hr) // nFrmN = nFrmC; // 2. 애니메이션에 관련된 행렬들을 갱신한다. // 월드 행렬 = 지역행렬 * 부모의 월드 행렬 * 전체 월드 행렬 // 전체 월드 행렬의 곱셈 적용은 쉐이더에서 한다. for(i=0; i<m_pLcHead->iNgeo; ++i) { CLcAcm::LcGeo* pDst = &m_pLcGeo[i]; MATA mtL(1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1); MATA mtP(1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1); if(pDst->nPrn>=0) { CLcAcm::LcGeo* pPrn = &m_pLcGeo[pDst->nPrn]; mtP = pPrn->mtWld; } // 프레임에 대한 행렬이 있는 경우 월드 행렬 = 지역행렬 * 부모의 월드 행렬 // 을 적용하지 않고 바로 월드 행렬을 구한다. // 여기서 구한 월드 행렬은 맥스에서 구성한 장면의 기준 좌표계가 된다. // 각각의 Geometry의 행렬 = 해당 방향 행렬 * 지역행렬 if(!pDst->vAniRot->empty()) { VEC3 p(0,0,0); QUAT q(0,0,0,1); VEC3 p1 = *(pDst->vAniTrn->begin() + nFrmC); VEC3 p2 = *(pDst->vAniTrn->begin() + nFrmN); QUAT q1 = *(pDst->vAniRot->begin() + nFrmC); QUAT q2 = *(pDst->vAniRot->begin() + nFrmN); p = p1 * (1.f - fFrmW) + p2 * fFrmW; // 위치를 선형 보간 D3DXQuaternionSlerp(&q, &q1, &q2, fFrmW); // 회전을 선형 보간. slerp을 이용 D3DXMatrixRotationQuaternion(&mtL, &q); mtL._41 = p.x; mtL._42 = p.y; mtL._43 = p.z; pDst->mtWld = pDst->mtOrn * mtL; // 맥스에서 지정된 방향 행렬을 곱해야 한다. if(NULL == m_mtPrn) pDst->mtPvt = mtL * m_mtWld; else pDst->mtPvt = mtL * (*m_mtPrn); } else // 계층구조와 동일 { pDst->mtWld = mtL * mtP; if(NULL == m_mtPrn) pDst->mtPvt = pDst->mtOrI * pDst->mtWld * m_mtWld; else pDst->mtPvt = pDst->mtOrI * pDst->mtWld * (*m_mtPrn); } m_pmtWld[i] = pDst->mtWld; // 행렬 팔레트에 복사. } // // Texture Animation // int iSize = (INT)m_vIfl.size(); // // dTimeC = ::timeGetTime(); // if(dTimeC >m_dIflTime+m_dIflIntv) // { // m_dIflTime = dTimeC; // // for(i=0; i<iSize; ++i) // { // CLcAcm::TaniTx* pTani = &m_vIfl[i]; // CLcAcm::LcMtl* pMtl = pTani->pM; // // ++pTani->nF; // // INT nTot = pMtl->pvIfl->size(); // // pTani->nF %= nTot; // // vector<TaniIfl>::iterator it = pMtl->pvIfl->begin() + pTani->nF; // // pTani->pT = it->pT; // } // } // For Sort...(optional) MATA mtViwI; LPDIRECT3DDEVICE9 pDev = (LPDIRECT3DDEVICE9)LcDev_GetD3Device(); pDev->GetTransform( D3DTS_VIEW, &mtViwI ); D3DXMatrixInverse(&mtViwI, NULL, &mtViwI); VEC3 vcCam = VEC3(mtViwI._41, mtViwI._42, mtViwI._43); VEC3 vcZ = VEC3(mtViwI._31, mtViwI._32, mtViwI._33); VEC3 vcTmp = m_vcTrn - vcCam; m_fStlSrtR = D3DXVec3Dot(&vcZ, &vcTmp); return hr; }
void CLcXSkinIns::Render() { HRESULT hr=-1; LPDIRECT3DDEVICE9 pDev = (LPDIRECT3DDEVICE9)LcDev_GetD3Device(); CLcXSkinSrc* pOrg = (CLcXSkinSrc*)m_pOrg; ID3DXEffect* pEft = pOrg->GetEffect(); // Setup the projection matrix D3DXMATRIX matProj; pDev->GetTransform(D3DTS_PROJECTION, &matProj); D3DLIGHT9 light; D3DXVECTOR3 vecLightDirUnnormalized(0.0f, -1.0f, 1.0f); ZeroMemory( &light, sizeof(D3DLIGHT9) ); light.Type = D3DLIGHT_DIRECTIONAL; light.Diffuse.r = 1.0f; light.Diffuse.g = 1.0f; light.Diffuse.b = 1.0f; D3DXVec3Normalize( (D3DXVECTOR3*)&light.Direction, &vecLightDirUnnormalized ); light.Position.x = 0.0f; light.Position.y = -1.0f; light.Position.z = 1.0f; light.Range = 1000.0f; pDev->SetLight(0, &light ); pDev->LightEnable(0, TRUE ); // Set Light for vertex shader D3DXVECTOR4 vLightDir( 0.0f, 1.0f, -1.0f, 0.0f ); D3DXVec4Normalize( &vLightDir, &vLightDir ); // for HLSL { pEft->SetMatrix( "mViewProj", &matProj); pEft->SetVector( "lhtDir", &vLightDir); } // for shader { // set the projection matrix for the vertex shader based skinning method D3DXMatrixTranspose(&matProj, &matProj); pDev->SetVertexShaderConstantF(2, (float*)&matProj, 4); pDev->SetVertexShaderConstantF(1, (float*)&vLightDir, 1); } if(m_pAC) m_pAC->AdvanceTime(m_fElapse, NULL); pOrg->UpdateFrameMatrices(m_pFrameOrg, &m_mtWld); pOrg->DrawFrame(m_pFrameOrg); static D3DXMATRIX mtI(1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1); pDev->SetTransform(D3DTS_WORLD, &mtI); }
////////////////////////////////////////////////////////////////////////// // render ////////////////////////////////////////////////////////////////////////// void RCharCloth::render() { // bHarewareBuffer = true; int i; LPDIRECT3DDEVICE9 dev = RGetDevice(); // Get Device Pointer UpdateNormal(); RMtrlMgr* pMtrlMgr = &mpMeshNode->m_pParentMesh->m_mtrl_list_ex; RMtrl* pMtrl = pMtrlMgr->Get_s(mpMeshNode->m_mtrl_id,-1); int num_mtrl = pMtrl->m_sub_mtrl_num; int point_index; // 현재 버텍스의 인덱스 for( i = 0 ; i < mpMeshNode->m_face_num ; ++i ) { for( int j = 0 ; j < 3; ++j ) { point_index = mpMeshNode->m_face_list[i].m_point_index[j]; gVertices[3*i+j].p = m_pX[point_index]; gVertices[3*i+j].tu = mpMeshNode->m_face_list[i].m_point_tex[j].x; gVertices[3*i+j].tv = mpMeshNode->m_face_list[i].m_point_tex[j].y; gVertices[3*i+j].n = m_pNormal[point_index]; } } if( bHarewareBuffer && gpClothVertexBuffer) { //// Copy Begin void *Buffer; if( FAILED( gpClothVertexBuffer->Lock( 0, sizeof(RVertex) * mpMeshNode->m_face_num * 3, (VOID**)&Buffer, D3DLOCK_DISCARD ))) { bHarewareBuffer = false; REL( gpClothVertexBuffer ); mlog("Fail to lock of Vertex Buffer\n"); goto e2SoftRender; } //memcpy( Buffer, gVertices, sizeof(RVertex) * m_nCntP ); memcpy( Buffer, gVertices, sizeof(RVertex) * mpMeshNode->m_face_num * 3 ); gpClothVertexBuffer->Unlock(); // Copy End } e2SoftRender: prerender(); if(mpMesh->m_pVisualMesh) mpMesh->m_pVisualMesh->UpdateLight(); rmatrix rtemp; dev->GetTransform( D3DTS_WORLD, &rtemp ); dev->SetTransform( D3DTS_WORLD , &( mLocalMat * mWorldMat ) ); mpMesh->SetCharacterMtrl_ON( pMtrl,mpMeshNode,1 ,mpMeshNode->GetTColor()); #ifdef USE_TOON_RENDER mpMeshNode->ToonRenderSettingOn(pMtrl); #endif if( bHarewareBuffer ) { dev->DrawPrimitive( D3DPT_TRIANGLELIST, 0, mpMeshNode->m_face_num ); } else { dev->DrawPrimitiveUP( D3DPT_TRIANGLELIST, mpMeshNode->m_face_num, gVertices, sizeof(RVertex)); } #ifdef USE_TOON_RENDER // if(Silhouette) { mpMeshNode->ToonRenderSilhouetteSettingOn(); if( bHarewareBuffer ) dev->DrawPrimitive( D3DPT_TRIANGLELIST, 0, mpMeshNode->m_face_num ); else dev->DrawPrimitiveUP( D3DPT_TRIANGLELIST, mpMeshNode->m_face_num, gVertices, sizeof(RVertex)); mpMeshNode->ToonRenderSilhouetteSettingOff(); } mpMeshNode->ToonRenderSettingOff(); #endif mpMesh->SetCharacterMtrl_OFF( pMtrl, 1 ); dev->SetTransform( D3DTS_WORLD, &rtemp ); postrender(); }
//------------------------------------- // Draw() //------------------------------------- void Shadow::Draw() { // DWORD zfunc; LPDIRECT3DDEVICE9 device = DirectX9Holder::device_; D3DXMATRIX world, scaling; D3DXMatrixIdentity(&world); D3DXMatrixScaling(&scaling, parameter_.scaling_.x_, 1.0f, parameter_.scaling_.z_); world *= scaling; world._41 += parameter_.position_.x_; world._42 += parameter_.position_.y_; world._43 += parameter_.position_.z_; D3DXMATRIX view, projection, wvp; device->GetTransform(D3DTS_VIEW, &view); device->GetTransform(D3DTS_PROJECTION, &projection); wvp = world * view * projection; shader_->vertex_table()->SetMatrix( device, "matrix_wvp", &wvp); device->SetTexture( shader_->pixel_table()->GetSamplerIndex("texture_0"), texture_); device->SetVertexShader(shader_->vertex_shader()); device->SetPixelShader(shader_->pixel_shader()); device->SetVertexDeclaration( DirectX9Holder::vertex_declaration_shadow_); device->SetStreamSource( 0, vertex_buffer_, 0, sizeof(Vertex3DShadow)); // レンダーステート保存 DWORD blendop, srcblend, destblend;//, zfunc; device->GetRenderState(D3DRS_BLENDOP, &blendop); device->GetRenderState(D3DRS_SRCBLEND, &srcblend); device->GetRenderState(D3DRS_DESTBLEND, &destblend); // device->GetRenderState(D3DRS_ZFUNC, &zfunc); //影用にレンダーステートを減算合成に変更する device->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_REVSUBTRACT); device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE); //Zバッファの設定を完全描画にする。 // device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS); // 描画 device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2); // 変更したレンダースレート修正 // device->SetRenderState(D3DRS_ZFUNC, zfunc); device->SetRenderState(D3DRS_BLENDOP, blendop); device->SetRenderState(D3DRS_SRCBLEND, srcblend); device->SetRenderState(D3DRS_DESTBLEND, destblend); device->SetVertexShader(NULL); device->SetPixelShader(NULL); }
HRESULT KLightning::Render() { HRESULT hr = 0; // Backup the previous render states DWORD dwPrevLightEnable = 0; g_pd3dDevice->GetRenderState(D3DRS_LIGHTING, &dwPrevLightEnable); g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE); DWORD dwPrevCullMode = 0; g_pd3dDevice->GetRenderState(D3DRS_CULLMODE, &dwPrevCullMode); g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); DWORD dwPrevFillMode = 0; g_pd3dDevice->GetRenderState(D3DRS_FILLMODE, &dwPrevFillMode); g_pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); DWORD dwPrevTexFactor = 0; g_pd3dDevice->GetRenderState(D3DRS_TEXTUREFACTOR, &dwPrevTexFactor); g_pd3dDevice->SetRenderState(D3DRS_TEXTUREFACTOR, (0x00000000 | m_Alpha) << 24); DWORD dwPrevAlphaBlendEnable = 0; g_pd3dDevice->GetRenderState(D3DRS_ALPHABLENDENABLE, &dwPrevAlphaBlendEnable); g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, false); DWORD dwPrevBlendOP = 0; g_pd3dDevice->GetRenderState(D3DRS_BLENDOP, &dwPrevBlendOP); g_pd3dDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD); DWORD dwPrevSrcBlend = 0; g_pd3dDevice->GetRenderState(D3DRS_SRCBLEND, &dwPrevSrcBlend); g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE); DWORD dwPrevDestBlend = 0; g_pd3dDevice->GetRenderState(D3DRS_DESTBLEND, &dwPrevDestBlend); g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE); D3DXMATRIX mtxPrevWorld, mtxMine; D3DXMatrixIdentity(&mtxMine); g_pd3dDevice->GetTransform(D3DTS_WORLD, &mtxPrevWorld); g_pd3dDevice->SetTransform(D3DTS_WORLD, &mtxMine); // Draw g_pd3dDevice->SetStreamSource(0, m_pVB, 0, sizeof(KLightningVertex)); g_pd3dDevice->SetFVF(LIGHTNING_VERTEX_FVF); g_pd3dDevice->SetTexture(0, m_pTexture); g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_TFACTOR); g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE); unsigned int NumOfPTs = m_CurrentNumOfNodes * 2; if(FAILED(hr = g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, NumOfPTs))) { return hr; } // Recover all the render states g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, dwPrevLightEnable); g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, dwPrevCullMode); g_pd3dDevice->SetRenderState(D3DRS_FILLMODE, dwPrevFillMode); g_pd3dDevice->SetRenderState(D3DRS_TEXTUREFACTOR, dwPrevTexFactor); g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, dwPrevAlphaBlendEnable); g_pd3dDevice->SetRenderState(D3DRS_BLENDOP, dwPrevBlendOP); g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, dwPrevSrcBlend); g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, dwPrevDestBlend); g_pd3dDevice->SetTransform(D3DTS_WORLD, &mtxPrevWorld); return S_OK; }
/// Rendering the particles involves looping through every live particle /// and writing it's relevant data to a vertex buffer, and then rendering /// that vertex buffer. void ParticleEffect::render() { if(m_nLiveParticleCount == 0) // make sure we have something to render return; if(m_sort) sort(); // save render states before starting DWORD lighting; DWORD alphablend; DWORD zwrite; DWORD zenable; DWORD srcblend; DWORD destblend; pD3DDevice->GetRenderState(D3DRS_LIGHTING, &lighting); pD3DDevice->GetRenderState(D3DRS_ALPHABLENDENABLE, &alphablend); pD3DDevice->GetRenderState(D3DRS_ZWRITEENABLE, &zwrite); pD3DDevice->GetRenderState(D3DRS_ZENABLE, &zenable); pD3DDevice->GetRenderState(D3DRS_SRCBLEND, &srcblend); pD3DDevice->GetRenderState(D3DRS_DESTBLEND, &destblend); // set up particle engine states pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE); pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); if(m_sort) pD3DDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE); else pD3DDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE); pD3DDevice->SetRenderState(D3DRS_ZENABLE, TRUE); pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); //pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); //pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ZERO); // set texture operations for alpha blending by setting the color // to texture color times diffuse color, and alpha taken entirely from // texture value pD3DDevice->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_MODULATE); pD3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1,D3DTA_TEXTURE); pD3DDevice->SetTextureStageState(0,D3DTSS_COLORARG2,D3DTA_DIFFUSE); pD3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTOP_MODULATE); pD3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG1,D3DTA_TEXTURE); pD3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG2,D3DTA_DIFFUSE); // get camera right and up vectors to figure out how to orient the sprites D3DXMATRIXA16 view; pD3DDevice->GetTransform(D3DTS_VIEW, &view); Vector3 vecRight = Vector3(view._11, view._21, view._31); Vector3 vecUp = Vector3(view._12, view._22, view._32); Vector3 vecForward = Vector3(view._13, view._23, view._33); // precalculate corners Vector3 ul, ur, bl, br; //ul = -vecRight + vecUp; // upper left //ur = vecRight + vecUp; // upper right //bl = -vecRight - vecUp; // bottom left //br = vecRight - vecUp; // bottom right pD3DDevice->SetTexture(0, m_txtParticleTexture); if(!m_vertBuffer->lock()) { return; } // shorthand to the current vertex RenderVertexL *vert = &((*m_vertBuffer)[0]); // although these values are the same for all particles (except color.alpha), // you could implement some randomness, at which point there would be a // reason to assign to them with every iteration of the loop unsigned int color; float size = m_fPISize/2.0f; // half of m_fPISize in each direction float tTop = 0; float tBottom = 1.0f; float tLeft = 0; float tRight = 1.0f; // loop through all live particles to assign proper values to the vertices for (int i=0; i<m_nLiveParticleCount; i++) { Vector3 pos = m_Particles[m_drawOrder[i]].position; color = m_Particles[m_drawOrder[i]].color; Vector3 myUp, myRight; Quaternion q; q.setToRotateAboutAxis(vecForward, m_Particles[m_drawOrder[i]].rotation); RotationMatrix r; r.fromObjectToInertialQuaternion(q); myUp = r.objectToInertial(vecUp); myRight = r.objectToInertial(vecRight); ul = -myRight + myUp; // upper left ur = myRight + myUp; // upper right bl = -myRight - myUp; // bottom left br = myRight - myUp; // bottom right vert->p = pos + ul*size; vert->argb = color; vert->u = tLeft; vert->v = tTop; vert++; vert->p = pos + ur*size; vert->argb = color; vert->u = tRight; vert->v = tTop; vert++; vert->p = pos + bl*size; vert->argb = color; vert->u = tLeft; vert->v = tBottom; vert++; vert->p = pos + br*size; vert->argb = color; vert->u = tRight; vert->v = tBottom; vert++; } m_vertBuffer->unlock(); gRenderer.render( m_vertBuffer, m_nLiveParticleCount * 4, m_indexBuffer, m_nLiveParticleCount * 2); // restore render states pD3DDevice->SetRenderState(D3DRS_LIGHTING, lighting); pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, alphablend); pD3DDevice->SetRenderState(D3DRS_ZWRITEENABLE, zwrite); pD3DDevice->SetRenderState(D3DRS_ZENABLE, zenable); pD3DDevice->SetRenderState(D3DRS_SRCBLEND, srcblend); pD3DDevice->SetRenderState(D3DRS_DESTBLEND, destblend); }
VOID Render() { if ( NULL == g_pd3dDevice ) { return; } g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB( 30, 10, 10 ), 1.0f, 0 ); if ( SUCCEEDED( g_pd3dDevice->BeginScene() ) ) { SetupLights(); SetupMatrices(); if ( g_Tick ) { g_pd3dDevice->SetTexture( 0, g_pTexture1 ); } else { g_pd3dDevice->SetTexture( 0, g_pTexture2 ); } g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE ); g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE ); g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE ); g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_DISABLE ); #ifdef SHOW_HOW_TO_USE_TCI D3DXMATRIXA16 mTextureTransform; D3DXMATRIXA16 mProj; D3DXMATRIXA16 mTrans; D3DXMATRIXA16 mScale; g_pd3dDevice->GetTransform( D3DTS_PROJECTION , &mProj ); D3DXMatrixTranslation( &mTrans , 0.5f , 0.5f , 0.0f ); D3DXMatrixScaling( &mScale , 0.5f , -0.5f , 1.0f ); mTextureTransform = mProj * mScale * mTrans; g_pd3dDevice->SetTransform( D3DTS_TEXTURE0 , &mTextureTransform ); g_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT4 | D3DTTFF_PROJECTED ); g_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEPOSITION ); #endif g_pd3dDevice->SetStreamSource( 0, g_pVB1, 0, sizeof( CUSTOMVERTEX ) ); g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX ); g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 * 50 - 2 ); if ( g_Tick ) { g_pd3dDevice->SetTexture( 0, g_pTexture2 ); } else { g_pd3dDevice->SetTexture( 0, g_pTexture1 ); } g_pd3dDevice->SetStreamSource( 0, g_pVB2, 0, sizeof( CUSTOMVERTEX ) ); g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX ); g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 * 50 - 2 ); float scale = sinf( static_cast<float>( D3DX_PI * timeGetTime() / 1000 ) ) * 0.8f; D3DXMATRIXA16 thisMatrix, prevMatrix; g_pd3dDevice->GetTransform( D3DTS_WORLD, &prevMatrix ); D3DXMATRIXA16 matFirstTiger, matTrans, matRotate; D3DXMatrixRotationY( &matRotate, timeGetTime() / 1000.0f ); D3DXMatrixTranslation( &matTrans, -3.f, 2.0f, 5.0f ); D3DXMatrixMultiply( &matFirstTiger, &matRotate, &matTrans ); g_pd3dDevice->SetTransform( D3DTS_WORLD, &matFirstTiger ); for ( DWORD i = 0; i < g_dwNumMaterials; ++i ) { g_pd3dDevice->SetMaterial( &g_pMeshMaterials[i] ); g_pd3dDevice->SetTexture( 0, g_pMeshTextures[i] ); g_pMesh->DrawSubset( i ); } g_pd3dDevice->SetTransform( D3DTS_WORLD, &prevMatrix ); D3DXMatrixTranslation( &thisMatrix, 3.0f, 2.0f, 5.0f ); g_pd3dDevice->MultiplyTransform( D3DTS_WORLD, &thisMatrix ); D3DXMatrixScaling( &thisMatrix, -scale + 1.0f, 1.0f, 1.0f ); g_pd3dDevice->MultiplyTransform( D3DTS_WORLD, &thisMatrix ); for ( DWORD i = 0; i < g_dwNumMaterials; ++i ) { g_pd3dDevice->SetMaterial( &g_pMeshMaterials[i] ); g_pd3dDevice->SetTexture( 0, g_pMeshTextures[i] ); g_pMesh->DrawSubset( i ); } g_pd3dDevice->EndScene(); } g_pd3dDevice->Present( NULL, NULL, NULL, NULL ); }
//----------------------------------------------------------------------------- // Name: Render() // Desc: Draws the scene //----------------------------------------------------------------------------- VOID Render() { // Clear the backbuffer and the zbuffer g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB( 0, 0, 255 ), 1.0f, 0 ); // Begin the scene if( SUCCEEDED( g_pd3dDevice->BeginScene() ) ) { SetupCameraMatrices(); SetupLights((timeGetTime() / 1000) % 2); SetupCylinderMatrices(); int TextureIndex = (timeGetTime() / 1000) % 2; g_pd3dDevice->SetTexture(0, g_pTexture[TextureIndex]); g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE); g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE); g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE); #ifdef SHOW_HOW_TO_USE_TCI D3DXMATRIXA16 mTextureTransform; D3DXMATRIXA16 mProj; D3DXMATRIXA16 mTrans; D3DXMATRIXA16 mScale; g_pd3dDevice->GetTransform(D3DTS_PROJECTION, &mProj); D3DXMatrixTranslation(&mTrans, 0.5f, 0.5f, 0.0f); D3DXMatrixScaling(&mScale, 0.5f, -0.5f, 1.0f); mTextureTransform = mProj * mScale * mTrans; g_pd3dDevice->SetTransform(D3DTS_TEXTURE0, &mTextureTransform); g_pd3dDevice->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT4 | D3DTTFF_PROJECTED); g_pd3dDevice->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEPOSITION); #endif g_pd3dDevice->SetStreamSource(0, g_pVB, 0, sizeof(CUSTOMVERTEX)); g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX); g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2 * 50 - 2); // Meshes are divided into subsets, one for each material. Render them in // a loop for (int loop = 0; loop < MESH_COUNT; ++loop) { // Setup the world, view, and projection matrices SetupMeshesMatrices(loop); for (DWORD i = 0; i < g_dwNumMaterials[loop]; i++) { // Set the material and texture for this subset g_pd3dDevice->SetMaterial(&g_pMeshMaterials[loop][i]); g_pd3dDevice->SetTexture(0, g_pMeshTextures[loop][i]); // Draw the mesh subset g_pMesh[loop]->DrawSubset(0); } } // End the scene g_pd3dDevice->EndScene(); } // Present the backbuffer contents to the display g_pd3dDevice->Present( NULL, NULL, NULL, NULL ); }
void COverlayImageDX::Render(SRenderState &state) { D3DXMATRIX orig; LPDIRECT3DDEVICE9 device = g_Windowing.Get3DDevice(); device->GetTransform(D3DTS_WORLD, &orig); D3DXMATRIX world = orig; D3DXMATRIX trans, scale; if(m_pos == POSITION_RELATIVE) D3DXMatrixTranslation(&trans, state.x - state.width * 0.5f - 0.5f , state.y - state.height * 0.5f - 0.5f , 0.0f); else D3DXMatrixTranslation(&trans, state.x - 0.5f , state.y - 0.5f , 0.0f); D3DXMatrixScaling (&scale, state.width , state.height , 1.0f); D3DXMatrixMultiply(&world, &world, &scale); D3DXMatrixMultiply(&world, &world, &trans); device->SetTransform(D3DTS_WORLD, &world); TransformMatrix* matModelView = g_Windowing.GetHardwareTransform(MATRIX_TYPE_MODEL_VIEW); TransformMatrix* matProjection = g_Windowing.GetHardwareTransform(MATRIX_TYPE_PROJECTION); device->SetTransform(D3DTS_VIEW, &D3DXMATRIX((float *)matModelView->m)); device->SetTransform(D3DTS_PROJECTION, &D3DXMATRIX((float *)matProjection->m)); device->SetTexture( 0, m_texture.Get() ); device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); device->SetSamplerState(0, D3DSAMP_ADDRESSU , D3DTADDRESS_CLAMP); device->SetSamplerState(0, D3DSAMP_ADDRESSV , D3DTADDRESS_CLAMP); device->SetTextureStageState(0, D3DTSS_COLOROP , D3DTOP_SELECTARG1 ); device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE ); device->SetTextureStageState(0, D3DTSS_ALPHAOP , D3DTOP_SELECTARG1 ); device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE ); device->SetRenderState( D3DRS_LIGHTING , FALSE ); device->SetRenderState( D3DRS_ZENABLE , FALSE ); device->SetRenderState( D3DRS_FOGENABLE, FALSE ); device->SetRenderState( D3DRS_ALPHATESTENABLE, TRUE ); device->SetRenderState( D3DRS_ALPHAREF , 0 ); device->SetRenderState( D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL ); device->SetRenderState( D3DRS_FILLMODE , D3DFILL_SOLID ); device->SetRenderState( D3DRS_CULLMODE , D3DCULL_NONE ); device->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE ); #if USE_PREMULTIPLIED_ALPHA device->SetRenderState( D3DRS_SRCBLEND , D3DBLEND_ONE ); #else device->SetRenderState( D3DRS_SRCBLEND , D3DBLEND_SRCALPHA ); #endif device->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA ); device->SetFVF(m_fvf); device->SetStreamSource(0, m_vertex.Get(), 0, sizeof(VERTEX)); device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2); device->SetTransform(D3DTS_WORLD, &orig); }