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

📄 1steps.cpp

📁 一的3d游戏开发的源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//-----------------------------------------------------------------------------
// File: steps.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
{
    D3DXVECTOR3 p;
    D3DXVECTOR3 n;
    FLOAT       tu, tv;
};

#define D3DFVF_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
    FLOAT       fR, fG, fB;    // Color of the object
    D3DMATRIX   matLocal;
};

//-----------------------------------------------------------------------------
// 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;
	LPDIRECT3DVERTEXBUFFER8 m_pVB; // Buffer to hold vertices
    DWORD m_dwSizeofVertices;
    LPDIRECT3DINDEXBUFFER8  m_pIB;
    DWORD m_dwSizeofIndices;

	FLOAT m_fStartTimeKey,     // Time reference for calculations
		  m_fTimeElapsed;

    CUSTOMVERTEX  m_pvObjectVertices[16];
    WORD       m_pwObjectIndices[30];
    Object     m_pObjects[2];

    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_pFont = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
    ZeroMemory( m_bKey, 256 );

}

//-----------------------------------------------------------------------------
// Name: OneTimeSceneInit()
// Desc: Called during initial app startup, this function performs all the
//       permanent initialization.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::OneTimeSceneInit()
{
    // Points and normals which make up a object geometry
    D3DXVECTOR3 p1 = D3DXVECTOR3( 0.00f, 0.00f, 0.50f );
    D3DXVECTOR3 p2 = D3DXVECTOR3( 0.50f, 0.00f,-0.50f );
    D3DXVECTOR3 p3 = D3DXVECTOR3( 0.15f, 0.15f,-0.35f );
    D3DXVECTOR3 p4 = D3DXVECTOR3(-0.15f, 0.15f,-0.35f );
    D3DXVECTOR3 p5 = D3DXVECTOR3( 0.15f,-0.15f,-0.35f );
    D3DXVECTOR3 p6 = D3DXVECTOR3(-0.15f,-0.15f,-0.35f );
    D3DXVECTOR3 p7 = D3DXVECTOR3(-0.50f, 0.00f,-0.50f );

    D3DXVECTOR3 n1 = D3DXVECTOR3( 0.2f, 1.0f, 0.0f );
    D3DXVec3Normalize(&n1, &n1);
    D3DXVECTOR3 n2 = D3DXVECTOR3( 0.1f, 1.0f, 0.0f );
    D3DXVec3Normalize(&n2, &n2);
    D3DXVECTOR3 n3 = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
    D3DXVec3Normalize(&n3, &n3);
    D3DXVECTOR3 n4 = D3DXVECTOR3(-0.1f, 1.0f, 0.0f );
    D3DXVec3Normalize(&n4, &n4);
    D3DXVECTOR3 n5 = D3DXVECTOR3(-0.2f, 1.0f, 0.0f );
    D3DXVec3Normalize(&n5, &n5);
    D3DXVECTOR3 n6 = D3DXVECTOR3(-0.4f, 0.0f, -1.0f );
    D3DXVec3Normalize(&n6, &n6);
    D3DXVECTOR3 n7 = D3DXVECTOR3(-0.2f, 0.0f, -1.0f );
    D3DXVec3Normalize(&n7, &n7);
    D3DXVECTOR3 n8 = D3DXVECTOR3( 0.2f, 0.0f, -1.0f );
    D3DXVec3Normalize(&n8, &n8);
    D3DXVECTOR3 n9 = D3DXVECTOR3( 0.4f, 0.0f, -1.0f );
    D3DXVec3Normalize(&n9, &n9);

	// vertices for the top
	m_pvObjectVertices[0].p = p1;
	m_pvObjectVertices[0].n = n1;
	m_pvObjectVertices[0].tu = 0.0f;
	m_pvObjectVertices[0].tv = 0.5f;
	m_pvObjectVertices[1].p = p2;
	m_pvObjectVertices[1].n = n2;
	m_pvObjectVertices[1].tu = 0.5f;
	m_pvObjectVertices[1].tv = 1.0f;
	m_pvObjectVertices[2].p = p3;
	m_pvObjectVertices[2].n = n3;
	m_pvObjectVertices[2].tu = 0.425f;
	m_pvObjectVertices[2].tv = 0.575f;
	m_pvObjectVertices[3].p = p4;
	m_pvObjectVertices[3].n = n4;
	m_pvObjectVertices[3].tu = 0.425f;
	m_pvObjectVertices[3].tv = 0.425f;
    m_pvObjectVertices[4].p = p7;
	m_pvObjectVertices[4].n = n5;
	m_pvObjectVertices[4].tu = 0.5f;
	m_pvObjectVertices[4].tv = 0.0f;

	// vertices for the bottom
	m_pvObjectVertices[5].p = p1;
	m_pvObjectVertices[5].n = -n5;
	m_pvObjectVertices[5].tu = 1.0f;
	m_pvObjectVertices[5].tv = 0.5f;
	m_pvObjectVertices[6].p = p2;
	m_pvObjectVertices[6].n = -n4;
	m_pvObjectVertices[6].tu = 0.5f;
	m_pvObjectVertices[6].tv = 1.0f;
	m_pvObjectVertices[7].p = p5;
	m_pvObjectVertices[7].n = -n3;
	m_pvObjectVertices[7].tu = 0.575f;
	m_pvObjectVertices[7].tv = 0.575f;
	m_pvObjectVertices[8].p = p6;
	m_pvObjectVertices[8].n = -n2;
	m_pvObjectVertices[8].tu = 0.575f;
	m_pvObjectVertices[8].tv = 0.425f;
	m_pvObjectVertices[9].p = p7;
	m_pvObjectVertices[9].n = -n1;
	m_pvObjectVertices[9].tu = 0.5f;
	m_pvObjectVertices[9].tv = 0.0f;

	// vertices for the rear
	m_pvObjectVertices[10].p = p2;
	m_pvObjectVertices[10].n = n6;
	m_pvObjectVertices[10].tu = 0.5f;
	m_pvObjectVertices[10].tv = 1.0f;
	m_pvObjectVertices[11].p = p3;
	m_pvObjectVertices[11].n = n7;
	m_pvObjectVertices[11].tu = 0.425f;
	m_pvObjectVertices[11].tv = 0.575f;
	m_pvObjectVertices[12].p = p4;
	m_pvObjectVertices[12].n = n8;
	m_pvObjectVertices[12].tu = 0.425f;
	m_pvObjectVertices[12].tv = 0.425f;
	m_pvObjectVertices[13].p = p7;
	m_pvObjectVertices[13].n = n9;
	m_pvObjectVertices[13].tu = 0.5f;
	m_pvObjectVertices[13].tv = 0.0f;
	m_pvObjectVertices[14].p = p6;
	m_pvObjectVertices[14].n = n8;
	m_pvObjectVertices[14].tu = 0.575f;
	m_pvObjectVertices[14].tv = 0.425f;
	m_pvObjectVertices[15].p = p5;
	m_pvObjectVertices[15].n = n7;
	m_pvObjectVertices[15].tu = 0.575f;
	m_pvObjectVertices[15].tv = 0.575f;
	

    // Vertex indices for the object
    m_pwObjectIndices[ 0] =  0; m_pwObjectIndices[ 1] =  1; m_pwObjectIndices[ 2] =  2;
    m_pwObjectIndices[ 3] =  0; m_pwObjectIndices[ 4] =  2; m_pwObjectIndices[ 5] =  3;
    m_pwObjectIndices[ 6] =  0; m_pwObjectIndices[ 7] =  3; m_pwObjectIndices[ 8] =  4;
    m_pwObjectIndices[ 9] =  5; m_pwObjectIndices[10] =  7; m_pwObjectIndices[11] =  6;
    m_pwObjectIndices[12] =  5; m_pwObjectIndices[13] =  8; m_pwObjectIndices[14] =  7;
    m_pwObjectIndices[15] =  5; m_pwObjectIndices[16] =  9; m_pwObjectIndices[17] =  8;
    m_pwObjectIndices[18] = 10; m_pwObjectIndices[19] = 15; m_pwObjectIndices[20] = 11;
    m_pwObjectIndices[21] = 11; m_pwObjectIndices[22] = 15; m_pwObjectIndices[23] = 12;
    m_pwObjectIndices[24] = 12; m_pwObjectIndices[25] = 15; m_pwObjectIndices[26] = 14;
    m_pwObjectIndices[27] = 12; m_pwObjectIndices[28] = 14; m_pwObjectIndices[29] = 13;

	// yellow object
	m_pObjects[0].vLoc   = D3DXVECTOR3(-1.0f, 0.0f, 0.0f);
    m_pObjects[0].vR.x   = 0.0f;
    m_pObjects[0].vR.y   = 0.0f;
    m_pObjects[0].vR.z   = 0.0f;
    m_pObjects[0].fR     = 1.0f;
    m_pObjects[0].fG     = 0.92f;
    m_pObjects[0].fB     = 0.0f;

	// red object
	m_pObjects[1].vLoc   = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
    m_pObjects[1].vR.x   = 0.0f;
    m_pObjects[1].vR.y   = 0.0f;
    m_pObjects[1].vR.z   = 0.0f;
    m_pObjects[1].fR     = 1.0f;
    m_pObjects[1].fG     = 0.0f;
    m_pObjects[1].fB     = 0.27f;

   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_pObjects[0].vR.z -= 1.0f * m_fTimeElapsed;
    if (m_bKey['L']) m_pObjects[0].vR.z += 1.0f * m_fTimeElapsed;
	if (m_bKey['I']) m_pObjects[0].vR.y -= 1.0f * m_fTimeElapsed;
    if (m_bKey['K']) m_pObjects[0].vR.y += 1.0f * m_fTimeElapsed;

    D3DXMATRIX matWorld;
	D3DXMatrixTranslation(&matWorld, m_pObjects[0].vLoc.x, m_pObjects[0].vLoc.y, m_pObjects[0].vLoc.z);

    D3DXMATRIX matTemp, matRotateX, matRotateY, matRotateZ;
    D3DXMatrixRotationY( &matRotateY, -m_pObjects[0].vR.x );
	D3DXMatrixRotationX( &matRotateX, -m_pObjects[0].vR.y );
	D3DXMatrixRotationZ( &matRotateZ, -m_pObjects[0].vR.z );

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

    m_pObjects[0].matLocal = matWorld;

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

	D3DXMatrixTranslation(&matWorld, m_pObjects[1].vLoc.x, m_pObjects[0].vLoc.y, m_pObjects[0].vLoc.z);
    

⌨️ 快捷键说明

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