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