Skip to content

Instantly share code, notes, and snippets.

@markus-seidl
Last active August 29, 2015 14:24
Show Gist options
  • Save markus-seidl/fd06a96d2cb85ce94af6 to your computer and use it in GitHub Desktop.
Save markus-seidl/fd06a96d2cb85ce94af6 to your computer and use it in GitHub Desktop.
package de.markusseidl;
import com.jogamp.newt.event.KeyAdapter;
import com.jogamp.newt.event.KeyEvent;
import com.jogamp.newt.opengl.GLWindow;
import com.jogamp.opengl.*;
import com.jogamp.opengl.glu.GLU;
import com.jogamp.opengl.glu.GLUquadric;
import com.jogamp.opengl.util.Animator;
/**
* Self-contained example (within a single class only to keep it simple)
* displaying a rotating quad
*/
// http://www.land-of-kain.de/docs/jogl/
public class App implements GLEventListener {
private float rotateT = 0.0f;
static GLProfile glp;
static int width, height;
GLU glu;
float distance = 0;
static float max_distance = 300;
public static void initClass() {
width = 640;
height = 480;
if (GLProfile.isAvailable(GLProfile.GL2)) {
System.out.println("GL2 available!");
glp = GLProfile.get(GLProfile.GL2);
} else if (GLProfile.isAvailable(GLProfile.GLES2)) {
System.out.println("GLES2 available!");
glp = GLProfile.get(GLProfile.GLES2);
}
System.out.println("0");
}
public static void main(String[] args) throws InterruptedException {
System.out.println("---");
initClass();
final GLCapabilities caps = new GLCapabilities(glp);
System.out.println("1");
final GLWindow glWindow = GLWindow.create(caps);
System.out.println("2");
glWindow.setTitle("Gears NEWT Test");
glWindow.addGLEventListener(new App());
final Animator animator = new Animator(glWindow);
// final QuitAdapter quitAdapter = new QuitAdapter();
//glWindow.addKeyListener(new TraceKeyAdapter(quitAdapter));
//glWindow.addWindowListener(new TraceWindowAdapter(quitAdapter));
// glWindow.addKeyListener(quitAdapter);
// glWindow.addWindowListener(quitAdapter);
final GLWindow f_glWindow = glWindow;
glWindow.addKeyListener(new KeyAdapter() {
public void keyReleased(final KeyEvent e) {
if (!e.isPrintableKey() || e.isAutoRepeat()) {
return;
}
if (e.getKeyChar() == 'f') {
new Thread() {
public void run() {
f_glWindow.setFullscreen(!f_glWindow.isFullscreen());
}
}.start();
} else if (e.getKeyChar() == 'd') {
new Thread() {
public void run() {
f_glWindow.setUndecorated(!f_glWindow.isUndecorated());
}
}.start();
} else if (e.getKeyChar() == 'x') {
System.exit(-1);
}
}
});
glWindow.setSize(width, height);
glWindow.setVisible(true);
animator.setUpdateFPSFrames(1, null);
animator.start();
while (animator.isAnimating() && animator.getTotalFPSDuration() < 5000) {
Thread.sleep(100);
}
animator.stop();
glWindow.destroy();
}
// GLEventListener
public void init(GLAutoDrawable drawable) {
GL gl = drawable.getGL();
glu = new GLU();
gl.glClearColor(0f, 0f, 0f, 1f);
}
public void dispose(GLAutoDrawable drawable) {
}
public void display(GLAutoDrawable drawable) {
GL gl = drawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
setCamera(gl.getGLES2(), glu, distance);
if(gl.isGL2()) {
GL2 gl2 = gl.getGL2();
/*// Write triangle.
gl2.glColor3f(0.9f, 0.5f, 0.2f);
gl2.glBegin(GL.GL_TRIANGLE_FAN);
gl2.glVertex3f(-20, -20, 0);
gl2.glVertex3f(+20, -20, 0);
gl2.glVertex3f(0, 20, 0);
gl2.glEnd();*/
// gl2.glColor3f(0.3f, 0.5f, 1f);
} else if (gl.isGLES2()) {
}
GLUquadric earth = glu.gluNewQuadric();
glu.gluQuadricDrawStyle(earth, GLU.GLU_FILL);
glu.gluQuadricNormals(earth, GLU.GLU_FLAT);
glu.gluQuadricOrientation(earth, GLU.GLU_OUTSIDE);
final float radius = 4*6.378f;
final int slices = 32;
final int stacks = 16;
glu.gluSphere(earth, radius, slices, stacks);
glu.gluDeleteQuadric(earth);
distance = Math.max(60, (distance+1) % max_distance);
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
}
// UTILITY
private void setCamera(GLES2 gl, GLU glu, float distance) {
// Change to projection matrix.
/*gl.matrix
gl.glMatrixMode(GL2ES1.GL_PROJECTION);
gl.glLoadIdentity();
// Perspective.
float widthHeightRatio = (float) width / (float) height;
glu.gluPerspective(45, widthHeightRatio, 1, 1000);
glu.gluLookAt(0, 0, distance, 0, 0, 0, 0, 1, 0);
// Change back to model view matrix.
gl.glMatrixMode(GL2ES1.GL_MODELVIEW);
gl.glLoadIdentity();*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment