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.renderer.pass;
35 import java.io.Serializable;
36 import java.util.ArrayList;
38 import com.jme.renderer.RenderContext;
39 import com.jme.renderer.Renderer;
40 import com.jme.scene.Spatial;
41 import com.jme.scene.state.RenderState;
42 import com.jme.system.DisplaySystem;
45 * <code>Pass</code> encapsulates logic necessary for rendering one or more
46 * steps in a multipass technique.
50 * When renderPass is called, a check is first made to see if the
51 * pass isEnabled(). Then any states set on this pass are enforced via
52 * Spatial.enforceState(RenderState). This is useful for doing things such
53 * as causing this pass to be blended to a previous pass via enforcing an
54 * BlendState, etc. Next, doRender(Renderer) is called to do the actual
55 * rendering work. Finally, any enforced states set before this pass was
58 * @author Joshua Slack
59 * @version $Id: Pass.java,v 1.9 2007/08/14 13:41:40 rherlitz Exp $
61 public abstract class Pass implements Serializable {
63 private static final long serialVersionUID = -2608939114161492853L;
65 /** list of spatials registered with this pass. */
66 protected ArrayList<Spatial> spatials = new ArrayList<Spatial>();
68 /** if false, pass will not be updated or rendered. */
69 protected boolean enabled = true;
71 /** offset params to use to differentiate multiple passes of the same scene in the zbuffer. */
72 protected float zFactor;
73 protected float zOffset;
76 * RenderStates registered with this pass - if a given state is not null it
77 * overrides the corresponding state set during rendering.
79 protected RenderState[] passStates = new RenderState[RenderState.StateType.values().length];
81 /** a place to internally save previous states setup before rendering this pass */
82 protected RenderState[] savedStates = new RenderState[RenderState.StateType.values().length];
84 protected RenderContext<?> context = null;
86 /** if enabled, set the states for this pass and then render. */
87 public final void renderPass(Renderer r) {
89 context = DisplaySystem.getDisplaySystem().getCurrentContext();
91 r.setPolygonOffset(zFactor, zOffset);
93 r.clearPolygonOffset();
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
108 public void setPassState(RenderState state) {
110 passStates[state.getStateType().ordinal()] = state;
114 * Returns the requested RenderState that this Pass currently has set or
115 * null if none is set.
118 * the renderstate type to retrieve
119 * @return a renderstate at the given position or null
120 * @deprecated As of 2.0, use {@link #getRenderState(com.jme.scene.state.RenderState.StateType)} instead.
122 public RenderState getRenderState(int type) {
123 return passStates != null ? passStates[type] : null;
127 * Returns the requested RenderState that this Pass currently has set or
128 * null if none is set.
131 * the renderstate type to retrieve
132 * @return a renderstate at the given position or null
134 public RenderState getRenderState(RenderState.StateType type) {
136 return passStates != null ? passStates[type.ordinal()] : null;
140 * Clears an enforced render state index by setting it to null. This allows
141 * object specific states to be used.
143 * @param renderStateType
144 * The type of RenderState to clear enforcement on.
145 * @deprecated As of 2.0, use {@link #clearPassState(com.jme.scene.state.RenderState.StateType)} instead.
147 public void clearPassState(int renderStateType) {
148 passStates[renderStateType] = null;
152 * Clears an enforced render state by setting it to null. This allows
153 * object specific states to be used.
156 * The type of RenderState to clear enforcement on.
158 public void clearPassState(RenderState.StateType type) {
159 passStates[type.ordinal()] = null;
163 * sets all enforced states to null.
165 * @see RenderContext#clearEnforcedState(int)
167 public void clearPassStates() {
168 for (int i = 0; i < passStates.length; i++)
169 passStates[i] = null;
172 protected void applyPassStates() {
173 for (int x = RenderState.StateType.values().length; --x >= 0;) {
174 if (passStates[x] != null) {
175 savedStates[x] = context.enforcedStateList[x];
176 context.enforcedStateList[x] = passStates[x];
181 protected abstract void doRender(Renderer r);
183 protected void resetOldStates() {
184 for (int x = RenderState.StateType.values().length; --x >= 0;) {
185 if (passStates[x] != null) {
186 context.enforcedStateList[x] = savedStates[x];
191 /** if enabled, call doUpdate to update information for this pass. */
192 public final void updatePass(float tpf) {
193 if (!enabled) return;
197 protected void doUpdate(float tpf) {
201 public void add(Spatial toAdd) {
205 public Spatial get(int index) {
206 return spatials.get(index);
209 public boolean contains(Spatial s) {
210 return spatials.contains(s);
213 public boolean remove(Spatial toRemove) {
214 return spatials.remove(toRemove);
218 public void removeAll() {
223 return spatials.size();
227 * @return Returns the enabled.
229 public boolean isEnabled() {
234 * @param enabled The enabled to set.
236 public void setEnabled(boolean enabled) {
237 this.enabled = enabled;
241 * @return Returns the zFactor.
243 public float getZFactor() {
248 * Sets the polygon offset param - factor - for this Pass.
251 * The zFactor to set.
253 public void setZFactor(float factor) {
258 * @return Returns the zOffset.
260 public float getZOffset() {
265 * Sets the polygon offset param - offset - for this Pass.
268 * The zOffset to set.
270 public void setZOffset(float offset) {
274 public void cleanUp() {