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

  1. VERSION 4.00
  2. Begin VB.Form PerspectiveForm 
  3.    Appearance      =   0  'Flat
  4.    BackColor       =   &H00C0C0C0&
  5.    Caption         =   "Perspective"
  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 DText 
  29.       Height          =   285
  30.       Left            =   6000
  31.       TabIndex        =   22
  32.       Text            =   "3"
  33.       Top             =   2640
  34.       Width           =   735
  35.    End
  36.    Begin VB.Frame Frame1 
  37.       Caption         =   "Post-Rotations"
  38.       Height          =   1335
  39.       Index           =   1
  40.       Left            =   5400
  41.       TabIndex        =   14
  42.       Top             =   3120
  43.       Width           =   1455
  44.       Begin VB.TextBox ZW2Text 
  45.          Height          =   285
  46.          Left            =   600
  47.          MaxLength       =   6
  48.          TabIndex        =   17
  49.          Text            =   "0.0"
  50.          Top             =   960
  51.          Width           =   735
  52.       End
  53.       Begin VB.TextBox YW2Text 
  54.          Height          =   285
  55.          Left            =   600
  56.          MaxLength       =   6
  57.          TabIndex        =   16
  58.          Text            =   "0.1"
  59.          Top             =   600
  60.          Width           =   735
  61.       End
  62.       Begin VB.TextBox XW2Text 
  63.          Height          =   285
  64.          Left            =   600
  65.          MaxLength       =   6
  66.          TabIndex        =   15
  67.          Text            =   "0.2"
  68.          Top             =   240
  69.          Width           =   735
  70.       End
  71.       Begin VB.Label Label1 
  72.          Caption         =   "Z"
  73.          Height          =   255
  74.          Index           =   11
  75.          Left            =   240
  76.          TabIndex        =   20
  77.          Top             =   960
  78.          Width           =   255
  79.       End
  80.       Begin VB.Label Label1 
  81.          Caption         =   "Y"
  82.          Height          =   255
  83.          Index           =   10
  84.          Left            =   240
  85.          TabIndex        =   19
  86.          Top             =   600
  87.          Width           =   255
  88.       End
  89.       Begin VB.Label Label1 
  90.          Caption         =   "X"
  91.          Height          =   255
  92.          Index           =   9
  93.          Left            =   240
  94.          TabIndex        =   18
  95.          Top             =   240
  96.          Width           =   255
  97.       End
  98.    End
  99.    Begin VB.Frame Frame1 
  100.       Caption         =   "Pre-Rotations"
  101.       Height          =   2415
  102.       Index           =   0
  103.       Left            =   5400
  104.       TabIndex        =   1
  105.       Top             =   0
  106.       Width           =   1455
  107.       Begin VB.TextBox XYText 
  108.          Height          =   285
  109.          Left            =   600
  110.          MaxLength       =   6
  111.          TabIndex        =   10
  112.          Text            =   "0.0"
  113.          Top             =   1320
  114.          Width           =   735
  115.       End
  116.       Begin VB.TextBox XZText 
  117.          Height          =   285
  118.          Left            =   600
  119.          MaxLength       =   6
  120.          TabIndex        =   9
  121.          Text            =   "0.0"
  122.          Top             =   1680
  123.          Width           =   735
  124.       End
  125.       Begin VB.TextBox YZText 
  126.          Height          =   285
  127.          Left            =   600
  128.          MaxLength       =   6
  129.          TabIndex        =   8
  130.          Text            =   "0.0"
  131.          Top             =   2040
  132.          Width           =   735
  133.       End
  134.       Begin VB.TextBox XWText 
  135.          Height          =   285
  136.          Left            =   600
  137.          MaxLength       =   6
  138.          TabIndex        =   4
  139.          Text            =   "0.0"
  140.          Top             =   240
  141.          Width           =   735
  142.       End
  143.       Begin VB.TextBox YWText 
  144.          Height          =   285
  145.          Left            =   600
  146.          MaxLength       =   6
  147.          TabIndex        =   3
  148.          Text            =   "0.0"
  149.          Top             =   600
  150.          Width           =   735
  151.       End
  152.       Begin VB.TextBox ZWText 
  153.          Height          =   285
  154.          Left            =   600
  155.          MaxLength       =   6
  156.          TabIndex        =   2
  157.          Text            =   "0.0"
  158.          Top             =   960
  159.          Width           =   735
  160.       End
  161.       Begin VB.Label Label1 
  162.          Caption         =   "XY"
  163.          Height          =   255
  164.          Index           =   5
  165.          Left            =   240
  166.          TabIndex        =   13
  167.          Top             =   1320
  168.          Width           =   375
  169.       End
  170.       Begin VB.Label Label1 
  171.          Caption         =   "XZ"
  172.          Height          =   255
  173.          Index           =   4
  174.          Left            =   240
  175.          TabIndex        =   12
  176.          Top             =   1680
  177.          Width           =   375
  178.       End
  179.       Begin VB.Label Label1 
  180.          Caption         =   "YZ"
  181.          Height          =   255
  182.          Index           =   3
  183.          Left            =   240
  184.          TabIndex        =   11
  185.          Top             =   2040
  186.          Width           =   375
  187.       End
  188.       Begin VB.Label Label1 
  189.          Caption         =   "XW"
  190.          Height          =   255
  191.          Index           =   0
  192.          Left            =   240
  193.          TabIndex        =   7
  194.          Top             =   240
  195.          Width           =   375
  196.       End
  197.       Begin VB.Label Label1 
  198.          Caption         =   "YW"
  199.          Height          =   255
  200.          Index           =   1
  201.          Left            =   240
  202.          TabIndex        =   6
  203.          Top             =   600
  204.          Width           =   375
  205.       End
  206.       Begin VB.Label Label1 
  207.          Caption         =   "ZW"
  208.          Height          =   255
  209.          Index           =   2
  210.          Left            =   240
  211.          TabIndex        =   5
  212.          Top             =   960
  213.          Width           =   375
  214.       End
  215.    End
  216.    Begin VB.PictureBox Pict 
  217.       AutoRedraw      =   -1  'True
  218.       Height          =   5295
  219.       Left            =   0
  220.       ScaleHeight     =   349
  221.       ScaleMode       =   3  'Pixel
  222.       ScaleWidth      =   349
  223.       TabIndex        =   0
  224.       Top             =   0
  225.       Width           =   5295
  226.    End
  227.    Begin VB.Label Label1 
  228.       Caption         =   "D"
  229.       Height          =   255
  230.       Index           =   12
  231.       Left            =   5640
  232.       TabIndex        =   21
  233.       Top             =   2640
  234.       Width           =   255
  235.    End
  236.    Begin VB.Menu mnuFile 
  237.       Caption         =   "&File"
  238.       Begin VB.Menu mnuFileExit 
  239.          Caption         =   "E&xit"
  240.       End
  241.    End
  242. Attribute VB_Name = "PerspectiveForm"
  243. Attribute VB_Creatable = False
  244. Attribute VB_Exposed = False
  245. Option Explicit
  246. ' Location of focus point.
  247. Const FocusX = 0#
  248. Const FocusY = 0#
  249. Const FocusZ = 0#
  250. Dim ThePicture As ObjPicture
  251. ' *******************************************************
  252. ' Draw the surface.
  253. ' *******************************************************
  254. Private Sub DrawData(pic As Object)
  255. Dim xw_rot As Single
  256. Dim yw_rot As Single
  257. Dim zw_rot As Single
  258. Dim xy_rot As Single
  259. Dim xz_rot As Single
  260. Dim yz_rot As Single
  261. Dim xw2_rot As Single
  262. Dim yw2_rot As Single
  263. Dim zw2_rot As Single
  264. Dim XW(1 To 5, 1 To 5) As Single
  265. Dim YW(1 To 5, 1 To 5) As Single
  266. Dim ZW(1 To 5, 1 To 5) As Single
  267. Dim XY(1 To 5, 1 To 5) As Single
  268. Dim XZ(1 To 5, 1 To 5) As Single
  269. Dim YZ(1 To 5, 1 To 5) As Single
  270. Dim XW2(1 To 5, 1 To 5) As Single
  271. Dim YW2(1 To 5, 1 To 5) As Single
  272. Dim ZW2(1 To 5, 1 To 5) As Single
  273. Dim S(1 To 5, 1 To 5) As Single
  274. Dim T(1 To 5, 1 To 5) As Single
  275. Dim P(1 To 5, 1 To 5) As Single
  276. Dim M12(1 To 5, 1 To 5) As Single
  277. Dim M34(1 To 5, 1 To 5) As Single
  278. Dim M1_4(1 To 5, 1 To 5) As Single
  279. Dim M56(1 To 5, 1 To 5) As Single
  280. Dim M78(1 To 5, 1 To 5) As Single
  281. Dim M5_8(1 To 5, 1 To 5) As Single
  282. Dim M1_8(1 To 5, 1 To 5) As Single
  283. Dim M910(1 To 5, 1 To 5) As Single
  284. Dim M1112(1 To 5, 1 To 5) As Single
  285. Dim M9_12(1 To 5, 1 To 5) As Single
  286. Dim M_All(1 To 5, 1 To 5) As Single
  287. Dim D As Single
  288.     If Not IsNumeric(XWText.Text) Then Exit Sub
  289.     If Not IsNumeric(YWText.Text) Then Exit Sub
  290.     If Not IsNumeric(ZWText.Text) Then Exit Sub
  291.     If Not IsNumeric(XYText.Text) Then Exit Sub
  292.     If Not IsNumeric(XZText.Text) Then Exit Sub
  293.     If Not IsNumeric(YZText.Text) Then Exit Sub
  294.     If Not IsNumeric(XW2Text.Text) Then Exit Sub
  295.     If Not IsNumeric(YW2Text.Text) Then Exit Sub
  296.     If Not IsNumeric(ZW2Text.Text) Then Exit Sub
  297.     If Not IsNumeric(DText.Text) Then Exit Sub
  298.     xw_rot = CSng(XWText.Text)
  299.     yw_rot = CSng(YWText.Text)
  300.     zw_rot = CSng(ZWText.Text)
  301.     xy_rot = CSng(XYText.Text)
  302.     xz_rot = CSng(XZText.Text)
  303.     yz_rot = CSng(YZText.Text)
  304.     xw2_rot = CSng(XW2Text.Text)
  305.     yw2_rot = CSng(YW2Text.Text)
  306.     zw2_rot = CSng(ZW2Text.Text)
  307.     D = CSng(DText.Text)
  308.     MousePointer = vbHourglass
  309.     Refresh
  310.     ' Prevent overflow errors when drawing lines
  311.     ' too far out of bounds.
  312.     On Error Resume Next
  313.     ' Calculate the rotation matrices.
  314.     m4XWRotate XW, xw_rot
  315.     m4YWRotate YW, yw_rot
  316.     m4ZWRotate ZW, zw_rot
  317.     m4XYRotate XY, xy_rot
  318.     m4XZRotate XZ, xz_rot
  319.     m4YZRotate YZ, yz_rot
  320.     m4XWRotate XW2, xw2_rot
  321.     m4YWRotate YW2, yw2_rot
  322.     m4ZWRotate ZW2, zw2_rot
  323.     ' Calculate the projection matrix.
  324.     m4PerspectiveW P, D
  325.     ' Scale and translate so it looks OK in pixels.
  326.     m4Scale S, 75, -75, 1, 1
  327.     m4Translate T, Pict.ScaleWidth / 2, Pict.ScaleHeight / 2, 0, 0
  328.     m4MatMultiply M12, XW, YW
  329.     m4MatMultiply M34, ZW, XY
  330.     m4MatMultiply M56, XZ, YZ
  331.     m4MatMultiplyFull M78, P, XW2
  332.     m4MatMultiply M1_4, M12, M34
  333.     m4MatMultiplyFull M5_8, M56, M78
  334.     m4MatMultiplyFull M1_8, M1_4, M5_8
  335.     m4MatMultiply M910, YW2, ZW2
  336.     m4MatMultiply M1112, S, T
  337.     m4MatMultiply M9_12, M910, M1112
  338.     m4MatMultiplyFull M_All, M1_8, M9_12
  339.     ' Transform the points.
  340.     ThePicture.ApplyFull M_All
  341.     ' Display the data.
  342.     pic.Cls
  343.     ThePicture.Draw pic
  344.     pic.Refresh
  345.     MousePointer = vbDefault
  346. End Sub
  347. Private Sub Form_Load()
  348.     ' Create the data.
  349.     CreateData
  350.     ' Project and draw the data.
  351.     Me.Show
  352.     DrawData Pict
  353. End Sub
  354. ' ************************************************
  355. ' Create the surface.
  356. ' ************************************************
  357. Sub CreateData()
  358. Dim pline As ObjPolyline4D
  359. Dim x As Integer
  360. Dim y As Integer
  361. Dim z As Integer
  362. Dim w As Integer
  363.     MousePointer = vbHourglass
  364.     Refresh
  365.     Set ThePicture = New ObjPicture
  366.     Set pline = New ObjPolyline4D
  367.     ThePicture.objects.Add pline
  368.     For x = -1 To 1 Step 2
  369.         For y = -1 To 1 Step 2
  370.             For z = -1 To 1 Step 2
  371.                 For w = -1 To 1 Step 2
  372.                     If x = -1 Then _
  373.                         pline.AddSegment _
  374.                             x, y, z, w, _
  375.                             1, y, z, w
  376.                     If y = -1 Then _
  377.                         pline.AddSegment _
  378.                             x, y, z, w, _
  379.                             x, 1, z, w
  380.                     If z = -1 Then _
  381.                         pline.AddSegment _
  382.                             x, y, z, w, _
  383.                             x, y, 1, w
  384.                     If w = -1 Then _
  385.                         pline.AddSegment _
  386.                             x, y, z, w, _
  387.                             x, y, z, 1
  388.                 Next w
  389.             Next z
  390.         Next y
  391.     Next x
  392.     MousePointer = vbDefault
  393. End Sub
  394. Private Sub mnuFileExit_Click()
  395.     Unload Me
  396. End Sub
  397. Private Sub DText_Change()
  398.     DrawData Pict
  399. End Sub
  400. Private Sub XW2Text_Change()
  401.     DrawData Pict
  402. End Sub
  403. Private Sub XYText_Change()
  404.     DrawData Pict
  405. End Sub
  406. Private Sub XZText_Change()
  407.     DrawData Pict
  408. End Sub
  409. Private Sub YW2Text_Change()
  410.     DrawData Pict
  411. End Sub
  412. Private Sub YWText_Change()
  413.     DrawData Pict
  414. End Sub
  415. Private Sub YZText_Change()
  416.     DrawData Pict
  417. End Sub
  418. Private Sub ZW2Text_Change()
  419.     DrawData Pict
  420. End Sub
  421. Private Sub ZWText_Change()
  422.     DrawData Pict
  423. End Sub
  424. Private Sub XWText_Change()
  425.     DrawData Pict
  426. End Sub
  427.