⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zfxd3d_vcache.cpp

📁 This is a book introduce some tech about Game Engine 3D
💻 CPP
📖 第 1 页 / 共 4 页
字号:
      // index buffer used?
      if (m_pSB[nID].bIndis) m_pDevice->SetIndices(m_pSB[nID].pIB);
   
      m_pDevice->SetStreamSource(0, m_pSB[nID].pVB, 
                                 0, m_pSB[nID].nStride);
      m_dwActiveSB = nID;
      }
   // there is maybe just another indexbuffer active
   else if (m_dwActiveIB != MAX_ID) {
      if (m_pSB[nID].bIndis) m_pDevice->SetIndices(m_pSB[nID].pIB);
      m_dwActiveIB = MAX_ID;
      }


   // is the device already using this skin?   
   if (m_pZFXD3D->GetActiveSkinID() != m_pSB[nID].nSkinID) {
      // set material for device
      ZFXSKIN *pSkin = &m_pSkinMan->m_pSkins[m_pSB[nID].nSkinID];
     
      
      if (sm == RS_SHADE_SOLID) {
         // set material for device
         ZFXMATERIAL *pMat = &m_pSkinMan->m_pMaterials[pSkin->nMaterial];

         if (!m_pZFXD3D->UsesShaders()) {
            D3DMATERIAL9 mat = { 
               pMat->cDiffuse.fR,  pMat->cDiffuse.fG,  pMat->cDiffuse.fB,  pMat->cDiffuse.fA,
               pMat->cAmbient.fR,  pMat->cAmbient.fG,  pMat->cAmbient.fB,  pMat->cAmbient.fA,
               pMat->cSpecular.fR, pMat->cSpecular.fG, pMat->cSpecular.fB, pMat->cSpecular.fA,
               pMat->cEmissive.fR, pMat->cEmissive.fG, pMat->cEmissive.fB, pMat->cEmissive.fA,
               pMat->fPower };

            m_pDevice->SetMaterial(&mat);
            }
         else {
            GetZFXD3D()->SetShaderConstant(SHT_PIXEL, DAT_FLOAT, 1, 1, &pMat->cAmbient);
            GetZFXD3D()->SetShaderConstant(SHT_PIXEL, DAT_FLOAT, 2, 1, &pMat->cDiffuse);
            GetZFXD3D()->SetShaderConstant(SHT_PIXEL, DAT_FLOAT, 3, 1, &pMat->cEmissive);
            GetZFXD3D()->SetShaderConstant(SHT_PIXEL, DAT_FLOAT, 4, 1, &pMat->cSpecular);
            }

         // forced rendering without textures?
         if ( m_pZFXD3D->UsesTextures() ) {
            // set texture for device
            for (iT=0; iT<8; iT++) {
               if (pSkin->nTexture[iT] != MAX_ID) {
                  m_pDevice->SetTexture(iT, (LPDIRECT3DTEXTURE9)
                                       m_pSkinMan->m_pTextures[
                                       pSkin->nTexture[iT]].pData);

                  m_pDevice->SetTextureStageState(iT, D3DTSS_TEXCOORDINDEX, 0);
                  m_pDevice->SetTextureStageState(iT, D3DTSS_COLORARG1, D3DTA_TEXTURE);
                  m_pDevice->SetTextureStageState(iT, D3DTSS_COLORARG2, D3DTA_CURRENT);
                  m_pDevice->SetTextureStageState(iT, D3DTSS_COLOROP, m_pZFXD3D->GetTOP(iT));
                  }
               else break;
               } 
            // deactivate unused stages at all costs
            m_pDevice->SetTextureStageState(iT, D3DTSS_COLOROP, D3DTOP_DISABLE);
            }
         else {
            m_pDevice->SetTexture(0, NULL);
            m_pDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
            }
         }
      else {
         ZFXCOLOR clrWire = m_pZFXD3D->GetWireColor();

         // set material
         D3DMATERIAL9 matW = { 
            clrWire.fR, clrWire.fG, clrWire.fB, clrWire.fA,
            clrWire.fR, clrWire.fG, clrWire.fB, clrWire.fA,
            0.0f,             0.0f,       0.0f,       1.0f,
            0.0f,             0.0f,       0.0f,       1.0f,
            1.0f };
         m_pDevice->SetMaterial(&matW);

         // set no texture for device
         m_pDevice->SetTexture(0, NULL);
         m_pDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
         }
      
      // set alpha states for device
      if (pSkin->bAlpha) {
         m_pDevice->SetRenderState(D3DRS_ALPHAREF, 50);
         m_pDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL);
         m_pDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
         m_pDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
         m_pDevice->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);
         m_pDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
         }
      else {
         m_pDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
         m_pDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
         }
      // skin will change now
      m_pZFXD3D->SetActiveSkinID(m_pSB[nID].nSkinID);
      } // [device->skin]

   // if using shaders they should be activated by the
   // ActivateVShader methods. Else set FVF
   if (!m_pZFXD3D->UsesShaders())
      m_pDevice->SetFVF(m_pSB[nID].dwFVF);

   //  should we use additive rendering?
   if (m_pZFXD3D->UsesAdditiveBlending()) {
      m_pDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
      m_pDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
      m_pDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
      }

   //  should we use rendering to color buffer at all?
   if (!m_pZFXD3D->UsesColorBuffer()) {
      m_pDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
      m_pDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
      m_pDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
      }


   // indexed primitive
   if (m_pSB[nID].bIndis) {
      // render content
      if ( sm == RS_SHADE_POINTS ) {
         hr = m_pDevice->DrawPrimitive(
                           D3DPT_POINTLIST, 0,
                           m_pSB[nID].nNumVerts);
         }
      else if ( sm == RS_SHADE_LINES ) {
         hr = m_pDevice->DrawIndexedPrimitive(
                        D3DPT_LINELIST,
                        0, 0, m_pSB[nID].nNumVerts,
                        0, m_pSB[nID].nNumIndis/2);
         }
      else if ( sm == RS_SHADE_HULLWIRE ) {
         hr = m_pDevice->DrawIndexedPrimitive(
                           D3DPT_LINESTRIP, 0,
                           0, m_pSB[nID].nNumVerts,
                           0, m_pSB[nID].nNumVerts);
         }
      else { // RS_SHADE_SOLID || RS_SHADE_TRIWIRE
         hr = m_pDevice->DrawIndexedPrimitive(
                           D3DPT_TRIANGLELIST, 0,
                           0, m_pSB[nID].nNumVerts,
                           0, m_pSB[nID].nNumTris);
         }
      }
   // non-indexed primitive
   else {
      // render content
      if ( sm == RS_SHADE_POINTS ) {
         hr = m_pDevice->DrawPrimitive(
                           D3DPT_POINTLIST, 0,
                           m_pSB[nID].nNumVerts);
         }
      else if ( sm == RS_SHADE_LINES ) {
         hr = m_pDevice->DrawPrimitive(
                           D3DPT_LINELIST, 0,
                           m_pSB[nID].nNumVerts/2);
         }
      else if ( sm == RS_SHADE_HULLWIRE ) {
         hr = m_pDevice->DrawPrimitive(
                           D3DPT_LINESTRIP, 0,
                           m_pSB[nID].nNumVerts);
         }
      else { // RS_SHADE_SOLID || RS_SHADE_TRIWIRE
         hr = m_pDevice->DrawPrimitive(
                           D3DPT_TRIANGLELIST, 0,
                           m_pSB[nID].nNumTris);
         }
      }

   return hr;
   } // Render
/*----------------------------------------------------------------*/


/**
 * Render static buffers with the given skin. Indexbuffer is optional.
 * -> IN: UINT    - index of static vertexbuffer to use
 */
HRESULT ZFXD3DVCManager::Render(UINT nSBID, UINT nIBID, UINT nSkin) {
   HRESULT hr=ZFX_OK;
   int iT=0;

   ZFXRENDERSTATE sm = m_pZFXD3D->GetShadeMode();

   // active cache gets invalid
   m_dwActiveCache = MAX_ID;

   if ((nSBID >= m_nNumSB) || (nIBID >= m_nNumIB)) {
      Log("error: invalid buffer ID");
      return ZFX_INVALIDPARAM;
      }

   // activate buffers if not already active
   if (m_dwActiveSB != nSBID) {
      m_pDevice->SetStreamSource(0, m_pSB[nSBID].pVB, 
                                 0, m_pSB[nSBID].nStride);
      m_dwActiveSB = nSBID;
      }
   if (m_dwActiveIB != nIBID) {
      m_pDevice->SetIndices(m_pIB[nIBID].pIB);
      m_dwActiveIB = nIBID;
      }


   // is the device already using this skin?   
   if (m_pZFXD3D->GetActiveSkinID() != nSkin) {
      // set material for device
      ZFXSKIN *pSkin = &m_pSkinMan->m_pSkins[nSkin];
     
      
      if (sm == RS_SHADE_SOLID) {
         // set material for device
         ZFXMATERIAL *pMat = &m_pSkinMan->m_pMaterials[pSkin->nMaterial];

         if (!m_pZFXD3D->UsesShaders()) {
            D3DMATERIAL9 mat = { 
               pMat->cDiffuse.fR,  pMat->cDiffuse.fG,  pMat->cDiffuse.fB,  pMat->cDiffuse.fA,
               pMat->cAmbient.fR,  pMat->cAmbient.fG,  pMat->cAmbient.fB,  pMat->cAmbient.fA,
               pMat->cSpecular.fR, pMat->cSpecular.fG, pMat->cSpecular.fB, pMat->cSpecular.fA,
               pMat->cEmissive.fR, pMat->cEmissive.fG, pMat->cEmissive.fB, pMat->cEmissive.fA,
               pMat->fPower };

            m_pDevice->SetMaterial(&mat);
            }
         else {
            GetZFXD3D()->SetShaderConstant(SHT_PIXEL, DAT_FLOAT, 1, 1, &pMat->cAmbient);
            GetZFXD3D()->SetShaderConstant(SHT_PIXEL, DAT_FLOAT, 2, 1, &pMat->cDiffuse);
            GetZFXD3D()->SetShaderConstant(SHT_PIXEL, DAT_FLOAT, 3, 1, &pMat->cEmissive);
            GetZFXD3D()->SetShaderConstant(SHT_PIXEL, DAT_FLOAT, 4, 1, &pMat->cSpecular);
            }

         // forced rendering without textures?
         if ( m_pZFXD3D->UsesTextures() ) {
            // set texture for device
            for (iT=0; iT<8; iT++) {
               if (pSkin->nTexture[iT] != MAX_ID) {
                  m_pDevice->SetTexture(iT, (LPDIRECT3DTEXTURE9)
                                       m_pSkinMan->m_pTextures[
                                       pSkin->nTexture[iT]].pData);

                  m_pDevice->SetTextureStageState(iT, D3DTSS_TEXCOORDINDEX, 0);
                  m_pDevice->SetTextureStageState(iT, D3DTSS_COLORARG1, D3DTA_TEXTURE);
                  m_pDevice->SetTextureStageState(iT, D3DTSS_COLORARG2, D3DTA_CURRENT);
                  m_pDevice->SetTextureStageState(iT, D3DTSS_COLOROP, m_pZFXD3D->GetTOP(iT));
                  }
               else break;
               } 
            // deactivate unused stages at all costs
            m_pDevice->SetTextureStageState(iT, D3DTSS_COLOROP, D3DTOP_DISABLE);
            }
         else {
            m_pDevice->SetTexture(0, NULL);
            m_pDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
            }
         }
      else {
         ZFXCOLOR clrWire = m_pZFXD3D->GetWireColor();
         // set material
         D3DMATERIAL9 matW = { 
            clrWire.fR, clrWire.fG, clrWire.fB, clrWire.fA,
            clrWire.fR, clrWire.fG, clrWire.fB, clrWire.fA,
            0.0f,             0.0f,       0.0f,       1.0f,
            0.0f,             0.0f,       0.0f,       1.0f,
            1.0f };
         m_pDevice->SetMaterial(&matW);

         // set no texture for device
         m_pDevice->SetTexture(0, NULL);
         m_pDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
         }
      
      // set alpha states for device
      if (pSkin->bAlpha) {
         m_pDevice->SetRenderState(D3DRS_ALPHAREF, 50);
         m_pDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL);
         m_pDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
         m_pDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
         m_pDevice->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);
         m_pDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
         }
      else {
         m_pDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
         m_pDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
         }
      // skin will change now
      m_pZFXD3D->SetActiveSkinID(nSkin);
      } // [device->skin]

   // if using shaders they should be activated by the
   // ActivateVShader methods. Else set FVF
   if (!m_pZFXD3D->UsesShaders())
      m_pDevice->SetFVF(m_pSB[nSBID].dwFVF);

   //  should we use additive rendering?
   if (m_pZFXD3D->UsesAdditiveBlending()) {
      m_pDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
      m_pDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
      m_pDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
      }

   //  should we use rendering to color buffer at all?
   if (!m_pZFXD3D->UsesColorBuffer()) {
      m_pDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
      m_pDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
      m_pDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
      }


   // render content
   if ( sm == RS_SHADE_POINTS ) {
      hr = m_pDevice->DrawPrimitive(
                        D3DPT_POINTLIST, 0,
                        m_pSB[nSBID].nNumVerts);
      }
   else if ( sm == RS_SHADE_LINES ) {
      hr = m_pDevice->DrawIndexedPrimitive(
                     D3DPT_LINELIST,
                     0, 0, m_pSB[nSBID].nNumVerts,
                     0, m_pIB[nIBID].nNumIndis/2);
      }
   else if ( sm == RS_SHADE_HULLWIRE ) {
      hr = m_pDevice->DrawIndexedPrimitive(
                        D3DPT_LINESTRIP, 0,
                        0, m_pSB[nSBID].nNumVerts,
                        0, m_pSB[nSBID].nNumVerts);
      }
   else { // RS_SHADE_SOLID || RS_SHADE_TRIWIRE
      hr = m_pDevice->DrawIndexedPrimitive(
                        D3DPT_TRIANGLELIST, 0,
                        0, m_pSB[nSBID].nNumVerts,
                        0, m_pIB[nIBID].nNumTris);
      }

   return hr;
   } // Render
/*----------------------------------------------------------------*/


/**
 * Render static buffers with the given skin. Indexbuffer is optional.
 * -> IN: UINT - index of static vertexbuffer to use
 *        UINT - skin to use while rendering the content
 *        UINT - min index used during this call
 *        UINT - max index used during this call
 *        UINT - number of primitives to render
 */
HRESULT ZFXD3DVCManager::Render(UINT nID, UINT SkinID, UINT StartIndex, 
                                UINT NumVerts, UINT NumTris) {
   HRESULT hr=ZFX_OK;
   int iT=0;

   ZFXRENDERSTATE sm = m_pZFXD3D->GetShadeMode();

   // active cache gets invalid
   m_dwActiveCache = MAX_ID;

   if (nID >= m_nNumSB) {
      Log("error: invalid static buffer ID");
      return ZFX_INVALIDPARAM;
      }

   // activate buffers if not already active
   if (m_dwActiveSB != nID) {
      // index buffer used?
      if (m_pSB[nID].bIndis) m_pDevice->SetIndices(m_pSB[nID].pIB);
   
      m_pDevice->SetStreamSource(0, m_pSB[nID].pVB, 
                                 0, m_pSB[nID].nStride);
      m_dwActiveSB = nID;
      }
   // there is maybe just another indexbuffer active
   else if (m_dwActiveIB != MAX_ID) {
      if (m_pSB[nID].bIndis) m_pDevice->SetIndices(m_pSB[nID].pIB);
      m_dwActiveIB = MAX_ID;
      }


   // is the device already using this skin?   
   if (m_pZFXD3D->GetActiveSkinID() != SkinID) {
      // set material for device
      ZFXSKIN *pSkin = &m_pSkinMan->m_pSkins[SkinID];
     
      
      if (sm == RS_SHADE_SOLID) {
         // set material for device
         ZFXMATERIAL *pMat = &m_pSkinMan->m_pMaterials[pSkin->nMaterial];

         if (!m_pZFXD3D->UsesShaders()) {
            D3DMATERIAL9 mat = { 
               pMat->cDiffuse.fR,  pMat->cDiffuse.fG,  pMat->cDiffuse.fB,  pMat->cDiffuse.fA,
               pMat->cAmbient.fR,  pMat->cAmbient.fG,  pMat->cAmbient.fB,  pMat->cAmbient.fA,
               pMat->cSpecular.fR, pMat->cSpecular.fG, pMat->cSpecular.fB, pMat->cSpecular.fA,
               pMat->cEmissive.fR, pMat->cEmissive.fG, pMat->cEmissive.fB, pMat->cEmissive.fA,
               pMat->fPower };

            m_pDevice->SetMaterial(&mat);
            }
         else {
            GetZFXD3D()->SetShaderConstant(SHT_PIXEL, DAT_FLOAT, 1, 1, &pMat->cAmbient);
            GetZFXD3D()->SetShaderConstant(SHT_PIXEL, DAT_FLOAT, 2, 1, &pMat->cDiffuse);
            GetZFXD3D()->SetShaderConstant(SHT_PIXEL, DAT_FLOAT, 3, 1, &pMat->cEmissive);
            GetZFXD3D()->SetShaderConstant(SHT_PIXEL, DAT_FLOAT, 4, 1, &pMat->cSpecular);
            }

         if ( m_pZFXD3D->UsesTextures() ) {
            // set texture for device
            for (iT=0; iT<8; iT++) {
               if (pSkin->nTexture[iT] != MAX_ID) {
                  m_pDevice->SetTexture(iT, (LPDIRECT3DTEXTURE9)
                                       m_pSkinMan->m_pTextures[
                                       pSkin->nTexture[iT]].pData);

                  m_pDevice->SetTextureStageState(iT, D3DTSS_TEXCOORDINDEX, 0);
                  m_pDevice->SetTextureStageState(iT, D3DTSS_COLORARG1, D3DTA_TEXTURE);
                  m_pDevice->SetTextureStageState(iT, D3DTSS_COLORARG2, D3DTA_CURRENT);
                  m_pDevice->SetTextureStageState(iT, D3DTSS_COLOROP, m_pZFXD3D->GetTOP(iT));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -