Draw a square

Let’s draw a shape on our newly created Display. Most of the basic OpenGL commands in LWJGL are contained in the GL11 class. They are all static methods due to the procedural nature of the specification. To create points/lines/shapes we specify a number of vertices wrapped in a context.

Vertices

…or points. Whatever you want to call them. These are simply a location in 3-dimensional space specified by 3 co-ordinates (the familiar x, y and z). Actually OpenGL handles vertices using 4 co-ordinates per point, but more about that later…

A vertex can be recorded using floats or doubles as the coordinates:

glVertex3f(float x, float y, float z)
glVertex3d(double x, double y, double z)

Contexts

There are many different contexts to choose from (GL_POINTS, GL_LINES, GL_LINE_LOOP to name a few). The context describes how to associate the vertices that are specified. For example, GL_LINES connects the points with lines, GL_TRIANGLE connects them in triangles etc. The context is specified like so:

glBegin(GL_LINE_LOOP);
		glVertex3f(-0.5f, -0.5f, 0f);
		glVertex3f(-0.5f, 0.5f, 0f);
		glVertex3f(0.5f, 0.5f, 0f);
		glVertex3f(0.5f, -0.5f, 0f);
glEnd();

The specific rules about each context is contained in any good OpenGL reference. A vertex call is meaningless without a context — the vertex cannot be stored and used later so it must always appear along with the information about how it is to be used.

We now have the prerequisite tools to create a square on screen. The code for this example is hosted here.

It's not a square!
It's not a square!

Simple! You probably have a lot of questions now:

1) Why has OpenGL rendered a rectangle instead of a square?
2) How did I know which coordinates to use so that the rectangle appears on the screen?

These questions will be answered in my next post.

Advertisements

3 thoughts on “Draw a square

  1. Curiously, I am unable to close either this or the first program by clicking the close box. The rectangle displays just fine. Only on the second mouse-click, Windows jumps in with a error: “This program is not responding”. I am asking about this here because I simply cut-and-paste the code from the example page into Eclipse. Any thoughts if there is some sort of configuration issue I might have overlooked?

    I tried putting a System.out.println() messages in the while loop and outside it. The code never breaks free of the while loop. And yes, here is the while section:

    while(!Display.isCloseRequested())
    {
    Thread.sleep(100);
    }
    Display.destroy();

    BTW, thank you very much for making these tutorial examples available! It is nice to be able to jump right in like this.

    1. In the following example, the Display.isCloseRequested() function works.
      http://www.lwjgl.org/wiki/index.php?title=LWJGL_Basics_1_%28The_Display%29

      The key difference I assume is that the Display is being run in its own thread rather than as part of the main() method. Unclear to me why this makes a difference. I do know that it is pretty much standard with Swing examples to launch a thread from main using EventQueue.invokeLater(new Runnable…

      Perhaps for Java 7 launching a thread has become mandatory for this code to work?

      1. Nope. Wrong. The key difference is the contents of the while loop. Instead of a Thread.sleep() command, (or in addition to), put in a Display.update(); Then it all works. The previous note about using a Runnable thread proved to be irrelevant. I think what might be happening is that the mouse is not polled until the update() method executes perhaps. But I am a total beginner so…could be wrong, again.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s