home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1992, 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- /* Tom Davis -- 1992 */
-
- /* lissajous figures */
-
- #include <stdio.h>
- #include <math.h>
- #include <gl.h>
- #include <device.h>
- #include "3d.h"
-
-
- long th=4;
- long ph=5;
- float theta, phi;
-
- long STEPS;
- #define PI 3.14159265358
-
- long lcm = 60;
-
- long gcd(x, y)
- {
- if (x % y == 0) return y;
- return gcd( y, x % y);
- }
-
- void errf(char *s)
- {
- /*fprintf(stderr, "%s\n", s);*/
- return;
- }
-
- float x(float t)
- {
- if (t == 1.0) t = 0.0; /* guarantees wrap */
- return sin(theta*t)*cos(phi*t);
- }
-
- float xx(float t)
- {
- if (t == 1.0) t = 0.0; /* guarantees wrap */
- return theta*cos(theta*t)*cos(phi*t)-phi*sin(phi*t)*sin(theta*t);
- }
-
- float xxx(float t)
- {
- if (t == 1.0) t = 0.0; /* guarantees wrap */
- return -(theta*theta+phi*phi)*sin(theta*t)*cos(phi*t)
- -2*phi*theta*sin(phi*t)*cos(theta*t);
- }
-
- float y(float t)
- {
- if (t == 1.0) t = 0.0; /* guarantees wrap */
- return cos(theta*t);
- }
-
- float yy(float t)
- {
- if (t == 1.0) t = 0.0; /* guarantees wrap */
- return -theta*sin(theta*t);
- }
-
- float yyy(float t)
- {
- if (t == 1.0) t = 0.0; /* guarantees wrap */
- return -theta*theta*cos(theta*t);
- }
-
- float z(float t)
- {
- if (t == 1.0) t = 0.0; /* guarantees wrap */
- return sin(theta*t)*sin(phi*t);
- }
-
- float zz(float t)
- {
- if (t == 1.0) t = 0.0; /* guarantees wrap */
- return theta*cos(theta*t)*sin(phi*t) + phi*sin(theta*t)*cos(phi*t);
- }
-
- float zzz(float t)
- {
- if (t == 1.0) t = 0.0; /* guarantees wrap */
- return -(theta*theta+phi*phi)*sin(theta*t)*sin(phi*t)
- +theta*phi*cos(theta*t)*cos(phi*t);
- }
-
- float blackvec[3] = {0.0, 0.0, 0.0};
- float whitevec[3] = {1.0, 1.0, 1.0};
- static float idmat[4][4] = {1.0,0.0,0.0,0.0,
- 0.0,1.0,0.0,0.0,
- 0.0,0.0,1.0,0.0,
- 0.0,0.0,0.0,1.0};
-
- float shiny_material[] = {SPECULAR, 0.8, 0.8, 0.8,
- DIFFUSE, 0.4, 0.4, 0.4,
- SHININESS, 30.0,
- LMNULL};
-
- float blue_light[] = {LCOLOR, 0.0, 0.0, 0.6,
- POSITION, 0.0, 1.0, 0.0, 0.0,
- LMNULL};
-
- /*
- ** Tell the Graphics Library to DEFINE a simple lighting model
- ** that accounts for diffuse and ambient reflection. This simple
- ** lighting model happens to be the default lighting model for
- ** the Graphics Library.
- */
- void def_simple_light_model()
-
- {
- lmdef(DEFLMODEL, 1, 0, NULL);
- lmdef(DEFMATERIAL, 1, 11, shiny_material);
- lmdef(DEFLIGHT, 1, 0, NULL);
- lmdef(DEFLIGHT, 2, 10, blue_light);
- }
-
- /*
- ** Tell the Graphics Library to USE the simple lighting model
- ** that we defined earlier.
- */
- use_simple_light_model()
-
- {
- lmbind(LMODEL, 1);
- lmbind(LIGHT0, 1);
- lmbind(LIGHT1, 2);
- }
-
- /* Here's an idiotic display list. Just save the vectors and
- * normals in a giant list, and spew them out in order when the
- * draw command comes.
- */
-
- float vectlist[200000][3];
- float normlist[200000][3];
- long vcount;
-
- float trilist[100000][3];
- float trinorm[100000][3];
- long tcount;
-
- void initdisplaylist()
- {
- tcount = vcount = 0;
- }
-
- /* ... and the corresponding idiotic savefunc */
-
- void savefunc(type, n0, v0, n1, v1, n2, v2, n3, v3)
- long type;
- float *n0, *n1, *n2, *n3, *v0, *v1, *v2, *v3;
- {
- if (type == ADD_QUAD) {
- if (vcount > 200000) {
- fprintf(stderr, "too many points\n");
- exit();
- }
- copy3(n0, &normlist[vcount][0]);
- copy3(v0, &vectlist[vcount++][0]);
- copy3(n1, &normlist[vcount][0]);
- copy3(v1, &vectlist[vcount++][0]);
- copy3(n2, &normlist[vcount][0]);
- copy3(v2, &vectlist[vcount++][0]);
- copy3(n3, &normlist[vcount][0]);
- copy3(v3, &vectlist[vcount++][0]);
- } else {
- if (tcount > 100000) {
- fprintf(stderr, "too many points\n");
- exit();
- }
- copy3(n0, &trinorm[tcount][0]);
- copy3(v0, &trilist[tcount++][0]);
- copy3(n1, &trinorm[tcount][0]);
- copy3(v1, &trilist[tcount++][0]);
- copy3(n2, &trinorm[tcount][0]);
- copy3(v2, &trilist[tcount++][0]);
- }
- }
-
- void drawdisplaylist()
- {
- long i;
-
- for (i = 0; i < vcount;) {
- bgnpolygon();
- n3f(&normlist[i][0]);
- v3f(&vectlist[i++][0]);
- n3f(&normlist[i][0]);
- v3f(&vectlist[i++][0]);
- n3f(&normlist[i][0]);
- v3f(&vectlist[i++][0]);
- n3f(&normlist[i][0]);
- v3f(&vectlist[i++][0]);
- endpolygon();
- }
- for (i = 0; i < tcount;) {
- bgnpolygon();
- n3f(&trinorm[i][0]);
- v3f(&trilist[i++][0]);
- n3f(&trinorm[i][0]);
- v3f(&trilist[i++][0]);
- n3f(&trinorm[i][0]);
- v3f(&trilist[i++][0]);
- endpolygon();
- }
- }
-
- void drawworld()
- {
- c3f(blackvec);
- clear();
- zclear();
- drawdisplaylist();
- }
-
-
- static long menu, materials, models;
-
- initmenus()
- {
- materials = defpup("Shiny%x1|Purple%x2|Chalk%x3|Blue%x4|Red Plastic%x5|Cyan%x6");
- models = defpup("Ethanol%x100|SGI Logo%x101|Benzene%x102|Spiral%x103");
- addtopup(models, "Solid of Revolution%x107");
- addtopup(models, "Smooth Solid of Rev.%x104|Square Worm%x105|Platonic Solids%x106");
- addtopup(models, "Warped Stuff%x108");
- menu = defpup("Models %m|Materials %m|Save Spin%x999|Quit%x1000", models, materials);
- }
-
- static void openfile(long);
- FILE *binfile;
-
- void savetospinfile()
- {
- long i;
-
- openfile(vcount + (tcount*4)/3);
- for (i = 0; i < vcount; i++) {
- fwrite(&normlist[i][0], 4, 3, binfile);
- fwrite(&vectlist[i][0], 4, 3, binfile);
- }
- for (i = 0; i < tcount;) { /* hack!!!! write triangle as quad */
- fwrite(&trinorm[i][0], 4, 3, binfile);
- fwrite(&trilist[i++][0], 4, 3, binfile);
- fwrite(&trinorm[i][0], 4, 3, binfile);
- fwrite(&trilist[i++][0], 4, 3, binfile);
- fwrite(&trinorm[i][0], 4, 3, binfile);
- fwrite(&trilist[i][0], 4, 3, binfile);
- fwrite(&trinorm[i][0], 4, 3, binfile);
- fwrite(&trilist[i++][0], 4, 3, binfile);
- }
- fclose(binfile);
- return;
- }
-
- void printshit()
- {
- long i;
-
- for (i = 0; i < vcount; i++) {
- printf("%g, %g, %g\n",
- vectlist[i][0], vectlist[i][1], vectlist[i][2]);
- printf("%8.8x, %8.8x, %8.8x\n\n",
- *(long *)&vectlist[i][0], *(long *)&vectlist[i][1], *(long *)&vectlist[i][2]);
- }
- }
-
- static void openfile(long pcount)
- {
- long magic[3];
-
- magic[0] = 0x5423;
- magic[1] = pcount*4;
- magic[2] = 0; /* no color values */
-
- if ((binfile = fopen("model.spin", "w")) == 0) {
- fprintf(stderr, "couldn't open model.spin\n");
- return;
- }
- fwrite(magic, 4, 3, binfile);
- }
-
-
-
-
- main(int argc, char **argv)
- {
- long i;
- curve_t *lisscrv;
-
- if (argc != 3) {
- printf("usage: %s n1, n2\n", argv[0]);
- exit(-1);
- }
- seterrorfunc(errf);
- th = atoi(argv[1]);
- ph = atoi(argv[2]);
- lcm = th*ph/gcd(th, ph);
- theta = th*2.0*PI;
- phi = ph*2.0*PI;
- STEPS = 500/lcm;
- keepaspect(1, 1);
- foreground();
- winopen("lissajous");
- backface(1);
- RGBmode();
- doublebuffer();
- zbuffer(TRUE);
- gconfig();
- mmode(MVIEWING);
- perspective(450, 1.0, 1.0, 15.0);
- def_simple_light_model();
- use_simple_light_model();
- lisscrv = newanalyticcurve(x, y, z, xx, yy, zz, xxx, yyy, zzz);
- makeworm(lisscrv, .11, 12, 350, savefunc);
- lmbind(MATERIAL, 1);
- /*
- savetospinfile();
- */
- /*printshit();*/
- while (1) {
- trackmodel(drawworld, 4.0, 0.0, 0.0, 0.0);
- }
- }
-