summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkcr <kcr@28c7f869-5b4e-e670-f602-82bfaf57f300>2005-12-06 19:46:01 +0000
committerkcr <kcr@28c7f869-5b4e-e670-f602-82bfaf57f300>2005-12-06 19:46:01 +0000
commita6f7445079ee5b8b24e48c456dff31ff70e1b5b1 (patch)
tree3f88c2f94c24fa5e1a6ab9a2d75bfe9778c92422
parentc6805f980728cf8ebe1a329dc0e584c741accc8d (diff)
Issue 198 : Need more shader example programs
1) Added Phong shading example 2) Minor mod to SphereGLSL 3) Added toon shader
-rw-r--r--src/GLSLShaderTest/PhongShadingGLSL.form138
-rw-r--r--src/GLSLShaderTest/PhongShadingGLSL.java448
-rw-r--r--src/GLSLShaderTest/SphereGLSL.java2
-rw-r--r--src/GLSLShaderTest/gouraud.frag50
-rw-r--r--src/GLSLShaderTest/gouraud.vert100
-rw-r--r--src/GLSLShaderTest/phong.frag98
-rw-r--r--src/GLSLShaderTest/phong.vert56
-rw-r--r--src/GLSLShaderTest/toon.frag34
-rw-r--r--src/GLSLShaderTest/toon.vert19
9 files changed, 943 insertions, 2 deletions
diff --git a/src/GLSLShaderTest/PhongShadingGLSL.form b/src/GLSLShaderTest/PhongShadingGLSL.form
new file mode 100644
index 0000000..dcccf73
--- /dev/null
+++ b/src/GLSLShaderTest/PhongShadingGLSL.form
@@ -0,0 +1,138 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+
+<Form version="1.0" type="org.netbeans.modules.form.forminfo.JFrameFormInfo">
+ <NonVisualComponents>
+ <Component class="javax.swing.ButtonGroup" name="shaderButtonGroup">
+ </Component>
+ <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="defaultCloseOperation" type="int" value="3"/>
+ <Property name="title" type="java.lang.String" value="Phong Shading Test"/>
+ </Properties>
+ <SyntheticProperties>
+ <SyntheticProperty name="menuBar" type="java.lang.String" value="jMenuBar1"/>
+ <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="guiPanel">
+ <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="jPanel1">
+ <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="Shader"/>
+ </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="-1" gridWidth="1" gridHeight="1" fill="2" ipadX="0" ipadY="0" insetsTop="4" insetsLeft="4" insetsBottom="4" insetsRight="4" anchor="10" weightX="0.0" weightY="0.0"/>
+ </Constraint>
+ </Constraints>
+
+ <Layout class="org.netbeans.modules.form.compat2.layouts.DesignGridBagLayout"/>
+ <SubComponents>
+ <Component class="javax.swing.JRadioButton" name="gouraudButton">
+ <Properties>
+ <Property name="buttonGroup" type="javax.swing.ButtonGroup" editor="org.netbeans.modules.form.RADComponent$ButtonGroupPropertyEditor">
+ <ComponentRef name="shaderButtonGroup"/>
+ </Property>
+ <Property name="selected" type="boolean" value="true"/>
+ <Property name="text" type="java.lang.String" value="Per-Vertex Lighting (Gouraud)"/>
+ <Property name="border" type="javax.swing.border.Border" editor="org.netbeans.modules.form.editors2.BorderEditor">
+ <Border info="org.netbeans.modules.form.compat2.border.EmptyBorderInfo">
+ <EmptyBorder bottom="0" left="0" right="0" top="0"/>
+ </Border>
+ </Property>
+ <Property name="margin" type="java.awt.Insets" editor="org.netbeans.beaninfo.editors.InsetsEditor">
+ <Insets value="[0, 0, 0, 0]"/>
+ </Property>
+ </Properties>
+ <Events>
+ <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="gouraudButtonActionPerformed"/>
+ </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="2" insetsLeft="2" insetsBottom="2" insetsRight="2" anchor="10" weightX="0.0" weightY="0.0"/>
+ </Constraint>
+ </Constraints>
+ </Component>
+ <Component class="javax.swing.JRadioButton" name="phongButton">
+ <Properties>
+ <Property name="buttonGroup" type="javax.swing.ButtonGroup" editor="org.netbeans.modules.form.RADComponent$ButtonGroupPropertyEditor">
+ <ComponentRef name="shaderButtonGroup"/>
+ </Property>
+ <Property name="text" type="java.lang.String" value="Per-Pixel Lighting (Phong)"/>
+ <Property name="border" type="javax.swing.border.Border" editor="org.netbeans.modules.form.editors2.BorderEditor">
+ <Border info="org.netbeans.modules.form.compat2.border.EmptyBorderInfo">
+ <EmptyBorder bottom="0" left="0" right="0" top="0"/>
+ </Border>
+ </Property>
+ <Property name="margin" type="java.awt.Insets" editor="org.netbeans.beaninfo.editors.InsetsEditor">
+ <Insets value="[0, 0, 0, 0]"/>
+ </Property>
+ </Properties>
+ <Events>
+ <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="phongButtonActionPerformed"/>
+ </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="2" insetsLeft="2" insetsBottom="2" insetsRight="2" 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>
+</Form>
diff --git a/src/GLSLShaderTest/PhongShadingGLSL.java b/src/GLSLShaderTest/PhongShadingGLSL.java
new file mode 100644
index 0000000..9958dc0
--- /dev/null
+++ b/src/GLSLShaderTest/PhongShadingGLSL.java
@@ -0,0 +1,448 @@
+/*
+ * $RCSfile$
+ *
+ * Copyright (c) 2005 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$
+ */
+
+import com.sun.j3d.utils.geometry.Sphere;
+import com.sun.j3d.utils.shader.StringIO;
+import com.sun.j3d.utils.universe.*;
+import javax.media.j3d.*;
+import javax.vecmath.*;
+import java.awt.GraphicsConfiguration;
+import java.io.IOException;
+
+/**
+ *
+ * @author kcr
+ */
+public class PhongShadingGLSL extends javax.swing.JFrame {
+
+ // Constants for type of light to use
+ private static final int DIRECTIONAL_LIGHT = 0;
+ private static final int POINT_LIGHT = 1;
+ private static final int SPOT_LIGHT = 2;
+
+ // Flag indicates type of lights: directional, point, or spot lights.
+ private static int lightType = DIRECTIONAL_LIGHT;
+
+ private SimpleUniverse u = null;
+
+ private ShaderAppearance sApp = null;
+ private ShaderProgram gouraudSP = null;
+ private ShaderProgram phongSP = null;
+
+ 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);
+// Color3f lColor1 = new Color3f(1.0f, 0.0f, 0.0f);
+// Color3f lColor2 = new Color3f(0.0f, 1.0f, 0.0f);
+ Color3f lColor1 = new Color3f(1.0f, 1.0f, 0.5f);
+ Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f);
+ Color3f bgColor = new Color3f(0.05f, 0.05f, 0.2f);
+
+ Transform3D t;
+
+ // Create the root of the branch graph
+ BranchGroup objRoot = new BranchGroup();
+
+ // Create a Transformgroup to scale all objects so they
+ // appear in the scene.
+ TransformGroup objScale = new TransformGroup();
+ Transform3D t3d = new Transform3D();
+ t3d.setScale(0.5);
+ objScale.setTransform(t3d);
+ objRoot.addChild(objScale);
+
+ // Create a bounds for the background and lights
+ BoundingSphere bounds =
+ new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
+
+ // Set up the background
+ 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);
+ objScale.addChild(objTrans);
+
+ // Create a Sphere object, generate one copy of the sphere,
+ // and add it into the scene graph.
+ sApp = new ShaderAppearance();
+ sApp.setCapability(ShaderAppearance.ALLOW_SHADER_PROGRAM_WRITE);
+ Material m = new Material(objColor, eColor, objColor, sColor, 100.0f);
+ sApp.setMaterial(m);
+
+ // Create Gouraud and Phong shader programs
+ String vertexProgram = null;
+ String fragmentProgram = null;
+ Shader[] shaders = new Shader[2];
+ String[] attrNames = { "numLights" };
+
+ try {
+ vertexProgram = StringIO.readFully("./gouraud.vert");
+ fragmentProgram = StringIO.readFully("./gouraud.frag");
+ }
+ catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ 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);
+ gouraudSP = new GLSLShaderProgram();
+ gouraudSP.setShaders(shaders);
+
+ try {
+ vertexProgram = StringIO.readFully("./phong.vert");
+ fragmentProgram = StringIO.readFully("./phong.frag");
+ }
+ catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ 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);
+ phongSP = new GLSLShaderProgram();
+ phongSP.setShaders(shaders);
+
+ if (gouraudButton.isSelected()) {
+ sApp.setShaderProgram(gouraudSP);
+ } else if (phongButton.isSelected()) {
+ sApp.setShaderProgram(phongSP);
+ }
+ Sphere sph = new Sphere(1.0f, Sphere.GENERATE_NORMALS, 30, sApp);
+ 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();
+ yAxis.rotZ(Math.PI);
+ Alpha rotationAlpha = new Alpha(-1, 10000);
+
+ RotationInterpolator rotator =
+ new RotationInterpolator(rotationAlpha, objTrans, yAxis,
+ 0.0f, (float) Math.PI*2.0f);
+ rotator.setSchedulingBounds(bounds);
+ objRoot.addChild(rotator);
+
+ // Create the transform group node for the each light and initialize
+ // it to the identity. Enable the TRANSFORM_WRITE capability so that
+ // our behavior code can modify it at runtime. Add them to the root
+ // of the subgraph.
+ TransformGroup l1RotTrans = new TransformGroup();
+ l1RotTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
+ objScale.addChild(l1RotTrans);
+
+ TransformGroup l2RotTrans = new TransformGroup();
+ l2RotTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
+ objScale.addChild(l2RotTrans);
+
+ // Create transformations for the positional lights
+ t = new Transform3D();
+ Vector3d lPos1 = new Vector3d(0.0, 0.0, 2.0);
+ t.set(lPos1);
+ TransformGroup l1Trans = new TransformGroup(t);
+ l1RotTrans.addChild(l1Trans);
+
+// t = new Transform3D();
+// Vector3d lPos2 = new Vector3d(0.5, 0.8, 2.0);
+// t.set(lPos2);
+// TransformGroup l2Trans = new TransformGroup(t);
+// l2RotTrans.addChild(l2Trans);
+
+ // Create Geometry for point lights
+ ColoringAttributes caL1 = new ColoringAttributes();
+// ColoringAttributes caL2 = new ColoringAttributes();
+ caL1.setColor(lColor1);
+// caL2.setColor(lColor2);
+ Appearance appL1 = new Appearance();
+// Appearance appL2 = new Appearance();
+ appL1.setColoringAttributes(caL1);
+// appL2.setColoringAttributes(caL2);
+ l1Trans.addChild(new Sphere(0.05f, appL1));
+// l2Trans.addChild(new Sphere(0.05f, appL2));
+
+ // Create lights
+ AmbientLight aLgt = new AmbientLight(alColor);
+
+ Light lgt1 = null;
+// Light lgt2 = null;
+
+ Point3f lPoint = new Point3f(0.0f, 0.0f, 0.0f);
+ Point3f atten = new Point3f(1.0f, 0.0f, 0.0f);
+ Vector3f lDirect1 = new Vector3f(lPos1);
+// Vector3f lDirect2 = new Vector3f(lPos2);
+ lDirect1.negate();
+// lDirect2.negate();
+
+ switch (lightType) {
+ case DIRECTIONAL_LIGHT:
+ lgt1 = new DirectionalLight(lColor1, lDirect1);
+// lgt2 = new DirectionalLight(lColor2, lDirect2);
+ break;
+ case POINT_LIGHT:
+ assert false : "can't get here";
+ lgt1 = new PointLight(lColor1, lPoint, atten);
+// lgt2 = new PointLight(lColor2, lPoint, atten);
+ break;
+ case SPOT_LIGHT:
+ assert false : "can't get here";
+ lgt1 = new SpotLight(lColor1, lPoint, atten, lDirect1,
+ 25.0f * (float)Math.PI / 180.0f, 10.0f);
+// lgt2 = new SpotLight(lColor2, lPoint, atten, lDirect2,
+// 25.0f * (float)Math.PI / 180.0f, 10.0f);
+ break;
+ }
+
+ // Set the influencing bounds
+ aLgt.setInfluencingBounds(bounds);
+ lgt1.setInfluencingBounds(bounds);
+// lgt2.setInfluencingBounds(bounds);
+
+ // Add the lights into the scene graph
+ objScale.addChild(aLgt);
+ l1Trans.addChild(lgt1);
+// l2Trans.addChild(lgt2);
+
+ // Create a new Behavior object that will perform the desired
+ // operation on the specified transform object and add it into the
+ // scene graph.
+ yAxis = new Transform3D();
+ Alpha rotor1Alpha = new Alpha(-1, Alpha.INCREASING_ENABLE,
+ 0, 0,
+ 4000, 0, 0,
+ 0, 0, 0);
+ RotationInterpolator rotator1 =
+ new RotationInterpolator(rotor1Alpha,
+ l1RotTrans,
+ yAxis,
+ 0.0f, (float) Math.PI*2.0f);
+ rotator1.setSchedulingBounds(bounds);
+ l1RotTrans.addChild(rotator1);
+
+ // Create a new Behavior object that will perform the desired
+ // operation on the specified transform object and add it into the
+ // scene graph.
+ Alpha rotor2Alpha = new Alpha(-1, Alpha.INCREASING_ENABLE,
+ 0, 0,
+ 1000, 0, 0,
+ 0, 0, 0);
+ RotationInterpolator rotator2 =
+ new RotationInterpolator(rotor2Alpha,
+ l2RotTrans,
+ yAxis,
+ 0.0f, 0.0f);
+ bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
+ rotator2.setSchedulingBounds(bounds);
+ l2RotTrans.addChild(rotator2);
+
+ return objRoot;
+ }
+
+ private Canvas3D initScene() {
+ GraphicsConfiguration config =
+ SimpleUniverse.getPreferredConfiguration();
+
+ Canvas3D c = new Canvas3D(config);
+
+ u = new SimpleUniverse(c);
+
+ // This will move the ViewPlatform back a bit so the
+ // objects in the scene can be viewed.
+ u.getViewingPlatform().setNominalViewingTransform();
+
+ BranchGroup scene = createSceneGraph();
+ u.addBranchGraph(scene);
+
+ return c;
+ }
+
+ /**
+ * Creates new form PhongShadingGLSL
+ */
+ public PhongShadingGLSL() {
+ // 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() {
+ java.awt.GridBagConstraints gridBagConstraints;
+
+ shaderButtonGroup = new javax.swing.ButtonGroup();
+ guiPanel = new javax.swing.JPanel();
+ jPanel1 = new javax.swing.JPanel();
+ gouraudButton = new javax.swing.JRadioButton();
+ phongButton = new javax.swing.JRadioButton();
+ drawingPanel = new javax.swing.JPanel();
+ jMenuBar1 = new javax.swing.JMenuBar();
+ fileMenu = new javax.swing.JMenu();
+ exitMenuItem = new javax.swing.JMenuItem();
+
+ setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
+ setTitle("Phong Shading Test");
+ guiPanel.setLayout(new java.awt.GridBagLayout());
+
+ jPanel1.setLayout(new java.awt.GridBagLayout());
+
+ jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Shader"));
+ shaderButtonGroup.add(gouraudButton);
+ gouraudButton.setSelected(true);
+ gouraudButton.setText("Per-Vertex Lighting (Gouraud)");
+ gouraudButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
+ gouraudButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
+ gouraudButton.addActionListener(new java.awt.event.ActionListener() {
+ public void actionPerformed(java.awt.event.ActionEvent evt) {
+ gouraudButtonActionPerformed(evt);
+ }
+ });
+
+ gridBagConstraints = new java.awt.GridBagConstraints();
+ gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
+ gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
+ jPanel1.add(gouraudButton, gridBagConstraints);
+
+ shaderButtonGroup.add(phongButton);
+ phongButton.setText("Per-Pixel Lighting (Phong)");
+ phongButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
+ phongButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
+ phongButton.addActionListener(new java.awt.event.ActionListener() {
+ public void actionPerformed(java.awt.event.ActionEvent evt) {
+ phongButtonActionPerformed(evt);
+ }
+ });
+
+ gridBagConstraints = new java.awt.GridBagConstraints();
+ gridBagConstraints.gridx = 0;
+ gridBagConstraints.gridy = 1;
+ gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
+ gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
+ jPanel1.add(phongButton, gridBagConstraints);
+
+ gridBagConstraints = new java.awt.GridBagConstraints();
+ gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
+ gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
+ guiPanel.add(jPanel1, gridBagConstraints);
+
+ getContentPane().add(guiPanel, java.awt.BorderLayout.NORTH);
+
+ drawingPanel.setLayout(new java.awt.BorderLayout());
+
+ drawingPanel.setPreferredSize(new java.awt.Dimension(500, 500));
+ getContentPane().add(drawingPanel, 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 phongButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_phongButtonActionPerformed
+ sApp.setShaderProgram(phongSP);
+ }//GEN-LAST:event_phongButtonActionPerformed
+
+ private void gouraudButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gouraudButtonActionPerformed
+ sApp.setShaderProgram(gouraudSP);
+ }//GEN-LAST:event_gouraudButtonActionPerformed
+
+ private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
+ System.exit(0);
+ }//GEN-LAST:event_exitMenuItemActionPerformed
+
+ /**
+ * @param args the command line arguments
+ */
+ public static void main(String args[]) {
+ java.awt.EventQueue.invokeLater(new Runnable() {
+ public void run() {
+ new PhongShadingGLSL().setVisible(true);
+ }
+ });
+ }
+
+ // Variables declaration - do not modify//GEN-BEGIN:variables
+ private javax.swing.JPanel drawingPanel;
+ private javax.swing.JMenuItem exitMenuItem;
+ private javax.swing.JMenu fileMenu;
+ private javax.swing.JRadioButton gouraudButton;
+ private javax.swing.JPanel guiPanel;
+ private javax.swing.JMenuBar jMenuBar1;
+ private javax.swing.JPanel jPanel1;
+ private javax.swing.JRadioButton phongButton;
+ private javax.swing.ButtonGroup shaderButtonGroup;
+ // End of variables declaration//GEN-END:variables
+
+}
diff --git a/src/GLSLShaderTest/SphereGLSL.java b/src/GLSLShaderTest/SphereGLSL.java
index a96dca6..8b7f5c6 100644
--- a/src/GLSLShaderTest/SphereGLSL.java
+++ b/src/GLSLShaderTest/SphereGLSL.java
@@ -156,7 +156,6 @@ public class SphereGLSL extends Applet {
TransformGroup l2Trans = new TransformGroup(t);
l2RotTrans.addChild(l2Trans);
- /*
// Create Geometry for point lights
ColoringAttributes caL1 = new ColoringAttributes();
ColoringAttributes caL2 = new ColoringAttributes();
@@ -168,7 +167,6 @@ public class SphereGLSL extends Applet {
appL2.setColoringAttributes(caL2);
l1Trans.addChild(new Sphere(0.05f, appL1));
l2Trans.addChild(new Sphere(0.05f, appL2));
- */
// Create lights
AmbientLight aLgt = new AmbientLight(alColor);
diff --git a/src/GLSLShaderTest/gouraud.frag b/src/GLSLShaderTest/gouraud.frag
new file mode 100644
index 0000000..912cccf
--- /dev/null
+++ b/src/GLSLShaderTest/gouraud.frag
@@ -0,0 +1,50 @@
+/*
+ * $RCSfile$
+ *
+ * Copyright (c) 2005 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$
+ */
+
+// Simple GLSL fragment program to add the primary and secondary (specular) colors
+
+void main()
+{
+ gl_FragColor = gl_Color + gl_SecondaryColor;
+}
diff --git a/src/GLSLShaderTest/gouraud.vert b/src/GLSLShaderTest/gouraud.vert
new file mode 100644
index 0000000..fe72b26
--- /dev/null
+++ b/src/GLSLShaderTest/gouraud.vert
@@ -0,0 +1,100 @@
+/*
+ * $RCSfile$
+ *
+ * Copyright (c) 2005 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 GLSL vertex program for handling 1 directional light with specular.
+// This implements per-vertex lighting (Gouraud shading).
+
+void directionalLight0(
+ in vec3 normal,
+ inout vec4 ambient,
+ inout vec4 diffuse,
+ inout vec4 specular)
+{
+ // Normalized light direction and half vector
+ vec3 lightDirection = normalize(vec3(gl_LightSource[0].position));
+ vec3 halfVector = normalize(vec3(gl_LightSource[0].halfVector));
+
+ float nDotVP; // normal . light_direction
+ float nDotHV; // normal . light_half_vector
+ float pf; // power factor
+
+ nDotVP = max(0.0, dot(normal, lightDirection));
+ nDotHV = max(0.0, dot(normal, halfVector));
+
+ if (nDotVP == 0.0) {
+ pf = 0.0;
+ }
+ else {
+ pf = pow(nDotHV, gl_FrontMaterial.shininess);
+ }
+
+ ambient += gl_LightSource[0].ambient;
+ diffuse += gl_LightSource[0].diffuse * nDotVP;
+ specular += gl_LightSource[0].specular * pf;
+}
+
+
+void main()
+{
+ vec3 tnorm = normalize(vec3(gl_NormalMatrix * gl_Normal));
+ vec4 amb = vec4(0.0);
+ vec4 diff = vec4(0.0);
+ vec4 spec = vec4(0.0);
+ int i;
+
+ // Transform the vertex
+ vec4 outPosition = gl_ModelViewProjectionMatrix * gl_Vertex;
+
+ directionalLight0(tnorm, amb, diff, spec);
+
+ // Apply the result of the lighting equation
+ vec4 outSecondaryColor = vec4(vec3(spec * gl_FrontMaterial.specular), 1.0);
+ vec4 outColor = vec4(vec3(gl_FrontLightModelProduct.sceneColor +
+ amb * gl_FrontMaterial.ambient +
+ diff * gl_FrontMaterial.diffuse), 1.0);
+
+ gl_FrontColor = outColor;
+ gl_FrontSecondaryColor = outSecondaryColor;
+ gl_Position = outPosition;
+}
diff --git a/src/GLSLShaderTest/phong.frag b/src/GLSLShaderTest/phong.frag
new file mode 100644
index 0000000..c9e4365
--- /dev/null
+++ b/src/GLSLShaderTest/phong.frag
@@ -0,0 +1,98 @@
+/*
+ * $RCSfile$
+ *
+ * Copyright (c) 2005 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 GLSL fragment program for handling 1 directional light with specular.
+// This implements per-pixel lighting (Phong shading)
+
+void directionalLight0(
+ in vec3 normal,
+ inout vec4 ambient,
+ inout vec4 diffuse,
+ inout vec4 specular)
+{
+ // Normalized light direction and half vector
+ vec3 lightDirection = normalize(vec3(gl_LightSource[0].position));
+ vec3 halfVector = normalize(vec3(gl_LightSource[0].halfVector));
+
+ float nDotVP; // normal . light_direction
+ float nDotHV; // normal . light_half_vector
+ float pf; // power factor
+
+ nDotVP = max(0.0, dot(normal, lightDirection));
+ nDotHV = max(0.0, dot(normal, halfVector));
+
+ if (nDotVP == 0.0) {
+ pf = 0.0;
+ }
+ else {
+ pf = pow(nDotHV, gl_FrontMaterial.shininess);
+ }
+
+ ambient += gl_LightSource[0].ambient;
+ diffuse += gl_LightSource[0].diffuse * nDotVP;
+ specular += gl_LightSource[0].specular * pf;
+}
+
+
+// Per-pixel normal (input from vertex shader)
+varying vec3 Normal;
+
+void main()
+{
+ vec3 unitNorm = normalize(Normal);
+ vec4 amb = vec4(0.0);
+ vec4 diff = vec4(0.0);
+ vec4 spec = vec4(0.0);
+ int i;
+
+ directionalLight0(unitNorm, amb, diff, spec);
+
+ // Apply the result of the lighting equation
+ vec4 secondaryColor = vec4(vec3(spec * gl_FrontMaterial.specular), 1.0);
+ vec4 color = vec4(vec3(gl_FrontLightModelProduct.sceneColor +
+ amb * gl_FrontMaterial.ambient +
+ diff * gl_FrontMaterial.diffuse), 1.0);
+
+ gl_FragColor = color + secondaryColor;
+}
diff --git a/src/GLSLShaderTest/phong.vert b/src/GLSLShaderTest/phong.vert
new file mode 100644
index 0000000..58a495f
--- /dev/null
+++ b/src/GLSLShaderTest/phong.vert
@@ -0,0 +1,56 @@
+/*
+ * $RCSfile$
+ *
+ * Copyright (c) 2005 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 GLSL vertex program for doing Phone shading (per-fragment lighting)
+
+// Per-pixel normal (output to fragment shader)
+varying vec3 Normal;
+
+void main()
+{
+ Normal = normalize(vec3(gl_NormalMatrix * gl_Normal));
+
+ // Transform the vertex
+ gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
+}
diff --git a/src/GLSLShaderTest/toon.frag b/src/GLSLShaderTest/toon.frag
new file mode 100644
index 0000000..fa50453
--- /dev/null
+++ b/src/GLSLShaderTest/toon.frag
@@ -0,0 +1,34 @@
+//
+// Fragment shader for cartoon-style shading
+//
+// Author: Philip Rideout
+//
+// Copyright (c) 2004 3Dlabs Inc. Ltd.
+//
+// See 3Dlabs-License.txt for license information
+//
+
+//uniform vec3 DiffuseColor;
+//uniform vec3 PhongColor;
+//uniform float Edge;
+//uniform float Phong;
+
+vec3 DiffuseColor = vec3(0.5,0.5,1.0);
+vec3 PhongColor = vec3(0.75,0.75,1.0);
+float Edge = 0.64;
+float Phong = 0.90;
+
+varying vec3 Normal;
+varying vec3 LightDir;
+
+void main (void)
+{
+ vec3 color = DiffuseColor;
+ float f = max( 0.0, dot(LightDir,Normal));
+ if (abs(f) < Edge)
+ color = DiffuseColor * 0.2;
+ if (f > Phong)
+ color = PhongColor;
+
+ gl_FragColor = vec4(color, 1);
+}
diff --git a/src/GLSLShaderTest/toon.vert b/src/GLSLShaderTest/toon.vert
new file mode 100644
index 0000000..d044af7
--- /dev/null
+++ b/src/GLSLShaderTest/toon.vert
@@ -0,0 +1,19 @@
+//
+// Vertex shader for cartoon-style shading
+//
+// Author: Philip Rideout
+//
+// Copyright (c) 2004 3Dlabs Inc. Ltd.
+//
+// See 3Dlabs-License.txt for license information
+//
+
+varying vec3 Normal;
+varying vec3 LightDir;
+
+void main(void)
+{
+ Normal = normalize(gl_NormalMatrix * gl_Normal);
+ gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
+ LightDir = vec3(normalize(gl_LightSource[0].position));
+}