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

📄 cubemap.cpp

📁 一个高手写的3D游戏
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#define STRICT
#include <tchar.h>
#include <math.h>
#include <stdio.h>
#include <D3DX8.h>
#include <dmusicc.h>
#include <dmusici.h>
#include <dxerr8.h>
#include "D3DApp.h"
#include "D3DFile.h"
#include "D3DFont.h"
#include "D3DUtil.h"
#include "DXUtil.h"
#include "DMUtil.h"
//#include "MySound.h"


#define CUBEMAP_RESOLUTION 256

//-----------------------------------------------------------------------------
// 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.
//-----------------------------------------------------------------------------
D3DXVECTOR3 VDot,VAtPoint,VUp;
float alpha = 0;
float jumpskip[20] = {  0.95,  0.85,  0.75,  0.65,  0.55,   0.45,  0.35,  0.25,  0.15, 0.05,
                      -0.05, -0.15, -0.25, -0.35, -0.45, -0.55, -0.65, -0.75, -0.85, -0.95, };

class CMyD3DApplication : public CD3DApplication
{

    D3DXMATRIX m_matProject;
    D3DXMATRIX m_matView;
	D3DXMATRIX m_AirplaneView;
    D3DXMATRIX m_matWorld;
	D3DXMATRIX m_matAirplane;

    CD3DFont* m_pFont;
	LPD3DXMESH  m_pSkyBox;
	CD3DMesh* m_pAirplane;

	CMusicManager*     g_pMusicManager;
	CMusicSegment*     g_pMusicSegment;

//	MySound  DemoMusic;

	D3DLIGHT8 m_light;
	bool jumpping;
	int jumpint;
	int delay ;


	LPDIRECT3DTEXTURE8*     g_pTexture;
	LPDIRECT3DTEXTURE8*     g_pTexture2;

	D3DMATERIAL8*           g_pMeshMaterials ; // Materials for our mesh
	LPDIRECT3DTEXTURE8*     g_pMeshTextures  ; // Textures for our mesh
	DWORD                   g_dwNumMaterials ;   // Number of mesh materials
    

    //IDirect3DCubeTexture8* m_pCubeMap;

protected:
    HRESULT RenderScene( CONST D3DXMATRIX* pView, CONST D3DXMATRIX* pProject, BOOL bRenderTeapot );

    HRESULT ConfirmDevice( D3DCAPS8*, DWORD, D3DFORMAT );
    HRESULT OneTimeSceneInit();
    HRESULT InitDeviceObjects();
    HRESULT RestoreDeviceObjects();
    HRESULT InvalidateDeviceObjects();
    HRESULT DeleteDeviceObjects();
    HRESULT Render();
	void RenderSkyBox();
	HRESULT SetFVF( LPDIRECT3DDEVICE8 pd3dDevice, DWORD dwFVF );
    HRESULT FrameMove();
    HRESULT FinalCleanup();
	HRESULT TurnView(WORD dir, float u);
	void    SetView();
	void    Jump();
	void    SetupPixelFog(DWORD Color, DWORD Mode);
	void    InitMusic(HWND hWnd);
	void    LoadSegmentFile( HWND hDlg, TCHAR* strFileName);
	void    PlayMusic(HWND hDlg, BOOL bLooped);
	void    StopMusic();



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 person shot Demo");
    m_bUseDepthBuffer   = TRUE;

    m_pFont             = NULL;
    m_pSkyBox           = NULL;
	m_pAirplane         = NULL;
	g_pTexture        = NULL;
    g_pTexture2       =NULL;

	g_pMusicManager = NULL;
	g_pMusicSegment = NULL;

    jumpping = false;
	jumpint = 0;
	delay = 0;
	//m_pCubeMap          = NULL;
}

void CMyD3DApplication::RenderSkyBox()
{
	SetupPixelFog(0x171515, D3DFOG_EXP2);

	for( DWORD ii=0; ii < g_dwNumMaterials; ii++ )
		{
			m_pd3dDevice->SetMaterial( &g_pMeshMaterials[ii] );
			//DWORD last = g_dwNumMaterials - 1;
			//if(ii == 0)
			//	m_pd3dDevice->SetTexture( 0,   * g_pTexture);
			//else
			m_pd3dDevice->SetTexture( 0, g_pMeshTextures[ii] );
			//m_pd3dDevice->SetTexture(0, *g_pTexture);
			m_pd3dDevice->SetTexture( 1,   * g_pTexture2 );
				
			m_pSkyBox->DrawSubset( ii );
		}
	m_pd3dDevice->SetRenderState(D3DRS_FOGENABLE, FALSE);
 
	
}

HRESULT CMyD3DApplication::SetFVF( LPDIRECT3DDEVICE8 pd3dDevice, DWORD dwFVF )
{
    LPD3DXMESH pTempSysMemMesh = NULL;

    if( m_pSkyBox )
    {
        if( FAILED( m_pSkyBox->CloneMeshFVF( 0L, dwFVF,
                                                 pd3dDevice, &pTempSysMemMesh ) ) )
            return E_FAIL;
    }
    
    SAFE_RELEASE( m_pSkyBox );
    
    if( pTempSysMemMesh ) m_pSkyBox = pTempSysMemMesh;
    
    // Compute normals in case the meshes have them
    if( m_pSkyBox )
        D3DXComputeNormals( m_pSkyBox, NULL );


    
    return S_OK;
}



//-----------------------------------------------------------------------------
// Name: OneTimeSceneInit()
// Desc: Called during initial app startup, this function performs all the
//       permanent initialization.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::OneTimeSceneInit()
{
    D3DXMatrixIdentity( &m_matWorld );
	m_pAirplane    = new CD3DMesh();
    m_pFont        = new CD3DFont( _T("Arial"), 9, D3DFONT_BOLD );
    //m_pAirplane    = new CD3DMesh();

	VDot=D3DXVECTOR3(0.0f,0.0f,-6.0f);
	VAtPoint=D3DXVECTOR3(0.0f,0.0f,-3.0f);
	VUp=D3DXVECTOR3(0.0f,1.0f,0.0f);
	D3DXMatrixLookAtLH(&m_matView,&VDot,&VAtPoint,&VUp);

	D3DXMatrixTranslation( &m_AirplaneView, -1.0f, -3.0f, 2.0f );
//	DemoMusic.InitMySound();
//	DemoMusic.PlayMySound();
	//m_AirplaneView = m_matView;
	InitMusic(m_hWnd);
	PlayMusic(m_hWnd, TRUE);

    return S_OK;
}




//-----------------------------------------------------------------------------
// Name: FrameMove()
// Desc: Called once per frame, the call is the entry point for animating
//       the scene.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::FrameMove()
{

	
	alpha++;
	if(alpha > 360.0f)
		alpha = 0.0f;

    D3DXMATRIX  mat;
    D3DXMatrixScaling( &m_matAirplane, 0.02f, 0.02f, 0.02f );
    D3DXMatrixTranslation( &mat, 1.0f, 2.0f, -0.20f );
    D3DXMatrixMultiply( &m_matAirplane, &m_matAirplane, &mat );
    //D3DXMatrixRotationX( &mat, -2.9f * m_fTime );
    //D3DXMatrixMultiply( &m_matAirplane, &m_matAirplane, &mat );
    //D3DXMatrixRotationY( &mat, 1.055f * m_fTime );
    //D3DXMatrixMultiply( &m_matAirplane, &m_matAirplane, &mat );

	D3DXMatrixLookAtLH(&m_matView,&VDot,&VAtPoint,&VUp);
	 // keep texture coordinates in a reasonable range



	ZeroMemory( &m_light, sizeof(m_light) );
    //m_light.Type = D3DLIGHT_DIRECTIONAL;
	m_light.Type = D3DLIGHT_POINT        ;
    DWORD dwCaps = m_d3dCaps.VertexProcessingCaps;
    if( 0 == ( dwCaps & D3DVTXPCAPS_POSITIONALLIGHTS ) )
    {
        if( m_light.Type == D3DLIGHT_POINT || m_light.Type == D3DLIGHT_SPOT )
            m_light.Type = D3DLIGHT_DIRECTIONAL;
    }
	FLOAT x = sinf( m_fTime*2.000f );
    FLOAT y = sinf( m_fTime*2.246f );
    FLOAT z = sinf( m_fTime*2.640f );
	
	//VDot,VAtPoint,VUp;
	//FLOAT x = VDot.x;
	//FLOAT y = VDot.y;
	//FLOAT z = VDot.z;

	m_light.Diffuse.r  = 0.5f + 0.5f * x;
    m_light.Diffuse.g  = 0.5f + 0.5f * y;
    m_light.Diffuse.b  = 0.5f + 0.5f * z;
    
	m_light.Diffuse.r  = 1.0f;
    m_light.Diffuse.g  = 0.5f ;
    m_light.Diffuse.b  = 0.85f ;
    m_light.Range      = 300.0f;
	
	switch( m_light.Type )
    {
        case D3DLIGHT_POINT:
            m_light.Position     = 6.5f * D3DXVECTOR3( x, y, z );
            m_light.Attenuation1 = 0.4f;
            break;
        case D3DLIGHT_DIRECTIONAL:
            m_light.Direction    = D3DXVECTOR3( x, y, z );
            break;
        case D3DLIGHT_SPOT:
            m_light.Position     = 2.0f * D3DXVECTOR3( x, y, z );
            //m_light.Position     = D3DXVECTOR3( x, y, z );
			m_light.Direction    =  D3DXVECTOR3( x, y, z );
            m_light.Theta        = 0.05f;
            m_light.Phi          = 1.0f;
            m_light.Falloff      = 1.0f;
            m_light.Attenuation0 = 1.0f;
    }
    m_pd3dDevice->SetLight( 0, &m_light );


	
		D3DXVECTOR3 NorVUp;
		D3DXVec3Normalize(&NorVUp, &VUp);
		
		
		if (jumpping)
		{
			delay ++;
			if (delay == 5)
			{
				delay = 0;
				VDot.y += jumpskip[jumpint];
				VAtPoint.y += jumpskip[jumpint++];
				if (jumpint == 20)
				{
					jumpint = 0;
					jumpping = false;
				}
				SetView();
			}

		}


    return S_OK;
}






//-----------------------------------------------------------------------------
// Name: RenderScene()
// Desc: Renders all visual elements in the scene. This is called by the main
//       Render() function, and also by the RenderIntoCubeMap() function.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::RenderScene( CONST D3DXMATRIX *pView, CONST D3DXMATRIX *pProject, 
                                        BOOL bRenderTeapot )
{
    // Render the Skybox
    {
        D3DXMATRIX matWorld;
        D3DXMatrixScaling( &matWorld, 2.0f, 2.0f, 2.0f );

        D3DXMATRIX matView(*pView);
        matView._41 = matView._42 = matView._43 = 0.0f;
	/*	
		m_pd3dDevice->SetTransform( D3DTS_WORLD, &m_matAirplane );
        m_pd3dDevice->SetTransform( D3DTS_VIEW, &m_AirplaneView );
        m_pd3dDevice->SetTransform( D3DTS_PROJECTION, pProject );
		m_pAirplane->Render( m_pd3dDevice );
*/
        m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
        m_pd3dDevice->SetTransform( D3DTS_VIEW, &m_matView );
        m_pd3dDevice->SetTransform( D3DTS_PROJECTION, pProject );

        /*m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
        m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_SELECTARG1 );
        m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
        m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
        m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSU,  D3DTADDRESS_MIRROR );
        m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSV,  D3DTADDRESS_MIRROR );

*/

   m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
   m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_SELECTARG1 );
   m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
   m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_SELECTARG1 );

 /*  m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG1, D3DTA_TEXTURE );
   m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP,   D3DTOP_MODULATE2X );
   m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP,   D3DTOP_MODULATE );
   m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG2, D3DTA_CURRENT );
   m_pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );
*/
        // Always pass Z-test, so we can avoid clearing color and depth buffers
        m_pd3dDevice->SetRenderState( D3DRS_ZFUNC, D3DCMP_ALWAYS );
        //m_pSkyBox->Render( m_pd3dDevice );
        RenderSkyBox();
		m_pd3dDevice->SetRenderState( D3DRS_ZFUNC, D3DCMP_LESSEQUAL );
    }


    {
        m_pd3dDevice->SetTransform( D3DTS_WORLD, &m_matAirplane );
        m_pd3dDevice->SetTransform( D3DTS_VIEW, &m_AirplaneView );
        m_pd3dDevice->SetTransform( D3DTS_PROJECTION, pProject );

        m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
        m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_SELECTARG1 );
        m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
        m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
        m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSU,  D3DTADDRESS_WRAP );
        m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSV,  D3DTADDRESS_WRAP );

        m_pAirplane->Render( m_pd3dDevice );

        m_pd3dDevice->SetTransform( D3DTS_WORLD, &m_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()
{

	m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,0x000000ff, 1.0f, 0L ); 
    m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
    m_pd3dDevice->LightEnable( 0, TRUE );
    
	// Begin the scene
    if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
    {
        RenderScene( &m_matView, &m_matProject, TRUE );

        // Output statistics
		m_pFont->DrawText( 2,  0, D3DCOLOR_ARGB(255,255,255,0), "Z3M4L 's FPS Demo" );
        m_pFont->DrawText( 2,  20, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats );
        m_pFont->DrawText( 2, 40, D3DCOLOR_ARGB(255,255,255,0), "Alt + Enter turn to FullScreen, 'w, s, a, d ' key to move and arrow key to turn around  " );

        // End the scene.
        m_pd3dDevice->EndScene();

⌨️ 快捷键说明

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