home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH14 / SRC / FOLD.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-05-02  |  14.7 KB  |  476 lines

  1. VERSION 4.00
  2. Begin VB.Form FoldForm 
  3.    Appearance      =   0  'Flat
  4.    BackColor       =   &H00C0C0C0&
  5.    Caption         =   "Fold"
  6.    ClientHeight    =   5310
  7.    ClientLeft      =   1380
  8.    ClientTop       =   1035
  9.    ClientWidth     =   6870
  10.    BeginProperty Font 
  11.       name            =   "MS Sans Serif"
  12.       charset         =   1
  13.       weight          =   700
  14.       size            =   8.25
  15.       underline       =   0   'False
  16.       italic          =   0   'False
  17.       strikethrough   =   0   'False
  18.    EndProperty
  19.    ForeColor       =   &H80000008&
  20.    Height          =   6000
  21.    KeyPreview      =   -1  'True
  22.    Left            =   1320
  23.    LinkTopic       =   "Form1"
  24.    ScaleHeight     =   5310
  25.    ScaleWidth      =   6870
  26.    Top             =   405
  27.    Width           =   6990
  28.    Begin VB.TextBox FPSText 
  29.       Height          =   285
  30.       Left            =   6480
  31.       TabIndex        =   11
  32.       Text            =   "10"
  33.       Top             =   2040
  34.       Width           =   375
  35.    End
  36.    Begin VB.CommandButton CmdGo 
  37.       Caption         =   "Go"
  38.       Default         =   -1  'True
  39.       Height          =   495
  40.       Left            =   5640
  41.       TabIndex        =   10
  42.       Top             =   2520
  43.       Width           =   975
  44.    End
  45.    Begin VB.TextBox DText 
  46.       Height          =   285
  47.       Left            =   6000
  48.       TabIndex        =   9
  49.       Text            =   "5"
  50.       Top             =   0
  51.       Width           =   735
  52.    End
  53.    Begin VB.Frame Frame1 
  54.       Caption         =   "Post-Rotations"
  55.       Height          =   1335
  56.       Index           =   1
  57.       Left            =   5400
  58.       TabIndex        =   1
  59.       Top             =   480
  60.       Width           =   1455
  61.       Begin VB.TextBox ZW2Text 
  62.          Height          =   285
  63.          Left            =   600
  64.          MaxLength       =   6
  65.          TabIndex        =   4
  66.          Text            =   "0.0"
  67.          Top             =   960
  68.          Width           =   735
  69.       End
  70.       Begin VB.TextBox YW2Text 
  71.          Height          =   285
  72.          Left            =   600
  73.          MaxLength       =   6
  74.          TabIndex        =   3
  75.          Text            =   "0.1"
  76.          Top             =   600
  77.          Width           =   735
  78.       End
  79.       Begin VB.TextBox XW2Text 
  80.          Height          =   285
  81.          Left            =   600
  82.          MaxLength       =   6
  83.          TabIndex        =   2
  84.          Text            =   "0.2"
  85.          Top             =   240
  86.          Width           =   735
  87.       End
  88.       Begin VB.Label Label1 
  89.          Caption         =   "Z"
  90.          Height          =   255
  91.          Index           =   11
  92.          Left            =   240
  93.          TabIndex        =   7
  94.          Top             =   960
  95.          Width           =   255
  96.       End
  97.       Begin VB.Label Label1 
  98.          Caption         =   "Y"
  99.          Height          =   255
  100.          Index           =   10
  101.          Left            =   240
  102.          TabIndex        =   6
  103.          Top             =   600
  104.          Width           =   255
  105.       End
  106.       Begin VB.Label Label1 
  107.          Caption         =   "X"
  108.          Height          =   255
  109.          Index           =   9
  110.          Left            =   240
  111.          TabIndex        =   5
  112.          Top             =   240
  113.          Width           =   255
  114.       End
  115.    End
  116.    Begin VB.PictureBox Pict 
  117.       AutoRedraw      =   -1  'True
  118.       Height          =   5295
  119.       Left            =   0
  120.       ScaleHeight     =   349
  121.       ScaleMode       =   3  'Pixel
  122.       ScaleWidth      =   349
  123.       TabIndex        =   0
  124.       Top             =   0
  125.       Width           =   5295
  126.    End
  127.    Begin VB.Label Label1 
  128.       Caption         =   "Frames/Sec"
  129.       Height          =   255
  130.       Index           =   0
  131.       Left            =   5400
  132.       TabIndex        =   12
  133.       Top             =   2040
  134.       Width           =   1095
  135.    End
  136.    Begin VB.Label Label1 
  137.       Caption         =   "D"
  138.       Height          =   255
  139.       Index           =   12
  140.       Left            =   5640
  141.       TabIndex        =   8
  142.       Top             =   0
  143.       Width           =   255
  144.    End
  145.    Begin VB.Menu mnuFile 
  146.       Caption         =   "&File"
  147.       Begin VB.Menu mnuFileExit 
  148.          Caption         =   "E&xit"
  149.       End
  150.    End
  151. Attribute VB_Name = "FoldForm"
  152. Attribute VB_Creatable = False
  153. Attribute VB_Exposed = False
  154. Option Explicit
  155. ' Location of focus point.
  156. Const FocusX = 0#
  157. Const FocusY = 0#
  158. Const FocusZ = 0#
  159. Dim ThePicture As ObjPicture
  160. Dim TheCubes(1 To 8) As ObjPicture
  161. Dim Running As Boolean
  162. Dim Projector(1 To 5, 1 To 5) As Single
  163. ' ************************************************
  164. ' Animate the picture.
  165. ' ************************************************
  166. Private Sub Animate()
  167. Dim xw2_rot As Single
  168. Dim yw2_rot As Single
  169. Dim zw2_rot As Single
  170. Dim XW2(1 To 5, 1 To 5) As Single
  171. Dim YW2(1 To 5, 1 To 5) As Single
  172. Dim ZW2(1 To 5, 1 To 5) As Single
  173. Dim S(1 To 5, 1 To 5) As Single
  174. Dim T(1 To 5, 1 To 5) As Single
  175. Dim P(1 To 5, 1 To 5) As Single
  176. Dim M12(1 To 5, 1 To 5) As Single
  177. Dim M34(1 To 5, 1 To 5) As Single
  178. Dim M1_4(1 To 5, 1 To 5) As Single
  179. Dim M56(1 To 5, 1 To 5) As Single
  180. Dim D As Single
  181. Dim ms_per_frame As Long
  182. Dim cube12 As ObjPicture
  183.     If Not IsNumeric(XW2Text.Text) Then Exit Sub
  184.     If Not IsNumeric(YW2Text.Text) Then Exit Sub
  185.     If Not IsNumeric(ZW2Text.Text) Then Exit Sub
  186.     If Not IsNumeric(DText.Text) Then Exit Sub
  187.     If Not IsNumeric(FPSText) Then Exit Sub
  188.     xw2_rot = CSng(XW2Text.Text)
  189.     yw2_rot = CSng(YW2Text.Text)
  190.     zw2_rot = CSng(ZW2Text.Text)
  191.     D = CSng(DText.Text)
  192.     ms_per_frame = 1000 / CLng(FPSText)
  193.     ' Create fresh data.
  194.     CreateData
  195.     ' Calculate the matrices.
  196.     m4XWRotate XW2, xw2_rot
  197.     m4YWRotate YW2, yw2_rot
  198.     m4ZWRotate ZW2, zw2_rot
  199.     m4PerspectiveW P, D
  200.     m4Scale S, 25, -25, 1, 1
  201.     m4Translate T, Pict.ScaleWidth * 0.75, Pict.ScaleHeight / 2, 0, 0
  202.     m4MatMultiplyFull M12, P, XW2
  203.     m4MatMultiply M34, YW2, ZW2
  204.     m4MatMultiplyFull M1_4, M12, M34
  205.     m4MatMultiply M56, S, T
  206.     m4MatMultiplyFull Projector, M1_4, M56
  207.     ' Present the original image.
  208.     If Not Running Then Exit Sub
  209.     ThePicture.ApplyFull Projector
  210.     Pict.Cls
  211.     ThePicture.Draw Pict
  212.     DoEvents
  213.     ' Fold up cube 5.
  214.     FoldYW ms_per_frame, TheCubes(5), 1, 0, PI / 2
  215.     If Not Running Then Exit Sub
  216.     ' Fold up cube 6.
  217.     FoldZW ms_per_frame, TheCubes(6), -1, 0, -PI / 2
  218.     If Not Running Then Exit Sub
  219.     ' Fold up cube 4.
  220.     FoldXW ms_per_frame, TheCubes(4), 1, 0, PI / 2
  221.     If Not Running Then Exit Sub
  222.     ' Fold up cube 7.
  223.     FoldYW ms_per_frame, TheCubes(7), -1, 0, -PI / 2
  224.     If Not Running Then Exit Sub
  225.     ' Fold up cube 8.
  226.     FoldZW ms_per_frame, TheCubes(8), 1, 0, PI / 2
  227.     If Not Running Then Exit Sub
  228.     ' Fold up cubes 2 and 1 together.
  229.     Set cube12 = New ObjPicture
  230.     cube12.objects.Add TheCubes(2)
  231.     cube12.objects.Add TheCubes(1)
  232.     FoldXW ms_per_frame, cube12, -1, 0, -PI / 2
  233.     If Not Running Then Exit Sub
  234.     ' Finish folding cube 1.
  235.     FoldXW ms_per_frame, TheCubes(1), -1, 2, -PI / 2
  236.     If Not Running Then Exit Sub
  237. End Sub
  238. ' ************************************************
  239. ' Animate folding this cube across the X = x,
  240. ' W = w plane.
  241. ' ************************************************
  242. Sub FoldXW(ms_per_frame As Long, cube As ObjPicture, x As Single, w As Single, theta As Single)
  243. Const NUM_FRAMES = 20
  244. Dim i As Single
  245. Dim next_time As Long
  246. Dim T1(1 To 5, 1 To 5) As Single
  247. Dim R(1 To 5, 1 To 5) As Single
  248. Dim T2(1 To 5, 1 To 5) As Single
  249. Dim T1R(1 To 5, 1 To 5) As Single
  250. Dim All(1 To 5, 1 To 5) As Single
  251.     next_time = GetTickCount + ms_per_frame
  252.     ' Create the transformation matrices.
  253.     m4Translate T1, -x, 0, 0, -w
  254.     m4Translate T2, x, 0, 0, w
  255.     m4YZRotate R, theta / NUM_FRAMES
  256.     m4MatMultiply T1R, T1, R
  257.     m4MatMultiply All, T1R, T2
  258.     For i = 1 To NUM_FRAMES
  259.         If Not Running Then Exit Sub
  260.         
  261.         ' Rotate the cube.
  262.         cube.Apply All
  263.         cube.FixPoints
  264.         
  265.         ' Wait until it's time for the next image.
  266.         WaitTill next_time
  267.         next_time = GetTickCount + ms_per_frame
  268.         ' Display the picture.
  269.         ThePicture.ApplyFull Projector
  270.         Pict.Cls
  271.         ThePicture.Draw Pict
  272.         DoEvents
  273.     Next i
  274. End Sub
  275. ' ************************************************
  276. ' Animate folding this cube across the Y = y,
  277. ' W = w plane.
  278. ' ************************************************
  279. Sub FoldYW(ms_per_frame As Long, cube As ObjPicture, y As Single, w As Single, theta As Single)
  280. Const NUM_FRAMES = 20
  281. Dim i As Single
  282. Dim next_time As Long
  283. Dim T1(1 To 5, 1 To 5) As Single
  284. Dim R(1 To 5, 1 To 5) As Single
  285. Dim T2(1 To 5, 1 To 5) As Single
  286. Dim T1R(1 To 5, 1 To 5) As Single
  287. Dim All(1 To 5, 1 To 5) As Single
  288.     next_time = GetTickCount + ms_per_frame
  289.     ' Create the transformation matrices.
  290.     m4Translate T1, 0, -y, 0, -w
  291.     m4Translate T2, 0, y, 0, w
  292.     m4XZRotate R, theta / NUM_FRAMES
  293.     m4MatMultiply T1R, T1, R
  294.     m4MatMultiply All, T1R, T2
  295.     For i = 1 To NUM_FRAMES
  296.         If Not Running Then Exit Sub
  297.         
  298.         ' Rotate the cube.
  299.         cube.Apply All
  300.         cube.FixPoints
  301.         
  302.         ' Wait until it's time for the next image.
  303.         WaitTill next_time
  304.         next_time = GetTickCount + ms_per_frame
  305.         ' Display the picture.
  306.         ThePicture.ApplyFull Projector
  307.         Pict.Cls
  308.         ThePicture.Draw Pict
  309.         DoEvents
  310.     Next i
  311. End Sub
  312. ' ************************************************
  313. ' Animate folding this cube across the Z = z,
  314. ' W = w plane.
  315. ' ************************************************
  316. Sub FoldZW(ms_per_frame As Long, cube As ObjPicture, z As Single, w As Single, theta As Single)
  317. Const NUM_FRAMES = 20
  318. Dim i As Single
  319. Dim next_time As Long
  320. Dim T1(1 To 5, 1 To 5) As Single
  321. Dim R(1 To 5, 1 To 5) As Single
  322. Dim T2(1 To 5, 1 To 5) As Single
  323. Dim T1R(1 To 5, 1 To 5) As Single
  324. Dim All(1 To 5, 1 To 5) As Single
  325.     next_time = GetTickCount + ms_per_frame
  326.     ' Create the transformation matrices.
  327.     m4Translate T1, 0, 0, -z, -w
  328.     m4Translate T2, 0, 0, z, w
  329.     m4XYRotate R, theta / NUM_FRAMES
  330.     m4MatMultiply T1R, T1, R
  331.     m4MatMultiply All, T1R, T2
  332.     For i = 1 To NUM_FRAMES
  333.         If Not Running Then Exit Sub
  334.         
  335.         ' Rotate the cube.
  336.         cube.Apply All
  337.         cube.FixPoints
  338.         
  339.         ' Wait until it's time for the next image.
  340.         WaitTill next_time
  341.         next_time = GetTickCount + ms_per_frame
  342.         ' Display the picture.
  343.         ThePicture.ApplyFull Projector
  344.         Pict.Cls
  345.         ThePicture.Draw Pict
  346.         DoEvents
  347.     Next i
  348. End Sub
  349. ' ************************************************
  350. ' Create a cube with the indicated minimum
  351. ' coordinates. W = 0 for all points.
  352. ' ************************************************
  353. Sub CreateCube(cube As ObjPicture, xmin As Single, ymin As Single, zmin As Single)
  354. Dim pline As ObjPolyline4D
  355. Dim x As Single
  356. Dim y As Single
  357. Dim z As Single
  358.     Set cube = New ObjPicture
  359.     ThePicture.objects.Add cube
  360.     Set pline = New ObjPolyline4D
  361.     cube.objects.Add pline
  362.     For x = xmin To xmin + 2 Step 2
  363.         For y = ymin To ymin + 2 Step 2
  364.             For z = zmin To zmin + 2 Step 2
  365.                 If x = xmin Then _
  366.                     pline.AddSegment _
  367.                         x, y, z, 0, _
  368.                         x + 2, y, z, 0
  369.                 If y = ymin Then _
  370.                     pline.AddSegment _
  371.                         x, y, z, 0, _
  372.                         x, y + 2, z, 0
  373.                 If z = zmin Then _
  374.                     pline.AddSegment _
  375.                         x, y, z, 0, _
  376.                         x, y, z + 2, 0
  377.             Next z
  378.         Next y
  379.     Next x
  380. End Sub
  381. ' ************************************************
  382. ' Display the data as it currently is.
  383. ' ************************************************
  384. Sub DrawData()
  385. Dim xw2_rot As Single
  386. Dim yw2_rot As Single
  387. Dim zw2_rot As Single
  388. Dim XW2(1 To 5, 1 To 5) As Single
  389. Dim YW2(1 To 5, 1 To 5) As Single
  390. Dim ZW2(1 To 5, 1 To 5) As Single
  391. Dim S(1 To 5, 1 To 5) As Single
  392. Dim T(1 To 5, 1 To 5) As Single
  393. Dim P(1 To 5, 1 To 5) As Single
  394. Dim M12(1 To 5, 1 To 5) As Single
  395. Dim M34(1 To 5, 1 To 5) As Single
  396. Dim M1_4(1 To 5, 1 To 5) As Single
  397. Dim M56(1 To 5, 1 To 5) As Single
  398. Dim D As Single
  399.     If Not IsNumeric(XW2Text.Text) Then Exit Sub
  400.     If Not IsNumeric(YW2Text.Text) Then Exit Sub
  401.     If Not IsNumeric(ZW2Text.Text) Then Exit Sub
  402.     If Not IsNumeric(DText.Text) Then Exit Sub
  403.     xw2_rot = CSng(XW2Text.Text)
  404.     yw2_rot = CSng(YW2Text.Text)
  405.     zw2_rot = CSng(ZW2Text.Text)
  406.     D = CSng(DText.Text)
  407.     ' Calculate the matrices.
  408.     m4XWRotate XW2, xw2_rot
  409.     m4YWRotate YW2, yw2_rot
  410.     m4ZWRotate ZW2, zw2_rot
  411.     m4PerspectiveW P, D
  412.     m4Scale S, 25, -25, 1, 1
  413.     m4Translate T, Pict.ScaleWidth * 0.75, Pict.ScaleHeight / 2, 0, 0
  414.     m4MatMultiplyFull M12, P, XW2
  415.     m4MatMultiply M34, YW2, ZW2
  416.     m4MatMultiplyFull M1_4, M12, M34
  417.     m4MatMultiply M56, S, T
  418.     m4MatMultiplyFull Projector, M1_4, M56
  419.     ThePicture.ApplyFull Projector
  420.     Pict.Cls
  421.     ThePicture.Draw Pict
  422. End Sub
  423. Private Sub CmdGo_Click()
  424.     If Running Then
  425.         CmdGo.Caption = "Stopped"
  426.         CmdGo.Enabled = False
  427.         Running = False
  428.     Else
  429.         CmdGo.Caption = "Stop"
  430.         Running = True
  431.         Animate
  432.         CmdGo.Enabled = True
  433.         CmdGo.Caption = "Go"
  434.         Running = False
  435.     End If
  436. End Sub
  437. Private Sub DText_Change()
  438.     DrawData
  439. End Sub
  440. Private Sub Form_Load()
  441.     ' Create the data.
  442.     CreateData
  443. End Sub
  444. ' ************************************************
  445. ' Create the initial cubes.
  446. ' ************************************************
  447. Sub CreateData()
  448.     MousePointer = vbHourglass
  449.     Refresh
  450.     Set ThePicture = New ObjPicture
  451.     CreateCube TheCubes(1), -5, -1, -1
  452.     CreateCube TheCubes(2), -3, -1, -1
  453.     CreateCube TheCubes(3), -1, -1, -1
  454.     CreateCube TheCubes(4), 1, -1, -1
  455.     CreateCube TheCubes(5), -1, 1, -1
  456.     CreateCube TheCubes(6), -1, -1, -3
  457.     CreateCube TheCubes(7), -1, -3, -1
  458.     CreateCube TheCubes(8), -1, -1, 1
  459.     MousePointer = vbDefault
  460. End Sub
  461. Private Sub Form_Unload(Cancel As Integer)
  462.     End
  463. End Sub
  464. Private Sub mnuFileExit_Click()
  465.     Unload Me
  466. End Sub
  467. Private Sub XW2Text_Change()
  468.     DrawData
  469. End Sub
  470. Private Sub YW2Text_Change()
  471.     DrawData
  472. End Sub
  473. Private Sub ZW2Text_Change()
  474.     DrawData
  475. End Sub
  476.