From 5a37c7f33e5b3584a3561f240392814940a69956 Mon Sep 17 00:00:00 2001 From: toruwest Date: Wed, 15 Oct 2014 23:11:11 +0900 Subject: Added. --- .../instancedRendering/ ManyTriangleInstanced.java | 112 -------- .../IInstancedRenderingView.java | 33 +++ .../instancedRendering/ManyTriangleInstanced.java | 110 ++++++++ .../ManyTriangleInstancedWithShaderStateNewt.java | 110 ++++++++ .../ManyTriangleInstancedWithShaderStateSwing.java | 103 ++++++++ .../TriangleInstancedRendererWithShaderState.java | 269 +++++++++++++++++++ .../TrianglesInstancedRendererHardcoded.java | 285 +++++++++++++++++++++ src/demos/instancedRendering/shader/triangles.fp | 15 ++ src/demos/instancedRendering/shader/triangles.vp | 17 ++ 9 files changed, 942 insertions(+), 112 deletions(-) delete mode 100644 src/demos/instancedRendering/ ManyTriangleInstanced.java create mode 100644 src/demos/instancedRendering/IInstancedRenderingView.java create mode 100644 src/demos/instancedRendering/ManyTriangleInstanced.java create mode 100644 src/demos/instancedRendering/ManyTriangleInstancedWithShaderStateNewt.java create mode 100644 src/demos/instancedRendering/ManyTriangleInstancedWithShaderStateSwing.java create mode 100644 src/demos/instancedRendering/TriangleInstancedRendererWithShaderState.java create mode 100644 src/demos/instancedRendering/TrianglesInstancedRendererHardcoded.java create mode 100644 src/demos/instancedRendering/shader/triangles.fp create mode 100644 src/demos/instancedRendering/shader/triangles.vp diff --git a/src/demos/instancedRendering/ ManyTriangleInstanced.java b/src/demos/instancedRendering/ ManyTriangleInstanced.java deleted file mode 100644 index 9f8e329..0000000 --- a/src/demos/instancedRendering/ ManyTriangleInstanced.java +++ /dev/null @@ -1,112 +0,0 @@ -/** - * 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 demos.instancedRendering; - -import java.awt.BorderLayout; -import java.awt.Dimension; -import java.awt.event.MouseWheelEvent; -import java.awt.event.MouseWheelListener; -import java.awt.event.WindowAdapter; -import java.awt.event.WindowEvent; - -import javax.media.opengl.GLCapabilities; -import javax.media.opengl.GLProfile; -import javax.media.opengl.awt.GLCanvas; -import javax.swing.JFrame; -import javax.swing.SwingUtilities; - -import com.jogamp.opengl.test.junit.jogl.demos.gl4.IView; -import com.jogamp.opengl.test.junit.jogl.demos.gl4.TrianglesInstancedRendererHardcoded; -import com.jogamp.opengl.util.FPSAnimator; - -public class ManyTriangleInstanced implements IView { - - private final JFrame frame; - private final FPSAnimator animator; - private final GLCanvas panel; - private final Dimension dim = new Dimension(1024, 768); - - protected float winScale = 0.1f; - private static final float WIN_SCALE_MIN = 1e-3f; - private static final float WIN_SCALE_MAX = 100f; - - private final TrianglesInstancedRendererHardcoded renderer; - - public static void main(String[] args){ - SwingUtilities.invokeLater(new Runnable() { - @Override - public void run() { - new ManyTriangleInstanced(); - } - }); - } - - public ManyTriangleInstanced() { - renderer = new TrianglesInstancedRendererHardcoded(this); - - frame = new JFrame(this.getClass().getSimpleName()); - frame.setLayout(new BorderLayout()); - frame.addWindowListener(new WindowAdapter() { - @Override - public void windowClosing(WindowEvent e) { - animator.stop(); - System.exit(0); - } - }); - panel = new GLCanvas(new GLCapabilities(GLProfile.get(GLProfile.GL4))); - panel.addGLEventListener(renderer); - - frame.addMouseWheelListener(new MouseWheelListener() { - - @Override - public void mouseWheelMoved(MouseWheelEvent e) { - int step = e.getWheelRotation(); - if(step > 0) { - winScale *= 1.05; - if(winScale > WIN_SCALE_MAX) winScale = WIN_SCALE_MAX; - } else if(0 > step ) { - winScale *= 0.95; - if(winScale < WIN_SCALE_MIN) winScale = WIN_SCALE_MIN; - } - } - }); - - panel.setPreferredSize(dim); - frame.add(panel, BorderLayout.CENTER); - frame.pack(); - frame.setVisible(true); - animator = new FPSAnimator(panel, 60, true); - animator.start(); - } - - @Override - public float getScale() { - return winScale; - } -} diff --git a/src/demos/instancedRendering/IInstancedRenderingView.java b/src/demos/instancedRendering/IInstancedRenderingView.java new file mode 100644 index 0000000..e76db6a --- /dev/null +++ b/src/demos/instancedRendering/IInstancedRenderingView.java @@ -0,0 +1,33 @@ +/** + * 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; + +public interface IInstancedRenderingView { + float getScale(); +} diff --git a/src/demos/instancedRendering/ManyTriangleInstanced.java b/src/demos/instancedRendering/ManyTriangleInstanced.java new file mode 100644 index 0000000..c236f86 --- /dev/null +++ b/src/demos/instancedRendering/ManyTriangleInstanced.java @@ -0,0 +1,110 @@ +/** + * 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 demos.instancedRendering; + +import java.awt.BorderLayout; +import java.awt.Dimension; +import java.awt.event.MouseWheelEvent; +import java.awt.event.MouseWheelListener; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; + +import javax.media.opengl.GLCapabilities; +import javax.media.opengl.GLProfile; +import javax.media.opengl.awt.GLCanvas; +import javax.swing.JFrame; +import javax.swing.SwingUtilities; + +import com.jogamp.opengl.util.FPSAnimator; + +public class ManyTriangleInstanced implements IInstancedRenderingView { + + private final JFrame frame; + private final FPSAnimator animator; + private final GLCanvas panel; + private final Dimension dim = new Dimension(1024, 768); + + protected float winScale = 0.1f; + private static final float WIN_SCALE_MIN = 1e-3f; + private static final float WIN_SCALE_MAX = 100f; + + private final TrianglesInstancedRendererHardcoded renderer; + + public static void main(String[] args){ + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + new ManyTriangleInstanced(); + } + }); + } + + public ManyTriangleInstanced() { + renderer = new TrianglesInstancedRendererHardcoded(this); + + frame = new JFrame(this.getClass().getSimpleName()); + frame.setLayout(new BorderLayout()); + frame.addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent e) { + animator.stop(); + System.exit(0); + } + }); + panel = new GLCanvas(new GLCapabilities(GLProfile.get(GLProfile.GL4))); + panel.addGLEventListener(renderer); + + frame.addMouseWheelListener(new MouseWheelListener() { + + @Override + public void mouseWheelMoved(MouseWheelEvent e) { + int step = e.getWheelRotation(); + if(step > 0) { + winScale *= 1.05; + if(winScale > WIN_SCALE_MAX) winScale = WIN_SCALE_MAX; + } else if(0 > step ) { + winScale *= 0.95; + if(winScale < WIN_SCALE_MIN) winScale = WIN_SCALE_MIN; + } + } + }); + + panel.setPreferredSize(dim); + frame.add(panel, BorderLayout.CENTER); + frame.pack(); + frame.setVisible(true); + animator = new FPSAnimator(panel, 60, true); + animator.start(); + } + + @Override + public float getScale() { + return winScale; + } +} diff --git a/src/demos/instancedRendering/ManyTriangleInstancedWithShaderStateNewt.java b/src/demos/instancedRendering/ManyTriangleInstancedWithShaderStateNewt.java new file mode 100644 index 0000000..63437dd --- /dev/null +++ b/src/demos/instancedRendering/ManyTriangleInstancedWithShaderStateNewt.java @@ -0,0 +1,110 @@ +/** + * 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 demos.instancedRendering; + +import javax.media.opengl.GLCapabilities; +import javax.media.opengl.GLProfile; +import javax.swing.SwingUtilities; + +import com.jogamp.newt.event.MouseAdapter; +import com.jogamp.newt.event.MouseEvent; +import com.jogamp.newt.event.WindowAdapter; +import com.jogamp.newt.event.WindowEvent; +import com.jogamp.newt.opengl.GLWindow; +import com.jogamp.opengl.util.FPSAnimator; + +public class ManyTriangleInstancedWithShaderStateNewt implements IInstancedRenderingView { + + protected float winScale = 0.1f; + private static final float WIN_SCALE_MIN = 1e-3f; + private static final float WIN_SCALE_MAX = 100f; + private final FPSAnimator animator; + + public static void main(String[] args) { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + new ManyTriangleInstancedWithShaderStateNewt(); + } + }); + } + + public ManyTriangleInstancedWithShaderStateNewt() { + TriangleInstancedRendererWithShaderState renderer = new TriangleInstancedRendererWithShaderState(this); + + GLProfile prof = GLProfile.get(GLProfile.GL4); + GLCapabilities caps = new GLCapabilities(prof); + GLWindow glWindow = GLWindow.create(caps); + + glWindow.addGLEventListener(renderer); + + glWindow.addMouseListener(new MouseAdapter() { + + @Override + public void mouseWheelMoved(MouseEvent e) { + float[] step = e.getRotation(); + if(step[1] > 0) { + winScale *= 1.05; + if(winScale > WIN_SCALE_MAX) winScale = WIN_SCALE_MAX; + } else if(0 > step[1] ) { + winScale *= 0.95; + if(winScale < WIN_SCALE_MIN) winScale = WIN_SCALE_MIN; + } + } + }); + + glWindow.addWindowListener(new WindowAdapter() { + + @Override + public void windowDestroyed(WindowEvent evt) { + if(animator.isAnimating()) animator.stop(); + } + + @Override + public void windowDestroyNotify(WindowEvent evt) { + animator.stop(); + } + }); + + animator = new FPSAnimator(glWindow, 60, true); + animator.start(); + + glWindow.setTitle("Instanced rendering experiment"); + glWindow.setSize(1024, 768); + glWindow.setUndecorated(false); + glWindow.setPointerVisible(true); + glWindow.setVisible(true); + } + + @Override + public float getScale() { + return winScale; + } + +} diff --git a/src/demos/instancedRendering/ManyTriangleInstancedWithShaderStateSwing.java b/src/demos/instancedRendering/ManyTriangleInstancedWithShaderStateSwing.java new file mode 100644 index 0000000..2111a08 --- /dev/null +++ b/src/demos/instancedRendering/ManyTriangleInstancedWithShaderStateSwing.java @@ -0,0 +1,103 @@ +/** + * 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 demos.instancedRendering; + +import java.awt.BorderLayout; +import java.awt.Dimension; +import java.awt.event.MouseWheelEvent; +import java.awt.event.MouseWheelListener; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; + +import javax.media.opengl.GLCapabilities; +import javax.media.opengl.GLProfile; +import javax.media.opengl.awt.GLCanvas; +import javax.swing.JFrame; +import javax.swing.SwingUtilities; + +import com.jogamp.opengl.util.FPSAnimator; + +public class ManyTriangleInstancedWithShaderStateSwing implements IInstancedRenderingView { + protected float winScale = 0.1f; + private static final float WIN_SCALE_MIN = 1e-3f; + private static final float WIN_SCALE_MAX = 100f; + private final FPSAnimator animator; + + public static void main(String[] args) { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + new ManyTriangleInstancedWithShaderStateSwing(); + } + }); + } + + public ManyTriangleInstancedWithShaderStateSwing() { + TriangleInstancedRendererWithShaderState renderer = new TriangleInstancedRendererWithShaderState(this); + + JFrame frame = new JFrame(this.getClass().getSimpleName()); + frame.setLayout(new BorderLayout()); + frame.addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent e) { + animator.stop(); + System.exit(0); + } + }); + GLCanvas canvas = new GLCanvas(new GLCapabilities(GLProfile.get(GLProfile.GL4))); + canvas.addGLEventListener(renderer); + + frame.addMouseWheelListener(new MouseWheelListener() { + + @Override + public void mouseWheelMoved(MouseWheelEvent e) { + int step = e.getWheelRotation(); + if(step > 0) { + winScale *= 1.05; + if(winScale > WIN_SCALE_MAX) winScale = WIN_SCALE_MAX; + } else if(0 > step ) { + winScale *= 0.95; + if(winScale < WIN_SCALE_MIN) winScale = WIN_SCALE_MIN; + } + } + }); + + canvas.setPreferredSize(new Dimension(1024, 768)); + frame.add(canvas, BorderLayout.CENTER); + frame.pack(); + frame.setVisible(true); + animator = new FPSAnimator(canvas, 60, true); + animator.start(); + } + + @Override + public float getScale() { + return winScale; + } +} diff --git a/src/demos/instancedRendering/TriangleInstancedRendererWithShaderState.java b/src/demos/instancedRendering/TriangleInstancedRendererWithShaderState.java new file mode 100644 index 0000000..9ba0c8d --- /dev/null +++ b/src/demos/instancedRendering/TriangleInstancedRendererWithShaderState.java @@ -0,0 +1,269 @@ +package com.jogamp.opengl.test.junit.jogl.demos.gl4; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.nio.FloatBuffer; +import java.util.Random; + +import javax.media.opengl.DebugGL4; +import javax.media.opengl.GL; +import javax.media.opengl.GL2; +import javax.media.opengl.GL2ES2; +import javax.media.opengl.GL4; +import javax.media.opengl.GLAutoDrawable; +import javax.media.opengl.GLEventListener; +import javax.media.opengl.GLException; +import javax.media.opengl.GLUniformData; +import javax.media.opengl.TraceGL4; + +import com.jogamp.opengl.math.Matrix4; +import com.jogamp.opengl.util.GLArrayDataClient; +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; + +public class TriangleInstancedRendererWithShaderState implements GLEventListener { + private float aspect; + + private static final String shaderBasename = "triangles"; +// private static final boolean useInterleaved = false; + private static final boolean useInterleaved = true; + + private ShaderState st; + private PMVMatrix projectionMatrix; + private GLUniformData projectionMatrixUniform; + private GLUniformData transformMatrixUniform; + + private GLArrayDataServer interleavedVBO; + private GLArrayDataClient verticesVBO; + private GLArrayDataClient colorsVBO; + + private static final int NO_OF_INSTANCE = 30; + private final FloatBuffer triangleTransform = FloatBuffer.allocate(16 * NO_OF_INSTANCE); + private final Matrix4[] mat = new Matrix4[NO_OF_INSTANCE]; + private final float[] rotationSpeed = new float[NO_OF_INSTANCE]; + + private static final boolean useTraceGL = false; + private PrintStream stream; + private final IInstancedRenderingView view; + + private boolean isInitialized = false; + + public TriangleInstancedRendererWithShaderState(IInstancedRenderingView view) { + this.view = view; + + if(useTraceGL) { + try { + stream = new PrintStream(new FileOutputStream(new File("instanced-with-st.txt"))); + } catch (IOException e1) { + e1.printStackTrace(); + } + } + + initTransform(); + } + + private void initTransform() { + Random rnd = new Random(); + for(int i = 0; i < NO_OF_INSTANCE; i++) { + rotationSpeed[i] = 0.3f * rnd.nextFloat(); + mat[i] = new Matrix4(); + mat[i].loadIdentity(); + float scale = 1f + 4 * rnd.nextFloat(); + mat[i].scale(scale, scale, scale); + //setup initial position of each triangle + mat[i].translate(20f * rnd.nextFloat() - 10f, + 10f * rnd.nextFloat() - 5f, + 0f); + } + } + + @Override + public void init(GLAutoDrawable drawable) { + GL4 gl = drawable.getGL().getGL4(); + drawable.setGL(new DebugGL4(gl)); + if(useTraceGL) { + drawable.setGL(new TraceGL4(gl, stream)); + } + + gl.glClearColor(1, 1, 1, 1); //white background +// gl.glClearColor(0, 0, 0, 1); //black background + gl.glClearDepth(1.0f); + + System.err.println("Chosen GLCapabilities: " + drawable.getChosenGLCapabilities()); + System.err.println("INIT GL IS: " + gl.getClass().getName()); + System.err.println("GL_VENDOR: " + gl.glGetString(GL4.GL_VENDOR)); + System.err.println("GL_RENDERER: " + gl.glGetString(GL4.GL_RENDERER)); + System.err.println("GL_VERSION: " + gl.glGetString(GL4.GL_VERSION)); + + initShader(gl); + projectionMatrix = new PMVMatrix(); + projectionMatrixUniform = new GLUniformData("mgl_PMatrix", 4, 4, projectionMatrix.glGetPMatrixf()); + st.ownUniform(projectionMatrixUniform); + if(!st.uniform(gl, projectionMatrixUniform)) { + throw new GLException("Error setting mgl_PMatrix in shader: " + st); + } + + transformMatrixUniform = new GLUniformData("mgl_MVMatrix", 4, 4, triangleTransform); + + st.ownUniform(transformMatrixUniform); + if(!st.uniform(gl, transformMatrixUniform)) { + throw new GLException("Error setting mgl_MVMatrix in shader: " + st); + } + + if(useInterleaved) { + initVBO_interleaved(gl); + } else { + initVBO_nonInterleaved(gl); + } + + isInitialized = true; + } + + @Override + public void display(GLAutoDrawable drawable) { + if(!isInitialized ) return; + + GL4 gl = drawable.getGL().getGL4(); + gl.glClear(GL4.GL_COLOR_BUFFER_BIT | GL4.GL_DEPTH_BUFFER_BIT); + + st.useProgram(gl, true); + projectionMatrix.glMatrixMode(GL2.GL_PROJECTION); + projectionMatrix.glPushMatrix(); + + float winScale = 0.1f; + if(view != null) winScale = view.getScale(); + projectionMatrix.glScalef(winScale, winScale, winScale); + projectionMatrix.update(); + st.uniform(gl, projectionMatrixUniform); + projectionMatrix.glPopMatrix(); + + generateTriangleTransform(); + st.uniform(gl, transformMatrixUniform); + if(useInterleaved) { + interleavedVBO.enableBuffer(gl, true); + } else { + verticesVBO.enableBuffer(gl, true); + colorsVBO.enableBuffer(gl, true); + } + //gl.glVertexAttribDivisor() is not required since each instance has the same attribute (color). + gl.glDrawArraysInstanced(GL4.GL_TRIANGLES, 0, 3, NO_OF_INSTANCE); + if(useInterleaved) { + interleavedVBO.enableBuffer(gl, false); + } else { + verticesVBO.enableBuffer(gl, false); + colorsVBO.enableBuffer(gl, false); + } + st.useProgram(gl, false); + } + + @Override + public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { + GL4 gl3 = drawable.getGL().getGL4(); + gl3.glViewport(0, 0, width, height); + aspect = (float) width / (float) height; + + projectionMatrix.glMatrixMode(GL2.GL_PROJECTION); + projectionMatrix.glLoadIdentity(); + projectionMatrix.gluPerspective(45, aspect, 0.001f, 20f); + projectionMatrix.gluLookAt(0, 0, -10, 0, 0, 0, 0, 1, 0); + } + + @Override + public void dispose(GLAutoDrawable drawable){ + GL4 gl = drawable.getGL().getGL4(); + st.destroy(gl); + } + + private void generateTriangleTransform() { + triangleTransform.clear(); + for(int i = 0; i < NO_OF_INSTANCE; i++) { + mat[i].rotate(rotationSpeed[i], 0, 0, 1); + triangleTransform.put(mat[i].getMatrix()); + } + triangleTransform.rewind(); + } + + private void initVBO_nonInterleaved(GL4 gl) { + int VERTEX_COUNT = 3; + + verticesVBO = GLArrayDataClient.createGLSL("mgl_Vertex", 3, GL4.GL_FLOAT, false, VERTEX_COUNT); + FloatBuffer verticeBuf = (FloatBuffer)verticesVBO.getBuffer(); + verticeBuf.put(vertices); + verticesVBO.seal(gl, true); + + colorsVBO = GLArrayDataClient.createGLSL("mgl_Color", 4, GL4.GL_FLOAT, false, VERTEX_COUNT); + FloatBuffer colorBuf = (FloatBuffer)colorsVBO.getBuffer(); + colorBuf.put(colors); + colorsVBO.seal(gl, true); + + verticesVBO.enableBuffer(gl, false); + colorsVBO.enableBuffer(gl, false); + + st.ownAttribute(verticesVBO, true); + st.ownAttribute(colorsVBO, true); + st.useProgram(gl, false); + } + + private void initVBO_interleaved(GL4 gl) { + int VERTEX_COUNT = 3; + interleavedVBO = GLArrayDataServer.createGLSLInterleaved(3 + 4, GL.GL_FLOAT, false, VERTEX_COUNT, GL.GL_STATIC_DRAW); + interleavedVBO.addGLSLSubArray("mgl_Vertex", 3, GL.GL_ARRAY_BUFFER); + interleavedVBO.addGLSLSubArray("mgl_Color", 4, GL.GL_ARRAY_BUFFER); + + FloatBuffer ib = (FloatBuffer)interleavedVBO.getBuffer(); + + for(int i = 0; i < VERTEX_COUNT; i++) { + ib.put(vertices, i*3, 3); + ib.put(colors, i*4, 4); + } + interleavedVBO.seal(gl, true); + interleavedVBO.enableBuffer(gl, false); + st.ownAttribute(interleavedVBO, true); + st.useProgram(gl, false); + } + + private void initShader(GL4 gl) { + // Create & Compile the shader objects + ShaderCode vp0 = ShaderCode.create(gl, GL2ES2.GL_VERTEX_SHADER, this.getClass(), + "shader", "shader/bin", shaderBasename, true); + ShaderCode fp0 = ShaderCode.create(gl, GL2ES2.GL_FRAGMENT_SHADER, this.getClass(), + "shader", "shader/bin", shaderBasename, true); + vp0.replaceInShaderSource("NO_OF_INSTANCE", String.valueOf(NO_OF_INSTANCE)); + + vp0.defaultShaderCustomization(gl, true, true); + fp0.defaultShaderCustomization(gl, true, true); + + //vp0.dumpShaderSource(System.out); + + // Create & Link the shader program + ShaderProgram sp = new ShaderProgram(); + sp.add(vp0); + sp.add(fp0); + if(!sp.link(gl, System.err)) { + throw new GLException("Couldn't link program: "+sp); + } + + // Let's manage all our states using ShaderState. + st = new ShaderState(); + st.attachShaderProgram(gl, sp, true); + } + + + private static final float[] vertices = { + 1.0f, 0.0f, 0, + -0.5f, 0.866f, 0, + -0.5f, -0.866f, 0 + }; + + private final float[] colors = { + 1.0f, 0.0f, 0.0f, 1.0f, + 0.0f, 1.0f, 0.0f, 1.0f, + 0f, 0f, 1.0f, 1f + }; + +} diff --git a/src/demos/instancedRendering/TrianglesInstancedRendererHardcoded.java b/src/demos/instancedRendering/TrianglesInstancedRendererHardcoded.java new file mode 100644 index 0000000..7396700 --- /dev/null +++ b/src/demos/instancedRendering/TrianglesInstancedRendererHardcoded.java @@ -0,0 +1,285 @@ +package com.jogamp.opengl.test.junit.jogl.demos.gl4; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.nio.FloatBuffer; +import java.util.Random; + +import javax.media.opengl.DebugGL4; +import javax.media.opengl.GL2; +import javax.media.opengl.GL4; +import javax.media.opengl.GLAutoDrawable; +import javax.media.opengl.GLEventListener; +import javax.media.opengl.TraceGL4; + +import com.jogamp.common.nio.Buffers; +import com.jogamp.opengl.math.Matrix4; +import com.jogamp.opengl.util.PMVMatrix; + +public class TrianglesInstancedRendererHardcoded implements GLEventListener { + private float aspect; + + private int shaderProgram; + private int vertShader; + private int fragShader; + private int projectionMatrixLocation; + private int transformMatrixLocation; + private static final int locPos = 1; + private static final int locCol = 2; + private PMVMatrix projectionMatrix; + + private static final int NO_OF_INSTANCE = 30; + private final FloatBuffer triangleTransform = FloatBuffer.allocate(16 * NO_OF_INSTANCE); + private final Matrix4[] mat = new Matrix4[NO_OF_INSTANCE]; + private final float[] rotationSpeed = new float[NO_OF_INSTANCE]; + + private int[] vbo; + private int[] vao; + private PrintStream stream; + private final IInstancedRenderingView view; + + private static final boolean useTraceGL = false; + + public TrianglesInstancedRendererHardcoded(IInstancedRenderingView view) { + this.view = view; + initTransform(); + + if(useTraceGL) { + try { + stream = new PrintStream(new FileOutputStream(new File("instanced.txt"))); + } catch (IOException e1) { + e1.printStackTrace(); + } + } + } + + @Override + public void init(GLAutoDrawable drawable) { + GL4 gl = drawable.getGL().getGL4(); + drawable.setGL(new DebugGL4(gl)); + if(useTraceGL) { + drawable.setGL(new TraceGL4(gl, stream)); + } + + gl.glClearColor(1, 1, 1, 1); //white background + // gl.glClearColor(0, 0, 0, 1); //black background + gl.glClearDepth(1.0f); + + System.err.println("Chosen GLCapabilities: " + drawable.getChosenGLCapabilities()); + System.err.println("INIT GL IS: " + gl.getClass().getName()); + System.err.println("GL_VENDOR: " + gl.glGetString(GL4.GL_VENDOR)); + System.err.println("GL_RENDERER: " + gl.glGetString(GL4.GL_RENDERER)); + System.err.println("GL_VERSION: " + gl.glGetString(GL4.GL_VERSION)); + + try { + initShaders(gl); + } catch (IOException e) { + e.printStackTrace(); + } + initVBO(gl); + } + + @Override + public void display(GLAutoDrawable drawable) { + + GL4 gl = drawable.getGL().getGL4(); + gl.glClear(GL4.GL_COLOR_BUFFER_BIT | GL4.GL_DEPTH_BUFFER_BIT); + + gl.glUseProgram(shaderProgram); + + projectionMatrix.glMatrixMode(GL2.GL_PROJECTION); + + projectionMatrix.glPushMatrix(); + float winScale = 0.1f; + if(view != null) { + winScale = view.getScale(); + } + projectionMatrix.glScalef(winScale, winScale, winScale); + projectionMatrix.update(); + gl.glUniformMatrix4fv(projectionMatrixLocation, 1, false, projectionMatrix.glGetPMatrixf()); + projectionMatrix.glPopMatrix(); + generateTriangleTransform(); + gl.glUniformMatrix4fv(transformMatrixLocation, NO_OF_INSTANCE, false, triangleTransform); + + gl.glBindVertexArray(vao[0]); + gl.glDrawArraysInstanced(GL4.GL_TRIANGLES, 0, 3, NO_OF_INSTANCE); + gl.glBindVertexArray(0); + gl.glUseProgram(0); + } + + @Override + public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { + System.out.println("Window resized to width=" + width + " height=" + height); + GL4 gl3 = drawable.getGL().getGL4(); + gl3.glViewport(0, 0, width, height); + aspect = (float) width / (float) height; + + projectionMatrix = new PMVMatrix(); + projectionMatrix.glMatrixMode(GL2.GL_PROJECTION); + projectionMatrix.glLoadIdentity(); + projectionMatrix.gluPerspective(45, aspect, 0.001f, 20f); + projectionMatrix.gluLookAt(0, 0, -10, 0, 0, 0, 0, 1, 0); + } + + @Override + public void dispose(GLAutoDrawable drawable){ + GL4 gl = drawable.getGL().getGL4(); + gl.glUseProgram(0); + gl.glDeleteBuffers(2, vbo, 0); + gl.glDetachShader(shaderProgram, vertShader); + gl.glDeleteShader(vertShader); + gl.glDetachShader(shaderProgram, fragShader); + gl.glDeleteShader(fragShader); + gl.glDeleteProgram(shaderProgram); + } + + private void initTransform() { + Random rnd = new Random(); + for(int i = 0; i < NO_OF_INSTANCE; i++) { + rotationSpeed[i] = 0.3f * rnd.nextFloat(); + mat[i] = new Matrix4(); + mat[i].loadIdentity(); + float scale = 1f + 4 * rnd.nextFloat(); + mat[i].scale(scale, scale, scale); + //setup initial position of each triangle + mat[i].translate(20f * rnd.nextFloat() - 10f, + 10f * rnd.nextFloat() - 5f, + 0f); + } + } + + private void initVBO(GL4 gl) { + FloatBuffer interleavedBuffer = Buffers.newDirectFloatBuffer(vertices.length + colors.length); + for(int i = 0; i < vertices.length/3; i++) { + for(int j = 0; j < 3; j++) { + interleavedBuffer.put(vertices[i*3 + j]); + } + for(int j = 0; j < 4; j++) { + interleavedBuffer.put(colors[i*4 + j]); + } + } + interleavedBuffer.flip(); + + vao = new int[1]; + gl.glGenVertexArrays(1, vao , 0); + gl.glBindVertexArray(vao[0]); + vbo = new int[1]; + gl.glGenBuffers(1, vbo, 0); + gl.glBindBuffer(GL4.GL_ARRAY_BUFFER, vbo[0]); + gl.glBufferData(GL4.GL_ARRAY_BUFFER, interleavedBuffer.limit() * Buffers.SIZEOF_FLOAT, interleavedBuffer, GL4.GL_STATIC_DRAW); + + gl.glEnableVertexAttribArray(locPos); + gl.glEnableVertexAttribArray(locCol); + + int stride = Buffers.SIZEOF_FLOAT * (3+4); + gl.glVertexAttribPointer( locPos, 3, GL4.GL_FLOAT, false, stride, 0); + gl.glVertexAttribPointer( locCol, 4, GL4.GL_FLOAT, false, stride, Buffers.SIZEOF_FLOAT * 3); + } + + private void initShaders(GL4 gl) throws IOException { + vertShader = gl.glCreateShader(GL4.GL_VERTEX_SHADER); + fragShader = gl.glCreateShader(GL4.GL_FRAGMENT_SHADER); + + String[] vlines = new String[] { vertexShaderString }; + int[] vlengths = new int[] { vlines[0].length() }; + gl.glShaderSource(vertShader, vlines.length, vlines, vlengths, 0); + gl.glCompileShader(vertShader); + + int[] compiled = new int[1]; + gl.glGetShaderiv(vertShader, GL4.GL_COMPILE_STATUS, compiled, 0); + if(compiled[0] != 0) { + System.out.println("Vertex shader compiled"); + } else { + int[] logLength = new int[1]; + gl.glGetShaderiv(vertShader, GL4.GL_INFO_LOG_LENGTH, logLength, 0); + + byte[] log = new byte[logLength[0]]; + gl.glGetShaderInfoLog(vertShader, logLength[0], (int[])null, 0, log, 0); + + System.err.println("Error compiling the vertex shader: " + new String(log)); + System.exit(1); + } + + String[] flines = new String[] { fragmentShaderString }; + int[] flengths = new int[] { flines[0].length() }; + gl.glShaderSource(fragShader, flines.length, flines, flengths, 0); + gl.glCompileShader(fragShader); + + gl.glGetShaderiv(fragShader, GL4.GL_COMPILE_STATUS, compiled, 0); + if(compiled[0] != 0){ + System.out.println("Fragment shader compiled."); + } else { + int[] logLength = new int[1]; + gl.glGetShaderiv(fragShader, GL4.GL_INFO_LOG_LENGTH, logLength, 0); + + byte[] log = new byte[logLength[0]]; + gl.glGetShaderInfoLog(fragShader, logLength[0], (int[])null, 0, log, 0); + + System.err.println("Error compiling the fragment shader: " + new String(log)); + System.exit(1); + } + + shaderProgram = gl.glCreateProgram(); + gl.glAttachShader(shaderProgram, vertShader); + gl.glAttachShader(shaderProgram, fragShader); + + gl.glBindAttribLocation(shaderProgram, locPos, "mgl_Vertex"); + gl.glBindAttribLocation(shaderProgram, locCol, "mgl_Color"); + + gl.glLinkProgram(shaderProgram); + + projectionMatrixLocation = gl.glGetUniformLocation(shaderProgram, "mgl_PMatrix"); + System.out.println("projectionMatrixLocation:" + projectionMatrixLocation); + transformMatrixLocation = gl.glGetUniformLocation(shaderProgram, "mgl_MVMatrix"); + System.out.println("transformMatrixLocation:" + transformMatrixLocation); + } + + private void generateTriangleTransform() { + triangleTransform.clear(); + for(int i = 0; i < NO_OF_INSTANCE; i++) { + // mat[i].translate(0.1f, 0.1f, 0); + mat[i].rotate(rotationSpeed[i], 0, 0, 1); + // mat[i].translate(-0.1f, -0.1f, 0); + triangleTransform.put(mat[i].getMatrix()); + } + triangleTransform.flip(); + } + + private final String vertexShaderString = + "#version 410 \n" + + "\n" + + "uniform mat4 mgl_PMatrix; \n" + + "uniform mat4 mgl_MVMatrix[" + NO_OF_INSTANCE + "]; \n" + + "in vec3 mgl_Vertex; \n" + + "in vec4 mgl_Color; \n" + + "out vec4 frontColor; \n" + + "void main(void) \n" + + "{ \n" + + " frontColor = mgl_Color; \n" + + " gl_Position = mgl_PMatrix * mgl_MVMatrix[gl_InstanceID] * vec4(mgl_Vertex, 1);" + + "} "; + + private final String fragmentShaderString = + "#version 410\n" + + "\n" + + "in vec4 frontColor; \n" + + "out vec4 mgl_FragColor; \n" + + "void main (void) \n" + + "{ \n" + + " mgl_FragColor = frontColor; \n" + + "} "; + + private final float[] vertices = { + 1.0f, 0.0f, 0, + -0.5f, 0.866f, 0, + -0.5f, -0.866f, 0 + }; + + private final float[] colors = { + 1.0f, 0.0f, 0.0f, 1.0f, + 0.0f, 1.0f, 0.0f, 1.0f, + 0f, 0f, 1.0f, 1f + }; +} diff --git a/src/demos/instancedRendering/shader/triangles.fp b/src/demos/instancedRendering/shader/triangles.fp new file mode 100644 index 0000000..005884a --- /dev/null +++ b/src/demos/instancedRendering/shader/triangles.fp @@ -0,0 +1,15 @@ +// Copyright 2014 JogAmp Community. All rights reserved. + +#if __VERSION__ >= 130 + #define varying in + out vec4 mgl_FragColor; + #define texture2D texture +#else + #define mgl_FragColor gl_FragColor +#endif + +varying vec4 frontColor; + +void main (void) { + mgl_FragColor = frontColor; +} diff --git a/src/demos/instancedRendering/shader/triangles.vp b/src/demos/instancedRendering/shader/triangles.vp new file mode 100644 index 0000000..91cc6ef --- /dev/null +++ b/src/demos/instancedRendering/shader/triangles.vp @@ -0,0 +1,17 @@ +// Copyright 2014 JogAmp Community. All rights reserved. + +#if __VERSION__ >= 130 + #define attribute in + #define varying out +#endif + +uniform mat4 mgl_PMatrix; +uniform mat4 mgl_MVMatrix[NO_OF_INSTANCE]; +attribute vec3 mgl_Vertex; +attribute vec4 mgl_Color; +varying vec4 frontColor; + +void main(void) { + frontColor = mgl_Color; + gl_Position = mgl_PMatrix * mgl_MVMatrix[gl_InstanceID] * vec4(mgl_Vertex, 1); +} -- cgit v1.2.3