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 * Describes a cubemap texture.
43 * The image specified by setImage must contain 6 data units,
44 * each data contains a 2D image representing a cube's face.
45 * The slices are specified in this order:<br/>
46 * <br/>
47 * 0 => Positive X (+x)<br/>
48 * 1 => Negative X (-x)<br/>
49 * 2 => Positive Y (+y)<br/>
50 * 3 => Negative Y (-y)<br/>
51 * 4 => Positive Z (+z)<br/>
52 * 5 => Negative Z (-z)<br/>
53 *
54 * @author Joshua Slack
55 */
56public class TextureCubeMap extends Texture {
57
58    private WrapMode wrapS = WrapMode.EdgeClamp;
59    private WrapMode wrapT = WrapMode.EdgeClamp;
60    private WrapMode wrapR = WrapMode.EdgeClamp;
61
62    /**
63     * Face of the Cubemap as described by its directional offset from the
64     * origin.
65     */
66//    public enum Face {
67//        PositiveX, NegativeX, PositiveY, NegativeY, PositiveZ, NegativeZ;
68//    }
69
70    public TextureCubeMap(){
71        super();
72    }
73
74    public TextureCubeMap(Image img){
75        super();
76        setImage(img);
77    }
78
79    public Texture createSimpleClone() {
80        return createSimpleClone(new TextureCubeMap());
81    }
82
83    @Override
84    public Texture createSimpleClone(Texture rVal) {
85        rVal.setWrap(WrapAxis.S, wrapS);
86        rVal.setWrap(WrapAxis.T, wrapT);
87        rVal.setWrap(WrapAxis.R, wrapR);
88        return super.createSimpleClone(rVal);
89    }
90
91    /**
92     * <code>setWrap</code> sets the wrap mode of this texture for a
93     * particular axis.
94     *
95     * @param axis
96     *            the texture axis to define a wrapmode on.
97     * @param mode
98     *            the wrap mode for the given axis of the texture.
99     * @throws IllegalArgumentException
100     *             if axis or mode are null
101     */
102    public void setWrap(WrapAxis axis, WrapMode mode) {
103        if (mode == null) {
104            throw new IllegalArgumentException("mode can not be null.");
105        } else if (axis == null) {
106            throw new IllegalArgumentException("axis can not be null.");
107        }
108        switch (axis) {
109            case S:
110                this.wrapS = mode;
111                break;
112            case T:
113                this.wrapT = mode;
114                break;
115            case R:
116                this.wrapR = mode;
117                break;
118        }
119    }
120
121    /**
122     * <code>setWrap</code> sets the wrap mode of this texture for all axis.
123     *
124     * @param mode
125     *            the wrap mode for the given axis of the texture.
126     * @throws IllegalArgumentException
127     *             if mode is null
128     */
129    public void setWrap(WrapMode mode) {
130        if (mode == null) {
131            throw new IllegalArgumentException("mode can not be null.");
132        }
133        this.wrapS = mode;
134        this.wrapT = mode;
135        this.wrapR = mode;
136    }
137
138    /**
139     * <code>getWrap</code> returns the wrap mode for a given coordinate axis
140     * on this texture.
141     *
142     * @param axis
143     *            the axis to return for
144     * @return the wrap mode of the texture.
145     * @throws IllegalArgumentException
146     *             if axis is null
147     */
148    public WrapMode getWrap(WrapAxis axis) {
149        switch (axis) {
150            case S:
151                return wrapS;
152            case T:
153                return wrapT;
154            case R:
155                return wrapR;
156        }
157        throw new IllegalArgumentException("invalid WrapAxis: " + axis);
158    }
159
160    @Override
161    public Type getType() {
162        return Type.CubeMap;
163    }
164
165    @Override
166    public boolean equals(Object other) {
167        if (!(other instanceof TextureCubeMap)) {
168            return false;
169        }
170        TextureCubeMap that = (TextureCubeMap) other;
171        if (this.getWrap(WrapAxis.S) != that.getWrap(WrapAxis.S))
172            return false;
173        if (this.getWrap(WrapAxis.T) != that.getWrap(WrapAxis.T))
174            return false;
175        if (this.getWrap(WrapAxis.R) != that.getWrap(WrapAxis.R))
176            return false;
177        return super.equals(other);
178    }
179
180    @Override
181    public int hashCode() {
182        int hash = super.hashCode();
183        hash = 53 * hash + (this.wrapS != null ? this.wrapS.hashCode() : 0);
184        hash = 53 * hash + (this.wrapT != null ? this.wrapT.hashCode() : 0);
185        hash = 53 * hash + (this.wrapR != null ? this.wrapR.hashCode() : 0);
186        return hash;
187    }
188
189    @Override
190    public void write(JmeExporter e) throws IOException {
191        super.write(e);
192        OutputCapsule capsule = e.getCapsule(this);
193        capsule.write(wrapS, "wrapS", WrapMode.EdgeClamp);
194        capsule.write(wrapT, "wrapT", WrapMode.EdgeClamp);
195        capsule.write(wrapR, "wrapR", WrapMode.EdgeClamp);
196    }
197
198    @Override
199    public void read(JmeImporter e) throws IOException {
200        super.read(e);
201        InputCapsule capsule = e.getCapsule(this);
202        wrapS = capsule.readEnum("wrapS", WrapMode.class, WrapMode.EdgeClamp);
203        wrapT = capsule.readEnum("wrapT", WrapMode.class, WrapMode.EdgeClamp);
204        wrapR = capsule.readEnum("wrapR", WrapMode.class, WrapMode.EdgeClamp);
205    }
206}
207