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 (.txt)
Encoding:
Visual Basic Form  |  1995-11-14  |  14.2 KB  |  397 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. Const SHIFT_MASK = 1
  43. Const CTRL_MASK = 2
  44. Const ALT_MASK = 4
  45. Const LEFT_BUTTON = 1
  46. Const RIGHT_BUTTON = 2
  47. Const MIDDLE_BUTTON = 4
  48. Dim Scene As Long
  49. Dim Camera As Long
  50. Dim CameraTilt As Single
  51. Dim CameraDistance As Single
  52. Dim Light As Long
  53. Dim SpinMatrix As Long
  54. Dim LastX As Integer
  55. Dim LastY As Integer
  56. Dim AnimMode As Integer
  57. Dim FrameNumber As Integer
  58. Const ANoAction = 0
  59. Const ASpinClump = 1
  60. Dim PickedClump As Long
  61. Dim MouseMoveMode As Integer
  62. Const MMNoAction = 0
  63. Const MMDragClump = 1
  64. Const MMDragClumpInZ = 2
  65. Const MMPanLight = 3
  66. Const MMPanAndZoomCamera = 4
  67. Const MMScrollBackdrop = 5
  68. Const MMSpinClump = 6
  69. Const MMTiltCamera = 7
  70. Sub About_Click ()
  71.     frmAbout.Show 1
  72. End Sub
  73. Sub Exit_Click ()
  74.     Unload Me
  75. End Sub
  76. Sub Form_Load ()
  77.     Dim VRet As Variant
  78.     CameraTilt = 0
  79.     CameraDistance = -7
  80.     FrameNumber = 0
  81.     If RwOpen("MSWindows", 0) = 0 Then
  82.         If RwGetError() = E_RW_NOMEM Then
  83.             MsgBox "Insufficient memory to open the RenderWare(tm) library", MB_OK
  84.         Else
  85.             MsgBox "Error opening the RenderWare(tm) library", MB_OK
  86.         End If
  87.         End
  88.     End If
  89.     Camera = RwCreateCamera(1024, 768, 0)
  90.     If Camera = 0 Then
  91.         If RwGetError() = E_RW_NOMEM Then
  92.             MsgBox "Insufficient memory to create the RenderWare(tm) camera", MB_OK
  93.         Else
  94.             MsgBox "Error creating the RenderWare(tm) camera", MB_OK
  95.         End If
  96.         RwClose
  97.         End
  98.     End If
  99.     VRet = RwSetCameraBackColor(Camera, 0, 0, 1)
  100.     VRet = RwTiltCamera(Camera, CameraTilt)
  101.     VRet = RwVCMoveCamera(Camera, 0, 0, CameraDistance)
  102.     VRet = RwSetCameraViewwindow(Camera, .2, .2)
  103.     Scene = RwCreateScene()
  104.     If Scene = 0 Then
  105.         VRet = RwDestroyCamera(Camera)
  106.         RwClose
  107.         MsgBox "Error creating the RenderWare(tm) scene", MB_OK
  108.         End
  109.     End If
  110.     Light = RwCreateLight(rwDIRECTIONAL, -1, -1, -1, 1)
  111.     If Light = 0 Then
  112.         VRet = RwDestroyScene(Scene)
  113.         VRet = RwDestroyCamera(Camera)
  114.         RwClose
  115.         MsgBox "Error creating the RenderWare(tm) light", MB_OK
  116.         End
  117.     End If
  118.     VRet = RwAddLightToScene(Scene, Light)
  119.     SpinMatrix = RwCreateMatrix()
  120.     If SpinMatrix = 0 Then
  121.         VRet = RwDestroyScene(Scene)
  122.         VRet = RwDestroyCamera(Camera)
  123.         RwClose
  124.         MsgBox "Error creating the RenderWare(tm) matrix", MB_OK
  125.         End
  126.     End If
  127. End Sub
  128. Sub Form_MouseDown (Button As Integer, Shift As Integer, x As Single, y As Single)
  129.     Dim Pick As RwPickRecord
  130.     Dim VRet As Variant
  131.     x = x / Screen.TwipsPerPixelX
  132.     y = y / Screen.TwipsPerPixelY
  133.     If (Button And LEFT_BUTTON) <> 0 Then
  134.         If RwPickScene(Scene, x, y, Camera, Pick) <> 0 Then
  135.             Select Case Pick.type
  136.             Case rwNAPICKOBJECT
  137.                 AnimMode = ANoAction
  138.                 PickedClump = 0
  139.                 If RwGetCameraBackdrop(Camera) <> 0 Then
  140.                     If (Shift And (SHIFT_MASK Or CTRL_MASK)) = (SHIFT_MASK Or CTRL_MASK) Then
  141.                         VRet = RwDestroyRaster(RwGetCameraBackdrop(Camera))
  142.                         VRet = RwSetCameraBackdrop(Camera, 0)
  143.                         RenderScene hWnd, hDC
  144.                         MouseMoveMode = MMNoAction
  145.                     Else
  146.                         MouseMoveMode = MMScrollBackdrop
  147.                     End If
  148.                 Else
  149.                     MouseMoveMode = MMNoAction
  150.                 End If
  151.             Case rwPICKEDCLUMP
  152.                 If (Shift And (SHIFT_MASK Or CTRL_MASK)) = (SHIFT_MASK Or CTRL_MASK) Then
  153.                     VRet = RwDestroyClump(Pick.Object.clump.clump)
  154.                     RenderScene hWnd, hDC
  155.                     MouseMoveMode = MMNoAction
  156.                     PickedClump = 0
  157.                 Else
  158.                     PickedClump = Pick.Object.clump.clump
  159.                     If (Shift And SHIFT_MASK) <> 0 Then
  160.                         MouseMoveMode = MMDragClump
  161.                     ElseIf (Shift And CTRL_MASK) <> 0 Then
  162.                         MouseMoveMode = MMDragClumpInZ
  163.                     Else
  164.                         MouseMoveMode = MMSpinClump
  165.                     End If
  166.                 End If
  167.             End Select
  168.         End If
  169.     ElseIf (Button And RIGHT_BUTTON) <> 0 Then
  170.         If (Shift And CTRL_MASK) <> 0 Then
  171.             MouseMoveMode = MMPanLight
  172.         ElseIf (Shift And SHIFT_MASK) <> 0 Then
  173.             MouseMoveMode = MMTiltCamera
  174.         Else
  175.             MouseMoveMode = MMPanAndZoomCamera
  176.         End If
  177.     End If
  178.     If MouseMoveMode <> MMNoAction Then
  179.         LastX = x
  180.         LastY = y
  181.     End If
  182. End Sub
  183. Sub Form_MouseMove (Button As Integer, Shift As Integer, x As Single, y As Single)
  184.     Dim Parent As Long
  185.     Dim tmpMatrix As Long
  186.     Dim worldToLocal As Long
  187.     Dim Up As RwV3d
  188.     Dim Rght As RwV3d
  189.     Dim At As RwV3d
  190.     Dim xDelta As Single
  191.     Dim yDelta As Single
  192.     Dim xAngle As Single
  193.     Dim yAngle As Single
  194.     Dim ZoomDelta As Single
  195.     Dim xOffset As Long
  196.     Dim yOffset As Long
  197.     Dim VRet As Variant
  198.     x = x / Screen.TwipsPerPixelX
  199.     y = y / Screen.TwipsPerPixelY
  200.     Select Case MouseMoveMode
  201.     Case MMScrollBackdrop
  202.         VRet = RwGetCameraBackdropOffset(Camera, xOffset, yOffset)
  203.         xOffset = xOffset + LastX - x
  204.         yOffset = yOffset + LastY - y
  205.         VRet = RwSetCameraBackdropOffset(Camera, xOffset, yOffset)
  206.     Case MMPanAndZoomCamera
  207.         VRet = RwVCMoveCamera(Camera, 0, 0, -CameraDistance)
  208.         VRet = RwTiltCamera(Camera, -CameraTilt)
  209.         VRet = RwPanCamera(Camera, LastX - x)
  210.         CameraDistance = CameraDistance + (LastY - y) / 10
  211.         VRet = RwTiltCamera(Camera, CameraTilt)
  212.         VRet = RwVCMoveCamera(Camera, 0, 0, CameraDistance)
  213.     Case MMTiltCamera
  214.         VRet = RwVCMoveCamera(Camera, 0, 0, -CameraDistance)
  215.         VRet = RwTiltCamera(Camera, -CameraTilt)
  216.         CameraTilt = CameraTilt + LastY - y
  217.         VRet = RwTiltCamera(Camera, CameraTilt)
  218.         VRet = RwVCMoveCamera(Camera, 0, 0, CameraDistance)
  219.     Case MMSpinClump
  220.         yAngle = x - LastX
  221.         xAngle = y - LastY
  222.         VRet = RwGetCameraLookUp(Camera, Up)
  223.         VRet = RwGetCameraLookRight(Camera, Rght)
  224.         Parent = RwGetClumpParent(PickedClump)
  225.         If Parent <> 0 Then
  226.             tmpMatrix = RwPushScratchMatrix()
  227.             worldToLocal = RwPushScratchMatrix()
  228.             VRet = RwGetClumpLTM(Parent, tmpMatrix)
  229.             VRet = RwInvertMatrix(tmpMatrix, worldToLocal)
  230.             VRet = RwTransformVector(Up, worldToLocal)
  231.             VRet = RwNormalize(Up)
  232.             VRet = RwTransformVector(Rght, worldToLocal)
  233.             VRet = RwNormalize(Rght)
  234.             VRet = RwPopScratchMatrix()
  235.             VRet = RwPopScratchMatrix()
  236.         End If
  237.         VRet = RwRotateMatrix(SpinMatrix, Up.x, Up.y, Up.z, yAngle, rwREPLACE)
  238.         VRet = RwRotateMatrix(SpinMatrix, Rght.x, Rght.y, Rght.z, xAngle, rwPOSTCONCAT)
  239.         VRet = RwTransformClumpJoint(PickedClump, SpinMatrix, rwPOSTCONCAT)
  240.         AnimMode = ASpinClump
  241.     Case MMDragClump
  242.         VRet = RwPushScratchMatrix()
  243.         xDelta = (x - LastX) / 50
  244.         yDelta = (LastY - y) / 50
  245.         VRet = RwGetCameraLookRight(Camera, Rght)
  246.         VRet = RwGetCameraLookUp(Camera, Up)
  247.         Parent = RwGetClumpParent(PickedClump)
  248.         If Parent <> 0 Then
  249.             tmpMatrix = RwPushScratchMatrix()
  250.             worldToLocal = RwPushScratchMatrix()
  251.             VRet = RwGetClumpLTM(Parent, tmpMatrix)
  252.             VRet = RwInvertMatrix(tmpMatrix, worldToLocal)
  253.             VRet = RwTransformVector(Up, worldToLocal)
  254.             VRet = RwNormalize(Up)
  255.             VRet = RwTransformVector(Rght, worldToLocal)
  256.             VRet = RwNormalize(Rght)
  257.             VRet = RwPopScratchMatrix()
  258.             VRet = RwPopScratchMatrix()
  259.         End If
  260.         VRet = RwTranslateMatrix(RwScratchMatrix(), Rght.x * xDelta, Rght.y * xDelta, Rght.z * xDelta, rwREPLACE)
  261.         VRet = RwTranslateMatrix(RwScratchMatrix(), Up.x * yDelta, Up.y * yDelta, Up.z * yDelta, rwPOSTCONCAT)
  262.         VRet = RwTransformClump(PickedClump, RwScratchMatrix(), rwPOSTCONCAT)
  263.         VRet = RwPopScratchMatrix()
  264.                 
  265.     Case MMDragClumpInZ
  266.         VRet = RwPushScratchMatrix()
  267.         yDelta = (LastY - y) / 50
  268.         VRet = RwGetCameraLookAt(Camera, At)
  269.         Parent = RwGetClumpParent(PickedClump)
  270.         If Parent <> 0 Then
  271.             tmpMatrix = RwPushScratchMatrix()
  272.             worldToLocal = RwPushScratchMatrix()
  273.             VRet = RwGetClumpLTM(Parent, tmpMatrix)
  274.             VRet = RwInvertMatrix(tmpMatrix, worldToLocal)
  275.             VRet = RwTransformVector(At, worldToLocal)
  276.             VRet = RwNormalize(At)
  277.             VRet = RwPopScratchMatrix()
  278.             VRet = RwPopScratchMatrix()
  279.         End If
  280.         VRet = RwTranslateMatrix(RwScratchMatrix(), At.x * yDelta, At.y * yDelta, At.z * yDelta, rwREPLACE)
  281.         VRet = RwTransformClump(PickedClump, RwScratchMatrix(), rwPOSTCONCAT)
  282.         VRet = RwPopScratchMatrix()
  283.                 
  284.     Case MMPanLight
  285.         Dim scratch As Long
  286.         VRet = RwPushScratchMatrix()
  287.         scratch = RwScratchMatrix()
  288.         VRet = RwRotateMatrix(scratch, 0, 1, 0, LastX - x, rwREPLACE)
  289.         VRet = RwRotateMatrix(scratch, 1, 0, 0, LastY - y, rwPOSTCONCAT)
  290.         VRet = RwTransformLight(Light, scratch, rwPOSTCONCAT)
  291.         VRet = RwPopScratchMatrix()
  292.     End Select
  293.     If MouseMoveMode <> MMNoAction Then
  294.         RenderScene hWnd, hDC
  295.         LastX = x
  296.         LastY = y
  297.     End If
  298. End Sub
  299. Sub Form_MouseUp (Button As Integer, Shift As Integer, x As Single, y As Single)
  300.     MouseMoveMode = MMNoAction
  301. End Sub
  302. Sub Form_Paint ()
  303.     Dim VRet As Variant
  304.     VRet = RwInvalidateCameraViewport(Camera)
  305.     VRet = RwShowCameraImage(Camera, hDC)
  306. End Sub
  307. Sub Form_Resize ()
  308.     Dim VRet As Variant
  309.     Dim IRet As Integer
  310.     Dim ScreenWidth As Integer
  311.     Dim ScreenHeight As Integer
  312.     ScreenWidth = Width / Screen.TwipsPerPixelX
  313.     ScreenHeight = Height / Screen.TwipsPerPixelY
  314.     VRet = RwSetCameraViewport(Camera, 0, 0, ScreenWidth, ScreenHeight)
  315.     VRet = RwSetCameraBackdropViewportRect(Camera, 0, 0, ScreenWidth, ScreenHeight)
  316.     RenderScene hWnd, hDC
  317. End Sub
  318. Sub Form_Unload (Cancel As Integer)
  319.     Dim VRet As Variant
  320.     Dim Raster As Long
  321.     VRet = RwDestroyMatrix(SpinMatrix)
  322.     VRet = RwDestroyScene(Scene)
  323.     Raster = RwGetCameraBackdrop(Camera)
  324.     If Raster Then
  325.         VRet = RwDestroyRaster(Raster)
  326.         VRet = RwSetCameraBackdrop(Camera, 0)
  327.     End If
  328.     VRet = RwDestroyCamera(Camera)
  329.     RwClose
  330. End Sub
  331. Sub Open_Click ()
  332.     Dim clump As Long
  333.     Dim Raster As Long
  334.     Dim Extension As String
  335.     Dim Filename As String
  336.     Dim VRet As Long
  337.     OpenFileDlg.Action = 1
  338.     Filename = OpenFileDlg.Filename
  339.     Extension = Right$(Filename, Len(Filename) - InStr(Filename, "."))
  340.     If Extension = "BMP" Or Extension = "RAS" Then
  341.         Raster = RwReadRaster(Filename, rwGAMMARASTER Or rwDITHERRASTER)
  342.         If Raster <> 0 Then
  343.             If RwGetCameraBackdrop(Camera) <> 0 Then
  344.                 VRet = RwDestroyRaster(RwGetCameraBackdrop(Camera))
  345.             End If
  346.             VRet = RwSetCameraBackdrop(Camera, Raster)
  347.             VRet = RwSetCameraBackdropOffset(Camera, 0, 0)
  348.             RenderScene hWnd, hDC
  349.         Else
  350.             ReportFileError Filename
  351.         End If
  352.     ElseIf Extension = "RWX" Then
  353.         clump = RwReadShape(Filename)
  354.         If clump <> 0 Then
  355.             VRet = RwAddClumpToScene(Scene, clump)
  356.             RenderScene hWnd, hDC
  357.         Else
  358.             ReportFileError Filename
  359.         End If
  360.     End If
  361. End Sub
  362. Sub RenderScene (ByVal hWnd As Integer, ByVal hDC As Integer)
  363.     Dim VRet As Long
  364.     VRet = RwBeginCameraUpdate(Camera, hWnd)
  365.     VRet = RwClearCameraViewport(Camera)
  366.     VRet = RwRenderScene(Scene)
  367.     VRet = RwEndCameraUpdate(Camera)
  368.     VRet = RwShowCameraImage(Camera, hDC)
  369. End Sub
  370. Sub ReportFileError (Filename As String)
  371.     Select Case RwGetError()
  372.     Case E_RW_NOMEM
  373.         MsgBox "Insufficient memory to load file " + Filename, MB_OK
  374.     Case Else
  375.         MsgBox "Error reading file " + Filename, MB_OK
  376.     End Select
  377. End Sub
  378. Sub tmrAnimate_Timer ()
  379.     Dim VRet As Variant
  380.     If PickedClump <> 0 And AnimMode <> ANoAction And MouseMoveMode <> MMSpinClump Then
  381.         Select Case AnimMode
  382.         Case ASpinClump:
  383.             VRet = RwTransformClumpJoint(PickedClump, SpinMatrix, rwPOSTCONCAT)
  384.             FrameNumber = FrameNumber + 1
  385.             If FrameNumber >= 128 Then
  386.                 FrameNumber = 0
  387.                 VRet = RwPushScratchMatrix()
  388.                 VRet = RwGetClumpJointMatrix(PickedClump, RwScratchMatrix())
  389.                 VRet = RwOrthoNormalizeMatrix(RwScratchMatrix(), RwScratchMatrix())
  390.                 VRet = RwTransformClumpJoint(PickedClump, RwScratchMatrix(), rwREPLACE)
  391.                 VRet = RwPopScratchMatrix()
  392.             End If
  393.             RenderScene hWnd, hDC
  394.         End Select
  395.     End If
  396. End Sub
  397.