home *** CD-ROM | disk | FTP | other *** search
/ Learn 3D Graphics Programming on the PC / Learn_3D_Graphics_Programming_on_the_PC_Ferraro.iso / rwwin / rwvbview.fr_ / rwvbview.bin
Text File  |  1995-11-14  |  15KB  |  448 lines

  1. VERSION 2.00
  2. Begin Form Form1 
  3.    Caption         =   "RenderWare VB Object Viewer"
  4.    ClientHeight    =   3648
  5.    ClientLeft      =   876
  6.    ClientTop       =   1464
  7.    ClientWidth     =   4104
  8.    Height          =   4200
  9.    Icon            =   RWVBVIEW.FRX:0000
  10.    Left            =   828
  11.    LinkTopic       =   "Form1"
  12.    ScaleHeight     =   3648
  13.    ScaleWidth      =   4104
  14.    Top             =   960
  15.    Width           =   4200
  16.    Begin Timer tmrAnimate 
  17.       Interval        =   20
  18.       Left            =   720
  19.       Top             =   120
  20.    End
  21.    Begin CommonDialog OpenFileDlg 
  22.       DialogTitle     =   "Load Script or Picture into Scene"
  23.       Filter          =   "Script file (*.rwx) | *.rwx | Picture file (*.ras;*.bmp) | *.ras;*.bmp"
  24.       Left            =   120
  25.       Top             =   120
  26.    End
  27.    Begin Menu File 
  28.       Caption         =   "&File"
  29.       Begin Menu Open 
  30.          Caption         =   "&Open..."
  31.       End
  32.       Begin Menu Exit 
  33.          Caption         =   "E&xit"
  34.       End
  35.    End
  36.    Begin Menu Help 
  37.       Caption         =   "&Help"
  38.       Begin Menu About 
  39.          Caption         =   "&About"
  40.       End
  41.    End
  42. End
  43. Const SHIFT_MASK = 1
  44. Const CTRL_MASK = 2
  45. Const ALT_MASK = 4
  46.  
  47. Const LEFT_BUTTON = 1
  48. Const RIGHT_BUTTON = 2
  49. Const MIDDLE_BUTTON = 4
  50.  
  51. Dim Scene As Long
  52.  
  53. Dim Camera As Long
  54. Dim CameraTilt As Single
  55. Dim CameraDistance As Single
  56.  
  57. Dim Light As Long
  58.  
  59. Dim SpinMatrix As Long
  60.  
  61. Dim LastX As Integer
  62. Dim LastY As Integer
  63.  
  64. Dim AnimMode As Integer
  65. Dim FrameNumber As Integer
  66.  
  67. Const ANoAction = 0
  68. Const ASpinClump = 1
  69.  
  70. Dim PickedClump As Long
  71. Dim MouseMoveMode As Integer
  72.  
  73. Const MMNoAction = 0
  74. Const MMDragClump = 1
  75. Const MMDragClumpInZ = 2
  76. Const MMPanLight = 3
  77. Const MMPanAndZoomCamera = 4
  78. Const MMScrollBackdrop = 5
  79. Const MMSpinClump = 6
  80. Const MMTiltCamera = 7
  81.  
  82. Sub About_Click ()
  83.     frmAbout.Show 1
  84. End Sub
  85.  
  86. Sub Exit_Click ()
  87.     Unload Me
  88. End Sub
  89.  
  90. Sub Form_Load ()
  91.     Dim VRet As Variant
  92.  
  93.     CameraTilt = 0
  94.     CameraDistance = -7
  95.     FrameNumber = 0
  96.  
  97.     If RwOpen("MSWindows", 0) = 0 Then
  98.         If RwGetError() = E_RW_NOMEM Then
  99.             MsgBox "Insufficient memory to open the RenderWare(tm) library", MB_OK
  100.         Else
  101.             MsgBox "Error opening the RenderWare(tm) library", MB_OK
  102.         End If
  103.         End
  104.     End If
  105.  
  106.     Camera = RwCreateCamera(1024, 768, 0)
  107.     If Camera = 0 Then
  108.         If RwGetError() = E_RW_NOMEM Then
  109.             MsgBox "Insufficient memory to create the RenderWare(tm) camera", MB_OK
  110.         Else
  111.             MsgBox "Error creating the RenderWare(tm) camera", MB_OK
  112.         End If
  113.         RwClose
  114.         End
  115.     End If
  116.  
  117.     VRet = RwSetCameraBackColor(Camera, 0, 0, 1)
  118.  
  119.     VRet = RwTiltCamera(Camera, CameraTilt)
  120.     VRet = RwVCMoveCamera(Camera, 0, 0, CameraDistance)
  121.     
  122.     VRet = RwSetCameraViewwindow(Camera, .2, .2)
  123.  
  124.     Scene = RwCreateScene()
  125.     If Scene = 0 Then
  126.         VRet = RwDestroyCamera(Camera)
  127.         RwClose
  128.         MsgBox "Error creating the RenderWare(tm) scene", MB_OK
  129.         End
  130.     End If
  131.  
  132.     Light = RwCreateLight(rwDIRECTIONAL, -1, -1, -1, 1)
  133.     If Light = 0 Then
  134.         VRet = RwDestroyScene(Scene)
  135.         VRet = RwDestroyCamera(Camera)
  136.         RwClose
  137.         MsgBox "Error creating the RenderWare(tm) light", MB_OK
  138.         End
  139.     End If
  140.  
  141.     VRet = RwAddLightToScene(Scene, Light)
  142.  
  143.     SpinMatrix = RwCreateMatrix()
  144.     If SpinMatrix = 0 Then
  145.         VRet = RwDestroyScene(Scene)
  146.         VRet = RwDestroyCamera(Camera)
  147.         RwClose
  148.         MsgBox "Error creating the RenderWare(tm) matrix", MB_OK
  149.         End
  150.     End If
  151. End Sub
  152.  
  153. Sub Form_MouseDown (Button As Integer, Shift As Integer, x As Single, y As Single)
  154.     Dim Pick As RwPickRecord
  155.     Dim VRet As Variant
  156.  
  157.     x = x / Screen.TwipsPerPixelX
  158.     y = y / Screen.TwipsPerPixelY
  159.     If (Button And LEFT_BUTTON) <> 0 Then
  160.         If RwPickScene(Scene, x, y, Camera, Pick) <> 0 Then
  161.             Select Case Pick.type
  162.             Case rwNAPICKOBJECT
  163.                 AnimMode = ANoAction
  164.                 PickedClump = 0
  165.     
  166.                 If RwGetCameraBackdrop(Camera) <> 0 Then
  167.                     If (Shift And (SHIFT_MASK Or CTRL_MASK)) = (SHIFT_MASK Or CTRL_MASK) Then
  168.                         VRet = RwDestroyRaster(RwGetCameraBackdrop(Camera))
  169.                         VRet = RwSetCameraBackdrop(Camera, 0)
  170.                         RenderScene hWnd, hDC
  171.                         MouseMoveMode = MMNoAction
  172.                     Else
  173.                         MouseMoveMode = MMScrollBackdrop
  174.                     End If
  175.                 Else
  176.                     MouseMoveMode = MMNoAction
  177.                 End If
  178.     
  179.             Case rwPICKEDCLUMP
  180.                 If (Shift And (SHIFT_MASK Or CTRL_MASK)) = (SHIFT_MASK Or CTRL_MASK) Then
  181.                     VRet = RwDestroyClump(Pick.Object.clump.clump)
  182.                     RenderScene hWnd, hDC
  183.                     MouseMoveMode = MMNoAction
  184.                     PickedClump = 0
  185.                 Else
  186.                     PickedClump = Pick.Object.clump.clump
  187.                     If (Shift And SHIFT_MASK) <> 0 Then
  188.                         MouseMoveMode = MMDragClump
  189.                     ElseIf (Shift And CTRL_MASK) <> 0 Then
  190.                         MouseMoveMode = MMDragClumpInZ
  191.                     Else
  192.                         MouseMoveMode = MMSpinClump
  193.                     End If
  194.                 End If
  195.             End Select
  196.         End If
  197.     ElseIf (Button And RIGHT_BUTTON) <> 0 Then
  198.         If (Shift And CTRL_MASK) <> 0 Then
  199.             MouseMoveMode = MMPanLight
  200.         ElseIf (Shift And SHIFT_MASK) <> 0 Then
  201.             MouseMoveMode = MMTiltCamera
  202.         Else
  203.             MouseMoveMode = MMPanAndZoomCamera
  204.         End If
  205.     End If
  206.     If MouseMoveMode <> MMNoAction Then
  207.         LastX = x
  208.         LastY = y
  209.     End If
  210. End Sub
  211.  
  212. Sub Form_MouseMove (Button As Integer, Shift As Integer, x As Single, y As Single)
  213.     Dim Parent As Long
  214.     Dim tmpMatrix As Long
  215.     Dim worldToLocal As Long
  216.     Dim Up As RwV3d
  217.     Dim Rght As RwV3d
  218.     Dim At As RwV3d
  219.     Dim xDelta As Single
  220.     Dim yDelta As Single
  221.     Dim xAngle As Single
  222.     Dim yAngle As Single
  223.     Dim ZoomDelta As Single
  224.     Dim xOffset As Long
  225.     Dim yOffset As Long
  226.     Dim VRet As Variant
  227.     
  228.     x = x / Screen.TwipsPerPixelX
  229.     y = y / Screen.TwipsPerPixelY
  230.     Select Case MouseMoveMode
  231.     Case MMScrollBackdrop
  232.         VRet = RwGetCameraBackdropOffset(Camera, xOffset, yOffset)
  233.         xOffset = xOffset + LastX - x
  234.         yOffset = yOffset + LastY - y
  235.         VRet = RwSetCameraBackdropOffset(Camera, xOffset, yOffset)
  236.  
  237.     Case MMPanAndZoomCamera
  238.         VRet = RwVCMoveCamera(Camera, 0, 0, -CameraDistance)
  239.         VRet = RwTiltCamera(Camera, -CameraTilt)
  240.         VRet = RwPanCamera(Camera, LastX - x)
  241.         CameraDistance = CameraDistance + (LastY - y) / 10
  242.         VRet = RwTiltCamera(Camera, CameraTilt)
  243.         VRet = RwVCMoveCamera(Camera, 0, 0, CameraDistance)
  244.  
  245.     Case MMTiltCamera
  246.         VRet = RwVCMoveCamera(Camera, 0, 0, -CameraDistance)
  247.         VRet = RwTiltCamera(Camera, -CameraTilt)
  248.         CameraTilt = CameraTilt + LastY - y
  249.         VRet = RwTiltCamera(Camera, CameraTilt)
  250.         VRet = RwVCMoveCamera(Camera, 0, 0, CameraDistance)
  251.  
  252.     Case MMSpinClump
  253.         yAngle = x - LastX
  254.         xAngle = y - LastY
  255.         VRet = RwGetCameraLookUp(Camera, Up)
  256.         VRet = RwGetCameraLookRight(Camera, Rght)
  257.         Parent = RwGetClumpParent(PickedClump)
  258.         If Parent <> 0 Then
  259.             tmpMatrix = RwPushScratchMatrix()
  260.             worldToLocal = RwPushScratchMatrix()
  261.             VRet = RwGetClumpLTM(Parent, tmpMatrix)
  262.             VRet = RwInvertMatrix(tmpMatrix, worldToLocal)
  263.             VRet = RwTransformVector(Up, worldToLocal)
  264.             VRet = RwNormalize(Up)
  265.             VRet = RwTransformVector(Rght, worldToLocal)
  266.             VRet = RwNormalize(Rght)
  267.             VRet = RwPopScratchMatrix()
  268.             VRet = RwPopScratchMatrix()
  269.         End If
  270.         VRet = RwRotateMatrix(SpinMatrix, Up.x, Up.y, Up.z, yAngle, rwREPLACE)
  271.         VRet = RwRotateMatrix(SpinMatrix, Rght.x, Rght.y, Rght.z, xAngle, rwPOSTCONCAT)
  272.         VRet = RwTransformClumpJoint(PickedClump, SpinMatrix, rwPOSTCONCAT)
  273.         AnimMode = ASpinClump
  274.  
  275.     Case MMDragClump
  276.         VRet = RwPushScratchMatrix()
  277.         xDelta = (x - LastX) / 50
  278.         yDelta = (LastY - y) / 50
  279.         VRet = RwGetCameraLookRight(Camera, Rght)
  280.         VRet = RwGetCameraLookUp(Camera, Up)
  281.         Parent = RwGetClumpParent(PickedClump)
  282.         If Parent <> 0 Then
  283.             tmpMatrix = RwPushScratchMatrix()
  284.             worldToLocal = RwPushScratchMatrix()
  285.             VRet = RwGetClumpLTM(Parent, tmpMatrix)
  286.             VRet = RwInvertMatrix(tmpMatrix, worldToLocal)
  287.             VRet = RwTransformVector(Up, worldToLocal)
  288.             VRet = RwNormalize(Up)
  289.             VRet = RwTransformVector(Rght, worldToLocal)
  290.             VRet = RwNormalize(Rght)
  291.             VRet = RwPopScratchMatrix()
  292.             VRet = RwPopScratchMatrix()
  293.         End If
  294.         VRet = RwTranslateMatrix(RwScratchMatrix(), Rght.x * xDelta, Rght.y * xDelta, Rght.z * xDelta, rwREPLACE)
  295.         VRet = RwTranslateMatrix(RwScratchMatrix(), Up.x * yDelta, Up.y * yDelta, Up.z * yDelta, rwPOSTCONCAT)
  296.         VRet = RwTransformClump(PickedClump, RwScratchMatrix(), rwPOSTCONCAT)
  297.         VRet = RwPopScratchMatrix()
  298.                 
  299.     Case MMDragClumpInZ
  300.         VRet = RwPushScratchMatrix()
  301.         yDelta = (LastY - y) / 50
  302.         VRet = RwGetCameraLookAt(Camera, At)
  303.         Parent = RwGetClumpParent(PickedClump)
  304.         If Parent <> 0 Then
  305.             tmpMatrix = RwPushScratchMatrix()
  306.             worldToLocal = RwPushScratchMatrix()
  307.             VRet = RwGetClumpLTM(Parent, tmpMatrix)
  308.             VRet = RwInvertMatrix(tmpMatrix, worldToLocal)
  309.             VRet = RwTransformVector(At, worldToLocal)
  310.             VRet = RwNormalize(At)
  311.             VRet = RwPopScratchMatrix()
  312.             VRet = RwPopScratchMatrix()
  313.         End If
  314.         VRet = RwTranslateMatrix(RwScratchMatrix(), At.x * yDelta, At.y * yDelta, At.z * yDelta, rwREPLACE)
  315.         VRet = RwTransformClump(PickedClump, RwScratchMatrix(), rwPOSTCONCAT)
  316.         VRet = RwPopScratchMatrix()
  317.                 
  318.     Case MMPanLight
  319.         Dim scratch As Long
  320.  
  321.         VRet = RwPushScratchMatrix()
  322.         scratch = RwScratchMatrix()
  323.         VRet = RwRotateMatrix(scratch, 0, 1, 0, LastX - x, rwREPLACE)
  324.         VRet = RwRotateMatrix(scratch, 1, 0, 0, LastY - y, rwPOSTCONCAT)
  325.         VRet = RwTransformLight(Light, scratch, rwPOSTCONCAT)
  326.         VRet = RwPopScratchMatrix()
  327.     End Select
  328.  
  329.     If MouseMoveMode <> MMNoAction Then
  330.         RenderScene hWnd, hDC
  331.         LastX = x
  332.         LastY = y
  333.     End If
  334. End Sub
  335.  
  336. Sub Form_MouseUp (Button As Integer, Shift As Integer, x As Single, y As Single)
  337.     MouseMoveMode = MMNoAction
  338. End Sub
  339.  
  340. Sub Form_Paint ()
  341.     Dim VRet As Variant
  342.  
  343.     VRet = RwInvalidateCameraViewport(Camera)
  344.     VRet = RwShowCameraImage(Camera, hDC)
  345. End Sub
  346.  
  347. Sub Form_Resize ()
  348.     Dim VRet As Variant
  349.     Dim IRet As Integer
  350.     Dim ScreenWidth As Integer
  351.     Dim ScreenHeight As Integer
  352.  
  353.     ScreenWidth = Width / Screen.TwipsPerPixelX
  354.     ScreenHeight = Height / Screen.TwipsPerPixelY
  355.     VRet = RwSetCameraViewport(Camera, 0, 0, ScreenWidth, ScreenHeight)
  356.     VRet = RwSetCameraBackdropViewportRect(Camera, 0, 0, ScreenWidth, ScreenHeight)
  357.     RenderScene hWnd, hDC
  358. End Sub
  359.  
  360. Sub Form_Unload (Cancel As Integer)
  361.     Dim VRet As Variant
  362.     Dim Raster As Long
  363.  
  364.     VRet = RwDestroyMatrix(SpinMatrix)
  365.     VRet = RwDestroyScene(Scene)
  366.     Raster = RwGetCameraBackdrop(Camera)
  367.     If Raster Then
  368.         VRet = RwDestroyRaster(Raster)
  369.         VRet = RwSetCameraBackdrop(Camera, 0)
  370.     End If
  371.     VRet = RwDestroyCamera(Camera)
  372.     RwClose
  373. End Sub
  374.  
  375. Sub Open_Click ()
  376.     Dim clump As Long
  377.     Dim Raster As Long
  378.     Dim Extension As String
  379.     Dim Filename As String
  380.     Dim VRet As Long
  381.  
  382.     OpenFileDlg.Action = 1
  383.     Filename = OpenFileDlg.Filename
  384.     Extension = Right$(Filename, Len(Filename) - InStr(Filename, "."))
  385.     If Extension = "BMP" Or Extension = "RAS" Then
  386.         Raster = RwReadRaster(Filename, rwGAMMARASTER Or rwDITHERRASTER)
  387.         If Raster <> 0 Then
  388.             If RwGetCameraBackdrop(Camera) <> 0 Then
  389.                 VRet = RwDestroyRaster(RwGetCameraBackdrop(Camera))
  390.             End If
  391.             VRet = RwSetCameraBackdrop(Camera, Raster)
  392.             VRet = RwSetCameraBackdropOffset(Camera, 0, 0)
  393.             RenderScene hWnd, hDC
  394.         Else
  395.             ReportFileError Filename
  396.         End If
  397.     ElseIf Extension = "RWX" Then
  398.         clump = RwReadShape(Filename)
  399.         If clump <> 0 Then
  400.             VRet = RwAddClumpToScene(Scene, clump)
  401.             RenderScene hWnd, hDC
  402.         Else
  403.             ReportFileError Filename
  404.         End If
  405.     End If
  406. End Sub
  407.  
  408. Sub RenderScene (ByVal hWnd As Integer, ByVal hDC As Integer)
  409.     Dim VRet As Long
  410.  
  411.     VRet = RwBeginCameraUpdate(Camera, hWnd)
  412.     VRet = RwClearCameraViewport(Camera)
  413.     VRet = RwRenderScene(Scene)
  414.     VRet = RwEndCameraUpdate(Camera)
  415.     VRet = RwShowCameraImage(Camera, hDC)
  416. End Sub
  417.  
  418. Sub ReportFileError (Filename As String)
  419.     Select Case RwGetError()
  420.     Case E_RW_NOMEM
  421.         MsgBox "Insufficient memory to load file " + Filename, MB_OK
  422.     Case Else
  423.         MsgBox "Error reading file " + Filename, MB_OK
  424.     End Select
  425. End Sub
  426.  
  427. Sub tmrAnimate_Timer ()
  428.     Dim VRet As Variant
  429.  
  430.     If PickedClump <> 0 And AnimMode <> ANoAction And MouseMoveMode <> MMSpinClump Then
  431.         Select Case AnimMode
  432.         Case ASpinClump:
  433.             VRet = RwTransformClumpJoint(PickedClump, SpinMatrix, rwPOSTCONCAT)
  434.             FrameNumber = FrameNumber + 1
  435.             If FrameNumber >= 128 Then
  436.                 FrameNumber = 0
  437.                 VRet = RwPushScratchMatrix()
  438.                 VRet = RwGetClumpJointMatrix(PickedClump, RwScratchMatrix())
  439.                 VRet = RwOrthoNormalizeMatrix(RwScratchMatrix(), RwScratchMatrix())
  440.                 VRet = RwTransformClumpJoint(PickedClump, RwScratchMatrix(), rwREPLACE)
  441.                 VRet = RwPopScratchMatrix()
  442.             End If
  443.             RenderScene hWnd, hDC
  444.         End Select
  445.     End If
  446. End Sub
  447.  
  448.