home *** CD-ROM | disk | FTP | other *** search
- //
- // File name: PolyDemo.CPP
- //
- // Description: The main section for a polygon demonstration
- //
- // Author: John De Goes
- //
- // Project: Cutting Edge 3D Game Programming
- //
-
- // ------------------------------------------------------------
- // | Global Include Files: |
- // ------------------------------------------------------------
-
- #include <Dos.h>
- #include <Math.h>
- #include <Time.h>
- #include <Conio.h>
- #include <Iostream.h>
-
- // ------------------------------------------------------------
- // | Local Include Files: |
- // ------------------------------------------------------------
-
- #include "32Bit.hpp"
- #include "3DClass.hpp"
- #include "MouseLib.hpp"
-
- // ------------------------------------------------------------
- // | Global variables/constants |
- // ------------------------------------------------------------
-
- // ------------------------------------------------------------
- // | Local classes/structs |
- // ------------------------------------------------------------
-
- // ------------------------------------------------------------
- // | Function section |
- // ------------------------------------------------------------
-
- // Function designed to emulate a track-ball:
- void UpdatePos ( MousePtr &Mouse, int &XRot, int &YRot, int &ZRot, double &ZPos )
- {
- // Get the mouse coordinates and map them to a suitable
- // range:
- int X = -( Mouse.GetX () - 50 ) >> 2;
- int Y = -( Mouse.GetY () - 50 ) >> 2;
-
- // If the left button is pressed:
- if ( ( Mouse.GetLb () ) && ( !Mouse.GetRb () ) )
- {
- // Do the rotations:
- XRot += Y;
- YRot += X;
- }
-
- // Else if the right button is pressed:
- else if ( ( Mouse.GetRb () ) && ( ! Mouse.GetLb () ) )
- {
- ZPos += ( double ) Y / -100.0F;
- ZRot += X;
- }
- }
-
- void FindExtents ( TriangleWorld &World, double &Xa,
- double &Ya, double &Za )
- {
- double TotalX = 0, TotalY = 0, TotalZ = 0;
- unsigned int VertexCount = 0;
- for ( unsigned int TCount = 0; TCount < World.TriCount; TCount++ )
- {
- TotalX += World.World [ TCount ].VPoint [ 0 ].Wx;
- TotalY += World.World [ TCount ].VPoint [ 0 ].Wy;
- TotalZ += World.World [ TCount ].VPoint [ 0 ].Wz;
- TotalX += World.World [ TCount ].VPoint [ 1 ].Wx;
- TotalY += World.World [ TCount ].VPoint [ 1 ].Wy;
- TotalZ += World.World [ TCount ].VPoint [ 1 ].Wz;
- TotalX += World.World [ TCount ].VPoint [ 2 ].Wx;
- TotalY += World.World [ TCount ].VPoint [ 2 ].Wy;
- TotalZ += World.World [ TCount ].VPoint [ 2 ].Wz;
- VertexCount += 3;
- }
- Xa = TotalX / ( double ) VertexCount;
- Ya = TotalY / ( double ) VertexCount;
- Za = TotalZ / ( double ) VertexCount;
- }
-
- // ------------------------------------------------------------
- // | Program entry |
- // ------------------------------------------------------------
-
- void main ()
- {
- MousePtr Mouse;
- TriangleWorld World;
- Matrix3D M;
- int YRot = 0, XRot = 0, ZRot = 0;
- double Xt = 0, Yt = 0, Zt = 0, ZPos;
- unsigned long Ticks = 0, StartTime, TotalTime;
- unsigned char *Buffer1, *VidMem;
-
- // Create an image of a cross for the mouse:
- unsigned char Cursor [ 1000 ] = {
- 0, 0, 0, 0, 0,17,17, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0,18,18, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0,19,19, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0,20,20, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0,21,21, 0, 0, 0, 0, 0,
- 17,18,19,20,21,22,22,21,20,19,18,17,
- 17,18,19,20,21,22,22,21,20,19,18,17,
- 0, 0, 0, 0, 0,21,21, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0,20,20, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0,19,19, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0,18,18, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0,17,17, 0, 0, 0, 0, 0,
- };
-
- // Allocate buffer memory:
- Buffer1 = new unsigned char [ 64000 ];
- // Check for error:
- if ( !Buffer1 )
- {
- cout << "\nNot enough memory to run application\n";
- return;
- }
-
- // Get the video address:
- VidMem = VideoAddress ();
-
- // Tell them what were doing:
- cout << "\nAttempting to load database...";
-
- // Load a complete virtual world in one line of code:
- if ( !World.Load ( "Test.raw" ) )
- {
- cout << "\nError Loading Test.RAW\n";
- cout << "Press any key to continue...";
- getch ();
- return;
- }
-
- // Find the extents of the file we just loaded:
- FindExtents ( World, Xt, Yt, Zt );
-
- // Initialize the mouse driver:
- Mouse.Init ();
-
- // Hide the pointer:
- Mouse.Hide ();
-
- // Set remap the cursor's coordinates:
- Mouse.MappingRange ( 100, 100 ); // Map the cursor to a 100x100
- // matrix
- // Clip the cursor to a rectangular region:
- Mouse.Clip ( 5, 5, 95, 95 );
-
-
- // Give the cursor a face-lift:
- Mouse.ChangeCursor ( Cursor, 12, 12 );
-
- // Set the video mode to 13h:
- SetVideo ( 0x13 );
-
- // Record the current time so we can calculate the
- // frame rate:
- StartTime = clock ();
-
- while ( !kbhit() ) // Loop until keyboard is hit...
- {
- // Clear buffer1 (the screen buffer) - writes four pixels
- // with each line of code:
- register unsigned long *Buf1Ptr = ( unsigned long * ) Buffer1;
- for ( unsigned short N = 0; N < 1000u; N++ )
- {
- *Buf1Ptr++ = 0ul;
- *Buf1Ptr++ = 0ul;
- *Buf1Ptr++ = 0ul;
- *Buf1Ptr++ = 0ul;
- *Buf1Ptr++ = 0ul;
- *Buf1Ptr++ = 0ul;
- *Buf1Ptr++ = 0ul;
- *Buf1Ptr++ = 0ul;
- *Buf1Ptr++ = 0ul;
- *Buf1Ptr++ = 0ul;
- *Buf1Ptr++ = 0ul;
- *Buf1Ptr++ = 0ul;
- *Buf1Ptr++ = 0ul;
- *Buf1Ptr++ = 0ul;
- *Buf1Ptr++ = 0ul;
- *Buf1Ptr++ = 0ul;
- }
-
- // Read the mouse position and adjust
- // our variables appropriately:
- UpdatePos ( Mouse, XRot, YRot, ZRot, ZPos );
-
- // Perform the matrix math:
- M.Initialize ();
- // Translate object to origin:
- M.Translate ( -Xt, -Yt, -Zt );
- // Rotate object:
- M.Rotate ( XRot, YRot, ZRot );
- // Translate back to previous position plus the Z
- // offset:
- M.Translate ( Xt, Yt, Zt + ZPos );
- // Find the new extents of the object:
- FindExtents ( World, Xt, Yt, Zt );
-
- // Set the rotation values to zero:
- ZRot = XRot = YRot = 0.0F;
- // Do the same with the ZPos variable:
- ZPos = 0.0F;
-
- // Transform and display the world(two lines of code!):
- World.Transform ( M );
- World.Display ( Buffer1 );
-
- // Display the mouse:
- Mouse.Display ( Buffer1 );
-
- // Copy the buffer to the screen - writes four pixels
- // with each line of C code:
- register unsigned long *VidPtr = ( unsigned long * ) VidMem;
- register unsigned long *BufPtr = ( unsigned long * ) Buffer1;
- for ( N = 0; N < 1000u; N++ )
- {
- *VidPtr++ = *BufPtr++;
- *VidPtr++ = *BufPtr++;
- *VidPtr++ = *BufPtr++;
- *VidPtr++ = *BufPtr++;
- *VidPtr++ = *BufPtr++;
- *VidPtr++ = *BufPtr++;
- *VidPtr++ = *BufPtr++;
- *VidPtr++ = *BufPtr++;
- *VidPtr++ = *BufPtr++;
- *VidPtr++ = *BufPtr++;
- *VidPtr++ = *BufPtr++;
- *VidPtr++ = *BufPtr++;
- *VidPtr++ = *BufPtr++;
- *VidPtr++ = *BufPtr++;
- *VidPtr++ = *BufPtr++;
- *VidPtr++ = *BufPtr++;
- }
-
- // Increment the number of frames:
- ++Ticks;
- }
-
- getch ();
-
- // Calculate elapsed time:
- TotalTime = clock () - StartTime;
-
- // Set the video mode to text mode:
- SetVideo ( 0x03 );
-
- // Unlock used memory:
- delete [] Buffer1;
-
- // Tell the user the frame rate:
- cout << "Frames per second = " << ( ( double ) Ticks *
- ( double ) CLK_TCK /
- ( double ) TotalTime );
- }