home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / opengl / siggraphCD / lib / libtk / shapes.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  14.0 KB  |  492 lines

  1. /*
  2.  * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED
  4.  * Permission to use, copy, modify, and distribute this software for
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  *
  25.  * US Government Users Restricted Rights
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36.  */
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <math.h>
  41. #include "tk.h"
  42. #include "private.h"
  43.  
  44. /******************************************************************************/
  45.  
  46. #define PI 3.14159265358979323846
  47.  
  48. /******************************************************************************/
  49.  
  50. void tkWireSphere(GLuint base, float radius)
  51. {
  52.     GLUquadricObj *quadObj;
  53.  
  54.     glNewList(base, GL_COMPILE_AND_EXECUTE);
  55.     quadObj = gluNewQuadric();
  56.     gluQuadricDrawStyle(quadObj, GLU_LINE);
  57.     gluSphere(quadObj, radius, 16, 16);
  58.     glEndList();
  59. }
  60.  
  61. /******************************************************************************/
  62.  
  63. void tkSolidSphere(GLuint base, float radius)
  64. {
  65.     GLUquadricObj *quadObj;
  66.  
  67.     glNewList(base, GL_COMPILE_AND_EXECUTE);
  68.     quadObj = gluNewQuadric();
  69.     gluQuadricDrawStyle(quadObj, GLU_FILL);
  70.     gluQuadricNormals(quadObj, GLU_SMOOTH);
  71.     gluSphere(quadObj, radius, 16, 16);
  72.     glEndList();
  73. }
  74.  
  75. /******************************************************************************/
  76.  
  77. void tkWireCube(GLuint base, float size)
  78. {
  79.     static float n[6][3] = {
  80.     {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},
  81.     {0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0}
  82.     };
  83.     static GLint faces[6][4] = {
  84.     {0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},
  85.     {4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3}
  86.     };
  87.     float x0, x1, y0, y1, z0, z1, tmp;
  88.     float v[8][3];
  89.     int i;
  90.  
  91.     x0 = -size / 2.0;
  92.     x1 = size / 2.0;
  93.     y0 = -size / 2.0;
  94.     y1 = size / 2.0;
  95.     z0 = -size / 2.0;
  96.     z1 = size / 2.0;
  97.  
  98.     if (x0 > x1) {
  99.     tmp = x0; x0 = x1; x1 = tmp;
  100.     }
  101.     if (y0 > y1) {
  102.     tmp = y0; y0 = y1; y1 = tmp; 
  103.     }
  104.     if (z0 > z1) {
  105.     tmp = z0; z0 = z1; z1 = tmp; 
  106.     }
  107.     v[0][0] = v[1][0] = v[2][0] = v[3][0] = x0;
  108.     v[4][0] = v[5][0] = v[6][0] = v[7][0] = x1;
  109.     v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0;
  110.     v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1;
  111.     v[0][2] = v[3][2] = v[4][2] = v[7][2] = z0;
  112.     v[1][2] = v[2][2] = v[5][2] = v[6][2] = z1;
  113.  
  114.     glNewList(base, GL_COMPILE_AND_EXECUTE);
  115.     for (i = 0; i < 6; i++) {
  116.         glBegin(GL_LINE_LOOP);
  117.         glNormal3fv(&n[i][0]);
  118.         glVertex3fv(&v[faces[i][0]][0]);
  119.         glNormal3fv(&n[i][0]);
  120.         glVertex3fv(&v[faces[i][1]][0]);
  121.         glNormal3fv(&n[i][0]);
  122.         glVertex3fv(&v[faces[i][2]][0]);
  123.         glNormal3fv(&n[i][0]);
  124.         glVertex3fv(&v[faces[i][3]][0]);
  125.         glEnd();
  126.     }
  127.     glEndList();
  128. }
  129.  
  130. /******************************************************************************/
  131.  
  132. void tkSolidCube(GLuint base, float size)
  133. {
  134.     static float n[6][3] = {
  135.     {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},
  136.     {0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0}
  137.     };
  138.     static GLint faces[6][4] = {
  139.     {0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},
  140.     {4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3}
  141.     };
  142.     float x0, x1, y0, y1, z0, z1, tmp;
  143.     float v[8][3];
  144.     int i;
  145.  
  146.     x0 = -size / 2.0;
  147.     x1 = size / 2.0;
  148.     y0 = -size / 2.0;
  149.     y1 = size / 2.0;
  150.     z0 = -size / 2.0;
  151.     z1 = size / 2.0;
  152.  
  153.     if (x0 > x1) {
  154.     tmp = x0; x0 = x1; x1 = tmp;
  155.     }
  156.     if (y0 > y1) {
  157.     tmp = y0; y0 = y1; y1 = tmp; 
  158.     }
  159.     if (z0 > z1) {
  160.     tmp = z0; z0 = z1; z1 = tmp; 
  161.     }
  162.     v[0][0] = v[1][0] = v[2][0] = v[3][0] = x0;
  163.     v[4][0] = v[5][0] = v[6][0] = v[7][0] = x1;
  164.     v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0;
  165.     v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1;
  166.     v[0][2] = v[3][2] = v[4][2] = v[7][2] = z0;
  167.     v[1][2] = v[2][2] = v[5][2] = v[6][2] = z1;
  168.  
  169.     glNewList(base, GL_COMPILE_AND_EXECUTE);
  170.     for (i = 0; i < 6; i++) {
  171.         glBegin(GL_POLYGON);
  172.         glNormal3fv(&n[i][0]);
  173.         glVertex3fv(&v[faces[i][0]][0]);
  174.         glNormal3fv(&n[i][0]);
  175.         glVertex3fv(&v[faces[i][1]][0]);
  176.         glNormal3fv(&n[i][0]);
  177.         glVertex3fv(&v[faces[i][2]][0]);
  178.         glNormal3fv(&n[i][0]);
  179.         glVertex3fv(&v[faces[i][3]][0]);
  180.         glEnd();
  181.     }
  182.     glEndList();
  183. }
  184.  
  185. /******************************************************************************/
  186.  
  187. void tkWireBox(GLuint base, float width, float height, float depth)
  188. {
  189.     static float n[6][3] = {
  190.     {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},
  191.     {0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0}
  192.     };
  193.     static GLint faces[6][4] = {
  194.     {0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},
  195.     {4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3}
  196.     };
  197.     float x0, x1, y0, y1, z0, z1, tmp;
  198.     float v[8][3];
  199.     int i;
  200.  
  201.     x0 = -width / 2.0;
  202.     x1 = width / 2.0;
  203.     y0 = -height / 2.0;
  204.     y1 = height / 2.0;
  205.     z0 = -depth / 2.0;
  206.     z1 = depth / 2.0;
  207.  
  208.     if (x0 > x1) {
  209.     tmp = x0; x0 = x1; x1 = tmp;
  210.     }
  211.     if (y0 > y1) {
  212.     tmp = y0; y0 = y1; y1 = tmp; 
  213.     }
  214.     if (z0 > z1) {
  215.     tmp = z0; z0 = z1; z1 = tmp; 
  216.     }
  217.     v[0][0] = v[1][0] = v[2][0] = v[3][0] = x0;
  218.     v[4][0] = v[5][0] = v[6][0] = v[7][0] = x1;
  219.     v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0;
  220.     v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1;
  221.     v[0][2] = v[3][2] = v[4][2] = v[7][2] = z0;
  222.     v[1][2] = v[2][2] = v[5][2] = v[6][2] = z1;
  223.  
  224.     glNewList(base, GL_COMPILE_AND_EXECUTE);
  225.     for (i = 0; i < 6; i++) {
  226.         glBegin(GL_LINE_LOOP);
  227.         glNormal3fv(&n[i][0]);
  228.         glVertex3fv(&v[faces[i][0]][0]);
  229.         glNormal3fv(&n[i][0]);
  230.         glVertex3fv(&v[faces[i][1]][0]);
  231.         glNormal3fv(&n[i][0]);
  232.         glVertex3fv(&v[faces[i][2]][0]);
  233.         glNormal3fv(&n[i][0]);
  234.         glVertex3fv(&v[faces[i][3]][0]);
  235.         glEnd();
  236.     }
  237.     glEndList();
  238. }
  239.  
  240. /******************************************************************************/
  241.  
  242. void tkSolidBox(GLuint base, float width, float height, float depth)
  243. {
  244.     static float n[6][3] = {
  245.     {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},
  246.     {0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0}
  247.     };
  248.     static GLint faces[6][4] = {
  249.     {0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},
  250.     {4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3}
  251.     };
  252.     float x0, x1, y0, y1, z0, z1, tmp;
  253.     float v[8][3];
  254.     int i;
  255.  
  256.     x0 = -width / 2.0;
  257.     x1 = width / 2.0;
  258.     y0 = -height / 2.0;
  259.     y1 = height / 2.0;
  260.     z0 = -depth / 2.0;
  261.     z1 = depth / 2.0;
  262.  
  263.     if (x0 > x1) {
  264.     tmp = x0; x0 = x1; x1 = tmp;
  265.     }
  266.     if (y0 > y1) {
  267.     tmp = y0; y0 = y1; y1 = tmp; 
  268.     }
  269.     if (z0 > z1) {
  270.     tmp = z0; z0 = z1; z1 = tmp; 
  271.     }
  272.     v[0][0] = v[1][0] = v[2][0] = v[3][0] = x0;
  273.     v[4][0] = v[5][0] = v[6][0] = v[7][0] = x1;
  274.     v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0;
  275.     v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1;
  276.     v[0][2] = v[3][2] = v[4][2] = v[7][2] = z0;
  277.     v[1][2] = v[2][2] = v[5][2] = v[6][2] = z1;
  278.  
  279.     glNewList(base, GL_COMPILE_AND_EXECUTE);
  280.     for (i = 0; i < 6; i++) {
  281.         glBegin(GL_POLYGON);
  282.         glNormal3fv(&n[i][0]);
  283.         glVertex3fv(&v[faces[i][0]][0]);
  284.         glNormal3fv(&n[i][0]);
  285.         glVertex3fv(&v[faces[i][1]][0]);
  286.         glNormal3fv(&n[i][0]);
  287.         glVertex3fv(&v[faces[i][2]][0]);
  288.         glNormal3fv(&n[i][0]);
  289.         glVertex3fv(&v[faces[i][3]][0]);
  290.         glEnd();
  291.     }
  292.     glEndList();
  293. }
  294.  
  295. /******************************************************************************/
  296.  
  297. void tkWireTorus(GLuint base, float innerRadius, float outerRadius)
  298. {
  299.     GLint i, j;
  300.     float theta1, phi1, theta2, phi2, rings, sides;
  301.     float v0[03], v1[3], v2[3], v3[3];
  302.     float n0[3], n1[3], n2[3], n3[3];
  303.  
  304.     rings = 5;
  305.     sides = 10;
  306.  
  307.     glNewList(base, GL_COMPILE_AND_EXECUTE);
  308.     for (i = 0; i < rings; i++) {
  309.     theta1 = (float)i * 2.0 * PI / rings;
  310.     theta2 = (float)(i + 1) * 2.0 * PI / rings;
  311.     for (j = 0; j < sides; j++) {
  312.         phi1 = (float)j * 2.0 * PI / sides;
  313.         phi2 = (float)(j + 1) * 2.0 * PI / sides;
  314.  
  315.         v0[0] = cos(theta1) * (outerRadius + innerRadius * cos(phi1));
  316.         v0[1] = -sin(theta1) * (outerRadius + innerRadius * cos(phi1));
  317.         v0[2] = innerRadius * sin(phi1);
  318.  
  319.         v1[0] = cos(theta2) * (outerRadius + innerRadius * cos(phi1));
  320.         v1[1] = -sin(theta2) * (outerRadius + innerRadius * cos(phi1));
  321.         v1[2] = innerRadius * sin(phi1);
  322.  
  323.         v2[0] = cos(theta2) * (outerRadius + innerRadius * cos(phi2));
  324.         v2[1] = -sin(theta2) * (outerRadius + innerRadius * cos(phi2));
  325.         v2[2] = innerRadius * sin(phi2);
  326.  
  327.         v3[0] = cos(theta1) * (outerRadius + innerRadius * cos(phi2));
  328.         v3[1] = -sin(theta1) * (outerRadius + innerRadius * cos(phi2));
  329.         v3[2] = innerRadius * sin(phi2);
  330.  
  331.         n0[0] = cos(theta1) * (cos(phi1));
  332.         n0[1] = -sin(theta1) * (cos(phi1));
  333.         n0[2] = sin(phi1);
  334.  
  335.         n1[0] = cos(theta2) * (cos(phi1));
  336.         n1[1] = -sin(theta2) * (cos(phi1));
  337.         n1[2] = sin(phi1);
  338.  
  339.         n2[0] = cos(theta2) * (cos(phi2));
  340.         n2[1] = -sin(theta2) * (cos(phi2));
  341.         n2[2] = sin(phi2);
  342.  
  343.         n3[0] = cos(theta1) * (cos(phi2));
  344.         n3[1] = -sin(theta1) * (cos(phi2));
  345.         n3[2] = sin(phi2);
  346.  
  347.         glBegin(GL_LINE_LOOP);
  348.         glNormal3fv(n3);
  349.         glVertex3fv(v3);
  350.         glNormal3fv(n2);
  351.         glVertex3fv(v2);
  352.         glNormal3fv(n1);
  353.         glVertex3fv(v1);
  354.         glNormal3fv(n0);
  355.         glVertex3fv(v0);
  356.         glEnd();
  357.     }
  358.     }
  359.     glEndList();
  360. }
  361.  
  362. /******************************************************************************/
  363.  
  364. void tkSolidTorus(GLuint base, float innerRadius, float outerRadius)
  365. {
  366.     GLint i, j;
  367.     float theta1, phi1, theta2, phi2, rings, sides;
  368.     float v0[03], v1[3], v2[3], v3[3];
  369.     float n0[3], n1[3], n2[3], n3[3];
  370.  
  371.     rings = 5;
  372.     sides = 10;
  373.  
  374.     glNewList(base, GL_COMPILE_AND_EXECUTE);
  375.     for (i = 0; i < rings; i++) {
  376.     theta1 = (float)i * 2.0 * PI / rings;
  377.     theta2 = (float)(i + 1) * 2.0 * PI / rings;
  378.     for (j = 0; j < sides; j++) {
  379.         phi1 = (float)j * 2.0 * PI / sides;
  380.         phi2 = (float)(j + 1) * 2.0 * PI / sides;
  381.  
  382.         v0[0] = cos(theta1) * (outerRadius + innerRadius * cos(phi1));
  383.         v0[1] = -sin(theta1) * (outerRadius + innerRadius * cos(phi1));
  384.         v0[2] = innerRadius * sin(phi1);
  385.  
  386.         v1[0] = cos(theta2) * (outerRadius + innerRadius * cos(phi1));
  387.         v1[1] = -sin(theta2) * (outerRadius + innerRadius * cos(phi1));
  388.         v1[2] = innerRadius * sin(phi1);
  389.  
  390.         v2[0] = cos(theta2) * (outerRadius + innerRadius * cos(phi2));
  391.         v2[1] = -sin(theta2) * (outerRadius + innerRadius * cos(phi2));
  392.         v2[2] = innerRadius * sin(phi2);
  393.  
  394.         v3[0] = cos(theta1) * (outerRadius + innerRadius * cos(phi2));
  395.         v3[1] = -sin(theta1) * (outerRadius + innerRadius * cos(phi2));
  396.         v3[2] = innerRadius * sin(phi2);
  397.  
  398.         n0[0] = cos(theta1) * (cos(phi1));
  399.         n0[1] = -sin(theta1) * (cos(phi1));
  400.         n0[2] = sin(phi1);
  401.  
  402.         n1[0] = cos(theta2) * (cos(phi1));
  403.         n1[1] = -sin(theta2) * (cos(phi1));
  404.         n1[2] = sin(phi1);
  405.  
  406.         n2[0] = cos(theta2) * (cos(phi2));
  407.         n2[1] = -sin(theta2) * (cos(phi2));
  408.         n2[2] = sin(phi2);
  409.  
  410.         n3[0] = cos(theta1) * (cos(phi2));
  411.         n3[1] = -sin(theta1) * (cos(phi2));
  412.         n3[2] = sin(phi2);
  413.  
  414.         glBegin(GL_POLYGON);
  415.         glNormal3fv(n3);
  416.         glVertex3fv(v3);
  417.         glNormal3fv(n2);
  418.         glVertex3fv(v2);
  419.         glNormal3fv(n1);
  420.         glVertex3fv(v1);
  421.         glNormal3fv(n0);
  422.         glVertex3fv(v0);
  423.         glEnd();
  424.     }
  425.     }
  426.     glEndList();
  427. }
  428.  
  429. /******************************************************************************/
  430.  
  431. void tkWireCylinder(GLuint base, float radius, float height)
  432. {
  433.     GLUquadricObj *quadObj;
  434.  
  435.     glNewList(base, GL_COMPILE_AND_EXECUTE);
  436.     glPushMatrix();
  437.     glRotatef(90.0, 1.0, 0.0, 0.0);
  438.     glTranslatef(0.0, 0.0, -1.0);
  439.     quadObj = gluNewQuadric();
  440.     gluQuadricDrawStyle(quadObj, GLU_LINE);
  441.     gluCylinder(quadObj, radius, radius, height, 12, 2);
  442.     glPopMatrix();
  443.     glEndList();
  444. }
  445.  
  446. /******************************************************************************/
  447.  
  448. void tkSolidCylinder(GLuint base, float radius, float height)
  449. {
  450.     GLUquadricObj *quadObj;
  451.  
  452.     glNewList(base, GL_COMPILE_AND_EXECUTE);
  453.     glPushMatrix();
  454.     glRotatef(90.0, 1.0, 0.0, 0.0);
  455.     glTranslatef(0.0, 0.0, -1.0);
  456.     quadObj = gluNewQuadric();
  457.     gluQuadricDrawStyle(quadObj, GLU_FILL);
  458.     gluQuadricNormals(quadObj, GLU_SMOOTH);
  459.     gluCylinder(quadObj, radius, radius, height, 12, 2);
  460.     glPopMatrix();
  461.     glEndList();
  462. }
  463.  
  464. /******************************************************************************/
  465.  
  466. void tkWireCone(GLuint base, float b, float h)
  467. {
  468.     GLUquadricObj *quadObj;
  469.  
  470.     glNewList(base, GL_COMPILE_AND_EXECUTE);
  471.     quadObj = gluNewQuadric();
  472.     gluQuadricDrawStyle(quadObj, GLU_LINE);
  473.     gluCylinder(quadObj, b, 0.0, h, 15, 10);
  474.     glEndList();
  475. }
  476.  
  477. /******************************************************************************/
  478.  
  479. void tkSolidCone(GLuint base, float b, float h)
  480. {
  481.     GLUquadricObj *quadObj;
  482.  
  483.     glNewList(base, GL_COMPILE_AND_EXECUTE);
  484.     quadObj = gluNewQuadric();
  485.     gluQuadricDrawStyle(quadObj, GLU_FILL);
  486.     gluQuadricNormals(quadObj, GLU_SMOOTH);
  487.     gluCylinder(quadObj, b, 0.0, h, 15, 10);
  488.     glEndList();
  489. }
  490.  
  491. /******************************************************************************/
  492.