main.cpp

00001 /*
00002 * "PS2" Application Framework
00003 *
00004 * University of Abertay Dundee
00005 * May be used for educational purposed only
00006 *
00007 * Author - Dr Henry S Fortuna
00008 *
00009 * $Revision: 1.5 $
00010 * $Date: 2007/08/27 20:22:34 $
00011 *
00012 */
00013 #include <sps2lib.h>
00014 #include <sps2tags.h>
00015 #include <sps2util.h>
00016 #include <sps2regstructs.h>
00017 #include <assert.h>
00018 #include <memory.h>
00019 #include <signal.h>
00020 #include "PS2Defines.h"
00021 #include "ps2maths.h"
00022 #include "ps2matrix4x4.h"
00023 #include "pad.h"
00024 #include "sps2wrap.h"
00025 #include "dma.h"
00026 #include "texture.h"
00027 #include "font.h"
00028 #include "pipeline.h"
00029 #include "entities.h"
00030 #include "vumanager.h"
00031 #include "timer.h"
00032 #include "ms3dmodel.h"
00033 #include "terrain.h"
00034 
00035 #include "xFile_Render.h"
00036 
00037 /*
00038         Look at the start of the vcl code to get the positions
00039         and layout of the packet data in VU data memrory.
00040         Remember that the data sent to the VU must be in 
00041         the specified format of the PS2 WILL CRASH
00042 
00043 */
00044 
00045 
00046 // These two external function pointers are exported by the VU code
00047 // and we can now use them as pointers to the compiled VU data
00048 extern "C" uint64 VU_vu1code_start;
00049 extern "C" uint64 VU_vu1code_end;
00050 
00051 
00052 bool g_bLoop = true;
00053 
00054 void sig_handle(int sig)
00055 {
00056         g_bLoop = false;
00057 }
00058 
00059 
00060 int main(int argc, char *argv[])
00061 {       
00062 
00063         cout << "You called program " << argv[0] << " with arguments " << endl;
00064         for (int i = 1; i < argc; i++) {
00065                 cout << argv[i] << " ";
00066         }
00067 
00068         LPCSTR FileName = "tiny.x";
00069         if(argv[1])
00070         {
00071                 FileName = argv[1];
00072         }
00073 
00074         bool isVerbose = true;
00075         if(argv[2])
00076         {
00077                 isVerbose = atoi(argv[2]);
00078         }
00079 
00080         bool drawBounds = false;
00081         if(argv[3])
00082         {
00083                 drawBounds = atoi(argv[3]);
00084         }
00085 
00086         cout << "Loading: " << FileName << " where isVerbose =" << isVerbose;
00087 
00088 
00089         cout << endl;
00090 
00091 
00092 
00093         // Make sure these four lines are put first since some of the 
00094         // other classes need the managers to be initialised so they 
00095         // can pick up the correct data.        
00096         SPS2Manager.Initialise(4096);   // 4096 * 4K Pages = 16MB Total
00097         VIFStaticDMA.Initialise(3072);  // 1536 * 4K Pages = 6MB Static DMA
00098         VIFDynamicDMA.Initialise(256);  // 256 * 4K Pages * 2 Buffers =
00099                                                                         // 2MB Dynamic DMA
00100         Pipeline.Initialise();                  // Initialise graphics pipline class
00101         
00102         
00103         // Initialise Lighting
00104         // Three lights and Ambient light
00105         //                                                      Direction vector                                  Colour        
00106         Pipeline.SetLight1(Vector4( 1.0f, 0.2f, 0.0f, 0.0f), Vector4(128.0f,128.0f,128.0f,0.0f));
00107         Pipeline.SetLight2(Vector4(-1.0f,-0.2f, 0.0f, 0.0f), Vector4(128.0f,128.0f,128.0f,0.0f));
00108         Pipeline.SetLight3(Vector4( 0.0f, 0.0f,-1.0f, 0.0f), Vector4(0.0f,0.0f,128.0f,0.0f));
00109         //                            Colour
00110         Pipeline.SetAmbient(Vector4(30.0f,30.0f,30.0f,0.0f));
00111         
00112         Matrix4x4 matScale;
00113 
00114 
00115         //XFile to Render
00116         xFile_Render XFILE;
00117         XFILE.load(FileName,isVerbose);
00118         XFILE.setAnimation("Anim-1");
00119         //XFILE.m_XFile.listFrames();
00120 
00121         Cube boundingBox;
00122 
00123         CTexture boxTex;
00124         boxTex.LoadBitmap("test.bmp");
00125         boxTex.Upload(TEXBUF480);
00126         
00127 
00128         // Performance timer - call after SPS2Manager.Initialise()
00129         CTimer Timer;
00130         
00131         // Initialise control pad 0
00132         if(!pad_init(PAD_0, PAD_INIT_LOCK | PAD_INIT_ANALOGUE | PAD_INIT_PRESSURE))
00133         {
00134                 printf("Failed to initialise control pad\n");
00135                 pad_cleanup(PAD_0);
00136                 exit(0);
00137         }       
00138         
00139         // Initialise the VU1 manager class
00140         CVU1MicroProgram VU1MicroCode(&VU_vu1code_start, &VU_vu1code_end);
00141         
00142         // Upload the microcode
00143         VU1MicroCode.Upload();
00144         
00145 
00146         // Register our signal function for every possible signal (i.e. ctrl + c)
00147         for(int sig=0; sig<128; sig++)
00148                 signal(sig, sig_handle);
00149                 
00150                 
00151         // Set up the DMA packet to clear the screen. We want to clear to blue.
00152         SPS2Manager.InitScreenClear( 0x00, 0x00,  0x77);
00153         
00154 
00155         // Load the font bitmap and data
00156         CFont Font;
00157         CTexture FontTex;
00158         if(!Font.Load("font.dat", true))
00159         {
00160                 g_bLoop = false;
00161                 printf("Can't load font.dat\n");
00162         }
00163         if(!FontTex.LoadBitmap("font.bmp", false, true))
00164         {
00165                 printf("Can't load font.bmp\n");
00166                 g_bLoop = false;
00167         }
00168 
00169         // Upload these once to VRAM since
00170         // they are not going to change
00171         //Test.Upload(TEXBUF480);
00172         FontTex.Upload(TEXBUF496);
00173         
00174         //Pipeline.PositionCamera(Vector4(0.0f, 35.0f, 80.0f, 1.0f), 0.0f, 0.0f);
00175         Pipeline.PositionCamera(Vector4(0.0f, 55.0f, 80.0f, 1.0f), Vector4(0.0f, 40.0f, 0.0f, 1.0f));
00176 
00177 
00178         int Frame = 0;
00179         float fps = 0.0f;
00180 
00181         Matrix4x4 Movement;
00182         Movement = Matrix4x4::IDENTITY;
00183                 
00184         // The main Render loop
00185         while(g_bLoop)
00186         {
00187 
00188         
00189 
00190                 //start the time
00191                 Timer.PrimeTimer();
00192                                         
00193                 VIFDynamicDMA.Fire();
00194                 
00195                 pad_update(PAD_0);
00196                 
00197                 // Check for exit condition
00198                 if((pad[0].buttons & PAD_START)&&(pad[0].buttons & PAD_SELECT)) g_bLoop = false;
00199                 
00200                 // Set up a world matrix that rotates the cube about it's own axis, and then again
00201                 // around the origin.
00202                 static float sfRotOrigin = 0.0f;
00203                 static float sfRotLocal = 0.0f;
00204                 sfRotOrigin += pad[0].pressures[PAD_PLEFT] * 0.1f;
00205                 sfRotOrigin -= pad[0].pressures[PAD_PRIGHT] * 0.1f;
00206                 sfRotLocal += 0.03f;
00207 
00208                 Matrix4x4 matWorld, matTrans, matRotLocal, matRotOrigin;
00209                 matTrans.Translation(0, 0, -8.0f);
00210                 matRotOrigin.RotationY(sfRotOrigin);
00211                 matRotLocal.RotationY(sfRotLocal);
00212 
00213                 matWorld = matRotLocal * matTrans * matRotOrigin;
00214                 
00215                 // Get any camera movements
00216                 // Get any requested translations
00217                 float Strafe =   pad[0].axes[PAD_AXIS_LX];
00218                 float Advance =  pad[0].axes[PAD_AXIS_LY];
00219                 float UpDown =   (pad[0].pressures[PAD_PL1] - pad[0].pressures[PAD_PL2]);
00220                 
00221                 // Get requested rotations
00222                 float YRot = pad[0].axes[PAD_AXIS_RY];
00223                 float XRot = pad[0].axes[PAD_AXIS_RX];
00224                                 
00225                 // Reset camera to default position if requested
00226                 if(pad[0].buttons & PAD_R3)
00227                 {
00228                         Pipeline.PositionCamera(Vector4(0.0f, 0.0f, 0.0f, 1.0f), 0.0f, 0.0f);
00229                 }
00230                 
00231                 // Update the Camera and viewProjection matrices
00232                 Pipeline.Update(Strafe, Advance, UpDown, YRot, XRot);
00233                         
00234                 SPS2Manager.BeginScene();
00235                 
00236                 //Render X File
00237                 //Movement.elem[3][2] += 0.5f;
00238                 //XFILE.setGlobalPose(Movement);
00239                 static float animationSpeed = 0.5f;
00240 
00241                 if(pad[0].pressures[PAD_PUP])
00242                 {
00243                         animationSpeed += 0.01f;
00244                 }
00245                 
00246                 if(pad[0].pressures[PAD_PDOWN])
00247                 {
00248                         animationSpeed -= 0.01f;
00249                 }
00250                 // Reset animation speed with X
00251                 if(pad[0].pressed & PAD_CROSS)
00252                 {
00253                         animationSpeed = 0.5f;
00254                 }
00255 
00256                 DWORD animationTime = (DWORD)(*EE_T3_COUNT) * animationSpeed;
00257                 XFILE.render(animationTime);
00258                 
00259                 if(drawBounds)
00260                 {
00261                         // Draw Bounding Boxes
00262                         matScale = Matrix4x4::IDENTITY;
00263                         for(int i = 0; i < XFILE.m_XFile.meshes.size(); i++)
00264                         {
00265                                 matScale.elem[0][0] = (XFILE.m_XFile.meshes[i]->maxX - XFILE.m_XFile.meshes[i]->minX) /2.0f;
00266                                 matScale.elem[1][1] = (XFILE.m_XFile.meshes[i]->maxY - XFILE.m_XFile.meshes[i]->minY) /2.0f;
00267                                 matScale.elem[2][2] = (XFILE.m_XFile.meshes[i]->maxZ - XFILE.m_XFile.meshes[i]->minZ) /2.0f;
00268 
00269                                 boundingBox.SetWorldMatrix(matScale * XFILE.m_XFile.meshes[i]->pFrame->transform->transformedMatrix);
00270                                 boxTex.Select();
00271                                 boundingBox.Render();   
00272                         }
00273                 }
00274 
00275 
00276 
00277 
00278                 FontTex.Select();
00279                 
00280                 // Render some text
00281                 /*
00282                 Font.printfL(   -300, -240, 127, 127, 127, 127, 
00283                                                 "Camera Position (x, y, z) = (%3.1f, %3.1f, %3.1f)", 
00284                                                 Pipeline.GetCameraX(), Pipeline.GetCameraY(), Pipeline.GetCameraZ());
00285                 Font.printfL(   -300, -210, 127, 127, 127, 127, 
00286                                                 "Camera Rotation in Degrees (XRot, YRot) = (%3.1f, %3.1f)", 
00287                                                 RadToDeg(Pipeline.GetCameraXRot()), RadToDeg(Pipeline.GetCameraYRot()));*/
00288                 Font.printfC(  150, -240, 127, 127, 127, 127, "Frame: %d\nFPS: %.1f \nAnimationSpeed: %f" , Frame++, fps,animationSpeed);
00289 
00290                 
00291                 //calculate the current frame rate in frames per second 
00292                 fps = Timer.GetFPS();
00293                 
00294                 SPS2Manager.EndScene(); 
00295                 
00296                 if(pad[0].pressed & PAD_TRI)SPS2Manager.ScreenShot();           
00297         }
00298 
00299         pad_cleanup(PAD_0);
00300         
00301         return 0;
00302 }
00303 

Generated on Sun May 18 21:45:08 2008 for PS2X by  doxygen 1.5.4