home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / audio / drive / Scenery.c++ < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  4.1 KB  |  194 lines

  1. /*
  2.  * Copyright 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. #include "Scenery.h"
  18. #include "Render.h"
  19. #include "MiscMath.h"
  20.  
  21.  
  22. typedef struct Mountain
  23. {
  24.     float left[3], peak[3], right[3];
  25.     float color[3];
  26. };
  27.  
  28. static void init_mountains(Mountain m[], int num, float dist)
  29. {
  30.     float angle = 0.0;
  31.     srand48(0);
  32.  
  33.     for (int i = 0; i < num; i++)
  34.     {
  35.         angle += 2.0*M_PI/(float)num;
  36.         float peak_angle = angle + drand48()/20.0;
  37.         float left_angle = peak_angle - 4.0*M_PI/(float)num + drand48()/10.0;
  38.         float right_angle = peak_angle + 4.0*M_PI/(float)num + drand48()/10.0;
  39.         
  40.         m[i].peak[0] = dist*fcos(peak_angle);
  41.         m[i].peak[2] = dist*fsin(peak_angle);
  42.         
  43.         m[i].left[0] = dist*fcos(left_angle);
  44.         m[i].left[2] = dist*fsin(left_angle);
  45.         
  46.         m[i].right[0] = dist*fcos(right_angle);
  47.         m[i].right[2] = dist*fsin(right_angle);
  48.  
  49.         m[i].peak[1] = 400.0 + 100.0*drand48();
  50.         m[i].left[1] = m[i].right[1] = -800.0;
  51.  
  52.         // random shades of dark green
  53.         m[i].color[0] = drand48()/5.0;
  54.         m[i].color[1] = .4 + drand48()/10.0;
  55.         m[i].color[2] = drand48()/5.0;
  56.     }
  57. }
  58.  
  59. void draw_mountain(Mountain *mountain, int i)
  60. {                            
  61.     c3f(mountain[i].color);        
  62.                                 
  63.     bgnpolygon();                
  64.         v3f(mountain[i].right);    
  65.         v3f(mountain[i].peak);    
  66.         v3f(mountain[i].left);    
  67.     endpolygon();                
  68. }
  69.  
  70.  
  71. void draw_mountains(float yaw, float dist, float fovx)
  72. {
  73.     const int NUM_MOUNTAINS = 30;
  74.     static Mountain *mountain = NULL;
  75.  
  76.     if (mountain == NULL)
  77.     {
  78.         mountain = new Mountain[NUM_MOUNTAINS];
  79.         init_mountains(mountain, NUM_MOUNTAINS, dist);
  80.     }
  81.     
  82.     long last_zbuffer = getzbuffer();
  83.  
  84.     if (yaw < 0)
  85.         yaw += 2.0*M_PI;
  86.         
  87.     // turn yaw into mountain angle
  88.     // mountains are 0 at +x, yaw is 0 at -z
  89.     float angle = yaw - M_PI/2.0;
  90.  
  91.     float mountains_per_radian = (float)NUM_MOUNTAINS/(2.0*M_PI);
  92.     
  93.     int start = (int)((angle - fovx/2.0)*mountains_per_radian);
  94.     int end = (int)((angle + fovx/2.0)*mountains_per_radian);
  95.  
  96.     // for safety's sake
  97.     start -= 2;
  98.     end += 2;
  99.  
  100.     if (start < 0)
  101.         start += NUM_MOUNTAINS;
  102.     if (end < 0)
  103.         end += NUM_MOUNTAINS;
  104.  
  105.     int i;
  106.     
  107.     if (start < end)
  108.         for (i = start; i <= end; i++)
  109.             draw_mountain(mountain,i);
  110.     else
  111.     {
  112.         for (i = start; i < NUM_MOUNTAINS; i++)
  113.             draw_mountain(mountain,i);
  114.         for (i = 0; i <= end; i++)
  115.             draw_mountain(mountain,i);
  116.     }
  117.  
  118.     zbuffer(last_zbuffer);
  119. }
  120.  
  121.  
  122. void draw_tree(int display_mode)
  123. {
  124.     if (display_mode == RENDER_FILLED)
  125.         lmbind(MATERIAL,WOOD);
  126.     else
  127.         cpack(0x4A75); // .46 .29 0.0 XXX
  128.  
  129.     pushmatrix();
  130.         translate(0.0,12.0,0.0);
  131.         scale(1.5,25.0,1.5);
  132.         rotate(900,'x');
  133.         if (display_mode == RENDER_FILLED)
  134.             fcylinder();
  135.         else
  136.             wcylinder();
  137.     popmatrix();
  138.  
  139.     
  140.     if (display_mode == RENDER_FILLED)
  141.         lmbind(MATERIAL,LEAVES);
  142.     else
  143.         cpack(0xFF10);
  144.  
  145.     pushmatrix();
  146.         translate(0.0,32.0,0.0);
  147.         scale(10.0,14.0,10.0);
  148.         if (display_mode == RENDER_FILLED)
  149.             fsphere();
  150.         else
  151.             wsphere();
  152.     popmatrix();
  153. }
  154.  
  155.  
  156. void draw_pole(int display_mode)
  157. {
  158.     if (display_mode == RENDER_FILLED)
  159.         lmbind(MATERIAL,WOOD);
  160.     else
  161.         cpack(0x4A75); // .46 .29 0.0 XXX
  162.             
  163.     pushmatrix();
  164.         translate(0.0,14.0,0.0);
  165.         scale(0.5,30.0,0.5);
  166.         rotate(900,'x');
  167.         if (display_mode == RENDER_FILLED)
  168.             fcylinder();
  169.         else
  170.             wcylinder();
  171.     popmatrix();
  172.  
  173.     pushmatrix();
  174.         translate(0.0,28.0,-1.0);
  175.         scale(10.0,0.4,0.2);
  176.         if (display_mode == RENDER_FILLED)
  177.             fcube();
  178.         else
  179.             wcube();
  180.     popmatrix();
  181.         
  182.     pushmatrix();
  183.         translate(0.0,25.0,-1.0);
  184.         scale(10.0,0.4,0.2);
  185.         if (display_mode == RENDER_FILLED)
  186.             fcube();
  187.         else
  188.             wcube();
  189.     popmatrix();
  190.  
  191.     if (display_mode == RENDER_FILLED)
  192.         lmbind(MATERIAL,0);
  193. }
  194.