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

📄 fixedfuncshader.cpp

📁 activate your member account after checking your files. If
💻 CPP
📖 第 1 页 / 共 4 页
字号:
   sprintf(tmp, "Material color: ( %.2f   %.2f   %.2f )\n", psceneState->m_vMaterialColor.r,psceneState->m_vMaterialColor.g,psceneState->m_vMaterialColor.b);
   m_pFontSmall->DrawText(xText, yText, cols[iCurParam == PARAM_MATERIAL_COLOR], tmp);
   yText += ySpacing;

   sprintf(tmp, "Material power: %.2f\n", psceneState->m_fMaterialPower);
   m_pFontSmall->DrawText(xText, yText, cols[iCurParam == PARAM_MATERIAL_POWER], tmp);
   yText += ySpacing;

   sprintf(tmp, "Ambient color: ( %.2f   %.2f   %.2f )\n", psceneState->m_vAmbientColor.r,psceneState->m_vAmbientColor.g,psceneState->m_vAmbientColor.b);
   m_pFontSmall->DrawText(xText, yText, cols[iCurParam == PARAM_AMBIENT_COLOR], tmp);
   yText += ySpacing;
   
   sprintf(tmp, "Specular: %s\n", onoff[psceneState->m_bSpecular == true]);
   m_pFontSmall->DrawText(xText, yText, cols[iCurParam == PARAM_SPECULAR_ENABLE], tmp);
   yText += ySpacing;

   sprintf(tmp, "Tweening: %s (Factor: %.3f)\n", onoff[psceneState->m_bTweening == true], m_fTweenFactor);
   m_pFontSmall->DrawText(xText, yText, cols[iCurParam == PARAM_TWEENING_ENABLE], tmp);
   yText += ySpacing;

   switch(psceneState->m_vfog.iType)
   {
      case FOG_TYPE_NONE:
         m_pFontSmall->DrawText(xText, yText, cols[iCurParam == PARAM_FOG_TYPE], "Fog type: OFF");
         break;
      case FOG_TYPE_EXP:
         m_pFontSmall->DrawText(xText, yText, cols[iCurParam == PARAM_FOG_TYPE], "Fog type: EXP");
         break;
      case FOG_TYPE_EXP2:
         m_pFontSmall->DrawText(xText, yText, cols[iCurParam == PARAM_FOG_TYPE], "Fog type: EXP2");
         break;
      case FOG_TYPE_LINEAR:
         m_pFontSmall->DrawText(xText, yText, cols[m_iCurParam == PARAM_FOG_TYPE], "Fog type: LINEAR");
         break;
   }
   yText += ySpacing;

   sprintf(tmp, "  Color: ( %.2f   %.2f   %.2f )\n", psceneState->m_vfog.vColor.r,psceneState->m_vfog.vColor.g,psceneState->m_vfog.vColor.b);
   m_pFontSmall->DrawText(xText, yText, cols[iCurParam == PARAM_FOG_COLOR], tmp);
   yText += ySpacing;

   sprintf(tmp, "  Start: %.2f\n", psceneState->m_vfog.fStart);
   m_pFontSmall->DrawText(xText, yText, cols[iCurParam == PARAM_FOG_START], tmp);
   yText += ySpacing;

   sprintf(tmp, "  End: %.2f\n", psceneState->m_vfog.fEnd);
   m_pFontSmall->DrawText(xText, yText, cols[iCurParam == PARAM_FOG_END], tmp);
   yText += ySpacing;

   sprintf(tmp, "  Density: %.2f\n", psceneState->m_vfog.fDensity);
   m_pFontSmall->DrawText(xText, yText, cols[iCurParam == PARAM_FOG_DENSITY], tmp);
   yText += ySpacing;

   sprintf(tmp, "  Range: %s\n", onoff[psceneState->m_vfog.bRange == true]);
   m_pFontSmall->DrawText(xText, yText, cols[iCurParam == PARAM_FOG_RANGE], tmp);
   yText += ySpacing;

   sprintf(tmp, "Light #: %i\n", m_iCurParamLight);
   m_pFontSmall->DrawText(xText, yText, cols[iCurParam == PARAM_LIGHT_NUM], tmp);
   yText += ySpacing;

   switch(psceneState->m_iLightType[m_iCurParamLight])
   {
      case LIGHT_TYPE_NONE:
         m_pFontSmall->DrawText(xText, yText, cols[iCurParam == PARAM_LIGHT_TYPE], "  Type: OFF");
         break;
      case LIGHT_TYPE_DIRECTIONAL:
         m_pFontSmall->DrawText(xText, yText, cols[iCurParam == PARAM_LIGHT_TYPE], "  Type: Directional");
         break;
      case LIGHT_TYPE_SPOT:
         m_pFontSmall->DrawText(xText, yText, cols[iCurParam == PARAM_LIGHT_TYPE], "  Type: Spot");
         break;
      case LIGHT_TYPE_POINT:
         m_pFontSmall->DrawText(xText, yText, cols[iCurParam == PARAM_LIGHT_TYPE], "  Type: Point");
         break;
   }
   yText += ySpacing;

   sprintf(tmp, "  Position: (%c%.2f%c %c%.2f%c %c%.2f%c)\n", 
         iCurParam == PARAM_LIGHT_POS_X ? '[' : ' ', 
         psceneState->m_light[m_iCurParamLight].Position.x,
         iCurParam == PARAM_LIGHT_POS_X ? ']' : ' ', 
         iCurParam == PARAM_LIGHT_POS_Y ? '[' : ' ', 
         psceneState->m_light[m_iCurParamLight].Position.y,
         iCurParam == PARAM_LIGHT_POS_Y ? ']' : ' ', 
         iCurParam == PARAM_LIGHT_POS_Z ? '[' : ' ', 
         psceneState->m_light[m_iCurParamLight].Position.z,
         iCurParam == PARAM_LIGHT_POS_Z ? ']' : ' ');
   m_pFontSmall->DrawText(xText, yText, cols[iCurParam >= PARAM_LIGHT_POS_X && iCurParam <= PARAM_LIGHT_POS_Z], tmp);
   yText += ySpacing;

   sprintf(tmp, "  Direction: (%c%.2f%c %c%.2f%c %c%.2f%c)\n", 
         iCurParam == PARAM_LIGHT_DIR_X ? '[' : ' ', 
         psceneState->m_light[m_iCurParamLight].Direction.x,
         iCurParam == PARAM_LIGHT_DIR_X ? ']' : ' ', 
         iCurParam == PARAM_LIGHT_DIR_Y ? '[' : ' ', 
         psceneState->m_light[m_iCurParamLight].Direction.y,
         iCurParam == PARAM_LIGHT_DIR_Y ? ']' : ' ', 
         iCurParam == PARAM_LIGHT_DIR_Z ? '[' : ' ', 
         psceneState->m_light[m_iCurParamLight].Direction.z,
         iCurParam == PARAM_LIGHT_DIR_Z ? ']' : ' ');
   m_pFontSmall->DrawText(xText, yText, cols[iCurParam >= PARAM_LIGHT_DIR_X && iCurParam <= PARAM_LIGHT_DIR_Z], tmp);
   yText += ySpacing;
      
   sprintf(tmp, "  Ambient color: ( %.2f   %.2f   %.2f )\n", 
         psceneState->m_light[m_iCurParamLight].Ambient.r,
         psceneState->m_light[m_iCurParamLight].Ambient.g,
         psceneState->m_light[m_iCurParamLight].Ambient.b);
   m_pFontSmall->DrawText(xText, yText, cols[iCurParam == PARAM_LIGHT_AMBIENT], tmp);
   yText += ySpacing;

   sprintf(tmp, "  Diffuse color: ( %.2f   %.2f   %.2f )\n", 
         psceneState->m_light[m_iCurParamLight].Diffuse.r,
         psceneState->m_light[m_iCurParamLight].Diffuse.g,
         psceneState->m_light[m_iCurParamLight].Diffuse.b);
   m_pFontSmall->DrawText(xText, yText, cols[iCurParam == PARAM_LIGHT_DIFFUSE], tmp);
   yText += ySpacing;

   sprintf(tmp, "  Specular color: ( %.2f   %.2f   %.2f )\n", 
         psceneState->m_light[m_iCurParamLight].Specular.r,
         psceneState->m_light[m_iCurParamLight].Specular.g,
         psceneState->m_light[m_iCurParamLight].Specular.b);
   m_pFontSmall->DrawText(xText, yText, cols[iCurParam == PARAM_LIGHT_SPECULAR], tmp);
   yText += ySpacing;

   sprintf(tmp, "  Range: %.2f\n", psceneState->m_light[m_iCurParamLight].Range);
   m_pFontSmall->DrawText(xText, yText, cols[iCurParam == PARAM_LIGHT_RANGE], tmp);
   yText += ySpacing;

   sprintf(tmp, "  Falloff: %.2f\n", psceneState->m_light[m_iCurParamLight].Falloff);
   m_pFontSmall->DrawText(xText, yText, cols[iCurParam == PARAM_LIGHT_FALLOFF], tmp);
   yText += ySpacing;

   sprintf(tmp, "  Attenuation0: %.2f\n", psceneState->m_light[m_iCurParamLight].Attenuation0);
   m_pFontSmall->DrawText(xText, yText, cols[iCurParam == PARAM_LIGHT_ATTENUATION0], tmp);
   yText += ySpacing;

   sprintf(tmp, "  Attenuation1: %.2f\n", psceneState->m_light[m_iCurParamLight].Attenuation1);
   m_pFontSmall->DrawText(xText, yText, cols[iCurParam == PARAM_LIGHT_ATTENUATION1], tmp);
   yText += ySpacing;

   sprintf(tmp, "  Attenuation2: %.2f\n", psceneState->m_light[m_iCurParamLight].Attenuation2);
   m_pFontSmall->DrawText(xText, yText, cols[iCurParam == PARAM_LIGHT_ATTENUATION2], tmp);
   yText += ySpacing;

   sprintf(tmp, "  Theta: %.2f\n", psceneState->m_light[m_iCurParamLight].Theta);
   m_pFontSmall->DrawText(xText, yText, cols[iCurParam == PARAM_LIGHT_THETA], tmp);
   yText += ySpacing;

   sprintf(tmp, "  Phi: %.2f\n", psceneState->m_light[m_iCurParamLight].Phi);
   m_pFontSmall->DrawText(xText, yText, cols[iCurParam == PARAM_LIGHT_PHI], tmp);
   yText += ySpacing;

   switch(psceneState->m_tex.iTexType)
   {
      case TEX_TYPE_NONE:
         m_pFontSmall->DrawText(xText, yText, cols[iCurParam == PARAM_TEX_TYPE], "Tex. type: OFF");
         break;
      case TEX_TYPE_CUBEMAP:
         m_pFontSmall->DrawText(xText, yText, cols[iCurParam == PARAM_TEX_TYPE], "Tex. type: CUBEMAP");
         break;
   }
   yText += ySpacing;

   switch(psceneState->m_tex.iTexGenType)
   {
      case TEXGEN_TYPE_NONE:
         m_pFontSmall->DrawText(xText, yText, cols[iCurParam == PARAM_TEXGEN_TYPE], "Tex. gen. type: OFF");
         break;
      case TEXGEN_TYPE_CAMERASPACENORMAL:
         m_pFontSmall->DrawText(xText, yText, cols[iCurParam == PARAM_TEXGEN_TYPE], "Tex. gen. type: CAMERASPACENORMAL");
         break;
      case TEXGEN_TYPE_CAMERASPACEPOSITION:
         m_pFontSmall->DrawText(xText, yText, cols[iCurParam == PARAM_TEXGEN_TYPE], "Tex. gen. type: CAMERASPACEPOSITION");
         break;
      case TEXGEN_TYPE_CAMERASPACEREFLECTIONVECTOR:
         m_pFontSmall->DrawText(xText, yText, cols[iCurParam == PARAM_TEXGEN_TYPE], "Tex. gen. type: CAMERASPACEREFLECTIONVECTOR");
         break;
   }
   yText += ySpacing;

   if(m_Mode == FFS_MODE_DIFF && m_statFrameError.GetAvg() > 0.f)
   {
      yText += ySpacing;
      char errorstr[16];
      sprintf(errorstr, "Error: %f", m_statFrameError.GetAvg());
      m_pFontSmall->DrawText(xText, yText, D3DCOLOR_ARGB(255,255,255,255), errorstr);
      yText += ySpacing;
   }
}


//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Called once per frame, the call is the entry point for 3d
//       rendering. This function sets up render states, clears the
//       viewport, and renders the scene.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::Render()
{

   //sets diff viewports
   D3DVIEWPORT9 vp  = {0, 0, m_iWidth, m_iHeight, 0.0f, 1.0f};
   D3DVIEWPORT9 vp1 = {0, 0, m_iWidth/2, m_iHeight, 0.0f, 1.0f};
   D3DVIEWPORT9 vp2 = {m_iWidth/2, 0, m_iWidth/2, m_iHeight, 0.0f, 1.0f};

   // Begin the scene
   if(SUCCEEDED(m_pd3dDevice->BeginScene()))
   {
      //uses render to texture for diff mode
      if(m_Mode == FFS_MODE_DIFF)
      {
         m_pd3dDevice->SetRenderTarget(0, m_pTextureSurface1);
      }

      //side by side view if "both" mode
      if(m_Mode == FFS_MODE_BOTH)
      {
         m_pd3dDevice->SetViewport(&vp1);
         FLOAT fAspect = ((FLOAT)m_iWidth/2) / m_iHeight;
         D3DXMatrixPerspectiveFovLH(&m_matProj, D3DX_PI/4, fAspect, 
                                m_fObjectRadius/64.0f, m_fObjectRadius*200.0f);
         m_pd3dDevice->SetTransform(D3DTS_PROJECTION, &m_matProj);
      }
      else
      {
		   m_pd3dDevice->SetViewport(&vp);
         FLOAT fAspect = ((FLOAT)m_iWidth) / m_iHeight;
         D3DXMatrixPerspectiveFovLH(&m_matProj, D3DX_PI/4, fAspect, 
                                m_fObjectRadius/64.0f, m_fObjectRadius*200.0f);
         m_pd3dDevice->SetTransform(D3DTS_PROJECTION, &m_matProj);
      }

      //clear the screen
      m_pd3dDevice->Clear(0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
                         0x00000000, 1.0f, 0L);

      //set texture state
      if(m_scenestate.m_tex.iTexType == TEX_TYPE_CUBEMAP)
      {
         m_pd3dDevice->SetTexture(0, m_pCubeMap);
         m_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_ADD);
         m_pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
         m_pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
      }
      else
      {
         m_pd3dDevice->SetTexture(0, NULL);
      }

      //set vertex declaration and stretch sources
      if(m_scenestate.m_bTweening)
      {
         m_fTweenFactor = (float)((cos(m_fTime)+1.)/2.);
         m_pEffect->SetValue("fTweenFactor", (void*)(FLOAT*)&m_fTweenFactor, sizeof(float));
         m_pd3dDevice->SetVertexDeclaration(m_pVertexDeclarationTween);
         m_pd3dDevice->SetStreamSource(0, m_pMeshVB, 0, sizeof(D3DVERTEX));
         m_pd3dDevice->SetStreamSource(1, m_pMesh2VB, 0, sizeof(D3DVERTEX));
         m_pd3dDevice->SetIndices(m_pMeshIB);
      }
      else
      {
         m_pd3dDevice->SetVertexDeclaration(m_pVertexDeclaration);
         m_pd3dDevice->SetStreamSource(0, m_pMeshVB, 0, sizeof(D3DVERTEX));
         m_pd3dDevice->SetIndices(m_pMeshIB);
      }

      if(m_Mode != FFS_MODE_FIXED)
      {
         //render using programmable shader
         m_pEffect->SetTechnique(m_pEffect->GetTechniqueByName("basic_with_shader"));

         D3DXMATRIXA16 compMat, invMat;
         m_pEffect->SetMatrixTranspose("matView", &m_matView);
         D3DXMatrixMultiply(&compMat, &m_matWorld, &m_matView);
         m_pEffect->SetMatrixTranspose("matWorldView", &compMat);
         D3DXMatrixInverse(&invMat, NULL, &compMat);
         D3DXMatrixTranspose(&invMat, &invMat);
         m_pEffect->SetMatrixTranspose("matWorldViewIT", &invMat);
         D3DXMatrixInverse(&invMat, NULL, &m_matView);
         D3DXMatrixTranspose(&invMat, &invMat);
         m_pEffect->SetMatrixTranspose("matViewIT", &invMat);
         D3DXMatrixMultiply(&compMat, &compMat, &m_matProj);
         m_pEffect->SetMatrixTranspose("matWorldViewProj", &compMat);
         
         m_pEffect->SetMatrixTranspose("matProj", &m_matProj);
         m_pEffect->SetMatrixTranspose("matWorld", &m_matWorld);

         UINT cPasses;
         m_pEffect->Begin(&cPasses, 0);
         m_pEffect->Pass(0);

         m_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0,
                                            0, m_dwNumVertices,
                                            0, m_dwNumFaces);

         m_pEffect->End();
      }

      if(m_Mode != FFS_MODE_SHADER)
      {
         if(m_scenestate.m_bTweening)
         {
            m_pd3dDevice->SetRenderState(D3DRS_VERTEXBLEND, D3DVBF_TWEENING);
            m_pd3dDevice->SetRenderState(D3DRS_TWEENFACTOR, *((DWORD*)&m_fTweenFactor));
         }

         //set viewports for fixed shader rendering
         if(m_Mode == FFS_MODE_BOTH)
         {
            m_pd3dDevice->SetViewport(&vp2);
            m_pd3dDevice->Clear(0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
                         0x00000000, 1.0f, 0L);
         }

         //if diff mode, render to a second texture
         if(m_Mode == FFS_MODE_DIFF)
         {
            m_pd3dDevice->SetRenderTarget(0, m_pTextureSurface2);
            m_pd3dDevice->Clear(0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
                         0x00000000, 1.0f, 0L);
         }

         //this effect has no vertex/pixel shaders; 
         //it just sets state and uses fixed pipeline
         m_pEffect->SetTechnique(m_pEffect->GetTechniqueByName("basic"));

         UINT cPasses;
         m_pEffect->Begin(&cPasses, 0);
         m_pEffect->Pass(0);

         m_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0,
                                            0, m_dwNumVertices,
                                            0, m_dwNumFaces);

         m_pEffect->End();
      }

      //clears texture settings
      m_pd3dDevice->SetTexture(0, NULL);

      if(m_scenestate.m_bTweening)
      {
         m_pd3dDevice->SetRenderState(D3DRS_VERTEXBLEND, D3DVBF_DISABLE);
         m_pd3dDevice->SetStreamSource(1, NULL, 0, 0);
         m_pd3dDevice->SetVertexDeclaration(m_pVertexDeclaration);
      }

      if(m_Mode == FFS_MODE_DIFF)
      {
         //renders diff mode using pixel shader to diff 
         // the two target textures from the previous passes
         m_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);

         m_pd3dDevice->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_PASSTHRU);
         m_pd3dDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_PASSTHRU);

         LPDIRECT3DSURFACE9 pBackBuffer;
         m_pd3dDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer);
         m_pd3dDevice->SetRenderTarget(0, pBackBuffer);

         m_pd3dDevice->Clear(0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
                         0x00000000, 1.0f, 0L);

         D3DXMATRIXA16 matIdentity;
         D3DXMATRIXA16 matProj2;
         D3DXMatrixIdentity(&matIdentity);

         m_pd3dDevice->SetTransform(D3DTS_VIEW, &matIdentity);
         m_pd3dDevice->SetTransform(D3DTS_WORLD,  &matIdentity);
         D3DXMatrixOrthoLH(&matProj2, (float)m_iWidth, (float)m_iHeight, 0.f, 1.f);
         m_pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProj2);

         m_pd3dDevice->SetRenderState(D3DRS_FOGENABLE,        FALSE);
         m_pd3dDevice->SetFVF(D3DVERTEX::FVF);
         m_pd3dDevice->SetStreamSource(0, m_pQuadVB, 0 , sizeof(D3DVERTEX));
   
         m_pEffect->SetTechnique(m_pEffect->GetTechniqueByName("technique_diff"));

         UINT cPasses;
         m_pEffect->Begin(&cPasses, 0);

         m_pEffect->Pass(0);

         m_pd3dDevice->SetTexture(0, m_pTexture1);
         m_pd3dDevice->SetTexture(1, m_pTexture2);
         m_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);
         m_pd3dDevice->SetTexture(0, NULL);
         m_pd3dDevice->SetTexture(1, NULL);

         m_pEffect->End();

         //reads in the framebuffer containing the diff data
         m_pd3dDevice->GetRenderTargetData(pBackBuffer, m_pTextureSurface3);

         //compute statistics on the diff data and render error bars
         D3DLOCKED_RECT lr;
         m_pTexture3->LockRect(0, &lr, NULL, D3DLOCK_READONLY);
         BYTE *lrb = (BYTE *)lr.pBits;
         m_statFrameError.Reset(false);
         for(int x = 0; x < m_iWidth; x++)
         for(int y = 0; y < m_iHeight; y++)
         {
            float e = ((float)lrb[(lr.Pitch/4 * y + x)*4+3]) / 255.f;
            m_statFrameError.Add(e);
         }
         m_pTexture3->UnlockRect(0);

         RenderErrorBar();
         m_statFrameError.RenderHistogram(m_pd3dDevice, m_pFontSmall, 
            m_iWidth/4.f,-m_iHeight/2.f,m_iWidth/4.f,m_iHeight/4.f,m_iWidth, m_iHeight); //lower-right corner

         m_pd3dDevice->SetTransform(D3DTS_VIEW, &m_matView);
         m_pd3dDevice->SetTransform(D3DTS_WORLD,  &m_matWorld);
         m_pd3dDevice->SetTransform(D3DTS_PROJECTION, &m_matProj);
         m_scenestate.Set(m_pd3dDevice, m_pEffect);

         m_pd3dDevice->SetRenderState(D3DRS_LIGHTING, TRUE);

⌨️ 快捷键说明

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