用DXUT写个Hello World级别的程序

DXUT

dxut是官方提供的一套Directx开发框架,它将一些没有技术含量的功能如窗口创建,设备创建,消息循环,顶点坐标转换,鼠标操作等封装起来,让我们专注于自己的核心代码,这样一来便娱乐了身心。
dxut具体提供了什么功能:

  • 简化了窗口、Direct3D设备创建
  • 声明设备事件
  • 在窗口模式和全屏模式间切换
  • 简单好用的计时器
  • 特别好用的摄像机类

每个功能对应的api

简化了窗口、Direct3D设备创建

    DXUTCreateWindow( L"EmptyProject" );//这行代码在WinMain中,根本不用改它
      DXUTCreateDevice( true, 640, 480 );//这行也是,有了框架,我好像没有用CreateDevice创建过设备对象!!

特别好用的摄像机类

CModelViewerCamera modelCamera;//能让茶壶动起来的东西 ,有了摄像机的好处就是
//可以通过鼠标来旋转物体,不需要你写矩阵了。

最重要的部分

最重要的当然是渲染了!!!当然前提是你缓冲区得要有数据!可以在OnD3D9CreateDevice中填充缓冲区


//--------------------------------------------------------------------------------------
// Render the scene using the D3D9 device
//--------------------------------------------------------------------------------------
void CALLBACK OnD3D9FrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
{
    HRESULT hr;
    // Clear the render target and the zbuffer 
    V( pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB( 0, 45, 50, 170 ), 1.0f, 0 ) );
    
	// Set world matrix
	D3DXMATRIX world = *modelCamera.GetWorldMatrix();
	pd3dDevice->SetTransform(D3DTS_WORLD, &world);

	// Set view matrix
	D3DXMATRIX view = *modelCamera.GetViewMatrix();
	pd3dDevice->SetTransform(D3DTS_VIEW, &view);

	// Set projection matrix
	D3DXMATRIX proj = *modelCamera.GetProjMatrix();
	pd3dDevice->SetTransform(D3DTS_PROJECTION, &proj);
    // Render the scene
    if( SUCCEEDED( pd3dDevice->BeginScene() ) )
    {
		mesh->DrawSubset(0);//render teapot
        V( pd3dDevice->EndScene() );
    }
}

你需要做的

在这里告诉计算机相机在哪?要往哪里看?

//--------------------------------------------------------------------------------------
// Create any D3D9 resources that will live through a device reset (D3DPOOL_MANAGED)
// and aren't tied to the back buffer size
//--------------------------------------------------------------------------------------
HRESULT CALLBACK OnD3D9CreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc,
                                     void* pUserContext )
{
	D3DXVECTOR3 eyePt(0.0f, 0.0f, -5.0f);//where eye
	D3DXVECTOR3 lookAt(0.0f, 0.0f, 0.0f);//looat  at  where
	modelCamera.SetViewParams(&eyePt, &lookAt);//有了这个camera后,之前定义的setupMatrix可以删了

	D3DXCreateTeapot(pd3dDevice, &mesh, NULL);//create teapot
    return S_OK;
}

告诉计算机投影参数,投影大小,这里基本上写死


//--------------------------------------------------------------------------------------
// Create any D3D9 resources that won't live through a device reset (D3DPOOL_DEFAULT) 
// or that are tied to the back buffer size 
//--------------------------------------------------------------------------------------
HRESULT CALLBACK OnD3D9ResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc,
                                    void* pUserContext )
{
	float fAspectRatio = pBackBufferSurfaceDesc->Width / (FLOAT)pBackBufferSurfaceDesc->Height;
	modelCamera.SetProjParams(D3DX_PI / 4, fAspectRatio, 0.1f, 1000.0f);
	modelCamera.SetWindow(pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height);
    return S_OK;
}

完整的程序

//--------------------------------------------------------------------------------------
// File: EmptyProject.cpp
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#include "DXUT.h"
#include "resource.h"
#include "DXUTcamera.h"
//====================================
//全局变量
//===================================
ID3DXMesh *mesh = 0;//mesh pointeer for teapot
CModelViewerCamera modelCamera;//能让茶壶动起来的东西



//--------------------------------------------------------------------------------------
// Rejects any D3D9 devices that aren't acceptable to the app by returning false
//--------------------------------------------------------------------------------------
bool CALLBACK IsD3D9DeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat, D3DFORMAT BackBufferFormat,
                                      bool bWindowed, void* pUserContext )
{
    // Typically want to skip back buffer formats that don't support alpha blending
    IDirect3D9* pD3D = DXUTGetD3D9Object();
    if( FAILED( pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal, pCaps->DeviceType,
                                         AdapterFormat, D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING,
                                         D3DRTYPE_TEXTURE, BackBufferFormat ) ) )
        return false;

    return true;
}


//--------------------------------------------------------------------------------------
// Before a device is created, modify the device settings as needed
//--------------------------------------------------------------------------------------
bool CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, void* pUserContext )
{
    return true;
}


//--------------------------------------------------------------------------------------
// Create any D3D9 resources that will live through a device reset (D3DPOOL_MANAGED)
// and aren't tied to the back buffer size
//--------------------------------------------------------------------------------------
HRESULT CALLBACK OnD3D9CreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc,
                                     void* pUserContext )
{
	D3DXVECTOR3 eyePt(0.0f, 0.0f, -5.0f);//where eye
	D3DXVECTOR3 lookAt(0.0f, 0.0f, 0.0f);//looat  at  where
	modelCamera.SetViewParams(&eyePt, &lookAt);//有了这个camera后,之前定义的setupMatrix可以删了

	D3DXCreateTeapot(pd3dDevice, &mesh, NULL);//create teapot
    return S_OK;
}


//--------------------------------------------------------------------------------------
// Create any D3D9 resources that won't live through a device reset (D3DPOOL_DEFAULT) 
// or that are tied to the back buffer size 
//--------------------------------------------------------------------------------------
HRESULT CALLBACK OnD3D9ResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc,
                                    void* pUserContext )
{
	float fAspectRatio = pBackBufferSurfaceDesc->Width / (FLOAT)pBackBufferSurfaceDesc->Height;
	modelCamera.SetProjParams(D3DX_PI / 4, fAspectRatio, 0.1f, 1000.0f);
	modelCamera.SetWindow(pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height);
    return S_OK;
}


//--------------------------------------------------------------------------------------
// Handle updates to the scene.  This is called regardless of which D3D API is used
//--------------------------------------------------------------------------------------
void CALLBACK OnFrameMove( double fTime, float fElapsedTime, void* pUserContext )
{
	modelCamera.FrameMove(fElapsedTime);//写死
}

//======================================
//告诉相机看什么?往哪看? //有了相机就不需要这个函数了!
//=======================================
VOID setupMatric(IDirect3DDevice9* pd3dDevice) {
	//set view matrix
	D3DXVECTOR3 vEyePt(0.0f, 0.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);
	pd3dDevice->SetTransform(D3DTS_VIEW, &matView);//告诉d设备
	//set projection matrix
	D3DXMATRIXA16 matProj;
	D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI / 4, 1.0f, 1.0f, 1000.0f);
	pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProj);

}


//--------------------------------------------------------------------------------------
// Render the scene using the D3D9 device
//--------------------------------------------------------------------------------------
void CALLBACK OnD3D9FrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
{
    HRESULT hr;
	//setupMatric(pd3dDevice);//set matrix,谁还自己搞矩阵啊,用camera
    // Clear the render target and the zbuffer 
    V( pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB( 0, 45, 50, 170 ), 1.0f, 0 ) );
	// Set world matrix
	D3DXMATRIX world = *modelCamera.GetWorldMatrix();
	pd3dDevice->SetTransform(D3DTS_WORLD, &world);

	// Set view matrix
	D3DXMATRIX view = *modelCamera.GetViewMatrix();
	pd3dDevice->SetTransform(D3DTS_VIEW, &view);

	// Set projection matrix
	D3DXMATRIX proj = *modelCamera.GetProjMatrix();
	pd3dDevice->SetTransform(D3DTS_PROJECTION, &proj);
    // Render the scene
    if( SUCCEEDED( pd3dDevice->BeginScene() ) )
    {
		mesh->DrawSubset(0);//render teapot
        V( pd3dDevice->EndScene() );
    }
}


//--------------------------------------------------------------------------------------
// Handle messages to the application 
//--------------------------------------------------------------------------------------
LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam,
                          bool* pbNoFurtherProcessing, void* pUserContext )
{
	modelCamera.HandleMessages(hWnd, uMsg, wParam, lParam);
    return 0;
}


//--------------------------------------------------------------------------------------
// Release D3D9 resources created in the OnD3D9ResetDevice callback 
//--------------------------------------------------------------------------------------
void CALLBACK OnD3D9LostDevice( void* pUserContext )
{
}


//--------------------------------------------------------------------------------------
// Release D3D9 resources created in the OnD3D9CreateDevice callback 
//--------------------------------------------------------------------------------------
void CALLBACK OnD3D9DestroyDevice( void* pUserContext )
{
	if (mesh != NULL) {
		mesh->Release();//release point
	}
}


//--------------------------------------------------------------------------------------
// Initialize everything and go into a render loop
//--------------------------------------------------------------------------------------
INT WINAPI wWinMain( HINSTANCE, HINSTANCE, LPWSTR, int )
{
    // Enable run-time memory check for debug builds.
#if defined(DEBUG) | defined(_DEBUG)
    _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif

    // Set the callback functions
    DXUTSetCallbackD3D9DeviceAcceptable( IsD3D9DeviceAcceptable );
    DXUTSetCallbackD3D9DeviceCreated( OnD3D9CreateDevice );
    DXUTSetCallbackD3D9DeviceReset( OnD3D9ResetDevice );
    DXUTSetCallbackD3D9FrameRender( OnD3D9FrameRender );
    DXUTSetCallbackD3D9DeviceLost( OnD3D9LostDevice );
    DXUTSetCallbackD3D9DeviceDestroyed( OnD3D9DestroyDevice );
    DXUTSetCallbackDeviceChanging( ModifyDeviceSettings );
    DXUTSetCallbackMsgProc( MsgProc );
    DXUTSetCallbackFrameMove( OnFrameMove );

    // TODO: Perform any application-level initialization here

    // Initialize DXUT and create the desired Win32 window and Direct3D device for the application
    DXUTInit( true, true ); // Parse the command line and show msgboxes
    DXUTSetHotkeyHandling( true, true, true );  // handle the default hotkeys
    DXUTSetCursorSettings( true, true ); // Show the cursor and clip it when in full screen
    DXUTCreateWindow( L"EmptyProject" );
    DXUTCreateDevice( true, 640, 480 );

    // Start the render loop
    DXUTMainLoop();

    // TODO: Perform any application-level cleanup here

    return DXUTGetExitCode();
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值