Computer graphics/2013-2014/Laboratory 5

Quick links: front; laboratories agenda, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, JOGL template.


2D GRAPHICS BASICS (PART 4)

edit

Placing text in your scene

edit

In (J)OGL one can place text at certain coordinates (2D or 3D). This is easily achieved by using the GLUT API. The method (function) intended for displaying text is glutBitmapString.

Important: it should never be placed inside a glBegin/glEnd block.

Before using it we need however to specify the raster position (used to position pixel and bitmap write operations) by using the glRasterPos method (function). The object coordinates presented by glRasterPos are treated just like those given with glVertex methods (functions).

// Import the GLUT class so that we can use it.
import com.jogamp.opengl.util.gl2.GLUT;

public class MainFrame [...] {

	// GLUT object used for displaying text.
	private GLUT glut;

	public void init(GLAutoDrawable canvas)
	{
		[...]

		// Create a new GLU object.
		glut = new GLUT();

		[...]
	}

	public void display(GLAutoDrawable canvas)
	{
		[...]

		// Specify the raster position.
		gl.glRasterPos2d(0.5, 0.5);
		// Render the text in the scene.
		glut.glutBitmapString(GLUT.BITMAP_HELVETICA_10, "Hello World");

		[...]
	}
}

2D Application Example

edit

Simple Star Chart

edit

In what follows we will put together what we have seen so far and create a simple 2D application which displays a Star Chart. A Star Chart is basically a projection (using for example the Polar Projection) of constellations, stars and other stellar objects on a screen (paper).

To do this we need an API for reading and computing the coordinates of our objects (constellations, stars, names) from some data files. A simple API which does the previous can be found here.

The next code fragment shows you how you it could be used:

// Import the PolarProjectionMap class so that we can use it.
import astro.PolarProjectionMap;

public class MainFrame [...] {

	// Holds a reference to the PolarProjectionMap object.
	private PolarProjectionMap ppm = null;
	// Used to identify the display list.
	private int ppm_list;

	public void init(GLAutoDrawable canvas)
	{
		[...]

		// Initialize the object.
		this.ppm = new PolarProjectionMap(21.53, 45.17);
		// Set the separator for the line fields.
		this.ppm.setFileSep(",");
		// Read the file and compute the coordinates.
		this.ppm.initializeConstellationLines("data/conlines.dat");
		// Initialize here the rest of the elements from the remaining files using the corresponding methods.

		// Create the display list.
		this.ppm_list = gl.glGenLists(1);
		gl.glNewList(this.ppm_list, GL.GL_COMPILE);
			this.makePPM(gl);
		gl.glEndList();
		[...]
	}

	public void display(GLAutoDrawable canvas)
	{
		[...]

		gl.glCallList(this.ppm_list);

		[...]
	}

	// We use this method for creating the display list.
	private void makePPM(GL2 gl) {
		final ArrayList<PolarProjectionMap.ConstellationLine> clLines = this.ppm.getConLines();
		// Add here the rest of the ArrayLists.
		
		gl.glColor3f(0.0f, 1.0f, 0.0f);

		gl.glBegin(GL2.GL_LINES);
		for (PolarProjectionMap.ConstellationLine cl : clLines) {
			if (cl.isVisible()) {
				gl.glVertex2d(cl.getPosX1(), cl.getPosY1());
				gl.glVertex2d(cl.getPosX2(), cl.getPosY2());
			}
		}
		gl.glEnd();
		
		// Add here the rest of the code for rendering constellation boundaries (use GL_LINES), 
		// names (use glutBitmapString), stars (use GL_POINTS) and cardinal points (use glutBitmapString).
	}
}

Links: