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

📄 ch24p2_toonshadersilo.cpp

📁 游戏开发特殊技巧-special.effects.game.programming
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	    
	    m_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
	    m_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
	    
	    m_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_CURRENT);
	    m_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_DIFFUSE);	
      m_pd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE);

      m_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 
        m_pTeapot->GetLocalMesh()->GetNumVertices(), 0, 
        m_pTeapot->GetLocalMesh()->GetNumFaces());
    }
    

    // pass 2 - draw siloheutte
    {
      // set up texture blending modes
      m_pd3dDevice->SetTexture(0, m_pSiloTexture);
      m_pd3dDevice->SetTexture(1, m_pSiloTexture);

      m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
      m_pd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
      m_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );
  
 	    m_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_DIFFUSE);
	    m_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
	    m_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE);
	    m_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
	    
      m_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
	    m_pd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
	    m_pd3dDevice->SetTextureStageState(1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
	    m_pd3dDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
	    
      m_pd3dDevice->SetTextureStageState(2, D3DTSS_COLOROP, D3DTOP_DISABLE);
      m_pd3dDevice->SetTextureStageState(2, D3DTSS_ALPHAOP, D3DTOP_DISABLE);

      m_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 
        m_pTeapot->GetLocalMesh()->GetNumVertices(), 0, 
        m_pTeapot->GetLocalMesh()->GetNumFaces());
    }

    pVB->Release();
    pIB->Release();    
      

    char buf[256];
    _snprintf(buf, 256, "Position: (%.2f, %.2f, %.2f)", 
      (float)m_Camera.GetPosition().x, (float)m_Camera.GetPosition().y, 
      (float)m_Camera.GetPosition().z);

    m_pFontSmall->DrawText( 2,  0,  D3DCOLOR_ARGB(255,255,255,0), buf );
    
    _snprintf(buf, 256, "Shading Texture: %s (T to change)", 
      m_ShadeTextures[m_iShadeTexture].m_name.c_str());

    m_pFontSmall->DrawText( 2,  15,  D3DCOLOR_ARGB(255,255,255,0), buf );
    
    _snprintf(buf, 256, "Silhouette Texture: %s (Y to change)", 
      m_SiloTextures[m_iSiloTexture].m_name.c_str());

    m_pFontSmall->DrawText( 2,  30,  D3DCOLOR_ARGB(255,255,255,0), buf );
    
    // End the scene.
    m_pd3dDevice->EndScene();
  }

  return S_OK;
}

//-----------------------------------------------------------------------------
// Name: InitDeviceObjects()
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InitDeviceObjects()
{
  m_pFont->InitDeviceObjects( m_pd3dDevice );
  m_pFontSmall->InitDeviceObjects( m_pd3dDevice );
	
  return S_OK;
}

void CMyD3DApplication::SwitchTextures()
{
  SAFE_RELEASE(m_pShadeTexture);
  SAFE_RELEASE(m_pSiloTexture);

  D3DXCreateTextureFromFile(m_pd3dDevice, 
    m_ShadeTextures[m_iShadeTexture].m_filename.c_str(), &m_pShadeTexture);

  D3DXCreateTextureFromFile(m_pd3dDevice, 
    m_SiloTextures[m_iSiloTexture].m_filename.c_str(), &m_pSiloTexture);
}
HRESULT CMyD3DApplication::SetMeshColor(LPDIRECT3DVERTEXBUFFER8 pVB, 
                                        int iNumVerts, D3DXCOLOR color)
{
  HRESULT hr;

  // lock vertex buffer
  VERTEX_XYZ_NORMAL_1DTEX2 *pVertices;

  if(FAILED(hr = pVB->Lock(0, 
            iNumVerts*sizeof(VERTEX_XYZ_NORMAL_1DTEX2), 
            (BYTE**)&pVertices, 0)))
    return hr;

  // set diffuse color of each vertex
  for (int q=0; q < iNumVerts; q++) {
    pVertices[q].diffuse = color;
  }

  pVB->Unlock();
  return S_OK;
}

//-----------------------------------------------------------------------------
// Name: RestoreDeviceObjects()
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::RestoreDeviceObjects()
{
  HRESULT hr;
	m_InputManager.CreateDevices(m_hWnd, true, true);

  m_pTeapot = new CD3DMesh();

  
  if( FAILED( m_pTeapot->Create( m_pd3dDevice, _T("Ch24p2_Teapot.x") ) ) )
    return D3DAPPERR_MEDIANOTFOUND;


  
  //D3DXCreateTorus(m_pd3dDevice, 0.5, 1, 16, 32, &m_pTeapot->m_pSysMemMesh, NULL);

  m_pTeapot->RestoreDeviceObjects( m_pd3dDevice );
  m_pTeapot->SetFVF(m_pd3dDevice, D3DFVF_XYZ_NORMAL_1DTEX2);

  // Create vertex shader
  {
    LPD3DXBUFFER pCode;

    DWORD dwDecl[] =
    {
      D3DVSD_STREAM(0),
      D3DVSD_REG(0, D3DVSDT_FLOAT3),   // position
      D3DVSD_REG(1, D3DVSDT_FLOAT3),   // normal
      D3DVSD_REG(4, D3DVSDT_D3DCOLOR), // diffuse color
      D3DVSD_REG(2, D3DVSDT_FLOAT1),   // u0
      D3DVSD_REG(3, D3DVSDT_FLOAT1),   // u1
      D3DVSD_END()
    };

    // Assemble the vertex shader from the file
    if( FAILED( hr = D3DXAssembleShaderFromFile( "Ch24p2_ToonShaderSilo.vs", 
                                                 0, NULL, &pCode, NULL ) ) )
      return hr;

    // Create the vertex shader
    hr = m_pd3dDevice->CreateVertexShader( dwDecl, 
      (DWORD*)pCode->GetBufferPointer(), &m_dwShader, 0 );
    pCode->Release();
    if( FAILED(hr) ) return hr;
  }


  m_ShadeTextures.clear();
  m_ShadeTextures.push_back(CShadeTextureEntry("3-color", "Ch24p2_ShadeTexture_3.bmp"));
  m_ShadeTextures.push_back(CShadeTextureEntry("16-color", "Ch24p2_ShadeTexture_16.bmp"));
  m_ShadeTextures.push_back(CShadeTextureEntry("Smooth", "Ch24p2_ShadeTexture_256.bmp"));
  m_ShadeTextures.push_back(CShadeTextureEntry("Inverted", "Ch24p2_ShadeTexture_Inverse.bmp"));
  
  m_iShadeTexture = 0;
  
  m_SiloTextures.clear();
  m_SiloTextures.push_back(CShadeTextureEntry("Normal Pen", "Ch24p2_SiloTextureNormal.dds"));
  m_SiloTextures.push_back(CShadeTextureEntry("Thin Pen", "Ch24p2_SiloTextureThin.dds"));
  m_SiloTextures.push_back(CShadeTextureEntry("Pencil", "Ch24p2_SiloTexturePencil.dds"));
  m_SiloTextures.push_back(CShadeTextureEntry("Thick Pen", "Ch24p2_SiloTextureThickPen.dds"));
  m_iSiloTexture = 0;
  
  SwitchTextures();
  
  m_pFont->RestoreDeviceObjects();
  m_pFontSmall->RestoreDeviceObjects();
  m_Camera.SetPosition(D3DXVECTOR3(0.0f, 0.0f, -5.0f));
	
	// Set the world matrix
  D3DXMATRIX matWorld;
  D3DXMatrixIdentity( &matWorld );
  m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );

  // Set projection matrix
  FLOAT fAspect = ((FLOAT)m_d3dsdBackBuffer.Width) / m_d3dsdBackBuffer.Height;
  D3DXMatrixPerspectiveFovLH( &m_matProj, D3DX_PI/4, fAspect, 0.1f, 100.0f );
  m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &m_matProj );

  m_pd3dDevice->SetRenderState( D3DRS_SRCBLEND,  D3DBLEND_ONE );
  m_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ONE );
  m_pd3dDevice->SetRenderState( D3DRS_LIGHTING,  FALSE );
  m_pd3dDevice->SetRenderState( D3DRS_CULLMODE,  D3DCULL_CCW );
  m_pd3dDevice->SetRenderState( D3DRS_SHADEMODE, D3DSHADE_GOURAUD );
  
  return S_OK;
}

//-----------------------------------------------------------------------------
// Name: InvalidateDeviceObjects()
// Desc:
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InvalidateDeviceObjects()
{
  m_pd3dDevice->DeleteVertexShader(m_dwShader);
  SAFE_RELEASE(m_pShadeTexture);
  SAFE_RELEASE(m_pSiloTexture);

  m_pTeapot->InvalidateDeviceObjects();
  SAFE_DELETE(m_pTeapot);

  m_InputManager.DestroyDevices();
  m_pFont->InvalidateDeviceObjects();
  m_pFontSmall->InvalidateDeviceObjects();
  return S_OK;
}

//-----------------------------------------------------------------------------
// Name: DeleteDeviceObjects()
// Desc: Called when the app is exiting, or the device is being changed,
//       this function deletes any device dependent objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::DeleteDeviceObjects()
{
  m_pFont->DeleteDeviceObjects();
  m_pFontSmall->DeleteDeviceObjects();
    
  return S_OK;
}

//-----------------------------------------------------------------------------
// Name: FinalCleanup()
// Desc: Called before the app exits, this function gives the app the chance
//       to cleanup after itself.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::FinalCleanup()
{
  SAFE_DELETE( m_pFont );
  SAFE_DELETE( m_pFontSmall );
  return S_OK;
}

//-----------------------------------------------------------------------------
// Name: ConfirmDevice()
// Desc: Called during device intialization, this code checks the device
//       for some minimum set of capabilities
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS8* pCaps, DWORD dwBehavior,
                                          D3DFORMAT Format )
{
  return S_OK;
}

//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: Message proc function to handle key and menu input
//-----------------------------------------------------------------------------
LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam,
                                    LPARAM lParam )
{
  if (uMsg == WM_KEYUP && (wParam == 't' || wParam == 'T')) {
    m_iShadeTexture++; if (m_iShadeTexture >= m_ShadeTextures.size()) {
      m_iShadeTexture = 0;
    }
    SwitchTextures();
  }
  if (uMsg == WM_KEYUP && (wParam == 'y' || wParam == 'Y')) {
    m_iSiloTexture++; if (m_iSiloTexture >= m_SiloTextures.size()) {
      m_iSiloTexture = 0;
    }
    SwitchTextures();
  }
 
  // Pass remaining messages to default handler
  return CD3DApplication::MsgProc( hWnd, uMsg, wParam, lParam );
}

⌨️ 快捷键说明

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