home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / GL / curve / whiz.c < prev   
Encoding:
C/C++ Source or Header  |  1994-08-02  |  5.2 KB  |  237 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *
  19.  * whiz.c
  20.  *
  21.  * Part of the "Curve Demo"
  22.  * by Howard Look for Silicon Graphics
  23.  *
  24.  * June, 1989
  25.  *
  26.  * This file contains routines for "whizbangs" added to the curve
  27.  * demo. A whizbang (genus "widgets") is a neat shape defined using
  28.  * only cubic splines.
  29.  * 
  30.  */
  31.  
  32. #include <stdio.h>
  33. #include <gl.h>
  34. #include <device.h>
  35. #include <math.h>
  36. #include "event.h"
  37. #include "curve.h"
  38.  
  39.  
  40. void create_whizbang(int);
  41. void create_coil(void);
  42. void create_bedspring(void);
  43. void create_doughnut(void);
  44. extern float generate_random_float(float);
  45. extern void remake_all_menus(void);
  46.  
  47. extern Coord geometry[MAX_MARKERS][3];
  48. extern Coord offset[MAX_MARKERS][3];
  49. extern int current_markers;
  50. extern int origin_x, origin_y, size_x, size_y;
  51.  
  52.  
  53. /* Environment variables */
  54. extern 
  55. int        curve_basis, 
  56.         display_mode, 
  57.         curve_precision,
  58.         speed, 
  59.         line_style, 
  60.         whizbang;
  61. extern
  62. Boolean    markers,
  63.         hulls,
  64.         motion, 
  65.         smear;
  66. extern
  67. Boolean    draw_active;
  68.  
  69. extern
  70. int        hardware;
  71.  
  72. /*
  73.  * Dispatches a call from the whizbang menu to a routine that replaces
  74.  * the current curve geometry with a whizbang geometry.
  75.  */
  76. void create_whizbang(int which_whiz)
  77. {
  78.     switch(which_whiz)
  79.     {
  80.         case COIL:
  81.             create_coil();
  82.             break;
  83.         case BEDSPRING:
  84.             create_bedspring();
  85.             break;
  86.         case DOUGHNUT:
  87.             create_doughnut();
  88.             break;
  89.     }
  90. }
  91.  
  92.  
  93. /*
  94.  * Replaces the current curve geometry with a constant radius coil.
  95.  * All offsets are the same, so the coil retains its shape when set in
  96.  * motion.
  97.  * Sets environment variables and rebuilds menus.
  98.  */
  99. void create_coil(void)
  100. {
  101.     float i, ox, oy, oz;
  102.  
  103.     current_markers = 0;
  104.     
  105.     ox = (generate_random_float(2.0) - 1.0)/(float)size_y;
  106.     oy = (generate_random_float(2.0) - 1.0)/(float)size_y;
  107.     oz = (generate_random_float(2.0) - 1.0)/(float)size_y;
  108.  
  109.     for(i = -0.75; i < 0.75; i += 0.02)
  110.     {
  111.         geometry[current_markers][0] = 0.5*fcos(20.0*i);
  112.         geometry[current_markers][1] = i;
  113.         geometry[current_markers][2] = 0.5*fsin(20.0*i);
  114.  
  115.         offset[current_markers][0] = ox;
  116.         offset[current_markers][1] = oy;
  117.         offset[current_markers][2] = oz;
  118.  
  119.         current_markers++;
  120.     }
  121.  
  122.     curve_basis = BSPLINE;
  123.     display_mode = THREE_D;
  124.     curve_precision = MIN_PRECISION;
  125.     if (hardware == GT)
  126.         speed = MIN_SPEED;
  127.     else
  128.         speed = MAX_SPEED;
  129.     line_style = SOLID;
  130.     markers = OFF;
  131.     motion = ON;
  132.     hulls = OFF;
  133.     smear = OFF;
  134.     frontbuffer(FALSE);    
  135.     draw_active = TRUE;
  136.  
  137.     remake_all_menus();
  138. }
  139.  
  140.  
  141. /*
  142.  * Replaces the current geometry with a varying radius spiral that
  143.  * resembles a standard Sealy bedspring.
  144.  * Sets environment variables and rebuilds menus.
  145.  */
  146. void create_bedspring(void)
  147. {
  148.     float i, ox, oy, oz;
  149.  
  150.     current_markers = 0;
  151.     
  152.     ox = (generate_random_float(2.0) - 1.0)/(float)size_y;
  153.     oy = (generate_random_float(2.0) - 1.0)/(float)size_y;
  154.     oz = (generate_random_float(2.0) - 1.0)/(float)size_y;
  155.  
  156.     for(i = -0.75; i < 0.75; i += 0.02)
  157.     {
  158.         geometry[current_markers][0] = i*fcos(40.0*i);
  159.         geometry[current_markers][1] = i;
  160.         geometry[current_markers][2] = i*fsin(40.0*i);
  161.  
  162.         offset[current_markers][0] = ox;
  163.         offset[current_markers][1] = oy;
  164.         offset[current_markers][2] = oz;
  165.  
  166.         current_markers++;
  167.     }
  168.  
  169.     curve_basis = BSPLINE;
  170.     display_mode = THREE_D;
  171.     curve_precision = MIN_PRECISION;
  172.     if (hardware == GT)
  173.         speed = MIN_SPEED;
  174.     else
  175.         speed = MAX_SPEED;
  176.     line_style = SOLID;
  177.     markers = OFF;
  178.     motion = ON;
  179.     hulls = OFF;
  180.     smear = OFF;
  181.     frontbuffer(FALSE);    
  182.     draw_active = TRUE;
  183.  
  184.     remake_all_menus();
  185. }
  186.  
  187.  
  188. /*
  189.  * Replaces the current curve geometry with a doughnut (toroid).
  190.  * Sets environment variables and rebuilds menus.
  191.  */
  192. void create_doughnut(void)
  193. {
  194.     float theta = 0.0, x, y, z, ox, oy, oz;
  195.  
  196.     ox = (generate_random_float(2.0) - 1.0)/(float)size_y;
  197.     oy = (generate_random_float(2.0) - 1.0)/(float)size_y;
  198.     oz = (generate_random_float(2.0) - 1.0)/(float)size_y;
  199.  
  200.     current_markers = 0;
  201.     
  202.     while (theta <= 2.0*PI + 6.0*PI/300.0)
  203.     {
  204.         x = 0.6*fcos(theta) + 0.3*fsin(50.0*theta)*fcos(theta);
  205.         y = 0.3*fcos(50.0*theta);
  206.         z = 0.6*fsin(theta) + 0.3*fsin(50.0*theta)*fsin(theta);
  207.  
  208.         geometry[current_markers][0] = x;
  209.         geometry[current_markers][1] = y;
  210.         geometry[current_markers][2] = z;
  211.  
  212.         offset[current_markers][0] = ox;
  213.         offset[current_markers][1] = oy;
  214.         offset[current_markers][2] = oz;
  215.  
  216.         theta += 2.0*PI/300.0;
  217.         current_markers++;
  218.     }
  219.  
  220.     curve_basis = BSPLINE;
  221.     display_mode = THREE_D;
  222.     curve_precision = MIN_PRECISION;
  223.     if (hardware == GT)
  224.         speed = MIN_SPEED;
  225.     else
  226.         speed = MAX_SPEED;
  227.     line_style = SOLID;
  228.     markers = OFF;
  229.     motion = ON;
  230.     hulls = OFF;
  231.     smear = OFF;
  232.     frontbuffer(FALSE);
  233.     draw_active = TRUE;
  234.     
  235.     remake_all_menus();
  236. }
  237.