OSDN Git Service

Set optimal mime types and executable settings.
[mikumikustudio/MikuMikuStudio.git] / src / com / jme / scene / state / ClipState.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.state;
34
35 import java.io.IOException;
36
37 import com.jme.util.export.InputCapsule;
38 import com.jme.util.export.JMEExporter;
39 import com.jme.util.export.JMEImporter;
40 import com.jme.util.export.OutputCapsule;
41
42 /**
43  * <code>ClipState</code> specifies a plane to test for clipping of the nodes. This can be used to
44  * take "slices" out of geometric objects. ClipPlane can add an additional (to the normal frustum planes) 
45  * six planes to clip against.
46  * @author Joshua Slack
47  * @version $Id: $
48  */
49 public abstract class ClipState extends RenderState {
50     public static final int CLIP_PLANE0 = 0;
51
52     public static final int CLIP_PLANE1 = 1;
53
54     public static final int CLIP_PLANE2 = 2;
55
56     public static final int CLIP_PLANE3 = 3;
57
58     public static final int CLIP_PLANE4 = 4;
59
60     public static final int CLIP_PLANE5 = 5;
61
62     public static final int MAX_CLIP_PLANES = 6;
63
64     protected boolean[] enabledClipPlanes = new boolean[MAX_CLIP_PLANES];
65
66     protected double[][] planeEquations = new double[MAX_CLIP_PLANES][4];
67
68     /**
69      * <code>getType</code> returns RenderState.RS_CLIP
70      * 
71      * @return RenderState.RS_CLIP
72      * @see RenderState#getType()
73      * @deprecated As of 2.0, use {@link #getStateType()} instead.
74      */
75     public int getType() {
76         return RS_CLIP;
77     }
78
79     /**
80      * <code>getStateType</code> returns {@link RenderState.StateType#Clip}
81      * 
82      * @return {@link RenderState.StateType#Clip}
83      * @see RenderState#getStateType()
84      */
85     public StateType getStateType() {
86         
87         return StateType.Clip;
88     }
89
90     /**
91      * Enables/disables a specific clip plane
92      * 
93      * @param planeIndex
94      *            Plane to enable/disable (CLIP_PLANE0-CLIP_PLANE5)
95      * @param enabled
96      *            true/false
97      */
98     public void setEnableClipPlane(int planeIndex, boolean enabled) {
99         if (planeIndex < 0 || planeIndex >= MAX_CLIP_PLANES) {
100             return;
101         }
102
103         enabledClipPlanes[planeIndex] = enabled;
104         setNeedsRefresh(true);
105     }
106
107     /**
108      * Sets plane equation for a specific clip plane
109      * 
110      * @param planeIndex
111      *            Plane to set equation for (CLIP_PLANE0-CLIP_PLANE5)
112      * @param clipX
113      *            plane x variable
114      * @param clipY
115      *            plane y variable
116      * @param clipZ
117      *            plane z variable
118      * @param clipW
119      *            plane w variable
120      */
121     public void setClipPlaneEquation(int planeIndex, double clipX,
122             double clipY, double clipZ, double clipW) {
123         if (planeIndex < 0 || planeIndex >= MAX_CLIP_PLANES) {
124             return;
125         }
126
127         planeEquations[planeIndex][0] = clipX;
128         planeEquations[planeIndex][1] = clipY;
129         planeEquations[planeIndex][2] = clipZ;
130         planeEquations[planeIndex][3] = clipW;
131         setNeedsRefresh(true);
132     }
133
134     /**
135      * @param index plane to check
136      * @return true if given clip plane is enabled
137      */
138     public boolean getPlaneEnabled(int index) {
139         return enabledClipPlanes[index];
140     }
141
142     public double getPlaneEq(int plane, int eqIndex) {
143         return planeEquations[plane][eqIndex];
144     }
145     public void setPlaneEq(int plane, int eqIndex, double value) {
146         planeEquations[plane][eqIndex] = value;
147         setNeedsRefresh(true);
148     }
149     
150     public void write(JMEExporter e) throws IOException {
151         super.write(e);
152         OutputCapsule capsule = e.getCapsule(this);
153         capsule.write(enabledClipPlanes, "enabledClipPlanes", new boolean[MAX_CLIP_PLANES]);
154         capsule.write(planeEquations, "planeEquations", new double[MAX_CLIP_PLANES][4]);
155     }
156
157     public void read(JMEImporter e) throws IOException {
158         super.read(e);
159         InputCapsule capsule = e.getCapsule(this);
160         enabledClipPlanes = capsule.readBooleanArray("enabledClipPlanes", new boolean[MAX_CLIP_PLANES]);
161         planeEquations = capsule.readDoubleArray2D("planeEquations", new double[MAX_CLIP_PLANES][4]);
162     }
163     
164     public Class getClassTag() {
165         return ClipState.class;
166     }
167 }