Exemple #1
0
void Viewer::renderModel()
{
  	
  // get the renderer of the model
  CalRenderer *pCalRenderer;
  pCalRenderer = m_calModel->getRenderer();

  // begin the rendering loop
  if(!pCalRenderer->beginRendering()) return;

  m_VBCursor=0;
  m_IBCursor=0;
  m_TBCursor=0;
  m_TCBCursor=0;

  DWORD dwVBLockFlags=D3DLOCK_NOOVERWRITE;
  DWORD dwIBLockFlags=D3DLOCK_NOOVERWRITE;
  DWORD dwTBLockFlags=D3DLOCK_NOOVERWRITE;
  DWORD dwTCBLockFlags=D3DLOCK_NOOVERWRITE;

  g_pD3DDevice->SetStreamSource( 0, m_pVB, 0,  sizeof(VERTEX) );
  g_pD3DDevice->SetStreamSource( 1, NULL , 0,  0 );
  g_pD3DDevice->SetStreamSource( 2, NULL , 0,  0 );

  g_pD3DDevice->SetFVF(D3DFVF_VERTEX);
  
      
  // get the number of meshes
  int meshCount;
  meshCount = pCalRenderer->getMeshCount();

  // render all meshes of the model
  int meshId;
  for(meshId = 0; meshId < meshCount; meshId++)
  {
    // get the number of submeshes
    int submeshCount;
    submeshCount = pCalRenderer->getSubmeshCount(meshId);

    // render all submeshes of the mesh
    int submeshId;
    for(submeshId = 0; submeshId < submeshCount; submeshId++)
    {
      // select mesh and submesh for further data access
      if(pCalRenderer->selectMeshSubmesh(meshId, submeshId))
      {

		  D3DMATERIAL9 mat;
          unsigned char meshColor[4];
          pCalRenderer->getAmbientColor(&meshColor[0]);
          mat.Ambient.r=meshColor[0]/255.0f;mat.Ambient.g=meshColor[1]/255.0f;
          mat.Ambient.b=meshColor[2]/255.0f;mat.Ambient.a=meshColor[3]/255.0f;

          pCalRenderer->getDiffuseColor(&meshColor[0]);
          mat.Diffuse.r=meshColor[0]/255.0f;mat.Diffuse.g=meshColor[1]/255.0f;
          mat.Diffuse.b=meshColor[2]/255.0f;mat.Diffuse.a=meshColor[3]/255.0f;

          pCalRenderer->getSpecularColor(&meshColor[0]);
          mat.Specular.r=meshColor[0]/255.0f;mat.Specular.g=meshColor[1]/255.0f;
          mat.Specular.b=meshColor[2]/255.0f;mat.Specular.a=meshColor[3]/255.0f;

		  mat.Power=0.0f;
          mat.Emissive.r=0.0f;mat.Emissive.g=0.0f;mat.Emissive.b=0.0f;mat.Emissive.a=0.0f;
				
          g_pD3DDevice->SetMaterial(&mat);


		  if(m_VBCursor+ pCalRenderer->getVertexCount()>=30000)
		  {
		  	  m_VBCursor=0;
			  dwVBLockFlags=D3DLOCK_DISCARD;
		  }

		  if(m_TBCursor+ pCalRenderer->getVertexCount()>=30000)
		  {
		  	  m_TBCursor=0;
			  dwTBLockFlags=D3DLOCK_DISCARD;
		  }

		  if(m_TCBCursor+ pCalRenderer->getVertexCount()>=30000)
		  {
		  	  m_TCBCursor=0;
			  dwTCBLockFlags=D3DLOCK_DISCARD;
		  }

		  if(m_IBCursor + pCalRenderer->getFaceCount()>=50000)
		  {
		  	  m_IBCursor=0;
			  dwIBLockFlags=D3DLOCK_DISCARD;

		  }
		  

		  // Get vertexbuffer from the model		  		  
		  VERTEX *pVertices;

		  m_pVB->Lock(m_VBCursor*sizeof(VERTEX), pCalRenderer->getVertexCount()*sizeof(VERTEX), (void**)&pVertices, dwVBLockFlags);
		  int vertexCount = pCalRenderer->getVerticesNormalsAndTexCoords(&pVertices->pos.x);
		  m_pVB->Unlock();
		  
		  CalIndex *meshFaces;

		  m_pIB->Lock(m_IBCursor* 3*sizeof(CalIndex), pCalRenderer->getFaceCount()*3* sizeof(CalIndex), (void**)&meshFaces,dwIBLockFlags);
		  int faceCount = pCalRenderer->getFaces(meshFaces);
		  m_pIB->Unlock();

		  
  		  if(pCalRenderer->getMapCount()==2 && m_bBump  && pCalRenderer->isTangentsEnabled(1))
		  {
  			  D3DXVECTOR4 *pTangents;
		      m_pTB->Lock(m_TBCursor*sizeof(D3DXVECTOR4), pCalRenderer->getVertexCount()*sizeof(D3DXVECTOR4), (void**)&pTangents, dwTBLockFlags);
	    	  pCalRenderer->getTangentSpaces(1,(float*)pTangents);
			  m_pTB->Unlock();

			  D3DXVECTOR2 *pTexCoords;
			  m_pTCB->Lock(m_TCBCursor*sizeof(D3DXVECTOR2), pCalRenderer->getVertexCount()*sizeof(D3DXVECTOR2), (void**)&pTexCoords, dwTCBLockFlags);
	    	  pCalRenderer->getTextureCoordinates(1,(float*)pTexCoords);
			  m_pTCB->Unlock();

			  // Rem:
			  // We need to swap the map, texture coord are also swapped in the vertex shader
              
  			  g_pD3DDevice->SetTexture(0,(LPDIRECT3DTEXTURE9)pCalRenderer->getMapUserData(1));
			  g_pD3DDevice->SetTexture(1,(LPDIRECT3DTEXTURE9)pCalRenderer->getMapUserData(0));

			  			  
			  g_pD3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1 ,D3DTA_TEXTURE);
		      g_pD3DDevice->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_DOTPRODUCT3);
		      g_pD3DDevice->SetTextureStageState(0,D3DTSS_COLORARG2 ,D3DTA_DIFFUSE);
			  
			  g_pD3DDevice->SetTextureStageState(1,D3DTSS_COLORARG1 ,D3DTA_TEXTURE);
		      g_pD3DDevice->SetTextureStageState(1,D3DTSS_COLOROP,D3DTOP_MODULATE);
		      g_pD3DDevice->SetTextureStageState(1,D3DTSS_COLORARG2 ,D3DTA_CURRENT);	

			  // Uncomment this if you want to use the diffuse color of
			  // The material, but I will not work on a geforce-class hardware

			  /*		      
			  g_pD3DDevice->SetTextureStageState(2,D3DTSS_COLORARG1 ,D3DTA_CURRENT);
    	      g_pD3DDevice->SetTextureStageState(2,D3DTSS_COLOROP,D3DTOP_MODULATE);
			  g_pD3DDevice->SetTextureStageState(2,D3DTSS_COLORARG2 ,D3DTA_SPECULAR);
			  */

              g_pD3DDevice->SetStreamSource( 0, m_pVB, 0,  sizeof(VERTEX) );
	  		  g_pD3DDevice->SetStreamSource( 1, m_pTB,0, sizeof(D3DXVECTOR4) );
			  g_pD3DDevice->SetStreamSource( 2, m_pTCB,0, sizeof(D3DXVECTOR2) );

              g_pD3DDevice->SetVertexShader(m_pVS);
			  g_pD3DDevice->SetVertexDeclaration(m_pVertexDeclaration);
			  g_pD3DDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
			
			  g_pD3DDevice->SetVertexShaderConstantF(9, (float*)&mat.Diffuse, 1 );
    	      g_pD3DDevice->SetVertexShaderConstantF(10, (float*)&mat.Specular, 1 );
	    	  g_pD3DDevice->SetVertexShaderConstantF(11, (float*)&mat.Ambient, 1 );
		  }
  		  
		  if(pCalRenderer->getMapCount()==1 || ( (!m_bBump || !pCalRenderer->isTangentsEnabled(1))  && pCalRenderer->getMapCount()==2))
		  {
  			  g_pD3DDevice->SetTexture(0,(LPDIRECT3DTEXTURE9)pCalRenderer->getMapUserData(0));
			  g_pD3DDevice->SetTexture(1,NULL);

			  g_pD3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1 ,D3DTA_CURRENT);
		      g_pD3DDevice->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_MODULATE);
		      g_pD3DDevice->SetTextureStageState(0,D3DTSS_COLORARG2 ,D3DTA_TEXTURE);			  
			  g_pD3DDevice->SetTextureStageState(1,D3DTSS_COLOROP,D3DTOP_DISABLE);          

              g_pD3DDevice->SetStreamSource( 0, m_pVB, 0,  sizeof(VERTEX) );
			  g_pD3DDevice->SetStreamSource( 1, NULL,0, 0 );
			  g_pD3DDevice->SetStreamSource( 2, NULL,0, 0 );

			  g_pD3DDevice->SetVertexShader(NULL);
			  g_pD3DDevice->SetVertexDeclaration(NULL);
  			  g_pD3DDevice->SetFVF(D3DFVF_VERTEX);

  			  g_pD3DDevice->SetRenderState( D3DRS_LIGHTING, TRUE );


		  }
		  if(pCalRenderer->getMapCount()==0)
		  {
  			  g_pD3DDevice->SetTexture(0,NULL);			  
  			  g_pD3DDevice->SetTexture(1,NULL);

			  g_pD3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1 ,D3DTA_CURRENT);
		      g_pD3DDevice->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_SELECTARG1);		      
			  g_pD3DDevice->SetTextureStageState(1,D3DTSS_COLOROP,D3DTOP_DISABLE);

  			  g_pD3DDevice->SetStreamSource( 1, NULL,0, 0 );
			  g_pD3DDevice->SetStreamSource( 2, NULL,0, 0 );

              g_pD3DDevice->SetStreamSource( 0, m_pVB, 0,  sizeof(VERTEX) );
			  g_pD3DDevice->SetVertexShader(NULL);
			  g_pD3DDevice->SetVertexDeclaration(NULL);
  			  g_pD3DDevice->SetFVF(D3DFVF_VERTEX);

  			  g_pD3DDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
		  }

          g_pD3DDevice->SetIndices(m_pIB);
          g_pD3DDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
          
		  
		  g_pD3DDevice->DrawIndexedPrimitive(
			  D3DPT_TRIANGLELIST,
			  m_VBCursor,
			  0,
			  vertexCount,
			  m_IBCursor*3,
			  faceCount
			);


		  m_VBCursor+=vertexCount;
		  m_TBCursor+=vertexCount;
		  m_TCBCursor+=vertexCount;
		  m_IBCursor+=faceCount;		  

		  dwIBLockFlags=D3DLOCK_NOOVERWRITE;
		  dwTBLockFlags=D3DLOCK_NOOVERWRITE;
		  dwTCBLockFlags=D3DLOCK_NOOVERWRITE;
		  dwVBLockFlags=D3DLOCK_NOOVERWRITE;

      }
    }
  }
  // end the rendering
  pCalRenderer->endRendering();
  

}
void Viewer::renderModel()
{

  // get the renderer of the model
  CalRenderer *pCalRenderer;
  pCalRenderer = m_calModel->getRenderer();

  // begin the rendering loop
  if(!pCalRenderer->beginRendering()) return;

  m_VBCursor=0;
  m_IBCursor=0;

  DWORD dwVBLockFlags=D3DLOCK_NOOVERWRITE;
  DWORD dwIBLockFlags=D3DLOCK_NOOVERWRITE;

  g_pD3DDevice->SetStreamSource( 0, m_pVB, 0,  sizeof(VERTEX) );
  g_pD3DDevice->SetFVF(D3DFVF_VERTEX);
  g_pD3DDevice->SetIndices(m_pIB);



  // get the number of meshes
  int meshCount;
  meshCount = pCalRenderer->getMeshCount();

  // render all meshes of the model
  int meshId;
  for(meshId = 0; meshId < meshCount; meshId++)
  {
    // get the number of submeshes
    int submeshCount;
    submeshCount = pCalRenderer->getSubmeshCount(meshId);

    // render all submeshes of the mesh
    int submeshId;
    for(submeshId = 0; submeshId < submeshCount; submeshId++)
    {
      // select mesh and submesh for further data access
      if(pCalRenderer->selectMeshSubmesh(meshId, submeshId))
      {

        D3DMATERIAL9 mat;
        unsigned char meshColor[4];
        pCalRenderer->getAmbientColor(&meshColor[0]);
        mat.Ambient.r=meshColor[0]/255.0f;mat.Ambient.g=meshColor[1]/255.0f;
        mat.Ambient.b=meshColor[2]/255.0f;mat.Ambient.a=meshColor[3]/255.0f;

        pCalRenderer->getDiffuseColor(&meshColor[0]);
        mat.Diffuse.r=meshColor[0]/255.0f;mat.Diffuse.g=meshColor[1]/255.0f;
        mat.Diffuse.b=meshColor[2]/255.0f;mat.Diffuse.a=meshColor[3]/255.0f;

        pCalRenderer->getSpecularColor(&meshColor[0]);
        mat.Specular.r=meshColor[0]/255.0f;mat.Specular.g=meshColor[1]/255.0f;
        mat.Specular.b=meshColor[2]/255.0f;mat.Specular.a=meshColor[3]/255.0f;

        mat.Power=0.0f;
        mat.Emissive.r=0.0f;mat.Emissive.g=0.0f;mat.Emissive.b=0.0f;mat.Emissive.a=0.0f;


        g_pD3DDevice->SetMaterial(&mat);

        if(m_VBCursor+ pCalRenderer->getVertexCount()>=30000)
        {
          m_VBCursor=0;
          dwVBLockFlags=D3DLOCK_DISCARD;
        }

        if(m_IBCursor + pCalRenderer->getFaceCount()>=50000)
        {
          m_IBCursor=0;
          dwIBLockFlags=D3DLOCK_DISCARD;

        }


        // Get vertexbuffer from the model		  		  
        VERTEX *pVertices;

        m_pVB->Lock(m_VBCursor*sizeof(VERTEX), pCalRenderer->getVertexCount()*sizeof(VERTEX), (void**)&pVertices, dwVBLockFlags);

        int vertexCount = pCalRenderer->getVerticesNormalsAndTexCoords(&pVertices->pos.x);
        m_pVB->Unlock();

        CalIndex *meshFaces;

        int faceCount;

        m_pIB->Lock(m_IBCursor* 3*sizeof(CalIndex), pCalRenderer->getFaceCount()*3* sizeof(CalIndex), (void**)&meshFaces,dwIBLockFlags);

        faceCount = pCalRenderer->getFaces(meshFaces);
        m_pIB->Unlock();


        g_pD3DDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
        g_pD3DDevice->SetTexture(0,(LPDIRECT3DTEXTURE9)pCalRenderer->getMapUserData(0));

        g_pD3DDevice->DrawIndexedPrimitive(
          D3DPT_TRIANGLELIST,
          m_VBCursor,
          0,
          vertexCount,
          m_IBCursor*3,
          faceCount
          );

        m_VBCursor+=vertexCount;
        m_IBCursor+=faceCount;

        dwIBLockFlags=D3DLOCK_NOOVERWRITE;
        dwVBLockFlags=D3DLOCK_NOOVERWRITE;

      }
    }
  }


  // end the rendering
  pCalRenderer->endRendering();


}
///<summary>
/// CAnimatedInstanceModel:: RenderModelBySoftware : Render del modelo en el software 
///</summary>
///<param name="RM"></param>
void CAnimatedInstanceModel::RenderModelBySoftware(CRenderManager *RM)
{
  //leer renderModel
  LPDIRECT3DDEVICE9 l_pDevice = RM->GetDevice();
  
  // get the renderer of the model
  CalRenderer * pCalRenderer;
  pCalRenderer = m_CalModel->getRenderer();

  // begin the rendering loop
  if(!pCalRenderer->beginRendering()) return;

  int l_iVBCursor=0;
  int l_iIBCursor=0;

  DWORD dwVBLockFlags=D3DLOCK_NOOVERWRITE;
  DWORD dwIBLockFlags=D3DLOCK_NOOVERWRITE;

  l_pDevice->SetStreamSource( 0, m_pVB, 0,  sizeof(TNORMAL_TEXTURE1_VERTEX) );
  l_pDevice->SetFVF(TNORMAL_TEXTURE1_VERTEX::GetFVF());
  l_pDevice->SetIndices(m_pIB);
  
  // get the number of meshes
  int l_iMeshCount;
  l_iMeshCount = pCalRenderer->getMeshCount();

  // render all meshes of the model
  int l_iMeshId;
  for(l_iMeshId = 0; l_iMeshId < l_iMeshCount; l_iMeshId++)
  {
    // get the number of submeshes
    int l_iSubmeshCount;
    l_iSubmeshCount = pCalRenderer->getSubmeshCount(l_iMeshId);

    // render all submeshes of the mesh
    int l_iSubmeshId;
    for(l_iSubmeshId = 0; l_iSubmeshId < l_iSubmeshCount; l_iSubmeshId++)
    {
      // select mesh and submesh for further data access
      if(pCalRenderer->selectMeshSubmesh(l_iMeshId, l_iSubmeshId))
      {
		   
		    // Get vertexbuffer from the model		  		  
		    TNORMAL_TEXTURE1_VERTEX* l_pVertices;

		    m_pVB->Lock(l_iVBCursor*sizeof(TNORMAL_TEXTURE1_VERTEX), pCalRenderer->getVertexCount()*sizeof(TNORMAL_TEXTURE1_VERTEX), (void**)&l_pVertices, dwVBLockFlags);

        int l_iVertexCount = pCalRenderer->getVerticesNormalsAndTexCoords((float*)l_pVertices);
		    m_pVB->Unlock();
		  
		    CalIndex *meshFaces;

        int l_iFaceCount;

		    m_pIB->Lock(l_iIBCursor* 3*sizeof(CalIndex), pCalRenderer->getFaceCount()*3* sizeof(CalIndex), (void**)&meshFaces,dwIBLockFlags);

		    l_iFaceCount = pCalRenderer->getFaces(meshFaces);
		    m_pIB->Unlock();


        l_pDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
        l_pDevice->SetTexture(0,(LPDIRECT3DTEXTURE9)m_TextureList[0]);

		    l_pDevice->DrawIndexedPrimitive(
			    D3DPT_TRIANGLELIST,
			    l_iVBCursor,
			    0,
			    l_iVertexCount,
			    l_iIBCursor*3,
			    l_iFaceCount
			  );

		    l_iVBCursor+=l_iVertexCount;
		    l_iIBCursor+=l_iFaceCount;

		    dwIBLockFlags=D3DLOCK_NOOVERWRITE;
		    dwVBLockFlags=D3DLOCK_NOOVERWRITE;

      }
    }
  }
  // end the rendering
  pCalRenderer->endRendering();
}