home *** CD-ROM | disk | FTP | other *** search
/ PC Graphics Unleashed / PC Graphics Unleashed.iso / ch17 / bas / quadratc.bas < prev   
Encoding:
BASIC Source File  |  1994-08-04  |  6.3 KB  |  197 lines

  1. '=================================================================
  2. '  PROGRAM: QUADRATC.BAS
  3. '   AUTHOR: Rob McGregor
  4. '-----------------------------------------------------------------
  5. '  PURPOSE: Creates a Polyray scene file containing 3 spheres
  6. '           that follow different quadratic paths, starting and
  7. '           ending at the same points.  The paths are controlled
  8. '           by the variables s1Nc, s2Nc, and s3Nc (the Nc
  9. '           parameter in the equations)
  10. '-----------------------------------------------------------------
  11. '           This is a "virtual animation," in that it calculates
  12. '           the positions of the 3 spheres for each virtual frame,
  13. '           and adjusts the transparency of the spheres based on
  14. '           the value of t.  The resulting image shows the entire
  15. '           animation in only 1 frame.
  16. '=================================================================
  17.  
  18. DECLARE SUB quadratic (s1Nc!, s2Nc!, s3Nc!, startF%, endF%, fout$)
  19.  
  20. CLS
  21. PRINT "This program creates a Polyray scene file demonstrating ";
  22. PRINT "quadratic blending."
  23. PRINT
  24. PRINT "Please enter a path and file name for the resulting ";
  25. PRINT "Polyray scene file"
  26. LINE INPUT "==>"; fout$
  27.  
  28. INPUT "Slope of sphere 1 (from -10 to 10)"; s1Nc!
  29. INPUT "Slope of sphere 2 (from -10 to 10)"; s2Nc!
  30. INPUT "Slope of sphere 3 (from -10 to 10)"; s3Nc!
  31. INPUT "Starting frame of the virtual animation"; startF%
  32. INPUT "Ending frame of the virtual animation"; endF%
  33.  
  34. CALL quadratic(s1Nc!, s2Nc!, s3Nc!, startF%, endF%, fout$)
  35. PRINT : PRINT "Done!": PRINT
  36.  
  37. SUB quadratic (s1Nc!, s2Nc!, s3Nc!, startF%, endF%, fout$)
  38.  
  39.   OPEN fout$ FOR OUTPUT AS #1
  40.  
  41.   ' Declare variables
  42.   DIM frame%, t, i, format$
  43.   DIM N3x!, N3y!, temp$, tex!
  44.   DIM s1x!, s1y!, s2x!, s2y!, s3x!, s3y!
  45.   DIM s1N1x!, s1N1y!, s1N2x!, s1N2y!
  46.   DIM s2N1x!, s2N1y!, s2N2x!, s2N2y!
  47.   DIM s3N1x!, s3N1y!, s3N2x!, s3N2y!
  48.  
  49.   ' Write the file
  50.   PRINT #1, "// Scene File: QUAD.PI"
  51.   PRINT #1, "// Author: Rob McGregor"
  52.   PRINT #1, ""
  53.   PRINT #1, "/***************************************************"
  54.   PRINT #1, " Virtual animation of 3 spheres following different"
  55.   PRINT #1, " quadratic paths, each starting and ending at the"
  56.   PRINT #1, " same relative points on the y-axis."
  57.   PRINT #1, "****************************************************/"
  58.   PRINT #1, ""
  59.   PRINT #1, "// SET UP THE CAMERA"
  60.   PRINT #1, "viewpoint {"
  61.   PRINT #1, "  from        <4.5, 0, -11.5>"
  62.   PRINT #1, "  at          <4.5, 1, 0>"
  63.   PRINT #1, "  up          <0, 1, 0>"
  64.   PRINT #1, "  angle       45"
  65.   PRINT #1, "  resolution  320, 240"
  66.   PRINT #1, "  aspect      4/3"
  67.   PRINT #1, "}"
  68.   PRINT #1, ""
  69.   PRINT #1, "background Grey"
  70.   PRINT #1, ""
  71.   PRINT #1, "// Lights"
  72.   PRINT #1, "light <-5, 5, -20>"
  73.   PRINT #1, "light <5, 5, -20>"
  74.   PRINT #1, ""
  75.   
  76.   PRINT #1, "define s1color"
  77.   PRINT #1, "surface { "
  78.   PRINT #1, "  color      red"
  79.   PRINT #1, "  ambient    0.1"
  80.   PRINT #1, "  diffuse    0.2"
  81.   PRINT #1, "  specular   0.8"
  82.   PRINT #1, "  microfacet Cook 0.2"
  83.   PRINT #1, "  reflection 0.3 "
  84.   PRINT #1, "}"
  85.   PRINT #1, ""
  86.   
  87.   PRINT #1, "define s2color"
  88.   PRINT #1, "surface { "
  89.   PRINT #1, "  color      yellow"
  90.   PRINT #1, "  ambient    0.1"
  91.   PRINT #1, "  diffuse    0.2"
  92.   PRINT #1, "  specular   0.8"
  93.   PRINT #1, "  microfacet Cook 0.2"
  94.   PRINT #1, "  reflection 0.3 "
  95.   PRINT #1, "}"
  96.   PRINT #1, ""
  97.   
  98.   PRINT #1, "define s3color"
  99.   PRINT #1, "surface { "
  100.   PRINT #1, "  color      blue"
  101.   PRINT #1, "  ambient    0.1"
  102.   PRINT #1, "  diffuse    0.2"
  103.   PRINT #1, "  specular   0.8"
  104.   PRINT #1, "  microfacet Cook 0.2"
  105.   PRINT #1, "  reflection 0.3 "
  106.   PRINT #1, "}"
  107.   PRINT #1, ""
  108.  
  109.   FOR i% = startF% TO endF%
  110.  
  111.     ' compute t
  112.     frame% = i%
  113.     t = (frame% - startF%) / (endF% - startF%)
  114.  
  115.     ' set the parameters of the equation
  116.     s1N1x! = 0 ' x-axis starting point
  117.     s1N1y! = -1 ' y-axis starting point
  118.     s1N2x! = 9 ' x-axis ending point
  119.     s1N2y! = -1 ' y-axis ending point
  120.  
  121.     s2N1x! = 0 ' x-axis starting point
  122.     s2N1y! = 0 ' y-axis starting point
  123.     s2N2x! = 9 ' x-axis ending point
  124.     s2N2y! = 0 ' y-axis ending point
  125.  
  126.     s3N1x! = 0 ' x-axis starting point
  127.     s3N1y! = 1 ' y-axis starting point
  128.     s3N2x! = 9 ' x-axis ending point
  129.     s3N2y! = 1 ' y-axis ending point
  130.  
  131.     ' Now calculate the locations of the spheres using:
  132.     '   N = (1 - t)^2 * N1 + t^2 * N2 + ((2 * t) * (1 - t)) * Nc
  133.    
  134.     ' use variables to calculate only once
  135.     t2 = t ^ 2
  136.     lessT2 = (1 - t) ^ 2
  137.  
  138.     s1x! = lessT2 * s1N1x! + t2 * s1N2x! + 2 * t * (1 - t) * s1Nc!
  139.     s1y! = lessT2 * s1N1y! + t2 * s1N2y! + 2 * t * (1 - t) * s1Nc!
  140.     s2x! = lessT2 * s2N1x! + t2 * s2N2x! + 2 * t * (1 - t) * s2Nc!
  141.     s2y! = lessT2 * s2N1y! + t2 * s2N2y! + 2 * t * (1 - t) * s2Nc!
  142.     s3x! = lessT2 * s3N1x! + t2 * s3N2x! + 2 * t * (1 - t) * s3Nc!
  143.     s3y! = lessT2 * s3N1y! + t2 * s3N2y! + 2 * t * (1 - t) * s3Nc!
  144.     
  145.     ' calc the transparency of the spheres
  146.     tex! = (1 - t) * .9 + t * .4
  147.     format$ = "###.###"
  148.     
  149.     line$ = "define s1tex" + LTRIM$(STR$(frame%))
  150.     line$ = line$ + " texture { s1color { transmission white,"
  151.     PRINT #1, line$;
  152.     PRINT #1, USING format$; tex!;
  153.     PRINT #1, ", 1 }}"
  154.     
  155.     line$ = "define s2tex" + LTRIM$(STR$(frame%))
  156.     line$ = line$ + " texture { s2color { transmission white,"
  157.     PRINT #1, line$;
  158.     PRINT #1, USING format$; tex!;
  159.     PRINT #1, ", 1 }}"
  160.     
  161.     line$ = "define s3tex" + LTRIM$(STR$(frame%))
  162.     line$ = line$ + " texture { s3color { transmission white,"
  163.     PRINT #1, line$;
  164.     PRINT #1, USING format$; tex!;
  165.     PRINT #1, ", 1 }}"
  166.     
  167.     PRINT #1, "object { sphere <";
  168.     PRINT #1, USING format$; s1x!;
  169.     PRINT #1, ",";
  170.     PRINT #1, USING format$; s1y!;
  171.     PRINT #1, ", 0>, 0.5";
  172.     line$ = " s1tex" + LTRIM$(STR$(frame%)) + " }"
  173.     PRINT #1, line$
  174.     
  175.     PRINT #1, "object { sphere <";
  176.     PRINT #1, USING format$; s2x!;
  177.     PRINT #1, ",";
  178.     PRINT #1, USING format$; s2y!;
  179.     PRINT #1, ", 0>, 0.5";
  180.     line$ = " s2tex" + LTRIM$(STR$(frame%)) + " }"
  181.     PRINT #1, line$
  182.     
  183.     PRINT #1, "object { sphere <";
  184.     PRINT #1, USING format$; s3x!;
  185.     PRINT #1, ",";
  186.     PRINT #1, USING format$; s3y!;
  187.     PRINT #1, ", 0>, 0.5";
  188.     line$ = " s3tex" + LTRIM$(STR$(frame%)) + " }"
  189.     PRINT #1, line$
  190.     PRINT #1, ""
  191.  
  192.   NEXT i%
  193.   CLOSE #1
  194.  
  195. END SUB
  196.  
  197.