OSDN Git Service

Set optimal mime types and executable settings.
[mikumikustudio/MikuMikuStudio.git] / src / com / jme / scene / PassNodeState.java
1 /*
2  * Copyright (c) 2003-2009 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.jme.scene;
34
35 import java.io.IOException;
36 import java.io.Serializable;
37
38 import com.jme.renderer.RenderContext;
39 import com.jme.renderer.Renderer;
40 import com.jme.scene.state.RenderState;
41 import com.jme.util.export.InputCapsule;
42 import com.jme.util.export.JMEExporter;
43 import com.jme.util.export.JMEImporter;
44 import com.jme.util.export.OutputCapsule;
45 import com.jme.util.export.Savable;
46
47 /** PassNodeState Creator: rikard.herlitz, 2007-maj-10 */
48 public class PassNodeState implements Savable, Serializable {
49
50         private static final long serialVersionUID = -2190443394368566111L;
51
52         /** if false, pass will not be updated or rendered. */
53     protected boolean enabled = true;
54
55     /**
56      * offset params to use to differentiate multiple passes of the same scene
57      * in the zbuffer.
58      */
59     protected float zFactor;
60     protected float zOffset;
61
62     /**
63      * RenderStates registered with this pass - if a given state is not null it
64      * overrides the corresponding state set during rendering.
65      */
66     protected RenderState[] passStates = new RenderState[RenderState.StateType.values().length];
67
68     /**
69      * a place to internally save previous states setup before rendering this
70      * pass
71      */
72     protected RenderState[] savedStates = new RenderState[RenderState.StateType.values().length];
73
74     /**
75      * Applies all currently set renderstates and z offset parameters to the
76      * supplied context
77      *
78      * @param r
79      * @param context
80      */
81     public void applyPassNodeState(Renderer r, RenderContext<?> context) {
82         applyPassStates(context);
83         r.setPolygonOffset(zFactor, zOffset);
84     }
85
86     /**
87      * Resets currently set renderstates and z offset parameters on the supplied
88      * context
89      *
90      * @param r
91      * @param context
92      */
93     public void resetPassNodeStates(Renderer r, RenderContext<?> context) {
94         r.clearPolygonOffset();
95         resetOldStates(context);
96     }
97
98     /**
99      * Enforce a particular state. In other words, the given state will override
100      * any state of the same type set on a scene object. Remember to clear the
101      * state when done enforcing. Very useful for multipass techniques where
102      * multiple sets of states need to be applied to a scenegraph drawn multiple
103      * times.
104      *
105      * @param state state to enforce
106      */
107     public void setPassState(RenderState state) {
108         
109         passStates[state.getStateType().ordinal()] = state;
110     }
111
112         /**
113          * @param renderStateType
114          *            the type to query
115          * @return the state enforced for a give state type, or null if none.
116          * @deprecated As of 2.0, use {@link #getPassState(com.jme.scene.state.RenderState.StateType)} instead.
117          */
118     public RenderState getPassState(int renderStateType) {
119         return passStates[renderStateType];
120     }
121
122         /**
123          * Returns the {@link RenderState} of the given type.
124          * 
125          * @param type
126          *            the type to query
127          * @return the {@link RenderState} enforced for a given state type, or null if none.
128          */
129     public RenderState getPassState(RenderState.StateType type) {
130         
131         return passStates[type.ordinal()];
132     }
133
134     /**
135      * Clears an enforced render state index by setting it to null. This allows
136      * object specific states to be used.
137      *
138      * @param renderStateType The type of RenderState to clear enforcement on.
139          * @deprecated As of 2.0, use {@link #clearPassState(com.jme.scene.state.RenderState.StateType)} instead.
140      */
141     public void clearPassState(int renderStateType) {
142         
143         passStates[renderStateType] = null;
144     }
145
146     /**
147      * Clears an enforced render state by setting it to null. This allows
148      * object specific states to be used.
149      *
150      * @param type The type of {@link RenderState} to clear enforcement on.
151      */
152     public void clearPassState(RenderState.StateType type) {
153         
154         passStates[type.ordinal()] = null;
155     }
156
157     /**
158      * sets all enforced states to null.
159      *
160      * @see RenderContext#clearEnforcedState(int)
161      */
162     public void clearPassStates() {
163         for (int i = 0; i < passStates.length; i++) {
164             passStates[i] = null;
165         }
166     }
167
168     /**
169      * Applies all currently set renderstates to the supplied context
170      *
171      * @param context
172      */
173     protected void applyPassStates(RenderContext<?> context) {
174         for (int x = RenderState.StateType.values().length; --x >= 0;) {
175             if (passStates[x] != null) {
176                 savedStates[x] = context.enforcedStateList[x];
177                 context.enforcedStateList[x] = passStates[x];
178             }
179         }
180     }
181
182     /**
183      * Resets all renderstates on the supplied context
184      *
185      * @param context
186      */
187     protected void resetOldStates(RenderContext<?> context) {
188         for (int x = RenderState.StateType.values().length; --x >= 0;) {
189             if (passStates[x] != null) {
190                 context.enforcedStateList[x] = savedStates[x];
191             }
192         }
193     }
194
195     /** @return Returns the enabled. */
196     public boolean isEnabled() {
197         return enabled;
198     }
199
200     /** @param enabled The enabled to set. */
201     public void setEnabled(boolean enabled) {
202         this.enabled = enabled;
203     }
204
205     /** @return Returns the zFactor. */
206     public float getZFactor() {
207         return zFactor;
208     }
209
210     /**
211      * Sets the polygon offset param - factor - for this Pass.
212      *
213      * @param factor The zFactor to set.
214      */
215     public void setZFactor(float factor) {
216         zFactor = factor;
217     }
218
219     /** @return Returns the zOffset. */
220     public float getZOffset() {
221         return zOffset;
222     }
223
224     /**
225      * Sets the polygon offset param - offset - for this Pass.
226      *
227      * @param offset The zOffset to set.
228      */
229     public void setZOffset(float offset) {
230         zOffset = offset;
231         }
232
233     public Class<?> getClassTag() {
234         return this.getClass();
235     }
236
237         public void write(JMEExporter e) throws IOException {
238                 OutputCapsule oc = e.getCapsule(this);
239                 oc.write(enabled, "enabled", true);
240                 oc.write(zFactor, "zFactor", 0);
241                 oc.write(zOffset, "zOffset", 0);
242                 oc.write(passStates, "passStates", null);
243                 oc.write(savedStates, "savedStates", null);
244         }
245
246         public void read(JMEImporter e) throws IOException {
247                 InputCapsule ic = e.getCapsule(this);
248                 enabled = ic.readBoolean("enabled", true);
249                 zFactor = ic.readFloat("zFactor", 0);
250                 zOffset = ic.readFloat("zOffset", 0);
251                 Savable[] temp = ic.readSavableArray("passStates", null);
252                 // TODO: Perhaps this should be redone to use the state type to place it
253                 // in the right spot in the array?
254                 passStates = new RenderState[temp.length];
255                 for (int i = 0; i < temp.length; i++) {
256                         passStates[i] = (RenderState) temp[i];
257                 }
258                 temp = ic.readSavableArray("savedStates", null);
259                 savedStates = new RenderState[temp.length];
260                 for (int i = 0; i < temp.length; i++) {
261                         savedStates[i] = (RenderState) temp[i];
262                 }
263         }
264 }