New GLGraphics introduces model and camera objects   7 comments

Release 0.9 of GLGraphics (which is available for download at sourceforge) finally implements a few new features that I wanted to add to the library for a long time:

  • a GLModel class to store 3D objects (including normals, color and texture information).
  • a GLCamera class for OpenGL-accelerated viewport transformations.
  • fast texture loading in the GLTexture class.

These additions complete the list of features I planned originally for the 1.0 release of the library. Although they are not finished at this point, the idea is to test, refine and debug them as I move closer to the 1.0 release of GLGraphics.

GLModel and GLCamera are inspired by two pre-existing libraries, OBJloader and OCD (Obsessive Camera Direction). Future refinements will probably include model loading from disk (GLModel) and different methods for camera positioning (GLCamera).

GLModel was also introduced with the goal of combining texture filtering (handled by the pre-existing GLTextureFilter class) with computation and modification of vertex coordinates.  This is achieved by writing the result of a texture filtering operation to the vertices of a GLModel. This functionality opens up many interesting applications, such as using image information to distort a 3D mesh or computing the motion of very large models with GPU. The GLTextureFilter.apply() method can be called now with a destination GLModel as a parameter:

void setup()
{
  size(640, 480, GLConstants.GLGRAPHICS);
  cam = new GLCamera(this);
  srcTex = new GLTexture(this, "milan_rubbish.jpg");
  ...

  // Creating the model, which will have as many vertices as pixels in the textures:
  int numPoints = srcTex.width * srcTex.height;
  destModel = new GLModel(this, numPoints, QUADS, GLModel.STREAM);
  ...
}

void draw()
{
  // The output of applying the filter to srcTex is written to destTex (as a texture)
  // and to destModel (as a 3D object):
  texFilter.apply(srcTex, destTex, destModel);

  ...

  // GLModel needs a GLCamera to be displayed:
  cam.beginDraw();
     destModel.render();
  cam.endDraw();
}

This sample code is included in the example RenderToModel that comes with the library.

The fast texture loading method should be much faster than the regular GLTexture.loadTexture() that copies the pixels array in the CPU to the texture in GPU memory. It will also allow to read and write pixels directly from and to the texture, without having to download the image to the pixels array. The two methods that were implemented so far are GLTexture.enableFastTextureLoad() and tex.loadTextureFast(), however this feature is not functional, at least on my test computer.

From the implementation point of view, the additions just described rely on a number of OpenGL advanced features, namely Vertex Buffer Objects (VBO) and Pixel Buffer Objects (PBO), particularly for the render to model mechanism. These objects will become the default method to handle vertex and pixel data as the so-called “direct mode” (sending vertex coordinates at each frame with glBegin()/glVertex3f()/glEnd()) is being deprecated with the recent introduction of OpenGL 3.0 and 3.1.

Posted May 28, 2009 by ac in Programming

Tagged with , , , , , ,

7 responses to “New GLGraphics introduces model and camera objects

Subscribe to comments with RSS.

  1. Looks like a great development of an already very useful library. Thanks for spending your time developing it.

    I downloaded the new version and tried to run the “RenderToModel” example, but it simply shows a black screen. When I disable the GLCamera code it renders, but the 3D model is not shown. I tried adding some other draw commands, but those shapes aren’t shown either.

    • What happens if you set the color of the vertices to white (and no transparency):

      float[] colors;
      colors = new float[4 * numPoints];
      for (int i = 0; i < numPoints; i++)
      {
      for (int j = 0; j < 3; j++) colors[4 * i + j] = 1.0;
      colors[4 * i + 3] = 1.0;
      }

      Is the output still black?

  2. Ok, now I’m getting some glitchy white polygon streaks. Is that what you had in mind? I took a screenshot for reference:

  3. Yes, that’s what it should do. Setting different alpha values can make it look a little bit more interesting. Anyways, I have to come up with better examples for this technique. An interesting one could be to use the brightness values of the pixels from a movie to set the Z coordinates of the model.

  4. This is awesome. Thanks very much for building and sharing this!

  5. Pingback: Live visuals with GLGraphics and GSVideo « codeanticode

Leave a comment