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

  1. VERSION 4.00
  2. Begin VB.Form AnimatedForm 
  3.    Appearance      =   0  'Flat
  4.    BackColor       =   &H00C0C0C0&
  5.    Caption         =   "Animated"
  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        =   15
  32.       Text            =   "10"
  33.       Top             =   3600
  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        =   14
  42.       Top             =   4080
  43.       Width           =   975
  44.    End
  45.    Begin VB.TextBox DText 
  46.       Height          =   285
  47.       Left            =   6000
  48.       TabIndex        =   10
  49.       Text            =   "3"
  50.       Top             =   1560
  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        =   2
  59.       Top             =   2040
  60.       Width           =   1455
  61.       Begin VB.TextBox ZW2Text 
  62.          Height          =   285
  63.          Left            =   600
  64.          MaxLength       =   6
  65.          TabIndex        =   5
  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        =   4
  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        =   3
  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        =   8
  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        =   7
  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        =   6
  112.          Top             =   240
  113.          Width           =   255
  114.       End
  115.    End
  116.    Begin VB.Frame Frame1 
  117.       Caption         =   "Pre-Rotations"
  118.       Height          =   1335
  119.       Index           =   0
  120.       Left            =   5400
  121.       TabIndex        =   1
  122.       Top             =   0
  123.       Width           =   1455
  124.       Begin VB.CheckBox YZCheck 
  125.          Caption         =   "YZ Plane"
  126.          Height          =   255
  127.          Left            =   120
  128.          TabIndex        =   13
  129.          Top             =   960
  130.          Width           =   1215
  131.       End
  132.       Begin VB.CheckBox XZCheck 
  133.          Caption         =   "XZ Plane"
  134.          Height          =   255
  135.          Left            =   120
  136.          TabIndex        =   12
  137.          Top             =   600
  138.          Width           =   1215
  139.       End
  140.       Begin VB.CheckBox XYCheck 
  141.          Caption         =   "XY Plane"
  142.          Height          =   255
  143.          Left            =   120
  144.          TabIndex        =   11
  145.          Top             =   240
  146.          Width           =   1215
  147.       End
  148.    End
  149.    Begin VB.PictureBox Pict 
  150.       AutoRedraw      =   -1  'True
  151.       Height          =   5295
  152.       Left            =   0
  153.       ScaleHeight     =   349
  154.       ScaleMode       =   3  'Pixel
  155.       ScaleWidth      =   349
  156.       TabIndex        =   0
  157.       Top             =   0
  158.       Width           =   5295
  159.    End
  160.    Begin VB.Label Label1 
  161.       Caption         =   "Frames/Sec"
  162.       Height          =   255
  163.       Index           =   0
  164.       Left            =   5400
  165.       TabIndex        =   16
  166.       Top             =   3600
  167.       Width           =   1095
  168.    End
  169.    Begin VB.Label Label1 
  170.       Caption         =   "D"
  171.       Height          =   255
  172.       Index           =   12
  173.       Left            =   5640
  174.       TabIndex        =   9
  175.       Top             =   1560
  176.       Width           =   255
  177.    End
  178.    Begin VB.Menu mnuFile 
  179.       Caption         =   "&File"
  180.       Begin VB.Menu mnuFileExit 
  181.          Caption         =   "E&xit"
  182.       End
  183.    End
  184. Attribute VB_Name = "AnimatedForm"
  185. Attribute VB_Creatable = False
  186. Attribute VB_Exposed = False
  187. Option Explicit
  188. ' Location of focus point.
  189. Const FocusX = 0#
  190. Const FocusY = 0#
  191. Const FocusZ = 0#
  192. Dim Projector(1 To 5, 1 To 5) As Single
  193. Dim ThePicture As ObjPicture
  194. Dim Running As Boolean
  195. ' ************************************************
  196. ' Animate the picture.
  197. ' ************************************************
  198. Private Sub Animate()
  199. Const Dtheta = PI / 40
  200. Dim xy_rot As Single
  201. Dim xz_rot As Single
  202. Dim yz_rot As Single
  203. Dim xw2_rot As Single
  204. Dim yw2_rot As Single
  205. Dim zw2_rot As Single
  206. Dim XY(1 To 5, 1 To 5) As Single
  207. Dim XZ(1 To 5, 1 To 5) As Single
  208. Dim YZ(1 To 5, 1 To 5) As Single
  209. Dim XW2(1 To 5, 1 To 5) As Single
  210. Dim YW2(1 To 5, 1 To 5) As Single
  211. Dim ZW2(1 To 5, 1 To 5) As Single
  212. Dim S(1 To 5, 1 To 5) As Single
  213. Dim T(1 To 5, 1 To 5) As Single
  214. Dim P(1 To 5, 1 To 5) As Single
  215. Dim M12(1 To 5, 1 To 5) As Single
  216. Dim M34(1 To 5, 1 To 5) As Single
  217. Dim M1_4(1 To 5, 1 To 5) As Single
  218. Dim M56(1 To 5, 1 To 5) As Single
  219. Dim M1_6(1 To 5, 1 To 5) As Single
  220. Dim M_All(1 To 5, 1 To 5) As Single
  221. Dim D As Single
  222. Dim AnimateXY As Boolean
  223. Dim AnimateXZ As Boolean
  224. Dim AnimateYZ As Boolean
  225. Dim next_time As Long
  226. Dim ms_per_frame As Long
  227.     If Not IsNumeric(XW2Text.Text) Then Exit Sub
  228.     If Not IsNumeric(YW2Text.Text) Then Exit Sub
  229.     If Not IsNumeric(ZW2Text.Text) Then Exit Sub
  230.     If Not IsNumeric(DText.Text) Then Exit Sub
  231.     If Not IsNumeric(FPSText) Then Exit Sub
  232.     xw2_rot = CSng(XW2Text.Text)
  233.     yw2_rot = CSng(YW2Text.Text)
  234.     zw2_rot = CSng(ZW2Text.Text)
  235.     D = CSng(DText.Text)
  236.     ms_per_frame = 1000 / CLng(FPSText)
  237.     MousePointer = vbHourglass
  238.     Refresh
  239.     ' Prevent overflow errors when drawing lines
  240.     ' too far out of bounds.
  241.     On Error Resume Next
  242.     ' Calculate the matrices that don't change.
  243.     m4XWRotate XW2, xw2_rot
  244.     m4YWRotate YW2, yw2_rot
  245.     m4ZWRotate ZW2, zw2_rot
  246.     ' Calculate the projection matrix.
  247.     m4PerspectiveW P, D
  248.     ' Scale and translate so it looks OK in pixels.
  249.     m4Scale S, 75, -75, 1, 1
  250.     m4Translate T, Pict.ScaleWidth / 2, Pict.ScaleHeight / 2, 0, 0
  251.     m4MatMultiplyFull M12, P, XW2
  252.     m4MatMultiply M34, YW2, ZW2
  253.     m4MatMultiplyFull M1_4, M12, M34
  254.     m4MatMultiply M56, S, T
  255.     m4MatMultiplyFull M1_6, M1_4, M56
  256.     ' See which rotations we are animating.
  257.     AnimateXY = (XYCheck.value = vbChecked)
  258.     AnimateXZ = (XZCheck.value = vbChecked)
  259.     AnimateYZ = (YZCheck.value = vbChecked)
  260.     ' Start the animation.
  261.     Do While Running
  262.         next_time = GetTickCount + ms_per_frame
  263.         
  264.         ' Calculate the changing transformations.
  265.         m4XYRotate XY, xy_rot
  266.         m4XZRotate XZ, xz_rot
  267.         m4YZRotate YZ, yz_rot
  268.         m4MatMultiply M12, XY, XZ
  269.         m4MatMultiply M1_4, M12, YZ
  270.         m4MatMultiplyFull M_All, M1_4, M1_6
  271.         If AnimateXY Then xy_rot = xy_rot + Dtheta
  272.         If AnimateXZ Then xz_rot = xz_rot + Dtheta
  273.         If AnimateYZ Then yz_rot = yz_rot + Dtheta
  274.         ' Transform the points.
  275.         ThePicture.ApplyFull M_All
  276.         ' Display the data.
  277.         Pict.Cls
  278.         ThePicture.Draw Pict
  279.         DoEvents
  280.         WaitTill next_time
  281.     Loop
  282.     MousePointer = vbDefault
  283. End Sub
  284. Private Sub CmdGo_Click()
  285.     If Running Then
  286.         CmdGo.Caption = "Stopped"
  287.         CmdGo.Enabled = False
  288.         Running = False
  289.     Else
  290.         CmdGo.Caption = "Stop"
  291.         Running = True
  292.         Animate
  293.         CmdGo.Enabled = True
  294.         CmdGo.Caption = "Go"
  295.         Running = False
  296.     End If
  297. End Sub
  298. Private Sub Form_Load()
  299.     ' Create the data.
  300.     CreateData
  301. End Sub
  302. ' ************************************************
  303. ' Create the surface.
  304. ' ************************************************
  305. Sub CreateData()
  306. Dim pline As ObjPolyline4D
  307. Dim x As Integer
  308. Dim y As Integer
  309. Dim z As Integer
  310. Dim w As Integer
  311.     MousePointer = vbHourglass
  312.     Refresh
  313.     Set ThePicture = New ObjPicture
  314.     Set pline = New ObjPolyline4D
  315.     ThePicture.objects.Add pline
  316.     For x = -1 To 1 Step 2
  317.         For y = -1 To 1 Step 2
  318.             For z = -1 To 1 Step 2
  319.                 For w = -1 To 1 Step 2
  320.                     If x = -1 Then _
  321.                         pline.AddSegment _
  322.                             x, y, z, w, _
  323.                             1, y, z, w
  324.                     If y = -1 Then _
  325.                         pline.AddSegment _
  326.                             x, y, z, w, _
  327.                             x, 1, z, w
  328.                     If z = -1 Then _
  329.                         pline.AddSegment _
  330.                             x, y, z, w, _
  331.                             x, y, 1, w
  332.                     If w = -1 Then _
  333.                         pline.AddSegment _
  334.                             x, y, z, w, _
  335.                             x, y, z, 1
  336.                 Next w
  337.             Next z
  338.         Next y
  339.     Next x
  340.     MousePointer = vbDefault
  341. End Sub
  342. Private Sub Form_Unload(Cancel As Integer)
  343.     End
  344. End Sub
  345. Private Sub mnuFileExit_Click()
  346.     Unload Me
  347. End Sub
  348.