From f01d00bb62b75a9e0c4d2926be7e02449a271de6 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Tue, 23 Nov 2010 01:56:10 +0100 Subject: JOGL/Junit: Refine TestTransformFeedbackVaryingsBug407NEWT, Add another simple GLSLShader test. --- .../test/junit/jogl/demos/es2/RedSquare0.java | 198 +++++++++++++++++++++ .../jogl/demos/es2/shader/RedSquareShader.java | 68 +++++++ .../test/junit/jogl/glsl/TestGLSLSimple01NEWT.java | 143 +++++++++++++++ .../TestTransformFeedbackVaryingsBug407NEWT.java | 179 +++++++++++-------- .../jogamp/test/junit/util/GLSLSimpleProgram.java | 121 +++++++++++++ 5 files changed, 633 insertions(+), 76 deletions(-) create mode 100644 src/junit/com/jogamp/test/junit/jogl/demos/es2/RedSquare0.java create mode 100644 src/junit/com/jogamp/test/junit/jogl/demos/es2/shader/RedSquareShader.java create mode 100644 src/junit/com/jogamp/test/junit/jogl/glsl/TestGLSLSimple01NEWT.java create mode 100644 src/junit/com/jogamp/test/junit/util/GLSLSimpleProgram.java (limited to 'src/junit/com') diff --git a/src/junit/com/jogamp/test/junit/jogl/demos/es2/RedSquare0.java b/src/junit/com/jogamp/test/junit/jogl/demos/es2/RedSquare0.java new file mode 100644 index 000000000..229635ae5 --- /dev/null +++ b/src/junit/com/jogamp/test/junit/jogl/demos/es2/RedSquare0.java @@ -0,0 +1,198 @@ +/** + * Copyright 2010 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.test.junit.jogl.demos.es2; + +import com.jogamp.common.nio.Buffers; +import com.jogamp.opengl.util.Animator; +import com.jogamp.opengl.util.GLArrayDataWrapper; +import com.jogamp.opengl.util.PMVMatrix; +import com.jogamp.test.junit.jogl.demos.es2.shader.RedSquareShader; +import com.jogamp.test.junit.util.GLSLSimpleProgram; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.nio.FloatBuffer; +import javax.media.opengl.GL; +import javax.media.opengl.GL2ES2; +import javax.media.opengl.GLAutoDrawable; +import javax.media.opengl.GLContext; +import javax.media.opengl.GLEventListener; +import javax.media.opengl.GLUniformData; +import org.junit.Assert; + +public class RedSquare0 implements GLEventListener { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream pbaos = new PrintStream(baos); + GLSLSimpleProgram myShader; + PMVMatrix pmvMatrix; + int mgl_PMVMatrix; + GLUniformData pmvMatrixUniform; + int mgl_Vertex; + int mgl_Color; + long t0; + + public void init(GLAutoDrawable glad) { + GLContext context = glad.getContext(); + context.makeCurrent(); + GL2ES2 gl = context.getGL().getGL2ES2(); + myShader = GLSLSimpleProgram.create(gl, RedSquareShader.VERTEX_SHADER_TEXT, RedSquareShader.FRAGMENT_SHADER_TEXT, true); + gl.glUseProgram(myShader.getShaderProgram()); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + // setup mgl_PMVMatrix + pmvMatrix = new PMVMatrix(); + pmvMatrix.glMatrixMode(PMVMatrix.GL_PROJECTION); + pmvMatrix.glLoadIdentity(); + pmvMatrix.glMatrixMode(PMVMatrix.GL_MODELVIEW); + pmvMatrix.glLoadIdentity(); + mgl_PMVMatrix = gl.glGetUniformLocation(myShader.getShaderProgram(), "mgl_PMVMatrix"); + Assert.assertTrue(0 <= mgl_PMVMatrix); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + pmvMatrixUniform = new GLUniformData("mgl_PMVMatrix", 4, 4, pmvMatrix.glGetPMvMatrixf()); + pmvMatrixUniform.setLocation(mgl_PMVMatrix); + gl.glUniform(pmvMatrixUniform); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + // Allocate Vertex Array + int components = 3; + int numElements = 4; + mgl_Vertex = gl.glGetAttribLocation(myShader.getShaderProgram(), "mgl_Vertex"); + Assert.assertTrue(0 <= mgl_Vertex); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + FloatBuffer buffer = Buffers.newDirectFloatBuffer(numElements * components); + GLArrayDataWrapper vertices = GLArrayDataWrapper.createGLSL(gl, "mgl_Vertex", 3, gl.GL_FLOAT, false, 0, buffer, -1, 0); + { + // Fill them up + FloatBuffer verticeb = (FloatBuffer) vertices.getBuffer(); + verticeb.put(-2); + verticeb.put(2); + verticeb.put(0); + verticeb.put(2); + verticeb.put(2); + verticeb.put(0); + verticeb.put(-2); + verticeb.put(-2); + verticeb.put(0); + verticeb.put(2); + verticeb.put(-2); + verticeb.put(0); + } + buffer.flip(); + vertices.setLocation(mgl_Vertex); + gl.glEnableVertexAttribArray(mgl_Vertex); + gl.glVertexAttribPointer(vertices); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + // Allocate Color Array + components = 4; + numElements = 4; + mgl_Color = gl.glGetAttribLocation(myShader.getShaderProgram(), "mgl_Color"); + Assert.assertTrue(0 <= mgl_Color); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + buffer = Buffers.newDirectFloatBuffer(numElements * components); + GLArrayDataWrapper colors = GLArrayDataWrapper.createGLSL(gl, "mgl_Color", 4, gl.GL_FLOAT, false, 0, buffer, -1, 0); + { + // Fill them up + FloatBuffer colorb = (FloatBuffer) colors.getBuffer(); + colorb.put(1); + colorb.put(0); + colorb.put(0); + colorb.put(1); + colorb.put(0); + colorb.put(0); + colorb.put(1); + colorb.put(1); + colorb.put(1); + colorb.put(0); + colorb.put(0); + colorb.put(1); + colorb.put(1); + colorb.put(0); + colorb.put(0); + colorb.put(1); + } + buffer.flip(); + colors.setLocation(mgl_Color); + gl.glEnableVertexAttribArray(mgl_Color); + gl.glVertexAttribPointer(colors); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + // OpenGL Render Settings + gl.glClearColor(0, 0, 0, 1); + gl.glEnable(GL2ES2.GL_DEPTH_TEST); + gl.glUseProgram(0); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + + t0 = System.currentTimeMillis(); + } + + public void reshape(GLAutoDrawable glad, int x, int y, int width, int height) { + GL2ES2 gl = glad.getGL().getGL2ES2(); + gl.glUseProgram(myShader.getShaderProgram()); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + // Set location in front of camera + pmvMatrix.glMatrixMode(PMVMatrix.GL_PROJECTION); + pmvMatrix.glLoadIdentity(); + pmvMatrix.gluPerspective(45.0F, (float) width / (float) height, 1.0F, 100.0F); + //pmvMatrix.glOrthof(-4.0f, 4.0f, -4.0f, 4.0f, 1.0f, 100.0f); + gl.glUniform(pmvMatrixUniform); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + gl.glUseProgram(0); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + } + + public void display(GLAutoDrawable glad) { + long t1 = System.currentTimeMillis(); + + GL2ES2 gl = glad.getGL().getGL2ES2(); + gl.glUseProgram(myShader.getShaderProgram()); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + // One rotation every four seconds + pmvMatrix.glMatrixMode(PMVMatrix.GL_MODELVIEW); + pmvMatrix.glLoadIdentity(); + pmvMatrix.glTranslatef(0, 0, -10); + float ang = ((float) (t1 - t0) * 360.0F) / 4000.0F; + pmvMatrix.glRotatef(ang, 0, 0, 1); + pmvMatrix.glRotatef(ang, 0, 1, 0); + gl.glUniform(pmvMatrixUniform); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + // Draw a square + gl.glDrawArrays(gl.GL_TRIANGLE_STRIP, 0, 4); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + gl.glUseProgram(0); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + } + + public void dispose(GLAutoDrawable glad) { + GL2ES2 gl = glad.getGL().getGL2ES2(); + gl.glDisableVertexAttribArray(mgl_Vertex); + gl.glDisableVertexAttribArray(mgl_Color); + myShader.release(gl); + Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError()); + pmvMatrix.destroy(); + pmvMatrix = null; + System.err.println("dispose done"); + } +} diff --git a/src/junit/com/jogamp/test/junit/jogl/demos/es2/shader/RedSquareShader.java b/src/junit/com/jogamp/test/junit/jogl/demos/es2/shader/RedSquareShader.java new file mode 100644 index 000000000..5a365be11 --- /dev/null +++ b/src/junit/com/jogamp/test/junit/jogl/demos/es2/shader/RedSquareShader.java @@ -0,0 +1,68 @@ +/** + * Copyright 2010 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.test.junit.jogl.demos.es2.shader; + +public class RedSquareShader { + public static String VERTEX_SHADER_TEXT = + " #ifdef GL_ES\n" + + " #define MEDIUMP mediump\n" + + " #define HIGHP highp\n" + + "#else\n" + + " #define MEDIUMP\n" + + " #define HIGHP\n" + + "#endif\n" + + "\n" + + "uniform MEDIUMP mat4 mgl_PMVMatrix[2];\n" + + "attribute HIGHP vec4 mgl_Vertex;\n" + + "attribute HIGHP vec4 mgl_Color;\n" + + "varying HIGHP vec4 frontColor;\n" + + "\n" + + "void main(void)\n" + + "{\n" + + " frontColor=mgl_Color;\n" + + " gl_Position = mgl_PMVMatrix[0] * mgl_PMVMatrix[1] * mgl_Vertex;\n" + + "}\n" ; + + public static String FRAGMENT_SHADER_TEXT = + "#ifdef GL_ES\n" + + " #define MEDIUMP mediump\n" + + " #define HIGHP highp\n" + + "#else\n" + + " #define MEDIUMP\n" + + " #define HIGHP\n" + + "#endif\n" + + "\n" + + "varying HIGHP vec4 frontColor;\n" + + "\n" + + "void main (void)\n" + + "{\n" + + " gl_FragColor = frontColor;\n" + + "}\n" ; + +} diff --git a/src/junit/com/jogamp/test/junit/jogl/glsl/TestGLSLSimple01NEWT.java b/src/junit/com/jogamp/test/junit/jogl/glsl/TestGLSLSimple01NEWT.java new file mode 100644 index 000000000..3a420e1ef --- /dev/null +++ b/src/junit/com/jogamp/test/junit/jogl/glsl/TestGLSLSimple01NEWT.java @@ -0,0 +1,143 @@ +/** + * Copyright 2010 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.test.junit.jogl.glsl; + +import com.jogamp.test.junit.jogl.demos.es2.RedSquare0; +import com.jogamp.test.junit.util.GLSLSimpleProgram; +import com.jogamp.test.junit.util.UITestCase; + + +import javax.media.opengl.GLCapabilities; +import javax.media.opengl.GLContext; +import javax.media.opengl.GLProfile; + +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.jogamp.newt.opengl.GLWindow; +import com.jogamp.opengl.util.Animator; +import com.jogamp.test.junit.jogl.demos.es2.shader.RedSquareShader; +import com.jogamp.test.junit.util.MiscUtils; + +import java.io.IOException; +import javax.media.opengl.GL2ES2; +import org.junit.AfterClass; + +public class TestGLSLSimple01NEWT extends UITestCase { + static long durationPerTest = 100; // ms + + @BeforeClass + public static void initClass() { + System.err.println("class init"); + GLProfile.initSingleton(true); + } + + @AfterClass + public static void tearDownClass() { + System.err.println("class tear down .."); + GLProfile.shutdown(); + System.err.println("class tear down end"); + } + + @Test(timeout=5000) + public void testGLSLCompilation01() { + GLProfile glp = GLProfile.get(GLProfile.GL2ES2); + Assert.assertNotNull(glp); + GLCapabilities caps = new GLCapabilities(glp); + Assert.assertNotNull(caps); + + GLWindow window = GLWindow.create(caps); + Assert.assertNotNull(window); + window.setSize(800, 600); + window.setVisible(true); + Assert.assertTrue(window.isNativeValid()); + + GLContext context = window.getContext(); + context.setSynchronized(true); + + // trigger native creation of drawable/context + window.display(); + Assert.assertTrue(window.isRealized()); + Assert.assertTrue(window.getContext().isCreated()); + + context.makeCurrent(); + + // given + + GL2ES2 gl = context.getGL().getGL2ES2(); + GLSLSimpleProgram myShader = GLSLSimpleProgram.create(gl, + RedSquareShader.VERTEX_SHADER_TEXT, + RedSquareShader.FRAGMENT_SHADER_TEXT, + true); + + myShader.release(gl); + context.release(); + window.destroy(); + } + + @Test(timeout=5000) + public void testGLSLUse01() throws InterruptedException { + GLProfile glp = GLProfile.get(GLProfile.GL2ES2); + Assert.assertNotNull(glp); + GLCapabilities caps = new GLCapabilities(glp); + Assert.assertNotNull(caps); + + GLWindow window = GLWindow.create(caps); + Assert.assertNotNull(window); + window.setSize(800, 600); + window.setVisible(true); + Assert.assertTrue(window.isNativeValid()); + window.addGLEventListener(new RedSquare0()); + + Animator animator = new Animator(window); + animator.start(); + Assert.assertEquals(true, animator.isAnimating()); + while(animator.isAnimating() && animator.getDuration()