Computer graphics -- 2008-2009 -- info.uvt.ro/JOGL-Template

From Wikiversity

Jump to: navigation, search

Quick links: front; laboratories agenda, 1, 2, 3, 4, 5, JOGL template.


[edit] Complete example

Two example templates are given here. One for scenes using an orthographic projection and one for scenes using a perspective projection. These examples are intended to be used as templates for future JOGL based applications.

[edit] Orthographic projection example

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
 
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
 
import com.sun.opengl.util.Animator;
import com.sun.opengl.util.FPSAnimator;
 
 
public class MainFrame
		extends Frame
		implements GLEventListener
{
 
	private GLCanvas canvas;
	private Animator animator;
 
	// For specifying the positions of the clipping planes (increase/decrease the distance) modify this variable.
 	// It is used by the glOrtho method.
	private double v_size = 1.0;
 
	public MainFrame()
	{
		super("Java OpenGL");
 
		this.setLayout(new BorderLayout());
 
		// Registering a window event listener to handle the closing event.
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
 
		this.setSize(800, 600);
 
		this.initializeJogl();
 
		this.setVisible(true);
	}
 
	private void initializeJogl()
	{
		// Creating an object to manipulate OpenGL parameters.
		GLCapabilities capabilities = new GLCapabilities();
 
		// Setting some OpenGL parameters.
		capabilities.setHardwareAccelerated(true);
		capabilities.setDoubleBuffered(true);
 
		// Creating an OpenGL display widget -- canvas.
		this.canvas = new GLCanvas();
 
		// Adding the canvas in the center of the frame.
		this.add(this.canvas, BorderLayout.CENTER);
 
		// Adding an OpenGL event listener to the canvas.
		this.canvas.addGLEventListener(this);
 
		// Creating an animator that will redraw the scene 40 times per second.
		this.animator = new FPSAnimator(40);
 
		// Registering the canvas to the animator.
		this.animator.add(this.canvas);
 
		// Starting the animator.
		this.animator.start();
	}
 
	public void init(GLAutoDrawable canvas)
	{
		// Obtaining the GL instance associated with the canvas.
		GL gl = canvas.getGL();
 
		// Setting the clear color -- the color which will be used to erase the canvas.
		gl.glClearColor(0, 0, 0, 0);
 
		// Selecting the projection matrix.
		gl.glMatrixMode(GL.GL_PROJECTION);
 
		// Initializing the projection matrix with the identity matrix.
		gl.glLoadIdentity();
 
		// Setting the projection to be orthographic.
		// Selecting the view volume to be x from 0 to 1, y from 0 to 1, z from -1 to 1. 
		gl.glOrtho(-v_size, v_size, -v_size, v_size, -1, 1);
 
		// Selecting the modelview matrix.
		gl.glMatrixMode(GL.GL_MODELVIEW);
 
	}
 
	public void display(GLAutoDrawable canvas)
	{
		GL gl = canvas.getGL();
 
		// Erasing the canvas -- filling it with the clear color.
		gl.glClear(GL.GL_COLOR_BUFFER_BIT);
 
		// Add your scene code here
 
		// Forcing the scene to be rendered.
		gl.glFlush();
	}
 
	public void reshape(GLAutoDrawable canvas, int left, int top, int width, int height)
	{
		GL gl = canvas.getGL();
 
		// Selecting the viewport -- the display area -- to be the entire widget.
		gl.glViewport(0, 0, width, height);
 
		// Determining the width to height ratio of the widget.
		double ratio = (double) width / (double) height;
 
		// Selecting the projection matrix.
		gl.glMatrixMode(GL.GL_PROJECTION);
 
		gl.glLoadIdentity();
 
		// Selecting the view volume to be x from 0 to 1, y from 0 to 1, z from -1 to 1.
		// But we are careful to keep the aspect ratio and enlarging the width or the height.
		if (ratio < 1)
			gl.glOrtho(-v_size, v_size, -v_size, v_size / ratio, -1, 1);
		else
			gl.glOrtho(-v_size, v_size * ratio, -v_size, v_size, -1, 1);
 
		// Selecting the modelview matrix.
		gl.glMatrixMode(GL.GL_MODELVIEW);		
	}
 
	public void displayChanged(GLAutoDrawable canvas, boolean modeChanged, boolean deviceChanged)
	{
		return;
	}
}

[edit] Perspective projection example

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
 
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
 
import com.sun.opengl.util.Animator;
import com.sun.opengl.util.FPSAnimator;
 
import javax.media.opengl.glu.GLU;
 
public class MainFrame
		extends Frame
		implements GLEventListener
{
 
	private GLCanvas canvas;
	private Animator animator;
 
	private GLU glu;
 
	public MainFrame()
	{
		super("Java OpenGL");
 
		this.setLayout(new BorderLayout());
 
		// Registering a window event listener to handle the closing event.
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
 
		this.setSize(800, 600);
 
		this.initializeJogl();
 
		this.setVisible(true);
	}
 
	private void initializeJogl()
	{
		// Creating an object to manipulate OpenGL parameters.
		GLCapabilities capabilities = new GLCapabilities();
 
		// Setting some OpenGL parameters.
		capabilities.setHardwareAccelerated(true);
		capabilities.setDoubleBuffered(true);
 
		// Creating an OpenGL display widget -- canvas.
		this.canvas = new GLCanvas();
 
		// Adding the canvas in the center of the frame.
		this.add(this.canvas, BorderLayout.CENTER);
 
		// Adding an OpenGL event listener to the canvas.
		this.canvas.addGLEventListener(this);
 
		// Creating an animator that will redraw the scene 40 times per second.
		this.animator = new FPSAnimator(40);
 
		// Registering the canvas to the animator.
		this.animator.add(this.canvas);
 
		// Starting the animator.
		this.animator.start();
	}
 
	public void init(GLAutoDrawable canvas)
	{
		// Obtaining the GL instance associated with the canvas.
		GL gl = canvas.getGL();
 
		// Initialize GLU. We'll need it for perspective and camera setup.
		this.glu = new GLU();
 
		// Setting the clear color -- the color which will be used to erase the canvas.
		gl.glClearColor(0, 0, 0, 0);
 
		// Selecting the projection matrix.
		gl.glMatrixMode(GL.GL_PROJECTION);
 
		// Initializing the projection matrix with the identity matrix.
		gl.glLoadIdentity();
 
		// Setting the projection to be perspective.
		// Selecting the view angle to be of 38 degrees, aspect to be 1, near clip plane to be 0.1 and far clip plane to be 100. 
		glu.gluPerspective (38, 1, 0.1, 100);
 
		// Selecting the modelview matrix.
		gl.glMatrixMode(GL.GL_MODELVIEW);
 
	}
 
	public void display(GLAutoDrawable canvas)
	{
		GL gl = canvas.getGL();
 
		// Erasing the canvas -- filling it with the clear color.
		gl.glClear(GL.GL_COLOR_BUFFER_BIT);
 
		gl.glLoadIdentity();
 
		// Add your scene code here
 
		// Forcing the scene to be rendered.
		gl.glFlush();
	}
 
	public void reshape(GLAutoDrawable canvas, int left, int top, int width, int height)
	{
		GL gl = canvas.getGL();
 
		// Selecting the viewport -- the display area -- to be the entire widget.
		gl.glViewport(0, 0, width, height);
 
		// Determining the width to height ratio of the widget.
		double ratio = (double) width / (double) height;
 
		// Selecting the projection matrix.
		gl.glMatrixMode(GL.GL_PROJECTION);
 
		gl.glLoadIdentity();
 
		glu.gluPerspective (38, ratio, 0.1, 100);
 
		// Selecting the modelview matrix.
		gl.glMatrixMode(GL.GL_MODELVIEW);		
	}
 
	public void displayChanged(GLAutoDrawable canvas, boolean modeChanged, boolean deviceChanged)
	{
		return;
	}
}