Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // include the basic windows header files and the Direct3D header file
- #include <d3d9.h>
- #include <d3dx9.h>
- #include <windows.h>
- #include <windowsx.h>
- #include "Camera.h"
- #pragma comment (lib, "d3d9.lib")
- #pragma comment (lib, "d3dx9.lib")
- #pragma comment (lib, "Dinput8.lib")
- #pragma comment (lib, "dxguid.lib")
- #pragma comment (lib, "winmm.lib")
- #pragma comment (lib, "strmiids.lib")
- // define the screen resolution and keyboard macros
- #define SCREEN_WIDTH 1280
- #define SCREEN_HEIGHT 720
- #define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
- #define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
- // global declarations
- LPDIRECT3D9 d3d; // the pointer to our Direct3D interface
- LPDIRECT3DDEVICE9 d3ddev; // the pointer to the device class
- LPDIRECT3DSURFACE9 z_buffer = NULL; // the pointer to the z-buffer
- ID3DXMesh* g_pMesh = NULL;
- IDirect3DTexture9* g_pMeshTexture = NULL; // Mesh texture
- D3DXMATRIXA16 g_mCenterWorld;
- ID3DXEffect* g_pEffect = NULL;
- float temp;
- // function prototypes
- void initD3D(HWND hWnd,HINSTANCE hInstance); // sets up and initializes Direct3D
- void render_frame(void); // renders a single frame
- void cleanD3D(void); // closes Direct3D and releases memory
- void init_light(void);
- LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
- // the entry point for any Windows program
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow){
- HWND hWnd;
- WNDCLASSEX wc;
- ZeroMemory(&wc, sizeof(WNDCLASSEX));
- wc.cbSize = sizeof(WNDCLASSEX);
- wc.style = CS_HREDRAW | CS_VREDRAW;
- wc.lpfnWndProc = (WNDPROC)WindowProc;
- wc.hInstance = hInstance;
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.lpszClassName = "PirateShipWars";
- RegisterClassEx(&wc);
- hWnd = CreateWindowEx(NULL,
- "PirateShipWars",
- "Our Direct3D Program",
- WS_OVERLAPPEDWINDOW,
- 0, 0,
- SCREEN_WIDTH, SCREEN_HEIGHT,
- NULL,
- NULL,
- hInstance,
- NULL);
- ShowWindow(hWnd, nCmdShow);
- // set up and initialize Direct3D
- initD3D(hWnd,hInstance);
- // enter the main loop:
- MSG msg;
- while(TRUE)
- {
- DWORD starting_point = GetTickCount();
- if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
- {
- if (msg.message == WM_QUIT)
- break;
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- render_frame();
- if(KEY_DOWN(VK_ESCAPE))
- {
- PostMessage(hWnd, WM_DESTROY, 0, 0);
- }
- while ((GetTickCount() - starting_point) < 25);
- }
- // clean up DirectX and COM
- cleanD3D();
- return msg.wParam;
- }
- // this is the main message handler for the program
- LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch(message)
- {
- case WM_DESTROY:
- {
- PostQuitMessage(0);
- return 0;
- } break;
- }
- return DefWindowProc (hWnd, message, wParam, lParam);
- }
- // this function initializes and prepares Direct3D for use
- void initD3D(HWND hWnd, HINSTANCE hInstance)
- {
- d3d = Direct3DCreate9(D3D_SDK_VERSION);
- D3DPRESENT_PARAMETERS d3dpp;
- ZeroMemory(&d3dpp, sizeof(d3dpp));
- d3dpp.Windowed = true;
- d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
- d3dpp.hDeviceWindow = hWnd;
- d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
- d3dpp.BackBufferWidth = SCREEN_WIDTH;
- d3dpp.BackBufferHeight = SCREEN_HEIGHT;
- d3dpp.EnableAutoDepthStencil = TRUE;
- d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
- // create a device class using this information and the info from the d3dpp stuct
- d3d->CreateDevice(D3DADAPTER_DEFAULT,
- D3DDEVTYPE_HAL,
- hWnd,
- D3DCREATE_SOFTWARE_VERTEXPROCESSING,
- &d3dpp,
- &d3ddev);
- // create the z-buffer
- d3ddev->CreateDepthStencilSurface(SCREEN_WIDTH,
- SCREEN_HEIGHT,
- D3DFMT_D16,
- D3DMULTISAMPLE_NONE,
- 0,
- TRUE,
- &z_buffer,
- NULL);
- // using the shader debugger.
- DWORD dwShaderFlags = D3DXFX_NOT_CLONEABLE;
- D3DXLoadMeshFromX("box.x", D3DXMESH_MANAGED, d3ddev, NULL, NULL, NULL, NULL, &g_pMesh);
- //Test compilation buffer variable
- LPD3DXBUFFER d3dBuffer;
- // If this fails, there should be debug output as to
- // why the .fx file failed to compile
- D3DXCreateEffectFromFile( d3ddev, "Ambient.fx", NULL, NULL, dwShaderFlags, NULL, &g_pEffect, &d3dBuffer ); //dwShaderFlags (5)
- float temp = 0;
- D3DXVECTOR4 Acolour = D3DXVECTOR4(1,0,1,1);
- g_pEffect->SetVector( "AmbientColor" , &Acolour);
- float Dtemp = 0.8f;
- D3DXVECTOR4 DColour = D3DXVECTOR4( 0, 1, 0, 1 );
- D3DXVECTOR4 DLight = D3DXVECTOR4( .7f, 1.0f, -.8f, 0 );
- g_pEffect->SetFloat( "DiffuseIntensity", Dtemp);
- g_pEffect->SetVector( "DiffuseColor" , &DColour);
- g_pEffect->SetVector( "DiffuseLightDirection" , &DLight);
- D3DXVECTOR3 eye(0, 10, 10);
- D3DXVECTOR3 at(0, 0, 0);
- D3DXVECTOR3 up(0, 1, 0);
- // Setup the matrices within the shader (shaders handle matrices on their own unlike the
- // fixed function pipeline)
- D3DXMATRIX worldMatrix;
- D3DXMatrixTranslation(&worldMatrix, 0,0, 0);
- D3DXMATRIX viewMatrix;
- D3DXMatrixLookAtLH(&viewMatrix, &eye, &at, &up);
- D3DXMATRIX projectionMatrix;
- D3DXMatrixPerspectiveFovLH(&projectionMatrix,
- D3DXToRadian(45), // the horizontal field of view
- (FLOAT)1920 / (FLOAT)1080, // aspect ratio
- 1.0f, // the near view-plane
- 70000.0f); // the far view-plane
- g_pEffect->SetMatrix( "World", &worldMatrix);
- g_pEffect->SetMatrix( "View", &viewMatrix );
- g_pEffect->SetMatrix( "Projection", &projectionMatrix );
- return;
- }
- // this is the function used to render a single frame
- void render_frame(void)
- {
- if(temp < 1)
- {
- temp += 0.02;
- }
- else
- {
- temp = 0;
- }
- g_pEffect->SetFloat( "AmbientIntensity", temp);
- UINT iPass, cPasses;
- // Clear the render target and the zbuffer
- d3ddev->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DXCOLOR(0.0f,0.0,0.0,0.0), 1.0f, 0);
- d3ddev->BeginScene();
- // Apply the technique contained in the effect
- g_pEffect->Begin(&cPasses, 0);
- for (iPass = 0; iPass < cPasses; iPass++)
- {
- g_pEffect->BeginPass(iPass);
- g_pMesh->DrawSubset(0);
- g_pEffect->EndPass();
- }
- g_pEffect->End();
- d3ddev->EndScene();
- d3ddev->Present(NULL, NULL, NULL, NULL);
- return;
- }
- // this is the function that cleans up Direct3D and COM
- void cleanD3D(void)
- {
- d3ddev->Release(); // close and release the 3D device
- d3d->Release(); // close and release Direct3D
- return;
- }
Add Comment
Please, Sign In to add comment