entities.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.2 $
00010 * $Date: 2007/08/19 12:45:09 $
00011 *
00012 */
00013 
00014 #include <sps2lib.h>
00015 #include <sps2tags.h>
00016 #include <sps2util.h>
00017 #include <sps2regstructs.h>
00018 #include "PS2Defines.h"
00019 #include "ps2matrix4x4.h"
00020 #include "sps2wrap.h"
00021 #include "dma.h"
00022 #include "entities.h"
00023 
00024 // Initialise static member
00025 int Cube::iStaticAddr = 0;
00026 
00027 Cube::Cube()
00028 {
00029         // Initialise some default vaules
00030         World = Matrix4x4::IDENTITY;
00031         WVP = Matrix4x4::IDENTITY;
00032         WorldLoaded = false;
00033         LoadCubeData();
00034 }
00035 
00036 Cube::~Cube()
00037 {
00038         // Nothing to destruct
00039 }
00040 
00041 // Load the world matrix
00042 void Cube::SetWorldMatrix(const Matrix4x4 & matWorld)
00043 {
00044         World = matWorld;
00045         WVP = World * Pipeline.GetViewProjection();
00046         WorldLoaded = true;
00047 }
00048 
00049 // Load the cube data into static DMA memory
00050 void Cube::LoadCubeData(void)
00051 {
00052         // Check if data already loaded - return if it is.
00053         // We only need the cube data loaded once.
00054         if(Cube::iStaticAddr)return;
00055         
00056         const int iDraw = 0;
00057         const int iDontDraw = 1;
00058                 
00059         const float fDraw = *((float *)&iDraw);
00060         const float fDontDraw = *((float *)&iDontDraw);
00061                 
00062         printf("Loading Cube Data\n");
00063         
00064         // Get the address of static data that we can call to later.
00065         Cube::iStaticAddr = VIFStaticDMA.GetPointer();
00066         
00067         VIFStaticDMA.Add32(FLUSH);                      // Make sure VU1 isn't busy
00068         VIFStaticDMA.Add32(STCYCL(1,1));        // Unpack linearly, i.e. don't skip any spaces
00069         VIFStaticDMA.Add32(BASE(32));           // The double buffers start at VU1 address 32 (giving us 32 QW to store data that won't change)
00070         VIFStaticDMA.Add32(OFFSET(496));        // The size of each buffer.
00071 
00072         VIFStaticDMA.AddUnpack(V4_32, 0, 1);    // Unpack 1QW - the scale vector
00073         VIFStaticDMA.AddVector(Pipeline.GetScaleVector());
00074         
00075 
00076         // The first face draws two triangle
00077         // Next faces draw four triangles
00078         // Five triangles needed for last face
00079         // Some triangles are "zero" area and will be culled by GS
00080         int iTriangles = 12;
00081         int iVerts = iTriangles * 3;
00082         
00083         // We want to unpack 3 quad words of misc data, and then 3QW per vertex starting at
00084         // VU mem location 32 (we will upload the matrices to the first 16QW per frame).
00085         VIFStaticDMA.AddUnpack(V4_32, 0, 2 + iVerts * 3, 1);
00086         
00087         // VU32 iVerts is in position "x" LSWord of the qword
00088         VIFStaticDMA.Add128(iVerts*3);
00089         
00090         // Add the GIFTag VU33
00091         VIFStaticDMA.Add128(GS_GIFTAG_BATCH(    iVerts,                                         // NLOOP
00092                                                                                         1,                                                      // EOP
00093                                                                                         1,                                                      // PRE
00094                                                                                         GS_PRIM(PRIM_TRI,                       // PRIM
00095                                                                                                         PRIM_IIP_GOURAUD, 
00096                                                                                                         PRIM_TME_ON,
00097                                                                                                         PRIM_FGE_OFF, 
00098                                                                                                         PRIM_ABE_ON, 
00099                                                                                                         PRIM_AA1_OFF, 
00100                                                                                                         PRIM_FST_STQ, 
00101                                                                                                         PRIM_CTXT_CONTEXT1, 
00102                                                                                                         PRIM_FIX_NOFIXDDA),                                     
00103                                                                                         GIF_FLG_PACKED,                         // FLG
00104                                                                                         GS_BATCH_3(     GIF_REG_ST,
00105                                                                                                                 GIF_REG_RGBAQ,  // BATCH
00106                                                                                                                 GIF_REG_XYZ2)));
00107         
00108         // Front face
00109         VIFStaticDMA.AddVector(Vector4(0, 0, 1, fDraw));                        // TexCoord (STQ)       
00110         VIFStaticDMA.AddVector(Vector4( 0, 0, 1, 0));                           // Normal
00111         VIFStaticDMA.AddVector(Vector4(-1.0f, 1.0f, 1.0f, 1.0f));       // Vert (xyzw)
00112         VIFStaticDMA.AddVector(Vector4(1, 0, 1, fDraw));                        // TexCoord (STQ)
00113         VIFStaticDMA.AddVector(Vector4( 0, 0, 1, 0));                           // Normal
00114         VIFStaticDMA.AddVector(Vector4(1.0f,  1.0f, 1.0f, 1.0f));       // Vert (xyzw)
00115         VIFStaticDMA.AddVector(Vector4(0, 1, 1, fDraw));                        // TexCoord (STQ)
00116         VIFStaticDMA.AddVector(Vector4( 0, 0, 1, 0));                           // Normal
00117         VIFStaticDMA.AddVector(Vector4(-1.0f,-1.0f, 1.0f, 1.0f));       // Vert (xyzw)
00118         VIFStaticDMA.AddVector(Vector4(0, 1, 1, fDraw));                        // TexCoord (STQ)
00119         VIFStaticDMA.AddVector(Vector4( 0, 0, 1, 0));                           // Normal
00120         VIFStaticDMA.AddVector(Vector4(-1.0f,-1.0f, 1.0f, 1.0f));       // Vert (xyzw)
00121         VIFStaticDMA.AddVector(Vector4(1, 0, 1, fDraw));                        // TexCoord (STQ)
00122         VIFStaticDMA.AddVector(Vector4( 0, 0, 1, 0));                           // Normal
00123         VIFStaticDMA.AddVector(Vector4(1.0f,  1.0f, 1.0f, 1.0f));       // Vert (xyzw)
00124         VIFStaticDMA.AddVector(Vector4(1 , 1, 1, fDraw));                       // TexCoord (STQ)
00125         VIFStaticDMA.AddVector(Vector4( 0, 0, 1, 0));                           // Normal
00126         VIFStaticDMA.AddVector(Vector4(1.0f, -1.0f, 1.0f, 1.0f));       // Vert (xyzw)
00127 
00128         // Right face
00129         VIFStaticDMA.AddVector(Vector4(0, 0, 1, fDraw));                        // TexCoord (STQ)       
00130         VIFStaticDMA.AddVector(Vector4( 1, 0, 0, 0));                           // Normal
00131         VIFStaticDMA.AddVector(Vector4(1.0f, 1.0f, 1.0f, 1.0f));        // Vert (xyzw)
00132         VIFStaticDMA.AddVector(Vector4(1, 0, 1, fDraw));                        // TexCoord (STQ)
00133         VIFStaticDMA.AddVector(Vector4( 1, 0, 0, 0));                           // Normal
00134         VIFStaticDMA.AddVector(Vector4(1.0f,  1.0f, -1.0f, 1.0f));      // Vert (xyzw)
00135         VIFStaticDMA.AddVector(Vector4(0, 1, 1, fDraw));                        // TexCoord (STQ)
00136         VIFStaticDMA.AddVector(Vector4( 1, 0, 0, 0));                           // Normal
00137         VIFStaticDMA.AddVector(Vector4(1.0f,-1.0f, 1.0f, 1.0f));        // Vert (xyzw)
00138         VIFStaticDMA.AddVector(Vector4(0, 1, 1, fDraw));                        // TexCoord (STQ)
00139         VIFStaticDMA.AddVector(Vector4( 1, 0, 0, 0));                           // Normal
00140         VIFStaticDMA.AddVector(Vector4(1.0f,-1.0f, 1.0f, 1.0f));        // Vert (xyzw)
00141         VIFStaticDMA.AddVector(Vector4(1, 0, 1, fDraw));                        // TexCoord (STQ)
00142         VIFStaticDMA.AddVector(Vector4( 1, 0, 0, 0));                           // Normal
00143         VIFStaticDMA.AddVector(Vector4(1.0f, 1.0f, -1.0f, 1.0f));       // Vert (xyzw)
00144         VIFStaticDMA.AddVector(Vector4(1 , 1, 1, fDraw));                       // TexCoord (STQ)
00145         VIFStaticDMA.AddVector(Vector4( 1, 0, 0, 0));                           // Normal
00146         VIFStaticDMA.AddVector(Vector4(1.0f, -1.0f, -1.0f, 1.0f));      // Vert (xyzw)
00147 
00148         // Back face
00149         VIFStaticDMA.AddVector(Vector4(1, 0, 1, fDraw));                        // TexCoord (STQ)       
00150         VIFStaticDMA.AddVector(Vector4( 0, 0,-1, 0));                           // Normal
00151         VIFStaticDMA.AddVector(Vector4(-1.0f, 1.0f, -1.0f, 1.0f));      // Vert (xyzw)
00152         VIFStaticDMA.AddVector(Vector4(1, 1, 1, 0));                            // TexCoord (STQ)
00153         VIFStaticDMA.AddVector(Vector4( 0, 0,-1, fDraw));                       // Normal
00154         VIFStaticDMA.AddVector(Vector4(-1.0f,-1.0f, -1.0f, 1.0f));      // Vert (xyzw)
00155         VIFStaticDMA.AddVector(Vector4(0, 0, 1, 0));                            // TexCoord (STQ)
00156         VIFStaticDMA.AddVector(Vector4( 0, 0,-1, fDraw));                       // Normal
00157         VIFStaticDMA.AddVector(Vector4(1.0f,  1.0f, -1.0f, 1.0f));      // Vert (xyzw)
00158         VIFStaticDMA.AddVector(Vector4(1, 1, 1, 0));                            // TexCoord (STQ)
00159         VIFStaticDMA.AddVector(Vector4( 0, 0,-1, fDraw));                       // Normal
00160         VIFStaticDMA.AddVector(Vector4(-1.0f,-1.0f, -1.0f, 1.0f));      // Vert (xyzw)
00161         VIFStaticDMA.AddVector(Vector4(0 , 1, 1, fDraw));                       // TexCoord (STQ)
00162         VIFStaticDMA.AddVector(Vector4( 0, 0,-1, 0));                           // Normal
00163         VIFStaticDMA.AddVector(Vector4(1.0f, -1.0f, -1.0f, 1.0f));      // Vert (xyzw)
00164         VIFStaticDMA.AddVector(Vector4(0, 0, 1, fDraw));                        // TexCoord (STQ)
00165         VIFStaticDMA.AddVector(Vector4( 0, 0,-1, 0));                           // Normal
00166         VIFStaticDMA.AddVector(Vector4(1.0f,  1.0f, -1.0f, 1.0f));      // Vert (xyzw)
00167 
00168         // Left face
00169         VIFStaticDMA.AddVector(Vector4(1, 0, 1, fDraw));                        // TexCoord (STQ)       
00170         VIFStaticDMA.AddVector(Vector4(-1, 0, 0, 0));                           // Normal
00171         VIFStaticDMA.AddVector(Vector4(-1.0f, 1.0f, 1.0f, 1.0f));       // Vert (xyzw)
00172         VIFStaticDMA.AddVector(Vector4(0, 0, 1, fDraw));                        // TexCoord (STQ)
00173         VIFStaticDMA.AddVector(Vector4(-1, 0, 0, 0));                           // Normal
00174         VIFStaticDMA.AddVector(Vector4(-1.0f,  1.0f, -1.0f, 1.0f));     // Vert (xyzw)
00175         VIFStaticDMA.AddVector(Vector4(1, 1, 1, fDraw));                        // TexCoord (STQ)
00176         VIFStaticDMA.AddVector(Vector4(-1, 0, 0, 0));                           // Normal
00177         VIFStaticDMA.AddVector(Vector4(-1.0f,-1.0f, 1.0f, 1.0f));       // Vert (xyzw)
00178         VIFStaticDMA.AddVector(Vector4(1, 1, 1, fDraw));                        // TexCoord (STQ)
00179         VIFStaticDMA.AddVector(Vector4(-1, 0, 0, 0));                           // Normal
00180         VIFStaticDMA.AddVector(Vector4(-1.0f,-1.0f, 1.0f, 1.0f));       // Vert (xyzw)
00181         VIFStaticDMA.AddVector(Vector4(0, 0, 1, fDraw));                        // TexCoord (STQ)
00182         VIFStaticDMA.AddVector(Vector4(-1, 0, 0, 0));                           // Normal
00183         VIFStaticDMA.AddVector(Vector4(-1.0f, 1.0f, -1.0f, 1.0f));      // Vert (xyzw)
00184         VIFStaticDMA.AddVector(Vector4(0 , 1, 1, fDraw));                       // TexCoord (STQ)
00185         VIFStaticDMA.AddVector(Vector4(-1, 0, 0, 0));                           // Normal
00186         VIFStaticDMA.AddVector(Vector4(-1.0f, -1.0f, -1.0f, 1.0f));     // Vert (xyzw)
00187         
00188         // Top face
00189         VIFStaticDMA.AddVector(Vector4(1, 0, 1, fDraw));                        // TexCoord (STQ)       
00190         VIFStaticDMA.AddVector(Vector4(0, 1, 0, 0));                            // Normal
00191         VIFStaticDMA.AddVector(Vector4(1.0f, 1.0f, 1.0f, 1.0f));        // Vert (xyzw)
00192         VIFStaticDMA.AddVector(Vector4(0, 0, 1, fDraw));                        // TexCoord (STQ)
00193         VIFStaticDMA.AddVector(Vector4(0, 1, 0, 0));                            // Normal
00194         VIFStaticDMA.AddVector(Vector4(1.0f,  1.0f, -1.0f, 1.0f));      // Vert (xyzw)
00195         VIFStaticDMA.AddVector(Vector4(1, 1, 1, fDraw));                        // TexCoord (STQ)
00196         VIFStaticDMA.AddVector(Vector4(0, 1, 0, 0));                            // Normal
00197         VIFStaticDMA.AddVector(Vector4(-1.0f, 1.0f, 1.0f, 1.0f));       // Vert (xyzw)
00198         VIFStaticDMA.AddVector(Vector4(1, 1, 1, fDraw));                        // TexCoord (STQ)
00199         VIFStaticDMA.AddVector(Vector4(0, 1, 0, 0));                            // Normal
00200         VIFStaticDMA.AddVector(Vector4(-1.0f, 1.0f, 1.0f, 1.0f));       // Vert (xyzw)
00201         VIFStaticDMA.AddVector(Vector4(0, 0, 1, fDraw));                        // TexCoord (STQ)
00202         VIFStaticDMA.AddVector(Vector4(0, 1, 0, 0));                            // Normal
00203         VIFStaticDMA.AddVector(Vector4(1.0f, 1.0f, -1.0f, 1.0f));       // Vert (xyzw)
00204         VIFStaticDMA.AddVector(Vector4(0 , 1, 1, fDraw));                       // TexCoord (STQ)
00205         VIFStaticDMA.AddVector(Vector4(0, 1, 0, 0));                            // Normal
00206         VIFStaticDMA.AddVector(Vector4(-1.0f,  1.0f, -1.0f, 1.0f));     // Vert (xyzw)
00207         
00208         // Bottom face
00209         VIFStaticDMA.AddVector(Vector4(0, 0, 1, fDraw));                        // TexCoord (STQ)       
00210         VIFStaticDMA.AddVector(Vector4(0, -1, 0, 0));                           // Normal
00211         VIFStaticDMA.AddVector(Vector4(1.0f, -1.0f, 1.0f, 1.0f));       // Vert (xyzw)
00212         VIFStaticDMA.AddVector(Vector4(1, 0, 1, fDraw));                        // TexCoord (STQ)
00213         VIFStaticDMA.AddVector(Vector4(0, -1, 0, 0));                           // Normal
00214         VIFStaticDMA.AddVector(Vector4(1.0f,  -1.0f, -1.0f, 1.0f));     // Vert (xyzw)
00215         VIFStaticDMA.AddVector(Vector4(0, 1, 1, fDraw));                        // TexCoord (STQ)
00216         VIFStaticDMA.AddVector(Vector4(0, -1, 0, 0));                           // Normal
00217         VIFStaticDMA.AddVector(Vector4(-1.0f,-1.0f, 1.0f, 1.0f));       // Vert (xyzw)
00218         VIFStaticDMA.AddVector(Vector4(0, 1, 1, fDraw));                        // TexCoord (STQ)
00219         VIFStaticDMA.AddVector(Vector4(0, -1, 0, 0));                           // Normal
00220         VIFStaticDMA.AddVector(Vector4(-1.0f,-1.0f, 1.0f, 1.0f));       // Vert (xyzw)
00221         VIFStaticDMA.AddVector(Vector4(1, 0, 1, fDraw));                        // TexCoord (STQ)
00222         VIFStaticDMA.AddVector(Vector4(0, -1, 0, 0));                           // Normal
00223         VIFStaticDMA.AddVector(Vector4(1.0f, -1.0f, -1.0f, 1.0f));      // Vert (xyzw)
00224         VIFStaticDMA.AddVector(Vector4(1 , 1, 1, fDraw));                       // TexCoord (STQ)
00225         VIFStaticDMA.AddVector(Vector4(0, -1, 0, 0));                           // Normal
00226         VIFStaticDMA.AddVector(Vector4(-1.0f, -1.0f, -1.0f, 1.0f));     // Vert (xyzw)
00227 
00228         
00229         // Flush to make sure VU1 isn't doing anything.
00230         VIFStaticDMA.Add32(FLUSH);
00231         // Then run the microcode.
00232         VIFStaticDMA.Add32(MSCALL(0));
00233 
00234         // Return back to the dynamic chain (note that we aren't aligned on a QWORD boundary, but
00235         // it doesn't matter because the packet class will sort that out for us).
00236         VIFStaticDMA.DMARet();
00237 }
00238         
00239 
00240 // Render the cube.
00241 void Cube::Render(void) const
00242 {
00243         // check world matrix has been loaded (at least once!)
00244         // and that the DMA data has been initialised
00245         // Bomb out if they havn't since something is wrong
00246         assert(WorldLoaded && (Cube::iStaticAddr));
00247         
00248         // Wait for all to be idle in VU1 land
00249         // Before uploading the data
00250         VIFDynamicDMA.Add32(FLUSH);
00251         
00252         // Upload the matrices in the dynamic buffer
00253         // because they can change every frame.
00254         VIFDynamicDMA.AddUnpack(V4_32, 1, 16);
00255         VIFDynamicDMA.AddMatrix(Pipeline.GetLightDirs());
00256         VIFDynamicDMA.AddMatrix(Pipeline.GetLightCols());
00257         VIFDynamicDMA.AddMatrix(WVP);
00258         VIFDynamicDMA.AddMatrix(World);
00259         VIFDynamicDMA.DMACall(Cube::iStaticAddr);
00260 }
00261 
00262 

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