Home | History | Annotate | Download | only in texture
      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 package com.jme3.texture;
     33 
     34 import com.jme3.export.InputCapsule;
     35 import com.jme3.export.JmeExporter;
     36 import com.jme3.export.JmeImporter;
     37 import com.jme3.export.OutputCapsule;
     38 import java.io.IOException;
     39 
     40 /**
     41  * @author Maarten Steur
     42  */
     43 public class Texture3D extends Texture {
     44 
     45     private WrapMode wrapS = WrapMode.EdgeClamp;
     46     private WrapMode wrapT = WrapMode.EdgeClamp;
     47     private WrapMode wrapR = WrapMode.EdgeClamp;
     48 
     49     /**
     50      * Creates a new two-dimensional texture with default attributes.
     51      */
     52     public Texture3D() {
     53         super();
     54     }
     55 
     56     /**
     57      * Creates a new three-dimensional texture using the given image.
     58      * @param img The image to use.
     59      */
     60     public Texture3D(Image img) {
     61         super();
     62         setImage(img);
     63         if (img.getFormat().isDepthFormat()) {
     64             setMagFilter(MagFilter.Nearest);
     65             setMinFilter(MinFilter.NearestNoMipMaps);
     66         }
     67     }
     68 
     69     /**
     70      * Creates a new three-dimensional texture for the purpose of offscreen
     71      * rendering.
     72      *
     73      * @see com.jme3.texture.FrameBuffer
     74      *
     75      * @param width
     76      * @param height
     77      * @param depth
     78      * @param format
     79      */
     80     public Texture3D(int width, int height, int depth, Image.Format format) {
     81         this(new Image(format, width, height, depth, null));
     82     }
     83 
     84     /**
     85      * Creates a new three-dimensional texture for the purpose of offscreen
     86      * rendering.
     87      *
     88      * @see com.jme3.texture.FrameBuffer
     89      *
     90      * @param width
     91      * @param height
     92      * @param format
     93      * @param numSamples
     94      */
     95     public Texture3D(int width, int height, int depth, int numSamples, Image.Format format) {
     96         this(new Image(format, width, height, depth, null));
     97         getImage().setMultiSamples(numSamples);
     98     }
     99 
    100     @Override
    101     public Texture createSimpleClone() {
    102         Texture3D clone = new Texture3D();
    103         createSimpleClone(clone);
    104         return clone;
    105     }
    106 
    107     @Override
    108     public Texture createSimpleClone(Texture rVal) {
    109         rVal.setWrap(WrapAxis.S, wrapS);
    110         rVal.setWrap(WrapAxis.T, wrapT);
    111         rVal.setWrap(WrapAxis.R, wrapR);
    112         return super.createSimpleClone(rVal);
    113     }
    114 
    115     /**
    116      * <code>setWrap</code> sets the wrap mode of this texture for a
    117      * particular axis.
    118      *
    119      * @param axis
    120      *            the texture axis to define a wrapmode on.
    121      * @param mode
    122      *            the wrap mode for the given axis of the texture.
    123      * @throws IllegalArgumentException
    124      *             if axis or mode are null
    125      */
    126     public void setWrap(WrapAxis axis, WrapMode mode) {
    127         if (mode == null) {
    128             throw new IllegalArgumentException("mode can not be null.");
    129         } else if (axis == null) {
    130             throw new IllegalArgumentException("axis can not be null.");
    131         }
    132         switch (axis) {
    133             case S:
    134                 this.wrapS = mode;
    135                 break;
    136             case T:
    137                 this.wrapT = mode;
    138                 break;
    139             case R:
    140                 this.wrapR = mode;
    141                 break;
    142         }
    143     }
    144 
    145     /**
    146      * <code>setWrap</code> sets the wrap mode of this texture for all axis.
    147      *
    148      * @param mode
    149      *            the wrap mode for the given axis of the texture.
    150      * @throws IllegalArgumentException
    151      *             if mode is null
    152      */
    153     public void setWrap(WrapMode mode) {
    154         if (mode == null) {
    155             throw new IllegalArgumentException("mode can not be null.");
    156         }
    157         this.wrapS = mode;
    158         this.wrapT = mode;
    159         this.wrapR = mode;
    160     }
    161 
    162     /**
    163      * <code>getWrap</code> returns the wrap mode for a given coordinate axis
    164      * on this texture.
    165      *
    166      * @param axis
    167      *            the axis to return for
    168      * @return the wrap mode of the texture.
    169      * @throws IllegalArgumentException
    170      *             if axis is null
    171      */
    172     public WrapMode getWrap(WrapAxis axis) {
    173         switch (axis) {
    174             case S:
    175                 return wrapS;
    176             case T:
    177                 return wrapT;
    178             case R:
    179                 return wrapR;
    180         }
    181         throw new IllegalArgumentException("invalid WrapAxis: " + axis);
    182     }
    183 
    184     @Override
    185     public Type getType() {
    186         return Type.ThreeDimensional;
    187     }
    188 
    189     @Override
    190     public boolean equals(Object other) {
    191         if (!(other instanceof Texture3D)) {
    192             return false;
    193         }
    194         Texture3D that = (Texture3D) other;
    195         if (this.getWrap(WrapAxis.S) != that.getWrap(WrapAxis.S)) {
    196             return false;
    197         }
    198         if (this.getWrap(WrapAxis.T) != that.getWrap(WrapAxis.T)) {
    199             return false;
    200         }
    201         if (this.getWrap(WrapAxis.R) != that.getWrap(WrapAxis.R)) {
    202             return false;
    203         }
    204         return super.equals(other);
    205     }
    206 
    207     @Override
    208     public void write(JmeExporter e) throws IOException {
    209         super.write(e);
    210         OutputCapsule capsule = e.getCapsule(this);
    211         capsule.write(wrapS, "wrapS", WrapMode.EdgeClamp);
    212         capsule.write(wrapT, "wrapT", WrapMode.EdgeClamp);
    213         capsule.write(wrapR, "wrapR", WrapMode.EdgeClamp);
    214     }
    215 
    216     @Override
    217     public void read(JmeImporter e) throws IOException {
    218         super.read(e);
    219         InputCapsule capsule = e.getCapsule(this);
    220         wrapS = capsule.readEnum("wrapS", WrapMode.class, WrapMode.EdgeClamp);
    221         wrapT = capsule.readEnum("wrapT", WrapMode.class, WrapMode.EdgeClamp);
    222         wrapR = capsule.readEnum("wrapR", WrapMode.class, WrapMode.EdgeClamp);
    223     }
    224 }