rbandrews: (Lambda)
[personal profile] rbandrews
Hard drive manufacturers rate their drives on "MTBF". The best drives have the highest MTBF, which is mean time before failure. 3D graphics programming tutorials should be rated on MTBT, or mean time before triangle, which is a term I just made up meaning "how much stuff do I have to do before I render my first triangle on the screen".

I will now attempt to write the OpenGL tutorial with the lowest MTBT:

OpenGL is a library, not a language. It's written in native code for each platform, but you can call it from Java through JOGL, which is what we're going to do in this tutorial so we don't have to worry about linker errors.

First, install OpenGL. You probably don't have to do this. If you have a Mac, then you (as usual) don't have to even worry about it. If you have Windows, then you probably have some game that needed it already.

Second, go download the JOGL JARs for your platform here. Go download the skeleton JOGL program here. Compile the program and run it, being sure to add the JAR files to the classpath (either through a command line parameter or with your IDE).

A little bit about how OpenGL works: you have a "rendering context" that is basically an AWT widget. It will occasionally call a function to render itself, which is where your code comes in. Forget everything you ever learned about clipping regions or buffering, your code will draw everything all over again each time and it won't even matter because it all runs so fast you don't have to care.

By the time you write anything in OpenGL that will run slow, you will know how to fix it.

The code you're going to change to make pretty shapes is the display function. You get passed a GLAutoDrawable, which you can call getGL() on to get a GL object. This is Java's interface to what C programmers know as the huge pile of global functions pattern. This has methods to draw things.

To draw things, first call gl.glBegin(GL.GL_TRIANGLES) to say "I'm going to start giving you points, and these points are parts of triangles". You then give as many points as you want, and each set of three consecutive points is drawn on the screen as a triangle.

Example:

gl.glBegin(GL.GL_TRIANGLES);
gl.glVertex3f(-1,0,0);
gl.glVertex3f(0,1,0);
gl.glVertex3f(1,0,0);
gl.glEnd();


The coordinate system is simple. (0,0,0) is the very center of the screen. The y axis is vertical (positive is up) and the x axis horizontal (positive is right). The z axis points into the screen, with the negative part going into it and the positive part coming out. The z=0 point is right at the screen, so anything drawn there won't appear (it's the same depth as your eye). All coordinates are floats. The minimum and maximum coordinates on each axis depend on how the scene is transformed.

There are five important functions for transforming the scene. The scene's current transformation (rotation, translation, scaling, whatever) is stored in a transformation matrix, which is complex and mathematical. You can load one that says "put (0,0,0) in the very center of the screen" by saying gl.glLoadIdentity().

The matrices are stored in a stack, and you can add and remove transformations with gl.glPushMatrix() and gl.glPopMatrix(). This allows you to render stuff at a particular place in a function, and then restore the old settings afterward.

To actually change the settings, gl.glTranslated(x,y,z) moves everything drawn by that amount, and gl.glRotatef(degrees,x,y,z) rotates everything drawn by that angle, around that vector.

Best way to learn that stuff is to play around with it.

gl.glColor3f(red,green,blue) sets the color of a vertex. In the skeleton program, the triangles' color is determined by blending the three corners. Again, better to see it.

The way to interact with your program is the normal AWT way: a keyboard listener. Edit keyPressed to make it do things, like move the triangle around in response to arrow keys.

At this point, you can draw triangles on the screen and handle input, which is enough to allow you to teach yourself more. Good places to start are with the GLUT functions (simple ways to draw spheres and the teapot and such) and with gluLookAt, which lets you move the "camera" around the scene.

Have fun!
This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

Profile

rbandrews: (Default)
rbandrews

July 2024

S M T W T F S
 123456
78910111213
14151617181920
212223242526 27
28293031   

Style Credit

Page generated Jul. 12th, 2025 01:02 pm
Powered by Dreamwidth Studios

Expand Cut Tags

No cut tags