////////////////////////////////////////////////////////////////////////////////////////////////// // // File: cameraApp.cpp // // TP 1 Sept 2005 ////////////////////////////////////////////////////////////////////////////////////////////////// #include "d3dUtility.h" #include "projector.h" #include "mesh.h" #include #include "d3dfont.h" #include #include "army.h" d3d::Ray PickingRay; d3d::Ray GetPickingRay(LPARAM); char * StringifyRay(d3d::Ray); bool PollKeys(float); int currentR = -1, currentC = -1; double phaseDelta = -.02; int numMeshes = 12; // // Globals // IDirect3DDevice9* Device = 0; const int Width = 1024; const int Height = 700; Projector TheProjector; bool pieces_loaded = false; Mesh * g_whitepieces[6]; Mesh * g_blackpieces[6]; CD3DFont * g_font; Army * army; LPARAM g_mouseLocation = 0; void AddLight(int n, float x, float y, float z, DWORD color_val = 0xffffffff) { // 2006: ignore color, just use white. // normalize (x,y,z) float magnitude = sqrt(x*x + y*y + z*z); x /= magnitude; y /= magnitude; z /= magnitude; D3DXVECTOR3 lightDir(x, y, z); D3DXCOLOR color = 0xffffffff; D3DLIGHT9 light = d3d::InitDirectionalLight(&lightDir, &color); Device->SetLight(n, &light); Device->LightEnable(n, true); } // // Framework functions // bool Setup() { // // Set projection matrix. // D3DXMATRIX proj; D3DXMatrixPerspectiveFovLH( &proj, D3DX_PI * 0.18f, (float)Width / (float)Height, 1.0f, 100.0f); Device->SetTransform(D3DTS_PROJECTION, &proj); // overhead light AddLight(0, 0.0f, 1.0f, 0.0f); // back light AddLight(1, 0.0f, 0.0f, -1.0f, 0x00ff0000); // right-side light AddLight(2, -1.5f, 0.0f, -1.0f, 0x0000ff00); // left-side light AddLight(3, 1.5f, 0.0f, 1.0f, 0x000000ff); // Taken from CFont, Luna ch. 9 g_font = new CD3DFont("Times New Roman", 24, D3DFONT_BOLD); g_font->InitDeviceObjects(Device); g_font->RestoreDeviceObjects(); return true; } void LoadPieces() { army = new Army(Device); ::pieces_loaded = true; double rad = 3.85; double legrad = 1.45; // initialize K'nex pieces Piece leg1 = {0, 13.6, 0, 0, legrad, 0.00, false}; Piece leg2 = {0, 8.1, 0, 0, legrad, 0.50, false}; Piece leg3 = {0, -7.5, 0, 0, legrad, 0.75, false}; Piece leg4 = {0, -13.0, 0, 0, legrad, 0.25, false}; Piece axle1 = {3, 0, 0, rad, 0, 0.0, true}; Piece axle2 = {3, 0, 0, -rad, 0, 0.0, true}; Piece axle3 = {3, 0, -rad, 0, 0, 0.0, true}; Piece brace1 = {2, 2.6, 0, 0, 0, 0.0, false}; Piece brace2 = {2, -2.6, 0, 0, 0, 0.0, false}; Piece grays1 = {4, 0, 0, rad, 0, 0.0, true}; Piece grays2 = {4, 0, 0, -rad, 0, 0.0, true}; Piece grays3 = {4, 0, -rad, 0, 0, 0.0, true}; ::army->SetPieceData(0, leg1); ::army->SetPieceData(1, leg2); ::army->SetPieceData(2, leg3); ::army->SetPieceData(3, leg4); ::army->SetPieceData(4, axle1); ::army->SetPieceData(5, axle2); ::army->SetPieceData(6, axle3); ::army->SetPieceData(7, brace1); ::army->SetPieceData(8, brace2); ::army->SetPieceData(9, grays1); ::army->SetPieceData(10, grays2); ::army->SetPieceData(11, grays3); } void Cleanup() { delete army; if (g_font){ g_font->InvalidateDeviceObjects(); g_font->DeleteDeviceObjects(); delete g_font; } } bool Display(float timeDelta) { static int local_mouseLocation = 0; static int frames = 0; if (! ::pieces_loaded){ LoadPieces(); } if( Device ) { bool redraw = false; frames++; if (frames > 10){ frames = 0; redraw = true; } if (PollKeys(timeDelta)) redraw = true; if (local_mouseLocation != g_mouseLocation){ local_mouseLocation = g_mouseLocation; redraw = true; } if (true || redraw){ // Update the view matrix representing the cameras // new position/orientation. Device->SetTransform(D3DTS_VIEW, &TheProjector.getViewMatrix()); // // Render // Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00404080, 1.0f, 0); Device->BeginScene(); Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); ::army->IncreasePhase(::phaseDelta); ::army->DrawAllPieces(::numMeshes); } /* if (redraw) */ Device->EndScene(); Device->Present(0, 0, 0, 0); } return true; } // // WndProc // LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch( msg ) { case WM_DESTROY: ::PostQuitMessage(0); break; case WM_KEYDOWN: if( wParam == VK_ESCAPE ) ::DestroyWindow(hwnd); else if (wParam == VK_INSERT) ::phaseDelta -= .01; else if (wParam == VK_DELETE) ::phaseDelta += .01; else if (wParam == VK_F1) ::numMeshes = 21 - ::numMeshes; break; case WM_MOUSEMOVE: g_mouseLocation = lParam; break; case WM_LBUTTONDOWN: //PiecePickedUp(); break; case WM_LBUTTONUP: //PieceDropped(); break; } return ::DefWindowProc(hwnd, msg, wParam, lParam); } // // WinMain // int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE prevInstance, PSTR cmdLine, int showCmd) { if(!d3d::InitD3D(hinstance, Width, Height, true, D3DDEVTYPE_HAL, &Device)) { ::MessageBox(0, "InitD3D() - FAILED", 0, 0); return 0; } if(!Setup()) { ::MessageBox(0, "Setup() - FAILED", 0, 0); return 0; } d3d::EnterMsgLoop( Display ); Cleanup(); Device->Release(); return 0; }