diff options
Diffstat (limited to 'src/test')
7 files changed, 488 insertions, 0 deletions
diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/TessellationShader01aGL4.java b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/TessellationShader01aGL4.java new file mode 100644 index 000000000..a5807a096 --- /dev/null +++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/TessellationShader01aGL4.java @@ -0,0 +1,191 @@ +/** + * Copyright 2014 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions 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. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ +package com.jogamp.opengl.test.junit.jogl.demos.gl4; + +import java.nio.FloatBuffer; + +import javax.media.opengl.GL; +import javax.media.opengl.GL2ES2; +import javax.media.opengl.GL2ES3; +import javax.media.opengl.GL2GL3; +import javax.media.opengl.GL4; +import javax.media.opengl.GLAutoDrawable; +import javax.media.opengl.GLEventListener; + +import com.jogamp.opengl.util.glsl.ShaderCode; +import com.jogamp.opengl.util.glsl.ShaderProgram; + +/** + * JOGL Tessellation ShaderCode GL4 test case. + * <p> + * Demonstrates tessellation-control and -evaluation shaders. + * </p> + * + * @author Raymond L. Rivera, 2014 + * @author Sven Gothel + */ +public class TessellationShader01aGL4 implements GLEventListener { + private static final double ANIMATION_RATE = 950.0; + + private ShaderProgram program; + private final int[] vertexArray = new int[1]; + private FloatBuffer vertexOffset; + private FloatBuffer backgroundColor; + + + @Override + public void init(final GLAutoDrawable auto) { + final double theta = System.currentTimeMillis() / ANIMATION_RATE; + vertexOffset = FloatBuffer.allocate(4); + vertexOffset.put(0, (float)(Math.sin(theta) * 0.5f)); + vertexOffset.put(1, (float)(Math.cos(theta) * 0.6f)); + vertexOffset.put(2, 0.0f); + vertexOffset.put(3, 0.0f); + + backgroundColor = FloatBuffer.allocate(4); + backgroundColor.put(0, 0.25f); + backgroundColor.put(1, 0.25f); + backgroundColor.put(2, 0.25f); + backgroundColor.put(3, 1.0f); + + final GL4 gl = auto.getGL().getGL4(); + program = createProgram(auto); + gl.glGenVertexArrays(vertexArray.length, vertexArray, 0); + gl.glBindVertexArray(vertexArray[0]); + gl.glPatchParameteri(GL4.GL_PATCH_VERTICES, 3); + gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL2GL3.GL_LINE); + } + + @Override + public void display(final GLAutoDrawable auto) { + final GL4 gl = auto.getGL().getGL4(); + final double value = System.currentTimeMillis() / ANIMATION_RATE; + gl.glClearBufferfv(GL2ES3.GL_COLOR, 0, backgroundColor); + gl.glUseProgram(program.program()); + vertexOffset.put(0, (float)(Math.sin(value) * 0.5f)); + vertexOffset.put(1, (float)(Math.cos(value) * 0.6f)); + gl.glVertexAttrib4fv(0, vertexOffset); + gl.glDrawArrays(GL4.GL_PATCHES, 0, 3); + } + + @Override + public void dispose(final GLAutoDrawable auto) { + final GL4 gl = auto.getGL().getGL4(); + gl.glDeleteVertexArrays(vertexArray.length, vertexArray, 0); + program.destroy(gl); + } + + @Override + public void reshape(final GLAutoDrawable auto, final int x, final int y, final int width, final int height) { + // final GL4 gl = auto.getGL().getGL4(); + } + + private ShaderProgram createProgram(final GLAutoDrawable auto) { + final GL4 gl = auto.getGL().getGL4(); + final String vertexSource = + "#version 440 core \n" + + " \n" + + "layout (location = 0) in vec4 offset; \n" + + " \n" + + "void main(void) \n" + + "{ \n" + + " const vec4 vertices[3] = vec4[3] ( \n" + + " vec4( 0.25, 0.25, 0.5, 1.0), \n" + + " vec4(-0.25, -0.25, 0.5, 1.0), \n" + + " vec4( 0.25, -0.25, 0.5, 1.0)); \n" + + " gl_Position = vertices[gl_VertexID] + offset; \n" + + "} \n"; + final String tessCtrlSource = + "#version 440 core \n" + + "layout (vertices = 3) out; \n" + + " \n" + + "void main(void) \n" + + "{ \n" + + " if (gl_InvocationID == 0) \n" + + " { \n" + + " gl_TessLevelInner[0] = 5.0; \n" + + " gl_TessLevelOuter[0] = 5.0; \n" + + " gl_TessLevelOuter[1] = 5.0; \n" + + " gl_TessLevelOuter[2] = 5.0; \n" + + " } \n" + + " gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position;\n" + + "} \n"; + final String tessEvalSource = + "#version 440 core \n" + + " \n" + + "layout (triangles, equal_spacing, cw) in; \n" + + " \n" + + "void main(void) \n" + + "{ \n" + + " gl_Position = (gl_TessCoord.x * gl_in[0].gl_Position) + \n" + + " (gl_TessCoord.y * gl_in[1].gl_Position) + \n" + + " (gl_TessCoord.z * gl_in[2].gl_Position); \n" + + "} \n"; + final String fragmentSource = + "#version 440 core \n" + + " \n" + + "out vec4 color; \n" + + " \n" + + "void main(void) \n" + + "{ \n" + + " color = vec4(1.0, 1.0, 1.0, 1.0); \n" + + "} \n"; + + final ShaderCode vertexShader = createShader(gl, GL2ES2.GL_VERTEX_SHADER, vertexSource); + final ShaderCode tessCtrlShader = createShader(gl, GL4.GL_TESS_CONTROL_SHADER, tessCtrlSource); + final ShaderCode tessEvalShader = createShader(gl, GL4.GL_TESS_EVALUATION_SHADER, tessEvalSource); + final ShaderCode fragmentShader = createShader(gl, GL2ES2.GL_FRAGMENT_SHADER, fragmentSource); + + final ShaderProgram program = new ShaderProgram(); + + program.init(gl); + program.add(vertexShader); + program.add(tessCtrlShader); + program.add(tessEvalShader); + program.add(fragmentShader); + + program.link(gl, System.err); + if(!program.validateProgram(gl, System.out)) + System.err.println("[error] Program linking failed."); + + return program; + } + + private ShaderCode createShader(final GL4 gl, final int shaderType, final String source) { + final String[][] sources = new String[1][1]; + sources[0] = new String[]{ source }; + final ShaderCode shader = new ShaderCode(shaderType, sources.length, sources); + + final boolean compiled = shader.compile(gl, System.err); + if (!compiled) + System.err.println("[error] Shader compilation failed."); + + return shader; + } + +} diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/TessellationShader01bGL4.java b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/TessellationShader01bGL4.java new file mode 100644 index 000000000..7be26e400 --- /dev/null +++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/TessellationShader01bGL4.java @@ -0,0 +1,142 @@ +/** + * Copyright 2014 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions 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. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ +package com.jogamp.opengl.test.junit.jogl.demos.gl4; + +import java.nio.FloatBuffer; + +import javax.media.opengl.GL; +import javax.media.opengl.GL2ES2; +import javax.media.opengl.GL2ES3; +import javax.media.opengl.GL2GL3; +import javax.media.opengl.GL4; +import javax.media.opengl.GLAutoDrawable; +import javax.media.opengl.GLEventListener; +import javax.media.opengl.GLException; + +import com.jogamp.opengl.util.glsl.ShaderCode; +import com.jogamp.opengl.util.glsl.ShaderProgram; + +/** + * JOGL Tessellation ShaderCode GL4 test case. + * <p> + * Demonstrates tessellation-control and -evaluation shaders. + * </p> + * + * @author Raymond L. Rivera, 2014 + * @author Sven Gothel + */ +public class TessellationShader01bGL4 implements GLEventListener { + private static final double ANIMATION_RATE = 950.0; + + private ShaderProgram program; + private final int[] vertexArray = new int[1]; + private FloatBuffer vertexOffset; + private FloatBuffer backgroundColor; + + + @Override + public void init(final GLAutoDrawable auto) { + final double theta = System.currentTimeMillis() / ANIMATION_RATE; + vertexOffset = FloatBuffer.allocate(4); + vertexOffset.put(0, (float)(Math.sin(theta) * 0.5f)); + vertexOffset.put(1, (float)(Math.cos(theta) * 0.6f)); + vertexOffset.put(2, 0.0f); + vertexOffset.put(3, 0.0f); + + backgroundColor = FloatBuffer.allocate(4); + backgroundColor.put(0, 0.25f); + backgroundColor.put(1, 0.25f); + backgroundColor.put(2, 0.25f); + backgroundColor.put(3, 1.0f); + + final GL4 gl = auto.getGL().getGL4(); + program = createProgram(auto); + gl.glGenVertexArrays(vertexArray.length, vertexArray, 0); + gl.glBindVertexArray(vertexArray[0]); + gl.glPatchParameteri(GL4.GL_PATCH_VERTICES, 3); + gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL2GL3.GL_LINE); + } + + @Override + public void display(final GLAutoDrawable auto) { + final GL4 gl = auto.getGL().getGL4(); + final double value = System.currentTimeMillis() / ANIMATION_RATE; + gl.glClearBufferfv(GL2ES3.GL_COLOR, 0, backgroundColor); + gl.glUseProgram(program.program()); + vertexOffset.put(0, (float)(Math.sin(value) * 0.5f)); + vertexOffset.put(1, (float)(Math.cos(value) * 0.6f)); + gl.glVertexAttrib4fv(0, vertexOffset); + gl.glDrawArrays(GL4.GL_PATCHES, 0, 3); + } + + @Override + public void dispose(final GLAutoDrawable auto) { + final GL4 gl = auto.getGL().getGL4(); + gl.glDeleteVertexArrays(vertexArray.length, vertexArray, 0); + program.destroy(gl); + } + + @Override + public void reshape(final GLAutoDrawable auto, final int x, final int y, final int width, final int height) { + // final GL4 gl = auto.getGL().getGL4(); + } + + static final String shaderBasename = "tess_example01"; + + private ShaderProgram createProgram(final GLAutoDrawable auto) { + final GL4 gl = auto.getGL().getGL4(); + + final ShaderProgram sp; + { + final ShaderCode vs, tcs, tes, fs; + vs = ShaderCode.create(gl, GL2ES2.GL_VERTEX_SHADER, this.getClass(), + "shader", "shader/bin", shaderBasename, true); + tcs = ShaderCode.create(gl, GL4.GL_TESS_CONTROL_SHADER, this.getClass(), + "shader", "shader/bin", shaderBasename, true); + tes = ShaderCode.create(gl, GL4.GL_TESS_EVALUATION_SHADER, this.getClass(), + "shader", "shader/bin", shaderBasename, true); + fs = ShaderCode.create(gl, GL2ES2.GL_FRAGMENT_SHADER, this.getClass(), + "shader", "shader/bin", shaderBasename, true); + vs.defaultShaderCustomization(gl, true, true); + tcs.defaultShaderCustomization(gl, true, true); + tes.defaultShaderCustomization(gl, true, true); + fs.defaultShaderCustomization(gl, true, true); + + sp = new ShaderProgram(); + sp.add(gl, vs, System.err); + sp.add(gl, tcs, System.err); + sp.add(gl, tes, System.err); + sp.add(gl, fs, System.err); + if(!sp.link(gl, System.err)) { + throw new GLException("Couldn't link program: "+sp); + } + } + + return sp; + } +} diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/newt/TestTessellationShader01GL4NEWT.java b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/newt/TestTessellationShader01GL4NEWT.java new file mode 100644 index 000000000..a05dcec06 --- /dev/null +++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/newt/TestTessellationShader01GL4NEWT.java @@ -0,0 +1,117 @@ +/** + * Copyright 2014 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions 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. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ +package com.jogamp.opengl.test.junit.jogl.demos.gl4.newt; + +import java.io.IOException; + +import javax.media.opengl.GLCapabilities; +import javax.media.opengl.GLEventListener; +import javax.media.opengl.GLProfile; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.FixMethodOrder; +import org.junit.runners.MethodSorters; + +import com.jogamp.newt.opengl.GLWindow; +import com.jogamp.opengl.test.junit.jogl.demos.gl4.TessellationShader01aGL4; +import com.jogamp.opengl.test.junit.jogl.demos.gl4.TessellationShader01bGL4; +import com.jogamp.opengl.test.junit.util.MiscUtils; +import com.jogamp.opengl.test.junit.util.QuitAdapter; +import com.jogamp.opengl.test.junit.util.UITestCase; +import com.jogamp.opengl.util.Animator; + +/** + * Test Geometry shader demo TessellationShader01aGL4 and TessellationShader01bGL4 + */ +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class TestTessellationShader01GL4NEWT extends UITestCase { + static long duration = 500; // ms + + static GLCapabilities getCaps(final String profile) { + if( !GLProfile.isAvailable(profile) ) { + System.err.println("Profile "+profile+" n/a"); + return null; + } + return new GLCapabilities(GLProfile.get(profile)); + } + + @Test + public void test01_01a() throws InterruptedException { + final GLCapabilities caps = getCaps(GLProfile.GL4); + if( null == caps ) { return; } + testImpl(caps, new TessellationShader01aGL4()); + } + + @Test + public void test02_01b() throws InterruptedException { + final GLCapabilities caps = getCaps(GLProfile.GL4); + if( null == caps ) { return; } + testImpl(caps, new TessellationShader01bGL4()); + } + + private void testImpl(final GLCapabilities caps, final GLEventListener glel) throws InterruptedException { + final GLWindow glWindow = GLWindow.create(caps); + Assert.assertNotNull(glWindow); + glWindow.setSize(800, 600); + glWindow.setVisible(true); + glWindow.setTitle("JOGL Tessellation Shader Test"); + Assert.assertTrue(glWindow.isNativeValid()); + + final QuitAdapter quitAdapter = new QuitAdapter(); + glWindow.addKeyListener(quitAdapter); + glWindow.addWindowListener(quitAdapter); + glWindow.addGLEventListener( glel ); + + final SnapshotGLEventListener snapshotGLEventListener = new SnapshotGLEventListener(); + glWindow.addGLEventListener(snapshotGLEventListener); + + final Animator animator = new Animator(glWindow); + animator.start(); + + animator.setUpdateFPSFrames(60, System.err); + snapshotGLEventListener.setMakeSnapshot(); + + while(!quitAdapter.shouldQuit() && animator.isAnimating() && animator.getTotalFPSDuration()<duration) { + Thread.sleep(100); + } + + animator.stop(); + glWindow.destroy(); + } + + public static void main(final String args[]) throws IOException { + for(int i=0; i<args.length; i++) { + if(args[i].equals("-time")) { + i++; + duration = MiscUtils.atol(args[i], duration); + } + } + org.junit.runner.JUnitCore.main(TestTessellationShader01GL4NEWT.class.getName()); + } +} diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/shader/tess_example01.fp b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/shader/tess_example01.fp new file mode 100644 index 000000000..8a3b23203 --- /dev/null +++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/shader/tess_example01.fp @@ -0,0 +1,6 @@ + +out vec4 color; + +void main(void) { + color = vec4(1.0, 1.0, 1.0, 1.0); +} diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/shader/tess_example01.tcp b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/shader/tess_example01.tcp new file mode 100644 index 000000000..b76aa580d --- /dev/null +++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/shader/tess_example01.tcp @@ -0,0 +1,12 @@ + +layout (vertices = 3) out; + +void main(void) { + if (gl_InvocationID == 0) { + gl_TessLevelInner[0] = 5.0; + gl_TessLevelOuter[0] = 5.0; + gl_TessLevelOuter[1] = 5.0; + gl_TessLevelOuter[2] = 5.0; + } + gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position; +} diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/shader/tess_example01.tep b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/shader/tess_example01.tep new file mode 100644 index 000000000..9c307f5d4 --- /dev/null +++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/shader/tess_example01.tep @@ -0,0 +1,8 @@ + +layout (triangles, equal_spacing, cw) in; + +void main(void) { + gl_Position = (gl_TessCoord.x * gl_in[0].gl_Position) + + (gl_TessCoord.y * gl_in[1].gl_Position) + + (gl_TessCoord.z * gl_in[2].gl_Position); +} diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/shader/tess_example01.vp b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/shader/tess_example01.vp new file mode 100644 index 000000000..c207c1335 --- /dev/null +++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/shader/tess_example01.vp @@ -0,0 +1,12 @@ + +layout (location = 0) in vec4 offset; + +void main(void) +{ + const vec4 vertices[3] = + vec4[3] ( + vec4( 0.25, 0.25, 0.5, 1.0), + vec4(-0.25, -0.25, 0.5, 1.0), + vec4( 0.25, -0.25, 0.5, 1.0) ); + gl_Position = vertices[gl_VertexID] + offset; +} |