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

  1. VERSION 4.00
  2. Begin VB.Form RayForm 
  3.    Appearance      =   0  'Flat
  4.    Caption         =   "RRay2"
  5.    ClientHeight    =   4590
  6.    ClientLeft      =   1830
  7.    ClientTop       =   1260
  8.    ClientWidth     =   6030
  9.    DrawMode        =   14  'Copy Pen
  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          =   5280
  21.    KeyPreview      =   -1  'True
  22.    Left            =   1770
  23.    LinkTopic       =   "Form1"
  24.    ScaleHeight     =   306
  25.    ScaleMode       =   3  'Pixel
  26.    ScaleWidth      =   402
  27.    Top             =   630
  28.    Width           =   6150
  29.    Begin VB.OptionButton Scene 
  30.       Caption         =   "Sphere + Prism"
  31.       BeginProperty Font 
  32.          name            =   "MS Sans Serif"
  33.          charset         =   0
  34.          weight          =   700
  35.          size            =   8.25
  36.          underline       =   0   'False
  37.          italic          =   0   'False
  38.          strikethrough   =   0   'False
  39.       EndProperty
  40.       Height          =   255
  41.       Index           =   4
  42.       Left            =   0
  43.       TabIndex        =   19
  44.       Top             =   1440
  45.       Width           =   2025
  46.    End
  47.    Begin VB.PictureBox Pict 
  48.       AutoRedraw      =   -1  'True
  49.       BackColor       =   &H00FFFF80&
  50.       BeginProperty Font 
  51.          name            =   "MS Sans Serif"
  52.          charset         =   0
  53.          weight          =   700
  54.          size            =   8.25
  55.          underline       =   0   'False
  56.          italic          =   0   'False
  57.          strikethrough   =   0   'False
  58.       EndProperty
  59.       Height          =   3975
  60.       Left            =   2040
  61.       Picture         =   "RRay2.frx":0000
  62.       ScaleHeight     =   261
  63.       ScaleMode       =   3  'Pixel
  64.       ScaleWidth      =   261
  65.       TabIndex        =   11
  66.       Top             =   0
  67.       Width           =   3975
  68.    End
  69.    Begin VB.TextBox RText 
  70.       BeginProperty Font 
  71.          name            =   "MS Sans Serif"
  72.          charset         =   0
  73.          weight          =   700
  74.          size            =   8.25
  75.          underline       =   0   'False
  76.          italic          =   0   'False
  77.          strikethrough   =   0   'False
  78.       EndProperty
  79.       Height          =   285
  80.       Left            =   840
  81.       TabIndex        =   10
  82.       Text            =   "1000"
  83.       Top             =   1800
  84.       Width           =   855
  85.    End
  86.    Begin VB.TextBox ThetaText 
  87.       BeginProperty Font 
  88.          name            =   "MS Sans Serif"
  89.          charset         =   0
  90.          weight          =   700
  91.          size            =   8.25
  92.          underline       =   0   'False
  93.          italic          =   0   'False
  94.          strikethrough   =   0   'False
  95.       EndProperty
  96.       Height          =   285
  97.       Left            =   840
  98.       TabIndex        =   9
  99.       Text            =   "0.6275"
  100.       Top             =   2160
  101.       Width           =   855
  102.    End
  103.    Begin VB.TextBox PhiText 
  104.       BeginProperty Font 
  105.          name            =   "MS Sans Serif"
  106.          charset         =   0
  107.          weight          =   700
  108.          size            =   8.25
  109.          underline       =   0   'False
  110.          italic          =   0   'False
  111.          strikethrough   =   0   'False
  112.       EndProperty
  113.       Height          =   285
  114.       Left            =   840
  115.       TabIndex        =   8
  116.       Text            =   "-0.4713"
  117.       Top             =   2520
  118.       Width           =   855
  119.    End
  120.    Begin VB.TextBox KdistText 
  121.       BeginProperty Font 
  122.          name            =   "MS Sans Serif"
  123.          charset         =   0
  124.          weight          =   700
  125.          size            =   8.25
  126.          underline       =   0   'False
  127.          italic          =   0   'False
  128.          strikethrough   =   0   'False
  129.       EndProperty
  130.       Height          =   285
  131.       Left            =   840
  132.       TabIndex        =   7
  133.       Text            =   "-850"
  134.       Top             =   3000
  135.       Width           =   855
  136.    End
  137.    Begin VB.CommandButton CmdGo 
  138.       Caption         =   "Go"
  139.       Default         =   -1  'True
  140.       BeginProperty Font 
  141.          name            =   "MS Sans Serif"
  142.          charset         =   0
  143.          weight          =   700
  144.          size            =   8.25
  145.          underline       =   0   'False
  146.          italic          =   0   'False
  147.          strikethrough   =   0   'False
  148.       EndProperty
  149.       Height          =   375
  150.       Left            =   600
  151.       TabIndex        =   6
  152.       Top             =   4200
  153.       Width           =   1095
  154.    End
  155.    Begin VB.TextBox StepText 
  156.       BeginProperty Font 
  157.          name            =   "MS Sans Serif"
  158.          charset         =   0
  159.          weight          =   700
  160.          size            =   8.25
  161.          underline       =   0   'False
  162.          italic          =   0   'False
  163.          strikethrough   =   0   'False
  164.       EndProperty
  165.       Height          =   285
  166.       Left            =   840
  167.       TabIndex        =   5
  168.       Text            =   "4"
  169.       Top             =   3840
  170.       Width           =   855
  171.    End
  172.    Begin VB.OptionButton Scene 
  173.       Caption         =   "Spheres Over Plane"
  174.       BeginProperty Font 
  175.          name            =   "MS Sans Serif"
  176.          charset         =   0
  177.          weight          =   700
  178.          size            =   8.25
  179.          underline       =   0   'False
  180.          italic          =   0   'False
  181.          strikethrough   =   0   'False
  182.       EndProperty
  183.       Height          =   255
  184.       Index           =   0
  185.       Left            =   0
  186.       TabIndex        =   4
  187.       Top             =   0
  188.       Value           =   -1  'True
  189.       Width           =   2025
  190.    End
  191.    Begin VB.OptionButton Scene 
  192.       Caption         =   "Spheres + Square"
  193.       BeginProperty Font 
  194.          name            =   "MS Sans Serif"
  195.          charset         =   0
  196.          weight          =   700
  197.          size            =   8.25
  198.          underline       =   0   'False
  199.          italic          =   0   'False
  200.          strikethrough   =   0   'False
  201.       EndProperty
  202.       Height          =   255
  203.       Index           =   1
  204.       Left            =   0
  205.       TabIndex        =   3
  206.       Top             =   360
  207.       Width           =   2025
  208.    End
  209.    Begin VB.OptionButton Scene 
  210.       Caption         =   "Three Spheres"
  211.       BeginProperty Font 
  212.          name            =   "MS Sans Serif"
  213.          charset         =   0
  214.          weight          =   700
  215.          size            =   8.25
  216.          underline       =   0   'False
  217.          italic          =   0   'False
  218.          strikethrough   =   0   'False
  219.       EndProperty
  220.       Height          =   255
  221.       Index           =   2
  222.       Left            =   0
  223.       TabIndex        =   2
  224.       Top             =   720
  225.       Width           =   2025
  226.    End
  227.    Begin VB.OptionButton Scene 
  228.       Caption         =   "Sphere + Cylinder"
  229.       BeginProperty Font 
  230.          name            =   "MS Sans Serif"
  231.          charset         =   0
  232.          weight          =   700
  233.          size            =   8.25
  234.          underline       =   0   'False
  235.          italic          =   0   'False
  236.          strikethrough   =   0   'False
  237.       EndProperty
  238.       Height          =   255
  239.       Index           =   3
  240.       Left            =   0
  241.       TabIndex        =   1
  242.       Top             =   1080
  243.       Width           =   2025
  244.    End
  245.    Begin VB.TextBox DepthText 
  246.       BeginProperty Font 
  247.          name            =   "MS Sans Serif"
  248.          charset         =   0
  249.          weight          =   700
  250.          size            =   8.25
  251.          underline       =   0   'False
  252.          italic          =   0   'False
  253.          strikethrough   =   0   'False
  254.       EndProperty
  255.       Height          =   285
  256.       Left            =   840
  257.       TabIndex        =   0
  258.       Text            =   "1"
  259.       Top             =   3480
  260.       Width           =   855
  261.    End
  262.    Begin VB.Label Label1 
  263.       Caption         =   "R"
  264.       BeginProperty Font 
  265.          name            =   "MS Sans Serif"
  266.          charset         =   0
  267.          weight          =   700
  268.          size            =   8.25
  269.          underline       =   0   'False
  270.          italic          =   0   'False
  271.          strikethrough   =   0   'False
  272.       EndProperty
  273.       Height          =   255
  274.       Index           =   0
  275.       Left            =   240
  276.       TabIndex        =   18
  277.       Top             =   1800
  278.       Width           =   255
  279.    End
  280.    Begin VB.Label Label1 
  281.       Caption         =   "Theta"
  282.       BeginProperty Font 
  283.          name            =   "MS Sans Serif"
  284.          charset         =   0
  285.          weight          =   700
  286.          size            =   8.25
  287.          underline       =   0   'False
  288.          italic          =   0   'False
  289.          strikethrough   =   0   'False
  290.       EndProperty
  291.       Height          =   255
  292.       Index           =   1
  293.       Left            =   240
  294.       TabIndex        =   17
  295.       Top             =   2160
  296.       Width           =   495
  297.    End
  298.    Begin VB.Label Label1 
  299.       Caption         =   "Phi"
  300.       BeginProperty Font 
  301.          name            =   "MS Sans Serif"
  302.          charset         =   0
  303.          weight          =   700
  304.          size            =   8.25
  305.          underline       =   0   'False
  306.          italic          =   0   'False
  307.          strikethrough   =   0   'False
  308.       EndProperty
  309.       Height          =   255
  310.       Index           =   2
  311.       Left            =   240
  312.       TabIndex        =   16
  313.       Top             =   2520
  314.       Width           =   375
  315.    End
  316.    Begin MSComDlg.CommonDialog LoadDialog 
  317.       Left            =   0
  318.       Top             =   4320
  319.       _Version        =   65536
  320.       _ExtentX        =   847
  321.       _ExtentY        =   847
  322.       _StockProps     =   0
  323.       CancelError     =   -1  'True
  324.    End
  325.    Begin VB.Label Label1 
  326.       Caption         =   "k"
  327.       BeginProperty Font 
  328.          name            =   "MS Sans Serif"
  329.          charset         =   0
  330.          weight          =   700
  331.          size            =   8.25
  332.          underline       =   0   'False
  333.          italic          =   0   'False
  334.          strikethrough   =   0   'False
  335.       EndProperty
  336.       Height          =   255
  337.       Index           =   6
  338.       Left            =   240
  339.       TabIndex        =   15
  340.       Top             =   3000
  341.       Width           =   135
  342.    End
  343.    Begin VB.Label Label1 
  344.       Caption         =   "dist"
  345.       BeginProperty Font 
  346.          name            =   "MS Sans Serif"
  347.          charset         =   0
  348.          weight          =   700
  349.          size            =   8.25
  350.          underline       =   0   'False
  351.          italic          =   0   'False
  352.          strikethrough   =   0   'False
  353.       EndProperty
  354.       Height          =   255
  355.       Index           =   8
  356.       Left            =   360
  357.       TabIndex        =   14
  358.       Top             =   3120
  359.       Width           =   375
  360.    End
  361.    Begin VB.Label Label1 
  362.       Caption         =   "Step"
  363.       BeginProperty Font 
  364.          name            =   "MS Sans Serif"
  365.          charset         =   0
  366.          weight          =   700
  367.          size            =   8.25
  368.          underline       =   0   'False
  369.          italic          =   0   'False
  370.          strikethrough   =   0   'False
  371.       EndProperty
  372.       Height          =   255
  373.       Index           =   13
  374.       Left            =   240
  375.       TabIndex        =   13
  376.       Top             =   3840
  377.       Width           =   615
  378.    End
  379.    Begin VB.Label Label1 
  380.       Caption         =   "Depth"
  381.       BeginProperty Font 
  382.          name            =   "MS Sans Serif"
  383.          charset         =   0
  384.          weight          =   700
  385.          size            =   8.25
  386.          underline       =   0   'False
  387.          italic          =   0   'False
  388.          strikethrough   =   0   'False
  389.       EndProperty
  390.       Height          =   255
  391.       Index           =   3
  392.       Left            =   240
  393.       TabIndex        =   12
  394.       Top             =   3480
  395.       Width           =   615
  396.    End
  397.    Begin VB.Menu mnuFile 
  398.       Caption         =   "&File"
  399.       Begin VB.Menu mnuFileSaveBitmap 
  400.          Caption         =   "&Save Bitmap..."
  401.          Shortcut        =   ^S
  402.       End
  403.       Begin VB.Menu mnuFileSep 
  404.          Caption         =   "-"
  405.       End
  406.       Begin VB.Menu mnuFileExit 
  407.          Caption         =   "E&xit"
  408.       End
  409.    End
  410. Attribute VB_Name = "RayForm"
  411. Attribute VB_Creatable = False
  412. Attribute VB_Exposed = False
  413. Option Explicit
  414. Dim SysPalSize As Integer
  415. Dim NumStaticColors As Integer
  416. Dim StaticColor1 As Integer
  417. Dim StaticColor2 As Integer
  418. Dim syspal(0 To 255) As PALETTEENTRY
  419. ' Location of viewing eye.
  420. Dim EyeR As Single
  421. Dim EyeTheta As Single
  422. Dim EyePhi As Single
  423. Const dtheta = PI / 20
  424. Const Dphi = PI / 20
  425. Const dR = 1
  426. ' Location of focus point.
  427. Const FocusX = 0#
  428. Const FocusY = 0#
  429. Const FocusZ = 0#
  430. Dim Projector(1 To 4, 1 To 4) As Single
  431. Dim Running As Boolean
  432. Dim SceneChoice As Integer
  433. ' ************************************************
  434. ' Create the objects in the scene.
  435. ' ************************************************
  436. Sub CreateData()
  437. Dim obj As Object
  438. Dim s As Single
  439. Dim i As Integer
  440. Dim j As Integer
  441. Dim j1 As Integer
  442. Dim x1 As Single
  443. Dim x2 As Single
  444. Dim xmid As Single
  445. Dim y1 As Single
  446. Dim y2 As Single
  447. Dim z1 As Single
  448. Dim z2 As Single
  449.     Set Objects = New Collection
  450.     Select Case SceneChoice
  451.         Case 0  ' 2 Spheres + Plane.
  452.             ' Reflective sphere of radius 40 at
  453.             ' (-40, -40, 0).
  454.             Set obj = New ObjSphere
  455.             Objects.Add obj
  456.             obj.Initialize 40, -40, -40, 0
  457.             obj.SetKd 0.45, 0.45, 0.45  ' Diffuse.
  458.             obj.SetKa 0.4, 0.4, 0.4     ' Ambient.
  459.             obj.SetSpec 20, 0.35        ' Specular.
  460.             obj.SetKr 0.5, 0.5, 0.5     ' Reflected.
  461.         
  462.             ' Transparent sphere of radius 40 at
  463.             ' (40, -40, 0).
  464.             Set obj = New ObjSphere
  465.             Objects.Add obj
  466.             obj.Initialize 40, 40, -40, 0
  467.             obj.SetKd 0.1, 0.1, 0.1     ' Diffuse.
  468.             obj.SetKa 0.1, 0.1, 0.1     ' Ambient.
  469.             obj.SetSpec 20, 0.35        ' Specular.
  470.             obj.SetKr 0.2, 0.2, 0.2     ' Reflected.
  471.             obj.SetKt 10, 1, 1.4, _
  472.                 1, 1, 1                 ' Transmitted.
  473.         
  474.             ' Relfective X-Z plane.
  475.             Set obj = New ObjPlane
  476.             Objects.Add obj
  477.             obj.Initialize 0, 0, 0, 0, -1, 0
  478.             obj.SetKd 0.3, 0.3, 0.3     ' Diffuse.
  479.             obj.SetKa 0.2, 0.2, 0.2     ' Ambient.
  480.             obj.SetSpec 20, 0.35        ' Specular.
  481.             obj.SetKr 0.5, 0.5, 0.5     ' Reflected.
  482.         Case 1  ' Spheres + Square.
  483.             ' Non-reflective sphere of radius 30
  484.             ' at (50, 20, -50).
  485.             Set obj = New ObjSphere
  486.             Objects.Add obj
  487.             obj.Initialize 30, 50, 20, -50
  488.             obj.SetKd 0.45, 0.45, 0.45  ' Diffuse.
  489.             obj.SetKa 0.4, 0.4, 0.4     ' Ambient.
  490.             obj.SetSpec 20, 0.35        ' Specular.
  491.         
  492.             ' Non-reflective sphere of radius 40
  493.             ' at (-50, 40, -30).
  494.             Set obj = New ObjSphere
  495.             Objects.Add obj
  496.             obj.Initialize 40, -50, 40, -30
  497.             obj.SetKd 0.45, 0.45, 0.45  ' Diffuse.
  498.             obj.SetKa 0.4, 0.4, 0.4     ' Ambient.
  499.             obj.SetSpec 20, 0.35        ' Specular.
  500.             ' Half-silvered square in the Y-Z plane.
  501.             s = 100
  502.             Set obj = New ObjPolygon
  503.             Objects.Add obj
  504.             obj.AddPoint _
  505.                 0, s, s, _
  506.                 0, -s, s, _
  507.                 0, -s, -s, _
  508.                 0, s, -s
  509.             obj.DefinePlane
  510.             obj.SetKd 0.2, 0.2, 0.2     ' Diffuse.
  511.             obj.SetKa 0.2, 0.2, 0.2     ' Ambient.
  512.             obj.SetSpec 20, 0.5         ' Specular.
  513.             obj.SetKr 0.5, 0.5, 0.5     ' Reflected.
  514.             obj.SetKt 10, 1, 1, _
  515.                 0.5, 0.5, 0.5           ' Transmitted.
  516.         Case 2  ' Three Spheres.
  517.             ' Reflective sphere of radius 50 at
  518.             ' (-50, -50, -50).
  519.             Set obj = New ObjSphere
  520.             Objects.Add obj
  521.             obj.Initialize 50, -50, -50, -50
  522.             obj.SetKd 0.45, 0.45, 0.45  ' Diffuse.
  523.             obj.SetKa 0.4, 0.4, 0.4     ' Ambient.
  524.             obj.SetSpec 20, 0.35        ' Specular.
  525.             obj.SetKr 0.75, 0.75, 0.75  ' Reflected.
  526.         
  527.             ' Non-reflective sphere of radius 40 at
  528.             ' (-40, -40, 40).
  529.             Set obj = New ObjSphere
  530.             Objects.Add obj
  531.             obj.Initialize 40, -40, -40, 40
  532.             obj.SetKd 0.45, 0.45, 0.45  ' Diffuse.
  533.             obj.SetKa 0.4, 0.4, 0.4     ' Ambient.
  534.             obj.SetSpec 20, 0.35        ' Specular.
  535.         
  536.             ' Transparent sphere of radius 40 at
  537.             ' (40, -40, 0).
  538.             Set obj = New ObjSphere
  539.             Objects.Add obj
  540.             obj.Initialize 40, 40, -40, 0
  541.             obj.SetKd 0.1, 0.1, 0.1     ' Diffuse.
  542.             obj.SetKa 0.1, 0.1, 0.1     ' Ambient.
  543.             obj.SetSpec 20, 0.3         ' Specular.
  544.             obj.SetKr 0.1, 0.1, 0.1     ' Reflected.
  545.             obj.SetKt 10, 1, 1.4, _
  546.                 1, 1, 1                 ' Transmitted.
  547.             ' Relfective X-Z plane.
  548.             Set obj = New ObjPlane
  549.             Objects.Add obj
  550.             obj.Initialize 0, 0, 0, 0, -1, 0
  551.             obj.SetKd 0.3, 0.3, 0.3     ' Diffuse.
  552.             obj.SetKa 0.2, 0.2, 0.2     ' Ambient.
  553.             obj.SetSpec 20, 0.35        ' Specular.
  554.             obj.SetKr 0.4, 0.4, 0.4     ' Reflected.
  555.         Case 3  ' Sphere + Cylinder.
  556.             ' Transparent cylinder of radius 30
  557.             ' between (-900, 1000, 0) and (1100, -1000, 0).
  558.             Set obj = New ObjCylinder
  559.             Objects.Add obj
  560.             obj.Initialize 30, -900, 1000, 0, 1100, -1000, 0
  561.             obj.SetKd 0.1, 0.1, 0.1     ' Diffuse.
  562.             obj.SetKa 0.1, 0.1, 0.1     ' Ambient.
  563.             obj.SetSpec 20, 0.5         ' Specular.
  564.             obj.SetKr 0.2, 0.2, 0.2     ' Reflected.
  565.             obj.SetKt 10, 1, 1.4, _
  566.                 1, 1, 1                 ' Transmitted.
  567.             
  568.             ' Transparent sphere of radius 60 at
  569.             ' (-50, -50, 0).
  570.             Set obj = New ObjSphere
  571.             Objects.Add obj
  572.             obj.Initialize 60, -50, -50, 0
  573.             obj.SetKd 0.1, 0.1, 0.1     ' Diffuse.
  574.             obj.SetKa 0.1, 0.1, 0.1     ' Ambient.
  575.             obj.SetSpec 20, 0.35        ' Specular.
  576.             obj.SetKr 0.2, 0.2, 0.2     ' Reflected.
  577.             obj.SetKt 10, 1, 1.4, _
  578.                 1, 1, 1                 ' Transmitted.
  579.             ' Make some non-reflective stripes.
  580.             s = 20
  581.             For i = -6 * s To 6 * s Step 2 * s
  582.                 ' Make a rectangle.
  583.                 Set obj = New ObjPolygon
  584.                 Objects.Add obj
  585.                 obj.AddPoint _
  586.                     i, -1000, -20, _
  587.                     i + s, -1000, -20, _
  588.                     i + s, 1000, -20, _
  589.                     i, 1000, -20
  590.                 obj.DefinePlane
  591.                 obj.SetKd 0.5, 0.5, 0.5     ' Diffuse.
  592.                 obj.SetKa 0.5, 0.5, 0.5     ' Ambient.
  593.                 obj.SetSpec 20, 0.1         ' Specular.
  594.             Next i
  595.         Case 4  ' Sphere + Prism.
  596.             ' Non-reflective sphere of radius 50
  597.             ' at (-50, -50, 0).
  598.             Set obj = New ObjSphere
  599.             Objects.Add obj
  600.             obj.Initialize 50, -50, -50, 0
  601.             obj.SetKd 0.45, 0.45, 0.45  ' Diffuse.
  602.             obj.SetKa 0.4, 0.4, 0.4     ' Ambient.
  603.             obj.SetSpec 20, 0.35        ' Specular.
  604.         
  605.             ' Transparent prism.
  606.             x1 = 15
  607.             x2 = 45
  608.             y1 = 0
  609.             y2 = -100
  610.             z1 = -50
  611.             z2 = 50
  612.             xmid = (x1 + x2) / 2
  613.             
  614.             Set obj = New ObjFace
  615.             Objects.Add obj
  616.             obj.AddPoint _
  617.                 x1, y1, z1, _
  618.                 x1, y2, z1, _
  619.                 x2, y2, z1, _
  620.                 x2, y1, z1
  621.             obj.DefinePlane
  622.             obj.SetKd 0.1, 0.1, 0.1     ' Diffuse.
  623.             obj.SetKa 0.1, 0.1, 0.1     ' Ambient.
  624.             obj.SetSpec 20, 0.35        ' Specular.
  625.             obj.SetKr 0.2, 0.2, 0.2     ' Reflected.
  626.             obj.SetKt 10, 1, 1.4, _
  627.                 1, 1, 1                 ' Transmitted.
  628.         
  629.             Set obj = New ObjFace
  630.             Objects.Add obj
  631.             obj.AddPoint _
  632.                 xmid, y1, z2, _
  633.                 x2, y1, z1, _
  634.                 x2, y2, z1, _
  635.                 xmid, y2, z2
  636.             obj.DefinePlane
  637.             obj.SetKd 0.1, 0.1, 0.1     ' Diffuse.
  638.             obj.SetKa 0.1, 0.1, 0.1     ' Ambient.
  639.             obj.SetSpec 20, 0.35        ' Specular.
  640.             obj.SetKr 0.2, 0.2, 0.2     ' Reflected.
  641.             obj.SetKt 10, 1, 1.4, _
  642.                 1, 1, 1                 ' Transmitted.
  643.         
  644.             Set obj = New ObjFace
  645.             Objects.Add obj
  646.             obj.AddPoint _
  647.                 xmid, y2, z2, _
  648.                 x2, y2, z1, _
  649.                 x1, y2, z1
  650.             obj.DefinePlane
  651.             obj.SetKd 0.1, 0.1, 0.1     ' Diffuse.
  652.             obj.SetKa 0.1, 0.1, 0.1     ' Ambient.
  653.             obj.SetSpec 20, 0.35        ' Specular.
  654.             obj.SetKr 0.2, 0.2, 0.2     ' Reflected.
  655.             obj.SetKt 10, 1, 1.4, _
  656.                 1, 1, 1                 ' Transmitted.
  657.             
  658.             Set obj = New ObjFace
  659.             Objects.Add obj
  660.             obj.AddPoint _
  661.                 x1, y1, z1, _
  662.                 x2, y1, z1, _
  663.                 xmid, y1, z2
  664.             obj.DefinePlane
  665.             obj.SetKd 0.1, 0.1, 0.1     ' Diffuse.
  666.             obj.SetKa 0.1, 0.1, 0.1     ' Ambient.
  667.             obj.SetSpec 20, 0.35        ' Specular.
  668.             obj.SetKr 0.2, 0.2, 0.2     ' Reflected.
  669.             obj.SetKt 10, 1, 1.4, _
  670.                 1, 1, 1                 ' Transmitted.
  671.             
  672.             Set obj = New ObjFace
  673.             Objects.Add obj
  674.             obj.AddPoint _
  675.                 xmid, y2, z2, _
  676.                 x1, y2, z1, _
  677.                 x1, y1, z1, _
  678.                 xmid, y1, z2
  679.             obj.DefinePlane
  680.             obj.SetKd 0.1, 0.1, 0.1     ' Diffuse.
  681.             obj.SetKa 0.1, 0.1, 0.1     ' Ambient.
  682.             obj.SetSpec 20, 0.35        ' Specular.
  683.             obj.SetKr 0.2, 0.2, 0.2     ' Reflected.
  684.             obj.SetKt 10, 1, 1.4, _
  685.                 1, 1, 1                 ' Transmitted.
  686.     End Select
  687. End Sub
  688. ' *******************************************************
  689. ' Project and draw.
  690. ' *******************************************************
  691. Private Sub DrawData(pic As Object)
  692. Dim Projector(1 To 4, 1 To 4) As Single
  693. Dim obj As Object
  694. Dim factor As Single
  695.     ' Get the current eye location.
  696.     EyeR = CSng(RText.Text)
  697.     EyeTheta = CSng(ThetaText.Text)
  698.     EyePhi = CSng(PhiText.Text)
  699.     ' Create the data.
  700.     CreateData
  701.     ' Get constants for the surfaces.
  702.     LightKdist = CSng(KdistText.Text)
  703.     ' Create a background color.
  704.     BackR = 0
  705.     BackG = 0
  706.     BackB = 0
  707.     ' Fill with another color so we can see progress.
  708.     pic.Line (pic.ScaleLeft, pic.ScaleTop)- _
  709.         Step(pic.ScaleWidth, pic.ScaleHeight), _
  710.         RGB(0, 0, &H80), BF
  711.     ' Rotate the eye onto the Z axis.
  712.     m3PProject Projector, m3Parallel, _
  713.         EyeR, EyePhi, EyeTheta, _
  714.         FocusX, FocusY, FocusZ, _
  715.         0, 1, 0
  716.     ' Transform the objects.
  717.     For Each obj In Objects
  718.         obj.Apply Projector
  719.     Next obj
  720.     ' Transform the light source.
  721.     m3Apply LightSource.coord, Projector, LightSource.trans
  722.     ' Adjust the incident light values.
  723.     factor = _
  724.         Sqr(LightSource.trans(1) * LightSource.trans(1) + _
  725.             LightSource.trans(2) * LightSource.trans(2) + _
  726.             LightSource.trans(3) * LightSource.trans(3)) _
  727.             + LightKdist + 4
  728.     LightIir = 255 * factor
  729.     LightIig = 255 * factor
  730.     LightIib = 255 * factor
  731.     ' Display the data.
  732.     RayTrace pic, CInt(StepText.Text)
  733.     ' Display the viewing parameters.
  734.     ShowViewingParameters
  735. End Sub
  736. ' ************************************************
  737. ' Start ray tracing for this picture box.
  738. ' ************************************************
  739. Sub RayTrace(pic As PictureBox, skip As Integer)
  740. Dim x As Integer
  741. Dim y As Integer
  742. Dim xmax As Integer
  743. Dim ymax As Integer
  744. Dim xoff As Integer
  745. Dim yoff As Integer
  746. Dim r As Integer
  747. Dim G As Integer
  748. Dim B As Integer
  749. Dim max_depth As Integer
  750.     ' Get the transformed coordinates of the eye.
  751.     EyeX = 0
  752.     EyeY = 0
  753.     EyeZ = EyeR
  754.     ' Get the maximum depth of recursion.
  755.     max_depth = CInt(DepthText.Text)
  756.     xoff = pic.ScaleWidth / 2
  757.     yoff = pic.ScaleHeight / 2
  758.     xmax = pic.ScaleLeft + pic.ScaleWidth - 1
  759.     ymax = pic.ScaleTop + pic.ScaleHeight - 1
  760.     For y = pic.ScaleTop To ymax Step skip
  761.         For x = pic.ScaleLeft To xmax Step skip
  762.             ' Calculate the value of pixel (x, y).
  763.             ' After transformation the eye is
  764.             ' at (0, 0, EyeR) and the plane of
  765.             ' projection lies in the X-Y plane.
  766.             TraceRay max_depth, 0, 0, EyeR, _
  767.                 CSng(x) - xoff, _
  768.                 CSng(y) - yoff, _
  769.                 -EyeR, _
  770.                 r, G, B
  771.                 
  772.             ' Draw the pixel.
  773.             If skip < 2 Then
  774.                 pic.PSet (x, y), RGB(r, G, B)
  775.             Else
  776.                 pic.Line (x, y)- _
  777.                     Step(skip - 1, skip - 1), _
  778.                     RGB(r, G, B), BF
  779.             End If
  780.         Next x
  781.         
  782.         ' Let the user see what's going on.
  783.         pic.Refresh
  784.         
  785.         ' If the Stop button was pressed, stop.
  786.         DoEvents
  787.         If Not Running Then Exit Sub
  788.     Next y
  789. End Sub
  790. Sub ShowViewingParameters()
  791.     RText.Text = Format$(EyeR, "0")
  792.     ThetaText.Text = Format$(EyeTheta, "0.0000")
  793.     PhiText.Text = Format$(EyePhi, "0.0000")
  794.     RText.Refresh
  795.     ThetaText.Refresh
  796.     PhiText.Refresh
  797. End Sub
  798. ' ************************************************
  799. ' Do the ray tracing.
  800. ' ************************************************
  801. Private Sub CmdGo_Click()
  802.     If Running Then
  803.         Running = False
  804.         CmdGo.Caption = "Stopped"
  805.         CmdGo.Enabled = False
  806.         DoEvents
  807.     Else
  808.         Running = True
  809.         CmdGo.Caption = "Stop"
  810.         MousePointer = vbHourglass
  811.         DoEvents
  812.         
  813.         DrawData Pict
  814.         
  815.         MousePointer = vbDefault
  816.         CmdGo.Enabled = True
  817.         CmdGo.Caption = "Go"
  818.         Running = False
  819.         Beep
  820.     End If
  821. End Sub
  822. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  823.     Select Case KeyCode
  824.         Case vbKeyLeft
  825.             EyeTheta = EyeTheta - dtheta
  826.         
  827.         Case vbKeyRight
  828.             EyeTheta = EyeTheta + dtheta
  829.         
  830.         Case vbKeyUp
  831.             EyePhi = EyePhi - Dphi
  832.         
  833.         Case vbKeyDown
  834.             EyePhi = EyePhi + Dphi
  835.                 
  836.         Case Else
  837.             Exit Sub
  838.     End Select
  839.     ShowViewingParameters
  840. End Sub
  841. Private Sub Form_KeyPress(KeyAscii As Integer)
  842.     Select Case KeyAscii
  843.         Case Asc("+")
  844.             EyeR = EyeR + dR
  845.         
  846.         Case Asc("-")
  847.             EyeR = EyeR - dR
  848.         
  849.         Case Else
  850.             Exit Sub
  851.     End Select
  852.     ShowViewingParameters
  853. End Sub
  854. Private Sub Form_Load()
  855.     ' Make sure the screen supports palettes.
  856.     If Not GetDeviceCaps(hdc, RASTERCAPS) And RC_PALETTE Then
  857.         Beep
  858.         MsgBox "This monitor does not support palettes.", _
  859.             vbCritical
  860.         End
  861.     End If
  862.     ' Get system palette size and # static colors.
  863.     SysPalSize = GetDeviceCaps(hdc, SIZEPALETTE)
  864.     NumStaticColors = GetDeviceCaps(hdc, NUMRESERVED)
  865.     StaticColor1 = NumStaticColors \ 2 - 1
  866.     StaticColor2 = SysPalSize - NumStaticColors \ 2
  867.     ' Fill the picture's palette with grays.
  868.     MatchGrayPalette Pict
  869.     Pict.Cls
  870.     ' Initialize lighting constants.
  871.     LightSource.coord(1) = 100
  872.     LightSource.coord(2) = -500
  873.     LightSource.coord(3) = 1000
  874.     LightSource.coord(4) = 1
  875.     LightIar = 128
  876.     LightIag = 128
  877.     LightIab = 128
  878.     ' Initialize the eye position.
  879.     EyeR = CSng(RText.Text)
  880.     EyeTheta = CSng(ThetaText.Text)
  881.     EyePhi = CSng(PhiText.Text)
  882.     ' Initialize the projection transformation.
  883.     m3PProject Projector, m3Perspective, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  884. End Sub
  885. ' ***********************************************
  886. ' Load the control's palette so the non-static
  887. ' colors are grays. Map the logical palette to
  888. ' match the system palette. Convert the image to
  889. ' use the non-static grays.
  890. ' Leave new system palette entries in SysPal().
  891. ' ***********************************************
  892. Sub MatchGrayPalette(pic As Control)
  893. Dim origpal(0 To 255) As PALETTEENTRY
  894. Dim wid As Long
  895. Dim hgt As Long
  896. Dim bytes() As Byte
  897. Dim i As Integer
  898. Dim bm As BITMAP
  899. Dim hbm As Integer
  900. Dim status As Long
  901. Dim x As Integer
  902. Dim y As Integer
  903. Dim gray As Single
  904. Dim dgray As Single
  905. Dim C As Integer
  906. Dim clr As Integer
  907. Dim logpal As Long
  908.     ' Make sure pic has the foreground palette.
  909.     pic.ZOrder
  910.     status = RealizePalette(pic.hdc)
  911.     DoEvents
  912.     ' Get the system palette entries.
  913.     status = GetSystemPaletteEntries(pic.hdc, 0, SysPalSize, origpal(0))
  914.         
  915.     ' Get the image pixels.
  916.     hbm = pic.Image
  917.     status = GetObject(hbm, BITMAP_SIZE, bm)
  918.     wid = bm.bmWidthBytes
  919.     hgt = bm.bmHeight
  920.     ReDim bytes(1 To wid, 1 To hgt)
  921.     status = GetBitmapBits(hbm, wid * hgt, bytes(1, 1))
  922.     ' Make the logical palette as big as possible.
  923.     logpal = pic.Picture.hPal
  924.     If ResizePalette(logpal, SysPalSize) = 0 Then
  925.         Beep
  926.         MsgBox "Error resizing logical palette.", _
  927.             vbExclamation
  928.         Exit Sub
  929.     End If
  930.     ' Blank the non-static colors.
  931.     For i = 0 To StaticColor1
  932.         syspal(i) = origpal(i)
  933.     Next i
  934.     For i = StaticColor1 + 1 To StaticColor2 - 1
  935.         With syspal(i)
  936.             .peRed = 0
  937.             .peGreen = 0
  938.             .peBlue = 0
  939.             .peFlags = PC_NOCOLLAPSE
  940.         End With
  941.     Next i
  942.     For i = StaticColor2 To 255
  943.         syspal(i) = origpal(i)
  944.     Next i
  945.     status = SetPaletteEntries(logpal, 0, SysPalSize, syspal(0))
  946.     ' Insert the non-static grays.
  947.     gray = 0
  948.     dgray = 255 / (StaticColor2 - StaticColor1 - 2)
  949.     For i = StaticColor1 + 1 To StaticColor2 - 1
  950.         C = gray
  951.         gray = gray + dgray
  952.         With syspal(i)
  953.             .peRed = C
  954.             .peGreen = C
  955.             .peBlue = C
  956.         End With
  957.     Next i
  958.     status = SetPaletteEntries(logpal, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, syspal(StaticColor1 + 1))
  959.     ' Realize the gray palette.
  960.     status = RealizePalette(pic.hdc)
  961.     pic.Refresh
  962. End Sub
  963. ' ************************************************
  964. ' Halt immediately in case we're in the middle of
  965. ' ray tracing.
  966. ' ************************************************
  967. Private Sub Form_Unload(Cancel As Integer)
  968.     End
  969. End Sub
  970. Private Sub mnuFileExit_Click()
  971.     Unload Me
  972. End Sub
  973. Private Sub mnuFileSaveBitmap_Click()
  974. Dim fname As String
  975.     ' Allow the user to pick a file.
  976.     On Error Resume Next
  977.     LoadDialog.filename = "*.BMP"
  978.     LoadDialog.Flags = cdlOFNOverwritePrompt + cdlOFNHideReadOnly
  979.     LoadDialog.ShowSave
  980.     If Err.Number = cdlCancel Then
  981.         Unload LoadDialog
  982.         Exit Sub
  983.     ElseIf Err.Number <> 0 Then
  984.         Unload LoadDialog
  985.         Beep
  986.         MsgBox "Error selecting file.", , vbExclamation
  987.         Exit Sub
  988.     End If
  989.     On Error GoTo 0
  990.     fname = LoadDialog.filename
  991.     SavePicture Pict.Image, fname
  992. End Sub
  993. ' ************************************************
  994. ' Select this choice.
  995. ' ************************************************
  996. Private Sub Scene_Click(index As Integer)
  997.     SceneChoice = index
  998. End Sub
  999.