TGC Codebase Backup



Pick Object for multiple cameras by Dmitry K

5th Feb 2004 15:00
Summary

A replacement Pick Object command for using with multiple cameras.



Description



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    `*******************************************************************************
`*                           Created: 26/01/2004                               *
`*              By: Dmitry K aka DMiTR0S   dmitr0s@newmail.ru                  *
`*******************************************************************************

`*******************************************************************************
`*                            Free License Agreement.                          *
`*                                                                             *
`*   The user/developer agrees to include a visible credit to "Dmitry Kuschev" *
`*   within your programs documentation and or web site.                       *
`*                                                                             *
`*   The user/developer agrees to NOT offer the this source code for Sale.     *
`*                                                                             *
`*   Providing the user/Developer agrees in full to the previous Free          *
`*   License Agreement clauses, the user/developer may use this source         *
`*   code FREELY in all their products, commercial or otherwise.               *
`*                                                                             *
`*******************************************************************************


Randomize Timer()

#CONSTANT ProjectVector    1
#CONSTANT LookAtVector     2
#CONSTANT LookUpVector     3
#CONSTANT EyeVector        4
#CONSTANT ViewMatrix       101
#CONSTANT ProjectionMatrix 102

r=Make Vector4(ProjectVector)
r=Make Vector3(LookAtVector)
r=Make Vector3(LookUpVector)
r=Make Vector3(EyeVector)
r=Make Matrix4(ViewMatrix)
r=Make Matrix4(ProjectionMatrix)

GLOBAL FOV As Float = 61.962139129638672

Make Object Sphere 1000, 50

For i=1 To 500
   Make Object Cube i, Rnd(10)
   Position Object i, 500-Rnd(1000), 500-Rnd(1000), 500-Rnd(1000)
   Rotate Object i, Rnd(360), Rnd(360), Rnd(360)
Next i

Camera=1
Make Camera Camera
Position Camera Camera, 100-Rnd(200), 100-Rnd(200), 100-Rnd(200)

Sync Rate 0
Sync On
Do

   Control Camera Using Arrowkeys Camera, 1, 1
   _2DTo3D(ProjectVector, 3000.0, MouseX() , MouseY(), 0, Camera Angle X(Camera), Camera Angle Y(Camera), Camera Angle Z(Camera), Camera Position X(Camera), Camera Position Y(Camera), Camera Position Z(Camera))

   NearestDist#=0.0
   IntObj=0
   For i=1 To 500
      d#=Intersect Object(i, Camera Position X(Camera), Camera Position Y(Camera), Camera Position Z(Camera), X Vector3(ProjectVector), Y Vector3(ProjectVector), Z Vector3(ProjectVector))
      If d#>0
         If d#<NearestDist# Or NearestDist#=0.0
            NearestDist#=d#
            IntObj=i
         EndIf
      EndIf
   Next i

   Position Object 1000, X Vector3(ProjectVector), Y Vector3(ProjectVector), Z Vector3(ProjectVector)

   Text 10, 10, "Code by Dmitry K"
   Text 10, 30, "Object # "+Str$(IntObj)
   Text 10, 50, "Distance "+Str$(NearestDist#)
   Sync
Loop

Function _2DTo3D(Vector, Range#, Mouse_X, Mouse_Y, Camera, CAX#, CAY#, CAZ#, CPX#, CPY#, CPZ#)
   X#=2.0*(Mouse_X*1.0)/(Screen Width()*1.0)-1.0
   Y#=1.0-2.0*(Mouse_Y*1.0)/(Screen Height()*1.0)
   Set Vector3 Vector, X#, Y#, 0.0
`View Matrix4 ViewMatrix - This command don't work with multiple cameras
   `Calculate view matrix of current camera
   CX#=Cos(CAX#)
   SX#=Sin(CAX#)
   CY#=Cos(CAY#)
   SY#=Sin(CAY#)
   CZ#=Cos(CAZ#)
   SZ#=Sin(CAZ#)
   Set Vector3 EyeVector, CPX#, CPY#, CPZ#
   Set Vector3 LookAtVector, -1.0*SZ#*(-1.0*SX#)+CZ#*SY#*CX#+CPX#, CZ#*(-1.0*SX#)+SZ#*SY#*CX#+CPY#, CY#*CX#+CPZ#
   Set Vector3 LookUpVector, -1.0*SZ#*CX#+CZ#*SY#*SX#, CZ#*CX#+SZ#*SY#*SX#, CY#*SX#
   Build LookAt LHMatrix4 ViewMatrix, EyeVector, LookAtVector, LookUpVector

`Projection Matrix4 ProjectionMatrix `- This command don't work with multiple cameras
   `Calculate projection matrix of current viewport
   Build FOV LHMatrix4 ProjectionMatrix, FOV*(3.1415926535897932384626433832795/180.0), (Screen Width()*1.0)/(Screen Height()*1.0), 1.0, 3000.0

   r=Inverse Matrix4(ViewMatrix, ViewMatrix)
   r=Inverse Matrix4(ProjectionMatrix, ProjectionMatrix)
   Transform Coords Vector3 Vector, Vector, ProjectionMatrix
   Normalize Vector3 Vector, Vector
   Multiply Vector3 Vector, Range#
   Transform Coords Vector3 Vector, Vector, ViewMatrix
EndFunction