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

📄 multitexture.cpp

📁 3d 游戏编程入门教程之例子源码--飞机绕茶壶
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//-----------------------------------------------------------------------------
// File: multitexture.cpp
//
// Desc: Example code showing how to use a vertex structure with a vertex buffer
//       and how to map a texture.
//
// Last modification: December 25th, 2002
//
// Copyright (c) Wolfgang F. Engel (wolf@direct3d.net)
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#define STRICT
#include <Windows.h>
#include <commctrl.h>
#include <stdio.h>
#include <math.h>
#include <D3DX9.h>
#include "DXUtil.h"
#include "D3DEnumeration.h"
#include "D3DSettings.h"
#include "D3DApp.h"
#include "D3DFile.h"
#include "D3DFont.h"
#include "D3DUtil.h"
#include "resource.h"


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

// Fixed-Function Vertex structure
const DWORD FVF = (D3DFVF_XYZRHW | D3DFVF_TEX1);

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

// cube FVF
const DWORD 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;
    
	LPDIRECT3DVERTEXBUFFER9 m_pVB; // Buffer to hold vertices
    DWORD m_dwSizeofVertices;
    LPDIRECT3DTEXTURE9      m_pBackgroundTexture;

	LPDIRECT3DVERTEXBUFFER9 m_pCubeVB; // Buffer to hold vertices
    DWORD m_dwSizeofCubeVertices;
    LPDIRECT3DINDEXBUFFER9  m_pCubeIB;
    DWORD m_dwSizeofCubeIndices;

    LPDIRECT3DTEXTURE9      m_pWallTexture;
    LPDIRECT3DTEXTURE9      m_pEnvTexture;
    LPDIRECT3DTEXTURE9      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];


protected:
    HRESULT OneTimeSceneInit();
    HRESULT InitDeviceObjects();
    HRESULT RestoreDeviceObjects();
    HRESULT InvalidateDeviceObjects();
    HRESULT DeleteDeviceObjects();
    HRESULT FinalCleanup();
    HRESULT Render();
    HRESULT FrameMove();
    HRESULT ConfirmDevice( D3DCAPS9* pCaps, DWORD dwBehavior, 
		D3DFORMAT adapterFormat, D3DFORMAT backBufferFormat );
    LRESULT MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );

    HRESULT CreateCube( CUBEVERTEX* pVertices, WORD* pIndices );
	HRESULT SetLights();

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;

    InitCommonControls();
    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_d3dEnumeration.AppUsesDepthBuffer = FALSE;
    m_dwCreationWidth		= 800;   // Width used to create window
    m_dwCreationHeight		= 600;
	m_pVB = 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;

    // 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 );
	    m_pd3dDevice->SetTextureStageState( 3, D3DTSS_COLOROP,   D3DTOP_DISABLE );
		m_pd3dDevice->SetTextureStageState( 3, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );
	    m_pd3dDevice->SetTextureStageState( 4, D3DTSS_COLOROP,   D3DTOP_DISABLE );
		m_pd3dDevice->SetTextureStageState( 4, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );
	    m_pd3dDevice->SetTextureStageState( 5, D3DTSS_COLOROP,   D3DTOP_DISABLE );
		m_pd3dDevice->SetTextureStageState( 5, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );
	    m_pd3dDevice->SetTextureStageState( 6, D3DTSS_COLOROP,   D3DTOP_DISABLE );
		m_pd3dDevice->SetTextureStageState( 6, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );
	    m_pd3dDevice->SetTextureStageState( 7, D3DTSS_COLOROP,   D3DTOP_DISABLE );
		m_pd3dDevice->SetTextureStageState( 7, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );



		// switch to clamp texture addressing mode
		if (m_bAdd1 == TRUE)
		{
			m_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSU,  D3DTADDRESS_CLAMP);
			m_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSV,  D3DTADDRESS_CLAMP);
			m_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSW, D3DTADDRESS_CLAMP );
		}

		// mirroronce
		if (m_bAdd2 == TRUE)
		{
			m_pd3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_MIRRORONCE);
			m_pd3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_MIRRORONCE);
			m_pd3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSW, D3DTADDRESS_MIRRORONCE);
		}

		// border color
		if (m_bAdd3 == TRUE)
		{
			m_pd3dDevice->SetSamplerState (0, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER);
			m_pd3dDevice->SetSamplerState (0, D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER);
			m_pd3dDevice->SetSamplerState( 0, D3DSAMP_BORDERCOLOR, 0x00000000);
		}

		// Set up texture stage states for the background
		m_pd3dDevice->SetTexture( 0, m_pBackgroundTexture );
		m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
		m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_SELECTARG1 );

		// Passing an FVF to IDirect3DDevice9::SetFVF specifies a legacy FVF with stream 0.
	    m_pd3dDevice->SetFVF(FVF ); 
		m_pd3dDevice->SetStreamSource( 0, m_pVB, 0, sizeof(VERTEX) );
	    m_pd3dDevice->DrawPrimitive (D3DPT_TRIANGLESTRIP, 0, 2);

		/*******************************************
		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);

⌨️ 快捷键说明

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