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

📄 textures.cpp

📁 D3D做的天空盒地面加上了纹理混合!写的非常清晰
💻 CPP
字号:

#include <Windows.h>
#include <mmsystem.h>
#include <d3dx9.h>
#include "FloorDeck.h"
#include "Skey.h"
#pragma warning( disable : 4996 ) // disable deprecated warning 
#include <strsafe.h>
#pragma warning( default : 4996 ) 
#include <string>
using namespace std;

// Global variables
//-----------------------------------------------------------------------------
LPDIRECT3D9					g_pD3D       = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE9			g_pd3dDevice = NULL; // Our rendering device
LPDIRECT3DVERTEXBUFFER9		g_pVB        = NULL; // Buffer to hold vertices
//LPDIRECT3DTEXTURE9		g_pTexture   = NULL; // Our texture
LPDIRECT3DTEXTURE9			g_pWater[30];
CSkey						g_sKey;
CCamera						*g_pCamera;
int Number = 0;
CFloorDeck Fk;
HRESULT InitD3D( HWND hWnd )
{
    // Create the D3D object.
    if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
        return E_FAIL;

    // Set up the structure used to create the D3DDevice. Since we are now
    // using more complex geometry, we will create a device with a zbuffer.
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory( &d3dpp, sizeof(d3dpp) );
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
    d3dpp.EnableAutoDepthStencil = TRUE;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

    // Create the D3DDevice
    if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                      &d3dpp, &g_pd3dDevice ) ) )
    {
        return E_FAIL;
    }

    // Turn off culling
    g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );

    // Turn off D3D lighting
    g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );

    // Turn on the zbuffer
    g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );

	g_pCamera = new CCamera();
	g_pCamera->SetViewParams(D3DXVECTOR3(0.0f,10.0f,0.0f),
		D3DXVECTOR3(0.0f,0.0f,0.0f),
		D3DXVECTOR3(0.0f,2.0f,0.0f));
	Fk.InitTexture(g_pd3dDevice);
	Fk.ReadData();
	g_pCamera->SetProjParams(D3DX_PI/3.0f,4.0f/3.0f,5.0f,2500.0f);
	g_pCamera->SetMoveVelocity(60.0f);
	g_pCamera->ProcessKey(60.0f);
	g_sKey.InitSkyBox(g_pd3dDevice);

	//CT.UpdataTime(0.001f);

    return S_OK;
}

//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
VOID Cleanup()
{
   /* if( g_pTexture != NULL )
        g_pTexture->Release();*/

    if( g_pVB != NULL )
        g_pVB->Release();

    if( g_pd3dDevice != NULL )
        g_pd3dDevice->Release();

    if( g_pD3D != NULL )
        g_pD3D->Release();
}



//-----------------------------------------------------------------------------
// Name: SetupMatrices()
// Desc: Sets up the world, view, and projection transform matrices.
//-----------------------------------------------------------------------------
VOID SetupMatrices(float fElapsedTime)
{
// Set up world matrix
// D3DXMATRIXA16 matWorld;
// D3DXMatrixIdentity( &matWorld );
// D3DXMatrixRotationX( &matWorld, timeGetTime()/1000.0f );
// g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
// 
// D3DXVECTOR3 vEyePt( 0.0f, 3.0f,-5.0f );
//  D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );
// D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
// D3DXMATRIXA16 matView;
// D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
// g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
// 
// D3DXMATRIXA16 matProj;
// D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );
	//g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
	g_pCamera->ProcessKey(fElapsedTime);
	g_pCamera->Update();
}




//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID Render()
{
    // Clear the backbuffer and the zbuffer
    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
                         D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );


    // Begin the scene
    if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
    {
		static float fPreTime = static_cast<float>(timeGetTime());
		float fCurrentTime = static_cast<float>(timeGetTime());
		float fElapsedTime = (fCurrentTime - fPreTime)*0.01f;

        // Setup the world, view, and projection matrices
        SetupMatrices( fElapsedTime);
       
		g_pCamera->ApplyDevice(g_pd3dDevice);
		g_sKey.Render(*g_pCamera);
		Fk.Render();
		fPreTime = fCurrentTime;
        // End the scene
        g_pd3dDevice->EndScene();
    }

    // Present the backbuffer contents to the display
    g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}




//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    switch( msg )
    {
        case WM_DESTROY:
            Cleanup();
            PostQuitMessage( 0 );
            return 0;
    }

    return DefWindowProc( hWnd, msg, wParam, lParam );
}




//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
    // Register the window class
    WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
                      GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
                      "D3D Tutorial", NULL };
    RegisterClassEx( &wc );

    // Create the application's window
    HWND hWnd = CreateWindow( "D3D Tutorial", "D3D Tutorial 05: Textures",
                              WS_OVERLAPPEDWINDOW, 100, 100, 800, 600,
                              NULL, NULL, wc.hInstance, NULL );

    // Initialize Direct3D
    if( SUCCEEDED( InitD3D( hWnd ) ) )
    {
        {
            // Show the window
            ShowWindow( hWnd, SW_SHOWDEFAULT );
            UpdateWindow( hWnd );

            // Enter the message loop
            MSG msg;
            ZeroMemory( &msg, sizeof(msg) );
            while( msg.message!=WM_QUIT )
            {
				g_pCamera->HandleMessage(hWnd,msg.message,NULL,NULL);
                if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
                {
                    TranslateMessage( &msg );
                    DispatchMessage( &msg );
                }
                else
                    Render();
            }
        }
    }

    UnregisterClass( "D3D Tutorial", wc.hInstance );
    return 0;
}



⌨️ 快捷键说明

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