cylinder.cpp
资源名称:Direct3D.rar [点击查看]
上传用户:junlon
上传日期:2022-01-05
资源大小:39075k
文件大小:10k
源码类别:
DirextX编程
开发平台:
Visual C++
- // cylinder.cpp : 定义应用程序的入口点。
- //
- #include "stdafx.h"
- #include "cylinder.h"
- #include "D3DEnvClass.h"
- #define MAX_LOADSTRING 100
- // 自定义顶点结构体
- typedef struct tagCUSTOMVERTEX{
- D3DXVECTOR3 position; // The position of the vertex.
- D3DCOLOR color; // The color
- }CUSTOMVERTEX;
- // 自定义顶点格式
- #define D3DFVF_CUSTOMVERTEX D3DFVF_XYZ|D3DFVF_DIFFUSE
- // 全局变量:
- HINSTANCE hInst; // 当前实例
- TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
- TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
- // 全局变量,D3D环境对象
- CZFXD3DEnv g_d3dEnv;
- IDirect3DVertexBuffer9* g_pVB1; //Vertex Buffer, 圆柱体的侧面
- IDirect3DVertexBuffer9* g_pVB2; //Vertex Buffer, 圆柱体的上表面
- IDirect3DVertexBuffer9* g_pVB3; //Vertex Buffer, 圆柱体的上表面
- BOOL CreateVertexBuf1( IDirect3DVertexBuffer9* & g_pVB);
- BOOL CreateVertexBuf2( IDirect3DVertexBuffer9* & g_pVB);
- BOOL CreateVertexBuf3( IDirect3DVertexBuffer9* & g_pVB);
- void SetupMatrices();
- // 此代码模块中包含的函数的前向声明:
- ATOM MyRegisterClass(HINSTANCE hInstance);
- BOOL InitInstance(HINSTANCE, int);
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
- int APIENTRY _tWinMain(HINSTANCE hInstance,
- HINSTANCE prevInstance,
- PSTR cmdLine,
- int nCmdShow)
- {
- // TODO: 在此放置代码。
- MSG msg;
- HACCEL hAccelTable;
- // 初始化全局字符串
- LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
- LoadString(hInstance, IDC_CYLINDER, szWindowClass, MAX_LOADSTRING);
- MyRegisterClass(hInstance);
- // 执行应用程序初始化:
- if (!InitInstance (hInstance, nCmdShow))
- {
- return FALSE;
- }
- hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_CYLINDER);
- // 主消息循环:
- while (GetMessage(&msg, NULL, 0, 0))
- {
- if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
- return (int) msg.wParam;
- }
- //
- // 函数: MyRegisterClass()
- //
- // 目的: 注册窗口类。
- //
- // 注释:
- //
- // 仅当希望在已添加到 Windows 95 的
- // “RegisterClassEx”函数之前此代码与 Win32 系统兼容时,
- // 才需要此函数及其用法。调用此函数
- // 十分重要,这样应用程序就可以获得关联的
- // “格式正确的”小图标。
- //
- ATOM MyRegisterClass(HINSTANCE hInstance)
- {
- WNDCLASSEX wcex;
- wcex.cbSize = sizeof(WNDCLASSEX);
- wcex.style = CS_HREDRAW | CS_VREDRAW;
- wcex.lpfnWndProc = (WNDPROC)WndProc;
- wcex.cbClsExtra = 0;
- wcex.cbWndExtra = 0;
- wcex.hInstance = hInstance;
- wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_CYLINDER);
- wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
- wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
- wcex.lpszMenuName = (LPCTSTR)IDC_CYLINDER;
- wcex.lpszClassName = szWindowClass;
- wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
- return RegisterClassEx(&wcex);
- }
- //
- // 函数: InitInstance(HANDLE, int)
- //
- // 目的: 保存实例句柄并创建主窗口
- //
- // 注释:
- //
- // 在此函数中,我们在全局变量中保存实例句柄并
- // 创建和显示主程序窗口。
- //
- BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
- {
- HWND hWnd;
- hInst = hInstance; // 将实例句柄存储在全局变量中
- hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
- if (!hWnd)
- {
- return FALSE;
- }
- if(FAILED(g_d3dEnv.InitD3D(TRUE, hWnd)))
- return FALSE;
- if( !CreateVertexBuf1(g_pVB1))
- {
- MessageBox(NULL, "Create Vertex Buffer Failed", "Textures.exe", MB_OK);
- return FALSE;
- }
- if( !CreateVertexBuf2(g_pVB2))
- {
- MessageBox(NULL, "Create Vertex Buffer Failed", "Textures.exe", MB_OK);
- return FALSE;
- }
- if( !CreateVertexBuf3(g_pVB3))
- {
- MessageBox(NULL, "Create Vertex Buffer Failed", "Textures.exe", MB_OK);
- return FALSE;
- }
- ShowWindow(hWnd, nCmdShow);
- UpdateWindow(hWnd);
- return TRUE;
- }
- //
- // 函数: WndProc(HWND, unsigned, WORD, LONG)
- //
- // 目的: 处理主窗口的消息。
- //
- // WM_COMMAND - 处理应用程序菜单
- // WM_PAINT - 绘制主窗口
- // WM_DESTROY - 发送退出消息并返回
- //
- //
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- int wmId, wmEvent;
- // PAINTSTRUCT ps;
- // HDC hdc;
- switch (message)
- {
- case WM_COMMAND:
- wmId = LOWORD(wParam);
- wmEvent = HIWORD(wParam);
- // 分析菜单选择:
- switch (wmId)
- {
- case IDM_ABOUT:
- DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
- break;
- case IDM_EXIT:
- DestroyWindow(hWnd);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- break;
- case WM_PAINT:
- //hdc = BeginPaint(hWnd, &ps);
- // TODO: 在此添加任意绘图代码...
- //EndPaint(hWnd, &ps);
- // 确信设备未丢失,如果丢失,函数会完成重置设备功能
- g_d3dEnv.MakeSureNotLost();
- // 清除屏幕
- g_d3dEnv.m_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0,0,0,0), 1.0f, 0 );
- // 开始渲染
- g_d3dEnv.m_pd3dDevice->BeginScene();
- // 在此渲染你要呈现的场景
- // .....
- // 设置变换矩阵
- SetupMatrices();
- // 设置渲染状态
- g_d3dEnv.m_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
- g_d3dEnv.m_pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
- g_d3dEnv.m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
- g_d3dEnv.m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
- // 输出顶点流
- g_d3dEnv.m_pd3dDevice->SetStreamSource(0, g_pVB1, 0, sizeof(CUSTOMVERTEX));
- g_d3dEnv.m_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
- g_d3dEnv.m_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 98);
- g_d3dEnv.m_pd3dDevice->SetStreamSource(0, g_pVB2, 0, sizeof(CUSTOMVERTEX));
- g_d3dEnv.m_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
- g_d3dEnv.m_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 49);
- g_d3dEnv.m_pd3dDevice->SetStreamSource(0, g_pVB3, 0, sizeof(CUSTOMVERTEX));
- g_d3dEnv.m_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
- g_d3dEnv.m_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 49);
- g_d3dEnv.m_pd3dDevice->EndScene();
- // 将后台缓冲的数据搬到屏幕
- g_d3dEnv.m_pd3dDevice->Present( NULL, NULL, NULL, NULL );
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- g_pVB1->Release();
- g_pVB2->Release();
- g_pVB3->Release();
- g_d3dEnv.Release();
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
- // “关于”框的消息处理程序。
- LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message)
- {
- case WM_INITDIALOG:
- return TRUE;
- case WM_COMMAND:
- if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
- {
- EndDialog(hDlg, LOWORD(wParam));
- return TRUE;
- }
- break;
- }
- return FALSE;
- }
- //100 Vertices created.........
- BOOL CreateVertexBuf1(IDirect3DVertexBuffer9* & g_pVB)
- {
- CUSTOMVERTEX pVertices[100];
- ZeroMemory( pVertices, 100*sizeof( CUSTOMVERTEX ) );
- for(int i=0;i<50;i++)
- {
- FLOAT alpha = (2*D3DX_PI*i)/49;
- pVertices[2*i].position = D3DXVECTOR3( sinf(alpha), 1.0f, cosf(alpha));
- pVertices[2*i].color = 0x88ffffff;
- pVertices[2*i+1].position = D3DXVECTOR3( sinf(alpha),-1.0f, cosf(alpha));
- pVertices[2*i+1].color = 0x88ffffff;
- }
- if( FAILED(g_d3dEnv.m_pd3dDevice->CreateVertexBuffer( 100*sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX
- , D3DPOOL_DEFAULT, &g_pVB, NULL)))
- return FALSE;
- void* pData;
- g_pVB->Lock(0, 100*sizeof(CUSTOMVERTEX), (void**)&pData, 0);
- memcpy(pData, pVertices, 100*sizeof(CUSTOMVERTEX));
- g_pVB->Unlock();
- return TRUE;
- }
- BOOL CreateVertexBuf2(IDirect3DVertexBuffer9* & g_pVB)
- {
- CUSTOMVERTEX pVertices[51];
- ZeroMemory( pVertices, 51*sizeof( CUSTOMVERTEX ) );
- pVertices[0].position = D3DXVECTOR3( 0.0f, 1.0f, 0.0f);
- pVertices[0].color = 0x88ffffff;
- for(int i=0;i<50;i++)
- {
- FLOAT alpha = (2*D3DX_PI*i)/49;
- pVertices[i+1].position = D3DXVECTOR3( sinf(alpha), 1.0f, cosf(alpha));
- pVertices[i+1].color = 0x88ffffff;
- }
- if( FAILED(g_d3dEnv.m_pd3dDevice->CreateVertexBuffer( 51*sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX
- , D3DPOOL_DEFAULT, &g_pVB, NULL)))
- return FALSE;
- void* pData;
- g_pVB->Lock(0, 51*sizeof(CUSTOMVERTEX), (void**)&pData, 0);
- memcpy(pData, pVertices, 51*sizeof(CUSTOMVERTEX));
- g_pVB->Unlock();
- return TRUE;
- }
- BOOL CreateVertexBuf3(IDirect3DVertexBuffer9* & g_pVB)
- {
- CUSTOMVERTEX pVertices[51];
- ZeroMemory( pVertices, 51*sizeof( CUSTOMVERTEX ) );
- pVertices[0].position = D3DXVECTOR3( 0.0f, -1.0f, 0.0f);
- pVertices[0].color = 0x88ffffff;
- for(int i=0;i<50;i++)
- {
- FLOAT alpha = (2*D3DX_PI*i)/49;
- pVertices[i+1].position = D3DXVECTOR3( sinf(alpha), -1.0f, cosf(alpha));
- pVertices[i+1].color = 0x88ffffff;
- }
- if( FAILED(g_d3dEnv.m_pd3dDevice->CreateVertexBuffer( 51*sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX
- , D3DPOOL_DEFAULT, &g_pVB, NULL)))
- return FALSE;
- void* pData;
- g_pVB->Lock(0, 51*sizeof(CUSTOMVERTEX), (void**)&pData, 0);
- memcpy(pData, pVertices, 51*sizeof(CUSTOMVERTEX));
- g_pVB->Unlock();
- return TRUE;
- }
- void SetupMatrices()
- {
- // Set up world matrix
- D3DXMATRIX matWorld;
- D3DXMatrixIdentity( &matWorld );
- g_d3dEnv.m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
- // Set up our view matrix. A view matrix can be defined given an eye point,
- // a point to lookat, and a direction for which way is up. Here, we set the
- // eye five units back along the z-axis and up three units, look at the
- // origin, and define "up" to be in the y-direction.
- D3DXVECTOR3 vEyePt( 0.0f, 2.0f,-4.0f );
- D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );
- D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
- D3DXMATRIX matView;
- D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
- g_d3dEnv.m_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
- // For the projection matrix, we set up a perspective transform (which
- // transforms geometry from 3D view space to 2D viewport space, with
- // a perspective divide making objects smaller in the distance). To build
- // a perpsective transform, we need the field of view (1/4 pi is common),
- // the aspect ratio, and the near and far clipping planes (which define at
- // what distances geometry should be no longer be rendered).
- D3DXMATRIX matProj;
- D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 20.0f );
- g_d3dEnv.m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
- }