2 * Copyright (c) 2003-2009 jMonkeyEngine
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
33 package com.jme.scene;
35 import java.io.IOException;
36 import java.io.Serializable;
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;
47 /** PassNodeState Creator: rikard.herlitz, 2007-maj-10 */
48 public class PassNodeState implements Savable, Serializable {
50 private static final long serialVersionUID = -2190443394368566111L;
52 /** if false, pass will not be updated or rendered. */
53 protected boolean enabled = true;
56 * offset params to use to differentiate multiple passes of the same scene
59 protected float zFactor;
60 protected float zOffset;
63 * RenderStates registered with this pass - if a given state is not null it
64 * overrides the corresponding state set during rendering.
66 protected RenderState[] passStates = new RenderState[RenderState.StateType.values().length];
69 * a place to internally save previous states setup before rendering this
72 protected RenderState[] savedStates = new RenderState[RenderState.StateType.values().length];
75 * Applies all currently set renderstates and z offset parameters to the
81 public void applyPassNodeState(Renderer r, RenderContext<?> context) {
82 applyPassStates(context);
83 r.setPolygonOffset(zFactor, zOffset);
87 * Resets currently set renderstates and z offset parameters on the supplied
93 public void resetPassNodeStates(Renderer r, RenderContext<?> context) {
94 r.clearPolygonOffset();
95 resetOldStates(context);
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
105 * @param state state to enforce
107 public void setPassState(RenderState state) {
109 passStates[state.getStateType().ordinal()] = state;
113 * @param renderStateType
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.
118 public RenderState getPassState(int renderStateType) {
119 return passStates[renderStateType];
123 * Returns the {@link RenderState} of the given type.
127 * @return the {@link RenderState} enforced for a given state type, or null if none.
129 public RenderState getPassState(RenderState.StateType type) {
131 return passStates[type.ordinal()];
135 * Clears an enforced render state index by setting it to null. This allows
136 * object specific states to be used.
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.
141 public void clearPassState(int renderStateType) {
143 passStates[renderStateType] = null;
147 * Clears an enforced render state by setting it to null. This allows
148 * object specific states to be used.
150 * @param type The type of {@link RenderState} to clear enforcement on.
152 public void clearPassState(RenderState.StateType type) {
154 passStates[type.ordinal()] = null;
158 * sets all enforced states to null.
160 * @see RenderContext#clearEnforcedState(int)
162 public void clearPassStates() {
163 for (int i = 0; i < passStates.length; i++) {
164 passStates[i] = null;
169 * Applies all currently set renderstates to the supplied context
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];
183 * Resets all renderstates on the supplied context
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];
195 /** @return Returns the enabled. */
196 public boolean isEnabled() {
200 /** @param enabled The enabled to set. */
201 public void setEnabled(boolean enabled) {
202 this.enabled = enabled;
205 /** @return Returns the zFactor. */
206 public float getZFactor() {
211 * Sets the polygon offset param - factor - for this Pass.
213 * @param factor The zFactor to set.
215 public void setZFactor(float factor) {
219 /** @return Returns the zOffset. */
220 public float getZOffset() {
225 * Sets the polygon offset param - offset - for this Pass.
227 * @param offset The zOffset to set.
229 public void setZOffset(float offset) {
233 public Class<?> getClassTag() {
234 return this.getClass();
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);
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];
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];