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

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