Home | History | Annotate | Download | only in shape
      1 /*
      2  * Copyright (c) 2009-2010 jMonkeyEngine
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are
      7  * met:
      8  *
      9  * * Redistributions of source code must retain the above copyright
     10  *   notice, this list of conditions and the following disclaimer.
     11  *
     12  * * Redistributions in binary form must reproduce the above copyright
     13  *   notice, this list of conditions and the following disclaimer in the
     14  *   documentation and/or other materials provided with the distribution.
     15  *
     16  * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
     17  *   may be used to endorse or promote products derived from this software
     18  *   without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 package com.jme3.scene.shape;
     34 
     35 import com.jme3.scene.Mesh;
     36 import com.jme3.scene.VertexBuffer.Type;
     37 
     38 /**
     39  * <code>Quad</code> represents a rectangular plane in space
     40  * defined by 4 vertices. The quad's lower-left side is contained
     41  * at the local space origin (0, 0, 0), while the upper-right
     42  * side is located at the width/height coordinates (width, height, 0).
     43  *
     44  * @author Kirill Vainer
     45  */
     46 public class Quad extends Mesh {
     47 
     48     private float width;
     49     private float height;
     50 
     51     /**
     52      * Serialization only. Do not use.
     53      */
     54     public Quad(){
     55     }
     56 
     57     /**
     58      * Create a quad with the given width and height. The quad
     59      * is always created in the XY plane.
     60      *
     61      * @param width The X extent or width
     62      * @param height The Y extent or width
     63      */
     64     public Quad(float width, float height){
     65         updateGeometry(width, height);
     66     }
     67 
     68     /**
     69      * Create a quad with the given width and height. The quad
     70      * is always created in the XY plane.
     71      *
     72      * @param width The X extent or width
     73      * @param height The Y extent or width
     74      * @param flipCoords If true, the texture coordinates will be flipped
     75      * along the Y axis.
     76      */
     77     public Quad(float width, float height, boolean flipCoords){
     78         updateGeometry(width, height, flipCoords);
     79     }
     80 
     81     public float getHeight() {
     82         return height;
     83     }
     84 
     85     public float getWidth() {
     86         return width;
     87     }
     88 
     89     public void updateGeometry(float width, float height){
     90         updateGeometry(width, height, false);
     91     }
     92 
     93     public void updateGeometry(float width, float height, boolean flipCoords) {
     94         this.width = width;
     95         this.height = height;
     96         setBuffer(Type.Position, 3, new float[]{0,      0,      0,
     97                                                 width,  0,      0,
     98                                                 width,  height, 0,
     99                                                 0,      height, 0
    100                                                 });
    101 
    102 
    103         if (flipCoords){
    104             setBuffer(Type.TexCoord, 2, new float[]{0, 1,
    105                                                     1, 1,
    106                                                     1, 0,
    107                                                     0, 0});
    108         }else{
    109             setBuffer(Type.TexCoord, 2, new float[]{0, 0,
    110                                                     1, 0,
    111                                                     1, 1,
    112                                                     0, 1});
    113         }
    114         setBuffer(Type.Normal, 3, new float[]{0, 0, 1,
    115                                               0, 0, 1,
    116                                               0, 0, 1,
    117                                               0, 0, 1});
    118         if (height < 0){
    119             setBuffer(Type.Index, 3, new short[]{0, 2, 1,
    120                                                  0, 3, 2});
    121         }else{
    122             setBuffer(Type.Index, 3, new short[]{0, 1, 2,
    123                                                  0, 2, 3});
    124         }
    125 
    126         updateBound();
    127     }
    128 
    129 
    130 }
    131