camera.cpp
资源名称:Direct3D.rar [点击查看]
上传用户:junlon
上传日期:2022-01-05
资源大小:39075k
文件大小:12k
源码类别:
DirextX编程
开发平台:
Visual C++
- // camera.cpp : 定义应用程序的入口点。
- //
- #include "stdafx.h"
- #include "camera.h"
- #include "D3DEnvClass.h"
- #include "zfxCamera.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;
- CZFXCamera g_camera;
- IDirect3DVertexBuffer9* g_pVB1; //Vertex Buffer, 圆柱体的侧面
- IDirect3DVertexBuffer9* g_pVB2; //Vertex Buffer, 圆柱体的上表面
- IDirect3DVertexBuffer9* g_pVB3; //Vertex Buffer, 圆柱体的上表面
- IDirect3DVertexBuffer9* g_pVB4; //Vertex Buffer, 大地
- // 创建圆柱体顶点缓冲
- BOOL CreateVertexBuf1( IDirect3DVertexBuffer9* & g_pVB);
- BOOL CreateVertexBuf2( IDirect3DVertexBuffer9* & g_pVB);
- BOOL CreateVertexBuf3( IDirect3DVertexBuffer9* & g_pVB);
- // 创建大地顶点缓冲
- BOOL CreateVertexBuf4( IDirect3DVertexBuffer9* & g_pVB);
- // 初始化和更新摄像机
- void InitCamera();
- void UpdateCamera();
- // 此代码模块中包含的函数的前向声明:
- 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 hPrevInstance,
- LPTSTR lpCmdLine,
- int nCmdShow)
- {
- // TODO: 在此放置代码。
- MSG msg;
- HACCEL hAccelTable;
- // 初始化全局字符串
- LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
- LoadString(hInstance, IDC_CAMERA, szWindowClass, MAX_LOADSTRING);
- MyRegisterClass(hInstance);
- // 执行应用程序初始化:
- if (!InitInstance (hInstance, nCmdShow))
- {
- return FALSE;
- }
- hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_CAMERA);
- // 主消息循环:
- 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_CAMERA);
- wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
- wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
- wcex.lpszMenuName = (LPCTSTR)IDC_CAMERA;
- 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;
- }
- // Direct3D初始化
- if(FAILED(g_d3dEnv.InitD3D(TRUE, hWnd)))
- return FALSE;
- // 初始化摄像机
- InitCamera();
- // 创建顶点缓冲
- 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;
- }
- if( !CreateVertexBuf4(g_pVB4))
- {
- 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;
- // 定义圆柱体的旋转角,每渲染一帧旋转角自加1度
- static float angle = 0.0f;
- D3DXMATRIX mat_view, mat_proj;
- D3DXMATRIX mat_earth;
- D3DXMATRIX mat_cylinder;
- 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, D3DCOLOR_ARGB(0,0,0,0), 1.0f, 0 );
- g_d3dEnv.m_pd3dDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0,0,0,0), 1.0f, 0 );
- // 开始渲染
- g_d3dEnv.m_pd3dDevice->BeginScene();
- // 在此渲染你要呈现的场景
- //.............................................................................
- // 更新摄像机状态
- UpdateCamera();
- // 设置摄像机的视口矩阵和投影矩阵
- g_d3dEnv.m_pd3dDevice->SetTransform( D3DTS_VIEW, g_camera.GetViewMatrix(&mat_view));
- g_d3dEnv.m_pd3dDevice->SetTransform( D3DTS_PROJECTION, g_camera.GetProjMatrix(&mat_proj));
- // 设置渲染状态
- g_d3dEnv.m_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
- g_d3dEnv.m_pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); // 实体模式
- g_d3dEnv.m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
- g_d3dEnv.m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
- // 输出顶点流
- // 绘制大地
- // 设置大地的世界矩阵,它始终是不动的,设为单位矩阵
- D3DXMatrixIdentity( &mat_earth );
- g_d3dEnv.m_pd3dDevice->SetTransform( D3DTS_WORLD, &mat_earth );
- g_d3dEnv.m_pd3dDevice->SetStreamSource(0, g_pVB4, 0, sizeof(CUSTOMVERTEX));
- g_d3dEnv.m_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
- g_d3dEnv.m_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
- // 绘制圆柱体
- g_d3dEnv.m_pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME); // 线框模式
- // 设置圆柱体的世界矩阵
- D3DXMatrixIdentity( &mat_cylinder );
- D3DXMatrixRotationY( &mat_cylinder, angle+=D3DX_PI/180 ); // 每渲染一帧旋转角自加1度
- g_d3dEnv.m_pd3dDevice->SetTransform( D3DTS_WORLD, &mat_cylinder );
- 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);
- // 释放D3D接口资源
- g_pVB1->Release();
- g_pVB2->Release();
- g_pVB3->Release();
- g_pVB4->Release();
- g_d3dEnv.Release();
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
- //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 InitCamera()
- {
- g_camera.SetPosX(0);
- g_camera.SetPosY(6.0f);
- g_camera.SetPosZ(-10.0f);
- g_camera.Pitch(-D3DX_PI*30/180);
- g_camera.SetProjProp(1.0f, 50.0f, D3DX_PI/3, 1);
- }
- void UpdateCamera()
- {
- static int i = 0;
- static bool bForward = true;
- // 每80帧改变一次视点移动方向
- if(i>=80)
- {
- i = 0;
- bForward = !bForward;
- }
- // 向前移动视点
- if(bForward)
- g_camera.Walk(0, 0.1f);
- else
- // 向后移动视点
- g_camera.Walk(0, -0.1f);
- i++;
- }
- // 创建大地顶点缓冲
- BOOL CreateVertexBuf4(IDirect3DVertexBuffer9* & g_pVB)
- {
- CUSTOMVERTEX pVertices[4];
- ZeroMemory( pVertices, 4*sizeof(CUSTOMVERTEX));
- pVertices[0].position = D3DXVECTOR3(-20.0f, -2.0f, -20.0f);
- pVertices[0].color = 0x881111aa;
- pVertices[1].position = D3DXVECTOR3(-20.0f, -2.0f, 20.0f);
- pVertices[1].color = 0x881111aa;
- pVertices[2].position = D3DXVECTOR3(20.0f, -2.0f, -20.0f);
- pVertices[2].color = 0x881111aa;
- pVertices[3].position = D3DXVECTOR3(20.0f, -2.0f, 20.0f);
- pVertices[3].color = 0x881111aa;
- if( FAILED(g_d3dEnv.m_pd3dDevice->CreateVertexBuffer( 4*sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX
- , D3DPOOL_DEFAULT, &g_pVB, NULL)))
- return FALSE;
- void* pData;
- g_pVB->Lock(0, 4*sizeof(CUSTOMVERTEX), (void**)&pData, 0);
- memcpy(pData, pVertices, 4*sizeof(CUSTOMVERTEX));
- g_pVB->Unlock();
- return TRUE;
- }
- // “关于”框的消息处理程序。
- 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;
- }