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

📄 multitexture.cpp

📁 Beginning Direct3D Game Programming源代码Part2chapter8
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//-----------------------------------------------------------------------------
// File: basic.cpp
//
// Desc: D3D sample showing the basics of DirectX Graphics Programming
// 
// Copyright (c) 1998-2000 Microsoft Corporation. All rights reserved.
// Copyright (c) 1998-2001 wolf@direct3d.net
//-----------------------------------------------------------------------------
#define STRICT
#include <tchar.h>
#include <math.h>
#include <stdio.h>
#include <D3DX8.h>
#include "D3DApp.h"
#include "D3DFont.h"
#include "D3DUtil.h"
#include "DXUtil.h"
#include "resource.h"


// A structure for our custom vertex type
struct CUSTOMVERTEX
{
    FLOAT x, y, z, rhw; // The transformed position for the vertex
    FLOAT tu, tv;		// texture coordinates
};

// Our custom FVF, which describes our custom vertex structure
#define FVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_TEX1)

// cube vertex 
struct CUBEVERTEX
{
    D3DXVECTOR3 p;
    D3DXVECTOR3 n;
    FLOAT       tu, tv;
};

// cube FVF
#define FVF_CUBEVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1)

#define FILL_CUBEVERTEX( v, vertex, normal, atu, atv )  \
{   v.p.x = vertex.x; v.p.y = vertex.y; v.p.z = vertex.z; \
    v.n.x = normal.x; v.n.y = normal.y; v.n.z = normal.z; \
    v.tu = atu; v.tv = atv; \
}

#define NUM_CUBE_VERTICES (6*4)
#define NUM_CUBE_INDICES  (6*2*3)

//-----------------------------------------------------------------------------
// 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
{
    CD3DFont* m_pFont;
    CD3DFont* m_pFontSmall;

	LPDIRECT3DVERTEXBUFFER8 m_pVB; // Buffer to hold vertices
    DWORD m_dwSizeofVertices;
    LPDIRECT3DINDEXBUFFER8  m_pIB;
    DWORD m_dwSizeofIndices;

	LPDIRECT3DVERTEXBUFFER8 m_pCubeVB; // Buffer to hold vertices
    DWORD m_dwSizeofCubeVertices;
    LPDIRECT3DINDEXBUFFER8  m_pCubeIB;
    DWORD m_dwSizeofCubeIndices;

    LPDIRECT3DTEXTURE8      m_pBackgroundTexture;
    LPDIRECT3DTEXTURE8      m_pWallTexture;
    LPDIRECT3DTEXTURE8      m_pEnvTexture;
    LPDIRECT3DTEXTURE8      m_pDetailTexture;

	// texture switches
	BOOL m_bTex1, m_bTex2, m_bTex3, m_bTex4, m_bTex5, m_bTex6, m_bTex7, m_bTex8;
	BOOL m_bAdd1, m_bAdd2, m_bAdd3;
	BOOL m_bFil1, m_bFil2;
    BOOL m_bDrawHelp;


	// for the cube
    CUBEVERTEX m_pCubeVertices[NUM_CUBE_VERTICES];
    WORD      m_pCubeIndices[NUM_CUBE_INDICES];

    HRESULT ConfirmDevice( D3DCAPS8*, DWORD, D3DFORMAT );

protected:
    HRESULT OneTimeSceneInit();
    HRESULT InitDeviceObjects();
    HRESULT RestoreDeviceObjects();
    HRESULT InvalidateDeviceObjects();
    HRESULT DeleteDeviceObjects();
    HRESULT Render();
    HRESULT FrameMove();
    HRESULT FinalCleanup();

    HRESULT CreateCube( CUBEVERTEX* pVertices, WORD* pIndices );
	HRESULT SetLights();
public:
    CMyD3DApplication();
    LRESULT MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
};

//-----------------------------------------------------------------------------
// 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("Multitexture");
    m_bUseDepthBuffer = TRUE;
	m_pVB = NULL;
	m_pIB = NULL;
	m_pCubeVB = NULL;
	m_pCubeIB = NULL;
    m_pFont = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
    m_pFontSmall = new CD3DFont( _T("Arial"), 7, D3DFONT_BOLD );
	m_bTex1 = TRUE;
	m_bTex2 = m_bTex3 = m_bTex4 = m_bTex5 = m_bTex6 = m_bTex7 = m_bTex8 = FALSE;
	m_bFil1 = TRUE;
	m_bFil2 = FALSE;
	m_bAdd1 = TRUE;
	m_bAdd2 = m_bAdd3 = FALSE;

    m_bDrawHelp  = FALSE;
}

//-----------------------------------------------------------------------------
// 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()
{
    FLOAT z=0;

    // Setup the world matrix to spin and translate the object
    D3DXMATRIX matSpinY, matSpinX, matWorld;
    D3DXMatrixRotationY( &matSpinY, m_fTime / 0.5f );
    D3DXMatrixRotationX( &matSpinX, m_fTime / 0.3f );
    D3DXMatrixTranslation( &matWorld, 0.0f, 0.0f, z );
    D3DXMatrixMultiply( &matWorld, &matSpinY, &matWorld );
    D3DXMatrixMultiply( &matWorld, &matSpinX, &matWorld );
    
    m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
   return S_OK;
}

//-----------------------------------------------------------------------------
// 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()
{
	// animate darkmap
	static int i=0;

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

    // Begin the scene
    if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
    {
		m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_DISABLE );
		m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );
		m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP,   D3DTOP_DISABLE );
		m_pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );
	    m_pd3dDevice->SetTextureStageState( 2, D3DTSS_COLOROP,   D3DTOP_DISABLE );
		m_pd3dDevice->SetTextureStageState( 2, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );

		// switch to clamp texture addressing mode
		if (m_bAdd1 == TRUE)
		{
			m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSU,  D3DTADDRESS_CLAMP);
			m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSV,  D3DTADDRESS_CLAMP);
			m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSW, D3DTADDRESS_CLAMP );
		}

		// mirroronce
		if (m_bAdd2 == TRUE)
		{
			m_pd3dDevice->SetTextureStageState(0, D3DTSS_ADDRESSU, D3DTADDRESS_MIRRORONCE);
			m_pd3dDevice->SetTextureStageState(0, D3DTSS_ADDRESSV, D3DTADDRESS_MIRRORONCE);
			m_pd3dDevice->SetTextureStageState(0, D3DTSS_ADDRESSW, D3DTADDRESS_MIRRORONCE);
		}

		// border color
		if (m_bAdd3 == TRUE)
		{
			m_pd3dDevice->SetTextureStageState (0, D3DTSS_ADDRESSU, D3DTADDRESS_BORDER);
			m_pd3dDevice->SetTextureStageState (0, D3DTSS_ADDRESSV, D3DTADDRESS_BORDER);
			m_pd3dDevice->SetTextureStageState( 0, D3DTSS_BORDERCOLOR, 0x00000000);
		}

		m_pd3dDevice->SetTexture( 0, m_pBackgroundTexture );
		m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
		m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_SELECTARG1 );
		
		m_pd3dDevice->SetStreamSource( 0, m_pVB, sizeof(CUSTOMVERTEX) );
		m_pd3dDevice->SetVertexShader( FVF_CUSTOMVERTEX );
        m_pd3dDevice->SetIndices( m_pIB, 0 );
		m_pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 
											0, 
											4,  // number of vertices
                                            0, 
											2); // number of primitives

		/*******************************************
		Dark mapping
		*******************************************/
		if (m_bTex1 == TRUE)
		{
		  m_pd3dDevice->SetTexture( 0, m_pWallTexture); 
		  m_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0 );
		  m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
	 	  m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_SELECTARG1 );

		  m_pd3dDevice->SetTexture(1, m_pEnvTexture); 
		  m_pd3dDevice->SetTextureStageState( 1, D3DTSS_TEXCOORDINDEX, 0 );
		  m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG1, D3DTA_TEXTURE );
		  m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG2, D3DTA_CURRENT );
		  m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP,   D3DTOP_MODULATE );
		}

		/**************************************
		Animated Dark mapping
		***************************************/
		if (m_bTex2 == TRUE)
		{
		  // Set texture for the cube
		  m_pd3dDevice->SetTexture( 0, m_pWallTexture); 
		  m_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0 );
		  m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
		  m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); 

		  // Set darkmap
		  m_pd3dDevice->SetTexture(1, m_pEnvTexture); 
		  m_pd3dDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 0 );
		  m_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE );
          m_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT );  

		  // animate darkmap
		  if (i < 40)
		  {
			m_pd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE);
		  }
 		  else if (i < 80)
		  {
			m_pd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE2X);
		  }
		  else if (i < 120)
		  {
			m_pd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE4X);
		  }
		  else if (i = 120)
		  {
			i = 0;
		  }

		 i++;
		}

		/****************************************
		Blending the texture with material color
		*****************************************/
		if (m_bTex3 == TRUE)
		{

			// Set texture for the cube
			m_pd3dDevice->SetTexture( 0, m_pWallTexture); 
	  	    m_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0 );

⌨️ 快捷键说明

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