Fixed a build problem.
[gljewel.git] / ico.c
bloba90df7ba6b45330dd45700e00985a1ef6d546e8b
1 /**
2 * @file ico.c
3 * @brief The icosahedron module.
5 * Copyright 2001, 2008 David Ashley <dashxdr@gmail.com>
6 * Copyright 2008 Stephen M. Webb <stephen.webb@bregmasoft.ca>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of Version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "ico.h"
23 #if defined(_WIN32) && !defined(__CYGWIN__)
24 # define WIN32_LEAN_AND_MEAN 1
25 # include <windows.h>
26 #endif
28 #include <GL/gl.h>
29 #include <math.h>
30 #include "misc.h"
31 #include <string.h>
33 #define X .525731112119133606f
34 #define Z .850650808352039932f
36 static GLfloat vdata [12][3] = {
37 {-X, 0.0, Z}, {X, 0.0, Z}, {-X, 0.0, -Z},
38 {X, 0.0, -Z}, {0.0, Z, X}, {0.0, Z, -X},
39 {0.0, -Z, X}, {0.0, -Z, -X}, {Z, X, 0.0},
40 {-Z, X, 0.0}, {Z, -X, 0.0}, {-Z, -X, 0.0}
43 static GLuint tindices [20][3] = {
44 {1, 4, 0}, {4, 9, 0},
45 {4, 5, 9}, {8, 5, 4},
46 {1, 8, 4}, {1, 10, 8},
47 {10, 3, 8}, {8, 3, 5},
48 {3, 2, 5}, {3, 7, 2},
49 {3, 10, 7}, {10, 6, 7},
50 {6, 11, 7}, {6, 0, 11},
51 {6, 1, 0}, {10, 1, 6},
52 {11, 0, 9}, {2, 11, 9},
53 {5, 2, 9}, {11, 2, 7}
56 void dup3(float *a,float *b)
58 *a++=*b++;
59 *a++=*b++;
60 *a=*b;
63 void mid(float *a,float *b,float *c)
65 float d;
66 a[0]=(*b++ + *c++)/2.0f;
67 a[1]=(*b++ + *c++)/2.0f;
68 a[2]=(*b + *c)/2.0f;
69 d=1.0f/sqrtf(a[0]*a[0]+a[1]*a[1]+a[2]*a[2]);
70 a[0]*=d;
71 a[1]*=d;
72 a[2]*=d;
77 void makeicosahedron(int sub,float scale)
79 float tris[65536][3];
80 int i,j,p,q;
81 int numt;
82 numt=0;
83 j=0;
84 for(i=0;i<20;++i)
86 p=tindices[i][0];
87 tris[j][0]=vdata[p][0];
88 tris[j][1]=vdata[p][1];
89 tris[j][2]=vdata[p][2];
90 ++j;
91 p=tindices[i][1];
92 tris[j][0]=vdata[p][0];
93 tris[j][1]=vdata[p][1];
94 tris[j][2]=vdata[p][2];
95 ++j;
96 p=tindices[i][2];
97 tris[j][0]=vdata[p][0];
98 tris[j][1]=vdata[p][1];
99 tris[j][2]=vdata[p][2];
100 ++j;
101 ++numt;
103 while(sub--)
105 j=numt;
106 numt<<=2;
107 while(--j>=0)
109 float ttt[3][3];
110 p=j*3;
111 q=p*4;
112 memcpy(ttt,tris[p],sizeof(ttt));
114 mid(tris[q++],ttt[0],ttt[1]);
115 dup3(tris[q++],ttt[1]);
116 mid(tris[q++],ttt[1],ttt[2]);
118 dup3(tris[q++],ttt[0]);
119 mid(tris[q++],ttt[0],ttt[1]);
120 mid(tris[q++],ttt[0],ttt[2]);
122 mid(tris[q++],ttt[1],ttt[2]);
123 dup3(tris[q++],ttt[2]);
124 mid(tris[q++],ttt[0],ttt[2]);
126 mid(tris[q++],ttt[0],ttt[1]);
127 mid(tris[q++],ttt[1],ttt[2]);
128 mid(tris[q++],ttt[0],ttt[2]);
132 glEnable(GL_NORMALIZE);
133 p=0;
134 glBegin(GL_TRIANGLES);
135 for(i=0;i<numt;++i)
137 norm2(tris[p],tris[p+1],tris[p+2]);
138 glVertex3f(tris[p][0]*scale,tris[p][1]*scale,tris[p][2]*scale);
139 ++p;
140 glVertex3f(tris[p][0]*scale,tris[p][1]*scale,tris[p][2]*scale);
141 ++p;
142 glVertex3f(tris[p][0]*scale,tris[p][1]*scale,tris[p][2]*scale);
143 ++p;
145 glEnd();