diff options
author | Sven Gothel <[email protected]> | 2013-01-12 09:04:31 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2013-01-12 09:04:31 +0100 |
commit | 62c8fcc30dd5f9558df9ca907a6936c7bc252527 (patch) | |
tree | 304acfb4d3628c809d49f06a894c5ba1b3081831 /src/test/com | |
parent | a1bc317c24d55da1199450fe4c9c85d1105844b5 (diff) |
Adding GEOMETRY_SHADER support in ShaderCode, adding core GL3/GEOMETRY_SHADER unit tests. ; Simplified GLContext version number
- Adding GEOMETRY_SHADER support in ShaderCode, adding core GL3/GEOMETRY_SHADER unit tests
Chuck Ritola reported in December 2012 that we lack support of GEOMETRY_SHADER
and he provided a test case.
The latter is cleaned up to use GL3 core profile features only
tesing a pass-through and the flip-XYZ geometry shader.
ShaderUtil is fixed.
- Simplified GLContext version number
The OpenGL major/minor version is now hold in a VersionNumber instance
to simplify usage. Also expose it via getGLVersionNumber() while marking
getGLVersionMajor() and getGLVersionMinor() deprecated.
Diffstat (limited to 'src/test/com')
6 files changed, 522 insertions, 0 deletions
diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl3/GeomShader01TextureGL3.java b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl3/GeomShader01TextureGL3.java new file mode 100644 index 000000000..929491187 --- /dev/null +++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl3/GeomShader01TextureGL3.java @@ -0,0 +1,293 @@ +/** + * Copyright 2013 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.gl3; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URLConnection; +import java.nio.FloatBuffer; + +import javax.media.opengl.GL; +import javax.media.opengl.GL2ES2; +import javax.media.opengl.GL3; +import javax.media.opengl.GLAutoDrawable; +import javax.media.opengl.GLEventListener; +import javax.media.opengl.GLException; +import javax.media.opengl.GLUniformData; +import javax.media.opengl.fixedfunc.GLMatrixFunc; + +import com.jogamp.common.util.IOUtil; +import com.jogamp.opengl.util.GLArrayDataServer; +import com.jogamp.opengl.util.PMVMatrix; +import com.jogamp.opengl.util.glsl.ShaderCode; +import com.jogamp.opengl.util.glsl.ShaderProgram; +import com.jogamp.opengl.util.glsl.ShaderState; +import com.jogamp.opengl.util.glsl.ShaderUtil; +import com.jogamp.opengl.util.texture.Texture; +import com.jogamp.opengl.util.texture.TextureCoords; +import com.jogamp.opengl.util.texture.TextureData; +import com.jogamp.opengl.util.texture.TextureIO; + +/** + * JOGL Geometry ShaderCode test case using OpenGL 3.2 core profile features only. + * <p> + * Demonstrates <code>pass through</code> and <code>XYZ flipping</code> + * geometry shader. + * </p> + * <p> + * If the <code>XYZ flipping</code> geometry shader functions properly, + * the texture will be flipped horizontally and vertically. + * </p> + * + * @author Chuck Ritola December 2012 + * @author Sven Gothel (GL3 core, pass-though, core geometry shader) + */ +public class GeomShader01TextureGL3 implements GLEventListener { + private final int geomShader; + private Texture texture; + private ShaderState st; + private PMVMatrix pmvMatrix; + private GLUniformData pmvMatrixUniform; + private GLArrayDataServer interleavedVBO; + + static final String shaderBasename = "texture01_xxx"; + static final String[] geomShaderBaseNames = new String[] { "passthrough01_xxx", "flipXYZ01_xxx" }; + + public GeomShader01TextureGL3(int geomShader) { + this.geomShader = geomShader; + } + + @Override + public void init(GLAutoDrawable drawable) { + { + final GL gl = drawable.getGL(); + System.err.println("Init - START - useGeomShader "+geomShader+" -> "+geomShaderBaseNames[geomShader]); + System.err.println("GL_VENDOR: " + gl.glGetString(GL.GL_VENDOR)); + System.err.println("GL_RENDERER: " + gl.glGetString(GL.GL_RENDERER)); + System.err.println("GL_VERSION: " + gl.glGetString(GL.GL_VERSION)); + System.err.println("GL GLSL: "+gl.hasGLSL()+", has-compiler: "+gl.isFunctionAvailable("glCompileShader")+", version "+(gl.hasGLSL() ? gl.glGetString(GL2ES2.GL_SHADING_LANGUAGE_VERSION) : "none")); + System.err.println("GL Profile: "+gl.getGLProfile()); + System.err.println("GL Renderer Quirks:" + gl.getContext().getRendererQuirks().toString()); + System.err.println("GL:" + gl + ", " + gl.getContext().getGLVersion()); + if( !gl.isGL3() ) { + throw new RuntimeException("GL object not a GL3 core compatible profile: "+gl); + } + if( !ShaderUtil.isGeometryShaderSupported(gl) ) { + throw new RuntimeException("GL object not >= 3.2, i.e. no geometry shader support.: "+gl); + } + } + final GL3 gl = drawable.getGL().getGL3(); + + final ShaderProgram sp; + { + final ShaderCode vs, gs, fs; + vs = ShaderCode.create(gl, GL2ES2.GL_VERTEX_SHADER, this.getClass(), + "shader", "shader/bin", shaderBasename, true); + gs = ShaderCode.create(gl, GL3.GL_GEOMETRY_SHADER, this.getClass(), + "shader", "shader/bin", geomShaderBaseNames[geomShader], true); + fs = ShaderCode.create(gl, GL2ES2.GL_FRAGMENT_SHADER, this.getClass(), + "shader", "shader/bin", shaderBasename, true); + vs.defaultShaderCustomization(gl, true, null); + gs.defaultShaderCustomization(gl, true, null); + fs.defaultShaderCustomization(gl, true, null); + + sp = new ShaderProgram(); + sp.add(gl, vs, System.err); + sp.add(gl, gs, System.err); + sp.add(gl, fs, System.err); + if(!sp.link(gl, System.err)) { + throw new GLException("Couldn't link program: "+sp); + } + } + + st=new ShaderState(); + st.attachShaderProgram(gl, sp, true); + + // setup mgl_PMVMatrix + pmvMatrix = new PMVMatrix(); + pmvMatrix.glMatrixMode(PMVMatrix.GL_PROJECTION); + pmvMatrix.glLoadIdentity(); + pmvMatrix.glMatrixMode(PMVMatrix.GL_MODELVIEW); + pmvMatrix.glLoadIdentity(); + pmvMatrixUniform = new GLUniformData("mgl_PMVMatrix", 4, 4, pmvMatrix.glGetPMvMatrixf()); // P, Mv + st.ownUniform(pmvMatrixUniform); + st.uniform(gl, pmvMatrixUniform); + + st.ownUniform(pmvMatrixUniform); + if(!st.uniform(gl, pmvMatrixUniform)) { + throw new GLException("Error setting PMVMatrix in shader: "+st); + } + if(!st.uniform(gl, new GLUniformData("mgl_ActiveTexture", 0))) { + throw new GLException("Error setting mgl_ActiveTexture in shader: "+st); + } + + try { + texture = createTestTexture(gl); + } catch (IOException e) { + throw new RuntimeException(e); + } + if(null == texture) { + throw new RuntimeException("Could not load test texture"); + } + + // Tri order: + // TL, BL, BR + // TL, TR, BR + { + int i=0; + TextureCoords tc = texture.getImageTexCoords(); + s_triTexCoords[i++] = tc.left(); s_triTexCoords[i++] = tc.top(); + s_triTexCoords[i++] = tc.left(); s_triTexCoords[i++] = tc.bottom(); + s_triTexCoords[i++] = tc.right(); s_triTexCoords[i++] = tc.bottom(); + s_triTexCoords[i++] = tc.left(); s_triTexCoords[i++] = tc.top(); + s_triTexCoords[i++] = tc.right(); s_triTexCoords[i++] = tc.top(); + s_triTexCoords[i++] = tc.right(); s_triTexCoords[i++] = tc.bottom(); + } + + interleavedVBO = GLArrayDataServer.createGLSLInterleaved(2+4+2, GL.GL_FLOAT, false, 3*6, GL.GL_STATIC_DRAW); + { + interleavedVBO.addGLSLSubArray("mgl_Vertex", 2, GL.GL_ARRAY_BUFFER); + interleavedVBO.addGLSLSubArray("mgl_Color", 4, GL.GL_ARRAY_BUFFER); + interleavedVBO.addGLSLSubArray("mgl_MultiTexCoord", 2, GL.GL_ARRAY_BUFFER); + + FloatBuffer ib = (FloatBuffer)interleavedVBO.getBuffer(); + + for(int i=0; i<6; i++) { + ib.put(s_triVertices, i*2, 2); + ib.put(s_triColors, i*4, 4); + ib.put(s_triTexCoords, i*2, 2); + } + } + interleavedVBO.seal(gl, true); + interleavedVBO.enableBuffer(gl, false); + st.ownAttribute(interleavedVBO, true); + + gl.glClearColor(0f, 0f, 0f, 0f); + gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); + st.useProgram(gl, false); + } + + private Texture createTestTexture(GL3 gl) throws IOException { + final URLConnection urlConn = IOUtil.getResource(this.getClass(), "../../util/texture/test-ntscI01-160x90.png"); + if(null == urlConn) { return null; } + final InputStream istream = urlConn.getInputStream(); + if(null == istream) { return null; } + final TextureData texData = TextureIO.newTextureData(gl.getGLProfile(), istream, false /* mipmap */, TextureIO.PNG); + final Texture res = TextureIO.newTexture(gl, texData); + texData.destroy(); + return res; + } + + @Override + public void dispose(GLAutoDrawable drawable) { + final GL3 gl = drawable.getGL().getGL3(); + if(null!=texture) { + texture.disable(gl); + texture.destroy(gl); + } + + if(null != st) { + pmvMatrixUniform = null; + pmvMatrix.destroy(); + pmvMatrix=null; + st.destroy(gl); + st=null; + } + } + + @Override + public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { + GL3 gl = drawable.getGL().getGL3(); + + gl.setSwapInterval(1); + + // Clear background to white + gl.glClearColor(1.0f, 1.0f, 1.0f, 0.4f); + + if(null != st) { + pmvMatrix.glMatrixMode(GLMatrixFunc.GL_PROJECTION); + pmvMatrix.glLoadIdentity(); + pmvMatrix.glOrthof(-1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 10.0f); + + pmvMatrix.glMatrixMode(GLMatrixFunc.GL_MODELVIEW); + pmvMatrix.glLoadIdentity(); + + st.useProgram(gl, true); + st.uniform(gl, pmvMatrixUniform); + st.useProgram(gl, false); + } + } + + @Override + public void display(GLAutoDrawable drawable) { + final GL3 gl = drawable.getGL().getGL3(); + + gl.glClear(GL.GL_DEPTH_BUFFER_BIT | GL.GL_COLOR_BUFFER_BIT); + + if(null != st) { + //Draw the image as a pseudo-quad using two triangles + st.useProgram(gl, true); + interleavedVBO.enableBuffer(gl, true); + gl.glActiveTexture(GL.GL_TEXTURE0); + texture.enable(gl); + texture.bind(gl); + + gl.glDrawArrays(GL.GL_TRIANGLES, 0, 6); + + texture.disable(gl); + interleavedVBO.enableBuffer(gl, false); + st.useProgram(gl, false); + } + }//end display() + + private static final float[] s_triVertices = { + -1f, 1f, // TL + -1f, -1f, // BL + 1f, -1f, // BR + -1f, 1f, // TL + 1f, 1f, // TR + 1f, -1f // BR + }; + private static final float[] s_triColors = { + 1f, 1f, 1f, 1f, + 1f, 1f, 1f, 1f, + 1f, 1f, 1f, 1f, + 1f, 1f, 1f, 1f, + 1f, 1f, 1f, 1f, + 1f, 1f, 1f, 1f + }; + private static final float[] s_triTexCoords = { + 0f, 1f, // TL + 0f, 0f, // BL + 1f, 0f, // BR + 0f, 1f, // TL + 1f, 1f, // TR + 1f, 0f // BR + }; + +}//end Test diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl3/newt/TestGeomShader01TextureGL3NEWT.java b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl3/newt/TestGeomShader01TextureGL3NEWT.java new file mode 100644 index 000000000..71c0ae7ac --- /dev/null +++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl3/newt/TestGeomShader01TextureGL3NEWT.java @@ -0,0 +1,126 @@ +/** + * Copyright 2013 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.gl3.newt; + +import java.io.IOException; + +import javax.media.opengl.GLCapabilities; +import javax.media.opengl.GLProfile; + +import org.junit.Assert; +import org.junit.Test; + +import com.jogamp.newt.opengl.GLWindow; +import com.jogamp.opengl.test.junit.jogl.demos.gl3.GeomShader01TextureGL3; +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 GeomShader01TextureGL3 + */ +public class TestGeomShader01TextureGL3NEWT extends UITestCase { + static long duration = 500; // ms + + static GLCapabilities getCaps(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_GL3Core_Passthrough() throws InterruptedException { + GLCapabilities caps = getCaps(GLProfile.GL3); + if( null == caps ) { return; } + testImpl(caps, 0); + } + + @Test + public void test02_GL3Core_FlipXYZ() throws InterruptedException { + GLCapabilities caps = getCaps(GLProfile.GL3); + if( null == caps ) { return; } + testImpl(caps, 1); + } + + @Test + public void test11_GL3Compat_Passthrough() throws InterruptedException { + GLCapabilities caps = getCaps(GLProfile.GL3bc); + if( null == caps ) { return; } + testImpl(caps, 0); + } + + @Test + public void test12_GL3Compat_FlipXYZ() throws InterruptedException { + GLCapabilities caps = getCaps(GLProfile.GL3bc); + if( null == caps ) { return; } + testImpl(caps, 1); + } + + private void testImpl(GLCapabilities caps, int geomShader) throws InterruptedException { + GLWindow glWindow = GLWindow.create(caps); + Assert.assertNotNull(glWindow); + glWindow.setSize(800, 600); + glWindow.setVisible(true); + glWindow.setTitle("JOGL Geometry Shader Banana Test"); + Assert.assertTrue(glWindow.isNativeValid()); + + QuitAdapter quitAdapter = new QuitAdapter(); + glWindow.addKeyListener(quitAdapter); + glWindow.addWindowListener(quitAdapter); + glWindow.addGLEventListener( new GeomShader01TextureGL3(geomShader) ); + + 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(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(TestGeomShader01TextureGL3NEWT.class.getName()); + } +} diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl3/shader/flipXYZ01_xxx.gp b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl3/shader/flipXYZ01_xxx.gp new file mode 100644 index 000000000..f65ffacdb --- /dev/null +++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl3/shader/flipXYZ01_xxx.gp @@ -0,0 +1,32 @@ +// Copyright 2012 JogAmp Community. All rights reserved. +// Requires version >= 150 + +layout (triangles) in; +layout (triangle_strip, max_vertices=3) out; + +in VertexData { + vec4 frontColor; + vec2 texCoord; +} vp_data[3]; + +out VertexData { + vec4 frontColor; + vec2 texCoord; +} gp_data; + +void main() +{ + for(int i = 0; i < gl_in.length(); i++) + { + // copy attributes + gl_Position = vec4(gl_in[i].gl_Position.xyz*-1,1); // This line flips the coordinates. + gp_data.frontColor = vp_data[i].frontColor; + gp_data.texCoord = vp_data[i].texCoord; + + // done with the vertex + EmitVertex(); + } +} + + + diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl3/shader/passthrough01_xxx.gp b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl3/shader/passthrough01_xxx.gp new file mode 100644 index 000000000..588b72426 --- /dev/null +++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl3/shader/passthrough01_xxx.gp @@ -0,0 +1,31 @@ +// Copyright 2012 JogAmp Community. All rights reserved. +// Requires version >= 150 + +layout (triangles) in; +layout (triangle_strip, max_vertices=3) out; + +in VertexData { + vec4 frontColor; + vec2 texCoord; +} vp_data[3]; + +out VertexData { + vec4 frontColor; + vec2 texCoord; +} gp_data; + +void main() +{ + for(int i = 0; i < gl_in.length(); i++) + { + // copy attributes + gl_Position = gl_in[i].gl_Position; + gp_data.frontColor = vp_data[i].frontColor; + gp_data.texCoord = vp_data[i].texCoord; + + // done with the vertex + EmitVertex(); + } +} + + diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl3/shader/texture01_xxx.fp b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl3/shader/texture01_xxx.fp new file mode 100644 index 000000000..61f4529ac --- /dev/null +++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl3/shader/texture01_xxx.fp @@ -0,0 +1,20 @@ +// Copyright 2012 JogAmp Community. All rights reserved. +// Requires version >= 130 + +in VertexData { + vec4 frontColor; + vec2 texCoord; +} gp_data; + +out vec4 mgl_FragColor; + +uniform sampler2D mgl_ActiveTexture; + +void main (void) +{ + vec4 texColor = texture(mgl_ActiveTexture, gp_data.texCoord); + + // mix frontColor with texture .. + mgl_FragColor = vec4(gp_data.frontColor*texColor); +} + diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl3/shader/texture01_xxx.vp b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl3/shader/texture01_xxx.vp new file mode 100644 index 000000000..b220c83f1 --- /dev/null +++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl3/shader/texture01_xxx.vp @@ -0,0 +1,20 @@ +// Copyright 2012 JogAmp Community. All rights reserved. +// Requires version >= 130 + +uniform mat4 mgl_PMVMatrix[2]; + +in vec4 mgl_Vertex; +in vec4 mgl_Color; +in vec4 mgl_MultiTexCoord; + +out VertexData { + vec4 frontColor; + vec2 texCoord; +} vp_data; + +void main(void) +{ + vp_data.frontColor = mgl_Color; + vp_data.texCoord = mgl_MultiTexCoord.st; + gl_Position = mgl_PMVMatrix[0] * mgl_PMVMatrix[1] * mgl_Vertex; +} |