From http://www.jwz.org/xscreensaver/xscreensaver-5.18.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 stonerview_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 = (stonerview_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
63 stonerview_win_draw(stonerview_state *st)
64 {
65   int ix;
66   static const GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 };
67   static const GLfloat gray[]  = { 0.6, 0.6, 0.6, 1.0 };
68
69   glDrawBuffer(GL_BACK);
70
71   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
72
73   glPushMatrix();
74   glScalef(view_scale, view_scale, view_scale);
75   glRotatef(view_rotx, 1.0, 0.0, 0.0);
76   glRotatef(view_roty, 0.0, 1.0, 0.0);
77   glRotatef(view_rotz, 0.0, 0.0, 1.0);
78
79   glShadeModel(GL_FLAT);
80
81   for (ix=0; ix < st->num_els; ix++) {
82     stonerview_elem_t *el = &st->elist[ix];
83
84     glNormal3f(0.0, 0.0, 1.0);
85
86     /* outline the square */
87     glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, (st->wireframe ? white : gray));
88     glBegin(GL_LINE_LOOP);
89     glVertex3f(el->pos[0]-el->vervec[0], el->pos[1]-el->vervec[1], el->pos[2]);
90     glVertex3f(el->pos[0]+el->vervec[1], el->pos[1]-el->vervec[0], el->pos[2]);
91     glVertex3f(el->pos[0]+el->vervec[0], el->pos[1]+el->vervec[1], el->pos[2]);
92     glVertex3f(el->pos[0]-el->vervec[1], el->pos[1]+el->vervec[0], el->pos[2]);
93     glEnd();
94
95     if (st->wireframe) continue;
96
97     /* fill the square */
98     glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, el->col);
99     glBegin(GL_QUADS);
100     glVertex3f(el->pos[0]-el->vervec[0], el->pos[1]-el->vervec[1], el->pos[2]);
101     glVertex3f(el->pos[0]+el->vervec[1], el->pos[1]-el->vervec[0], el->pos[2]);
102     glVertex3f(el->pos[0]+el->vervec[0], el->pos[1]+el->vervec[1], el->pos[2]);
103     glVertex3f(el->pos[0]-el->vervec[1], el->pos[1]+el->vervec[0], el->pos[2]);
104     glEnd();
105   }
106
107   glPopMatrix();
108 }
109
110 void
111 stonerview_win_release(stonerview_state *st)
112 {
113   free (st->elist);
114   /*free (st->oscroot);  -- #### how do we free this? */
115   free (st);
116 }