001/*****************************************************************************
002 *                      J3D.org Copyright (c) 2000
003 *                              Java Source
004 *
005 * This source is licensed under the GNU LGPL v2.1
006 * Please read http://www.gnu.org/copyleft/lgpl.html for more information
007 *
008 ****************************************************************************/
009
010package jahuwaldt.j3d.geom;
011
012// Standard imports
013import java.text.NumberFormat;
014
015// Application specific imports
016// none
017
018/**
019 * Data representation of geometry information that is created through the
020 * various generator classes in this package.
021 * <p>
022 *
023 * This data representation is used to hold information needed to generate
024 * geometry from one of the generator classes in this package. In general,
025 * data does not get filled in for items that are not requested.
026 * <p>
027 *
028 * The type of data to be produced can be changed with each call. While it is
029 * possible to ask for both 2D and 3D texture coordinates, the code will only
030 * generate 2D values if asked.
031 *
032 * @author Justin Couch
033 * @version $Revision: 1.10 $
034 */
035public class GeometryData implements Cloneable
036{
037    /** Generate the geometry as individual unindexed triangles */
038    public static final int TRIANGLES = 1;
039
040    /** Generate the geometry as individual unindexed quads */
041    public static final int QUADS = 2;
042
043    /** Generate the geometry as a triangle strip array(s) */
044    public static final int TRIANGLE_STRIPS = 3;
045
046    /** Generate the geometry as a triangle fan array(s) */
047    public static final int TRIANGLE_FANS = 4;
048
049    /** Generate the geometry as indexed quads */
050    public static final int INDEXED_QUADS = 5;
051
052    /** Generate the geometry as an indexed triangle array */
053    public static final int INDEXED_TRIANGLES = 6;
054
055    /** Generate the geometry as an indexed triangle strip array */
056    public static final int INDEXED_TRIANGLE_STRIPS = 7;
057
058    /** Generate the geometry as an indexed triangle fan array */
059    public static final int INDEXED_TRIANGLE_FANS = 8;
060
061    /** Generate the geometry as a line array */
062    public static final int LINES = 9;
063
064    /** Generate the geometry as an line strip array */
065    public static final int LINE_STRIPS = 10;
066
067    /** Generate the geometry as an indexed line array */
068    public static final int INDEXED_LINES = 11;
069
070    /** Generate the geometry as an indexed line strip array */
071    public static final int INDEXED_LINE_STRIPS = 12;
072
073
074    /** Request for lighting normal data to be produced */
075    public static final int NORMAL_DATA = 0x02;
076
077    /** Request for 2D Texture coordinate data to be produced */
078    public static final int TEXTURE_2D_DATA = 0x04;
079
080    /** Request for 3D Texture coordinate data to be produced */
081    public static final int TEXTURE_3D_DATA = 0x08;
082
083    /** This is the type of geometry that you want to have made */
084    public int geometryType = 0;
085
086    /**
087     * A generator specific field that describes the type of output
088     * algorithm you would like to use for the geometry. May be ignored.
089     */
090    public int geometrySubType;
091
092    /**
093     * The attributes of the geometry you want created. This is an OR'd
094     * list of the above variables. It is not possible to generate anything
095     * without the raw geometry being computed.
096     */
097    public int geometryComponents;
098
099    /** The number of vertices stored in the coordinates array */
100    public int vertexCount;
101
102    /**
103     * Storage for coordinate information. These are stored in flat
104     * [x1, y1, z1, x2, y2, z2, ...] configuration
105     */
106    public float[] coordinates;
107
108    /**
109     * Storage for lighting normal information. This should be at least the
110     * length of the coordinates array. Data is stored in the same fashion.
111     * If normals are requested, the count is the same as vertexCount.
112     */
113    public float[] normals;
114
115    /** The number of items stored in the indexes array */
116    public int indexesCount;
117
118    /** Storage for coordinate index information if the shape type requires it. */
119    public int[] indexes;
120
121    /** The number of items stored in the strip counts */
122    public int numStrips;
123
124    /** Storage for strip counts if the shape type uses it */
125    public int[] stripCounts;
126
127    /**
128     * Texture coordinate information if requested. May be 2D or 3D depending
129     * on the requested type. If 2D the values are stored [s1, t1, s2, t2...]
130     * For 3D coordinates it is stores as [r1, s1, t1, r2, s2, t2,...]
131     */
132    public float[] textureCoordinates;
133
134    /**
135     * Colour values if using per-vertex coloring. This array will be identical
136     * in length to the coordinate array and index values match etc.
137     */
138    public float[] colors;
139
140    /**
141     * Storage for normal index information if the shape type requires it. Not
142     * used by the geometry generation classes, but may be by 3rd party software.
143     */
144    public int[] normalIndexes;
145
146    /**
147     * Storage for texture coordinate index information if the shape type
148     * requires it. Not used by the geometry generation classes, but may be by
149     * 3rd party software.
150     */
151    public int[] texCoordIndexes;
152
153    /**
154     * Storage for color index information if the shape type requires it. Not
155     * used by the geometry generation classes, but may be by 3rd party software.
156     */
157    public int[] colorIndexes;
158
159    /**
160     * Convenience method to print out all the data associated with
161     * this geometry array. Prints one vertex per line. Ignores
162     * index information.
163     */
164    public void prettyPrint()
165    {
166        NumberFormat format = NumberFormat.getInstance();
167        format.setMaximumFractionDigits(3);
168        format.setMinimumFractionDigits(3);
169
170        boolean has_3d_texture =
171            (geometryComponents & GeometryData.TEXTURE_3D_DATA) > 0;
172
173        System.out.println();
174        for(int i = 0; i < vertexCount; i++)
175        {
176            System.out.print(i);
177            System.out.print(" v: ");
178            System.out.print(format.format(coordinates[i * 3]));
179            System.out.print(' ');
180            System.out.print(format.format(coordinates[i * 3 + 1]));
181            System.out.print(' ');
182            System.out.print(format.format(coordinates[i * 3 + 2]));
183
184            if(normals != null)
185            {
186                System.out.print(", n: ");
187                System.out.print(format.format(normals[i * 3]));
188                System.out.print(' ');
189                System.out.print(format.format(normals[i * 3 + 1]));
190                System.out.print(' ');
191                System.out.print(format.format(normals[i * 3 + 2]));
192            }
193
194            if(colors != null)
195            {
196                System.out.print(", c: ");
197                System.out.print(format.format(colors[i * 3]));
198                System.out.print(' ');
199                System.out.print(format.format(colors[i * 3 + 1]));
200                System.out.print(' ');
201                System.out.print(format.format(colors[i * 3 + 2]));
202            }
203
204            if(textureCoordinates != null)
205            {
206                if(has_3d_texture)
207                {
208                    System.out.print(", t: ");
209                    System.out.print(format.format(textureCoordinates[i * 3]));
210                    System.out.print(' ');
211                    System.out.print(format.format(textureCoordinates[i * 3 + 1]));
212                    System.out.print(' ');
213                    System.out.print(format.format(textureCoordinates[i * 3 + 2]));
214                }
215                else
216                {
217                    System.out.print(", t: ");
218                    System.out.print(format.format(textureCoordinates[i * 2]));
219                    System.out.print(' ');
220                    System.out.print(format.format(textureCoordinates[i * 2 + 1]));
221                }
222
223            }
224
225            System.out.println();
226        }
227    }
228
229    /**
230     * Make a copy of this object with identical data. Does a
231     * deep copy of all data associated.
232     *
233     * @return The copy of the data
234     */
235    public Object clone()
236    {
237        GeometryData gd = new GeometryData();
238        gd.geometryType = geometryType;
239        gd.geometrySubType = geometrySubType;
240        gd.geometryComponents = geometryComponents;
241        gd.vertexCount = vertexCount;
242        if(coordinates != null)
243        {
244            gd.coordinates = new float[coordinates.length];
245            System.arraycopy(coordinates,
246                             0,
247                             gd.coordinates,
248                             0,
249                             coordinates.length);
250        }
251
252        if(normals != null)
253        {
254            gd.normals = new float[normals.length];
255            System.arraycopy(normals, 0, gd.normals, 0, normals.length);
256        }
257
258        gd.indexesCount = indexesCount;
259
260        if(indexes != null)
261        {
262            gd.indexes = new int[indexes.length];
263            System.arraycopy(indexes, 0, gd.indexes, 0, indexes.length);
264        }
265
266        gd.numStrips = numStrips;
267
268        if(stripCounts != null)
269        {
270            gd.stripCounts = new int[stripCounts.length];
271            System.arraycopy(stripCounts,
272                             0,
273                             gd.stripCounts,
274                             0,
275                             stripCounts.length);
276        }
277
278        if(textureCoordinates != null)
279        {
280            gd.textureCoordinates = new float[textureCoordinates.length];
281            System.arraycopy(textureCoordinates,
282                             0,
283                             gd.textureCoordinates,
284                             0,
285                             textureCoordinates.length);
286        }
287
288        if(colors != null)
289        {
290            gd.colors = new float[colors.length];
291            System.arraycopy(colors, 0, gd.colors, 0, colors.length);
292        }
293
294        if(normalIndexes != null)
295        {
296            gd.normalIndexes = new int[normalIndexes.length];
297            System.arraycopy(normalIndexes,
298                             0,
299                             gd.normalIndexes,
300                             0,
301                             normalIndexes.length);
302        }
303
304        if(texCoordIndexes != null)
305        {
306            gd.texCoordIndexes = new int[texCoordIndexes.length];
307            System.arraycopy(texCoordIndexes,
308                             0,
309                             gd.texCoordIndexes,
310                             0,
311                             texCoordIndexes.length);
312        }
313
314        if(colorIndexes != null)
315        {
316            gd.colorIndexes = new int[colorIndexes.length];
317            System.arraycopy(colorIndexes,
318                             0,
319                             gd.colorIndexes,
320                             0,
321                             colorIndexes.length);
322        }
323
324        return gd;
325    }
326}