summaryrefslogtreecommitdiffstats
path: root/src/demos
diff options
context:
space:
mode:
Diffstat (limited to 'src/demos')
-rw-r--r--src/demos/es2/RawGL2ES2demo.java158
1 files changed, 90 insertions, 68 deletions
diff --git a/src/demos/es2/RawGL2ES2demo.java b/src/demos/es2/RawGL2ES2demo.java
index 2e2f345..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.newt.awt.NewtCanvasAWT;
-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;
/**
* <pre>
@@ -68,12 +56,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
* </p>
*
*
@@ -114,13 +102,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 +151,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 +175,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 +189,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 +206,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,
@@ -229,14 +214,7 @@ 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 GL2ES demo
+/* Introducing the GL2ES2 demo
*
* How to render a triangle using 424 lines of code using the RAW
* OpenGL ES 2 API.
@@ -246,7 +224,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 +249,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,23 +360,31 @@ 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) {
// Update variables used in animation
theta += 0.08;
s = Math.sin(theta);
- c = Math.cos(theta);
// 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 +400,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,26 +425,42 @@ 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
};
- 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, //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));
+ 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){