Debian Codegraph @

apps/demos/qmorf/quadCube.c++

raw
1 /*
2 *
3 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 //
38 // Code to generate a cube made out of a single quad-mesh
39 //
40 // Basic shape is:
41 // /\
42 // / \
43 // / \
44 // / \
45 // --------
46 // | |
47 // | |
48 // | -- |
49 // | |
50 // | |
51 // --------
52 // \ /
53 // \ /
54 // \ /
55 // \/
56 // This is rotated about the y axis to create 4 symmetric copies.
57 //
58
59 #include "QuadThing.h"
60 #include <Inventor/SoDB.h>
61 #include <Inventor/nodes/SoSeparator.h>
62 #include <Inventor/actions/SoWriteAction.h>
63
64 //
65 // Can create either a seam on the back-diagonal (in which case the
66 // cube is rotated 45 degrees) or a seam along the back face.
67 //
68 int diagonalFlag = 1;
69
70 QuadThing *
71 createQuadCube(int sizex, int sizey)
72 {
73 int nh = sizey/4;
74 int nw = sizex/4;
75
76 SbVec3f delta_row, delta_column, r_start;
77 SbVec3f *points = new SbVec3f[sizex*sizey];
78 int row, column, c;
79
80 // This rotation matrix is used if the diagonalFlag is set. If it
81 // is, we rotate the cube so the corners face toward and away from
82 // the viewer, and the seam of the quadmesh is along the far
83 // corner. This matches other primitives we create (including
84 // CyberWare heads).
85 // Otherwise, the indexing is mucked to make the seam the middle
86 // of the far face (and the cube is not rotated).
87
88 SbMatrix m;
89 m.setRotate(SbRotation(SbVec3f(0,1,0), M_PI/4.0));
90
91 // Form top triangular piece:
92 delta_row.setValue(1.0/(nh-1), 0.0, -1.0/(nh-1));
93
94 for (row = 0; row < nh; row++)
95 {
96 r_start = SbVec3f(0.0, 1., 0.0) + delta_row * row;
97
98 // Width of entire row is (2*row)/(nh-1), so:
99 delta_column.setValue( -(2.0*row/(nh-1))/(nw-1), 0, 0);
100
101 for (column = 0; column < nw; column++)
102 {
103 if (diagonalFlag) c = column;
104 else c = (column+sizex-nw/2)%sizex;
105
106 int i = row*sizex+c;
107 points[i] = r_start + delta_column * column;
108 points[i] *= .1;
109 if (diagonalFlag)
110 m.multVecMatrix(points[i], points[i]);
111 }
112 }
113
114 // Do big square piece:
115 delta_row.setValue(0, -2.0/(2*nh-1), 0);
116 delta_column.setValue(-2.0/(nw-1), 0, 0);
117 for (row = 0; row < nh*2; row++)
118 {
119 r_start = SbVec3f(1.0, 1.0, -1.0) + delta_row * row;
120
121 for (column = 0; column < nw; column++)
122 {
123 if (diagonalFlag) c = column;
124 else c = (column+sizex-nw/2)%sizex;
125
126 int i = (nh+row)*sizex+c;
127 points[i] = r_start + delta_column * column;
128 points[i] *= .1;
129 if (diagonalFlag)
130 m.multVecMatrix(points[i], points[i]);
131 }
132 }
133
134 // Do bottom triangular piece:
135 delta_row.setValue(-1.0/(nh-1), 0, 1.0/(nh-1));
136 for (row = 0; row < nh; row++)
137 {
138 r_start = SbVec3f(1.0, -1.0, -1.0) + delta_row * row;
139
140 delta_column.setValue( -(2.0*((nh-1)-row)/(nh-1))/(nw-1), 0, 0);
141
142 for (column = 0; column < nw; column++)
143 {
144 if (diagonalFlag) c = column;
145 else c = (column+sizex-nw/2)%sizex;
146
147 int i = (nh*3+row)*sizex+c;
148 points[i] = r_start + delta_column * column;
149 points[i] *= .1;
150 if (diagonalFlag)
151 m.multVecMatrix(points[i], points[i]);
152 }
153 }
154
155 // Now, rotate the piece to create other 3 pieces:
156 for (row = 0; row < nh*4; row++)
157 {
158 for (column = 0; column < nw; column++)
159 {
160 if (diagonalFlag) c = column;
161 else c = (column+sizex-nw/2)%sizex;
162
163 float x, y, z;
164 points[row*sizex + c].getValue(x, y, z);
165
166 points[row*sizex + (nw+c)%sizex].setValue(z, y, -x);
167 points[row*sizex + (nw*2+c)%sizex].setValue(-x, y, -z);
168 points[row*sizex + (nw*3+c)%sizex].setValue(-z, y, x);
169 }
170 }
171 if (!diagonalFlag) // HACK WARNING!
172 {
173 // Force first and last vertices to match
174 for (row = 0; row < nh*4; row++)
175 {
176 points[row*sizex+sizex-1] = points[row*sizex];
177 }
178 }
179
180 QuadThing *result = new QuadThing(sizex, sizey, points);
181
182 return result;
183 }
184
185 int
186 main(int argc, char **argv)
187 {
188 if (argc > 1) diagonalFlag = 0;
189
190 SoDB::init();
191
192 QuadThing *result = createQuadCube(40, 40);
193
194 // Now write it out to stdout
195 SoWriteAction wa;
196
197 wa.apply(result->getSceneGraph());
198
199 return 0;
200 }