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.
…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)
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.
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.