学习了三维的地形系统之后,该抬头看看天空啦。下面学习一下传说中的三维天空系统。其实三维天空的实现比地形系统简单得多,我们只需要用一点点小伎俩,就可以蒙混大多数人。
我们在玩游戏的时候,尤其是野外的大场景中,要想更逼真离不开三维天空系统。要用程序来模拟无边无际的天空是不可能的,而聪明的前辈们自然也想到了用“天圆地方”这种古代人们对于天空的认识来作为三维天空设计思想。即用一个足够大的面罩住我们,只要这个罩足够大,在里面看天空就和真实的天空没什么区别啦。而基于这种思想的天空设计方案中,最常用的就是天空盒。所谓天空盒,就是用一个立方体,罩住玩家所在的世界空间,天空盒常常是24个点,6个面,也可以是5个面,地面用三维地形,就不需要天空盒来覆盖了。
而要做出天空的效果,我们就必须要去找到一些天空的纹理素材,找到几张无缝拼接的图片,作为纹理贴图,这样我们看起来才不会穿帮。
天空盒其实比较简单,就是一个立方体,然后在立方体的内侧进行纹理贴图即可。要注意的就是纹理贴图的顺序不要搞错,不然一眼就会被发现。
和之前的地形系统一样,都需要保存设备指针,系统内部自己设定顶点,然后根据参数初始化天空盒的大小,以及设置纹理贴图。使用时,我们只需要设定这几个参数就可以创建出各式各样的天空系统啦!
.h文件
/*! * \file SkyBox.h * * \author puppet_master * \date 七月 2015 * * 三维天空盒的封装 */ #ifndef __SKYBOX_H_ #define __SKYBOX_H_ #pragma once struct SkyBoxVertex { float x, y, z; //定点位置 float u, v; //纹理位置 }; #define D3DFVF_SKYBOX D3DFVF_XYZ | D3DFVF_TEX1 class CSkyBox { private: LPDIRECT3DDEVICE9 m_pDevice; //设备指针 LPDIRECT3DVERTEXBUFFER9 m_pVertexBuffer;//定点缓冲区指针 LPDIRECT3DTEXTURE9 m_pTexture[5]; //纹理对象指针 float m_fLength; //天空盒宽度 public: CSkyBox(LPDIRECT3DDEVICE9 pDevice); virtual ~CSkyBox(void); //初始化天空盒 bool InitSkyBox(float length); //加载纹理 bool InitSkyBoxTexture(LPSTR szFrontTexture, LPSTR szBackTexture, LPSTR szLeftTexture, LPSTR szRightTexture, LPSTR szTopTexture); //渲染天空盒 void RenderSkyBox(D3DXMATRIX *matWorld, bool bRenderFrame = false); }; #endif
.cpp文件
#include "stdafx.h" #include "SkyBox.h" CSkyBox::CSkyBox(LPDIRECT3DDEVICE9 pDevice) { m_pDevice = pDevice; m_pVertexBuffer = NULL; for (int i = 0; i < 5; i++) m_pTexture[i] = NULL; m_fLength = 0.0f; } CSkyBox::~CSkyBox(void) { SAFE_RELEASE(m_pVertexBuffer); for (int i = 0; i < 5; i++) { SAFE_RELEASE(m_pTexture[i]); } } bool CSkyBox::InitSkyBox(float length) { m_fLength = length; //创建定点缓冲区 m_pDevice->CreateVertexBuffer(20 * sizeof(SkyBoxVertex), 0, D3DFVF_SKYBOX, D3DPOOL_MANAGED, &m_pVertexBuffer, 0); SkyBoxVertex vertices[] = { //前面的顶点 {-m_fLength / 2, 0.0f, m_fLength / 2, 0.0f, 1.0f,}, {-m_fLength / 2, m_fLength / 2, m_fLength / 2, 0.0f, 0.0f,}, {m_fLength / 2, 0.0f, m_fLength / 2, 1.0f, 1.0f,}, {m_fLength / 2, m_fLength / 2, m_fLength / 2, 1.0f, 0.0f,}, //后面的顶点 {m_fLength / 2, 0.0f, -m_fLength / 2, 0.0f, 1.0f,}, {m_fLength / 2, m_fLength / 2, -m_fLength / 2, 0.0f, 0.0f,}, {-m_fLength / 2, 0.0f, -m_fLength / 2, 1.0f, 1.0f,}, {-m_fLength / 2, m_fLength / 2, -m_fLength / 2, 1.0f, 0.0f,}, //左面的顶点 {-m_fLength / 2, 0.0f, -m_fLength / 2 , 0.0f, 1.0f,}, {-m_fLength / 2, m_fLength / 2, -m_fLength / 2, 0.0f, 0.0f,}, {-m_fLength / 2, 0.0f, m_fLength / 2, 1.0f, 1.0f,}, {-m_fLength / 2, m_fLength / 2, m_fLength / 2, 1.0f, 0.0f,}, //右面的顶点 {m_fLength / 2, 0.0f, m_fLength / 2, 0.0f, 1.0f,}, {m_fLength / 2, m_fLength / 2, m_fLength / 2, 0.0f, 0.0f,}, {m_fLength / 2, 0.0f, -m_fLength / 2, 1.0f, 1.0f,}, {m_fLength / 2, m_fLength / 2, -m_fLength / 2, 1.0f, 0.0f,}, //上面的顶点 {m_fLength / 2, m_fLength / 2, -m_fLength / 2, 1.0f, 0.0f,}, {m_fLength / 2, m_fLength / 2, m_fLength / 2, 1.0f, 1.0f,}, {-m_fLength / 2, m_fLength / 2, -m_fLength / 2, 0.0f, 0.0f,}, {-m_fLength / 2, m_fLength / 2, m_fLength / 2, 0.0f, 1.0f,}, }; //填充定点缓冲数据 void* pVertices = NULL; //加锁 m_pVertexBuffer->Lock(0, 0, (void**)&pVertices, 0); //拷贝顶点数据到缓冲区中 memcpy(pVertices, vertices, sizeof(vertices)); //解锁 m_pVertexBuffer->Unlock(); return true; } bool CSkyBox::InitSkyBoxTexture(LPSTR szFrontTexture, LPSTR szBackTexture, LPSTR szLeftTexture, LPSTR szRightTexture, LPSTR szTopTexture) { //从文件中加载天空盒纹理 D3DXCreateTextureFromFile(m_pDevice, szFrontTexture, &m_pTexture[0]);//前 D3DXCreateTextureFromFile(m_pDevice, szBackTexture, &m_pTexture[1]);//后 D3DXCreateTextureFromFile(m_pDevice, szLeftTexture, &m_pTexture[2]);//左 D3DXCreateTextureFromFile(m_pDevice, szRightTexture, &m_pTexture[3]);//右 D3DXCreateTextureFromFile(m_pDevice, szTopTexture, &m_pTexture[4]);//上 return true; } void CSkyBox::RenderSkyBox(D3DXMATRIX *matWorld, bool bRenderFrame /* = false */) { //纹理颜色混合的第一个参数用于输出 m_pDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); //纹理颜色混合的第一个参数取纹理的颜色值 m_pDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); //世界变换 m_pDevice->SetTransform(D3DTS_WORLD, matWorld); //设置资源流 m_pDevice->SetStreamSource(0, m_pVertexBuffer, 0, sizeof(SkyBoxVertex)); //设置灵活顶点格式 m_pDevice->SetFVF(D3DFVF_SKYBOX); //绘制 for(int i = 0; i < 5; i++) { m_pDevice->SetTexture(0, m_pTexture[i]); m_pDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, i * 4, 2); } }
CSkyBox* g_pSkyBox = NULL; //天空盒类指针创建天空盒
void CreateSkyBox() { g_pSkyBox = new CSkyBox(g_pDevice); g_pSkyBox->InitSkyBox(20000.0f); g_pSkyBox->InitSkyBoxTexture("skyfront.png", "skyback.png", "skyleft.png", "skyright.png", "skytop.png"); }
D3DXMatrixTranslation(&matWorldSky, 0.0f, -2000.0f, 0.0f); g_pSkyBox->RenderSkyBox(&matWorldSky);
换个角度:
飞到天外:
附上主体代码:
// D3DDemo.cpp : 定义应用程序的入口点。 // #include "stdafx.h" #include "D3DDemo.h" #include "DirectInput.h" #include "Camera.h" #include "Terrain.h" #include "Mesh.h" #include "SkyBox.h" #include "Particle.h" #define MAX_LOADSTRING 100 // 全局变量: HINSTANCE hInst; // 当前实例 TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本 TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名 // 此代码模块中包含的函数的前向声明: HWND g_hWnd; ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); //---------改造3D窗口需要的内容------------ LPDIRECT3D9 g_pD3D = NULL; //D3D接口指针 LPDIRECT3DDEVICE9 g_pDevice = NULL; //D3D设备指针 CDirectInput* g_pDirectInput = NULL; //控制指针 CCamera* g_pCamera = NULL; //摄像机指针 CTerrain* g_pTerrian = NULL; //地形类指针 CSkyBox* g_pSkyBox = NULL; //天空盒类指针 CMesh* g_pMesh1 = NULL; //网格对象指针1 CMesh* g_pMesh2 = NULL; //网格对象指针2 CMesh* g_pMesh3 = NULL; //网格对象指针3 D3DXMATRIX g_matWorld; //世界矩阵 void onCreatD3D() { g_pD3D = Direct3DCreate9(D3D_SDK_VERSION); if (!g_pD3D) return; //检测硬件设备能力的方法 /*D3DCAPS9 caps; ZeroMemory(&caps, sizeof(caps)); g_pD3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);*/ //获得相关信息,屏幕大小,像素点属性 D3DDISPLAYMODE d3ddm; ZeroMemory(&d3ddm, sizeof(d3ddm)); g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm); //设置全屏模式 D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp, sizeof(d3dpp)); /*d3dpp.Windowed = false; d3dpp.BackBufferWidth = d3ddm.Width; d3dpp.BackBufferHeight = d3ddm.Height;*/ d3dpp.Windowed = true; d3dpp.BackBufferFormat = d3ddm.Format; d3dpp.BackBufferCount = 1; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;//交换后原缓冲区数据丢弃 //是否开启自动深度模板缓冲 d3dpp.EnableAutoDepthStencil = true; //当前自动深度模板缓冲的格式 d3dpp.AutoDepthStencilFormat = D3DFMT_D16;//每个像素点有16位的存储空间,存储离摄像机的距离 g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pDevice); if (!g_pDevice) return; //设置渲染状态,设置启用深度值 g_pDevice->SetRenderState(D3DRS_ZENABLE, true); //设置渲染状态,关闭灯光 //g_pDevice->SetRenderState(D3DRS_LIGHTING, false); //设置渲染状态,裁剪模式 g_pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); //g_pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE) ; } void CreateMesh() { g_pMesh1 = new CMesh(g_pDevice); g_pMesh1->CreateMesh("Demon.X"); g_pMesh2 = new CMesh(g_pDevice); g_pMesh2->CreateMesh("dragon.X"); g_pMesh3 = new CMesh(g_pDevice); g_pMesh3->CreateMesh("miki.X"); } void CreateCamera() { g_pCamera = new CCamera(g_pDevice); g_pCamera->SetCameraPosition(&D3DXVECTOR3(0.0f, 800.0f, -500.0f)); g_pCamera->SetTargetPosition(&D3DXVECTOR3(0.0f, 800.0f, 0.0f)); g_pCamera->SetViewMatrix(); g_pCamera->SetProjectionMartix(); } void CreateTerrain() { g_pTerrian = new CTerrain(g_pDevice); g_pTerrian->LoadTerrainFromFile(TEXT("heighmap.raw"), TEXT("TerrainTexture.jpg")); g_pTerrian->InitTerrain(200, 200, 100.0f, 5.0f); } void CreateSkyBox() { g_pSkyBox = new CSkyBox(g_pDevice); g_pSkyBox->InitSkyBox(20000.0f); g_pSkyBox->InitSkyBoxTexture("skyfront.png", "skyback.png", "skyleft.png", "skyright.png", "skytop.png"); } void SetLight() { D3DLIGHT9 light; ::ZeroMemory(&light, sizeof(light)); light.Type = D3DLIGHT_DIRECTIONAL; light.Ambient = D3DXCOLOR(0.7f, 0.7f, 0.7f, 1.0f); light.Diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f); light.Specular = D3DXCOLOR(0.9f, 0.9f, 0.9f, 1.0f); light.Direction = D3DXVECTOR3(1.0f, 1.0f, 1.0f); g_pDevice->SetLight(0, &light); g_pDevice->LightEnable(0, true); g_pDevice->SetRenderState(D3DRS_NORMALIZENORMALS, true); g_pDevice->SetRenderState(D3DRS_SPECULARENABLE, true); } void onInit() { //初始化D3D onCreatD3D(); //创建Mesh模型 CreateMesh(); //创建摄像机 CreateCamera(); //创建地形 CreateTerrain(); //创建天空盒 CreateSkyBox(); //设置光照 SetLight(); } void onDestroy() { SAFE_DELETE(g_pDirectInput); SAFE_DELETE(g_pCamera); SAFE_DELETE(g_pTerrian); SAFE_DELETE(g_pSkyBox); SAFE_RELEASE(g_pDevice); SAFE_DELETE(g_pMesh1); SAFE_DELETE(g_pMesh2); SAFE_DELETE(g_pMesh3); } void onLogic(float fElapsedTime) { //使用DirectInput类读取数据 g_pDirectInput->GetInput(); // 沿摄像机各分量移动视角 if (g_pDirectInput->IsKeyDown(DIK_A)) g_pCamera->MoveAlongRightVec(-10.0f); if (g_pDirectInput->IsKeyDown(DIK_D)) g_pCamera->MoveAlongRightVec( 10.0f); if (g_pDirectInput->IsKeyDown(DIK_W)) g_pCamera->MoveAlongLookVec( 10.0f); if (g_pDirectInput->IsKeyDown(DIK_S)) g_pCamera->MoveAlongLookVec(-10.0f); if (g_pDirectInput->IsKeyDown(DIK_I)) g_pCamera->MoveAlongUpVec( 10.0f); if (g_pDirectInput->IsKeyDown(DIK_K)) g_pCamera->MoveAlongUpVec(-10.0f); //沿摄像机各分量旋转视角 if (g_pDirectInput->IsKeyDown(DIK_LEFT)) g_pCamera->RotationUpVec(-0.003f); if (g_pDirectInput->IsKeyDown(DIK_RIGHT)) g_pCamera->RotationUpVec( 0.003f); if (g_pDirectInput->IsKeyDown(DIK_UP)) g_pCamera->RotationRightVec(-0.003f); if (g_pDirectInput->IsKeyDown(DIK_DOWN)) g_pCamera->RotationRightVec( 0.003f); if (g_pDirectInput->IsKeyDown(DIK_J)) g_pCamera->RotationLookVec(-0.001f); if (g_pDirectInput->IsKeyDown(DIK_L)) g_pCamera->RotationLookVec( 0.001f); //鼠标控制右向量和上向量的旋转 //g_pCamera->RotationUpVec(g_pDirectInput->MouseDX()* 0.001f); //g_pCamera->RotationRightVec(g_pDirectInput->MouseDY() * 0.001f); //鼠标滚轮控制观察点收缩操作 static FLOAT fPosZ=0.0f; fPosZ += g_pDirectInput->MouseDZ()*3.0f; //计算并设置取景变换矩阵 D3DXMATRIX matView; g_pCamera->CalculateViewMatrix(&matView); g_pDevice->SetTransform(D3DTS_VIEW, &matView); //把正确的世界变换矩阵存到g_matWorld中 D3DXMatrixTranslation(&g_matWorld, 0.0f, 0.0f, fPosZ); } void onRender(float fElasedTime) { //前两个参数是0和NULL时,清空整个游戏窗口的内容(清的是后台) //第三个是清除的对象:前面表示清除颜色缓冲区,后面表示清除深度缓冲区,D3DCLEAR_STENCIL清空模板缓冲区 g_pDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,100,100), 1.0f, 0); g_pDevice->BeginScene(); D3DXMATRIX matWorld1, matWorld2, matWorld3, matWorldTerrain, matWorldSky; D3DXMatrixTranslation(&matWorld1, 0.0f, 800.0f, 0.0f); D3DXMatrixTranslation(&matWorld2, 0.0f, 1300.0f, 0.0f); D3DXMatrixTranslation(&matWorld3, -30.0f, 900.0f, -50.0f); D3DXMatrixTranslation(&matWorldTerrain, 0.0f, 0.0f, 0.0f); D3DXMatrixTranslation(&matWorldSky, 0.0f, -2000.0f, 0.0f); g_pMesh1->DrawMesh(matWorld1); g_pMesh2->DrawMesh(matWorld2); g_pMesh3->DrawMesh(matWorld3); g_pSkyBox->RenderSkyBox(&matWorldSky); g_pTerrian->RenderTerrain(&matWorldTerrain, false); g_pDevice->EndScene(); g_pDevice->Present(NULL, NULL, NULL, NULL); } int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPTSTR lpCmdLine, _In_ int nCmdShow){ UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); // TODO: 在此放置代码。 MSG msg; HACCEL hAccelTable; // 初始化全局字符串 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_D3DDEMO, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // 执行应用程序初始化: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_D3DDEMO)); ZeroMemory(&msg, sizeof(msg)); while (msg.message != WM_QUIT) { if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } else { static DWORD dwTime = timeGetTime(); DWORD dwCurrentTime = timeGetTime(); DWORD dwElapsedTime = dwCurrentTime - dwTime; float fElapsedTime = dwElapsedTime * 0.001f; //------------渲染和逻辑部分代码---------- onLogic(fElapsedTime); onRender(fElapsedTime); //----------------------------------------- if (dwElapsedTime < 1000 / 60) { Sleep(1000/ 60 - dwElapsedTime); } dwTime = dwCurrentTime; } } onDestroy(); return (int) msg.wParam; } // // 函数: MyRegisterClass() // // 目的: 注册窗口类。 // ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_D3DDEMO)); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = MAKEINTRESOURCE(IDC_D3DDEMO); wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); return RegisterClassEx(&wcex); } // // 函数: InitInstance(HINSTANCE, int) // // 目的: 保存实例句柄并创建主窗口 // // 注释: // // 在此函数中,我们在全局变量中保存实例句柄并 // 创建和显示主程序窗口。 // BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { hInst = hInstance; // 将实例句柄存储在全局变量中 g_hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); if (!g_hWnd) { return FALSE; } //初始化DirectInput类 g_pDirectInput = new CDirectInput(); g_pDirectInput->Init(g_hWnd, hInst, DISCL_FOREGROUND|DISCL_NONEXCLUSIVE, DISCL_FOREGROUND|DISCL_NONEXCLUSIVE); SetMenu(g_hWnd, NULL); ShowWindow(g_hWnd, nCmdShow); UpdateWindow(g_hWnd); onInit(); return TRUE; } // // 函数: WndProc(HWND, UINT, WPARAM, LPARAM) // // 目的: 处理主窗口的消息。 // // WM_COMMAND - 处理应用程序菜单 // WM_PAINT - 绘制主窗口 // WM_DESTROY - 发送退出消息并返回 // // LRESULT CALLBACK WndProc(HWND g_hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_KEYDOWN: if (wParam == VK_ESCAPE) PostQuitMessage(0); break; case WM_CLOSE: DestroyWindow(g_hWnd); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(g_hWnd, message, wParam, lParam); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/puppet_master/article/details/47281617