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

📄 ch24p1_toonshader.cpp

📁 游戏开发特殊技巧-special.effects.game.programming
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	  m_pd3dDevice->SetTextureStageState(0, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP);
	  
	  m_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
	  m_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
	  m_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
	  m_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, 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->SetTransform(D3DTS_WORLD, &matWorld);
    m_pd3dDevice->SetTransform(D3DTS_VIEW, &m_Camera.GetViewMatrix());
    m_pd3dDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
    m_pd3dDevice->SetVertexShader(m_dwShader); // set our vertex shader active

    LPDIRECT3DVERTEXBUFFER8 pVB; m_pTeapot->GetLocalMesh()->GetVertexBuffer(&pVB);
    LPDIRECT3DINDEXBUFFER8 pIB; m_pTeapot->GetLocalMesh()->GetIndexBuffer(&pIB);
    
    SetMeshColor(pVB, m_pTeapot->GetLocalMesh()->GetNumVertices(),
     D3DXCOLOR(1.0, 0.0, 0.0, 1.0));

    m_pd3dDevice->SetStreamSource( 0, pVB, sizeof(VERTEX_XYZ_NORMAL_1DTEX2));
    m_pd3dDevice->SetIndices(pIB, 0);
    
    m_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 
      m_pTeapot->GetLocalMesh()->GetNumVertices(), 0, 
      m_pTeapot->GetLocalMesh()->GetNumFaces());
    
    pVB->Release();
    pIB->Release();

    char buf[256];
    
    D3DXVECTOR3 v = m_Camera.GetPosition();
    _snprintf(buf, 256, "Position: (%02f, %02f, %02f)", v.x, v.y, v.z);

    m_pFontSmall->DrawText( 2,  0,  D3DCOLOR_ARGB(255,255,255,0), buf );
    
    _snprintf(buf, 256, "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 );
    
    // 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);
  D3DXCreateTextureFromFile(m_pd3dDevice, 
    m_ShadeTextures[m_iShadeTexture].m_filename.c_str(), &m_pShadeTexture);
}
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("Ch24p1_Teapot.x") ) ) )
    return D3DAPPERR_MEDIANOTFOUND;

  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( "Ch24p1_ToonShader.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", "Ch24p1_ShadeTexture_3.bmp"));
  m_ShadeTextures.push_back(CShadeTextureEntry("16-color", "Ch24p1_ShadeTexture_16.bmp"));
  m_ShadeTextures.push_back(CShadeTextureEntry("Smooth", "Ch24p1_ShadeTexture_256.bmp"));
  m_ShadeTextures.push_back(CShadeTextureEntry("Inverted", "Ch24p1_ShadeTexture_Inverse.bmp"));
  
  m_pShadeTexture = NULL;
  m_iShadeTexture = 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 );

  m_pd3dDevice->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_POINT);
	m_pd3dDevice->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_POINT);
	m_pd3dDevice->SetTextureStageState(0, D3DTSS_MIPFILTER, D3DTEXF_NONE);
  
  return S_OK;
}

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

  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();
  }
  // Pass remaining messages to default handler
  return CD3DApplication::MsgProc( hWnd, uMsg, wParam, lParam );
}

⌨️ 快捷键说明

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