From http://www.jwz.org/xscreensaver/xscreensaver-5.30.tar.gz
[xscreensaver] / hacks / glx / gllist.c
1 /* xscreensaver, Copyright (c) 1998-2014 Jamie Zawinski <jwz@jwz.org>
2  *
3  * Permission to use, copy, modify, distribute, and sell this software and its
4  * documentation for any purpose is hereby granted without fee, provided that
5  * the above copyright notice appear in all copies and that both that
6  * copyright notice and this permission notice appear in supporting
7  * documentation.  No representations are made about the suitability of this
8  * software for any purpose.  It is provided "as is" without express or 
9  * implied warranty.
10  */
11
12 #include "gllist.h"
13
14 void
15 renderList (const struct gllist *list, int wire_p)
16 {
17   while (list)
18     {
19       if (!wire_p || list->primitive == GL_LINES)
20         {
21           glInterleavedArrays (list->format, 0, list->data);
22           glDrawArrays (list->primitive, 0, list->points);
23         }
24       else
25         {
26           /* For wireframe, do it the hard way: treat every tuple of
27              points as its own line loop.
28            */
29           const GLfloat *p = (GLfloat *) list->data;
30           int i, j, tick, skip, stride;
31
32           switch (list->primitive) {
33           case GL_QUADS: tick = 4; break;
34           case GL_TRIANGLES: tick = 3; break;
35           default: abort(); break; /* write me */
36           }
37
38           switch (list->format) {
39           case GL_C3F_V3F: case GL_N3F_V3F: skip = 3; stride = 6; break;
40           default: abort(); break; /* write me */
41           }
42
43           glBegin (GL_LINE_LOOP);
44           for (i = 0, j = skip;
45                i < list->points;
46                i++, j += stride)
47             {
48               if (i && !(i % tick))
49                 {
50                   glEnd();
51                   glBegin (GL_LINE_LOOP);
52                 }
53               glVertex3f (p[j], p[j+1], p[j+2]);
54             }
55           glEnd();
56         }
57       list = list->next;
58     }
59 }
60
61
62 void
63 renderListNormals (const struct gllist *list, GLfloat length, int faces_p)
64 {
65   while (list)
66     {
67       const GLfloat *p = (GLfloat *) list->data;
68       int i, j, tick, skip, stride;
69       GLfloat v[3], n[3];
70
71       if (list->primitive == GL_LINES) continue;
72
73       if (! faces_p)
74         tick = 1;
75       else
76         switch (list->primitive) {
77         case GL_QUADS: tick = 4; break;
78         case GL_TRIANGLES: tick = 3; break;
79         default: abort(); break; /* write me */
80         }
81
82       switch (list->format) {
83       case GL_N3F_V3F: skip = 0; stride = 6; break;
84       case GL_C3F_V3F: continue; break;
85       default: abort(); break; /* write me */
86       }
87
88       v[0] = v[1] = v[2] = 0;
89       n[0] = n[1] = n[2] = 0;
90
91       for (i = 0, j = skip;
92            i <= list->points;
93            i++, j += stride)
94         {
95           if (i && !(i % tick))
96             {
97               n[0] /= tick;
98               n[1] /= tick;
99               n[2] /= tick;
100               v[0] /= tick;
101               v[1] /= tick;
102               v[2] /= tick;
103               glPushMatrix();
104               glTranslatef (v[0], v[1], v[2]);
105               glScalef (length, length, length);
106               glBegin (GL_LINES);
107               glVertex3f (0, 0, 0);
108               glVertex3f (n[0], n[1], n[2]);
109               glEnd();
110               glPopMatrix();
111               v[0] = v[1] = v[2] = 0;
112               n[0] = n[1] = n[2] = 0;
113             }
114
115           if (i == list->points) break;
116           n[0] += p[j];
117           n[1] += p[j+1];
118           n[2] += p[j+2];
119           v[0] += p[j+3];
120           v[1] += p[j+4];
121           v[2] += p[j+5];
122
123         }
124       list = list->next;
125     }
126 }