From ccb06ad604c0dd33cb8dd361413aef671af42c15 Mon Sep 17 00:00:00 2001
From: Xerxes Rånby
Date: Wed, 22 Aug 2012 13:34:16 +0200
Subject: es2/RawGL2ES2demo: Remove AWT dependency to allow running on a
Raspberry Pi; Make syntax BeanShell compatible.
- Raspberry Pi: Removed dependency of com.jogamp.newt.awt.NewtCanvasAWT
to allow running on -Djava.awt.headlessi=true systems,
we can do this by using NEWT directly to open a window.
You can run this demo on a Raspberry Pi
using the JogAmp JOGL auto-build aggregate from 20 Aug 2012 and later:
wget http://jogamp.org/deployment/archive/master/gluegen_577-joal_347-jogl_790-jocl_653/archive/jogamp-all-platforms.7z
7z x jogamp-all-platforms.7z
cd jogamp-all-platforms
wget https://raw.github.com/xranby/jogl-demos/master/src/demos/es2/RawGL2ES2demo.java
javac -cp jar/jogl-all.jar:jar/gluegen-rt.jar RawGL2ES2demo.java
java -Dnativewindow.ws.name=jogamp.newt.driver.bcm.vc.iv -cp jar/jogl-all.jar:jar/gluegen-rt.jar:. RawGL2ES2demo
- Beanshell: removed final in function arguments
and changed float array declaration
to become BeanShell compatible.
You can run this demo interpreted using Beanshell:
wget http://www.beanshell.org/bsh-2.0b4.jar
java -cp jar/jogl-all.jar:jar/gluegen-rt.jar:bsh-2.0b4.jar:. bsh.Interpreter RawGL2ES2demo.java
- Vertex and Fragment shaders: remove #version lines to make the shaders compile on more drivers.
---
src/demos/es2/RawGL2ES2demo.java | 118 ++++++++++++++++++++++++---------------
1 file changed, 72 insertions(+), 46 deletions(-)
diff --git a/src/demos/es2/RawGL2ES2demo.java b/src/demos/es2/RawGL2ES2demo.java
index 2e2f345..c055aeb 100644
--- a/src/demos/es2/RawGL2ES2demo.java
+++ b/src/demos/es2/RawGL2ES2demo.java
@@ -35,7 +35,6 @@ import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLContext;
import com.jogamp.newt.opengl.GLWindow;
-import com.jogamp.newt.awt.NewtCanvasAWT;
import com.jogamp.opengl.util.GLArrayDataServer;
import com.jogamp.opengl.util.glsl.ShaderCode;
import com.jogamp.opengl.util.glsl.ShaderState;
@@ -68,12 +67,12 @@ import javax.media.opengl.awt.GLCanvas;
* JOGL2 OpenGL ES 2 demo to expose and learn what the RAW OpenGL ES 2 API looks like.
*
* Compile, run and enjoy:
- wget http://jogamp.org/deployment/jogamp-test/archive/jogamp-all-platforms.7z
+ wget http://jogamp.org/deployment/jogamp-current/archive/jogamp-all-platforms.7z
7z x jogamp-all-platforms.7z
cd jogamp-all-platforms
wget https://raw.github.com/xranby/jogl-demos/master/src/demos/es2/RawGL2ES2demo.java
- javac -cp jar/jogl.all.jar:jar/gluegen-rt.jar RawGL2ES2demo.java
- java -cp jar/jogl.all.jar:jar/gluegen-rt.jar:. RawGL2ES2demo
+ javac -cp jar/jogl-all.jar:jar/gluegen-rt.jar RawGL2ES2demo.java
+ java -cp jar/jogl-all.jar:jar/gluegen-rt.jar:. RawGL2ES2demo
*
*
*
@@ -114,13 +113,13 @@ public class RawGL2ES2demo implements GLEventListener{
* sent to the GPU driver for compilation.
*/
static final String vertexShader =
+// For GLSL 1 and 1.1 code i highly recomend to not include a
+// GLSL ES language #version line, GLSL ES section 3.4
+// Many GPU drivers refuse to compile the shader if #version is different from
+// the drivers internal GLSL version.
"#ifdef GL_ES \n" +
-"#version 100 \n" + // GLSL ES language version, GLSL ES section 3.4
- // many GPU drivers require it.
"precision mediump float; \n" + // Precision Qualifiers
"precision mediump int; \n" + // GLSL ES section 4.5.2
-"#else \n" +
-"#version 110 \n" + // Mesa X11 GL drivers require version 1.1
"#endif \n" +
"uniform mat4 uniform_Projection; \n" + // Incomming data used by
@@ -163,17 +162,14 @@ static final String vertexShader =
*/
static final String fragmentShader =
"#ifdef GL_ES \n" +
-"#version 100 \n" +
"precision mediump float; \n" +
"precision mediump int; \n" +
-"#else \n" +
-"#version 110 \n" +
"#endif \n" +
"varying vec4 varying_Color; \n" + //incomming varying data to the
//frament shader
//sent from the vertex shader
-"void main (void)\n" +
+"void main (void) \n" +
"{ \n" +
" gl_FragColor = varying_Color; \n" +
"} ";
@@ -190,7 +186,7 @@ static final String fragmentShader =
* These helpers here are based on PMVMatrix code and common linear
* algebra for matrix multiplication, translate and rotations.
*/
- private void glMultMatrixf(final FloatBuffer a, final FloatBuffer b, FloatBuffer d) {
+ private void glMultMatrixf(FloatBuffer a, FloatBuffer b, FloatBuffer d) {
final int aP = a.position();
final int bP = b.position();
final int dP = d.position();
@@ -204,13 +200,13 @@ static final String fragmentShader =
}
private float[] multiply(float[] a,float[] b){
- float tmp[] = new float[16];
+ float[] tmp = new float[16];
glMultMatrixf(FloatBuffer.wrap(a),FloatBuffer.wrap(b),FloatBuffer.wrap(tmp));
return tmp;
}
private float[] translate(float[] m,float x,float y,float z){
- float t[] = { 1.0f, 0.0f, 0.0f, 0.0f,
+ float[] t = { 1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
x, y, z, 1.0f };
@@ -221,7 +217,7 @@ static final String fragmentShader =
float s, c;
s = (float)Math.sin(Math.toRadians(a));
c = (float)Math.cos(Math.toRadians(a));
- float r[] = {
+ float[] r = {
x * x * (1.0f - c) + c, y * x * (1.0f - c) + z * s, x * z * (1.0f - c) - y * s, 0.0f,
x * y * (1.0f - c) - z * s, y * y * (1.0f - c) + c, y * z * (1.0f - c) + x * s, 0.0f,
x * z * (1.0f - c) + y * s, y * z * (1.0f - c) - x * s, z * z * (1.0f - c) + c, 0.0f,
@@ -236,7 +232,7 @@ static final String fragmentShader =
m[12]+" "+m[13]+" "+m[14]+" "+m[15]+"\n");
}
-/* Introducing the GL2ES demo
+/* Introducing the GL2ES2 demo
*
* How to render a triangle using 424 lines of code using the RAW
* OpenGL ES 2 API.
@@ -247,6 +243,9 @@ static final String fragmentShader =
private double theta=0;
private double s=0;
private double c=0;
+
+ private static int width=1920;
+ private static int height=1080;
private int shaderProgram;
private int vertShader;
@@ -269,22 +268,40 @@ static final String fragmentShader =
*/
GLCapabilities caps = new GLCapabilities(GLProfile.get(GLProfile.GL2ES2));
- GLWindow canvas = GLWindow.create(caps);
-
- NewtCanvasAWT newtCanvas = new NewtCanvasAWT(canvas);
- JFrame frame = new JFrame("RAW GL2ES Demo");
- frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- frame.setSize(300,300);
- frame.add(newtCanvas);
- //add some swing code if you like.
- /* javax.swing.JButton b = new javax.swing.JButton();
- b.setText("Hi");
- frame.add(b); */
- frame.setVisible(true);
-
- canvas.addGLEventListener(new RawGL2ES2demo());
- FPSAnimator animator = new FPSAnimator(canvas,60);
- animator.add(canvas);
+ // We may at this point tweak the caps and request a translucent drawable
+ caps.setBackgroundOpaque(false);
+ GLWindow glWindow = GLWindow.create(caps);
+
+ /* You may combine the NEWT GLWindow inside existing Swing and AWT
+ * applications by encapsulating the glWindow inside a
+ * com.jogamp.newt.awt.NewtCanvasAWT canvas.
+ *
+ * NewtCanvasAWT newtCanvas = new NewtCanvasAWT(glWindow);
+ * JFrame frame = new JFrame("RAW GL2ES2 Demo inside a JFrame!");
+ * frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
+ * frame.setSize(width,height);
+ * frame.add(newtCanvas);
+ * // add some swing code if you like.
+ * // javax.swing.JButton b = new javax.swing.JButton();
+ * // b.setText("Hi");
+ * // frame.add(b);
+ * frame.setVisible(true);
+ */
+
+ // In this demo we prefer to setup and view the GLWindow directly
+ // this allows the demo to run on -Djava.awt.headless=true systems
+ glWindow.setTitle("Raw GL2ES2 Demo");
+ glWindow.setSize(width,height);
+ glWindow.setUndecorated(false);
+ glWindow.setPointerVisible(true);
+ glWindow.setVisible(true);
+
+ // Finally we connect the GLEventListener application code to the NEWT GLWindow.
+ // GLWindow will call the GLEventListener init, reshape, display and dispose
+ // functions when needed.
+ glWindow.addGLEventListener(new RawGL2ES2demo() /* GLEventListener */);
+ FPSAnimator animator = new FPSAnimator(glWindow,60);
+ animator.add(glWindow);
animator.start();
}
@@ -362,6 +379,16 @@ static final String fragmentShader =
}
public void reshape(GLAutoDrawable drawable, int x, int y, int z, int h) {
+ System.out.println("Window resized to width=" + z + " height=" + h);
+ width = z;
+ height = h;
+
+ // Get gl
+ GL2ES2 gl = drawable.getGL().getGL2ES2();
+
+ // Optional: Set viewport
+ // Render to a square at the center of the window.
+ gl.glViewport((width-height)/2,0,height,height);
}
public void display(GLAutoDrawable drawable) {
@@ -373,12 +400,11 @@ static final String fragmentShader =
// Get gl
GL2ES2 gl = drawable.getGL().getGL2ES2();
- // Set viewport
- //gl.glViewport(0,0,300,300);
-
// Clear screen
- gl.glClearColor(1, 0, 1, 1); //Purple
- gl.glClear(GL2ES2.GL_COLOR_BUFFER_BIT | GL2ES2.GL_DEPTH_BUFFER_BIT);
+ gl.glClearColor(1, 0, 1, 0.5f); // Purple
+ gl.glClear(GL2ES2.GL_STENCIL_BUFFER_BIT |
+ GL2ES2.GL_COLOR_BUFFER_BIT |
+ GL2ES2.GL_DEPTH_BUFFER_BIT );
// Use the shaderProgram that got linked during the init part.
gl.glUseProgram(shaderProgram);
@@ -394,8 +420,8 @@ static final String fragmentShader =
*
*/
- float model_view_projection[];
- float identity_matrix[] = {
+ float[] model_view_projection;
+ float[] identity_matrix = {
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
@@ -419,7 +445,7 @@ static final String fragmentShader =
* gl.glEnd(); // Finished Drawing The Triangle
*/
- float vertices[] = { 0.0f, 1.0f, 0.0f, //Top
+ float[] vertices = { 0.0f, 1.0f, 0.0f, //Top
-1.0f, -1.0f, 0.0f, //Bottom Left
1.0f, -1.0f, 0.0f //Bottom Right
};
@@ -427,12 +453,12 @@ static final String fragmentShader =
gl.glVertexAttribPointer(0, 3, GL2ES2.GL_FLOAT, false, 0, FloatBuffer.wrap(vertices));
gl.glEnableVertexAttribArray(0);
- float colors[] = { 1.0f, 0.0f, 0.0f, //Top color (red)
- 0.0f, 0.0f, 0.0f, //Bottom Left color (black)
- 1.0f, 1.0f, 0.0f //Bottom Right color (yellow)
- };
+ float[] colors = { 1.0f, 0.0f, 0.0f, 1.0f, //Top color (red)
+ 0.0f, 0.0f, 0.0f, 1.0f, //Bottom Left color (black)
+ 1.0f, 1.0f, 0.0f, 0.9f //Bottom Right color (yellow) with 10% transparence
+ };
- gl.glVertexAttribPointer(1, 3, GL2ES2.GL_FLOAT, false, 0, FloatBuffer.wrap(colors));
+ gl.glVertexAttribPointer(1, 4, GL2ES2.GL_FLOAT, false, 0, FloatBuffer.wrap(colors));
gl.glEnableVertexAttribArray(1);
gl.glDrawArrays(GL2ES2.GL_TRIANGLES, 0, 3); //Draw the vertices as triangle
--
cgit v1.2.3
From c9553e82282d86e336ab21e6565f0c828cfb8351 Mon Sep 17 00:00:00 2001
From: Xerxes Rånby
Date: Thu, 20 Sep 2012 12:43:10 +0200
Subject: Use NIO Direct buffers to pass data to glVertexAttribPointer in
RawGL2ES2demo.java
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
https://github.com/sgothel/jogl/commit/3f5aac3536956e245d0e151e4915e7dd67a08b7e
Fixed two logical flaws regarding:
- GL spec: Pointer functions required passed memory pointers to persist
- JNI: Primitive arrays may change their native memory location (swap),
even if they were not GC'ed.
The demo code is now updated to stay compatible with this change.
Signed-off-by Xerxes Rånby
---
src/demos/es2/RawGL2ES2demo.java | 21 +++++++++++++++++++--
1 file changed, 19 insertions(+), 2 deletions(-)
diff --git a/src/demos/es2/RawGL2ES2demo.java b/src/demos/es2/RawGL2ES2demo.java
index c055aeb..f03de41 100644
--- a/src/demos/es2/RawGL2ES2demo.java
+++ b/src/demos/es2/RawGL2ES2demo.java
@@ -40,6 +40,7 @@ import com.jogamp.opengl.util.glsl.ShaderCode;
import com.jogamp.opengl.util.glsl.ShaderState;
import com.jogamp.opengl.util.glsl.ShaderProgram;
import com.jogamp.opengl.util.*;
+import com.jogamp.common.nio.Buffers;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
@@ -450,7 +451,17 @@ static final String fragmentShader =
1.0f, -1.0f, 0.0f //Bottom Right
};
- gl.glVertexAttribPointer(0, 3, GL2ES2.GL_FLOAT, false, 0, FloatBuffer.wrap(vertices));
+
+ // Observe that the vertex data passed to glVertexAttribPointer must stay valid
+ // through the OpenGL rendering lifecycle.
+ // Therefore it is mandatory to allocate a NIO Direct buffer that stays pinned in memory
+ // and thus can not get moved by the java garbage collector.
+ // Also we need to keep a reference to the NIO Direct buffer around up untill
+ // we call glDisableVertexAttribArray first then will it be safe to garbage collect the memory.
+ // I will here use the com.jogamp.common.nio.Buffers to quicly wrap the array in a Direct NIO buffer.
+ FloatBuffer fbVertices = Buffers.newDirectFloatBuffer(vertices);
+
+ gl.glVertexAttribPointer(0, 3, GL2ES2.GL_FLOAT, false, 0, fbVertices);
gl.glEnableVertexAttribArray(0);
float[] colors = { 1.0f, 0.0f, 0.0f, 1.0f, //Top color (red)
@@ -458,13 +469,19 @@ static final String fragmentShader =
1.0f, 1.0f, 0.0f, 0.9f //Bottom Right color (yellow) with 10% transparence
};
- gl.glVertexAttribPointer(1, 4, GL2ES2.GL_FLOAT, false, 0, FloatBuffer.wrap(colors));
+ FloatBuffer fbColors = Buffers.newDirectFloatBuffer(colors);
+
+ gl.glVertexAttribPointer(1, 4, GL2ES2.GL_FLOAT, false, 0, fbColors);
gl.glEnableVertexAttribArray(1);
gl.glDrawArrays(GL2ES2.GL_TRIANGLES, 0, 3); //Draw the vertices as triangle
gl.glDisableVertexAttribArray(0); // Allow release of vertex position memory
gl.glDisableVertexAttribArray(1); // Allow release of vertex color memory
+ // It is only safe to let the garbage collector collect the vertices and colors
+ // NIO buffers data after first calling glDisableVertexAttribArray.
+ fbVertices = null;
+ fbColors = null;
}
public void dispose(GLAutoDrawable drawable){
--
cgit v1.2.3
From 970515a87942224361a036d3ad17713ee3dac366 Mon Sep 17 00:00:00 2001
From: Xerxes Rånby
Date: Thu, 20 Sep 2012 12:53:49 +0200
Subject: Fix ecj warnings in src/demos/es2/RawGL2ES2demo.java Removed unused
imports, unused c field and the unused printMatrix function.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Signed-off-by: Xerxes Rånby
---
src/demos/es2/RawGL2ES2demo.java | 21 ---------------------
1 file changed, 21 deletions(-)
diff --git a/src/demos/es2/RawGL2ES2demo.java b/src/demos/es2/RawGL2ES2demo.java
index f03de41..6cccc6f 100644
--- a/src/demos/es2/RawGL2ES2demo.java
+++ b/src/demos/es2/RawGL2ES2demo.java
@@ -32,24 +32,12 @@ import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.GLCapabilities;
-import javax.media.opengl.GLContext;
import com.jogamp.newt.opengl.GLWindow;
-import com.jogamp.opengl.util.GLArrayDataServer;
-import com.jogamp.opengl.util.glsl.ShaderCode;
-import com.jogamp.opengl.util.glsl.ShaderState;
-import com.jogamp.opengl.util.glsl.ShaderProgram;
import com.jogamp.opengl.util.*;
import com.jogamp.common.nio.Buffers;
-import java.nio.ByteBuffer;
-import java.nio.ByteOrder;
import java.nio.FloatBuffer;
-import java.nio.Buffer;
-
-import javax.swing.JFrame;
-import javax.media.opengl.*;
-import javax.media.opengl.awt.GLCanvas;
/**
*
@@ -226,13 +214,6 @@ static final String fragmentShader =
return multiply(m, r);
}
- private void printMatrix(float[] m){
- System.out.println(m[0]+" "+m[1]+" "+m[2]+" "+m[3]+"\n"+
- m[4]+" "+m[5]+" "+m[6]+" "+m[7]+"\n"+
- m[8]+" "+m[9]+" "+m[10]+" "+m[11]+"\n"+
- m[12]+" "+m[13]+" "+m[14]+" "+m[15]+"\n");
- }
-
/* Introducing the GL2ES2 demo
*
* How to render a triangle using 424 lines of code using the RAW
@@ -243,7 +224,6 @@ static final String fragmentShader =
*/
private double theta=0;
private double s=0;
- private double c=0;
private static int width=1920;
private static int height=1080;
@@ -396,7 +376,6 @@ static final String fragmentShader =
// Update variables used in animation
theta += 0.08;
s = Math.sin(theta);
- c = Math.cos(theta);
// Get gl
GL2ES2 gl = drawable.getGL().getGL2ES2();
--
cgit v1.2.3