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

📄 ch24p1_toonshader.cpp

📁 游戏开发特殊技巧-special.effects.game.programming
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
#############################################################################

  Ch24p1_ToonShader.cpp: a program that demonstrates the fire algorithm,
  without any annoying bells and/or whistles.
  
#############################################################################
*/

// include files ////////////////////////////////////////////////////////////
#define STRICT
#include <stdio.h>
#include <math.h>
#include <D3DX8.h>
#include "D3DApp.h"
#include "D3DFile.h"
#include "D3DFont.h"
#include "D3DUtil.h"
#include "DXUtil.h"
#include "D3DHelperFuncs.h"
#include "Ch24p1_resource.h"
#include "Camera.h"
#include "InputManager.h"
#include "SkyBox.h"
#include "Timer.h"
#include "d3dx8mesh.h"

typedef struct
{
	D3DXVECTOR3 pos;
	D3DXVECTOR3 normal;
  D3DCOLOR diffuse;
	float uShade;         //texcoord for stage 0
	float uEdge;         //texcoord for stage 1
} VERTEX_XYZ_NORMAL_1DTEX2;

class CShadeTextureEntry
{
public:
  CShadeTextureEntry(std::string name, std::string filename) {
    m_name = name; m_filename = filename;
  }

  std::string m_name;
  std::string m_filename;
};

#define D3DFVF_XYZ_NORMAL_1DTEX2 \
  D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_TEX2 | \
  D3DFVF_TEXCOORDSIZE1( 0 ) | D3DFVF_TEXCOORDSIZE1( 1 )

//-----------------------------------------------------------------------------
// Name: class CMyD3DApplication
// Desc: Application class. The base class (CD3DApplication) provides the 
//       generic functionality needed in all Direct3D samples. CMyD3DApplication 
//       adds functionality specific to this sample program.
//-----------------------------------------------------------------------------
class CMyD3DApplication : public CD3DApplication
{
  // Font for drawing text
  CD3DFont* m_pFont;
  CD3DFont* m_pFontSmall;

  // Scene
  CUserControlledCamera m_Camera;

  // Mouse Input
  CInputManager m_InputManager;

  // teapot
  CD3DMesh *m_pTeapot;
  LPDIRECT3DTEXTURE8 m_pShadeTexture;
	
  D3DXMATRIX m_matProj;

  DWORD m_dwShader;
  std::string m_strTextureDesc;

  std::vector<CShadeTextureEntry> m_ShadeTextures;
  int m_iShadeTexture;
	
protected:
  HRESULT OneTimeSceneInit();
  HRESULT InitDeviceObjects();
  HRESULT RestoreDeviceObjects();
  HRESULT InvalidateDeviceObjects();
  HRESULT DeleteDeviceObjects();
  HRESULT FinalCleanup();
  HRESULT Render();
  HRESULT FrameMove();
  HRESULT ConfirmDevice( D3DCAPS8* pCaps, DWORD dwBehavior, D3DFORMAT Format );
  LRESULT MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
  
  HRESULT SetMeshColor(LPDIRECT3DVERTEXBUFFER8 pVB, 
    int iNumVerts, D3DXCOLOR color);
  void SwitchTextures();

  void ProcessInput();

public:
  CMyD3DApplication();
};

//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: Entry point to the program. Initializes everything, and goes into a
//       message-processing loop. Idle time is used to render the scene.
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
  CMyD3DApplication d3dApp;

  if( FAILED( d3dApp.Create( hInst ) ) )
    return 0;

  return d3dApp.Run();
}

//-----------------------------------------------------------------------------
// Name: CMyD3DApplication()
// Desc: Application constructor. Sets attributes for the app.
//-----------------------------------------------------------------------------
CMyD3DApplication::CMyD3DApplication()
{
  m_strWindowTitle    = _T("Ch24p1_ToonShader");
  m_bUseDepthBuffer   = TRUE;
  m_pFont            = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
  m_pFontSmall       = new CD3DFont( _T("Arial"),  9, D3DFONT_BOLD );
  m_pShadeTexture = NULL;
}

//-----------------------------------------------------------------------------
// Name: OneTimeSceneInit()
// Desc: Called during initial app startup, this function performs all the
//       permanent initialization.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::OneTimeSceneInit()
{
  return S_OK;
}

//-----------------------------------------------------------------------------
// Name: FrameMove()
// Desc: Called once per frame, the call is the entry point for animating
//       the scene.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::FrameMove()
{
	if (m_fElapsedTime < 0.0001f) {
		m_fElapsedTime = 0.0001f;
	}
	
  if (m_fElapsedTime > 0.005f) {
		m_fElapsedTime = 0.005f;
    m_fTime -= m_fElapsedTime - 0.005f;
	}
	ProcessInput();
	CTimer::UpdateAll(m_fElapsedTime);
	return S_OK;
}

void CMyD3DApplication::ProcessInput()
{

  float fSpeed = 100.0f * m_fElapsedTime;
  unsigned char m_bKey[256];
  ZeroMemory( m_bKey, 256 );
  GetKeyboardState(m_bKey);
  // Process keyboard input
  if(m_bKey['D'] & 128) m_Camera.AddToVelocity(D3DXVECTOR3(fSpeed, 0.0f, 0.0f)); // Slide Right
  if(m_bKey['A'] & 128) m_Camera.AddToVelocity(D3DXVECTOR3(-fSpeed, 0.0f, 0.0f));// Slide Left
  if(m_bKey['Q'] & 128) m_Camera.AddToVelocity(D3DXVECTOR3(0.0f, fSpeed, 0.0f)); // Slide Up
  if(m_bKey['Z'] & 128) m_Camera.AddToVelocity(D3DXVECTOR3(0.0f, -fSpeed, 0.0f));// Slide Down
  if(m_bKey['W'] & 128) m_Camera.AddToVelocity(D3DXVECTOR3(0.0f, 0.0f, fSpeed)); // Slide Foward
  if(m_bKey['S'] & 128) m_Camera.AddToVelocity(D3DXVECTOR3(0.0f, 0.0f, -fSpeed));// Slide Back
  if(m_bKey['L'] & 128) m_Camera.AddToYawPitchRoll(fSpeed, 0.0f, 0.0f);  // Turn Right
  if(m_bKey['J'] & 128) m_Camera.AddToYawPitchRoll(-fSpeed, 0.0f, 0.0f); // Turn Left
  if(m_bKey['K'] & 128) m_Camera.AddToYawPitchRoll(0.0f, fSpeed, 0.0f); // Turn Down
  if(m_bKey['I'] & 128) m_Camera.AddToYawPitchRoll(0.0f, -fSpeed, 0.0f);// Turn Up

  // mouse look
  DIMOUSESTATE2 dims2;
  m_InputManager.ReadMouse(dims2);

  // play with the divisor constants to change the mouselook sensitivity.
  // I've found that these values most accurately simulate my beloved Q3A setup. :)
  m_Camera.AddToYawPitchRoll(
    (float)dims2.lX*2000.0f*m_fElapsedTime, 
    (float)dims2.lY*2000.0f*m_fElapsedTime, 0.0f);
  
}

//-----------------------------------------------------------------------------
// 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()
{
  
  D3DXMATRIX matWorld;
  D3DXMatrixIdentity( &matWorld );
  m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );

  
  m_Camera.Update(m_fElapsedTime);

  m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE );
  m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
  
  // Clear the backbuffer
  m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
                       0xB0B0B0, 1.0f, 0L );
  
  // Now that our fire texture's updated, we can begin rendering the scene
  if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
  {
    // draw teapot
    D3DXMatrixIdentity(&matWorld);
    
    // Set up the vertex shader constants
    {
      D3DXVECTOR4 vLightDir(0.0f, 0.0f, -1.0f, 0.0f);
	    D3DXVec4Normalize(&vLightDir, &vLightDir);
	    
	    m_pd3dDevice->SetVertexShaderConstant(4, &vLightDir, 1);
	    
	    D3DXMATRIX matWorldViewProj;
      D3DXMATRIX matWorldView, matWorldViewInverse;

      matWorldViewProj = matWorld * m_Camera.GetViewMatrix() * m_matProj;
      matWorldView = matWorld * m_Camera.GetViewMatrix();
      D3DXMatrixInverse(&matWorldViewInverse, NULL, &matWorldView);
      D3DXMatrixTranspose(&matWorldViewProj, &matWorldViewProj);
      
	  	m_pd3dDevice->SetVertexShaderConstant(0, &matWorldViewProj, 4);
	    m_pd3dDevice->SetVertexShaderConstant(5, &matWorldViewInverse, 4);
    }
 
    // set up texture blending modes
    m_pd3dDevice->SetTexture(0, m_pShadeTexture);
	  

⌨️ 快捷键说明

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