⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bsploader.cpp

📁 一个DXD制作的读取QUAKE的BSP结构的Demo可以参考
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
   Bsp Loader Demo.
   Created by the Programming Ace.
   www.UltimateGameProgramming.com


   The purpose of this demo is to load a Quake 3 map in our application.
   This is done pretty easily by loading the data section by section.
   Once loaded this map can be rendered out to the scene.  This demo
   also has light maps which are added using simple multi-texture.

   This map was created using Q3Radiant.  This map is just a simple room
   with a few squares in it.  Q3 maps can be textured with either .tga
   images or .jpg (that is what the Q3 engine supports) so keep that in
   mind when trying to load maps others made.  If they use any .jpgs you
   either must convert to .tga or write code to load a .jpg.
*/


// Demo: Bsp Loader.


#define WIN32_LEAN_AND_MEAN
#define VC_LEANMEAN

#include<d3dx9.h>
#include"CBspLevel.h"
#include"CCamera.h"


// Function Prototypes...
bool InitializeDirect3D(HWND hwnd, bool fullscreen);
bool InitializeObject();
void RenderScene();
void ShutdownDirect3D();


LPDIRECT3D9 Direct3D_Object = NULL;
LPDIRECT3DDEVICE9 D3D_Device = NULL;
LPDIRECT3DTEXTURE9 *Texture = NULL;
LPDIRECT3DTEXTURE9 *LightMaps = NULL;
LPDIRECT3DVERTEXBUFFER9 Vertex_Buffer = NULL;
LPDIRECT3DVERTEXDECLARATION9 VertexDeclaration = NULL;

// Object to hold the level and a camera object.
CBspLevel BspLevel;
CCamera Camera;


void ApplyGamma(unsigned char *image, int size, int componenets, float gamma)
{
   if(!image) return;

   for(int i = 0; i < size / componenets; i++) 
      {
         float scale = 1.0f, temp = 0.0f;
         float r = 0, g = 0, b = 0;

         // extract the current RGB values
         r = (float)image[0];
         g = (float)image[1];
         b = (float)image[2];

         // Apply gamma.  Divide to keep 0 to 255.
         r *= gamma / 255.0f;
         g *= gamma / 255.0f;
         b *= gamma / 255.0f;

         temp = (1.0f / r);
         if(r > 1.0f && temp < scale) scale = temp;
         temp = (1.0f / g);
         if(g > 1.0f && temp < scale) scale = temp;
         temp = (1.0f / b);
         if(b > 1.0f && temp < scale) scale = temp;

         // Apply scale.
         scale *= 255.0f;		
         r *= scale;
         g *= scale;
         b *= scale;

         image[0] = (unsigned char)r;
         image[1] = (unsigned char)g;
         image[2] = (unsigned char)b;
         image += componenets;
      }
}



LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
       {
		   case WM_KEYUP:
            // If the user presses the escape key then exit the application.
            if(wParam == VK_ESCAPE)
               PostQuitMessage(0);
            break;


         case WM_DESTROY:
         case WM_CLOSE:
            PostQuitMessage(0);
            break;


            default:
               break;
       }

    return DefWindowProc( hwnd, message, wParam, lParam );
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
   MSG msg;                                           // Message object.
   HWND hwnd;                                         // Handle to the window.
   WNDCLASSEX windowClass;                            // Window class object.
   bool done = false;                                 // False if we should exit.

   // This is the Window class.
   windowClass.cbSize = sizeof(WNDCLASSEX);           // size of the WNDCLASSEX structure.
   windowClass.style = CS_HREDRAW | CS_VREDRAW;       // style of the window.
   windowClass.lpfnWndProc = WndProc;                 // Address to the windows procedure.
   windowClass.cbClsExtra = 0;                        // Extra class information.
   windowClass.cbWndExtra = 0;                        // Extra window information.
   windowClass.hInstance = hInstance;                 // Handle of application Instance.
   windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);// Window Icon.
   windowClass.hCursor = LoadCursor(NULL, IDC_ARROW); // Load mouse cursor.
   windowClass.hbrBackground = NULL;                  // Background color.
   windowClass.lpszMenuName = NULL;                   // Menu.
   windowClass.lpszClassName = "UGPClass";            // Name of the window class.
   windowClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);// Minimized window icon.

   // You must register you class with Windows.
   if(!RegisterClassEx(&windowClass)) return 0;

   // After your class has been registered then you are ready to create your window.
   hwnd = CreateWindowEx(NULL,                                       // The extended style.
                         "UGPClass",                                 // Window class.
                         "Quake 3 BSP - by The Programming Ace",// Window name.
                          WS_OVERLAPPEDWINDOW | WS_VISIBLE |         // Window style.
                          WS_SYSMENU |WS_CLIPCHILDREN |              // Window style.
                          WS_CLIPSIBLINGS,                           // Window style.
                          100, 100,                                  // X, Y coords.
                          640, 480,                                  // Window size.
                          NULL,                                      // Handle to parent window.
                          NULL,                                      // Menu.
                          hInstance,                                 // Handle to app instance.
                          NULL);                                     // Pointer to window.

   // If there was an error with creating the window, then close the program.
   if(!hwnd) return 0;

   ShowWindow(hwnd, SW_SHOW);    // Show the window.
   UpdateWindow(hwnd);           // Update its display.

   done = false;                 // false = run program, true means stop.

   // Initialize Direct3D.  If fails then we don't want to run the application.
   if(!InitializeDirect3D(hwnd, false)) done = true;

   // Application loop.
   while(!done)
      {
         if(PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))
            {
               // If a quit message is received then stop rendering and quit the app.
               if(msg.message == WM_QUIT)
                  {
                     done = true;
                  }

               TranslateMessage(&msg);
               DispatchMessage(&msg);
            }
         else
            {
               RenderScene();
            }
      }

    // Here we shutdown Direct 3D.
	ShutdownDirect3D();

   // Here we unregister the window class with the OS.
   UnregisterClass("UGPClass", windowClass.hInstance);

   return (int)msg.wParam;
}


bool InitializeDirect3D(HWND hwnd, bool fullscreen)
{
   // This object will allow us to set the display mode of the screen.
   D3DDISPLAYMODE DisplayMode;

   // This will allow us to set the parameters of the screen.
   D3DPRESENT_PARAMETERS Present_Parameters;

   // This is used to get the capabilities of the hardware.
   D3DCAPS9 D3DCaps;

   // It is always a good idea to clear out memory in object although not necessary.
   ZeroMemory(&Present_Parameters, sizeof(Present_Parameters));

   // Create the Direct3D object to get everything started.
   Direct3D_Object = Direct3DCreate9(D3D_SDK_VERSION);

   // Error checking.  Make sure that it was successful.
   if(Direct3D_Object == NULL)
      {
         MessageBox(NULL, "Error, couldn't initialize DirectX!?!",
                    "Error!", MB_OK);
         return false;
      }

   // This function will get the display mode of the device and place it in DisplayMode.
   if(FAILED(Direct3D_Object->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &DisplayMode)))
      {
         MessageBox(NULL, "Error setting the display mode.", "Error!", MB_OK);
         return false;
      }

   // Get the capabilities of the hardware.
	if(FAILED(Direct3D_Object->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &D3DCaps)))
		return false;

   // Test which is supported, hardware or software vertex processing.
	DWORD VertexProcessing = 0;

	if(D3DCaps.VertexProcessingCaps != 0)
		VertexProcessing |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
	else
		VertexProcessing |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;

   // Here we are setting the applications parameters...
   if(fullscreen)
      {
         Present_Parameters.Windowed = FALSE;               // Window mode (fullscreen).
         Present_Parameters.BackBufferWidth = DisplayMode.Width;
         Present_Parameters.BackBufferHeight = DisplayMode.Height;
      }
   else
      Present_Parameters.Windowed = TRUE;                   // Window mode (not fullscreen).
   Present_Parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;   // Dealing with animation (see doc).

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -