Course pages 2015–16
Computer Graphics and Image Processing
Using OpenGL
These notes should help you install and run OpenGL for Java in Eclipse.
Eclipse
Install:
- Eclipse from www.eclipse.org/downloads/
- Java SDK from www.oracle.com/technetwork/java/javase/downloads/
Java OpenGL (JOGL)
Follow the guidance about installing JOGL on Justin Stoecker's web pages.
Install:
- JOGL from jogamp.org
- Note that the libraries are actually gluegen-rt.jar, jogl-all.jar, nativewindow.jar and newt.jar
Write a program
Follow the guidance on creating a window and drawing.
In Eclipse:
- File > New > Java Project with some suitable name, say MyOpenGL
- Java > Build Path > User Libraries and include the four .jar files
- MyOpenGL > Properties > Java Build Path > Libraries > Add Library > User Library and add jogl-2.0
- MyOpenGL > New > Package with some suitable name, say myopengl
- myopengl > New > Class with some suitable name, say MyMain
public class MyMain extends JFrame { private MyGraphics mg; MyMain(String name) { ... GLProfile glp = GLProfile.getDefault(); GLCapabilities glc = new GLCapabilities(glp); mg = new MyGraphics(glc); add(mg,...) JPanel mc = new MyControl(...) { protected void onInput(...) {mg.setInput(...);}; }; add(mc,...) } public static void main(String[] args) { ... } }
- myopengl > New > Class with some suitable name, say MyControl
public class MyControl extends JPanel { JInput ji; MyControl(...) { ... ji = createInput(...); ji.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) {onInput(...);}; }); } }
- myopengl > New > Class with some suitable name, say MyGraphics
public class MyGraphics extends GLCanvas implements GLEventListener { MyGraphics(GLCapabilities glc) { ... this.addGLEventListener(this); } public void init(GLAutoDrawable drawable) { // Build model in buffers // Compile and link shaders // Bind buffers and transformations to shader variables } public void display(GLAutoDrawable drawable) { // Update transformations // Draw } public void dispose(GLAutoDrawable drawable) { ... } public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { ... } }
Vertex and fragment shaders
Follow the guidance on rendering models.
Still in Eclipse:
- myopengl > New > Untitled Text File with some suitable name, say vertexshader.glsl
// Per object input uniform mat4 model; uniform mat4 projection; // Per vertex input in vec3 position; in vec3 colour; // Per vertex output - will be interpolated across a face out vec3 vPosition; out vec3 vNormal; out vec3 vColour; void main(void) { vec4 p = model * vec4(position, 1.0); gl_Position = projection * p; vPosition = p.xyz / p.w; vNormal = ...; vColour = colour; }
- myopengl > New > Untitled Text File with some suitable name, say fragmentshader.glsl
in vec3 vPosition; in vec3 vNormal; in vec3 vColour; void main(void) { // computer illumination gl_FragColor = vec4(..., 1.0); }
A sample program
Here is a complete program to draw a tetrahedron: