What is dxut?
Different people have different understandings of this concept. In general, it is a relatively simple d3d small framework. A simple encapsulation of d3d can be understood as a class library. The role of Microsoft can better display d3d functions and new features released; the role of game developers can display model results faster and lighter, and test algorithm effects; the role of learners, it can be an exquisite example of getting started. Because most of the d3d examples Use dxut, you can learn the examples in d3d SDK faster and accelerate the learning process.
Current dxut
Currently, dxut mainly includes three versions: d3d9 d3d10 and d3d11. It can be seen that the d3dx version follows the SDK, and dxut of d3d11 is an additional part, it is not released along with the SDK. This is not a problem, but it is still common. This entry-level article is about d3d9.
Architecture shortest dxut Library
Open the example in the SDK and you will see dxut. Then you will see two directories, core and optional. It is very clear that one is the core part of dxut and the other is the optional part of dxut. Our article is only focusing on the core part. Open the project in core. If there is no configuration problem, directly compile the project to generate a static dxut library. Dxut. Lib is the core library of dxut. We can add header files and generated libraries to the project to build the program, that is, using dxut. I have not found the possibility that dxut is encapsulated into DLL, because the functions in it do not have the Import and Export keywords, that is, the current dxut only supports static links. Optional libraries can also be obtained in this way, but this article does not.
Dxut code
We can also directly add the dxut source code in the project without using the library. The cost is that the compilation time is longer, but it does not conform to the library idea.
Dxut running process
Dxut first configures some callback functions, and then the actual operation of the main function enters the message loop. To use these functions, you need to write code in various callback functions. It's quite simple, but if you want to really learn the dxut class library, you still need to be familiar with some functions. Yes, we do not need to memorize it in programming, however, if you spend half an hour carrying back some major functions, you will find that your understanding of the database is more than just half a bit. Understanding this section requires a certain degree of Win32 and d3d basics.
Application exercise configuration directory
Configure the d3d and dxut header file directories. The d3d header file directory is in the SDK. The dxut header file directory is created by yourself. You can add the header file under the core file. Configure the library directory. The d3d library directory is still in the SDK. You can add the generated dxut. lib to the dxut library directory. As for how to configure these directories, it is found in the project properties.
Program code
#pragma once//win32#pragma comment(lib,"comctl32.lib")//d3d#pragma comment(lib,"dxerr.lib")#pragma comment(lib,"d3dx9.lib")//dxut#pragma comment(lib,"dxut.lib")//dxut#include"DXUT.h"HRESULT CALLBACK OnDeviceCreate( IDirect3DDevice9* pd3dDevice,const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext );void CALLBACK OnDeviceDestroyed(void* pUserContext );void CALLBACK OnFrameRender(IDirect3DDevice9* pd3dDevice,double fTime,float fElapsedTime, void* pUserContext );void SetUpMatrix(IDirect3DDevice9* pd3dDevice);void SetupLight(IDirect3DDevice9* pd3dDevice);//TODO: global variables.ID3DXMesh* g_pd3dMesh=NULL;INT WINAPI wWinMain(HINSTANCE,HINSTANCE,LPWSTR,INT){DXUTSetCallbackD3D9DeviceCreated(OnDeviceCreate,NULL);DXUTSetCallbackD3D9DeviceDestroyed(OnDeviceDestroyed,NULL);DXUTSetCallbackD3D9FrameRender(OnFrameRender,NULL);//0.dxut init.DXUTInit(true,true,NULL,true);//1.create windowDXUTCreateWindow(L"dxut window",GetModuleHandle(NULL),NULL,NULL,CW_USEDEFAULT,CW_USEDEFAULT);//2.create device.DXUTCreateDevice(true,640,480);//3.main loopDXUTMainLoop();//exit.return DXUTGetExitCode();}HRESULT CALLBACK OnDeviceCreate( IDirect3DDevice9* pd3dDevice,const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ){//TODO: create d3d device.D3DXCreateTeapot(pd3dDevice,&g_pd3dMesh,NULL);return S_OK;}void CALLBACK OnDeviceDestroyed(void* pUserContext ){//TODO:destroy d3d device.if(g_pd3dMesh!=NULL){g_pd3dMesh->Release();g_pd3dMesh=NULL;}}void CALLBACK OnFrameRender(IDirect3DDevice9* pd3dDevice,double fTime,float fElapsedTime, void* pUserContext ){pd3dDevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,255),1.0f,0);SetUpMatrix(pd3dDevice);SetupLight(pd3dDevice);if(SUCCEEDED(pd3dDevice->BeginScene())){//TODO:render d3d scene.g_pd3dMesh->DrawSubset(0);pd3dDevice->EndScene();}pd3dDevice->Present(NULL,NULL,NULL,NULL);}void SetUpMatrix(IDirect3DDevice9* pd3dDevice){D3DXMATRIXA16 viewMatrix;D3DXVECTOR3 vEye(0.0f,5.0f,-3.0f);D3DXVECTOR3 vLookAt(0.0f,0.0f,0.0f);D3DXVECTOR3 vUp(0.0f,1.0f,0.0f);D3DXMatrixLookAtLH(&viewMatrix,&vEye,&vLookAt,&vUp);pd3dDevice->SetTransform(D3DTS_VIEW,&viewMatrix);D3DXMATRIXA16 projectionMatrix;D3DXMatrixPerspectiveFovLH(&projectionMatrix,D3DX_PI/4,1.0f,1.0f,1000.0f);pd3dDevice->SetTransform(D3DTS_PROJECTION,&projectionMatrix);}void SetupLight(IDirect3DDevice9* pd3dDevice){D3DMATERIAL9 mtrl;ZeroMemory(&mtrl,sizeof(mtrl));mtrl.Ambient.r=mtrl.Diffuse.r=1.0f;mtrl.Ambient.g=mtrl.Diffuse.g=1.0f;mtrl.Ambient.b=mtrl.Diffuse.b=0.0f;mtrl.Ambient.a=mtrl.Diffuse.a=1.0f;pd3dDevice->SetMaterial(&mtrl);D3DLIGHT9 light;D3DXVECTOR3 dir(1.0f,0.0f,0.0f);D3DXCOLOR color(1.0f,1.0f,1.0f,0.0f);ZeroMemory(&light,sizeof(light));light.Type=D3DLIGHT_DIRECTIONAL;light.Ambient=color*0.6f;light.Diffuse=color;light.Specular=color*0.6f;light.Direction=dir;pd3dDevice->SetLight(0,&light);pd3dDevice->LightEnable(0,TRUE);pd3dDevice->SetRenderState(D3DRS_LIGHTING,TRUE);pd3dDevice->SetRenderState(D3DRS_NORMALIZENORMALS,TRUE);}
Summary of running results
Comprehensive
I am also a newbie and I am studying. I think it is helpful to understand the idea of the database. The graphics algorithm is not involved yet, but I will learn it. It takes some time to study dxut. In the future, we can quickly get results when testing the model, and it is also a powerful helper to understand the new version of d3d. Didn't you say that?