diff options
Diffstat (limited to 'src/net/java/joglutils/msg/nodes')
-rw-r--r-- | src/net/java/joglutils/msg/nodes/Blend.java | 15 | ||||
-rw-r--r-- | src/net/java/joglutils/msg/nodes/Camera.java | 19 | ||||
-rw-r--r-- | src/net/java/joglutils/msg/nodes/Color4.java | 10 | ||||
-rw-r--r-- | src/net/java/joglutils/msg/nodes/Coordinate3.java | 10 | ||||
-rw-r--r-- | src/net/java/joglutils/msg/nodes/IndexedTriangleSet.java | 2 | ||||
-rw-r--r-- | src/net/java/joglutils/msg/nodes/Node.java | 14 | ||||
-rw-r--r-- | src/net/java/joglutils/msg/nodes/PerspectiveCamera.java | 8 | ||||
-rw-r--r-- | src/net/java/joglutils/msg/nodes/Separator.java | 11 | ||||
-rw-r--r-- | src/net/java/joglutils/msg/nodes/Texture2.java | 10 | ||||
-rw-r--r-- | src/net/java/joglutils/msg/nodes/TextureCoordinate2.java | 10 | ||||
-rw-r--r-- | src/net/java/joglutils/msg/nodes/Transform.java | 13 | ||||
-rw-r--r-- | src/net/java/joglutils/msg/nodes/TriangleSet.java | 70 |
12 files changed, 172 insertions, 20 deletions
diff --git a/src/net/java/joglutils/msg/nodes/Blend.java b/src/net/java/joglutils/msg/nodes/Blend.java index 18cd5a6..af95e00 100644 --- a/src/net/java/joglutils/msg/nodes/Blend.java +++ b/src/net/java/joglutils/msg/nodes/Blend.java @@ -38,6 +38,7 @@ package net.java.joglutils.msg.nodes; import net.java.joglutils.msg.actions.*; +import net.java.joglutils.msg.elements.*; import net.java.joglutils.msg.math.*; /** Provides control over OpenGL blending modes. */ @@ -49,6 +50,11 @@ public class Blend extends Node { private int destFunc = ZERO; private int blendEquation = FUNC_ADD; + static { + // Enable the elements this node affects for known actions + GLBlendElement.enable(GLRenderAction.getDefaultState()); + } + /** One of the blend functions. See the OpenGL documentation for glBlendFunc for more details. */ public static final int ZERO = 1; /** One of the blend functions. See the OpenGL documentation for glBlendFunc for more details. */ @@ -163,6 +169,13 @@ public class Blend extends Node { } public void doAction(Action action) { - action.visit(this); + if (BlendElement.isEnabled(action.getState())) { + BlendElement.set(action.getState(), + getEnabled(), + getBlendColor(), + getSourceFunc(), + getDestFunc(), + getBlendEquation()); + } } } diff --git a/src/net/java/joglutils/msg/nodes/Camera.java b/src/net/java/joglutils/msg/nodes/Camera.java index 9d1f965..5d405fe 100644 --- a/src/net/java/joglutils/msg/nodes/Camera.java +++ b/src/net/java/joglutils/msg/nodes/Camera.java @@ -37,6 +37,8 @@ package net.java.joglutils.msg.nodes; +import net.java.joglutils.msg.actions.*; +import net.java.joglutils.msg.elements.*; import net.java.joglutils.msg.math.*; /** Represents a camera which is used to view the scene. The camera @@ -62,6 +64,14 @@ public abstract class Camera extends Node { protected Mat4f projMatrix; protected Mat4f viewMatrix; + static { + // Enable the elements this node affects for known actions + // Note that all of these elements are interdependent + GLModelMatrixElement .enable(GLRenderAction.getDefaultState()); + GLProjectionMatrixElement .enable(GLRenderAction.getDefaultState()); + GLViewingMatrixElement .enable(GLRenderAction.getDefaultState()); + } + public Camera() { position = new Vec3f(0, 0, 1); orientation = new Rotf(); @@ -123,4 +133,13 @@ public abstract class Camera extends Node { /** Returns the projection matrix associated with this camera's parameters. */ public abstract Mat4f getProjectionMatrix(); + + public void doAction(Action action) { + if (ViewingMatrixElement.isEnabled(action.getState())) { + ViewingMatrixElement.set(action.getState(), getViewingMatrix()); + } + if (ProjectionMatrixElement.isEnabled(action.getState())) { + ProjectionMatrixElement.set(action.getState(), getProjectionMatrix()); + } + } } diff --git a/src/net/java/joglutils/msg/nodes/Color4.java b/src/net/java/joglutils/msg/nodes/Color4.java index d6568e3..8d6696f 100644 --- a/src/net/java/joglutils/msg/nodes/Color4.java +++ b/src/net/java/joglutils/msg/nodes/Color4.java @@ -38,6 +38,7 @@ package net.java.joglutils.msg.nodes; import net.java.joglutils.msg.actions.*; +import net.java.joglutils.msg.elements.*; import net.java.joglutils.msg.collections.*; /** Represents a set of 4-valued colors which are applied on a @@ -55,6 +56,11 @@ import net.java.joglutils.msg.collections.*; public class Color4 extends Node { private Vec4fCollection data; + static { + // Enable the elements this node affects for known actions + GLColorElement.enable(GLRenderAction.getDefaultState()); + } + // private int colorBinding = AMBIENT_AND_DIFFUSE; /****** @@ -90,6 +96,8 @@ public class Color4 extends Node { } public void doAction(Action action) { - action.visit(this); + if (ColorElement.isEnabled(action.getState())) { + ColorElement.set(action.getState(), getData().getData()); + } } } diff --git a/src/net/java/joglutils/msg/nodes/Coordinate3.java b/src/net/java/joglutils/msg/nodes/Coordinate3.java index 4e2b94b..e8e2631 100644 --- a/src/net/java/joglutils/msg/nodes/Coordinate3.java +++ b/src/net/java/joglutils/msg/nodes/Coordinate3.java @@ -38,6 +38,7 @@ package net.java.joglutils.msg.nodes; import net.java.joglutils.msg.actions.*; +import net.java.joglutils.msg.elements.*; import net.java.joglutils.msg.collections.*; /** Represents a set of 3-dimensional vertices which can be assembled @@ -46,6 +47,11 @@ import net.java.joglutils.msg.collections.*; public class Coordinate3 extends Node { private Vec3fCollection data; + static { + // Enable the elements this node affects for known actions + GLCoordinateElement.enable(GLRenderAction.getDefaultState()); + } + /** Sets the coordinate data in this node. */ public void setData(Vec3fCollection data) { this.data = data; @@ -57,6 +63,8 @@ public class Coordinate3 extends Node { } public void doAction(Action action) { - action.visit(this); + if (CoordinateElement.isEnabled(action.getState())) { + CoordinateElement.set(action.getState(), getData().getData()); + } } } diff --git a/src/net/java/joglutils/msg/nodes/IndexedTriangleSet.java b/src/net/java/joglutils/msg/nodes/IndexedTriangleSet.java index d2932c8..5e45367 100644 --- a/src/net/java/joglutils/msg/nodes/IndexedTriangleSet.java +++ b/src/net/java/joglutils/msg/nodes/IndexedTriangleSet.java @@ -62,6 +62,6 @@ public class IndexedTriangleSet extends Node { } public void doAction(Action action) { - action.visit(this); + throw new RuntimeException("Not yet implemented"); } } diff --git a/src/net/java/joglutils/msg/nodes/Node.java b/src/net/java/joglutils/msg/nodes/Node.java index 6fc4eb9..e9eefc4 100644 --- a/src/net/java/joglutils/msg/nodes/Node.java +++ b/src/net/java/joglutils/msg/nodes/Node.java @@ -42,11 +42,13 @@ import net.java.joglutils.msg.actions.*; /** The base class for all nodes in the scene graph. */ public class Node { + /** Performs the "typical" operation for this node when an action is + applied to it. The default implementation does nothing. */ + public void doAction(Action action) {} - /** This is an internal API not intended for public use. To apply an - action to the scene graph or a portion of the scene graph, use - {@link net.java.joglutils.msg.actions.Action#apply - Action.apply}. */ - public void doAction(Action action) { - } + /** Support for the built-in GLRenderAction. Note that supplying + virtual methods in Node subclasses to support various actions is + not required due to the framework supporting action methods, but + for built-in actions it may make it simpler. */ + public void render(GLRenderAction action) { doAction(action); } } diff --git a/src/net/java/joglutils/msg/nodes/PerspectiveCamera.java b/src/net/java/joglutils/msg/nodes/PerspectiveCamera.java index b72507a..50c646b 100644 --- a/src/net/java/joglutils/msg/nodes/PerspectiveCamera.java +++ b/src/net/java/joglutils/msg/nodes/PerspectiveCamera.java @@ -91,7 +91,11 @@ public class PerspectiveCamera extends Camera { return vertFOVScale * DEFAULT_HEIGHT_ANGLE; } - public void doAction(Action action) { - action.visit(this); + public void render(GLRenderAction action) { + // FIXME: unclear whether we should be doing this, or whether we + // should have a mechanism which doesn't require mutation of the + // camera + setAspectRatio(action.getCurAspectRatio()); + doAction(action); } } diff --git a/src/net/java/joglutils/msg/nodes/Separator.java b/src/net/java/joglutils/msg/nodes/Separator.java index c60ba45..fdebece 100644 --- a/src/net/java/joglutils/msg/nodes/Separator.java +++ b/src/net/java/joglutils/msg/nodes/Separator.java @@ -38,6 +38,7 @@ package net.java.joglutils.msg.nodes; import net.java.joglutils.msg.actions.*; +import net.java.joglutils.msg.misc.*; /** Represents a push / pop of OpenGL state, "separating" the sub-graph below this separator from the nodes which follow it in @@ -45,8 +46,12 @@ import net.java.joglutils.msg.actions.*; public class Separator extends Group { public void doAction(Action action) { - action.visitPre(this); - super.doAction(action); - action.visitPost(this); + State state = action.getState(); + state.push(); + try { + super.doAction(action); + } finally { + state.pop(); + } } } diff --git a/src/net/java/joglutils/msg/nodes/Texture2.java b/src/net/java/joglutils/msg/nodes/Texture2.java index 441c2f5..fd1b82a 100644 --- a/src/net/java/joglutils/msg/nodes/Texture2.java +++ b/src/net/java/joglutils/msg/nodes/Texture2.java @@ -45,6 +45,7 @@ import javax.media.opengl.*; import com.sun.opengl.util.texture.*; import net.java.joglutils.msg.actions.*; +import net.java.joglutils.msg.elements.*; /** Represents a two-dimensional texture. */ @@ -54,6 +55,11 @@ public class Texture2 extends Node { private int texEnvMode = MODULATE; private boolean dirty; + static { + // Enable the elements this node affects for known actions + GLTextureElement.enable(GLRenderAction.getDefaultState()); + } + /** Represents the OpenGL MODULATE texture environment mode. */ public static final int MODULATE = 1; /** Represents the OpenGL DECAL texture environment mode. */ @@ -134,6 +140,8 @@ public class Texture2 extends Node { } public void doAction(Action action) { - action.visit(this); + if (TextureElement.isEnabled(action.getState())) { + TextureElement.set(action.getState(), getTexture(), getTexEnvMode()); + } } } diff --git a/src/net/java/joglutils/msg/nodes/TextureCoordinate2.java b/src/net/java/joglutils/msg/nodes/TextureCoordinate2.java index ed4ecf8..f7720d2 100644 --- a/src/net/java/joglutils/msg/nodes/TextureCoordinate2.java +++ b/src/net/java/joglutils/msg/nodes/TextureCoordinate2.java @@ -39,6 +39,7 @@ package net.java.joglutils.msg.nodes; import net.java.joglutils.msg.actions.*; import net.java.joglutils.msg.collections.*; +import net.java.joglutils.msg.elements.*; /** Represents a set of 2-dimensional texture coordinates which can be used to texture geometric shapes. */ @@ -46,6 +47,11 @@ import net.java.joglutils.msg.collections.*; public class TextureCoordinate2 extends Node { private Vec2fCollection data; + static { + // Enable the elements this node affects for known actions + GLTextureCoordinateElement.enable(GLRenderAction.getDefaultState()); + } + /** Sets the texture coordinate data in this node. */ public void setData(Vec2fCollection data) { this.data = data; @@ -57,6 +63,8 @@ public class TextureCoordinate2 extends Node { } public void doAction(Action action) { - action.visit(this); + if (TextureCoordinateElement.isEnabled(action.getState())) { + TextureCoordinateElement.set(action.getState(), getData().getData()); + } } } diff --git a/src/net/java/joglutils/msg/nodes/Transform.java b/src/net/java/joglutils/msg/nodes/Transform.java index 0faf8e6..ad5e1bd 100644 --- a/src/net/java/joglutils/msg/nodes/Transform.java +++ b/src/net/java/joglutils/msg/nodes/Transform.java @@ -38,6 +38,7 @@ package net.java.joglutils.msg.nodes; import net.java.joglutils.msg.actions.*; +import net.java.joglutils.msg.elements.*; import net.java.joglutils.msg.math.*; /** Represents a generalized 4x4 matrix transformation. */ @@ -45,6 +46,14 @@ import net.java.joglutils.msg.math.*; public class Transform extends Node { private Mat4f transform; + static { + // Enable the elements this node affects for known actions + // Note that all of these elements are interdependent + GLModelMatrixElement .enable(GLRenderAction.getDefaultState()); + GLProjectionMatrixElement .enable(GLRenderAction.getDefaultState()); + GLViewingMatrixElement .enable(GLRenderAction.getDefaultState()); + } + public Transform() { transform = new Mat4f(); transform.makeIdent(); @@ -61,6 +70,8 @@ public class Transform extends Node { } public void doAction(Action action) { - action.visit(this); + if (ModelMatrixElement.isEnabled(action.getState())) { + ModelMatrixElement.mult(action.getState(), getTransform()); + } } } diff --git a/src/net/java/joglutils/msg/nodes/TriangleSet.java b/src/net/java/joglutils/msg/nodes/TriangleSet.java index 775c622..0692738 100644 --- a/src/net/java/joglutils/msg/nodes/TriangleSet.java +++ b/src/net/java/joglutils/msg/nodes/TriangleSet.java @@ -37,7 +37,13 @@ package net.java.joglutils.msg.nodes; +import javax.media.opengl.*; +import com.sun.opengl.util.texture.*; + import net.java.joglutils.msg.actions.*; +import net.java.joglutils.msg.elements.*; +import net.java.joglutils.msg.math.*; +import net.java.joglutils.msg.misc.*; /** A TriangleSet assembles the coordinates specified by a Coordinate3 node, and any auxiliary nodes such as a TextureCoordinate2 node, @@ -56,7 +62,67 @@ public class TriangleSet extends Node { return numTriangles; } - public void doAction(Action action) { - action.visit(this); + public void render(GLRenderAction action) { + State state = action.getState(); + if (!CoordinateElement.isEnabled(state)) + return; + + if (CoordinateElement.get(state) != null) { + // OK, we have coordinates to send down, at least + + GL gl = action.getGL(); + + Texture tex = null; + boolean haveTexCoords = false; + + if (GLTextureElement.isEnabled(state) && + GLTextureCoordinateElement.isEnabled(state)) { + tex = GLTextureElement.get(state); + haveTexCoords = (GLTextureCoordinateElement.get(state) != null); + } + + if (tex != null) { + // Set up the texture matrix to uniformly map [0..1] to the used + // portion of the texture image + gl.glMatrixMode(GL.GL_TEXTURE); + gl.glPushMatrix(); + gl.glLoadTransposeMatrixf(getTextureMatrix(tex).getRowMajorData(), 0); + gl.glMatrixMode(GL.GL_MODELVIEW); + } else if (haveTexCoords) { + // Want to turn off the use of texture coordinates to avoid errors + // FIXME: not 100% sure whether we need to do this, but think we should + gl.glDisableClientState(GL.GL_TEXTURE_COORD_ARRAY); + } + + // For now, assume the triangle set and the number of available + // coordinates match -- may want to add debugging information + // for this later + gl.glDrawArrays(GL.GL_TRIANGLES, 0, 3 * getNumTriangles()); + + if (tex != null) { + gl.glMatrixMode(GL.GL_TEXTURE); + gl.glPopMatrix(); + gl.glMatrixMode(GL.GL_MODELVIEW); + } else if (haveTexCoords) { + // Might want this the next time we render a shape + gl.glEnableClientState(GL.GL_TEXTURE_COORD_ARRAY); + } + } + } + + // Helper routine for setting up a texture matrix to allow texture + // coords in the scene graph to always be specified from (0..1) + private Mat4f textureMatrix = new Mat4f(); + private Mat4f getTextureMatrix(Texture texture) { + textureMatrix.makeIdent(); + TextureCoords coords = texture.getImageTexCoords(); + // Horizontal scale + textureMatrix.set(0, 0, coords.right() - coords.left()); + // Vertical scale (may be negative if texture needs to be flipped vertically) + float vertScale = coords.top() - coords.bottom(); + textureMatrix.set(1, 1, vertScale); + textureMatrix.set(0, 3, coords.left()); + textureMatrix.set(1, 3, coords.bottom()); + return textureMatrix; } } |