home *** CD-ROM | disk | FTP | other *** search
- VERSION 4.00
- Begin VB.Form RayForm
- Appearance = 0 'Flat
- Caption = "Ray2"
- ClientHeight = 4005
- ClientLeft = 1905
- ClientTop = 1320
- ClientWidth = 6030
- BeginProperty Font
- name = "MS Sans Serif"
- charset = 0
- weight = 700
- size = 8.25
- underline = 0 'False
- italic = 0 'False
- strikethrough = 0 'False
- EndProperty
- ForeColor = &H80000008&
- Height = 4695
- KeyPreview = -1 'True
- Left = 1845
- LinkTopic = "Form1"
- ScaleHeight = 267
- ScaleMode = 3 'Pixel
- ScaleWidth = 402
- Top = 690
- Width = 6150
- Begin VB.OptionButton Scene
- Caption = "Spheres + Cylinders"
- BeginProperty Font
- name = "MS Sans Serif"
- charset = 0
- weight = 700
- size = 8.25
- underline = 0 'False
- italic = 0 'False
- strikethrough = 0 'False
- EndProperty
- Height = 255
- Index = 3
- Left = 0
- TabIndex = 16
- Top = 1080
- Width = 2025
- End
- Begin VB.OptionButton Scene
- Caption = "Sphere + Cube"
- BeginProperty Font
- name = "MS Sans Serif"
- charset = 0
- weight = 700
- size = 8.25
- underline = 0 'False
- italic = 0 'False
- strikethrough = 0 'False
- EndProperty
- Height = 255
- Index = 2
- Left = 0
- TabIndex = 15
- Top = 720
- Width = 2025
- End
- Begin VB.OptionButton Scene
- Caption = "Sphere + Square"
- BeginProperty Font
- name = "MS Sans Serif"
- charset = 0
- weight = 700
- size = 8.25
- underline = 0 'False
- italic = 0 'False
- strikethrough = 0 'False
- EndProperty
- Height = 255
- Index = 1
- Left = 0
- TabIndex = 14
- Top = 360
- Width = 2025
- End
- Begin VB.OptionButton Scene
- Caption = "Spheres + Plane"
- BeginProperty Font
- name = "MS Sans Serif"
- charset = 0
- weight = 700
- size = 8.25
- underline = 0 'False
- italic = 0 'False
- strikethrough = 0 'False
- EndProperty
- Height = 255
- Index = 0
- Left = 0
- TabIndex = 13
- Top = 0
- Value = -1 'True
- Width = 2025
- End
- Begin VB.TextBox StepText
- BeginProperty Font
- name = "MS Sans Serif"
- charset = 0
- weight = 700
- size = 8.25
- underline = 0 'False
- italic = 0 'False
- strikethrough = 0 'False
- EndProperty
- Height = 285
- Left = 840
- TabIndex = 11
- Text = "4"
- Top = 3240
- Width = 855
- End
- Begin VB.CommandButton CmdGo
- Caption = "Go"
- Default = -1 'True
- BeginProperty Font
- name = "MS Sans Serif"
- charset = 0
- weight = 700
- size = 8.25
- underline = 0 'False
- italic = 0 'False
- strikethrough = 0 'False
- EndProperty
- Height = 375
- Left = 600
- TabIndex = 10
- Top = 3600
- Width = 1095
- End
- Begin VB.TextBox KdistText
- BeginProperty Font
- name = "MS Sans Serif"
- charset = 0
- weight = 700
- size = 8.25
- underline = 0 'False
- italic = 0 'False
- strikethrough = 0 'False
- EndProperty
- Height = 285
- Left = 840
- TabIndex = 7
- Text = "-850"
- Top = 2640
- Width = 855
- End
- Begin VB.TextBox PhiText
- BeginProperty Font
- name = "MS Sans Serif"
- charset = 0
- weight = 700
- size = 8.25
- underline = 0 'False
- italic = 0 'False
- strikethrough = 0 'False
- EndProperty
- Height = 285
- Left = 840
- TabIndex = 6
- Text = "-0.4713"
- Top = 2160
- Width = 855
- End
- Begin VB.TextBox ThetaText
- BeginProperty Font
- name = "MS Sans Serif"
- charset = 0
- weight = 700
- size = 8.25
- underline = 0 'False
- italic = 0 'False
- strikethrough = 0 'False
- EndProperty
- Height = 285
- Left = 840
- TabIndex = 4
- Text = "0.6275"
- Top = 1800
- Width = 855
- End
- Begin VB.TextBox RText
- BeginProperty Font
- name = "MS Sans Serif"
- charset = 0
- weight = 700
- size = 8.25
- underline = 0 'False
- italic = 0 'False
- strikethrough = 0 'False
- EndProperty
- Height = 285
- Left = 840
- TabIndex = 2
- Text = "1000"
- Top = 1440
- Width = 855
- End
- Begin VB.PictureBox Pict
- AutoRedraw = -1 'True
- BackColor = &H00FFFF80&
- BeginProperty Font
- name = "MS Sans Serif"
- charset = 0
- weight = 700
- size = 8.25
- underline = 0 'False
- italic = 0 'False
- strikethrough = 0 'False
- EndProperty
- Height = 3975
- Left = 2040
- Picture = "Ray2.frx":0000
- ScaleHeight = 261
- ScaleMode = 3 'Pixel
- ScaleWidth = 261
- TabIndex = 0
- Top = 0
- Width = 3975
- End
- Begin VB.Label Label1
- Caption = "Step"
- BeginProperty Font
- name = "MS Sans Serif"
- charset = 0
- weight = 700
- size = 8.25
- underline = 0 'False
- italic = 0 'False
- strikethrough = 0 'False
- EndProperty
- Height = 255
- Index = 13
- Left = 240
- TabIndex = 12
- Top = 3240
- Width = 615
- End
- Begin VB.Label Label1
- Caption = "dist"
- BeginProperty Font
- name = "MS Sans Serif"
- charset = 0
- weight = 700
- size = 8.25
- underline = 0 'False
- italic = 0 'False
- strikethrough = 0 'False
- EndProperty
- Height = 255
- Index = 8
- Left = 360
- TabIndex = 9
- Top = 2760
- Width = 375
- End
- Begin VB.Label Label1
- Caption = "k"
- BeginProperty Font
- name = "MS Sans Serif"
- charset = 0
- weight = 700
- size = 8.25
- underline = 0 'False
- italic = 0 'False
- strikethrough = 0 'False
- EndProperty
- Height = 255
- Index = 6
- Left = 240
- TabIndex = 8
- Top = 2640
- Width = 135
- End
- Begin MSComDlg.CommonDialog LoadDialog
- Left = 0
- Top = 3720
- _Version = 65536
- _ExtentX = 847
- _ExtentY = 847
- _StockProps = 0
- CancelError = -1 'True
- End
- Begin VB.Label Label1
- Caption = "Phi"
- BeginProperty Font
- name = "MS Sans Serif"
- charset = 0
- weight = 700
- size = 8.25
- underline = 0 'False
- italic = 0 'False
- strikethrough = 0 'False
- EndProperty
- Height = 255
- Index = 2
- Left = 240
- TabIndex = 5
- Top = 2160
- Width = 375
- End
- Begin VB.Label Label1
- Caption = "Theta"
- BeginProperty Font
- name = "MS Sans Serif"
- charset = 0
- weight = 700
- size = 8.25
- underline = 0 'False
- italic = 0 'False
- strikethrough = 0 'False
- EndProperty
- Height = 255
- Index = 1
- Left = 240
- TabIndex = 3
- Top = 1800
- Width = 495
- End
- Begin VB.Label Label1
- Caption = "R"
- BeginProperty Font
- name = "MS Sans Serif"
- charset = 0
- weight = 700
- size = 8.25
- underline = 0 'False
- italic = 0 'False
- strikethrough = 0 'False
- EndProperty
- Height = 255
- Index = 0
- Left = 240
- TabIndex = 1
- Top = 1440
- Width = 255
- End
- Begin VB.Menu mnuFile
- Caption = "&File"
- Begin VB.Menu mnuFileSaveBitmap
- Caption = "&Save Bitmap..."
- Shortcut = ^S
- End
- Begin VB.Menu mnuFileSep
- Caption = "-"
- End
- Begin VB.Menu mnuFileExit
- Caption = "E&xit"
- End
- End
- Attribute VB_Name = "RayForm"
- Attribute VB_Creatable = False
- Attribute VB_Exposed = False
- Option Explicit
- Dim SysPalSize As Integer
- Dim NumStaticColors As Integer
- Dim StaticColor1 As Integer
- Dim StaticColor2 As Integer
- Dim syspal(0 To 255) As PALETTEENTRY
- ' Location of viewing eye.
- Dim EyeR As Single
- Dim EyeTheta As Single
- Dim EyePhi As Single
- Const dtheta = PI / 20
- Const Dphi = PI / 20
- Const dR = 1
- ' Location of focus point.
- Const FocusX = 0#
- Const FocusY = 0#
- Const FocusZ = 0#
- Dim Projector(1 To 4, 1 To 4) As Single
- ' The collection of objects in the scene.
- Dim Objects As Collection
- Dim Running As Boolean
- Dim SceneChoice As Integer
- ' ************************************************
- ' Halt immediately in case we're in the middle of
- ' ray tracing.
- ' ************************************************
- Private Sub Form_Unload(Cancel As Integer)
- End
- End Sub
- ' ************************************************
- ' Create the objects in the scene.
- ' ************************************************
- Sub CreateData()
- Dim obj As Object
- Dim s As Single
- Set Objects = New Collection
- Select Case SceneChoice
- Case 0 ' Spheres + Plane.
- ' Sphere of radius 60 at (-40, 0, 0).
- Set obj = New ObjSphere
- Objects.Add obj
- obj.Initialize 60, -40, 0, 0
- obj.SetKd 0.45, 0.45, 0.45 ' Diffuse.
- obj.SetKa 0.4, 0.4, 0.4 ' Ambient.
- obj.SetSpec 20, 0.35 ' Specular.
-
- ' Sphere of radius 60 at (40, 0, 0).
- Set obj = New ObjSphere
- Objects.Add obj
- obj.Initialize 60, 40, 0, 0
- obj.SetKd 0.45, 0.45, 0.45 ' Diffuse.
- obj.SetKa 0.4, 0.4, 0.4 ' Ambient.
- obj.SetSpec 20, 0.35 ' Specular.
-
- ' X-Z plane.
- Set obj = New ObjPlane
- Objects.Add obj
- obj.Initialize 0, 0, 0, 0, -1, 0
- obj.SetKd 0.45, 0.45, 0.45 ' Diffuse.
- obj.SetKa 0.3, 0.3, 0.3 ' Ambient.
- obj.SetSpec 20, 0.35 ' Specular.
- Case 1 ' Sphere + Square.
- ' Sphere of radius 70 at (0, 0, 0).
- Set obj = New ObjSphere
- Objects.Add obj
- obj.Initialize 70, 0, 0, 0
- obj.SetKd 0.45, 0.45, 0.45 ' Diffuse.
- obj.SetKa 0.4, 0.4, 0.4 ' Ambient.
- obj.SetSpec 20, 0.35 ' Specular.
- ' Square in the X-Z plane with side
- ' length 160.
- Set obj = New ObjPolygon
- Objects.Add obj
- obj.AddPoint _
- 80, 0, 80, _
- -80, 0, 80, _
- -80, 0, -80, _
- 80, 0, -80
- obj.DefinePlane
- obj.SetKd 0.45, 0.45, 0.45 ' Diffuse.
- obj.SetKa 0.3, 0.3, 0.3 ' Ambient.
- obj.SetSpec 20, 0.35 ' Specular.
- Case 2 ' Sphere + Cube.
- s = 70
- ' Sphere of radius s * Sqr(2) at (0, 0, 0).
- Set obj = New ObjSphere
- Objects.Add obj
- obj.Initialize s * Sqr(2), 0, 0, 0
- obj.SetKd 0.45, 0.45, 0.45 ' Diffuse.
- obj.SetKa 0.4, 0.4, 0.4 ' Ambient.
- obj.SetSpec 20, 0.35 ' Specular.
- ' X+ side.
- Set obj = New ObjPolygon
- Objects.Add obj
- obj.AddPoint _
- s, s, s, _
- s, -s, s, _
- s, -s, -s, _
- s, s, -s
- obj.DefinePlane
- obj.SetKd 0.45, 0.45, 0.45 ' Diffuse.
- obj.SetKa 0.3, 0.3, 0.3 ' Ambient.
- obj.SetSpec 20, 0.35 ' Specular.
- ' X- side.
- Set obj = New ObjPolygon
- Objects.Add obj
- obj.AddPoint _
- -s, s, s, _
- -s, -s, s, _
- -s, -s, -s, _
- -s, s, -s
- obj.DefinePlane
- obj.SetKd 0.45, 0.45, 0.45 ' Diffuse.
- obj.SetKa 0.3, 0.3, 0.3 ' Ambient.
- obj.SetSpec 20, 0.35 ' Specular.
- ' Y+ side.
- Set obj = New ObjPolygon
- Objects.Add obj
- obj.AddPoint _
- s, s, s, _
- -s, s, s, _
- -s, s, -s, _
- s, s, -s
- obj.DefinePlane
- obj.SetKd 0.45, 0.45, 0.45 ' Diffuse.
- obj.SetKa 0.3, 0.3, 0.3 ' Ambient.
- obj.SetSpec 20, 0.35 ' Specular.
- ' Y- side.
- Set obj = New ObjPolygon
- Objects.Add obj
- obj.AddPoint _
- s, -s, s, _
- -s, -s, s, _
- -s, -s, -s, _
- s, -s, -s
- obj.DefinePlane
- obj.SetKd 0.45, 0.45, 0.45 ' Diffuse.
- obj.SetKa 0.3, 0.3, 0.3 ' Ambient.
- obj.SetSpec 20, 0.35 ' Specular.
- ' Z+ side.
- Set obj = New ObjPolygon
- Objects.Add obj
- obj.AddPoint _
- s, s, s, _
- -s, s, s, _
- -s, -s, s, _
- s, -s, s
- obj.DefinePlane
- obj.SetKd 0.45, 0.45, 0.45 ' Diffuse.
- obj.SetKa 0.3, 0.3, 0.3 ' Ambient.
- obj.SetSpec 20, 0.35 ' Specular.
- ' Z- side.
- Set obj = New ObjPolygon
- Objects.Add obj
- obj.AddPoint _
- s, s, -s, _
- -s, s, -s, _
- -s, -s, -s, _
- s, -s, -s
- obj.DefinePlane
- obj.SetKd 0.45, 0.45, 0.45 ' Diffuse.
- obj.SetKa 0.3, 0.3, 0.3 ' Ambient.
- obj.SetSpec 20, 0.35 ' Specular.
- Case 3 ' Spheres + Cylinders.
- s = 15
- ' Cylinder of radius s between
- ' (-100, 0, 0) and (100, 0, 0).
- Set obj = New ObjCylinder
- Objects.Add obj
- obj.Initialize s, -100, 0, 0, 100, 0, 0
- obj.SetKd 0.45, 0.45, 0.45 ' Diffuse.
- obj.SetKa 0.4, 0.4, 0.4 ' Ambient.
- obj.SetSpec 20, 0.35 ' Specular.
-
- ' Cylinder of radius s between
- ' (0, -100, 0) and (0, 100, 0).
- Set obj = New ObjCylinder
- Objects.Add obj
- obj.Initialize s, 0, -100, 0, 0, 100, 0
- obj.SetKd 0.45, 0.45, 0.45 ' Diffuse.
- obj.SetKa 0.4, 0.4, 0.4 ' Ambient.
- obj.SetSpec 20, 0.35 ' Specular.
-
- ' Cylinder of radius s between
- ' (0, 0, -100) and (0, 0, 100).
- Set obj = New ObjCylinder
- Objects.Add obj
- obj.Initialize s, 0, 0, -100, 0, 0, 100
- obj.SetKd 0.45, 0.45, 0.45 ' Diffuse.
- obj.SetKa 0.4, 0.4, 0.4 ' Ambient.
- obj.SetSpec 20, 0.35 ' Specular.
-
- s = 30
- ' Sphere of radius s at (-100, 0, 0).
- Set obj = New ObjSphere
- Objects.Add obj
- obj.Initialize s, -100, 0, 0
- obj.SetKd 0.45, 0.45, 0.45 ' Diffuse.
- obj.SetKa 0.4, 0.4, 0.4 ' Ambient.
- obj.SetSpec 20, 0.35 ' Specular.
- ' Sphere of radius s at (100, 0, 0).
- Set obj = New ObjSphere
- Objects.Add obj
- obj.Initialize s, 100, 0, 0
- obj.SetKd 0.45, 0.45, 0.45 ' Diffuse.
- obj.SetKa 0.4, 0.4, 0.4 ' Ambient.
- obj.SetSpec 20, 0.35 ' Specular.
- ' Sphere of radius s at (0, -100, 0).
- Set obj = New ObjSphere
- Objects.Add obj
- obj.Initialize s, 0, -100, 0
- obj.SetKd 0.45, 0.45, 0.45 ' Diffuse.
- obj.SetKa 0.4, 0.4, 0.4 ' Ambient.
- obj.SetSpec 20, 0.35 ' Specular.
- ' Sphere of radius s at (0, 100, 0).
- Set obj = New ObjSphere
- Objects.Add obj
- obj.Initialize s, 0, 100, 0
- obj.SetKd 0.45, 0.45, 0.45 ' Diffuse.
- obj.SetKa 0.4, 0.4, 0.4 ' Ambient.
- obj.SetSpec 20, 0.35 ' Specular.
- ' Sphere of radius s at (0, 0, -100).
- Set obj = New ObjSphere
- Objects.Add obj
- obj.Initialize s, 0, 0, -100
- obj.SetKd 0.45, 0.45, 0.45 ' Diffuse.
- obj.SetKa 0.4, 0.4, 0.4 ' Ambient.
- obj.SetSpec 20, 0.35 ' Specular.
-
- ' Sphere of radius s at (0, 0, 100).
- Set obj = New ObjSphere
- Objects.Add obj
- obj.Initialize s, 0, 0, 100
- obj.SetKd 0.45, 0.45, 0.45 ' Diffuse.
- obj.SetKa 0.4, 0.4, 0.4 ' Ambient.
- obj.SetSpec 20, 0.35 ' Specular.
- End Select
- End Sub
- ' *******************************************************
- ' Project and draw.
- ' *******************************************************
- Private Sub DrawData(pic As Object)
- Dim Projector(1 To 4, 1 To 4) As Single
- Dim obj As Object
- Dim factor As Single
- ' Get the current eye location.
- EyeR = CSng(RText.Text)
- EyeTheta = CSng(ThetaText.Text)
- EyePhi = CSng(PhiText.Text)
- ' Create the data.
- CreateData
- ' Get constants for the surfaces.
- LightKdist = CSng(KdistText.Text)
- ' Create a background color.
- BackR = 0
- BackG = 0
- BackB = 0
- ' Fill with another color so we can see progress.
- pic.Line (pic.ScaleLeft, pic.ScaleTop)- _
- Step(pic.ScaleWidth, pic.ScaleHeight), _
- RGB(0, 0, &H80), BF
- ' Rotate the eye onto the Z axis.
- m3PProject Projector, m3Parallel, _
- EyeR, EyePhi, EyeTheta, _
- FocusX, FocusY, FocusZ, _
- 0, 1, 0
- ' Transform the objects.
- For Each obj In Objects
- obj.Apply Projector
- Next obj
- ' Transform the light source.
- m3Apply LightSource.coord, Projector, LightSource.trans
- ' Adjust the incident light values.
- factor = _
- Sqr(LightSource.trans(1) * LightSource.trans(1) + _
- LightSource.trans(2) * LightSource.trans(2) + _
- LightSource.trans(3) * LightSource.trans(3)) _
- + LightKdist + 4
- LightIir = 255 * factor
- LightIig = 255 * factor
- LightIib = 255 * factor
- ' Display the data.
- RayTrace pic, CInt(StepText.Text)
- ' Display the viewing parameters.
- ShowViewingParameters
- End Sub
- ' ************************************************
- ' Start ray tracing for this picture box.
- ' ************************************************
- Sub RayTrace(pic As PictureBox, skip As Integer)
- Dim x As Integer
- Dim y As Integer
- Dim xmax As Integer
- Dim ymax As Integer
- Dim xoff As Integer
- Dim yoff As Integer
- ' Get the transformed coordinates of the eye.
- EyeX = 0
- EyeY = 0
- EyeZ = EyeR
- xoff = pic.ScaleWidth / 2
- yoff = pic.ScaleHeight / 2
- xmax = pic.ScaleLeft + pic.ScaleWidth - 1
- ymax = pic.ScaleTop + pic.ScaleHeight - 1
- For y = pic.ScaleTop To ymax Step skip
- For x = pic.ScaleLeft To xmax Step skip
- ' Calculate the value of pixel (x, y).
- ' After transformation the eye is
- ' at (0, 0, EyeR) and the plane of
- ' projection lies in the X-Y plane.
- If skip < 2 Then
- pic.PSet (x, y), _
- TraceRay(0, 0, EyeR, CSng(x) - xoff, CSng(y) - yoff, -EyeR)
- Else
- pic.Line (x, y)-Step(skip - 1, skip - 1), _
- TraceRay(0, 0, EyeR, CSng(x) - xoff, CSng(y) - yoff, -EyeR), BF
- End If
- Next x
-
- ' Let the user see what's going on.
- pic.Refresh
-
- ' If the Stop button was pressed, stop.
- DoEvents
- If Not Running Then Exit Sub
- Next y
- End Sub
- Sub ShowViewingParameters()
- RText.Text = Format$(EyeR, "0")
- ThetaText.Text = Format$(EyeTheta, "0.0000")
- PhiText.Text = Format$(EyePhi, "0.0000")
- RText.Refresh
- ThetaText.Refresh
- PhiText.Refresh
- End Sub
- ' ************************************************
- ' Return the pixel color given by tracing from
- ' point (px, py, pz) in direction <vx, vy, vz>.
- ' ************************************************
- Function TraceRay(px As Single, py As Single, pz As Single, Vx As Single, Vy As Single, Vz As Single) As Long
- Dim i As Integer
- Dim best_i As Integer
- Dim best_dist As Single
- Dim dist As Single
- Dim r As Integer
- Dim G As Integer
- Dim B As Integer
- If Objects.Count < 1 Then Exit Function
- ' Find the object that's closest.
- best_dist = INFINITY
- best_i = -1
- For i = 1 To Objects.Count
- dist = Objects.Item(i).RayDistance( _
- px, py, pz, Vx, Vy, Vz)
- If best_dist > dist Then
- best_dist = dist
- best_i = i
- End If
- Next i
- ' If we hit nothing, return the background color.
- If best_i < 1 Then
- TraceRay = &H2000000 + _
- RGB(BackR, BackG, BackB)
- Exit Function
- End If
- ' Compute the color at that point.
- Objects.Item(best_i).HitColor Objects, r, G, B
- ' This is a problem for some values of LightKdist.
- If r < 0 Then r = 0
- If G < 0 Then G = 0
- If B < 0 Then B = 0
- TraceRay = &H2000000 + RGB(r, G, B)
- End Function
- ' ************************************************
- ' Do the ray tracing.
- ' ************************************************
- Private Sub CmdGo_Click()
- If Running Then
- Running = False
- CmdGo.Caption = "Stopped"
- CmdGo.Enabled = False
- DoEvents
- Else
- Running = True
- CmdGo.Caption = "Stop"
- MousePointer = vbHourglass
- DoEvents
-
- DrawData Pict
-
- MousePointer = vbDefault
- CmdGo.Enabled = True
- CmdGo.Caption = "Go"
- Running = False
- Beep
- End If
- End Sub
- Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
- Select Case KeyCode
- Case vbKeyLeft
- EyeTheta = EyeTheta - dtheta
-
- Case vbKeyRight
- EyeTheta = EyeTheta + dtheta
-
- Case vbKeyUp
- EyePhi = EyePhi - Dphi
-
- Case vbKeyDown
- EyePhi = EyePhi + Dphi
-
- Case Else
- Exit Sub
- End Select
- ShowViewingParameters
- End Sub
- Private Sub Form_KeyPress(KeyAscii As Integer)
- Select Case KeyAscii
- Case Asc("+")
- EyeR = EyeR + dR
-
- Case Asc("-")
- EyeR = EyeR - dR
-
- Case Else
- Exit Sub
- End Select
- ShowViewingParameters
- End Sub
- Private Sub Form_Load()
- ' Make sure the screen supports palettes.
- If Not GetDeviceCaps(hdc, RASTERCAPS) And RC_PALETTE Then
- Beep
- MsgBox "This monitor does not support palettes.", _
- vbCritical
- End
- End If
- ' Get system palette size and # static colors.
- SysPalSize = GetDeviceCaps(hdc, SIZEPALETTE)
- NumStaticColors = GetDeviceCaps(hdc, NUMRESERVED)
- StaticColor1 = NumStaticColors \ 2 - 1
- StaticColor2 = SysPalSize - NumStaticColors \ 2
- ' Fill the picture's palette with grays.
- MatchGrayPalette Pict
- Pict.Cls
- ' Initialize lighting constants.
- LightSource.coord(1) = 100
- LightSource.coord(2) = -500
- LightSource.coord(3) = 1000
- LightSource.coord(4) = 1
- LightIar = 128
- LightIag = 128
- LightIab = 128
- ' Initialize the eye position.
- EyeR = CSng(RText.Text)
- EyeTheta = CSng(ThetaText.Text)
- EyePhi = CSng(PhiText.Text)
- ' Initialize the projection transformation.
- m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
- End Sub
- ' ***********************************************
- ' Load the control's palette so the non-static
- ' colors are grays. Map the logical palette to
- ' match the system palette. Convert the image to
- ' use the non-static grays.
- ' Leave new system palette entries in SysPal().
- ' ***********************************************
- Sub MatchGrayPalette(pic As Control)
- Dim origpal(0 To 255) As PALETTEENTRY
- Dim wid As Long
- Dim hgt As Long
- Dim bytes() As Byte
- Dim i As Integer
- Dim bm As BITMAP
- Dim hbm As Integer
- Dim status As Long
- Dim x As Integer
- Dim y As Integer
- Dim gray As Single
- Dim dgray As Single
- Dim C As Integer
- Dim clr As Integer
- Dim logpal As Long
- ' Make sure pic has the foreground palette.
- pic.ZOrder
- status = RealizePalette(pic.hdc)
- DoEvents
- ' Get the system palette entries.
- status = GetSystemPaletteEntries(pic.hdc, 0, SysPalSize, origpal(0))
-
- ' Get the image pixels.
- hbm = pic.Image
- status = GetObject(hbm, BITMAP_SIZE, bm)
- wid = bm.bmWidthBytes
- hgt = bm.bmHeight
- ReDim bytes(1 To wid, 1 To hgt)
- status = GetBitmapBits(hbm, wid * hgt, bytes(1, 1))
- ' Make the logical palette as big as possible.
- logpal = pic.Picture.hPal
- If ResizePalette(logpal, SysPalSize) = 0 Then
- Beep
- MsgBox "Error resizing logical palette.", _
- vbExclamation
- Exit Sub
- End If
- ' Blank the non-static colors.
- For i = 0 To StaticColor1
- syspal(i) = origpal(i)
- Next i
- For i = StaticColor1 + 1 To StaticColor2 - 1
- With syspal(i)
- .peRed = 0
- .peGreen = 0
- .peBlue = 0
- .peFlags = PC_NOCOLLAPSE
- End With
- Next i
- For i = StaticColor2 To 255
- syspal(i) = origpal(i)
- Next i
- status = SetPaletteEntries(logpal, 0, SysPalSize, syspal(0))
- ' Insert the non-static grays.
- gray = 0
- dgray = 255 / (StaticColor2 - StaticColor1 - 2)
- For i = StaticColor1 + 1 To StaticColor2 - 1
- C = gray
- gray = gray + dgray
- With syspal(i)
- .peRed = C
- .peGreen = C
- .peBlue = C
- End With
- Next i
- status = SetPaletteEntries(logpal, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, syspal(StaticColor1 + 1))
- ' Realize the gray palette.
- status = RealizePalette(pic.hdc)
- pic.Refresh
- End Sub
- Private Sub mnuFileExit_Click()
- Unload Me
- End Sub
- Private Sub mnuFileSaveBitmap_Click()
- Dim fname As String
- ' Allow the user to pick a file.
- On Error Resume Next
- LoadDialog.filename = "*.BMP"
- LoadDialog.Flags = cdlOFNOverwritePrompt + cdlOFNHideReadOnly
- LoadDialog.ShowSave
- If Err.Number = cdlCancel Then
- Unload LoadDialog
- Exit Sub
- ElseIf Err.Number <> 0 Then
- Unload LoadDialog
- Beep
- MsgBox "Error selecting file.", , vbExclamation
- Exit Sub
- End If
- On Error GoTo 0
- fname = LoadDialog.filename
- SavePicture Pict.Image, fname
- End Sub
- ' ************************************************
- ' Select this choice.
- ' ************************************************
- Private Sub Scene_Click(index As Integer)
- SceneChoice = index
- End Sub
-