GLGraphics 0.9.4   24 comments

The version 0.9.4 of the GLGraphics library is available for download. This release is very significant because it incorporates a number of improvements and fixes suggested over the last few months by users of the library (enhanced Cg and GLSL shader support is one of such improvements), and also because this is the last release before the integration with the ongoing work I’m doing in the 3D renderer for Processing Android. For a full list of changes, fixes and issues please take a look at the release notes.

GLSL and Cg shaders

Using code from Victor Martinsshaderlib as the starting point, I revamped the API of the  GLSLShader class, and added two entirely new classes: GLCgShader and GLCgFXEffect. These two provide support for Cg shaders and the effect format CgFX (both created by NVidia). CgFX allows to define sophisticated rendering effects by encapsulating several Cg shaders together. Another interesting thing about CgFX is that there is a visual editor for it, the FX Composer. Cg and CgFX require the separate install of the Cg toolkit from NVidia.

The GLSLShader and GLCgShader classes expose a series of methods to access the parameters of the shader, which could be either uniform or attribute variables in the case of GLSL. Take a look at the following code snippets:

GLSL shader example:

void setup() {
  size(800, 600, GLConstants.GLGRAPGHICS);
  shader = new GLSLShader(this, "vertex.glsl", "fragment.glsl");

void draw() {
  shader.setFloatUniform("fog_factor", 0.1);
  shader.setVecUniform("camera_pos", 0, 100, -100);
  shader.setMatUniform("kernel", 0.3, 1.3, 0.0, -2);

  ... Geometry is drawn here ...


Cg shader example:

void setup() {
  size(800, 600, GLConstants.GLGRAPGHICS);
  shader = new GLCgShader(this, "", "");

void draw() {

  // We have to set the vertex program in order to modify its parameters:
  shader.setVecParameter("dir", 1, 1, -);
  // Passing the modelview-projection matrix from OpenGL to Cg

  // And then the fragment program...
  shader.setFloatParameter("fog_factor", 0.1);
  shader.setVecParameter("camera_pos", 0, 100, -100);

  ... Geometry is drawn here ...


Please consult the online API reference for a full specification of the methods you can use in GLSLShader, GLCgShader and GLCgFXShader to maniuplate shader’s parameters.

NVidia hosts a large library of CgFX effects, many of them looking really great. However, the use of CgFX effects from plain OpenGL/Java code seems to be tricky, as it is explained here and experienced myself. Testing of CgFX in GLGraphics has not been extensive, although the GLCgFXShader class should be able to load full blown CgFX files, which could even link to include files. Your success running CgFX effects from Processing using GLGraphics will depend on the platform, graphics hardware and version of the Cg toolkit installed.

Below you can see the output of the GLSL shader examples included in GLGraphics 0.9.4, the first one a toon shader and the second a fish eye lens effect:



Future plans (GLGraphics 1.0 and A3D)

GLGraphics evolved in a rather unplanned fashion. It started as a library only for OpenGL textures and filters, but it now includes a full Processing renderer, an offscreen drawing surface, VBO models, etc., features that many people find useful for their creative projects (take a look at a sample of visuals generated with Processing and GLGraphics: 1, 2, 3, 4, 5, 6, 7). The problem with this sort of “organic” growth is that many times the API is not entirely consistent, and sometimes is definitely clunky. The overall plan has always been to somehow fix all these issues with an ideal 1.0 release. In the meantime, however, I got involved in the development of the 3D renderer for the Android version of Processing (wiki, forum). This actually made the roadmap for GLGraphics much more clear.

The 3D renderer of Processing for Android, called A3D, is a pure OpenGL ES 1.1 renderer that solves many of the issues found in the default OPENGL renderer of Processing: a somewhat messy mixture of software and hardware rendering (in fact, most of the stages of the graphics pipeline in the OPENGL renderer such as transformation and lighting are implemented in software, while using the GPU only to blit triangles as the last step), reliance on direct rendering (i.e.: glBegin/glEnd with lots of glVertex calls in between) which is very inefficient for large geometries and has been made obsolete in the latest releases of OpenGL. GLGraphics managed to solve some of these issues, but still depends on the OPENGL renderer for most of the drawing operations.

The good news is that A3D implements the entire rendering pipeline in the GPU, using vertex arrays for fast vertex pushing. Furthermore, GLGraphics classes such as GLTexture and GLModel have been integrated as part of the android core (PTexture, PShape3D), and fonts were properly implemented for accelerated text rendering with OpenGL. In fact the 3D mode in Android Processing is already usable, and most of the OpenGL examples that come with Processing work reasonably well on Android devices:


Since during the development of A3D I fixed most of the API inconsistencies present in GLGraphics, the plan is to backport A3D into GLGraphics (removing the OpenGL ES specific features) so the latter becomes the 1.0 version. In this process, some of the advanced functionality of GLGraphics, such as shaders and physical simulations on GPU (particle systems for instance) might be split into a separate library. This means that GLGraphics 0.9.4 represents the last release with the current API. Bugfixes could make their way into eventual 0.9.4.x packages, but the next major release of GLGraphics moving towards 1.0 will take the A3D code into the PC/Mac platform.

Posted August 22, 2010 by ac in Programming

Tagged with , , , , , , , , ,

24 responses to “GLGraphics 0.9.4

Subscribe to comments with RSS.

  1. I like this.

    I have a question. Will A3D be available as a renderer on the PC such as P3D,P2D,OPENGL,JAVA2D,”hipstersinc.P5Sunflow” and GLConstants.GLGRAPGHICS ?

    I really like the part about it is a better rendition of OPENGL than the basic OPENGL that is currently in Processing.

  2. I was quite keen to discover this library! I wrote most of those CgFX/FXComposer samples @ NVIDIA (though I left there a couple of years back). I still use HLSL-based fx on a game I’m currently working on (Rift: Planes of Telara), and CgFX daily for our Maya-based artist tools (so that animators and modelers can get a good approximation of final-game rendering right in the Maya viewport). I’m a big fan of Processing so I’ll have to try out the CgFX support — and looking forward to more Processing on Android, too.

    • Hi, happy to know you like the library. Please feel free to try out CgFX effects on it. I didn’t have much time to do myself, so any feedback will be greatly appreciated :-)
      Stay tuned for more Processing on Android and A3D news!

      • Good luck so far with Cg and GLSL… What’s missing in the library, CgFX-wise, are query methods. What parameters do I have? What are their attributes? The purpose of a meta-format like CgFX (which was the proto-format that subsequently spawned HLSL-fx and COLLADA-FX) is to provide a degree of swappable-ness to shader materials and effects, so that the artist/application knows what they can meaningfully tweak (colors, texture choices, etc) versus “un-tweakable” stuff provided by the context that’s needed by the material but not part of its description (e.g., camera transforms and timers). In the current Processing implementation, a human needs to parse the FX file, find the parameters, and hard-code any needed connections. Which is less than efficient, coding-wise (There’s even a “meta-meta-format” part of the FX Composer samples, a few of them use “Script” attributes on techniques and passes to define bindings to render targets and so forth). It would be cool to provide the queries so that people could more-easily swap (at least some) materials more freely.

      • Yes, having query capabilities to the GLCgFXEffect class makes a lot of sense (similar thing actually applies for the custom xml format I’m currently using for texture filters). Do you have any specific suggestions regarding this querying API? What kind of methods would be useful to have, etc?
        In terms of future enhancements for GLGraphics in the area of shader effects, I started to pay attention to COLLADA. It seems to me having support for this meta-format would be even better than CgFX. At this point I’m still not very familiar with COLLADA, although I saw some online resources, such as this one, where it describes the use of GLSL shaders inside a collada effects file. So, one can have either GLSL or Cg shaders inside COLLADA, is this correct?
        Just as another quick question, did you succeed in running any CgFX effect from GLGraphics?

      • It might be enough simple to provide a getEffect() method that exposes the effect object. Then people who are keen to dig, for whatever their reason, can use all the methods already defined by the CgGL class.

        In your examples, btw, the lighting setup is completely unnecessary, once you replace the OpenGL fixed-function pipeline with a shader-driven one. I’ll try to send you a quick rewrite of the base Cg sample.

      • I added the getEffect() to the trunk. However, following the “Processing” API style more closely would probably require to add a bunch of new methods to remove the need of explicitly using CgGL. This is in fact the reason why all the set methods are currently there (setIntParameter, setFloatParameter, etc), since all the do is just to call the appropriate CgGL.cgSetParameter method.
        About the lighting setup in the examples, yes I did put together some of them rather quickly so I’d expect them to contain redundancies/inefficiencies like that. Go ahead and send me your improvements whenever you have the chance, and I’ll add to the distribution.

  3. I could send you a zip, but… where?

  4. hi! gl graphics is a nice feature for processing. i take a look ay examples. Glslshader work good for me but i have a problem with Cg and cgfx example. i have the cg toolkit installed(the last ver 3.0) all vambient variables are installed on my system . i receive this error:” UnsatisfiedLinkError: C:\processing\processing-1.1\libraries\opengl\library\jogl_cg.dll: Impossibile trovare la procedura specificata
    at Source)
    at Source)
    at Source)
    at Source)
    at Source)
    at Source)
    at Source)
    Exception in thread “Animation Thread” java.lang.UnsatisfiedLinkError: C:\processing\processing-1.1\libraries\opengl\library\jogl_cg.dll: Impossibile trovare la procedura specificata
    at java.lang.ClassLoader$NativeLibrary.load(Native Method)
    at java.lang.ClassLoader.loadLibrary0(
    at java.lang.ClassLoader.loadLibrary(
    at java.lang.Runtime.loadLibrary0(
    at java.lang.System.loadLibrary(
    at com.sun.opengl.impl.NativeLibLoader.loadLibraryInternal(
    at com.sun.opengl.impl.NativeLibLoader.access$000(
    at com.sun.opengl.impl.NativeLibLoader$DefaultAction.loadLibrary(
    at com.sun.opengl.impl.NativeLibLoader.loadLibrary(
    at com.sun.opengl.impl.NativeLibLoader.access$200(
    at com.sun.opengl.impl.NativeLibLoader$
    at Method)
    at com.sun.opengl.impl.NativeLibLoader.loadCgImpl(
    at codeanticode.glgraphics.GLCgShader.(
    at codeanticode.glgraphics.GLCgShader.(
    at CGshader.setup(
    at processing.core.PApplet.handleDraw(Unknown Source)
    at Source)

    anyone encountered this problem? who can help me please?
    i’m using processing version 1.1. sometimes, i noticed some libraries can work in conflict. what do you think?

    • I have the suspicion that jogl (the java binding that processing uses to access opengl) still doesn’t support cg toolkit 3, can you uninstall and install 2.2 instead? Let me know how it goes.

      • Hi ac, i have uninstalled and installed the version 2.2; but still the same problem. (the same if i try Cg_2.2_February2010 version, or Cg_2.2_October2009 version.) it’s very strange…I have also try to reinstall jogl_cg.dll but without success. maybe some libraires conflict? for example i month ago i’have a problem with the opengl libraries when i discover on the forum that the Voltron librarie go in conflict with opengl librarie i uninstall the voltron library and the problem was solved…

    • Have you update to Processing 1.2 as well?

  5. I have tried diferent version of cg 2.2, reinstalled processing and left only the necessary libraries but the problem remain. what do you suggest to do ? I would use this library for a my interactive project. I’m new to openGl and shader language,and i would like to improve my knowledge…

  6. Unfortunately I don’t have many useful suggestions at this point, since I’m not able to reproduce your error.

    I’m running the Cg examples without any problem on the three platforms (OSX, Windows and Linux), with the version 2.2 of the Cg toolkit on Linux and Windows, and 3.0 on OSX.

    As a relevant piece of information, I’m using Processing 1.2.1, maybe you could try updating to this version as Kevin suggests.

  7. I have also tried the Cg examples in ShaderLib but i get the same error. This is very strange…

    • I solved the problem. I have put all the CG Dll of cgtoolkit (cg.dll, cgGl.dll ect…..)in processing/libraries/OpenGL/library. For some reason that i don’t understand even if i setted in a right way the Ambient variables, processing doesn’t found them.

      • Ohhh.. do you have MAYA, or some other program that includes its own (older) cg.dll and cgc.exe and (iirc) CgGL.dll? If they come earlier in your system path you may have issues.

        In the specific case of maya, just replace those dll’s with newer ones from your Cg Toolkit installation (just copy the files).

  8. oh Yes,I have Maya and 3dsmax…happy to know this.Now, i can start to use the Cg language. there are other examples available or in a next relase of GLgraphics?

    • Ok, good to know you found the source of the problem. And thanks to Kevin for the tip. Regarding further Cg and CgFX examples, yes the idea is to add some more in next releases of GLGraphics.

    • Hey guys i got the same problem with the CG_Examples and can confirm the solution. I ran “depency walker” over the jogl_cg.dll and it found that 2 depencies were missing:


      In my case i took them from my MAXMSP installation and a copied them right away into the place where the jogl_cg.dll sits!

Leave a Reply

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

You are commenting using your 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 )

Connecting to %s

%d bloggers like this: