diff options
author | jada <jada@28c7f869-5b4e-e670-f602-82bfaf57f300> | 2006-03-01 07:07:39 +0000 |
---|---|---|
committer | jada <jada@28c7f869-5b4e-e670-f602-82bfaf57f300> | 2006-03-01 07:07:39 +0000 |
commit | 374714fa97f3aeacee236e3a019b7c252cb671c0 (patch) | |
tree | fb114eda87c590db52b7408b5cf0d5f95b41ba36 | |
parent | 800d2e0d5d0116046ba501f2c76dc5ab362e4563 (diff) |
1) Added a new examples : dot3/Dot3Demo and glsl_shader/VertexAttrTest
2) Converted SphereMotion, PureImmediate and PureImeediateStereo to use JFrame instead of MainFrame.
16 files changed, 2032 insertions, 108 deletions
@@ -180,6 +180,13 @@ </java> </target> + <target name="run.Dot3Demo" depends="init-run"> + <java fork="true" + classname="org.jdesktop.j3d.examples.dot3.Dot3Demo"> + <classpath refid="run.classpath"/> + </java> + </target> + <target name="run.FourByFour" depends="init-run"> <java fork="true" classname="org.jdesktop.j3d.examples.four_by_four.FourByFour"> @@ -257,6 +264,26 @@ </java> </target> + <target name="run.VertexAttrTest" depends="init-run"> + <java fork="true" + classname="org.jdesktop.j3d.examples.glsl_shader.VertexAttrTest"> + <classpath refid="run.classpath"/> + </java> + </target> + + <target name="run.PureImmediate" depends="init-run"> + <java fork="true" + classname="org.jdesktop.j3d.examples.pure_immediate.PureImmediate"> + <classpath refid="run.classpath"/> + </java> + </target> + + <target name="run.PureImmediateStereo" depends="init-run"> + <java fork="true" + classname="org.jdesktop.j3d.examples.pure_immediate.PureImmediateStereo"> + <classpath refid="run.classpath"/> + </java> + </target> <target name="run.SphereMotion" depends="init-run"> <java fork="true" classname="org.jdesktop.j3d.examples.sphere_motion.SphereMotion"> diff --git a/src/classes/org/jdesktop/j3d/examples/dot3/Dot3Demo.java b/src/classes/org/jdesktop/j3d/examples/dot3/Dot3Demo.java new file mode 100644 index 0000000..f47eb4e --- /dev/null +++ b/src/classes/org/jdesktop/j3d/examples/dot3/Dot3Demo.java @@ -0,0 +1,551 @@ +/* + * $RCSfile$ + * + * Copyright (c) 2006 Sun Microsystems, Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistribution of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistribution in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of Sun Microsystems, Inc. or the names of + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * This software is provided "AS IS," without a warranty of any + * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND + * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY + * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL + * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF + * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS + * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR + * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, + * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND + * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR + * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGES. + * + * You acknowledge that this software is not designed, licensed or + * intended for use in the design, construction, operation or + * maintenance of any nuclear facility. + * + * $Revision$ + * $Date$ + * $State$ + */ + +package org.jdesktop.j3d.examples.dot3; + +import com.sun.j3d.utils.behaviors.vp.OrbitBehavior; +import com.sun.j3d.utils.geometry.GeometryInfo; +import com.sun.j3d.utils.image.TextureLoader; +import com.sun.j3d.utils.universe.SimpleUniverse; +import com.sun.j3d.utils.universe.ViewingPlatform; + +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics2D; +import java.awt.GraphicsConfiguration; +import java.awt.image.BufferedImage; + +import java.util.Enumeration; + +import javax.media.j3d.AmbientLight; +import javax.media.j3d.Appearance; +import javax.media.j3d.Background; +import javax.media.j3d.Behavior; +import javax.media.j3d.BoundingSphere; +import javax.media.j3d.BranchGroup; +import javax.media.j3d.Canvas3D; +import javax.media.j3d.DirectionalLight; +import javax.media.j3d.GeometryArray; +import javax.media.j3d.ImageComponent2D; +import javax.media.j3d.Material; +import javax.media.j3d.PolygonAttributes; +import javax.media.j3d.Shape3D; +import javax.media.j3d.TexCoordGeneration; +import javax.media.j3d.Texture; +import javax.media.j3d.Texture2D; +import javax.media.j3d.TextureAttributes; +import javax.media.j3d.TextureUnitState; +import javax.media.j3d.WakeupOnElapsedFrames; + +import javax.swing.JFrame; +import javax.swing.JPanel; + +import javax.vecmath.Color3f; +import javax.vecmath.Point3d; +import javax.vecmath.Vector3f; +import org.jdesktop.j3d.examples.Resources; + +/** + * This example program is contributed by Alessandro Borges + */ + +/** + * <pre> + * DOT3 per-pixel lighting demo. + * It uses a Normal map and a Light map, both coded as independent textures. + * Each pixel color is a vector coded, where color range [0,255] is mapped + * as vector in range [-1.0,+1.0]. + * + * A math operation called DOT3 applied to Light vector and Normal vector results + * a scalar value, interpreted as light intensity. This operation is made for each + * pixel on texture. + * Light Intensity = DOT3(light, normal); + * + * This technique allows complex lighting effects, as bumps, on low polygon count + * geometries. + * </pre> + * + */ + +public class Dot3Demo extends JFrame { + // a external control panel for this demo + private TextureControlPanel ctrlPanel = null; + // default bounds used in this application + private BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), + 100.0); + // TextureUnitStates used in this application + TextureUnitState tuLightMap; + TextureUnitState tuDOT3NormalMap; + TextureUnitState tuColor; + + /** Where the TUs are applied **/ + TextureUnitState[] tusArr; + /** appearance will be changed at runtime **/ + Appearance appearance; + /** polygonAttributes will be changed at runtime **/ + PolygonAttributes polygonAttributes; + + // textures used + Texture textureColor; + Texture textureDOT3NormalMap; + Texture2D textureLightMap; + // needs for runtime updates on lightMap + ImageComponent2D imageLightMap; + + // default texture names used + String textureColorName= "resources/images/wood.jpg"; + String textureDOT3NormalMapName = "resources/images/Java3Ddot3.png"; + + /** + * Constructor. + */ + public Dot3Demo() { + super("Java3D DOT3 demo"); + try { + init(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private void init() throws Exception { + this.setSize(new Dimension(400, 400)); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + JPanel mainPanel = new JPanel(); + this.getContentPane().add(mainPanel, null); + mainPanel.setLayout(new BorderLayout()); + // get default configuration for 3D + GraphicsConfiguration conf = SimpleUniverse.getPreferredConfiguration(); + Canvas3D canvas = new Canvas3D(conf); + // create simpleUniverse + SimpleUniverse su = new SimpleUniverse(canvas); + // create sceneGraph and add it to universe + BranchGroup sceneGraph = createSceneGraph(); + su.addBranchGraph(sceneGraph); + + // This will move the ViewPlatform back a bit so the + // objects in the scene can be viewed. + su.getViewingPlatform().setNominalViewingTransform(); + + // Ensure at least 5 msec per frame (i.e., < 200Hz) + su.getViewer().getView().setMinimumFrameCycleTime(5); + + // add the behaviors to the ViewingPlatform + ViewingPlatform viewingPlatform = su.getViewingPlatform(); + viewingPlatform.setNominalViewingTransform(); + + // add orbit behavior to ViewingPlatform + OrbitBehavior orbit = new OrbitBehavior(canvas, OrbitBehavior.REVERSE_ALL | + OrbitBehavior.STOP_ZOOM); + orbit.setSchedulingBounds(bounds); + viewingPlatform.setViewPlatformBehavior(orbit); + + mainPanel.add(canvas, BorderLayout.CENTER); + this.setVisible(true); + //create a control panel to user interaction + ctrlPanel = new TextureControlPanel(this); + ctrlPanel.setVisible(true); + ctrlPanel.setLocation(410,10); + } + + /** + * loads all needed textures, and creates light map texture + */ + private void loadTextures() { + try { + //java.net.URL urlColor = new java.net.URL("file:" + textureColorName); + //java.net.URL urlDot3 = new java.net.URL("file:" + textureDOT3NormalMapName); + java.net.URL urlColor = Resources.getResource(textureColorName); + java.net.URL urlDot3 = Resources.getResource(textureDOT3NormalMapName); + + // loading textures + textureColor = new TextureLoader(urlColor,this).getTexture(); + textureDOT3NormalMap = new TextureLoader(urlDot3,this) .getTexture(); + + // create Image for textureLightMap + BufferedImage image = new BufferedImage(256,256,BufferedImage.TYPE_INT_RGB); + Graphics2D graphics = image.createGraphics(); + graphics.setPaint(new Color(130,130,250)); + graphics.fillRect(0,0,image.getWidth(),image.getHeight()); + graphics.dispose(); + + imageLightMap = new ImageComponent2D(ImageComponent2D.FORMAT_RGB,image,false,false); + imageLightMap.setCapability(ImageComponent2D.ALLOW_IMAGE_WRITE); + imageLightMap.setCapability(ImageComponent2D.ALLOW_IMAGE_READ); + + //create textureLightMap with above imageLightMap + textureLightMap = new Texture2D(Texture2D.BASE_LEVEL,Texture2D.RGB,256,256); + textureLightMap.setImage(0,imageLightMap); + textureLightMap.setMagFilter(Texture2D.NICEST); + textureLightMap.setMinFilter(Texture2D.NICEST); + + // application with update textureLightMap at runtime, so lets enable some caps + textureLightMap.setCapability(Texture2D.ALLOW_ENABLE_WRITE); + textureLightMap.setCapability(Texture2D.ALLOW_ENABLE_READ); + textureLightMap.setCapability(Texture2D.ALLOW_IMAGE_WRITE); + textureLightMap.setCapability(Texture2D.ALLOW_IMAGE_READ); + + } catch(Exception e) { + System.err.println("Failed to load textures"); + e.printStackTrace(); + } + } + + /** + * setup TextureUnitStates used in this demo. * + * @return + */ + private TextureUnitState[] setupTextureUnitState() { + //texture Attributes for DOT3 normal map + TextureAttributes textAttDot3 = new TextureAttributes(); + + + // lightMap uses TextureAttributes with default REPLACE mode + TextureAttributes textAttLightMap = new TextureAttributes(); + + TextureAttributes texAttColor = new TextureAttributes(); + texAttColor.setTextureMode(TextureAttributes.COMBINE); + + //CombineRgbMode could be also COMBINE_ADD or COMBINE_ADD_SIGNED, with + //different results + texAttColor.setCombineRgbMode(TextureAttributes.COMBINE_MODULATE); + // increase light depth effect + texAttColor.setCombineRgbScale(2); + + textAttDot3.setTextureMode(TextureAttributes.COMBINE); + textAttDot3.setCombineRgbMode(TextureAttributes.COMBINE_DOT3); + textAttDot3.setCombineAlphaMode(TextureAttributes.COMBINE_DOT3); + textAttDot3.setTextureBlendColor(1.f,1.0f,1.0f,0.0f); + // increase light intesity + textAttDot3.setCombineRgbScale(2); + // setup functions + textAttDot3.setCombineRgbFunction(0,TextureAttributes.COMBINE_SRC_COLOR); + textAttDot3.setCombineRgbFunction(1,TextureAttributes.COMBINE_SRC_COLOR); + textAttDot3.setCombineRgbFunction(2,TextureAttributes.COMBINE_SRC_COLOR); + //combine with previous TUS, lightMap + textAttDot3.setCombineRgbSource(0,TextureAttributes.COMBINE_PREVIOUS_TEXTURE_UNIT_STATE); + textAttDot3.setCombineRgbSource(1,TextureAttributes.COMBINE_TEXTURE_COLOR ); + textAttDot3.setCombineRgbSource(2,TextureAttributes.COMBINE_OBJECT_COLOR); + + TexCoordGeneration tcg1=null; + // SphereMap tcg can add nice dynamic effects for curved surfaces, because it + // distributes texture like a light bean over geometry. + // It os not used in this demo, but you can try yourself at home + // with *complex* lightmaps, i.e., spherical light distributions, + // multi light sorces, degradee, waves,etc + /* + tcg1 = new TexCoordGeneration(TexCoordGeneration.SPHERE_MAP, + TexCoordGeneration.TEXTURE_COORDINATE_3); + */ + + // create TUS + tuLightMap = new TextureUnitState(textureLightMap,textAttLightMap,tcg1); + tuDOT3NormalMap = new TextureUnitState(textureDOT3NormalMap,textAttDot3,null); + tuColor = new TextureUnitState(textureColor,texAttColor,null); + + // this TUS array is used by geometry at runtime + TextureUnitState[] tus = new TextureUnitState[3]; + tus[0] = tuLightMap; + tus[1] = tuDOT3NormalMap; + tus[2] = tuColor; + // enable texture units for read/write at runtime + for (int i = 0; i < tus.length; i++) { + tus[i].setCapability(TextureUnitState.ALLOW_STATE_WRITE); + tus[i].setCapability(TextureUnitState.ALLOW_STATE_READ); + } + + return tus; + } + + /** + * creates a single Quad geometry with 4 TextureCoordinateMaps, for multitexture use.<br> + * Dimension is scale*(2m , 1m) + * @param scale a scale for this quad + * @return quad geometry for multitexture use + */ + private GeometryArray createGeometry(float scale) { + // vertex coordinates + float[] verts = { 2.0f, -1.0f, 0.0f, + 2.0f, 1.0f, 0.0f, + -2.0f, 1.0f, 0.0f, + -2.0f, -1.0f, 0.0f }; + // 2D texture Coords - each texture unit will use one set of this + float[] texCoords = { 1.0f, 0.0f, + 1.0f, 1.0f, + 0.0f, 1.0f, + 0.0f, 0.0f}; + // all texture units will use texCoords from unit 0 + int[] texCoordSetMap = {0,0,0,0}; + // normals + Vector3f normal = new Vector3f( 0.0f, 0.0f, 1.0f); + Vector3f[] normals = { normal, normal, normal, normal} ; + // resize quad dimension + for(int i = 0;i<verts.length;i++) { + verts[i] *= scale; + } + // create geometry using GeometryInfo + GeometryInfo gi = new GeometryInfo(GeometryInfo.QUAD_ARRAY); + + gi.setCoordinates(verts); + gi.setNormals(normals); + // preparing for multitexture + // To get up to 4 TUS, it needs 4 sets of 2D texture + gi.setTextureCoordinateParams(4, 2); + gi.setTexCoordSetMap(texCoordSetMap); + + // this demo needs just 3 TUS, but geometry + // is prepared for up to 4 TUS stages + gi.setTextureCoordinates(0,texCoords); + gi.setTextureCoordinates(1,texCoords); + gi.setTextureCoordinates(2,texCoords); + gi.setTextureCoordinates(3,texCoords); + + return gi.getGeometryArray(); + } + + /** + * Creates scenegraphs + * @return a BranchGroup with all needed objects in scene + */ + private BranchGroup createSceneGraph() { + BranchGroup bgRoot = new BranchGroup(); + CheckNewLightMapBehavior checkNewLightMapBehavior = new CheckNewLightMapBehavior(); + + bgRoot.addChild(checkNewLightMapBehavior); + + // a blue background + Background background = new Background(0.4f,0.4f,0.8f); + background.setApplicationBounds(bounds); + bgRoot.addChild(background); + + AmbientLight alit = new AmbientLight(true,new Color3f(0.4f,0.4f,0.4f)); + bgRoot.addChild(alit); + + // Set up some directional lights + // DOT3 doesnot need light, because it is a perpixel lighting technique + //but we add this lights to show + // geometry when using non-DOT3 lighting, as color texture only and + // light map texture mode + Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f); + Vector3f light1Direction = new Vector3f(1.0f, 1.0f, 1.0f); + Color3f light2Color = new Color3f(1.0f, 1.0f, 0.9f); + Vector3f light2Direction = new Vector3f(-1.0f, -1.0f, -1.0f); + + DirectionalLight light1 + = new DirectionalLight(light1Color, light1Direction); + light1.setInfluencingBounds(bounds); + bgRoot.addChild(light1); + + DirectionalLight light2 + = new DirectionalLight(light2Color, light2Direction); + light2.setInfluencingBounds(bounds); + bgRoot.addChild(light2); + + //loading color and DOT3 normal map textures from disk, + //and creating light map at runtime + loadTextures(); + //our single Quad geometry, enabled for multitexture + GeometryArray geo = createGeometry(0.4f); + // a appearance for our geometry + appearance = new Appearance(); + // polygon and texture unit will be updated at runtime + // so we must enabled read/write operations for then + appearance.setCapability(Appearance.ALLOW_POLYGON_ATTRIBUTES_READ); + appearance.setCapability(Appearance.ALLOW_POLYGON_ATTRIBUTES_WRITE); + appearance.setCapability(Appearance.ALLOW_TEXTURE_UNIT_STATE_READ); + appearance.setCapability(Appearance.ALLOW_TEXTURE_UNIT_STATE_WRITE); + + //use a default material. It is necessary when running + //on non per-pixel lighting mod, i.e., using non DOT3 textures + appearance.setMaterial(new Material()); + + polygonAttributes = new PolygonAttributes(); + polygonAttributes.setCapability(PolygonAttributes.ALLOW_MODE_WRITE); + polygonAttributes.setCullFace(PolygonAttributes.CULL_NONE); + + appearance.setPolygonAttributes(polygonAttributes); + + // uses a TUS dot3 enabled + tusArr = setupTextureUnitState(); + appearance.setTextureUnitState(tusArr); + + // joining geometry and appearance in a shape3D + Shape3D shape3D = new Shape3D(geo,appearance); + shape3D.setCapability(Shape3D.ALLOW_APPEARANCE_READ); + shape3D.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE); + + bgRoot.addChild(shape3D); + + bgRoot.compile(); + + return bgRoot; + } + + /** + * Toggles wireframe mode + * @param mode true for wireframe, false for fill polygon + */ + public void setWireframeMode(boolean mode) { + if(mode) + polygonAttributes.setPolygonMode(PolygonAttributes.POLYGON_LINE); + else + polygonAttributes.setPolygonMode(PolygonAttributes.POLYGON_FILL); + } + + + /** + * This method togles on/off textures and updates TextureUnitState in correct Order. + * Some video drivers does not accept TextureUnitState arrays with null values among + * non-null values + * @param showLightMap togles LightMap texture + * @param showDot3 togles DOT3 Normal texture + * @param showColor togles Color texture + */ + public void showTextures(boolean showLightMap, boolean showDot3, boolean showColor) { + int bitSet = 0; + bitSet |= showLightMap ? 4 : 0; + bitSet |= showDot3 ? 2 : 0; + bitSet |= showColor ? 1 : 0; + + tusArr[0] = null; + tusArr[1] = null; + tusArr[2] = null; + + switch (bitSet) { + case 7: { //all bit == all tus + tusArr[0] = tuLightMap; + tusArr[1] = tuDOT3NormalMap; + tusArr[2] = tuColor; + } + break; + case 6: { //no Color + tusArr[0] = tuLightMap; + tusArr[1] = tuDOT3NormalMap; + } + break; + case 5: { //no Dot3 + tusArr[0] = tuLightMap; + tusArr[1] = tuColor; + } + break; + case 4: { //lightMap only + tusArr[0] = tuLightMap; + } + break; + case 3: { //no LightMap + tusArr[0] = tuDOT3NormalMap; + tusArr[1] = tuColor; + } + break; + case 2: { //Dot3 Only + tusArr[0] = tuDOT3NormalMap; + } + break; + case 1: { // Color Only + tusArr[0] = tuColor; + } + break; + default: { // case 0, no textures shows at all + } + break; + } + appearance.setTextureUnitState(tusArr); + } + + /** + * updates LightMap texture. + * This method is called from checkNewLightMapBehavior + * @param image new image to be applied + */ + public void updateLighMap(BufferedImage image) { + imageLightMap.setSubImage(image,image.getWidth(),image.getHeight(),0,0,0,0); + } + + private BufferedImage tempImage; + private boolean lockTempImage = false; + + + /** + * main method + * @param args + */ + public static void main(String[] args) { + javax.swing.SwingUtilities.invokeLater(new Runnable() { + public void run() { + new Dot3Demo(); + } + }); + + } + + /** + * A internal class to check if there is a new Light Map to be applied + */ + class CheckNewLightMapBehavior extends Behavior { + WakeupOnElapsedFrames wakeup = new WakeupOnElapsedFrames(0); + + public CheckNewLightMapBehavior() {// auto enable and set schedulling bounds + setEnable(true); + setSchedulingBounds(bounds); + } + + public void initialize() { + wakeupOn(wakeup); + } + + public void processStimulus(Enumeration e) { + // check if there are a new light map ready to use + if (ctrlPanel.hasTextureImageReady()) { + updateLighMap(ctrlPanel.getTextureImage()); + } + //wake up on next frame + wakeupOn(wakeup); + } + } + +} + + diff --git a/src/classes/org/jdesktop/j3d/examples/dot3/MyCanvas.java b/src/classes/org/jdesktop/j3d/examples/dot3/MyCanvas.java new file mode 100644 index 0000000..6e668be --- /dev/null +++ b/src/classes/org/jdesktop/j3d/examples/dot3/MyCanvas.java @@ -0,0 +1,241 @@ +/* + * $RCSfile$ + * + * Copyright (c) 2006 Sun Microsystems, Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistribution of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistribution in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of Sun Microsystems, Inc. or the names of + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * This software is provided "AS IS," without a warranty of any + * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND + * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY + * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL + * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF + * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS + * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR + * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, + * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND + * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR + * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGES. + * + * You acknowledge that this software is not designed, licensed or + * intended for use in the design, construction, operation or + * maintenance of any nuclear facility. + * + * $Revision$ + * $Date$ + * $State$ + */ + +package org.jdesktop.j3d.examples.dot3; + +import java.awt.*; +import java.awt.image.*; +import java.awt.event.*; +import javax.swing.*; +import javax.swing.event.MouseInputListener; + +/** + * A mouse interactive canvas for lightMap image + */ +public class MyCanvas extends JPanel implements MouseInputListener { + BufferedImage lightMask = null; + BufferedImage textureImage = null; + Graphics2D gr = null; + Point location = new Point(); + // default color light map + Color bgColor = new Color(147, 147, 147); + + int x = 0; + int y = 0; + int z = 142; + // texture image size + private static final int textureSize = 256; + boolean mouseOut = true; + //flag about image is ready or not for use + boolean imageReady = false; + // allows mask be dragged with mouse + boolean dragMask = false; + boolean updateLightDir = false; + boolean updateMaskPosition = false; + + /** + * Creates a MyCanvas object with a image lightMask. + * Also creates a default ImageLight map + * @param mask light lightMask used + */ + public MyCanvas(BufferedImage mask) { + super(); + this.lightMask = mask; + // create a light map + setTextureImage(new BufferedImage(textureSize, textureSize, + BufferedImage.TYPE_INT_RGB)); + // Graphics used to update lightmap + gr = getTextureImage().createGraphics(); + + Dimension dimSize = new Dimension(textureSize, textureSize); + // lock size + this.setSize(dimSize); + this.setMaximumSize(dimSize); + this.setMinimumSize(dimSize); + + this.setDoubleBuffered(true); + this.setOpaque(true); + this.addMouseMotionListener(this); + this.addMouseListener(this); + } + + /** + * Handles mouse click event. + * Get mouse coords call repaint for proper imageLight update + * @param ev mouse event + */ + public void mouseClicked(MouseEvent ev) { + x = ev.getX(); + y = this.getHeight() - ev.getY(); + updateLightDir = true; + repaint(); + } + + public void mousePressed(MouseEvent e) { + } + + public void mouseReleased(MouseEvent e) { + } + + /** + * Handles mouse drag event. + * get current mouse position and calls repaint for proper imageLight update + * @param ev + */ + public void mouseDragged(MouseEvent ev) { + if (!mouseOut) { + x = ev.getX(); + y = this.getHeight() - ev.getY(); + + //changes lightDir + if ((ev.getModifiers()& MouseEvent.BUTTON1_MASK) == + MouseEvent.BUTTON1_MASK) { + updateLightDir = true; + updateMaskPosition = false; + } + //updates light mask position + if ((ev.getModifiers() & ev.BUTTON2_MASK) == ev.BUTTON2_MASK || + (ev.getModifiers() & ev.BUTTON3_MASK) == ev.BUTTON3_MASK) { + updateLightDir = false; + updateMaskPosition = true; + } + repaint(); + } + } + + public void mouseMoved(MouseEvent ev) { + // disable updates on lightMap + updateLightDir = false; + updateMaskPosition = false; + } + + public void mouseEntered(MouseEvent e) { + mouseOut = false; + } + + public void mouseExited(MouseEvent e) { + mouseOut = true; + } + + /** + * updates imageLight using current setings + * @param g + */ + public void paintComponent(Graphics g) { + imageReady = false; + Graphics2D g2d = (Graphics2D)g; + + // ligthDir has changed, we must update bgColor li + if(updateLightDir) { + int blue = bgColor.getBlue(); + //clamp values to 255 + y = y>255?255:y; + x = x>255?255:x; + bgColor = new Color(y,x,blue); + } + // paint lightMap + gr.setColor(bgColor); + gr.fillRect(0, 0, textureSize, textureSize); + + // draw mask on mouse position + if (dragMask || updateMaskPosition) { + int maskWH = lightMask.getWidth()/2; + int mx = x - maskWH ; + int my = textureSize - y - maskWH ; // y value is inverted + // clamp mouse position, to avoid drawing outside imageLigh bounds + mx = mx > textureSize ? textureSize : mx; + my = my > textureSize ? textureSize : my; + // draw light mask + gr.drawImage(lightMask, mx, my, this); + } + + g2d.drawImage(getTextureImage(), 0, 0, this); + imageReady = true; + } + + /** + * + * @return true if exists a new texture image available + */ + public boolean hasTextureImageReady() { + return imageReady; + } + + /** + * Returns a texture image.<br> + * You can avoid calling the same image several times + * by checking hasTextureImageReady() first. + * @return latest texture image available + */ + public BufferedImage getTextureImage() { + // sign as texture used for next call; + imageReady = false; + //return image + return textureImage; + } + + + public Image getMask() { + return lightMask; + } + + public void setLightMask(BufferedImage mask) { + this.lightMask = mask; + } + + public Color getBgColor() { + return bgColor; + } + + public void setBgColor(Color bgColor) { + this.bgColor = bgColor; + } + + public void setTextureImage(BufferedImage textureImage) { + this.textureImage = textureImage; + } + + +} + diff --git a/src/classes/org/jdesktop/j3d/examples/dot3/TextureControlPanel.java b/src/classes/org/jdesktop/j3d/examples/dot3/TextureControlPanel.java new file mode 100644 index 0000000..72a0027 --- /dev/null +++ b/src/classes/org/jdesktop/j3d/examples/dot3/TextureControlPanel.java @@ -0,0 +1,233 @@ +/* + * $RCSfile$ + * + * Copyright (c) 2006 Sun Microsystems, Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistribution of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistribution in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of Sun Microsystems, Inc. or the names of + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * This software is provided "AS IS," without a warranty of any + * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND + * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY + * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL + * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF + * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS + * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR + * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, + * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND + * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR + * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGES. + * + * You acknowledge that this software is not designed, licensed or + * intended for use in the design, construction, operation or + * maintenance of any nuclear facility. + * + * $Revision$ + * $Date$ + * $State$ + */ + +package org.jdesktop.j3d.examples.dot3; + +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Rectangle; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.image.BufferedImage; + +import java.net.URL; + +import javax.imageio.ImageIO; + +import javax.swing.JCheckBox; +import javax.swing.JComponent; +import javax.swing.JDialog; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JSlider; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; +import org.jdesktop.j3d.examples.Resources; + +/** + * A control panel for Dot3Demo. + * It enables user change LightMap, enables/disables textures units states + * and toggles geometry wireframes on/off + */ +public class TextureControlPanel extends JDialog implements ChangeListener, + ActionListener { + /** renderer for lightMap, with support for mouse interaction **/ + private MyCanvas canvas = null; + /** file name for light mask */ + private String maskFileName = "resources/images/mask.png"; + /** a slider to change Z light direction, i.e, blue channel */ + private JSlider sliderZ = new JSlider(JSlider.HORIZONTAL, 1, 255, 142); + /** target demo instance to be controled **/ + private Dot3Demo dot3DemoFrame; + + // some checkboxes for user interaction + private JCheckBox cbWireframe = new JCheckBox("Show as Wireframe", false); + private JCheckBox cbDot3 = new JCheckBox("Show Dot3 texture", true); + private JCheckBox cbShowLightMap = new JCheckBox("Show LightMap texture", true); + private JCheckBox cbShowColor = new JCheckBox("Show Color texture", true); + private JCheckBox cbDragLightMask = new JCheckBox("Drag light mask"); + + private JLabel lbSliderZ = new JLabel(); + private JLabel lbMessage = new JLabel(); + + public TextureControlPanel(Dot3Demo owner) { + super(owner); + dot3DemoFrame = owner; + try { + URL url = Resources.getResource(maskFileName); + BufferedImage mask = ImageIO.read(url); + canvas = new MyCanvas(mask); + init(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public TextureControlPanel() { + this(null); + } + + /** + * Creates Graphical User Interface + * @throws Exception + */ + private void init() throws Exception { + Dimension dim = new Dimension(540, 350); + this.setSize(dim); + this.setPreferredSize(dim); + this.setTitle("DOT3Demo Texture Control Panel"); + this.setLayout(new BorderLayout()); + + JPanel panel = new JPanel(); + this.getContentPane().add(panel, BorderLayout.CENTER); + canvas.setSize(new Dimension(256, 256)); + canvas.setBounds(new Rectangle(40, 40, 256, 256)); + + sliderZ.setBounds(new Rectangle(310, 190, 205, 45)); + sliderZ.setPaintTicks(true); + sliderZ.setMajorTickSpacing(63); + + cbWireframe.setBounds(new Rectangle(310, 50, 200, 20)); + cbWireframe.setToolTipText("Toggles Wireframe"); + cbDot3.setBounds(new Rectangle(310, 70, 150, 20)); + cbShowLightMap.setBounds(new Rectangle(310, 90, 200, 20)); + cbShowLightMap.setToolTipText("Toggles DOT3 texture"); + cbShowColor.setBounds(new Rectangle(310, 110, 200, 20)); + cbShowColor.setToolTipText("Toggles Color texture"); + + panel.setLayout(null); + + cbDragLightMask.setBounds(new Rectangle(310, 135, 200, 20)); + lbMessage.setText("<html>Left-click and drag to change Light Direction." + + " Right-click and drag to move spotlight.</html>"); + lbMessage.setBounds(new Rectangle(305, 245, 210, 60)); + + lbSliderZ.setText("Blue Light (Dot3 Z axis)"); + lbSliderZ.setBounds(new Rectangle(310, 170, 210, 15)); + lbSliderZ.setToolTipText("changes light intensity from Z axis"); + + panel.add(cbDragLightMask, null); + panel.add(lbMessage, null); + panel.add(lbSliderZ, null); + panel.add(sliderZ, null); + panel.add(canvas, null); + panel.add(cbShowColor, null); + panel.add(cbShowLightMap, null); + panel.add(cbWireframe, null); + panel.add(cbDot3, null); + + sliderZ.addChangeListener(this); + + cbDot3.addActionListener(this); + cbShowColor.addActionListener(this); + cbShowLightMap.addActionListener(this); + cbWireframe.addActionListener(this); + cbDragLightMask.addActionListener(this); + } + + public void stateChanged(ChangeEvent ev) { + JComponent source = (JComponent)ev.getSource(); + if (sliderZ.equals(source)) { + int xVal = canvas.getBgColor().getRed(); + int yVal = canvas.getBgColor().getGreen(); + int zVal = sliderZ.getValue(); + Color ligtDir = new Color(xVal, yVal, zVal); + updateLightMap(ligtDir) ; + } + } + + private void updateLightMap(Color ligtDir) { + canvas.setBgColor(ligtDir); + canvas.repaint(); + dot3DemoFrame.updateLighMap(canvas.getTextureImage()); + } + + + public void actionPerformed(ActionEvent ev) { + JComponent source = (JComponent)ev.getSource(); + if (cbWireframe.equals(source)) { + dot3DemoFrame.setWireframeMode(cbWireframe.isSelected()); + } else + if (cbDot3.equals(source) + || cbShowColor.equals(source) + || cbShowLightMap.equals(source)) { + dot3DemoFrame.showTextures(cbShowLightMap.isSelected(), + cbDot3.isSelected(), + cbShowColor.isSelected()); + } else if (cbDragLightMask.equals(source)) { + canvas.dragMask = cbDragLightMask.isSelected(); + } + + } + + /** + * Wrapper method call for MyCanvas. hasTextureImageReady() + * @return true if exists a new texture image available + */ + public boolean hasTextureImageReady() { + return canvas.hasTextureImageReady(); + } + + /** + * Wrapper method call for MyCanvas.getTextureImage() + * Returns a texture image.<br> + * Avoid calling the same image several times + * by cheking hasTextureImageReady() first. + * @return latest texture image available + */ + public BufferedImage getTextureImage() { + return canvas.getTextureImage(); + } + + /** + * Wrapper method to MyCanvas.setLightMask(mask) + * @param mask a new light mask + */ + public void setLightMask(BufferedImage mask) { + canvas.setLightMask(mask); + } +} + + diff --git a/src/classes/org/jdesktop/j3d/examples/glsl_shader/VertexAttrTest.form b/src/classes/org/jdesktop/j3d/examples/glsl_shader/VertexAttrTest.form new file mode 100644 index 0000000..736b93c --- /dev/null +++ b/src/classes/org/jdesktop/j3d/examples/glsl_shader/VertexAttrTest.form @@ -0,0 +1,203 @@ +<?xml version="1.0" encoding="UTF-8" ?> + +<Form version="1.0" type="org.netbeans.modules.form.forminfo.JFrameFormInfo"> + <NonVisualComponents> + <Menu class="javax.swing.JMenuBar" name="jMenuBar1"> + <SubComponents> + <Menu class="javax.swing.JMenu" name="fileMenu"> + <Properties> + <Property name="text" type="java.lang.String" value="File"/> + </Properties> + <SubComponents> + <MenuItem class="javax.swing.JMenuItem" name="exitMenuItem"> + <Properties> + <Property name="text" type="java.lang.String" value="Exit"/> + </Properties> + <Events> + <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="exitMenuItemActionPerformed"/> + </Events> + </MenuItem> + </SubComponents> + </Menu> + </SubComponents> + </Menu> + </NonVisualComponents> + <Properties> + <Property name="title" type="java.lang.String" value="Vertex Attribute Test"/> + </Properties> + <SyntheticProperties> + <SyntheticProperty name="menuBar" type="java.lang.String" value="jMenuBar1"/> + <SyntheticProperty name="formSizePolicy" type="int" value="1"/> + </SyntheticProperties> + <Events> + <EventHandler event="windowClosing" listener="java.awt.event.WindowListener" parameters="java.awt.event.WindowEvent" handler="exitForm"/> + </Events> + <AuxValues> + <AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/> + <AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/> + <AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/> + <AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/> + <AuxValue name="designerSize" type="java.awt.Dimension" value="-84,-19,0,5,115,114,0,18,106,97,118,97,46,97,119,116,46,68,105,109,101,110,115,105,111,110,65,-114,-39,-41,-84,95,68,20,2,0,2,73,0,6,104,101,105,103,104,116,73,0,5,119,105,100,116,104,120,112,0,0,1,44,0,0,1,-112"/> + </AuxValues> + + <Layout class="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout"/> + <SubComponents> + <Container class="javax.swing.JPanel" name="mainPanel"> + <Constraints> + <Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout" value="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout$BorderConstraintsDescription"> + <BorderConstraints direction="Center"/> + </Constraint> + </Constraints> + + <Layout class="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout"/> + <SubComponents> + <Container class="javax.swing.JPanel" name="guiPanel"> + <Properties> + <Property name="border" type="javax.swing.border.Border" editor="org.netbeans.modules.form.editors2.BorderEditor"> + <Border info="org.netbeans.modules.form.compat2.border.LineBorderInfo"> + <LineBorder/> + </Border> + </Property> + </Properties> + <Constraints> + <Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout" value="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout$BorderConstraintsDescription"> + <BorderConstraints direction="North"/> + </Constraint> + </Constraints> + + <Layout class="org.netbeans.modules.form.compat2.layouts.DesignGridBagLayout"/> + <SubComponents> + <Container class="javax.swing.JPanel" name="vertexCheckBoxPanel"> + <Properties> + <Property name="border" type="javax.swing.border.Border" editor="org.netbeans.modules.form.editors2.BorderEditor"> + <Border info="org.netbeans.modules.form.compat2.border.TitledBorderInfo"> + <TitledBorder title="vertexFormat"> + <Font PropertyName="font" name="Lucida Sans" size="10" style="0"/> + </TitledBorder> + </Border> + </Property> + </Properties> + <Constraints> + <Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignGridBagLayout" value="org.netbeans.modules.form.compat2.layouts.DesignGridBagLayout$GridBagConstraintsDescription"> + <GridBagConstraints gridX="-1" gridY="0" gridWidth="1" gridHeight="1" fill="0" ipadX="0" ipadY="0" insetsTop="2" insetsLeft="2" insetsBottom="2" insetsRight="2" anchor="10" weightX="0.0" weightY="0.0"/> + </Constraint> + </Constraints> + + <Layout class="org.netbeans.modules.form.compat2.layouts.DesignGridBagLayout"/> + <SubComponents> + <Container class="javax.swing.JPanel" name="jPanel1"> + <Constraints> + <Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignGridBagLayout" value="org.netbeans.modules.form.compat2.layouts.DesignGridBagLayout$GridBagConstraintsDescription"> + <GridBagConstraints gridX="-1" gridY="-1" gridWidth="1" gridHeight="1" fill="0" ipadX="0" ipadY="0" insetsTop="0" insetsLeft="0" insetsBottom="0" insetsRight="0" anchor="11" weightX="0.0" weightY="0.0"/> + </Constraint> + </Constraints> + + <Layout class="org.netbeans.modules.form.compat2.layouts.DesignGridBagLayout"/> + <SubComponents> + <Component class="javax.swing.JSeparator" name="jSeparator1"> + <Properties> + <Property name="preferredSize" type="java.awt.Dimension" editor="org.netbeans.beaninfo.editors.DimensionEditor"> + <Dimension value="[0, 4]"/> + </Property> + </Properties> + <Constraints> + <Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignGridBagLayout" value="org.netbeans.modules.form.compat2.layouts.DesignGridBagLayout$GridBagConstraintsDescription"> + <GridBagConstraints gridX="0" gridY="1" gridWidth="1" gridHeight="1" fill="0" ipadX="0" ipadY="0" insetsTop="0" insetsLeft="0" insetsBottom="0" insetsRight="0" anchor="10" weightX="0.0" weightY="0.0"/> + </Constraint> + </Constraints> + </Component> + <Component class="javax.swing.JSeparator" name="jSeparator2"> + <Properties> + <Property name="preferredSize" type="java.awt.Dimension" editor="org.netbeans.beaninfo.editors.DimensionEditor"> + <Dimension value="[0, 4]"/> + </Property> + </Properties> + <Constraints> + <Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignGridBagLayout" value="org.netbeans.modules.form.compat2.layouts.DesignGridBagLayout$GridBagConstraintsDescription"> + <GridBagConstraints gridX="0" gridY="3" gridWidth="1" gridHeight="1" fill="0" ipadX="0" ipadY="0" insetsTop="0" insetsLeft="0" insetsBottom="0" insetsRight="0" anchor="10" weightX="0.0" weightY="0.0"/> + </Constraint> + </Constraints> + </Component> + </SubComponents> + </Container> + <Container class="javax.swing.JPanel" name="jPanel2"> + <Constraints> + <Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignGridBagLayout" value="org.netbeans.modules.form.compat2.layouts.DesignGridBagLayout$GridBagConstraintsDescription"> + <GridBagConstraints gridX="-1" gridY="-1" gridWidth="1" gridHeight="1" fill="0" ipadX="0" ipadY="0" insetsTop="0" insetsLeft="0" insetsBottom="0" insetsRight="0" anchor="11" weightX="0.0" weightY="0.0"/> + </Constraint> + </Constraints> + + <Layout class="org.netbeans.modules.form.compat2.layouts.DesignGridBagLayout"/> + <SubComponents> + <Component class="javax.swing.JCheckBox" name="vertexAttrsBox"> + <Properties> + <Property name="selected" type="boolean" value="true"/> + <Property name="text" type="java.lang.String" value="VertexAttrs"/> + </Properties> + <Constraints> + <Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignGridBagLayout" value="org.netbeans.modules.form.compat2.layouts.DesignGridBagLayout$GridBagConstraintsDescription"> + <GridBagConstraints gridX="0" gridY="4" gridWidth="1" gridHeight="1" fill="2" ipadX="0" ipadY="0" insetsTop="0" insetsLeft="0" insetsBottom="0" insetsRight="0" anchor="10" weightX="0.0" weightY="0.0"/> + </Constraint> + </Constraints> + </Component> + </SubComponents> + </Container> + </SubComponents> + </Container> + <Container class="javax.swing.JPanel" name="geometryPanel"> + <Constraints> + <Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignGridBagLayout" value="org.netbeans.modules.form.compat2.layouts.DesignGridBagLayout$GridBagConstraintsDescription"> + <GridBagConstraints gridX="-1" gridY="0" gridWidth="1" gridHeight="1" fill="0" ipadX="0" ipadY="0" insetsTop="2" insetsLeft="2" insetsBottom="2" insetsRight="2" anchor="10" weightX="0.0" weightY="0.0"/> + </Constraint> + </Constraints> + + <Layout class="org.netbeans.modules.form.compat2.layouts.DesignGridBagLayout"/> + <SubComponents> + <Component class="javax.swing.JButton" name="createButton"> + <Properties> + <Property name="text" type="java.lang.String" value="Create Geometry"/> + </Properties> + <Events> + <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="createButtonActionPerformed"/> + </Events> + <Constraints> + <Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignGridBagLayout" value="org.netbeans.modules.form.compat2.layouts.DesignGridBagLayout$GridBagConstraintsDescription"> + <GridBagConstraints gridX="-1" gridY="-1" gridWidth="1" gridHeight="1" fill="2" ipadX="0" ipadY="0" insetsTop="0" insetsLeft="0" insetsBottom="0" insetsRight="0" anchor="10" weightX="0.0" weightY="0.0"/> + </Constraint> + </Constraints> + </Component> + <Component class="javax.swing.JButton" name="destroyButton"> + <Properties> + <Property name="text" type="java.lang.String" value="Destroy Geometry"/> + </Properties> + <Events> + <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="destroyButtonActionPerformed"/> + </Events> + <Constraints> + <Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignGridBagLayout" value="org.netbeans.modules.form.compat2.layouts.DesignGridBagLayout$GridBagConstraintsDescription"> + <GridBagConstraints gridX="0" gridY="-1" gridWidth="1" gridHeight="1" fill="2" ipadX="0" ipadY="0" insetsTop="0" insetsLeft="0" insetsBottom="2" insetsRight="0" anchor="10" weightX="0.0" weightY="0.0"/> + </Constraint> + </Constraints> + </Component> + </SubComponents> + </Container> + </SubComponents> + </Container> + <Container class="javax.swing.JPanel" name="drawingPanel"> + <Properties> + <Property name="preferredSize" type="java.awt.Dimension" editor="org.netbeans.beaninfo.editors.DimensionEditor"> + <Dimension value="[500, 500]"/> + </Property> + </Properties> + <Constraints> + <Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout" value="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout$BorderConstraintsDescription"> + <BorderConstraints direction="Center"/> + </Constraint> + </Constraints> + + <Layout class="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout"/> + </Container> + </SubComponents> + </Container> + </SubComponents> +</Form> diff --git a/src/classes/org/jdesktop/j3d/examples/glsl_shader/VertexAttrTest.java b/src/classes/org/jdesktop/j3d/examples/glsl_shader/VertexAttrTest.java new file mode 100644 index 0000000..ae8c664 --- /dev/null +++ b/src/classes/org/jdesktop/j3d/examples/glsl_shader/VertexAttrTest.java @@ -0,0 +1,401 @@ +/* + * $RCSfile$ + * + * Copyright (c) 2006 Sun Microsystems, Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistribution of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistribution in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of Sun Microsystems, Inc. or the names of + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * This software is provided "AS IS," without a warranty of any + * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND + * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY + * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL + * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF + * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS + * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR + * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, + * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND + * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR + * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGES. + * + * You acknowledge that this software is not designed, licensed or + * intended for use in the design, construction, operation or + * maintenance of any nuclear facility. + * + * $Revision$ + * $Date$ + * $State$ + */ +package org.jdesktop.j3d.examples.glsl_shader; + +import com.sun.j3d.utils.universe.*; +import com.sun.j3d.utils.shader.StringIO; +import javax.media.j3d.*; +import java.awt.GraphicsConfiguration; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.FloatBuffer; +import javax.swing.JFrame; +import javax.swing.JOptionPane; +import javax.vecmath.Color3f; +import javax.vecmath.Point3d; +import org.jdesktop.j3d.examples.Resources; + +public class VertexAttrTest extends javax.swing.JFrame { + + SimpleUniverse universe = null; + BranchGroup scene = null; + boolean attached = false; + + public BranchGroup createSceneGraph( boolean hasVertexAttrs ) { + + // Bounds for BG and behavior + BoundingSphere bounds = + new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0); + + // Create the root of the branch graph + BranchGroup objRoot = new BranchGroup(); + objRoot.setCapability(BranchGroup.ALLOW_DETACH); + + // Set up the background + Color3f bgColor = new Color3f(0.1f, 0.1f, 0.1f); + Background bg = new Background(bgColor); + bg.setApplicationBounds(bounds); + objRoot.addChild(bg); + + // Create the TransformGroup node and initialize it to the + // identity. Enable the TRANSFORM_WRITE capability so that + // our behavior code can modify it at run time. Add it to + // the root of the subgraph. + TransformGroup objTrans = new TransformGroup(); + objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); + objRoot.addChild(objTrans); + + // Create a simple Shape3D node; add it to the scene graph. + objTrans.addChild(new MyShape(this, hasVertexAttrs)); + + return objRoot; + } + + private Canvas3D initScene() { + GraphicsConfiguration config = + SimpleUniverse.getPreferredConfiguration(); + + Canvas3D c = new Canvas3D(config); + universe = new SimpleUniverse(c); + + ViewingPlatform viewingPlatform = universe.getViewingPlatform(); + // This will move the ViewPlatform back a bit so the + // objects in the scene can be viewed. + viewingPlatform.setNominalViewingTransform(); + + return c; + } + + /** + * Creates new form VertexAttrTest + */ + public VertexAttrTest() { + // Initialize the GUI components + initComponents(); + + // Create the scene and add the Canvas3D to the drawing panel + Canvas3D c = initScene(); + drawingPanel.add(c, java.awt.BorderLayout.CENTER); + } + + static class MyShape extends Shape3D { + private static String vertexProgName = "glsl_shader/vertexshader.vert"; + + // Coordinate data + private static final float[] coords = { + 0.0f, 0.0f, 0.0f, + 0.5f, 0.0f, 0.0f, + 0.0f, 0.5f, 0.0f, + }; + + private static final int[] sizes = { 1, 3 }; + private static final float[] weights = { + 0.45f, + 0.15f, + 0.95f, + }; + private static final float[] temps = { + 1.0f, 0.5f, 0.5f, + 0.5f, 1.0f, 0.5f, + 0.5f, 0.5f, 1.0f, + }; + + private static final String[] vaNames = { "weight", "temperature" }; + + J3DBuffer createDirectFloatBuffer(float[] arr) { + ByteOrder order = ByteOrder.nativeOrder(); + + FloatBuffer nioBuf = ByteBuffer.allocateDirect(arr.length * 4).order(order).asFloatBuffer(); + nioBuf.put(arr); + return new J3DBuffer(nioBuf); + } + + + MyShape(JFrame frame, boolean hasVertexAttrs) { + + int vertexFormat = GeometryArray.COORDINATES; + int vertexAttrCount = 0; + int[] vertexAttrSizes = null; + String[] vertexAttrNames = null; + String[] shaderAttrNames = null; + + if (hasVertexAttrs) { + vertexFormat |= GeometryArray.VERTEX_ATTRIBUTES; + vertexAttrCount = vaNames.length; + vertexAttrSizes = sizes; + vertexAttrNames = vaNames; + } + + TriangleArray tri = new TriangleArray(6, vertexFormat, 0, null, vertexAttrCount, vertexAttrSizes); + tri.setValidVertexCount(3); + tri.setCoordinates(0, coords); + + if (hasVertexAttrs) { + tri.setVertexAttrs(0, 0, weights); + tri.setVertexAttrs(1, 0, temps); + + String vertexProgram = null; + try { + vertexProgram = StringIO.readFully(Resources.getResource(vertexProgName)); + } catch (IOException e) { + throw new RuntimeException(e); + } + + Shader[] shaders = new Shader[1]; + shaders[0] = new SourceCodeShader(Shader.SHADING_LANGUAGE_GLSL, + Shader.SHADER_TYPE_VERTEX, + vertexProgram); + + ShaderProgram shaderProgram = new GLSLShaderProgram(); + shaderProgram.setShaders(shaders); + shaderProgram.setVertexAttrNames(vertexAttrNames); + shaderProgram.setShaderAttrNames(shaderAttrNames); + + ShaderAppearance app = new ShaderAppearance(); + app.setShaderProgram(shaderProgram); + + this.setGeometry(tri); + + this.setAppearance(app); + } else { + this.setGeometry(tri); + this.setAppearance(new Appearance()); + } + } + } + + // ---------------------------------------------------------------- + + /** This method is called from within the constructor to + * initialize the form. + * WARNING: Do NOT modify this code. The content of this method is + * always regenerated by the Form Editor. + */ + // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents + private void initComponents() { + java.awt.GridBagConstraints gridBagConstraints; + + mainPanel = new javax.swing.JPanel(); + guiPanel = new javax.swing.JPanel(); + vertexCheckBoxPanel = new javax.swing.JPanel(); + jPanel1 = new javax.swing.JPanel(); + jSeparator1 = new javax.swing.JSeparator(); + jSeparator2 = new javax.swing.JSeparator(); + jPanel2 = new javax.swing.JPanel(); + vertexAttrsBox = new javax.swing.JCheckBox(); + geometryPanel = new javax.swing.JPanel(); + createButton = new javax.swing.JButton(); + destroyButton = new javax.swing.JButton(); + drawingPanel = new javax.swing.JPanel(); + jMenuBar1 = new javax.swing.JMenuBar(); + fileMenu = new javax.swing.JMenu(); + exitMenuItem = new javax.swing.JMenuItem(); + + setTitle("Vertex Attribute Test"); + addWindowListener(new java.awt.event.WindowAdapter() { + public void windowClosing(java.awt.event.WindowEvent evt) { + exitForm(evt); + } + }); + + mainPanel.setLayout(new java.awt.BorderLayout()); + + guiPanel.setLayout(new java.awt.GridBagLayout()); + + guiPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); + vertexCheckBoxPanel.setLayout(new java.awt.GridBagLayout()); + + vertexCheckBoxPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "vertexFormat", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Lucida Sans", 0, 10))); + jPanel1.setLayout(new java.awt.GridBagLayout()); + + jSeparator1.setPreferredSize(new java.awt.Dimension(0, 4)); + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridx = 0; + gridBagConstraints.gridy = 1; + jPanel1.add(jSeparator1, gridBagConstraints); + + jSeparator2.setPreferredSize(new java.awt.Dimension(0, 4)); + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridx = 0; + gridBagConstraints.gridy = 3; + jPanel1.add(jSeparator2, gridBagConstraints); + + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; + vertexCheckBoxPanel.add(jPanel1, gridBagConstraints); + + jPanel2.setLayout(new java.awt.GridBagLayout()); + + vertexAttrsBox.setSelected(true); + vertexAttrsBox.setText("VertexAttrs"); + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridx = 0; + gridBagConstraints.gridy = 4; + gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; + jPanel2.add(vertexAttrsBox, gridBagConstraints); + + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; + vertexCheckBoxPanel.add(jPanel2, gridBagConstraints); + + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridy = 0; + gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2); + guiPanel.add(vertexCheckBoxPanel, gridBagConstraints); + + geometryPanel.setLayout(new java.awt.GridBagLayout()); + + createButton.setText("Create Geometry"); + createButton.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + createButtonActionPerformed(evt); + } + }); + + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; + geometryPanel.add(createButton, gridBagConstraints); + + destroyButton.setText("Destroy Geometry"); + destroyButton.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + destroyButtonActionPerformed(evt); + } + }); + + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridx = 0; + gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; + gridBagConstraints.insets = new java.awt.Insets(0, 0, 2, 0); + geometryPanel.add(destroyButton, gridBagConstraints); + + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridy = 0; + gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2); + guiPanel.add(geometryPanel, gridBagConstraints); + + mainPanel.add(guiPanel, java.awt.BorderLayout.NORTH); + + drawingPanel.setLayout(new java.awt.BorderLayout()); + + drawingPanel.setPreferredSize(new java.awt.Dimension(500, 500)); + mainPanel.add(drawingPanel, java.awt.BorderLayout.CENTER); + + getContentPane().add(mainPanel, java.awt.BorderLayout.CENTER); + + fileMenu.setText("File"); + exitMenuItem.setText("Exit"); + exitMenuItem.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + exitMenuItemActionPerformed(evt); + } + }); + + fileMenu.add(exitMenuItem); + + jMenuBar1.add(fileMenu); + + setJMenuBar(jMenuBar1); + + pack(); + }// </editor-fold>//GEN-END:initComponents + + private void destroyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_destroyButtonActionPerformed + if (scene != null) { + universe.getLocale().removeBranchGraph(scene); + attached = false; + scene = null; + } + }//GEN-LAST:event_destroyButtonActionPerformed + + private void createButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createButtonActionPerformed + if (scene == null) { + boolean hasVertexAttrs = vertexAttrsBox.isSelected(); + scene = createSceneGraph(hasVertexAttrs); + universe.addBranchGraph(scene); + attached = true; + System.err.println("Scene created"); + } + }//GEN-LAST:event_createButtonActionPerformed + + private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed + System.exit(0); + }//GEN-LAST:event_exitMenuItemActionPerformed + + /** Exit the Application */ + private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm + System.exit(0); + }//GEN-LAST:event_exitForm + + /** + * @param args the command line arguments + */ + public static void main(String args[]) { + java.awt.EventQueue.invokeLater(new Runnable() { + public void run() { + new VertexAttrTest().setVisible(true); + } + }); + } + + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JButton createButton; + private javax.swing.JButton destroyButton; + private javax.swing.JPanel drawingPanel; + private javax.swing.JMenuItem exitMenuItem; + private javax.swing.JMenu fileMenu; + private javax.swing.JPanel geometryPanel; + private javax.swing.JPanel guiPanel; + private javax.swing.JMenuBar jMenuBar1; + private javax.swing.JPanel jPanel1; + private javax.swing.JPanel jPanel2; + private javax.swing.JSeparator jSeparator1; + private javax.swing.JSeparator jSeparator2; + private javax.swing.JPanel mainPanel; + private javax.swing.JCheckBox vertexAttrsBox; + private javax.swing.JPanel vertexCheckBoxPanel; + // End of variables declaration//GEN-END:variables + +} diff --git a/src/classes/org/jdesktop/j3d/examples/glsl_shader/vertexshader.vert b/src/classes/org/jdesktop/j3d/examples/glsl_shader/vertexshader.vert new file mode 100644 index 0000000..8f331ec --- /dev/null +++ b/src/classes/org/jdesktop/j3d/examples/glsl_shader/vertexshader.vert @@ -0,0 +1,61 @@ +/* + * $RCSfile$ + * + * Copyright (c) 2006 Sun Microsystems, Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistribution of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistribution in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of Sun Microsystems, Inc. or the names of + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * This software is provided "AS IS," without a warranty of any + * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND + * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY + * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL + * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF + * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS + * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR + * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, + * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND + * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR + * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGES. + * + * You acknowledge that this software is not designed, licensed or + * intended for use in the design, construction, operation or + * maintenance of any nuclear facility. + * + * $Revision$ + * $Date$ + * $State$ + */ + +// A simple GLSL vertex program for demo. vertex attributes + +attribute float weight; +attribute vec3 temperature; + +void main() +{ + // Transform the vertex + vec4 outPosition = gl_ModelViewProjectionMatrix * gl_Vertex; + + // Compute color from temperature + vec4 outColor; + outColor = vec4(temperature * weight, 1); + // Assign output parameters + gl_FrontColor = outColor; + gl_Position = outPosition; +} diff --git a/src/classes/org/jdesktop/j3d/examples/pure_immediate/PureImmediate.form b/src/classes/org/jdesktop/j3d/examples/pure_immediate/PureImmediate.form new file mode 100644 index 0000000..3c5f559 --- /dev/null +++ b/src/classes/org/jdesktop/j3d/examples/pure_immediate/PureImmediate.form @@ -0,0 +1,36 @@ +<?xml version="1.0" encoding="UTF-8" ?> + +<Form version="1.0" type="org.netbeans.modules.form.forminfo.JFrameFormInfo"> + <Properties> + <Property name="defaultCloseOperation" type="int" value="3"/> + <Property name="title" type="java.lang.String" value="PureImmediate"/> + </Properties> + <SyntheticProperties> + <SyntheticProperty name="formSizePolicy" type="int" value="1"/> + </SyntheticProperties> + <AuxValues> + <AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/> + <AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/> + <AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/> + <AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/> + <AuxValue name="designerSize" type="java.awt.Dimension" value="-84,-19,0,5,115,114,0,18,106,97,118,97,46,97,119,116,46,68,105,109,101,110,115,105,111,110,65,-114,-39,-41,-84,95,68,20,2,0,2,73,0,6,104,101,105,103,104,116,73,0,5,119,105,100,116,104,120,112,0,0,1,44,0,0,1,-112"/> + </AuxValues> + + <Layout class="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout"/> + <SubComponents> + <Container class="javax.swing.JPanel" name="drawingPanel"> + <Properties> + <Property name="preferredSize" type="java.awt.Dimension" editor="org.netbeans.beaninfo.editors.DimensionEditor"> + <Dimension value="[250, 250]"/> + </Property> + </Properties> + <Constraints> + <Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout" value="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout$BorderConstraintsDescription"> + <BorderConstraints direction="Center"/> + </Constraint> + </Constraints> + + <Layout class="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout"/> + </Container> + </SubComponents> +</Form> diff --git a/src/classes/org/jdesktop/j3d/examples/pure_immediate/PureImmediate.java b/src/classes/org/jdesktop/j3d/examples/pure_immediate/PureImmediate.java index 72e5bdd..726637b 100644 --- a/src/classes/org/jdesktop/j3d/examples/pure_immediate/PureImmediate.java +++ b/src/classes/org/jdesktop/j3d/examples/pure_immediate/PureImmediate.java @@ -44,15 +44,10 @@ package org.jdesktop.j3d.examples.pure_immediate; -import java.applet.Applet; -import java.awt.BorderLayout; -import java.awt.GraphicsConfiguration; -import java.awt.event.*; -import com.sun.j3d.utils.applet.MainFrame; -import com.sun.j3d.utils.geometry.ColorCube; import com.sun.j3d.utils.universe.*; +import com.sun.j3d.utils.geometry.ColorCube; import javax.media.j3d.*; -import javax.vecmath.*; +import java.awt.GraphicsConfiguration; /** * Pure immediate mode example program. In pure immediate mode, the @@ -60,7 +55,11 @@ import javax.vecmath.*; * example, this is done immediately after the canvas is created. A * separate thread is started up to do the immediate mode rendering. */ -public class PureImmediate extends Applet implements Runnable { +public class PureImmediate extends javax.swing.JFrame implements Runnable { + + + private SimpleUniverse univ = null; + private BranchGroup scene = null; private Canvas3D canvas; private GraphicsContext3D gc = null; @@ -70,8 +69,6 @@ public class PureImmediate extends Applet implements Runnable { // One rotation (2*PI radians) every 6 seconds private Alpha rotAlpha = new Alpha(-1, 6000); - private SimpleUniverse u = null; - // // Renders a single frame by clearing the canvas, drawing the // geometry, and swapping the draw and display buffer. @@ -97,7 +94,6 @@ public class PureImmediate extends Applet implements Runnable { canvas.swap(); } - // // Run method for our immediate mode rendering thread. // @@ -110,42 +106,74 @@ public class PureImmediate extends Applet implements Runnable { } - public PureImmediate() { - } + private void createUniverse() { + // Get the preferred graphics configuration for the default screen + GraphicsConfiguration config = + SimpleUniverse.getPreferredConfiguration(); - // - // init: create the canvas, stop the renderer, - // create the universe, and start the drawing thread. - // - public void init() { - setLayout(new BorderLayout()); - GraphicsConfiguration config = - SimpleUniverse.getPreferredConfiguration(); - - canvas = new Canvas3D(config); + // Create a Canvas3D using the preferred configuration + canvas = new Canvas3D(config); canvas.stopRenderer(); - add("Center", canvas); + // Create simple universe with view branch + univ = new SimpleUniverse(canvas); - // Create the universe and viewing branch - u = new SimpleUniverse(canvas); + // This will move the ViewPlatform back a bit so the + // objects in the scene can be viewed. + univ.getViewingPlatform().setNominalViewingTransform(); - // This will move the ViewPlatform back a bit so the - // objects in the scene can be viewed. - u.getViewingPlatform().setNominalViewingTransform(); - - // Start a new thread that will continuously render - new Thread(this).start(); + // Ensure at least 5 msec per frame (i.e., < 200Hz) + univ.getViewer().getView().setMinimumFrameCycleTime(5); } - public void destroy() { - u.cleanup(); + /** + * Creates new form PureImmediate + */ + public PureImmediate() { + // Initialize the GUI components + initComponents(); + + // Create Canvas3D and SimpleUniverse; add canvas to drawing panel + createUniverse(); + drawingPanel.add(canvas, java.awt.BorderLayout.CENTER); + + // Start a new thread that will continuously render + new Thread(this).start(); } - // - // The following allows PureImmediate to be run as an application - // as well as an applet - // - public static void main(String[] args) { - new MainFrame(new PureImmediate(), 256, 256); + // ---------------------------------------------------------------- + + /** This method is called from within the constructor to + * initialize the form. + * WARNING: Do NOT modify this code. The content of this method is + * always regenerated by the Form Editor. + */ + // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents + private void initComponents() { + drawingPanel = new javax.swing.JPanel(); + + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + setTitle("PureImmediate"); + drawingPanel.setLayout(new java.awt.BorderLayout()); + + drawingPanel.setPreferredSize(new java.awt.Dimension(250, 250)); + getContentPane().add(drawingPanel, java.awt.BorderLayout.CENTER); + + pack(); + }// </editor-fold>//GEN-END:initComponents + + /** + * @param args the command line arguments + */ + public static void main(String args[]) { + java.awt.EventQueue.invokeLater(new Runnable() { + public void run() { + new PureImmediate().setVisible(true); + } + }); } + + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JPanel drawingPanel; + // End of variables declaration//GEN-END:variables + } diff --git a/src/classes/org/jdesktop/j3d/examples/pure_immediate/PureImmediateStereo.form b/src/classes/org/jdesktop/j3d/examples/pure_immediate/PureImmediateStereo.form new file mode 100644 index 0000000..dd881b5 --- /dev/null +++ b/src/classes/org/jdesktop/j3d/examples/pure_immediate/PureImmediateStereo.form @@ -0,0 +1,36 @@ +<?xml version="1.0" encoding="UTF-8" ?> + +<Form version="1.0" type="org.netbeans.modules.form.forminfo.JFrameFormInfo"> + <Properties> + <Property name="defaultCloseOperation" type="int" value="3"/> + <Property name="title" type="java.lang.String" value="PureImmediateStereo"/> + </Properties> + <SyntheticProperties> + <SyntheticProperty name="formSizePolicy" type="int" value="1"/> + </SyntheticProperties> + <AuxValues> + <AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/> + <AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/> + <AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/> + <AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/> + <AuxValue name="designerSize" type="java.awt.Dimension" value="-84,-19,0,5,115,114,0,18,106,97,118,97,46,97,119,116,46,68,105,109,101,110,115,105,111,110,65,-114,-39,-41,-84,95,68,20,2,0,2,73,0,6,104,101,105,103,104,116,73,0,5,119,105,100,116,104,120,112,0,0,1,44,0,0,1,-112"/> + </AuxValues> + + <Layout class="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout"/> + <SubComponents> + <Container class="javax.swing.JPanel" name="drawingPanel"> + <Properties> + <Property name="preferredSize" type="java.awt.Dimension" editor="org.netbeans.beaninfo.editors.DimensionEditor"> + <Dimension value="[512, 256]"/> + </Property> + </Properties> + <Constraints> + <Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout" value="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout$BorderConstraintsDescription"> + <BorderConstraints direction="Center"/> + </Constraint> + </Constraints> + + <Layout class="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout"/> + </Container> + </SubComponents> +</Form> diff --git a/src/classes/org/jdesktop/j3d/examples/pure_immediate/PureImmediateStereo.java b/src/classes/org/jdesktop/j3d/examples/pure_immediate/PureImmediateStereo.java index 8c52509..2d030fe 100644 --- a/src/classes/org/jdesktop/j3d/examples/pure_immediate/PureImmediateStereo.java +++ b/src/classes/org/jdesktop/j3d/examples/pure_immediate/PureImmediateStereo.java @@ -44,15 +44,13 @@ package org.jdesktop.j3d.examples.pure_immediate; -import java.applet.Applet; -import java.awt.*; import java.util.*; -import java.awt.event.*; -import com.sun.j3d.utils.applet.MainFrame; -import com.sun.j3d.utils.universe.*; import com.sun.j3d.utils.geometry.*; +import com.sun.j3d.utils.universe.*; import javax.media.j3d.*; import javax.vecmath.*; +import java.awt.GraphicsConfiguration; +import java.awt.GraphicsEnvironment; /** * Pure immediate mode stereo example program for stereo. In pure @@ -61,7 +59,11 @@ import javax.vecmath.*; * canvas is created. A separate thread is started up to do the * immediate mode rendering. */ -public class PureImmediateStereo extends Applet implements Runnable { + +public class PureImmediateStereo extends javax.swing.JFrame implements Runnable { + + private SimpleUniverse univ = null; + private BranchGroup scene = null; // Set this to true when the graphics card use shared z buffer // in stereo mode. @@ -78,7 +80,6 @@ public class PureImmediateStereo extends Applet implements Runnable { // One rotation (2*PI radians) every 6 seconds private Alpha rotAlpha = new Alpha(-1, 6000); - private SimpleUniverse u = null; private double angle; // Compute data which is common for both @@ -199,16 +200,7 @@ public class PureImmediateStereo extends Applet implements Runnable { } - public PureImmediateStereo() { - } - - // - // init: create the canvas, stop the renderer, - // create the universe, and start the drawing thread. - // - public void init() { - setLayout(new BorderLayout()); - + private void createUniverse() { // Preferred to use Stereo GraphicsConfigTemplate3D gct = new GraphicsConfigTemplate3D(); gct.setStereo(GraphicsConfigTemplate3D.PREFERRED); @@ -238,29 +230,67 @@ public class PureImmediateStereo extends Applet implements Runnable { // we must stop the Renderer in PureImmediate mode canvas.stopRenderer(); - add("Center", canvas); - // Create the universe and viewing branch - u = new SimpleUniverse(canvas); + // Create simple universe with view branch + univ = new SimpleUniverse(canvas); - // This will move the ViewPlatform back a bit so the - // objects in the scene can be viewed. - u.getViewingPlatform().setNominalViewingTransform(); + // This will move the ViewPlatform back a bit so the + // objects in the scene can be viewed. + univ.getViewingPlatform().setNominalViewingTransform(); - // Start a new thread that will continuously render + // Ensure at least 5 msec per frame (i.e., < 200Hz) + univ.getViewer().getView().setMinimumFrameCycleTime(5); + + // Start a new thread that will continuously render (new Thread(this)).start(); } - // Cleanup all Java3D threads and memory when this applet exit - public void destroy() { - u.cleanup(); + /** + * Creates new form PureImmediateStereo + */ + public PureImmediateStereo() { + // Initialize the GUI components + initComponents(); + + // Create Canvas3D and SimpleUniverse; add canvas to drawing panel + createUniverse(); + drawingPanel.add(canvas, java.awt.BorderLayout.CENTER); } - // - // The following allows PureImmediateStereo to be run as an application - // as well as an applet - // - public static void main(String[] args) { - new MainFrame(new PureImmediateStereo(), 512, 256); + // ---------------------------------------------------------------- + + /** This method is called from within the constructor to + * initialize the form. + * WARNING: Do NOT modify this code. The content of this method is + * always regenerated by the Form Editor. + */ + // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents + private void initComponents() { + drawingPanel = new javax.swing.JPanel(); + + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + setTitle("PureImmediateStereo"); + drawingPanel.setLayout(new java.awt.BorderLayout()); + + drawingPanel.setPreferredSize(new java.awt.Dimension(512, 256)); + getContentPane().add(drawingPanel, java.awt.BorderLayout.CENTER); + + pack(); + }// </editor-fold>//GEN-END:initComponents + + /** + * @param args the command line arguments + */ + public static void main(String args[]) { + java.awt.EventQueue.invokeLater(new Runnable() { + public void run() { + new PureImmediateStereo().setVisible(true); + } + }); } + + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JPanel drawingPanel; + // End of variables declaration//GEN-END:variables + } diff --git a/src/classes/org/jdesktop/j3d/examples/sphere_motion/SphereMotion.form b/src/classes/org/jdesktop/j3d/examples/sphere_motion/SphereMotion.form new file mode 100644 index 0000000..036d770 --- /dev/null +++ b/src/classes/org/jdesktop/j3d/examples/sphere_motion/SphereMotion.form @@ -0,0 +1,36 @@ +<?xml version="1.0" encoding="UTF-8" ?> + +<Form version="1.0" type="org.netbeans.modules.form.forminfo.JFrameFormInfo"> + <Properties> + <Property name="defaultCloseOperation" type="int" value="3"/> + <Property name="title" type="java.lang.String" value="SphereMotion"/> + </Properties> + <SyntheticProperties> + <SyntheticProperty name="formSizePolicy" type="int" value="1"/> + </SyntheticProperties> + <AuxValues> + <AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/> + <AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/> + <AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/> + <AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/> + <AuxValue name="designerSize" type="java.awt.Dimension" value="-84,-19,0,5,115,114,0,18,106,97,118,97,46,97,119,116,46,68,105,109,101,110,115,105,111,110,65,-114,-39,-41,-84,95,68,20,2,0,2,73,0,6,104,101,105,103,104,116,73,0,5,119,105,100,116,104,120,112,0,0,1,44,0,0,1,-112"/> + </AuxValues> + + <Layout class="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout"/> + <SubComponents> + <Container class="javax.swing.JPanel" name="drawingPanel"> + <Properties> + <Property name="preferredSize" type="java.awt.Dimension" editor="org.netbeans.beaninfo.editors.DimensionEditor"> + <Dimension value="[700, 700]"/> + </Property> + </Properties> + <Constraints> + <Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout" value="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout$BorderConstraintsDescription"> + <BorderConstraints direction="Center"/> + </Constraint> + </Constraints> + + <Layout class="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout"/> + </Container> + </SubComponents> +</Form> diff --git a/src/classes/org/jdesktop/j3d/examples/sphere_motion/SphereMotion.java b/src/classes/org/jdesktop/j3d/examples/sphere_motion/SphereMotion.java index 49b3293..bf30793 100644 --- a/src/classes/org/jdesktop/j3d/examples/sphere_motion/SphereMotion.java +++ b/src/classes/org/jdesktop/j3d/examples/sphere_motion/SphereMotion.java @@ -44,16 +44,16 @@ package org.jdesktop.j3d.examples.sphere_motion; -import java.applet.Applet; -import java.awt.*; -import com.sun.j3d.utils.applet.MainFrame; import com.sun.j3d.utils.geometry.Sphere; import com.sun.j3d.utils.universe.*; import javax.media.j3d.*; import javax.vecmath.*; -import java.util.Enumeration; +import java.awt.GraphicsConfiguration; -public class SphereMotion extends Applet { +public class SphereMotion extends javax.swing.JFrame { + + private SimpleUniverse univ = null; + private BranchGroup scene = null; // Constants for type of light to use private static final int DIRECTIONAL_LIGHT = 0; @@ -64,9 +64,7 @@ public class SphereMotion extends Applet { // lights. This flag is set based on command line argument private static int lightType = POINT_LIGHT; - private SimpleUniverse u = null; - - public BranchGroup createSceneGraph(SimpleUniverse u) { + public BranchGroup createSceneGraph() { Color3f eColor = new Color3f(0.0f, 0.0f, 0.0f); Color3f sColor = new Color3f(1.0f, 1.0f, 1.0f); Color3f objColor = new Color3f(0.6f, 0.6f, 0.6f); @@ -218,7 +216,7 @@ public class SphereMotion extends Applet { // Create a position interpolator and attach it to the view // platform TransformGroup vpTrans = - u.getViewingPlatform().getViewPlatformTransform(); + univ.getViewingPlatform().getViewPlatformTransform(); Transform3D axisOfTranslation = new Transform3D(); Alpha transAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE | @@ -240,37 +238,33 @@ public class SphereMotion extends Applet { return objRoot; } + + private Canvas3D createUniverse() { + // Get the preferred graphics configuration for the default screen + GraphicsConfiguration config = + SimpleUniverse.getPreferredConfiguration(); - public SphereMotion() { - } - - public void init() { - setLayout(new BorderLayout()); - GraphicsConfiguration config = - SimpleUniverse.getPreferredConfiguration(); + // Create a Canvas3D using the preferred configuration + Canvas3D c = new Canvas3D(config); - Canvas3D c = new Canvas3D(config); - add("Center", c); + // Create simple universe with view branch + univ = new SimpleUniverse(c); - u = new SimpleUniverse(c); - BranchGroup scene = createSceneGraph(u); + // This will move the ViewPlatform back a bit so the + // objects in the scene can be viewed. + univ.getViewingPlatform().setNominalViewingTransform(); - // This will move the ViewPlatform back a bit so the - // objects in the scene can be viewed. - u.getViewingPlatform().setNominalViewingTransform(); + // Ensure at least 5 msec per frame (i.e., < 200Hz) + univ.getViewer().getView().setMinimumFrameCycleTime(5); - u.addBranchGraph(scene); + return c; } - public void destroy() { - u.cleanup(); - } - - // - // The following allows SphereMotion to be run as an application - // as well as an applet - // - public static void main(String[] args) { + /** + * Creates new form SphereMotion + */ + public SphereMotion(final String[] args) { + // Parse the Input Arguments String usage = "Usage: java SphereMotion [-point | -spot | -dir]"; for (int i = 0; i < args.length; i++) { @@ -296,8 +290,55 @@ public class SphereMotion extends Applet { System.out.println(usage); System.exit(0); } - } + } + + // Initialize the GUI components + initComponents(); + + // Create Canvas3D and SimpleUniverse; add canvas to drawing panel + Canvas3D c = createUniverse(); + drawingPanel.add(c, java.awt.BorderLayout.CENTER); + + // Create the content branch and add it to the universe + scene = createSceneGraph(); + univ.addBranchGraph(scene); + } - new MainFrame(new SphereMotion(), 700, 700); + // ---------------------------------------------------------------- + + /** This method is called from within the constructor to + * initialize the form. + * WARNING: Do NOT modify this code. The content of this method is + * always regenerated by the Form Editor. + */ + // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents + private void initComponents() { + drawingPanel = new javax.swing.JPanel(); + + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + setTitle("SphereMotion"); + drawingPanel.setLayout(new java.awt.BorderLayout()); + + drawingPanel.setPreferredSize(new java.awt.Dimension(700, 700)); + getContentPane().add(drawingPanel, java.awt.BorderLayout.CENTER); + + pack(); + }// </editor-fold>//GEN-END:initComponents + + /** + * @param args the command line arguments + */ + public static void main(final String args[]) { + java.awt.EventQueue.invokeLater(new Runnable() { + public void run() { + SphereMotion sphereMotion = new SphereMotion(args); + sphereMotion.setVisible(true); + } + }); } + + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JPanel drawingPanel; + // End of variables declaration//GEN-END:variables + } diff --git a/src/resources/images/Java3Ddot3.png b/src/resources/images/Java3Ddot3.png Binary files differnew file mode 100644 index 0000000..996b3bc --- /dev/null +++ b/src/resources/images/Java3Ddot3.png diff --git a/src/resources/images/mask.png b/src/resources/images/mask.png Binary files differnew file mode 100644 index 0000000..e5a2e35 --- /dev/null +++ b/src/resources/images/mask.png diff --git a/src/resources/images/wood.jpg b/src/resources/images/wood.jpg Binary files differnew file mode 100644 index 0000000..195b903 --- /dev/null +++ b/src/resources/images/wood.jpg |