Skip to content

Instantly share code, notes, and snippets.

@xranby
Created September 25, 2015 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xranby/cd01e3075da128703b9f to your computer and use it in GitHub Desktop.
Save xranby/cd01e3075da128703b9f to your computer and use it in GitHub Desktop.
JOGLQuadES2.java
import com.jogamp.opengl.*;
import com.jogamp.opengl.fixedfunc.*;
import com.jogamp.opengl.util.Animator;
import com.jogamp.opengl.util.ImmModeSink;
import com.jogamp.opengl.util.PMVMatrix;
import com.jogamp.opengl.util.glsl.ShaderState;
import com.jogamp.opengl.util.glsl.fixedfunc.FixedFuncUtil;
import com.jogamp.opengl.util.glsl.fixedfunc.ShaderSelectionMode;
import com.jogamp.newt.opengl.GLWindow;
/**
* Self-contained example (within a single class only to keep it simple)
* displaying a rotating quad
*/
public class JOGLQuadES2 implements GLEventListener {
private float rotateT = 0.0f;
private static Animator animator;
private static final ShaderState shaderState = new ShaderState();
private static final ImmModeSink glSink = ImmModeSink.createFixed( 40,
3, GL.GL_FLOAT, // vertex
4, GL.GL_FLOAT, // color
0, GL.GL_FLOAT, // normal
0, GL.GL_FLOAT, // texCoords
GL.GL_STATIC_DRAW);
private static GL2ES1 glFixedFuncUtil;
@Override
public void init(GLAutoDrawable glDrawable) {
GL2ES2 gl = glDrawable.getGL().getGL2ES2();
PMVMatrix pmvMatrix = new PMVMatrix();
// All future GLAutoDrawable will appear as an GL2ES1 profile after wrapping the ES2 hardware using the glFixedFuncUtil
glFixedFuncUtil = FixedFuncUtil.wrapFixedFuncEmul(gl, ShaderSelectionMode.AUTO, pmvMatrix);
glFixedFuncUtil.glShadeModel(GLLightingFunc.GL_SMOOTH);
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glClearDepth(1.0f);
gl.glEnable(GL.GL_DEPTH_TEST);
gl.glDepthFunc(GL.GL_LEQUAL);
glFixedFuncUtil.glHint(GL2ES1.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
}
@Override
public void display(GLAutoDrawable glDrawable) {
final GL2ES1 gl = glDrawable.getGL().getGL2ES1();
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
gl.glClear(GL.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, -5.0f);
// rotate about the three axes
gl.glRotatef(rotateT, 1.0f, 0.0f, 0.0f);
gl.glRotatef(rotateT, 0.0f, 1.0f, 0.0f);
gl.glRotatef(rotateT, 0.0f, 0.0f, 1.0f);
// Draw A Quad
// ES2 do not support glBegin(GL2.GL_QUADS)
// JogAmp JOGL can use its ImmModeSink class to emulate GL2 QUADS on ES2 hardware.
glSink.glBegin(glSink.GL_QUADS);
glSink.glColor3f(0.0f, 1.0f, 0.5f); // set the color of the quad
glSink.glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
glSink.glColor3f(1.0f, 0.0f, 0.5f);
glSink.glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right
glSink.glColor3f(1.0f, 0.5f, 0.0f);
glSink.glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glSink.glColor3f(0.0f, 0.5f, 1.0f);
glSink.glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
// Done Drawing The Quad
glSink.glEnd(gl);
// increasing rotation for the next iteration
rotateT += 0.2f;
}
@Override
public void reshape(GLAutoDrawable glDrawable, int x, int y, int width, int height) {
GL2ES1 gl = glDrawable.getGL().getGL2ES1();
final float aspect = (float) width / (float) height;
gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
gl.glLoadIdentity();
final float fh = 0.5f;
final float fw = fh * aspect;
gl.glFrustumf(-fw, fw, -fh, fh, 1.0f, 1000.0f);
gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
gl.glLoadIdentity();
}
@Override
public void dispose(GLAutoDrawable glDrawable) {
animator.stop();
glSink.destroy(glDrawable.getGL());
}
public static void main(String[] args) {
GLCapabilities caps = new GLCapabilities(GLProfile.getGL2ES2());//.get(GLProfile.GL2ES2));
// We may at this point tweak the caps and request a translucent drawable
caps.setBackgroundOpaque(false);
GLWindow glWindow = GLWindow.create(caps);
animator = new Animator(glWindow);
glWindow.addGLEventListener(new JOGLQuadES2());
glWindow.setTitle("Jogl ES2 Quad drawing");
glWindow.setSize(600, 400);
glWindow.setVisible(true);
animator.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment