P3D Display of GLUT Shapes

The following source code demonstrates display of GLUT (OpenGL Utility Toolkit) shapes in a P3D GLWindow. Initial implementation of GL2ES1 is necessary and carried out in the settings() method before the sketch is set up. It is not possible to use Swing or AWT components with a GLWindow so a ControlP5 scrollableList was used for selecting the GLUT shapes.

Source Code:

//Reference: //https://github.com/processing/processing/blob/master/core/src/processing/opengl/PSurfaceJOGL.java

import com.jogamp.newt.opengl.GLWindow;
import com.jogamp.opengl.util.gl2.GLUT;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GL;
import controlP5.*;
import java.util.*;

GLWindow window;
GL2 gl;
GLUT glut;
ControlP5 cp5;

PFont font;
float angle;
int selection;

void settings() { 
  size(800, 600, P3D);
  PJOGL.profile = 1; // GL2ES1 Implementation
}

void setup() {
  window = (GLWindow) surface.getNative();
  surface.setTitle("JOGL_GLUT");
  gl = (GL2)window.getGL();
  glut = new GLUT();
  font = createFont("Arial", 6, true);
  cp5 = new ControlP5(this);
  List l = Arrays.asList("sphere", "cone", "cylinder", "cube", "torus", "octahedron", "icosahedron", "tetrahedron", "teapot", "rhombicDodeca...");
  cp5.addScrollableList("dropdown")
    .setPosition(30, 20)
    .setLabel("shapes")
    .setColorCaptionLabel(color(255, 255, 0))
    .setFont(font)
    .setWidth(140)
    .setBarHeight(24)
    .setItemHeight(16)
    .addItems(l)
    ;
}

void draw() {
  background(0);
  beginPGL();
  gl.glPushMatrix();
  gl.glTranslatef(width/2, height/2, 0);
  gl.glScalef(200, 200, 200);
  gl.glLineWidth(4.0);
  gl.glRotatef(angle, 1, 1, 0);
  switch(selection) {
  case 0:
    gl.glColor3f(0.0, 1.0, 0.0);
    glut.glutWireSphere(1.0, 64, 16);
    break;
  case 1:
    gl.glColor3f(1.0, 0.0, 0.0);
    glut.glutWireCone(1.0, 1.0, 64, 16);
    break;
  case 2:
    gl.glColor3f(0.0, 1.0, 1.0);
    glut.glutWireCylinder(0.5, 1.5, 64, 32);
    break;
  case 3:
    gl.glColor3f(0.9, 0.7, 0.1);
    glut.glutWireCube(1.0);
    break;
  case 4:
    gl.glColor3f(0.15, 0.9, 0.84);
    gl.glLineWidth(1.0);
    glut.glutWireTorus(0.25, 0.5, 140, 35);
    break;
  case 5:
    gl.glColor3f(0.0, 0.0, 1.0);
    glut.glutWireOctahedron();
    break;
  case 6:
    gl.glColor3f(1.0, 1.0, 1.0);
    glut.glutWireIcosahedron();
    break;
  case 7:
    gl.glColor3f(1.0, 0.0, 1.0);
    glut.glutWireTetrahedron();
    break;
  case 8:
    gl.glColor3f(0.75, 0.0, 0.0);
    glut.glutWireTeapot(0.75);
    break;
  case 9:
    gl.glColor3f(1.0, 1.0, 0.0);
    glut.glutWireRhombicDodecahedron();
    break;
  default:
  }
  gl.glPopMatrix();
  endPGL();
  angle += 0.5;
}

void dropdown(int n) {
  cp5.get(ScrollableList.class, "dropdown").getItem(n);
  selection = n;
}

Output:

2 Likes