~ubuntu-branches/ubuntu/gutsy/rss-glx/gutsy

« back to all changes in this revision

Viewing changes to reallyslick/cpp_src/impCubeTable.cpp

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2005-11-30 18:21:27 UTC
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20051130182127-5iww7elbiyzej1lk
Tags: upstream-0.8.0
ImportĀ upstreamĀ versionĀ 0.8.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2002  Terence M. Welsh
3
 
 * Ported to Linux by Tugrul Galatali <tugrul@galatali.com>
4
 
 *
5
 
 * Implicit is free software; you can redistribute it and/or modify
6
 
 * it under the terms of the GNU General Public License version 2 as 
7
 
 * published by the Free Software Foundation.
8
 
 *
9
 
 * Implicit is distributed in the hope that it will be useful,
10
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
 * GNU General Public License for more details.
13
 
 *
14
 
 * You should have received a copy of the GNU General Public License
15
 
 * along with this program; if not, write to the Free Software
16
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 
 */
18
 
 
19
 
#include "impCubeTable.h"
20
 
 
21
 
impCubeTable::impCubeTable ()
22
 
{
23
 
        cubetable = new int *[256];
24
 
 
25
 
        for (int i = 0; i < 256; i++)
26
 
                cubetable[i] = new int[17];
27
 
 
28
 
        ec[0][0] = 0;
29
 
        ec[0][1] = 1;
30
 
        ec[1][0] = 0;
31
 
        ec[1][1] = 2;
32
 
        ec[2][0] = 1;
33
 
        ec[2][1] = 3;
34
 
        ec[3][0] = 2;
35
 
        ec[3][1] = 3;
36
 
        ec[4][0] = 0;
37
 
        ec[4][1] = 4;
38
 
        ec[5][0] = 1;
39
 
        ec[5][1] = 5;
40
 
        ec[6][0] = 2;
41
 
        ec[6][1] = 6;
42
 
        ec[7][0] = 3;
43
 
        ec[7][1] = 7;
44
 
        ec[8][0] = 4;
45
 
        ec[8][1] = 5;
46
 
        ec[9][0] = 4;
47
 
        ec[9][1] = 6;
48
 
        ec[10][0] = 5;
49
 
        ec[10][1] = 7;
50
 
        ec[11][0] = 6;
51
 
        ec[11][1] = 7;
52
 
 
53
 
        vc[0][0] = 0;
54
 
        vc[0][1] = 1;
55
 
        vc[0][2] = 4;
56
 
        vc[1][0] = 0;
57
 
        vc[1][1] = 5;
58
 
        vc[1][2] = 2;
59
 
        vc[2][0] = 1;
60
 
        vc[2][1] = 3;
61
 
        vc[2][2] = 6;
62
 
        vc[3][0] = 2;
63
 
        vc[3][1] = 7;
64
 
        vc[3][2] = 3;
65
 
        vc[4][0] = 4;
66
 
        vc[4][1] = 9;
67
 
        vc[4][2] = 8;
68
 
        vc[5][0] = 5;
69
 
        vc[5][1] = 8;
70
 
        vc[5][2] = 10;
71
 
        vc[6][0] = 6;
72
 
        vc[6][1] = 11;
73
 
        vc[6][2] = 9;
74
 
        vc[7][0] = 7;
75
 
        vc[7][1] = 10;
76
 
        vc[7][2] = 11;
77
 
 
78
 
        makecubetable ();
79
 
 
80
 
        crawltable = new bool *[256];
81
 
        for (int i = 0; i < 256; i++)
82
 
                crawltable[i] = new bool[6];
83
 
 
84
 
        makecrawltable ();
85
 
}
86
 
 
87
 
// determines next edge extending from a vertex in counterclockwise order
88
 
int
89
 
  impCubeTable::nextedge (int vertex, int edge)
90
 
{
91
 
        if (vc[vertex][0] == edge)
92
 
                return (vc[vertex][1]);
93
 
        if (vc[vertex][1] == edge)
94
 
                return (vc[vertex][2]);
95
 
        if (vc[vertex][2] == edge)
96
 
                return (vc[vertex][0]);
97
 
 
98
 
        return (-1);
99
 
}
100
 
 
101
 
// adds a triangle strip description to the cube table
102
 
void impCubeTable::addtotable (int row, int edgecount, int *edgelist)
103
 
{
104
 
        static int lastrow = -1;
105
 
        static int totalcount;
106
 
 
107
 
        if (row != lastrow)
108
 
                totalcount = 0;
109
 
 
110
 
        // enter the number of vertices into the cubetable
111
 
        cubetable[row][totalcount] = edgecount;
112
 
 
113
 
        // The edges are listed in counterclockwise order in the edgelist.
114
 
        // Notice how they are added to the cubetable out of order, in
115
 
        // a zig-zag pattern the way a triangle strip is drawn.
116
 
        // There can be at most 7 vertices in one of these triangle strips.
117
 
        switch (edgecount) {
118
 
        case 3:
119
 
                cubetable[row][totalcount + 1] = edgelist[0];
120
 
                cubetable[row][totalcount + 2] = edgelist[1];
121
 
                cubetable[row][totalcount + 3] = edgelist[2];
122
 
                break;
123
 
        case 4:
124
 
                cubetable[row][totalcount + 1] = edgelist[0];
125
 
                cubetable[row][totalcount + 2] = edgelist[1];
126
 
                cubetable[row][totalcount + 3] = edgelist[3];
127
 
                cubetable[row][totalcount + 4] = edgelist[2];
128
 
                break;
129
 
        case 5:
130
 
                cubetable[row][totalcount + 1] = edgelist[0];
131
 
                cubetable[row][totalcount + 2] = edgelist[1];
132
 
                cubetable[row][totalcount + 3] = edgelist[4];
133
 
                cubetable[row][totalcount + 4] = edgelist[2];
134
 
                cubetable[row][totalcount + 5] = edgelist[3];
135
 
                break;
136
 
        case 6:
137
 
                cubetable[row][totalcount + 1] = edgelist[0];
138
 
                cubetable[row][totalcount + 2] = edgelist[1];
139
 
                cubetable[row][totalcount + 3] = edgelist[5];
140
 
                cubetable[row][totalcount + 4] = edgelist[2];
141
 
                cubetable[row][totalcount + 5] = edgelist[4];
142
 
                cubetable[row][totalcount + 6] = edgelist[3];
143
 
                break;
144
 
        case 7:
145
 
                cubetable[row][totalcount + 1] = edgelist[0];
146
 
                cubetable[row][totalcount + 2] = edgelist[1];
147
 
                cubetable[row][totalcount + 3] = edgelist[6];
148
 
                cubetable[row][totalcount + 4] = edgelist[2];
149
 
                cubetable[row][totalcount + 5] = edgelist[5];
150
 
                cubetable[row][totalcount + 6] = edgelist[3];
151
 
                cubetable[row][totalcount + 7] = edgelist[4];
152
 
                break;
153
 
        }
154
 
 
155
 
        totalcount += (edgecount + 1);
156
 
        lastrow = row;
157
 
}
158
 
 
159
 
void impCubeTable::makecubetable ()
160
 
{
161
 
        int i, j, k;
162
 
        int currentvertex;
163
 
        int currentedge;
164
 
        bool vertices[8];       // true if on low side of gradient (outside of surface)
165
 
        bool edges[12];
166
 
        bool edgesdone[12];
167
 
        int edgelist[7];        // final list of egdes used in a triangle strip
168
 
        int edgecount;
169
 
 
170
 
        // Set cubetable values to zero
171
 
        // A zero will indicate that there are no more triangle strips to build
172
 
        for (i = 0; i < 256; i++) {
173
 
                for (j = 0; j < 17; j++) {
174
 
                        cubetable[i][j] = 0;
175
 
                }
176
 
        }
177
 
 
178
 
        // For each vertex combination
179
 
        for (i = 0; i < 256; i++) {
180
 
                // identify the vertices on the low side of the gradient
181
 
                int andbit;
182
 
 
183
 
                for (j = 0; j < 8; j++) {
184
 
                        andbit = 1;
185
 
                        for (k = 0; k < j; k++)
186
 
                                andbit *= 2;
187
 
                        if (i & andbit)
188
 
                                vertices[j] = 1;
189
 
                        else
190
 
                                vertices[j] = 0;
191
 
                }
192
 
 
193
 
                // Identify the edges that cross threshold value
194
 
                // These are edges that connect 1 turned-on and 1 turned-off vertex
195
 
                for (j = 0; j < 12; j++) {
196
 
                        if ((vertices[ec[j][0]] + vertices[ec[j][1]]) == 1)
197
 
                                edges[j] = 1;
198
 
                        else
199
 
                                edges[j] = 0;
200
 
                        edgesdone[j] = 0;       // no edges have been used yet
201
 
                }
202
 
 
203
 
                // Construct lists of edges that form triangle strips
204
 
                // try starting from each edge (no need to try last 2 edges)
205
 
                for (j = 0; j < 10; j++) {
206
 
                        currentedge = j;
207
 
                        edgecount = 0;
208
 
                        // if this edge contains a surface vertex and hasn't been used
209
 
                        while (edges[currentedge] && !edgesdone[currentedge]) {
210
 
                                // add edge to list
211
 
                                edgelist[edgecount] = currentedge;
212
 
                                edgecount++;
213
 
                                edgesdone[currentedge] = 1;
214
 
                                // find that edge's vertex on low side of gradient
215
 
                                if (vertices[ec[currentedge][0]])
216
 
                                        currentvertex = ec[currentedge][0];
217
 
                                else
218
 
                                        currentvertex = ec[currentedge][1];
219
 
                                // move along gradiant boundary to find next edge
220
 
                                currentedge = nextedge (currentvertex, currentedge);
221
 
                                while (!edges[currentedge]) {
222
 
                                        if (currentvertex != ec[currentedge][0])
223
 
                                                currentvertex = ec[currentedge][0];
224
 
                                        else
225
 
                                                currentvertex = ec[currentedge][1];
226
 
                                        currentedge = nextedge (currentvertex, currentedge);
227
 
                                }
228
 
                        }
229
 
                        // if a surface has been created add it to the table
230
 
                        // and start over to try to make another surface
231
 
                        if (edgecount)
232
 
                                addtotable (i, edgecount, edgelist);
233
 
                }
234
 
        }
235
 
}
236
 
 
237
 
void impCubeTable::makecrawltable ()
238
 
{
239
 
        int i, j, k;
240
 
        bool vertices[8];       // vertices below gradient threshold get turned on
241
 
        bool edges[12];         // edges that cross gradient threshold get turned on
242
 
 
243
 
        // For each vertex combination
244
 
        for (i = 0; i < 256; i++) {
245
 
                // identify the vertices on the low side of the gradient
246
 
                int andbit;
247
 
 
248
 
                for (j = 0; j < 8; j++) {
249
 
                        andbit = 1;
250
 
                        for (k = 0; k < j; k++)
251
 
                                andbit *= 2;
252
 
                        if (i & andbit)
253
 
                                vertices[j] = 1;
254
 
                        else
255
 
                                vertices[j] = 0;
256
 
                }
257
 
 
258
 
                // Identify the edges that cross threshold value
259
 
                // These are edges that connect 1 turned-on and 1 turned-off vertex
260
 
                for (j = 0; j < 12; j++) {
261
 
                        if ((vertices[ec[j][0]] + vertices[ec[j][1]]) == 1)
262
 
                                edges[j] = 1;
263
 
                        else
264
 
                                edges[j] = 0;
265
 
                }
266
 
 
267
 
                // -x
268
 
                if (edges[0] || edges[1] || edges[2] || edges[3])
269
 
                        crawltable[i][0] = true;
270
 
                else
271
 
                        crawltable[i][0] = false;
272
 
                // +x
273
 
                if (edges[8] || edges[9] || edges[10] || edges[11])
274
 
                        crawltable[i][1] = true;
275
 
                else
276
 
                        crawltable[i][1] = false;
277
 
                // -y
278
 
                if (edges[0] || edges[4] || edges[5] || edges[8])
279
 
                        crawltable[i][2] = true;
280
 
                else
281
 
                        crawltable[i][2] = false;
282
 
                // +y
283
 
                if (edges[3] || edges[6] || edges[7] || edges[11])
284
 
                        crawltable[i][3] = true;
285
 
                else
286
 
                        crawltable[i][3] = false;
287
 
                // -z
288
 
                if (edges[1] || edges[4] || edges[6] || edges[9])
289
 
                        crawltable[i][4] = true;
290
 
                else
291
 
                        crawltable[i][4] = false;
292
 
                // +z
293
 
                if (edges[2] || edges[5] || edges[7] || edges[10])
294
 
                        crawltable[i][5] = true;
295
 
                else
296
 
                        crawltable[i][5] = false;
297
 
        }
298
 
}