summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/IInstancedRenderingView.java33
-rw-r--r--src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/TriangleInstancedRendererWithShaderState.java269
-rw-r--r--src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/TrianglesInstancedRendererHardcoded.java285
-rw-r--r--src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/awt/TestInstancedReneringGL4AWT.java244
-rw-r--r--src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/newt/TestInstancedReneringGL4NEWT.java108
-rw-r--r--src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/shader/triangles.fp15
-rw-r--r--src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/shader/triangles.vp17
7 files changed, 971 insertions, 0 deletions
diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/IInstancedRenderingView.java b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/IInstancedRenderingView.java
new file mode 100644
index 000000000..e76db6ab5
--- /dev/null
+++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/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/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/TriangleInstancedRendererWithShaderState.java b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/TriangleInstancedRendererWithShaderState.java
new file mode 100644
index 000000000..9ba0c8d59
--- /dev/null
+++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/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/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/TrianglesInstancedRendererHardcoded.java b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/TrianglesInstancedRendererHardcoded.java
new file mode 100644
index 000000000..739670053
--- /dev/null
+++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/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/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/awt/TestInstancedReneringGL4AWT.java b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/awt/TestInstancedReneringGL4AWT.java
new file mode 100644
index 000000000..c602ffd7b
--- /dev/null
+++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/awt/TestInstancedReneringGL4AWT.java
@@ -0,0 +1,244 @@
+/**
+ * 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.awt;
+
+import java.awt.AWTException;
+import java.awt.BorderLayout;
+import java.awt.Dimension;
+import java.lang.reflect.InvocationTargetException;
+
+import javax.media.opengl.GLCapabilities;
+import javax.media.opengl.GLCapabilitiesImmutable;
+import javax.media.opengl.GLProfile;
+import javax.media.opengl.awt.GLJPanel;
+import javax.swing.JFrame;
+import javax.swing.SwingUtilities;
+
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.FixMethodOrder;
+import org.junit.Test;
+import org.junit.runners.MethodSorters;
+
+import com.jogamp.newt.event.KeyEvent;
+import com.jogamp.newt.event.TraceKeyAdapter;
+import com.jogamp.newt.event.TraceWindowAdapter;
+import com.jogamp.newt.event.awt.AWTKeyAdapter;
+import com.jogamp.newt.event.awt.AWTWindowAdapter;
+import com.jogamp.opengl.test.junit.jogl.demos.gl4.TriangleInstancedRendererWithShaderState;
+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.FPSAnimator;
+
+/**
+ * Test Instanced rendering demo TrianglesInstancedRenderer
+ */
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
+public class TestInstancedReneringGL4AWT extends UITestCase {
+ static GLProfile glp;
+ static int width, height;
+ static boolean shallUsePBuffer = false;
+ static boolean shallUseBitmap = false;
+ static boolean useMSAA = false;
+ static int swapInterval = 0;
+ static boolean useAnimator = true;
+ static boolean manualTest = false;
+
+ @BeforeClass
+ public static void initClass() {
+ if(GLProfile.isAvailable(GLProfile.GL4)) {
+ glp = GLProfile.get(GLProfile.GL4);
+ Assert.assertNotNull(glp);
+ width = 640;
+ height = 480;
+ } else {
+ setTestSupported(false);
+ }
+ }
+
+ @AfterClass
+ public static void releaseClass() {
+ }
+
+ protected void runTestGL(final GLCapabilities caps)
+ throws AWTException, InterruptedException, InvocationTargetException
+ {
+ final JFrame frame = new JFrame("Swing GLJPanel");
+ Assert.assertNotNull(frame);
+ final GLJPanel glJPanel = new GLJPanel(caps);
+ Assert.assertNotNull(glJPanel);
+ final Dimension glc_sz = new Dimension(width, height);
+ glJPanel.setMinimumSize(glc_sz);
+ glJPanel.setPreferredSize(glc_sz);
+ glJPanel.setSize(glc_sz);
+ glJPanel.addGLEventListener(new TriangleInstancedRendererWithShaderState(null));
+ final SnapshotGLEventListener snap = new SnapshotGLEventListener();
+ glJPanel.addGLEventListener(snap);
+ final FPSAnimator animator = useAnimator ? new FPSAnimator(glJPanel, 60) : null;
+ SwingUtilities.invokeAndWait(new Runnable() {
+ @Override
+ public void run() {
+ frame.getContentPane().add(glJPanel, BorderLayout.CENTER);
+ frame.getContentPane().validate();
+ frame.pack();
+ frame.setVisible(true);
+ } } ) ;
+ if( useAnimator ) {
+ animator.setUpdateFPSFrames(60, System.err);
+ animator.start();
+ Assert.assertEquals(true, animator.isAnimating());
+ }
+ final QuitAdapter quitAdapter = new QuitAdapter();
+ new AWTKeyAdapter(new TraceKeyAdapter(quitAdapter), glJPanel).addTo(glJPanel);
+ new AWTWindowAdapter(new TraceWindowAdapter(quitAdapter), glJPanel).addTo(frame);
+ final com.jogamp.newt.event.KeyListener kl = new com.jogamp.newt.event.KeyAdapter() {
+ @Override
+ public void keyPressed(final KeyEvent e) {
+ if( e.isAutoRepeat() ) {
+ return;
+ }
+ if(e.getKeyChar()=='m') {
+ final GLCapabilitiesImmutable capsPre = glJPanel.getChosenGLCapabilities();
+ final GLCapabilities capsNew = new GLCapabilities(capsPre.getGLProfile());
+ capsNew.copyFrom(capsPre);
+ final boolean msaa;
+ if( capsPre.getSampleBuffers() ) {
+ capsNew.setSampleBuffers(false);
+ capsNew.setDoubleBuffered(false);
+ msaa = false;
+ } else {
+ capsNew.setSampleBuffers(true);
+ capsNew.setNumSamples(4);
+ msaa = true;
+ }
+ System.err.println("[set MSAA "+msaa+" Caps had]: "+capsPre);
+ System.err.println("[set MSAA "+msaa+" Caps new]: "+capsNew);
+ System.err.println("XXX-A1: "+animator.toString());
+// glJPanel.setRequestedGLCapabilities(capsNew);
+ System.err.println("XXX-A2: "+animator.toString());
+ System.err.println("XXX: "+glJPanel.toString());
+ } else if(e.getKeyChar()=='b') {
+ final GLCapabilitiesImmutable capsPre = glJPanel.getChosenGLCapabilities();
+ final GLCapabilities capsNew = new GLCapabilities(capsPre.getGLProfile());
+ capsNew.copyFrom(capsPre);
+ final boolean bmp;
+ if( capsPre.isBitmap() ) {
+ capsNew.setBitmap(false); // auto-choose
+ bmp = false;
+ } else {
+ capsNew.setBitmap(true);
+ capsNew.setFBO(false);
+ capsNew.setPBuffer(false);
+ bmp = true;
+ }
+ System.err.println("[set Bitmap "+bmp+" Caps had]: "+capsPre);
+ System.err.println("[set Bitmap "+bmp+" Caps new]: "+capsNew);
+ System.err.println("XXX-A1: "+animator.toString());
+// glJPanel.setRequestedGLCapabilities(capsNew);
+ System.err.println("XXX-A2: "+animator.toString());
+ System.err.println("XXX: "+glJPanel.toString());
+ }
+ } };
+ new AWTKeyAdapter(kl, glJPanel).addTo(glJPanel);
+ final long t0 = System.currentTimeMillis();
+ long t1 = t0;
+ boolean triggerSnap = false;
+ while(!quitAdapter.shouldQuit() && t1 - t0 < duration) {
+ Thread.sleep(100);
+ t1 = System.currentTimeMillis();
+ snap.getDisplayCount();
+ if( !triggerSnap && snap.getDisplayCount() > 1 ) {
+ // Snapshot only after one frame has been rendered to suite FBO MSAA!
+ snap.setMakeSnapshot();
+ triggerSnap = true;
+ }
+ }
+ Assert.assertNotNull(frame);
+ Assert.assertNotNull(glJPanel);
+ Assert.assertNotNull(animator);
+ if( useAnimator ) {
+ animator.stop();
+ Assert.assertEquals(false, animator.isAnimating());
+ }
+ SwingUtilities.invokeAndWait(new Runnable() {
+ @Override
+ public void run() {
+ frame.setVisible(false);
+ frame.getContentPane().remove(glJPanel);
+ frame.remove(glJPanel);
+ glJPanel.destroy();
+ frame.dispose();
+ } } );
+ }
+
+ @Test
+ public void test01_DefaultMsaa()
+ throws AWTException, InterruptedException, InvocationTargetException
+ {
+ if( manualTest ) {
+ return;
+ }
+ final GLCapabilities caps = new GLCapabilities(GLProfile.get(GLProfile.GL4));
+ caps.setNumSamples(4);
+ caps.setSampleBuffers(true);
+ runTestGL(caps);
+ }
+
+ static long duration = 500; // ms
+
+ public static void main(final String args[]) {
+ for(int i=0; i<args.length; i++) {
+ if(args[i].equals("-time")) {
+ i++;
+ duration = MiscUtils.atol(args[i], duration);
+ } else if(args[i].equals("-vsync")) {
+ i++;
+ swapInterval = MiscUtils.atoi(args[i], swapInterval);
+ } else if(args[i].equals("-msaa")) {
+ useMSAA = true;
+ } else if(args[i].equals("-noanim")) {
+ useAnimator = false;
+ } else if(args[i].equals("-pbuffer")) {
+ shallUsePBuffer = true;
+ } else if(args[i].equals("-bitmap")) {
+ shallUseBitmap = true;
+ } else if(args[i].equals("-manual")) {
+ manualTest = true;
+ }
+ }
+ System.err.println("swapInterval "+swapInterval);
+ System.err.println("useMSAA "+useMSAA);
+ System.err.println("useAnimator "+useAnimator);
+ System.err.println("shallUsePBuffer "+shallUsePBuffer);
+ System.err.println("shallUseBitmap "+shallUseBitmap);
+ System.err.println("manualTest "+manualTest);
+ org.junit.runner.JUnitCore.main(TestInstancedReneringGL4AWT.class.getName());
+ }
+}
diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/newt/TestInstancedReneringGL4NEWT.java b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/newt/TestInstancedReneringGL4NEWT.java
new file mode 100644
index 000000000..aeaaf0846
--- /dev/null
+++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/newt/TestInstancedReneringGL4NEWT.java
@@ -0,0 +1,108 @@
+/**
+ * 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.FixMethodOrder;
+import org.junit.Test;
+import org.junit.runners.MethodSorters;
+
+import com.jogamp.newt.opengl.GLWindow;
+import com.jogamp.opengl.test.junit.jogl.demos.gl4.TriangleInstancedRendererWithShaderState;
+import com.jogamp.opengl.test.junit.jogl.demos.gl4.TrianglesInstancedRendererHardcoded;
+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 Instanced rendering demo TrianglesInstancedRenderer
+ */
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
+public class TestInstancedReneringGL4NEWT 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 TriangleInstancedRendererWithShaderState(null));
+ }
+
+// @Test
+// public void test02_01b() throws InterruptedException {
+// final GLCapabilities caps = getCaps(GLProfile.GL4);
+// if( null == caps ) { return; }
+// testImpl(caps, new TrianglesInstancedRendererHardcoded(null));
+// }
+
+ 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 Instanced Rendering 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(TestInstancedReneringGL4NEWT.class.getName());
+ }
+}
diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/shader/triangles.fp b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/shader/triangles.fp
new file mode 100644
index 000000000..005884a1f
--- /dev/null
+++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/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/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/shader/triangles.vp b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/shader/triangles.vp
new file mode 100644
index 000000000..91cc6eff9
--- /dev/null
+++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/gl4/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);
+}