home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH12 / SRC / LIGHT3.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-01-08  |  19.8 KB  |  639 lines

  1. VERSION 4.00
  2. Begin VB.Form LightForm 
  3.    Appearance      =   0  'Flat
  4.    Caption         =   "Light3"
  5.    ClientHeight    =   6075
  6.    ClientLeft      =   1335
  7.    ClientTop       =   630
  8.    ClientWidth     =   6030
  9.    BeginProperty Font 
  10.       name            =   "MS Sans Serif"
  11.       charset         =   0
  12.       weight          =   700
  13.       size            =   8.25
  14.       underline       =   0   'False
  15.       italic          =   0   'False
  16.       strikethrough   =   0   'False
  17.    EndProperty
  18.    ForeColor       =   &H80000008&
  19.    Height          =   6765
  20.    KeyPreview      =   -1  'True
  21.    Left            =   1275
  22.    LinkTopic       =   "Form1"
  23.    ScaleHeight     =   6075
  24.    ScaleWidth      =   6030
  25.    Top             =   0
  26.    Width           =   6150
  27.    Begin VB.TextBox KdistText 
  28.       BeginProperty Font 
  29.          name            =   "MS Sans Serif"
  30.          charset         =   0
  31.          weight          =   700
  32.          size            =   8.25
  33.          underline       =   0   'False
  34.          italic          =   0   'False
  35.          strikethrough   =   0   'False
  36.       EndProperty
  37.       Height          =   285
  38.       Left            =   3600
  39.       TabIndex        =   13
  40.       Text            =   "-1100"
  41.       Top             =   5760
  42.       Width           =   855
  43.    End
  44.    Begin VB.TextBox KaText 
  45.       BeginProperty Font 
  46.          name            =   "MS Sans Serif"
  47.          charset         =   0
  48.          weight          =   700
  49.          size            =   8.25
  50.          underline       =   0   'False
  51.          italic          =   0   'False
  52.          strikethrough   =   0   'False
  53.       EndProperty
  54.       Height          =   285
  55.       Left            =   2040
  56.       TabIndex        =   10
  57.       Text            =   "0.30"
  58.       Top             =   5760
  59.       Width           =   855
  60.    End
  61.    Begin VB.TextBox KdText 
  62.       BeginProperty Font 
  63.          name            =   "MS Sans Serif"
  64.          charset         =   0
  65.          weight          =   700
  66.          size            =   8.25
  67.          underline       =   0   'False
  68.          italic          =   0   'False
  69.          strikethrough   =   0   'False
  70.       EndProperty
  71.       Height          =   285
  72.       Left            =   480
  73.       TabIndex        =   8
  74.       Text            =   "0.65"
  75.       Top             =   5760
  76.       Width           =   855
  77.    End
  78.    Begin VB.TextBox PhiText 
  79.       BeginProperty Font 
  80.          name            =   "MS Sans Serif"
  81.          charset         =   0
  82.          weight          =   700
  83.          size            =   8.25
  84.          underline       =   0   'False
  85.          italic          =   0   'False
  86.          strikethrough   =   0   'False
  87.       EndProperty
  88.       Height          =   285
  89.       Left            =   3600
  90.       TabIndex        =   6
  91.       Text            =   "0.1571"
  92.       Top             =   5400
  93.       Width           =   855
  94.    End
  95.    Begin VB.TextBox ThetaText 
  96.       BeginProperty Font 
  97.          name            =   "MS Sans Serif"
  98.          charset         =   0
  99.          weight          =   700
  100.          size            =   8.25
  101.          underline       =   0   'False
  102.          italic          =   0   'False
  103.          strikethrough   =   0   'False
  104.       EndProperty
  105.       Height          =   285
  106.       Left            =   2040
  107.       TabIndex        =   4
  108.       Text            =   "1.8850"
  109.       Top             =   5400
  110.       Width           =   855
  111.    End
  112.    Begin VB.TextBox RText 
  113.       BeginProperty Font 
  114.          name            =   "MS Sans Serif"
  115.          charset         =   0
  116.          weight          =   700
  117.          size            =   8.25
  118.          underline       =   0   'False
  119.          italic          =   0   'False
  120.          strikethrough   =   0   'False
  121.       EndProperty
  122.       Height          =   285
  123.       Left            =   480
  124.       TabIndex        =   2
  125.       Text            =   "20.0000"
  126.       Top             =   5400
  127.       Width           =   855
  128.    End
  129.    Begin VB.PictureBox Pict 
  130.       AutoRedraw      =   -1  'True
  131.       BackColor       =   &H00FFFF80&
  132.       BeginProperty Font 
  133.          name            =   "MS Sans Serif"
  134.          charset         =   0
  135.          weight          =   700
  136.          size            =   8.25
  137.          underline       =   0   'False
  138.          italic          =   0   'False
  139.          strikethrough   =   0   'False
  140.       EndProperty
  141.       Height          =   5295
  142.       Left            =   0
  143.       Picture         =   "Light3.frx":0000
  144.       ScaleHeight     =   -14
  145.       ScaleLeft       =   -7
  146.       ScaleMode       =   0  'User
  147.       ScaleTop        =   7
  148.       ScaleWidth      =   15.926
  149.       TabIndex        =   0
  150.       Top             =   0
  151.       Width           =   6015
  152.    End
  153.    Begin VB.Label Label1 
  154.       Caption         =   "dist"
  155.       BeginProperty Font 
  156.          name            =   "MS Sans Serif"
  157.          charset         =   0
  158.          weight          =   700
  159.          size            =   8.25
  160.          underline       =   0   'False
  161.          italic          =   0   'False
  162.          strikethrough   =   0   'False
  163.       EndProperty
  164.       Height          =   255
  165.       Index           =   8
  166.       Left            =   3240
  167.       TabIndex        =   15
  168.       Top             =   5880
  169.       Width           =   375
  170.    End
  171.    Begin VB.Label Label1 
  172.       Caption         =   "k"
  173.       BeginProperty Font 
  174.          name            =   "MS Sans Serif"
  175.          charset         =   0
  176.          weight          =   700
  177.          size            =   8.25
  178.          underline       =   0   'False
  179.          italic          =   0   'False
  180.          strikethrough   =   0   'False
  181.       EndProperty
  182.       Height          =   255
  183.       Index           =   6
  184.       Left            =   3120
  185.       TabIndex        =   14
  186.       Top             =   5760
  187.       Width           =   135
  188.    End
  189.    Begin VB.Label Label1 
  190.       Caption         =   "k"
  191.       BeginProperty Font 
  192.          name            =   "MS Sans Serif"
  193.          charset         =   0
  194.          weight          =   700
  195.          size            =   8.25
  196.          underline       =   0   'False
  197.          italic          =   0   'False
  198.          strikethrough   =   0   'False
  199.       EndProperty
  200.       Height          =   255
  201.       Index           =   5
  202.       Left            =   1680
  203.       TabIndex        =   12
  204.       Top             =   5760
  205.       Width           =   135
  206.    End
  207.    Begin VB.Label Label1 
  208.       Caption         =   "a"
  209.       BeginProperty Font 
  210.          name            =   "MS Sans Serif"
  211.          charset         =   0
  212.          weight          =   700
  213.          size            =   8.25
  214.          underline       =   0   'False
  215.          italic          =   0   'False
  216.          strikethrough   =   0   'False
  217.       EndProperty
  218.       Height          =   255
  219.       Index           =   4
  220.       Left            =   1800
  221.       TabIndex        =   11
  222.       Top             =   5880
  223.       Width           =   135
  224.    End
  225.    Begin VB.Label Label1 
  226.       Caption         =   "d"
  227.       BeginProperty Font 
  228.          name            =   "MS Sans Serif"
  229.          charset         =   0
  230.          weight          =   700
  231.          size            =   8.25
  232.          underline       =   0   'False
  233.          italic          =   0   'False
  234.          strikethrough   =   0   'False
  235.       EndProperty
  236.       Height          =   255
  237.       Index           =   3
  238.       Left            =   240
  239.       TabIndex        =   9
  240.       Top             =   5880
  241.       Width           =   135
  242.    End
  243.    Begin MSComDlg.CommonDialog LoadDialog 
  244.       Left            =   4560
  245.       Top             =   5160
  246.       _Version        =   65536
  247.       _ExtentX        =   847
  248.       _ExtentY        =   847
  249.       _StockProps     =   0
  250.       CancelError     =   -1  'True
  251.    End
  252.    Begin VB.Label Label1 
  253.       Caption         =   "k"
  254.       BeginProperty Font 
  255.          name            =   "MS Sans Serif"
  256.          charset         =   0
  257.          weight          =   700
  258.          size            =   8.25
  259.          underline       =   0   'False
  260.          italic          =   0   'False
  261.          strikethrough   =   0   'False
  262.       EndProperty
  263.       Height          =   255
  264.       Index           =   7
  265.       Left            =   120
  266.       TabIndex        =   7
  267.       Top             =   5760
  268.       Width           =   135
  269.    End
  270.    Begin VB.Label Label1 
  271.       Caption         =   "Phi"
  272.       BeginProperty Font 
  273.          name            =   "MS Sans Serif"
  274.          charset         =   0
  275.          weight          =   700
  276.          size            =   8.25
  277.          underline       =   0   'False
  278.          italic          =   0   'False
  279.          strikethrough   =   0   'False
  280.       EndProperty
  281.       Height          =   255
  282.       Index           =   2
  283.       Left            =   3240
  284.       TabIndex        =   5
  285.       Top             =   5400
  286.       Width           =   375
  287.    End
  288.    Begin VB.Label Label1 
  289.       Caption         =   "Theta"
  290.       BeginProperty Font 
  291.          name            =   "MS Sans Serif"
  292.          charset         =   0
  293.          weight          =   700
  294.          size            =   8.25
  295.          underline       =   0   'False
  296.          italic          =   0   'False
  297.          strikethrough   =   0   'False
  298.       EndProperty
  299.       Height          =   255
  300.       Index           =   1
  301.       Left            =   1440
  302.       TabIndex        =   3
  303.       Top             =   5400
  304.       Width           =   495
  305.    End
  306.    Begin VB.Label Label1 
  307.       Caption         =   "R"
  308.       BeginProperty Font 
  309.          name            =   "MS Sans Serif"
  310.          charset         =   0
  311.          weight          =   700
  312.          size            =   8.25
  313.          underline       =   0   'False
  314.          italic          =   0   'False
  315.          strikethrough   =   0   'False
  316.       EndProperty
  317.       Height          =   255
  318.       Index           =   0
  319.       Left            =   240
  320.       TabIndex        =   1
  321.       Top             =   5400
  322.       Width           =   255
  323.    End
  324.    Begin VB.Menu mnuFile 
  325.       Caption         =   "&File"
  326.       Begin VB.Menu mnuFileLoad 
  327.          Caption         =   "&Load..."
  328.          Shortcut        =   ^L
  329.       End
  330.       Begin VB.Menu mnuFileSep 
  331.          Caption         =   "-"
  332.       End
  333.       Begin VB.Menu mnuFileExit 
  334.          Caption         =   "E&xit"
  335.       End
  336.    End
  337. Attribute VB_Name = "LightForm"
  338. Attribute VB_Creatable = False
  339. Attribute VB_Exposed = False
  340. Option Explicit
  341. Dim SysPalSize As Integer
  342. Dim NumStaticColors As Integer
  343. Dim StaticColor1 As Integer
  344. Dim StaticColor2 As Integer
  345. Dim syspal(0 To 255) As PALETTEENTRY
  346. ' Location of viewing eye.
  347. Dim EyeR As Single
  348. Dim EyeTheta As Single
  349. Dim EyePhi As Single
  350. Const Dtheta = PI / 20
  351. Const Dphi = PI / 20
  352. Const dr = 1
  353. ' Location of focus point.
  354. Const FocusX = 0#
  355. Const FocusY = 0#
  356. Const FocusZ = 0#
  357. Dim Projector(1 To 4, 1 To 4) As Single
  358. Dim ThePicture As ObjPicture
  359. Dim ShowingParameters As Boolean
  360. ' *******************************************************
  361. ' Rotate the points in the cube and draw the cube.
  362. ' *******************************************************
  363. Private Sub DrawData(pic As Object)
  364. Dim X As Single
  365. Dim Y As Single
  366. Dim Z As Single
  367. Dim old_draw As Integer
  368. Dim old_fill As Integer
  369. Dim t1(1 To 4, 1 To 4) As Single
  370. Dim t2(1 To 4, 1 To 4) As Single
  371. Dim T12(1 To 4, 1 To 4) As Single
  372. Dim T123(1 To 4, 1 To 4) As Single
  373. Dim pt As Point3D
  374.     MousePointer = vbHourglass
  375.     ' Get constants for the surfaces.
  376.     LightKd = CSng(KdText.Text)
  377.     LightKa = CSng(KaText.Text)
  378.     LightKdist = CSng(KdistText.Text)
  379.     ' Adjust LightIi.
  380.     LightIi = 255 * _
  381.         (ThePicture.Distance(LightX, LightY, LightZ) _
  382.         + LightKdist + 4)
  383.     ' Prevent overflow errors when drawing lines
  384.     ' too far out of bounds.
  385.     On Error Resume Next
  386.     ' Cull backfaces.
  387.     ThePicture.Culled = False
  388.     m3SphericalToCartesian EyeR, EyeTheta, EyePhi, X, Y, Z
  389.     ThePicture.Cull X, Y, Z
  390.     ' Clip faces behind the center of projection.
  391.     ThePicture.ClipEye EyeR
  392.     ' Transform coordinates into pixels.
  393.     m3Scale t1, _
  394.         Pict.ScaleX(1, Pict.ScaleMode, vbPixels), _
  395.         Pict.ScaleY(1, Pict.ScaleMode, vbPixels), _
  396.         1
  397.     m3Translate t2, _
  398.         -Pict.ScaleX(Pict.ScaleLeft, Pict.ScaleMode, vbPixels), _
  399.         -Pict.ScaleY(Pict.ScaleTop, Pict.ScaleMode, vbPixels), _
  400.         0
  401.     m3MatMultiply T12, t1, t2
  402.     m3MatMultiplyFull T123, Projector, T12
  403.     ' Transform the points.
  404.     ThePicture.ApplyFull T123
  405.     ' Clear the screen. We must do this before
  406.     ' selecting the pen and brush since Cls resets
  407.     ' the pen and brush to default values.
  408.     pic.Cls
  409.     ' Prepare to fill polygons.
  410.     old_draw = pic.DrawStyle
  411.     old_fill = pic.FillStyle
  412.     pic.DrawStyle = vbInvisible
  413.     pic.FillStyle = vbFSSolid
  414.     ' Display the data.
  415.     ThePicture.DrawShaded pic, EyeR
  416.     pic.Refresh
  417.     ' Restore the old draw and fill styles.
  418.     pic.DrawStyle = old_draw
  419.     pic.FillStyle = old_fill
  420.     ' Display the viewing parameters.
  421.     ShowViewingParameters
  422.     MousePointer = vbDefault
  423. End Sub
  424. Sub ShowViewingParameters()
  425.     ShowingParameters = True
  426.     RText.Text = Format$(EyeR, "0.0000")
  427.     ThetaText.Text = Format$(EyeTheta, "0.0000")
  428.     PhiText.Text = Format$(EyePhi, "0.0000")
  429.     RText.Refresh
  430.     ThetaText.Refresh
  431.     PhiText.Refresh
  432.     ShowingParameters = False
  433. End Sub
  434. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  435.     Select Case KeyCode
  436.         Case vbKeyLeft
  437.             EyeTheta = EyeTheta - Dtheta
  438.         
  439.         Case vbKeyRight
  440.             EyeTheta = EyeTheta + Dtheta
  441.         
  442.         Case vbKeyUp
  443.             EyePhi = EyePhi - Dphi
  444.         
  445.         Case vbKeyDown
  446.             EyePhi = EyePhi + Dphi
  447.                 
  448.         Case Else
  449.             Exit Sub
  450.     End Select
  451.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  452.     DrawData Pict
  453. End Sub
  454. Private Sub Form_KeyPress(KeyAscii As Integer)
  455.     Select Case KeyAscii
  456.         Case Asc("+")
  457.             EyeR = EyeR + dr
  458.         
  459.         Case Asc("-")
  460.             EyeR = EyeR - dr
  461.         
  462.         Case Else
  463.             Exit Sub
  464.     End Select
  465.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  466.     DrawData Pict
  467. End Sub
  468. Private Sub Form_Load()
  469.     ' Make sure the screen supports palettes.
  470.     If Not GetDeviceCaps(hdc, RASTERCAPS) And RC_PALETTE Then
  471.         Beep
  472.         MsgBox "This monitor does not support palettes.", _
  473.             vbCritical
  474.         End
  475.     End If
  476.     ' Get system palette size and # static colors.
  477.     SysPalSize = GetDeviceCaps(hdc, SIZEPALETTE)
  478.     NumStaticColors = GetDeviceCaps(hdc, NUMRESERVED)
  479.     StaticColor1 = NumStaticColors \ 2 - 1
  480.     StaticColor2 = SysPalSize - NumStaticColors \ 2
  481.     ' Fill the picture's palette with grays.
  482.     MatchGrayPalette Pict
  483.     Pict.Cls
  484.     ' Initialize the eye position.
  485.     EyeR = 20
  486.     EyeTheta = PI * 0.2
  487.     EyePhi = PI * 0.05
  488.     ' Initialize the projection transformation.
  489.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  490. End Sub
  491. ' ***********************************************
  492. ' Load the control's palette so the non-static
  493. ' colors are grays. Map the logical palette to
  494. ' match the system palette. Convert the image to
  495. ' use the non-static grays.
  496. ' Leave new system palette entries in SysPal().
  497. ' ***********************************************
  498. Sub MatchGrayPalette(pic As Control)
  499. Dim origpal(0 To 255) As PALETTEENTRY
  500. Dim wid As Long
  501. Dim hgt As Long
  502. Dim bytes() As Byte
  503. Dim i As Integer
  504. Dim bm As BITMAP
  505. Dim hbm As Integer
  506. Dim status As Long
  507. Dim X As Integer
  508. Dim Y As Integer
  509. Dim gray As Single
  510. Dim dgray As Single
  511. Dim c As Integer
  512. Dim clr As Integer
  513. Dim logpal As Long
  514.     ' Make sure pic has the foreground palette.
  515.     pic.ZOrder
  516.     status = RealizePalette(pic.hdc)
  517.     DoEvents
  518.     ' Get the system palette entries.
  519.     status = GetSystemPaletteEntries(pic.hdc, 0, SysPalSize, origpal(0))
  520.         
  521.     ' Get the image pixels.
  522.     hbm = pic.Image
  523.     status = GetObject(hbm, BITMAP_SIZE, bm)
  524.     wid = bm.bmWidthBytes
  525.     hgt = bm.bmHeight
  526.     ReDim bytes(1 To wid, 1 To hgt)
  527.     status = GetBitmapBits(hbm, wid * hgt, bytes(1, 1))
  528.     ' Make the logical palette as big as possible.
  529.     logpal = pic.Picture.hPal
  530.     If ResizePalette(logpal, SysPalSize) = 0 Then
  531.         Beep
  532.         MsgBox "Error resizing logical palette.", _
  533.             vbExclamation
  534.         Exit Sub
  535.     End If
  536.     ' Blank the non-static colors.
  537.     For i = 0 To StaticColor1
  538.         syspal(i) = origpal(i)
  539.     Next i
  540.     For i = StaticColor1 + 1 To StaticColor2 - 1
  541.         With syspal(i)
  542.             .peRed = 0
  543.             .peGreen = 0
  544.             .peBlue = 0
  545.             .peFlags = PC_NOCOLLAPSE
  546.         End With
  547.     Next i
  548.     For i = StaticColor2 To 255
  549.         syspal(i) = origpal(i)
  550.     Next i
  551.     status = SetPaletteEntries(logpal, 0, SysPalSize, syspal(0))
  552.     ' Insert the non-static grays.
  553.     gray = 0
  554.     dgray = 255 / (StaticColor2 - StaticColor1 - 2)
  555.     For i = StaticColor1 + 1 To StaticColor2 - 1
  556.         c = gray
  557.         gray = gray + dgray
  558.         With syspal(i)
  559.             .peRed = c
  560.             .peGreen = c
  561.             .peBlue = c
  562.         End With
  563.     Next i
  564.     status = SetPaletteEntries(logpal, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, syspal(StaticColor1 + 1))
  565.     ' Realize the gray palette.
  566.     status = RealizePalette(pic.hdc)
  567.     pic.Refresh
  568. End Sub
  569. Private Sub mnuFileExit_Click()
  570.     Unload Me
  571. End Sub
  572. Private Sub mnuFileLoad_Click()
  573. Dim fname As String
  574. Dim filenum As Integer
  575. Dim txt As String
  576. Dim xmin As Single
  577. Dim ymin As Single
  578. Dim xmax As Single
  579. Dim ymax As Single
  580.     ' Allow the user to pick a file.
  581.     On Error Resume Next
  582.     LoadDialog.filename = "*.APF"
  583.     LoadDialog.ShowOpen
  584.     LoadDialog.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  585.     If Err.Number = cdlCancel Then
  586.         Unload LoadDialog
  587.         Exit Sub
  588.     ElseIf Err.Number <> 0 Then
  589.         Unload LoadDialog
  590.         Beep
  591.         MsgBox "Error selecting file.", , vbExclamation
  592.         Exit Sub
  593.     End If
  594.     On Error GoTo 0
  595.     fname = LoadDialog.filename
  596.     LoadDialog.InitDir = Left$(fname, Len(fname) _
  597.         - Len(LoadDialog.FileTitle) - 1)
  598.     ' Clear the picture.
  599.     Set ThePicture = Nothing
  600.     ' Open the file.
  601.     filenum = FreeFile
  602.     Open fname For Input As #filenum
  603.     ' Make sure it's an Object Picture File.
  604.     Input #filenum, txt
  605.     If txt <> "3D APF PICTURE" Then
  606.         Close filenum
  607.         Caption = "Light3"
  608.         Beep
  609.         MsgBox "Error reading file """ & fname & """.", , vbExclamation
  610.         Exit Sub
  611.     End If
  612.     ' Read the picture.
  613.     Set ThePicture = New ObjPicture
  614.     ThePicture.FileInput filenum
  615.     ' Close the file.
  616.     Close filenum
  617.     Caption = "Light3 [" & LoadDialog.FileTitle & "]"
  618.     ' Refresh the display.
  619.     DrawData Pict
  620. End Sub
  621. Private Sub PhiText_Change()
  622.     If ShowingParameters Then Exit Sub
  623.     EyePhi = CSng(PhiText.Text)
  624.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  625.     DrawData Pict
  626. End Sub
  627. Private Sub RText_Change()
  628.     If ShowingParameters Then Exit Sub
  629.     EyeR = CSng(RText.Text)
  630.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  631.     DrawData Pict
  632. End Sub
  633. Private Sub ThetaText_Change()
  634.     If ShowingParameters Then Exit Sub
  635.     EyeTheta = CSng(ThetaText.Text)
  636.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  637.     DrawData Pict
  638. End Sub
  639.