jogl 2d newbie

Hi, I’m very new to gl and jogl (and debated posting this here or newless cluebies). I want to use jogl for my 2D game so it will run well on more than just Windows. But in my research I’ve come up with some questions…

I’m a little stumped as to where the GL class resides? I have downloaded the jogl source out of cvs. There’s no GL.java in jogl, and searching for things like “class GL” or “interface GL” turns up nothing. I assume all gl* functions from the standard C implementation of openGL would be found in the GL class? Searching for things like glBegin show a lot of other classes using the method, but not where it’s originating from.

What about glDrawPixels, glBitmap, etc? I also was unable to find those as well. I am curious how much of the 2D side of openGl is implemented, or if I’m even on the right track at all.

In doing some small toy programs with jogl, I can’t get the color to change no matter what I do. For example I stripped the Gears demo down and replaced drawing the gears with just drawing a few polys, I have something like:


public void display(GLDrawable drawable)
{
       // glClearColor set to black
      gl.glclear( GL.GL_COLOR_BUFFER_BIT );
      
      gl.glColor3f( 0.0f, 1.0f, 0.0f );
      gl.glBegin( GL.GL_POLYGON );
          gl.glVertex2f( 0.0f, 0.0f );
          gl.glVertex2f( 1.0f, 0.0f );
          gl.glVertex2f( 0.0f, 1.0f );
      gl.glEnd();
      gl.glFlush();
}

but no matter what the triangle is always a dark red. Weird that’s dark red, I’d think if anything it’d be (1.0, 0.0, 0.0) red, I’ve experimented with specifying the color at other times and such with no luck.

My game currently has a render class that all sprites register themselves with. When it comes time to draw the frame the renderer’s Graphic object is passed around to everyone and they all do their thing. Is this a feasable approach with jogl? Obviously not a Graphics object, but perhaps a small inner class of my renderer that accepts an image and coordinates and draws it to the GLCanvas with I assume glDrawPixels or something like it?

I’m also curious on the whole pasting sprites onto polygons as textures vs just blitting the sprites as is. In searching (not just these forums but the internet for openGL stuff in general) I’ve found it split about 50/50. It seems that generally the more game oriented people seem to prefer the polygon/texture route. Should that be the way I head? I have a feeling that’d drastically change my game’s structure.

I think the source to that class is generated by the build process from the openGL C header files. That’s what all the fuss is about with respect to GlueGen and antlr

Yep… it is automagically created by GLEmitter.java.

Instead of compiling Jogl for yourself, you can also download the binary archive for the current supported platforms from http://jogl.dev.java.net/servlets/ProjectDocumentList

The flush()-command isn’t necessary AFAIK. According to the user’s manual http://jogl.dev.java.net/unbranded-source/browse/checkout/jogl/doc/userguide/index.html it’s advisable to set the gl and glu variables every time in the reshape and display event-handler methods.


GL  gl  = drawable.getGL();
GLU glu = drawable.getGLU();

But this shouldn’t be the problem at your side, where the triangle is dark red. :slight_smile:

Well, I’ve to set the camera back a bit in order to see your triangle. So between your clClear and the glColor I’ve to do:


o.glLoadIdentity();
o.glTranslated(0, 0, -10);

Then the triangle is green, as you specified it.
Still, this doesn’t explain why you’re triangle is dark red. :slight_smile: Do you issue some exotic commands in the initial init() method of your render class?

[quote]My game currently has a render class that all sprites register themselves with. When it comes time to draw the frame the renderer’s Graphic object is passed around to everyone and they all do their thing. Is this a feasable approach with jogl? Obviously not a Graphics object, but perhaps a small inner class of my renderer that accepts an image and coordinates and draws it to the GLCanvas with I assume glDrawPixels or something like it?
[/quote]
I’m not sure if I understood you correctly, but in OpenGL - and also Jogl - all drawing happens in the display-method of your render class which implements GLEventListener. There you can of course fetch all your registered sprite classes and call their render method (with parameters “GL gl” and “GLU glu”) which then just issue the appropriate glCommands.
Or did you mean it that way?

[quote]I’m also curious on the whole pasting sprites onto polygons as textures vs just blitting the sprites as is. In searching (not just these forums but the internet for openGL stuff in general) I’ve found it split about 50/50. It seems that generally the more game oriented people seem to prefer the polygon/texture route. Should that be the way I head? I have a feeling that’d drastically change my game’s structure.
[/quote]
Unfortunately I can’t judge this from experience. However I read that today’s 3d cards are really optimized for texture polygons so they’re not slower than glDrawpixel, probable even faster - especially when you glBindTexture so that the texture bitmaps resides on the graphics card’s fast V-RAM.
Further I’d use the textured polygon way because in case you want to change the camera during the game - and if it’s just to zoom in and out, or rotate around the z axis (or any other) from time to time. So it could look as cool as the funny Spactripper game does ( http://www.pompom.org.uk/ )

:slight_smile:

Regarding your colors, are you doing flat shading instead of smooth shading?

The code below draws a tri-colored triangle and a single solid colored square.


public void display(GLDrawable gLDrawable)
{
  final GL gl = gLDrawable.getGL();
  gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
  gl.glLoadIdentity();
  gl.glTranslatef(-1.5f, 0.0f, -6.0f);
  gl.glBegin(GL.GL_TRIANGLES);                                    // Drawing Using Triangles
    gl.glColor3f(1.0f, 0.0f, 0.0f);
    gl.glVertex3f( 0.0f, 1.0f, 0.0f);            // Top
    gl.glColor3f(0.0f, 1.0f, 0.0f);
    gl.glVertex3f(-1.0f,-1.0f, 0.0f);            // Bottom Left
    gl.glColor3f(0.0f, 0.0f, 1.0f);
    gl.glVertex3f( 1.0f,-1.0f, 0.0f);            // Bottom Right
  gl.glEnd();                                          // Finished Drawing The Triangle
  gl.glTranslatef(3.0f, 0.0f, 0.0f);
  gl.glBegin(GL.GL_QUADS);              // Draw A Quad
    gl.glVertex3f(-1.0f, 1.0f, 0.0f);            // Top Left
    gl.glVertex3f( 1.0f, 1.0f, 0.0f);            // Top Right
    gl.glVertex3f( 1.0f,-1.0f, 0.0f);            // Bottom Right
    gl.glVertex3f(-1.0f,-1.0f, 0.0f);            // Bottom Left
  gl.glEnd();                                          // Done Drawing The Quad
  gl.glFlush();
}

public void init(GLDrawable gLDrawable)
{
  final GL gl = gLDrawable.getGL();
  gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  gl.glShadeModel(GL.GL_SMOOTH);
}

Turns out the color thing was a driver problem. All JOGL apps drew in a dark red on both my laptop (win2k) and desktop (winXP). No matter what I did or what app it was. The gears demo had dark red gears and an example I ported out of the OpenGL red book verbatim was all red too. I should run some standard C OpenGL code and see what it does.

I then installed the nvidia drivers and everything’s ok now. I guess the OpenGL dll’s that ship with Windows aren’t very good. I just hope my dinkey laptop video card has some drivers I can install.

I originally wanted to look at the GL class to see if there are any differences I should watch out for, but so far it’s dead on, so I’ll stop worrying about it.

I read up more on textures vs bitmaps and it seems textures are better, but I’ll drop that as it’s not really a JOGL thing.

Thanks for all the help guys. Both OpenGL and JOGL have turned out to be quite nice so far.

[quote]It seems that generally the more game oriented people seem to prefer the polygon/texture route. Should that be the way I head?
[/quote]
I expect most here would concur with that statement. Graphics cards these days are designed primarily to draw textured triangles very, very fast. And they’re getting faster! That consideration alone should sway you. :wink:

Textures by far are the best way to go - they fit right into the optimal OpenGL pipeline and should always yield the best performance.