began for maemo
[xscreensaver] / 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 = 0.0;
26 static GLfloat view_scale = 4.0;
27
28
29 stonerview_state *
30 init_view(int wireframe_p)
31 {
32   stonerview_state *st = (stonerview_state *) calloc (1, sizeof(*st));
33
34   st->wireframe = wireframe_p;
35   st->num_els = NUM_ELS;
36   st->elist = (elem_t *) calloc (st->num_els, sizeof(*st->elist));
37
38   st->osctail = &st->oscroot;
39
40   /* for trackball, two-sided lighting and no face culling */
41   glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
42
43   glEnable(GL_LIGHTING);
44   glEnable(GL_LIGHT0);
45   glEnable(GL_DEPTH_TEST);
46
47   glEnable(GL_NORMALIZE);
48
49   return st;
50 }
51
52 /* callback: draw everything */
53 void win_draw(stonerview_state *st)
54 {
55   int ix;
56   static const GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 };
57   static const GLfloat gray[]  = { 0.6, 0.6, 0.6, 1.0 };
58
59   glDrawBuffer(GL_BACK);
60
61   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
62
63   glPushMatrix();
64   glScalef(view_scale, view_scale, view_scale);
65   glRotatef(view_rotx, 1.0, 0.0, 0.0);
66   glRotatef(view_roty, 0.0, 1.0, 0.0);
67   glRotatef(view_rotz, 0.0, 0.0, 1.0);
68
69   glShadeModel(GL_FLAT);
70
71   for (ix=0; ix < st->num_els; ix++) {
72     elem_t *el = &st->elist[ix];
73
74     glNormal3f(0.0, 0.0, 1.0);
75
76     /* outline the square */
77     glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, (st->wireframe ? white : gray));
78     glBegin(GL_LINE_LOOP);
79     glVertex3f(el->pos[0]-el->vervec[0], el->pos[1]-el->vervec[1], el->pos[2]);
80     glVertex3f(el->pos[0]+el->vervec[1], el->pos[1]-el->vervec[0], el->pos[2]);
81     glVertex3f(el->pos[0]+el->vervec[0], el->pos[1]+el->vervec[1], el->pos[2]);
82     glVertex3f(el->pos[0]-el->vervec[1], el->pos[1]+el->vervec[0], el->pos[2]);
83     glEnd();
84
85     if (st->wireframe) continue;
86
87     /* fill the square */
88     glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, el->col);
89     glBegin(GL_QUADS);
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     glVertex3f(el->pos[0]+el->vervec[0], el->pos[1]+el->vervec[1], el->pos[2]);
93     glVertex3f(el->pos[0]-el->vervec[1], el->pos[1]+el->vervec[0], el->pos[2]);
94     glEnd();
95   }
96
97   glPopMatrix();
98 }
99
100 void win_release(stonerview_state *st)
101 {
102   free (st->elist);
103   /*free (st->oscroot);  -- #### how do we free this? */
104   free (st);
105 }