diff options
author | jada <jada@28c7f869-5b4e-e670-f602-82bfaf57f300> | 2006-02-24 22:37:27 +0000 |
---|---|---|
committer | jada <jada@28c7f869-5b4e-e670-f602-82bfaf57f300> | 2006-02-24 22:37:27 +0000 |
commit | 800d2e0d5d0116046ba501f2c76dc5ab362e4563 (patch) | |
tree | d70b0e8087b4851ada9d366e61b6ae9df0403119 /src | |
parent | 521df2f4b6deb4154eb15e4879c0132fba9bf5df (diff) |
Added a couple of Java 3D Programmable Shader examples.
Diffstat (limited to 'src')
11 files changed, 1019 insertions, 0 deletions
diff --git a/src/classes/org/jdesktop/j3d/examples/cg_shader/SamplerTestCg.form b/src/classes/org/jdesktop/j3d/examples/cg_shader/SamplerTestCg.form new file mode 100644 index 0000000..2be0fec --- /dev/null +++ b/src/classes/org/jdesktop/j3d/examples/cg_shader/SamplerTestCg.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="SamplerTestCg"/> + </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="[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> +</Form> diff --git a/src/classes/org/jdesktop/j3d/examples/cg_shader/SamplerTestCg.java b/src/classes/org/jdesktop/j3d/examples/cg_shader/SamplerTestCg.java new file mode 100644 index 0000000..757b02e --- /dev/null +++ b/src/classes/org/jdesktop/j3d/examples/cg_shader/SamplerTestCg.java @@ -0,0 +1,239 @@ +/* + * $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.cg_shader; + +import com.sun.j3d.utils.universe.*; +import com.sun.j3d.utils.geometry.Sphere; +import com.sun.j3d.utils.image.TextureLoader; +import com.sun.j3d.utils.shader.StringIO; +import javax.media.j3d.*; +import javax.vecmath.*; +import java.awt.GraphicsConfiguration; +import java.io.IOException; +import java.net.URL; +import org.jdesktop.j3d.examples.Resources; + +public class SamplerTestCg extends javax.swing.JFrame { + + private static String cloudTexName = "resources/images/bg.jpg"; + private static String earthTexName = "resources/images/earth.jpg"; + private static String fragmentProgName = "cg_shader/multitex_fp.cg"; + private URL cloudURL = null; + private URL earthURL = null; + private static final int CLOUD = 0; + private static final int EARTH = 1; + + SimpleUniverse u = null; + + public BranchGroup createSceneGraph() { + // Create the root of the branch graph + BranchGroup objRoot = new BranchGroup(); + + // 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 texture objects + cloudURL = Resources.getResource(cloudTexName); + Texture cloudTex = new TextureLoader(cloudURL, this).getTexture(); + earthURL = Resources.getResource(earthTexName); + Texture earthTex = new TextureLoader(earthURL, this).getTexture(); + + // Create the shader program + String vertexProgram = null; + String fragmentProgram = null; + try { + fragmentProgram = StringIO.readFully(Resources.getResource(fragmentProgName)); + } + catch (IOException e) { + System.err.println(e); + } + Shader[] shaders = new Shader[1]; + shaders[0] = new SourceCodeShader(Shader.SHADING_LANGUAGE_CG, + Shader.SHADER_TYPE_FRAGMENT, + fragmentProgram); + final String[] shaderAttrNames = { + "cloudFactor" + }; + final Object[] shaderAttrValues = { + new Float(0.6f), + }; + ShaderProgram shaderProgram = new CgShaderProgram(); + shaderProgram.setShaders(shaders); + shaderProgram.setShaderAttrNames(shaderAttrNames); + + // Create the shader attribute set + ShaderAttributeSet shaderAttributeSet = new ShaderAttributeSet(); + for (int i = 0; i < shaderAttrNames.length; i++) { + ShaderAttribute shaderAttribute = + new ShaderAttributeValue(shaderAttrNames[i], shaderAttrValues[i]); + shaderAttributeSet.put(shaderAttribute); + } + + // Create shader appearance to hold the shader program and + // shader attributes + ShaderAppearance app = new ShaderAppearance(); + app.setShaderProgram(shaderProgram); + app.setShaderAttributeSet(shaderAttributeSet); + + // Setup texture coordinate generation + Vector4f plane0S = new Vector4f(3.0f, 1.5f, 0.3f, 0.0f); + Vector4f plane0T = new Vector4f(1.0f, 2.5f, 0.24f, 0.0f); + TexCoordGeneration tcg0 = + new TexCoordGeneration(TexCoordGeneration.OBJECT_LINEAR, + TexCoordGeneration.TEXTURE_COORDINATE_2, plane0S, plane0T); + + // Setup texture coordinate generation + TexCoordGeneration tcg1 = + new TexCoordGeneration(TexCoordGeneration.SPHERE_MAP, + TexCoordGeneration.TEXTURE_COORDINATE_2); + + // Put the textures in unit 0,1 + TextureUnitState[] tus = new TextureUnitState[2]; + tus[CLOUD] = new TextureUnitState(); + tus[CLOUD].setTexture(cloudTex); + tus[CLOUD].setTexCoordGeneration(tcg0); + tus[EARTH] = new TextureUnitState(); + tus[EARTH].setTexture(earthTex); + tus[EARTH].setTexCoordGeneration(tcg1); + app.setTextureUnitState(tus); + + // Create a Sphere object using the shader appearance, + // and add it into the scene graph. + Sphere sph = new Sphere(0.4f, Sphere.GENERATE_NORMALS, 30, app); + objTrans.addChild(sph); + + // Create a new Behavior object that will perform the + // desired operation on the specified transform and add + // it into the scene graph. + Transform3D yAxis = new Transform3D(); + Alpha rotationAlpha = new Alpha(-1, 4000); + + RotationInterpolator rotator = + new RotationInterpolator(rotationAlpha, objTrans, yAxis, + 0.0f, (float) Math.PI*2.0f); + BoundingSphere bounds = + new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0); + rotator.setSchedulingBounds(bounds); + objRoot.addChild(rotator); + + // Have Java 3D perform optimizations on this scene graph. + //objRoot.compile(); + + return objRoot; + } + + private Canvas3D initScene() { + GraphicsConfiguration config = + SimpleUniverse.getPreferredConfiguration(); + + Canvas3D c = new Canvas3D(config); + + BranchGroup scene = createSceneGraph(); + u = new SimpleUniverse(c); + + ViewingPlatform viewingPlatform = u.getViewingPlatform(); + // This will move the ViewPlatform back a bit so the + // objects in the scene can be viewed. + viewingPlatform.setNominalViewingTransform(); + + u.addBranchGraph(scene); + + return c; + } + + /** + * Creates new form SamplerTestCg + */ + public SamplerTestCg() { + // 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); + } + + // ---------------------------------------------------------------- + + /** 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("SamplerTestCg"); + drawingPanel.setLayout(new java.awt.BorderLayout()); + + drawingPanel.setPreferredSize(new java.awt.Dimension(500, 500)); + 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 SamplerTestCg().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/cg_shader/multitex_fp.cg b/src/classes/org/jdesktop/j3d/examples/cg_shader/multitex_fp.cg new file mode 100644 index 0000000..400a252 --- /dev/null +++ b/src/classes/org/jdesktop/j3d/examples/cg_shader/multitex_fp.cg @@ -0,0 +1,56 @@ +/* + * $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$ + */ + +float4 main(float2 tc0 : TEXCOORD0, + float2 tc1: TEXCOORD1, + uniform float cloudFactor, + uniform sampler2D earthTex : TEXUNIT1, + uniform sampler2D cloudTex : TEXUNIT0): COLOR +{ + float3 color0 = tex2D(cloudTex, tc0).rgb; + float3 color1 = tex2D(earthTex, tc1).rgb; + float3 finalColor = color0*cloudFactor + color1; + + return float4(finalColor, 1.0); +} diff --git a/src/classes/org/jdesktop/j3d/examples/glsl_shader/EnvironmentMappingGLSL.form b/src/classes/org/jdesktop/j3d/examples/glsl_shader/EnvironmentMappingGLSL.form new file mode 100644 index 0000000..b6a3e94 --- /dev/null +++ b/src/classes/org/jdesktop/j3d/examples/glsl_shader/EnvironmentMappingGLSL.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="EnvironmentMappingGLSL"/> + </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="[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> +</Form> diff --git a/src/classes/org/jdesktop/j3d/examples/glsl_shader/EnvironmentMappingGLSL.java b/src/classes/org/jdesktop/j3d/examples/glsl_shader/EnvironmentMappingGLSL.java new file mode 100644 index 0000000..bfada86 --- /dev/null +++ b/src/classes/org/jdesktop/j3d/examples/glsl_shader/EnvironmentMappingGLSL.java @@ -0,0 +1,226 @@ +/* + * $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.geometry.Sphere; +import com.sun.j3d.utils.image.TextureLoader; +import com.sun.j3d.utils.shader.StringIO; +import javax.media.j3d.*; +import javax.vecmath.*; +import java.awt.GraphicsConfiguration; +import java.io.IOException; +import java.net.URL; +import org.jdesktop.j3d.examples.Resources; + +public class EnvironmentMappingGLSL extends javax.swing.JFrame { + + private URL textureURL = null; + private static final int NUM_TEX_UNITS = 1; + private static final int TEX_UNIT = 0; + + SimpleUniverse u = null; + + public BranchGroup createSceneGraph() { + // Create the root of the branch graph + BranchGroup objRoot = new BranchGroup(); + + // 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 texture object + textureURL = Resources.getResource("resources/images/duke-gears.jpg"); + Texture tex = new TextureLoader(textureURL, this).getTexture(); + // Create the shader program + String vertexProgram = null; + String fragmentProgram = null; + try { + vertexProgram = StringIO.readFully(Resources.getResource("glsl_shader/envmap.vert")); + fragmentProgram = StringIO.readFully(Resources.getResource("glsl_shader/envmap.frag")); + } + catch (IOException e) { + System.err.println(e); + } + Shader[] shaders = new Shader[2]; + shaders[0] = new SourceCodeShader(Shader.SHADING_LANGUAGE_GLSL, + Shader.SHADER_TYPE_VERTEX, + vertexProgram); + shaders[1] = new SourceCodeShader(Shader.SHADING_LANGUAGE_GLSL, + Shader.SHADER_TYPE_FRAGMENT, + fragmentProgram); + final String[] shaderAttrNames = { + "LightPos", + "BaseColor", + "MixRatio", + "EnvMap", + }; + final Object[] shaderAttrValues = { + new Point3f(1.0f, -1.0f, 2.0f), + new Color3f(0.2f, 0.9f, 0.5f), + new Float(0.4f), + new Integer(TEX_UNIT), + }; + ShaderProgram shaderProgram = new GLSLShaderProgram(); + shaderProgram.setShaders(shaders); + shaderProgram.setShaderAttrNames(shaderAttrNames); + + // Create the shader attribute set + ShaderAttributeSet shaderAttributeSet = new ShaderAttributeSet(); + for (int i = 0; i < shaderAttrNames.length; i++) { + ShaderAttribute shaderAttribute = + new ShaderAttributeValue(shaderAttrNames[i], shaderAttrValues[i]); + shaderAttributeSet.put(shaderAttribute); + } + + // Create shader appearance to hold the shader program and + // shader attributes + ShaderAppearance app = new ShaderAppearance(); + app.setShaderProgram(shaderProgram); + app.setShaderAttributeSet(shaderAttributeSet); + + // Put the texture in specified texture unit + TextureUnitState[] tus = new TextureUnitState[NUM_TEX_UNITS]; + tus[TEX_UNIT] = new TextureUnitState(); + tus[TEX_UNIT].setTexture(tex); + app.setTextureUnitState(tus); + + // Create a Sphere object using the shader appearance, + // and add it into the scene graph. + Sphere sph = new Sphere(0.4f, Sphere.GENERATE_NORMALS, 20, app); + objTrans.addChild(sph); + + // Create a new Behavior object that will perform the + // desired operation on the specified transform and add + // it into the scene graph. + Transform3D yAxis = new Transform3D(); + Alpha rotationAlpha = new Alpha(-1, 4000); + + RotationInterpolator rotator = + new RotationInterpolator(rotationAlpha, objTrans, yAxis, + 0.0f, (float) Math.PI*2.0f); + BoundingSphere bounds = + new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0); + rotator.setSchedulingBounds(bounds); + objRoot.addChild(rotator); + + // Have Java 3D perform optimizations on this scene graph. + //objRoot.compile(); + + return objRoot; + } + + private Canvas3D initScene() { + GraphicsConfiguration config = + SimpleUniverse.getPreferredConfiguration(); + + Canvas3D c = new Canvas3D(config); + + BranchGroup scene = createSceneGraph(); + u = new SimpleUniverse(c); + + ViewingPlatform viewingPlatform = u.getViewingPlatform(); + // This will move the ViewPlatform back a bit so the + // objects in the scene can be viewed. + viewingPlatform.setNominalViewingTransform(); + + u.addBranchGraph(scene); + + return c; + } + + /** + * Creates new form EnvironmentMappingGLSL + */ + public EnvironmentMappingGLSL() { + // 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); + } + + // ---------------------------------------------------------------- + + /** 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("EnvironmentMappingGLSL"); + drawingPanel.setLayout(new java.awt.BorderLayout()); + + drawingPanel.setPreferredSize(new java.awt.Dimension(500, 500)); + 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 EnvironmentMappingGLSL().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/glsl_shader/SamplerTestGLSL.form b/src/classes/org/jdesktop/j3d/examples/glsl_shader/SamplerTestGLSL.form new file mode 100644 index 0000000..6a1a031 --- /dev/null +++ b/src/classes/org/jdesktop/j3d/examples/glsl_shader/SamplerTestGLSL.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="SamplerTestGLSL"/> + </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="[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> +</Form> diff --git a/src/classes/org/jdesktop/j3d/examples/glsl_shader/SamplerTestGLSL.java b/src/classes/org/jdesktop/j3d/examples/glsl_shader/SamplerTestGLSL.java new file mode 100644 index 0000000..d7fe88f --- /dev/null +++ b/src/classes/org/jdesktop/j3d/examples/glsl_shader/SamplerTestGLSL.java @@ -0,0 +1,243 @@ +/* + * $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.geometry.Sphere; +import com.sun.j3d.utils.image.TextureLoader; +import com.sun.j3d.utils.shader.StringIO; +import javax.media.j3d.*; +import javax.vecmath.*; +import java.awt.GraphicsConfiguration; +import java.io.IOException; +import java.net.URL; +import org.jdesktop.j3d.examples.Resources; + +public class SamplerTestGLSL extends javax.swing.JFrame { + + private static String cloudTexName = "resources/images/bg.jpg"; + private static String earthTexName = "resources/images/earth.jpg"; + private static String fragmentProgName = "glsl_shader/multitex.frag"; + private URL cloudURL = null; + private URL earthURL = null; + private static final int CLOUD = 0; + private static final int EARTH = 1; + + SimpleUniverse u = null; + + public BranchGroup createSceneGraph() { + // Create the root of the branch graph + BranchGroup objRoot = new BranchGroup(); + + // 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 texture objects + cloudURL = Resources.getResource(cloudTexName); + Texture cloudTex = new TextureLoader(cloudURL, this).getTexture(); + earthURL = Resources.getResource(earthTexName); + Texture earthTex = new TextureLoader(earthURL, this).getTexture(); + + // Create the shader program + String vertexProgram = null; + String fragmentProgram = null; + try { + fragmentProgram = StringIO.readFully(Resources.getResource(fragmentProgName)); + } + catch (IOException e) { + System.err.println(e); + } + Shader[] shaders = new Shader[1]; + shaders[0] = new SourceCodeShader(Shader.SHADING_LANGUAGE_GLSL, + Shader.SHADER_TYPE_FRAGMENT, + fragmentProgram); + final String[] shaderAttrNames = { + "cloudFactor", + "cloudTex", + "earthTex", + }; + final Object[] shaderAttrValues = { + new Float(0.6f), + new Integer(0), + new Integer(1), + }; + ShaderProgram shaderProgram = new GLSLShaderProgram(); + shaderProgram.setShaders(shaders); + shaderProgram.setShaderAttrNames(shaderAttrNames); + + // Create the shader attribute set + ShaderAttributeSet shaderAttributeSet = new ShaderAttributeSet(); + for (int i = 0; i < shaderAttrNames.length; i++) { + ShaderAttribute shaderAttribute = + new ShaderAttributeValue(shaderAttrNames[i], shaderAttrValues[i]); + shaderAttributeSet.put(shaderAttribute); + } + + // Create shader appearance to hold the shader program and + // shader attributes + ShaderAppearance app = new ShaderAppearance(); + app.setShaderProgram(shaderProgram); + app.setShaderAttributeSet(shaderAttributeSet); + + // Setup texture coordinate generation + Vector4f plane0S = new Vector4f(3.0f, 1.5f, 0.3f, 0.0f); + Vector4f plane0T = new Vector4f(1.0f, 2.5f, 0.24f, 0.0f); + TexCoordGeneration tcg0 = + new TexCoordGeneration(TexCoordGeneration.OBJECT_LINEAR, + TexCoordGeneration.TEXTURE_COORDINATE_2, plane0S, plane0T); + + // Setup texture coordinate generation + TexCoordGeneration tcg1 = + new TexCoordGeneration(TexCoordGeneration.SPHERE_MAP, + TexCoordGeneration.TEXTURE_COORDINATE_2); + + // Put the textures in unit 0,1 + TextureUnitState[] tus = new TextureUnitState[2]; + tus[CLOUD] = new TextureUnitState(); + tus[CLOUD].setTexture(cloudTex); + tus[CLOUD].setTexCoordGeneration(tcg0); + tus[EARTH] = new TextureUnitState(); + tus[EARTH].setTexture(earthTex); + tus[EARTH].setTexCoordGeneration(tcg1); + app.setTextureUnitState(tus); + + // Create a Sphere object using the shader appearance, + // and add it into the scene graph. + Sphere sph = new Sphere(0.4f, Sphere.GENERATE_NORMALS, 30, app); + objTrans.addChild(sph); + + // Create a new Behavior object that will perform the + // desired operation on the specified transform and add + // it into the scene graph. + Transform3D yAxis = new Transform3D(); + Alpha rotationAlpha = new Alpha(-1, 4000); + + RotationInterpolator rotator = + new RotationInterpolator(rotationAlpha, objTrans, yAxis, + 0.0f, (float) Math.PI*2.0f); + BoundingSphere bounds = + new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0); + rotator.setSchedulingBounds(bounds); + objRoot.addChild(rotator); + + // Have Java 3D perform optimizations on this scene graph. + //objRoot.compile(); + + return objRoot; + } + + private Canvas3D initScene() { + GraphicsConfiguration config = + SimpleUniverse.getPreferredConfiguration(); + + Canvas3D c = new Canvas3D(config); + + BranchGroup scene = createSceneGraph(); + u = new SimpleUniverse(c); + + ViewingPlatform viewingPlatform = u.getViewingPlatform(); + // This will move the ViewPlatform back a bit so the + // objects in the scene can be viewed. + viewingPlatform.setNominalViewingTransform(); + + u.addBranchGraph(scene); + + return c; + } + + /** + * Creates new form SamplerTestGLSL + */ + public SamplerTestGLSL() { + // 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); + } + + // ---------------------------------------------------------------- + + /** 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("SamplerTestGLSL"); + drawingPanel.setLayout(new java.awt.BorderLayout()); + + drawingPanel.setPreferredSize(new java.awt.Dimension(500, 500)); + 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 SamplerTestGLSL().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/glsl_shader/envmap.frag b/src/classes/org/jdesktop/j3d/examples/glsl_shader/envmap.frag new file mode 100644 index 0000000..3e298f8 --- /dev/null +++ b/src/classes/org/jdesktop/j3d/examples/glsl_shader/envmap.frag @@ -0,0 +1,61 @@ +// +// Fragment shader for environment mapping with an +// equirectangular 2D texture +// +// Authors: John Kessenich, Randi Rost +// +// Copyright (c) 2002-2004 3Dlabs Inc. Ltd. +// +// See 3Dlabs-License.txt for license information +// + +const vec3 Xunitvec = vec3 (1.0, 0.0, 0.0); +const vec3 Yunitvec = vec3 (0.0, 1.0, 0.0); + +uniform vec3 BaseColor; +uniform float MixRatio; + +uniform sampler2D EnvMap; + +varying vec3 Normal; +varying vec3 EyeDir; +varying float LightIntensity; + +void main (void) +{ + // Compute reflection vector + vec3 reflectDir = reflect(EyeDir, Normal); + + // Compute altitude and azimuth angles + + vec2 index; + + index.y = dot(normalize(reflectDir), Yunitvec); + reflectDir.y = 0.0; + index.x = dot(normalize(reflectDir), Xunitvec) * 0.5; + + // Translate index values into proper range + + if (reflectDir.z >= 0.0) + index = (index + 1.0) * 0.5; + else + { + index.t = (index.t + 1.0) * 0.5; + index.s = (-index.s) * 0.5 + 1.0; + } + + // if reflectDir.z >= 0.0, s will go from 0.25 to 0.75 + // if reflectDir.z < 0.0, s will go from 0.75 to 1.25, and + // that's OK, because we've set the texture to wrap. + + // Do a lookup into the environment map. + + vec3 envColor = vec3 (texture2D(EnvMap, index)); + + // Add lighting to base color and mix + + vec3 base = LightIntensity * BaseColor; + envColor = mix(envColor, base, MixRatio); + + gl_FragColor = vec4 (envColor, 1.0); +} diff --git a/src/classes/org/jdesktop/j3d/examples/glsl_shader/envmap.vert b/src/classes/org/jdesktop/j3d/examples/glsl_shader/envmap.vert new file mode 100644 index 0000000..d4e8b44 --- /dev/null +++ b/src/classes/org/jdesktop/j3d/examples/glsl_shader/envmap.vert @@ -0,0 +1,25 @@ +// +// Vertex shader for environment mapping with an +// equirectangular 2D texture +// +// Authors: John Kessenich, Randi Rost +// +// Copyright (c) 2002-2004 3Dlabs Inc. Ltd. +// +// See 3Dlabs-License.txt for license information +// + +varying vec3 Normal; +varying vec3 EyeDir; +varying float LightIntensity; + +uniform vec3 LightPos; + +void main(void) +{ + gl_Position = ftransform(); + Normal = normalize(gl_NormalMatrix * gl_Normal); + vec4 pos = gl_ModelViewMatrix * gl_Vertex; + EyeDir = pos.xyz; + LightIntensity = max(dot(normalize(LightPos - EyeDir), Normal), 0.0); +} diff --git a/src/classes/org/jdesktop/j3d/examples/glsl_shader/multitex.frag b/src/classes/org/jdesktop/j3d/examples/glsl_shader/multitex.frag new file mode 100644 index 0000000..b0426cd --- /dev/null +++ b/src/classes/org/jdesktop/j3d/examples/glsl_shader/multitex.frag @@ -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$ + */ + +uniform float cloudFactor; +uniform sampler2D earthTex; +uniform sampler2D cloudTex; + +uniform sampler2D EnvMap; + +void main (void) +{ + vec2 tc0 = gl_TexCoord[0].xy; + vec2 tc1 = gl_TexCoord[1].xy; + + vec3 color0 = vec3(texture2D(cloudTex, tc0)); + vec3 color1 = vec3(texture2D(earthTex, tc1)); + vec3 finalColor = color0*cloudFactor + color1; + + gl_FragColor = vec4(finalColor, 1.0); +} diff --git a/src/resources/images/duke-gears.jpg b/src/resources/images/duke-gears.jpg Binary files differnew file mode 100644 index 0000000..5b98667 --- /dev/null +++ b/src/resources/images/duke-gears.jpg |