001    /**
002     * PlaneElementShape.java
003     *
004     * This file is part of jndclifford package and it's distributed under the terms of the MIT license.
005     *
006     * The MIT License :
007     * -----------------
008     * Copyright (c) 2002, 2003, 2004, 2005 Pietro Brignola
009     *
010     * Permission is hereby granted, free of charge, to any person obtaining a
011     * copy of this software and associated documentation files (the "Software"),
012     * to deal in the Software without restriction, including without limitation
013     * the rights to use, copy, modify, merge, publish, distribute, sublicense,
014     * and/or sell copies of the Software, and to permit persons to whom the
015     * Software is furnished to do so, subject to the following conditions:
016     * The above copyright notice and this permission notice shall be included in
017     * all copies or substantial portions of the Software.
018     *
019     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
020     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
021     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
022     * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
023     * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
024     * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
025     * DEALINGS IN THE SOFTWARE.
026     */
027    
028    package jndclifford;
029    
030    import javax.media.j3d.Appearance;
031    import javax.media.j3d.Material;
032    import javax.media.j3d.Shape3D;
033    import javax.vecmath.Color3f;
034    import javax.vecmath.Point3f;
035    
036    import com.sun.j3d.utils.geometry.GeometryInfo;
037    import com.sun.j3d.utils.geometry.NormalGenerator;
038    
039    
040    /**
041     * <p>This class represents a directed triangle element in 3-dimensional space, non shadow face is the direct span area.</p>
042     * @version <p>0.2</p>
043     * @author <p>Realized by <a href="mailto:vassallo@csai.unipa.it">Giorgio Vassallo</a>, <a href="mailto:pietro.brignola@libero.it">Pietro Brignola</a>, May 2003.</p>
044     */
045    public class TriangleShape extends Shape3D{
046    
047            /*
048             * Constructs a representation of a directed triangle element in 3-dimensional space.
049             * Triangle element span an area that is its module and the direct face is colored.
050             * @param p1 the 1st point in n-dimensional Euclidean space.
051             * @param p2 the 2nd point in n-dimensional Euclidean space.
052             * @param p3 the 3th point in n-dimensional Euclidean space.
053             * @param color color of the plane element.
054             */
055            public TriangleShape(Point3f p1, Point3f p2, Point3f p3, Color3f color)
056            {
057                    GeometryInfo gi = new GeometryInfo(GeometryInfo.TRIANGLE_ARRAY);
058    
059                    //Setting verts
060                    float data[] = {
061                                            p1.x, p1.y, p1.z,
062                                            p2.x, p2.y, p2.z,
063                                            p3.x, p3.y, p3.z,
064                                            p3.x, p3.y, p3.z,
065                                            p2.x, p2.y, p2.z,
066                                            p1.x, p1.y, p1.z
067                    };
068                    gi.setCoordinates(data);
069    
070                    //Clamping color in the range [0,1]
071                    color.clamp(0.0f, 1.0f);
072    
073                    //Back face colors
074                    Color3f backcolor = new Color3f(color.x*0.5f, color.y*0.5f, color.z*0.5f);
075    
076                    //Setting colors
077                    float colors[] = {
078                                            color.x, color.y, color.z,
079                                            color.x, color.y, color.z,
080                                            color.x, color.y, color.z,
081                                            backcolor.x, backcolor.y, backcolor.z,
082                                            backcolor.x, backcolor.y, backcolor.z,
083                                            backcolor.x, backcolor.y, backcolor.z
084                    };
085                    gi.setColors3(colors);
086    
087                    // generate normals
088                    NormalGenerator ng = new NormalGenerator();
089                    ng.generateNormals(gi);
090    
091                    //Setting geometry
092                    this.setGeometry(gi.getGeometryArray());
093    
094                    //Setting material properties
095                    Material material = new Material();
096                    material.setSpecularColor(0.0f, 0.0f, 0.0f);
097                    material.setShininess(1.0f);
098    
099                    //Apparence
100                    Appearance appear = new Appearance();
101                    appear.setMaterial(material);
102                    //appear.setTransparencyAttributes(new TransparencyAttributes(TransparencyAttributes.NICEST, 0.15f));
103    
104                    //Setting appearance
105                    this.setAppearance(appear);
106            }
107    
108    }