Direct3d 9 example program cone

Source: Internet
Author: User
This article introduces directx3d 9 and what is DirectX. It is very easy to say that Win32 C ++ API is mainly used in multimedia programming. The advantage is high performance. Now I know that the version is d3d11, however, I started to learn about d3d9, which is not very outdated. There are many directx3d elements. The elements mentioned here are the elements used in the program. 1> the starting point of d3d programming can be understood as the d3d SDK Assembly used. 2> d3d present parameter, rendering parameters. Professional parameters are configured as the best through a check. The parameters in the program are simply displayed with the default parameters. 3> the d3d device can be understood as a virtual multimedia device. It involves various concepts in d3d theory. 4> d3d vertex vertices a meaningful 3D vertex in a graph, which is a special vertex, for example, a triangle vertex. 5> d3d vertex buffer vertex cache, which temporarily stores the memory area of the vertex. Program structure: a dialog box-based program. MFC is very easy to understand, where a thing is placed, and when it meets any event, it will do the corresponding thing. Program parsing description initialization 1> initialization d3d2> initialization logic unit configuration 1> Configuration d3d2> Configuration logic execution 1> Rotation 2> rendering end 1> release various objects UML
Program Implementation Framework a dialog box inherited from cwnd
Details 1. Create a project d3dmfcapp2. Add the button to the end of the d3dmfcappdlg main window. 3. Add the d3ddlg class to the project and find an image (JPG my is). Change the size to 256x256 name texture.jpg and copy it to the project. 4. d3dmfcappdlg add variable
CD3DDlg m_D3DDlg;
5. d3dmfcappdlg start button generation function Addition
m_D3DDlg.CreateEx(0,AfxRegisterWndClass(NULL,NULL,NULL,NULL),L"D3D Window",WS_POPUP|WS_CAPTION|WS_VISIBLE,CRect(100,100,500,500),this,0);
6. d3dmfcappdlg end button generation function Addition
m_D3DDlg.DestroyWindow();
7. The cd3ddlg header file introduces the file and defines the vertex structure.
#pragma comment(lib,"d3d9.lib")#pragma comment(lib,"d3dx9.lib")#include <d3d9.h>#include <d3dx9math.h>typedef struct _CUSTOMVERTEX1{D3DXVECTOR3 position;D3DXVECTOR3 normal;float fu;float fv;}CUSTOMVERTEX1;#define D3DFVF_CUSTOMVERTEX1 (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1)typedef struct _CUSTOMVERTEX2{D3DXVECTOR3 position;D3DXVECTOR3 normal;}CUSTOMVERTEX2;#define D3DFVF_CUSTOMVERTEX2 (D3DFVF_XYZ|D3DFVF_NORMAL)
The installation of DirectX may be involved here, but there are many examples on the Internet. I only pay attention to the instance exercises. I just said that if the d3d SDK is installed, there will be no errors in the correct path configuration. 8. Add a message function in the Class Wizard. Remember that this is the result of the Class Wizard.
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);afx_msg void OnDestroy();afx_msg void OnTimer(UINT_PTR nIDEvent);
9. Add private functions in the Class Wizard
void InitD3D9(void);void Render(void);void CleanUp(void);void InitGeometry(void);void SetupMatrices(void);void SetLight(void);void SetMaterial1(void);void SetMaterial2(void);
10. Add private variables in the Class Wizard
LPDIRECT3D9 m_pD3D9;LPDIRECT3DDEVICE9 m_pD3DDevice9;LPDIRECT3DVERTEXBUFFER9 m_pVB1;LPDIRECT3DVERTEXBUFFER9 m_pVB2;LPDIRECT3DTEXTURE9 m_pD3DTexture9;int m_nRotateY;

11. cd3ddlg implements file initialization d3d

<pre name="code" class="cpp">void CD3DDlg::InitD3D9(void){m_pD3D9=Direct3DCreate9(D3D_SDK_VERSION);D3DPRESENT_PARAMETERS d3dpp;ZeroMemory(&d3dpp,sizeof(D3DPRESENT_PARAMETERS));d3dpp.BackBufferFormat=D3DFMT_UNKNOWN;d3dpp.BackBufferCount=1;d3dpp.EnableAutoDepthStencil=TRUE;d3dpp.AutoDepthStencilFormat=D3DFMT_D16;d3dpp.Windowed=TRUE;d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;m_pD3D9->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,m_hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&m_pD3DDevice9);m_pD3DDevice9->SetRenderState(D3DRS_ZENABLE,D3DZB_TRUE);m_pD3DDevice9->SetRenderState(D3DRS_LIGHTING,TRUE);m_pD3DDevice9->SetRenderState(D3DRS_NORMALIZENORMALS,TRUE);}
12. initialize the elements 
void CD3DDlg::InitGeometry(void){D3DXCreateTextureFromFile(m_pD3DDevice9,_T("texture.jpg"),&m_pD3DTexture9);CUSTOMVERTEX1 vertices1[32];vertices1[0].position=D3DXVECTOR3(-1.0f,0.0f,0.0f);vertices1[0].normal=D3DXVECTOR3(-1.0f,0.0f,0.0f);vertices1[0].fu=0.0f;vertices1[0].fv=0.5f;for (int i=1;i<32;i++){float theat=(i-1)*2*D3DX_PI/30.0f;vertices1[i].position=D3DXVECTOR3(1.0f,sinf(theat),cosf(theat));vertices1[i].normal=D3DXVECTOR3(0.0f,sinf(theat),cosf(theat));float m=sqrtf(5.0f);float beta=(0.5f-1.0f/m+(i-1.0f)/15.0f/m)*D3DX_PI;vertices1[i].fu=0.5*sinf(beta);vertices1[i].fv=0.5-0.5*cosf(beta);}m_pD3DDevice9->CreateVertexBuffer(sizeof(vertices1),0,D3DFVF_CUSTOMVERTEX1,D3DPOOL_DEFAULT,&m_pVB1,NULL);VOID* pVertices=NULL;m_pVB1->Lock(0,sizeof(vertices1),&pVertices,0);memcpy(pVertices,vertices1,sizeof(vertices1));m_pVB1->Unlock();CUSTOMVERTEX2 vertices2[32];vertices2[0].position=D3DXVECTOR3(1.0f,0.0f,0.0f);vertices2[0].normal=D3DXVECTOR3(1.0f,0.0f,0.0f);for (int i=1;i<32;i++){float theat=(i-1)*2*D3DX_PI/30.0f;vertices2[i].position=vertices1[32-i].position;vertices2[i].normal=D3DXVECTOR3(1.0f,0.0f,0.0f);}m_pD3DDevice9->CreateVertexBuffer(sizeof(vertices2),0,D3DFVF_CUSTOMVERTEX2,D3DPOOL_DEFAULT,&m_pVB2,NULL);m_pVB2->Lock(0,sizeof(vertices2),&pVertices,0);memcpy(pVertices,vertices2,sizeof(vertices2));m_pVB2->Unlock();}
13. Set the left-side Transform
void CD3DDlg::SetupMatrices(void){D3DXMATRIX matWorld;float angle=m_nRotateY*D3DX_PI/180;D3DXMatrixRotationY(&matWorld,angle );m_pD3DDevice9->SetTransform(D3DTS_WORLD,&matWorld);D3DXVECTOR3 eye(0.0f,3.0f,-5.0f);D3DXVECTOR3 lookAt(0.0f,0.0f,0.0f);D3DXVECTOR3 up(0.0f,1.0f,0.0f);D3DXMATRIX matView;D3DXMatrixLookAtLH(&matView,&eye,&lookAt,&up);m_pD3DDevice9->SetTransform(D3DTS_VIEW,&matView);D3DXMATRIXA16 matProj;D3DXMatrixPerspectiveFovLH(&matProj,D3DX_PI/4,1.0f,1.0f,100.0f);m_pD3DDevice9->SetTransform(D3DTS_PROJECTION,&matProj);}
14. Set material 1
void CD3DDlg::SetMaterial1(void){D3DMATERIAL9 mtrl;ZeroMemory(&mtrl,sizeof(D3DMATERIAL9));mtrl.Diffuse.r=mtrl.Ambient.r=1.0f;mtrl.Diffuse.g=mtrl.Ambient.g=1.0f;mtrl.Diffuse.b=mtrl.Ambient.b=1.0f;mtrl.Diffuse.a=mtrl.Ambient.a=1.0f;m_pD3DDevice9->SetMaterial(&mtrl);}
15. Set material 2
void CD3DDlg::SetMaterial2(void){D3DMATERIAL9 mtrl;ZeroMemory(&mtrl,sizeof(D3DMATERIAL9));mtrl.Diffuse.r=mtrl.Ambient.r=1.0f;mtrl.Diffuse.g=mtrl.Ambient.g=1.0f;mtrl.Diffuse.b=mtrl.Ambient.b=0.0f;mtrl.Diffuse.a=mtrl.Ambient.a=1.0f;m_pD3DDevice9->SetMaterial(&mtrl);}
16. Set care
void CD3DDlg::SetLight(void){D3DLIGHT9 light;ZeroMemory(&light,sizeof(light));light.Type=D3DLIGHT_DIRECTIONAL;light.Diffuse.r=1.0f;light.Diffuse.g=1.0f;light.Diffuse.b=1.0f;light.Direction=D3DXVECTOR3(0.0f,0.0,1.0f);light.Range=1000.0f;m_pD3DDevice9->SetLight(0,&light);m_pD3DDevice9->LightEnable(0,TRUE);m_pD3DDevice9->SetRenderState(D3DRS_AMBIENT,D3DCOLOR_RGBA(32,32,32,0));}
17. Rendering execution
void CD3DDlg::Render(void){m_pD3DDevice9->Clear(0,NULL,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,255),1.0f,0);m_pD3DDevice9->BeginScene();SetupMatrices();SetLight();SetMaterial1();m_pD3DDevice9->SetTexture(0,m_pD3DTexture9);m_pD3DDevice9->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_MODULATE);m_pD3DDevice9->SetTextureStageState(0,D3DTSS_COLORARG1,D3DTA_TEXTURE);m_pD3DDevice9->SetTextureStageState(0,D3DTSS_COLORARG2,D3DTA_DIFFUSE);m_pD3DDevice9->SetFVF(D3DFVF_CUSTOMVERTEX1);m_pD3DDevice9->SetStreamSource(0,m_pVB1,0,sizeof(CUSTOMVERTEX1));m_pD3DDevice9->DrawPrimitive(D3DPT_TRIANGLEFAN,0,30);m_pD3DDevice9->SetTexture(0,NULL);m_pD3DDevice9->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_DISABLE);SetMaterial2();m_pD3DDevice9->SetFVF(D3DFVF_CUSTOMVERTEX2);m_pD3DDevice9->SetStreamSource(0,m_pVB2,0,sizeof(CUSTOMVERTEX2));m_pD3DDevice9->DrawPrimitive(D3DPT_TRIANGLEFAN,0,30);m_pD3DDevice9->EndScene();m_pD3DDevice9->Present(NULL,NULL,NULL,NULL);}
18. release resources
void CD3DDlg::CleanUp(void){m_pVB1->Release();m_pVB2->Release();m_pD3DTexture9->Release();m_pD3DDevice9->Release();m_pD3D9->Release();}
19. The public function is finally initialized.
int CD3DDlg::OnCreate(LPCREATESTRUCT lpCreateStruct){if (CWnd::OnCreate(lpCreateStruct) == -1)return -1;InitD3D9();InitGeometry();SetTimer(1,40,NULL);return 0;}
20. Scheduled Rendering
void CD3DDlg::OnTimer(UINT_PTR nIDEvent){Render();m_nRotateY+=2;CWnd::OnTimer(nIDEvent);}
21. End
void CD3DDlg::OnDestroy(){CWnd::OnDestroy();CleanUp();}
Program result


To sum up the concept of direct3d, no one can make it clear at once. I write this program for three weeks, and one of the bugs gets stuck for nearly a week. No one can explain how to do this without having to practice more. However, we recommend that you copy the program. Program 1> the best engineering type for getting started with the MFC project. I tried Win32 and a framework would just confuse people. It's better to simply use MFC, but only learning is required for MFC. When it comes to application or Win32, it should be well said. 2> the variable is d3ddevice9 rather than d3d9device. Of course, you can write it on your own. But on the official website, you get used to it. 3> function call is similar to Win32, for example
m_pD3DDevice9->CreateVertexBuffer(sizeof(vertices1),0,D3DFVF_CUSTOMVERTEX1,D3DPOOL_DEFAULT,&m_pVB1,NULL);
For each row of a parameter, you still need to be sure which function branch is written. When Microsoft internally calls the first parameter and function, the last brace will be split into another line. I think it is difficult to adjust it to this format. 4> to copy images to a project, you need to modify the properties. The type is resource compiler. Otherwise, the error 1102 is reported. If you want to learn more, please refer to the elementary tutorial of directx3d9. I have read this article very well, when Baidu received the praise, he wanted to give the praise result a star and he could not change it any more. I am so angry that I am sorry here.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.