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

📄 xfiles.cpp

📁 Beginning Direct3D Game Programming源代码Part3chapter9
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//-----------------------------------------------------------------------------
// File: Xfiles.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 "D3DFile.h"
#include "D3DUtil.h"
#include "DXUtil.h"
#include "resource.h"

// A structure for our custom vertex type
struct CUSTOMVERTEX
{
    D3DXVECTOR3 p;
    D3DXVECTOR3 n;
    FLOAT       tu, tv;
};

#define FVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1)

//-----------------------------------------------------------------------------
// Name: struct Object
// Desc: Structure for holding data for each object
//-----------------------------------------------------------------------------
struct Object
{
    D3DXVECTOR3 vLoc;		// Location/Translation 
	D3DXVECTOR3	vR;			// Rotation vector
    D3DMATRIX   matLocal;	
    D3DMATERIAL8 mtrl;		// material data
    CD3DMesh* mesh;	// mesh data
};

//-----------------------------------------------------------------------------
// 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;
	Object m_oYellow;
	Object m_oRed;

	LPDIRECT3DVERTEXBUFFER8 m_pVB; // Buffers to hold vertices
    DWORD m_dwNumVertices;
	DWORD m_dwNumFaces;
    LPDIRECT3DINDEXBUFFER8  m_pIB;

	LPDIRECT3DVERTEXBUFFER8 m_pVB2; 
    DWORD m_dwNumVertices2;
	DWORD m_dwNumFaces2;
    LPDIRECT3DINDEXBUFFER8  m_pIB2;

	FLOAT m_fStartTimeKey,     // Time reference for calculations
		  m_fTimeElapsed;

    // Variables for determining view position
	D3DXVECTOR3	m_pvVelocity, 
				m_pvAngularVelocity, 
				m_pvPosition, 
				m_pvYPR;

	D3DXMATRIX m_matPosition, 
			   m_matView;

	FLOAT m_fAngularSpeed, 
		  m_fSpeed;

    BYTE m_bKey[256];			// keyboard state buffer

    HRESULT ConfirmDevice( D3DCAPS8*, DWORD, D3DFORMAT );

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

public:
    LRESULT MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
    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("First Steps to Animation");
    m_bUseDepthBuffer = TRUE;
	m_pVB = NULL;
	m_pIB = NULL;
	m_pVB2 = NULL;
	m_pIB2 = NULL;

    m_pFont = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
    m_oYellow.mesh = new CD3DMesh();
    m_oRed.mesh = new CD3DMesh();

	m_fAngularSpeed = 0.5;
	m_fSpeed= 1.0f;
	m_pvVelocity = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
	m_pvAngularVelocity = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
    
	ZeroMemory( m_bKey, 256 );
}

//-----------------------------------------------------------------------------
// Name: OneTimeSceneInit()
// Desc: Called during initial app startup, this function performs all the
//       permanent initialization.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::OneTimeSceneInit()
{
	// yellow object
	m_oYellow.vLoc   = D3DXVECTOR3(-1.0f, 0.0f, 0.0f);
    m_oYellow.vR.x   = 0.0f;
    m_oYellow.vR.y   = 0.0f;
    m_oYellow.vR.z   = 0.0f;


	// red object
	m_oRed.vLoc   = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
    m_oRed.vR.x   = 0.0f;
    m_oRed.vR.y   = 0.0f;
    m_oRed.vR.z   = 0.0f;

   return S_OK;
}

//-----------------------------------------------------------------------------
// Name: FrameMove()
// Desc: Called once per frame, the call is the entry point for animating
//       the scene.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::FrameMove()
{
	// timing code: 
	// the object should move/rotate in the same speed 
	// at every possible fps
	const cTimeScale = 5;
	
	// calculate elapsed time
	m_fTimeElapsed=(m_fTime-m_fStartTimeKey) * cTimeScale;
	
	// store last time
	m_fStartTimeKey=m_fTime;

	//**********************************************************
	// yellow object
	//**********************************************************
	if (m_bKey['J']) m_oYellow.vR.z -= m_fTimeElapsed;
    if (m_bKey['L']) m_oYellow.vR.z += m_fTimeElapsed;
	if (m_bKey['I']) m_oYellow.vR.y -= m_fTimeElapsed;
    if (m_bKey['K']) m_oYellow.vR.y += m_fTimeElapsed;

    D3DXMATRIX matWorld;
	D3DXMatrixTranslation(&matWorld, m_oYellow.vLoc.x, m_oYellow.vLoc.y, m_oYellow.vLoc.z);

    D3DXMATRIX matTemp, matRotateX, matRotateY, matRotateZ;
    D3DXMatrixRotationY( &matRotateY, -m_oYellow.vR.x );
	D3DXMatrixRotationX( &matRotateX, -m_oYellow.vR.y );
	D3DXMatrixRotationZ( &matRotateZ, -m_oYellow.vR.z );

	D3DXMatrixMultiply( &matTemp, &matRotateX, &matRotateY );
	D3DXMatrixMultiply( &matTemp, &matRotateZ, &matTemp );
	D3DXMatrixMultiply( &matWorld, &matTemp, &matWorld ); 

    m_oYellow.matLocal = matWorld;

	//**********************************************************
	// red object
	//**********************************************************
    if (m_bKey['D'])	m_oRed.vR.z -= m_fTimeElapsed;
    if (m_bKey['A'])	m_oRed.vR.z += m_fTimeElapsed;
	if (m_bKey['S'])	m_oRed.vR.y -= m_fTimeElapsed;
    if (m_bKey['W'])	m_oRed.vR.y += m_fTimeElapsed;

	D3DXMatrixTranslation(&matWorld, m_oRed.vLoc.x, m_oRed.vLoc.y, m_oRed.vLoc.z);
    
	D3DXMatrixRotationY( &matRotateY, -m_oRed.vR.x );
	D3DXMatrixRotationX( &matRotateX, -m_oRed.vR.y );
	D3DXMatrixRotationZ( &matRotateZ, -m_oRed.vR.z );
	D3DXMatrixMultiply( &matTemp, &matRotateX, &matRotateY );
	D3DXMatrixMultiply( &matTemp, &matRotateZ, &matTemp );
	D3DXMatrixMultiply( &matWorld, &matTemp, &matWorld ); 

    m_oRed.matLocal = matWorld;

	//************************************************************
	// camera stuff
	//************************************************************
    D3DXVECTOR3 vTrans(0.0f, 0.0f, 0.0f);
    D3DXVECTOR3 vRot(0.0f, 0.0f, 0.0f);

	// Process keyboard input
    if (m_bKey[VK_HOME])		vTrans.z += 0.2f; // Move Forward
    if (m_bKey[VK_END])			vTrans.z -= 0.2f; // Move Backward
    if (m_bKey[VK_NUMPAD4])		vTrans.x -= 0.1f; // Slide Left
    if (m_bKey[VK_NUMPAD6])		vTrans.x += 0.1f; // Slide Right
    if (m_bKey[VK_NUMPAD8])     vTrans.y += 0.1f; // Slide Down
    if (m_bKey[VK_NUMPAD2])     vTrans.y -= 0.1f; // Slide Up

	if (m_bKey[VK_UP])			vRot.y += 0.1f; // Pitch Up
    if (m_bKey[VK_DOWN])		vRot.y -= 0.1f; // Pitch Down
    if (m_bKey[VK_LEFT])	    vRot.x += 0.1f; // Turn Left
    if (m_bKey[VK_RIGHT])	    vRot.x -= 0.1f; // Turn Right
    if (m_bKey['C'])		    vRot.z += 0.1f; // Rotate Left
    if (m_bKey['X'])		    vRot.z -= 0.1f; // Rotate Right
	
	// turn cfSmooth to 0.98f
	const FLOAT cfSmooth = 0.9f;

	// transform and rotation velocity
    m_pvVelocity = m_pvVelocity * cfSmooth + vTrans;
    m_pvAngularVelocity = m_pvAngularVelocity * cfSmooth + vRot;

⌨️ 快捷键说明

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