http://www.jwz.org/xscreensaver/xscreensaver-5.07.tar.gz
[xscreensaver] / hacks / glx / stonerview-view.c
1 /* StonerView: An eccentric visual toy.
2    Copyright 1998-2001 by Andrew Plotkin (erkyrath@eblong.com)
3    http://www.eblong.com/zarf/stonerview.html
4  
5    Permission to use, copy, modify, distribute, and sell this software and its
6    documentation for any purpose is hereby granted without fee, provided that
7    the above copyright notice appear in all copies and that both that
8    copyright notice and this permission notice appear in supporting
9    documentation.  No representations are made about the suitability of this
10    software for any purpose.  It is provided "as is" without express or 
11    implied warranty.
12 */
13
14 /* Ported away from GLUT (so that it can do `-root' and work with xscreensaver)
15    by Jamie Zawinski <jwz@jwz.org>, 22-Jan-2001.
16  */
17
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21
22 #include <stdlib.h>
23 #include "stonerview.h"
24
25 static GLfloat view_rotx = -45.0, view_roty = 0.0, view_rotz = 15.0;
26 static GLfloat view_scale = 4.0;
27
28
29 stonerview_state *
30 init_view(int wireframe_p, int transparent_p)
31 {
32   stonerview_state *st = (stonerview_state *) calloc (1, sizeof(*st));
33
34   st->wireframe = wireframe_p;
35   st->transparent = transparent_p;
36   st->num_els = NUM_ELS;
37   st->elist = (elem_t *) calloc (st->num_els, sizeof(*st->elist));
38
39   st->osctail = &st->oscroot;
40
41   /* for trackball, two-sided lighting and no face culling */
42   glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
43
44   glEnable(GL_CULL_FACE);
45   glEnable(GL_LIGHTING);
46   glEnable(GL_LIGHT0);
47   glEnable(GL_DEPTH_TEST);
48
49   glEnable(GL_NORMALIZE);
50
51   glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
52   glEnable(GL_LINE_SMOOTH);
53   glEnable(GL_BLEND);
54
55   if (st->transparent)
56     glBlendFunc(GL_SRC_ALPHA,GL_ONE);
57
58   return st;
59 }
60
61 /* callback: draw everything */
62 void win_draw(stonerview_state *st)
63 {
64   int ix;
65   static const GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 };
66   static const GLfloat gray[]  = { 0.6, 0.6, 0.6, 1.0 };
67
68   glDrawBuffer(GL_BACK);
69
70   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
71
72   glPushMatrix();
73   glScalef(view_scale, view_scale, view_scale);
74   glRotatef(view_rotx, 1.0, 0.0, 0.0);
75   glRotatef(view_roty, 0.0, 1.0, 0.0);
76   glRotatef(view_rotz, 0.0, 0.0, 1.0);
77
78   glShadeModel(GL_FLAT);
79
80   for (ix=0; ix < st->num_els; ix++) {
81     elem_t *el = &st->elist[ix];
82
83     glNormal3f(0.0, 0.0, 1.0);
84
85     /* outline the square */
86     glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, (st->wireframe ? white : gray));
87     glBegin(GL_LINE_LOOP);
88     glVertex3f(el->pos[0]-el->vervec[0], el->pos[1]-el->vervec[1], el->pos[2]);
89     glVertex3f(el->pos[0]+el->vervec[1], el->pos[1]-el->vervec[0], el->pos[2]);
90     glVertex3f(el->pos[0]+el->vervec[0], el->pos[1]+el->vervec[1], el->pos[2]);
91     glVertex3f(el->pos[0]-el->vervec[1], el->pos[1]+el->vervec[0], el->pos[2]);
92     glEnd();
93
94     if (st->wireframe) continue;
95
96     /* fill the square */
97     glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, el->col);
98     glBegin(GL_QUADS);
99     glVertex3f(el->pos[0]-el->vervec[0], el->pos[1]-el->vervec[1], el->pos[2]);
100     glVertex3f(el->pos[0]+el->vervec[1], el->pos[1]-el->vervec[0], el->pos[2]);
101     glVertex3f(el->pos[0]+el->vervec[0], el->pos[1]+el->vervec[1], el->pos[2]);
102     glVertex3f(el->pos[0]-el->vervec[1], el->pos[1]+el->vervec[0], el->pos[2]);
103     glEnd();
104   }
105
106   glPopMatrix();
107 }
108
109 void win_release(stonerview_state *st)
110 {
111   free (st->elist);
112   /*free (st->oscroot);  -- #### how do we free this? */
113   free (st);
114 }