Object Oriented OpenGL

Let me start off by saying that I am not overly concerned with optimization right now (maybe I should be?), it’s more important that I get something that just works. I have spent a lot of time with 2D programming and am trying to make the leap into 3D. I am concentrating on OpenGL 3. While I’ve enjoyed making floating triangles and cubes, I think it’s time for something a little more substantial. I am right now trying to develop a 3D entity class and I am not totally sure what goes where. In a 2D setting, I am very comfortable with something like the following:

public class Entity2D {
     private float x, y;
     Sprite sprite;

     ...                    // other instance variables and methods

     public void drawMe(Graphics g) {
          sprite.draw(g, x, y);                // maybe you also translate by some world coordinates, whatever!
          ...                                        // draw other things that have to do with this entity, HP bars, armor, etc.
     }

     ...                    // more stuff , update(float delta), etc...
}

and this kind of structure makes sense to me. But now in OpenGL there is a lot more information, and I am having a difficult time deciding who (which object) owns that data. Is a Mesh object just a collection of 3d points, a VAO, a VAO and a texture, a VAO a texture and what shader program to use, etc… It doesn’t seem unreasonable to me that different entities would use the same vertex data, but a different texture for instance. Or that sometimes you would want to render a mesh with one shader program, and other times another…

So my question is, is there some sort of convention for this type of thing? My fear is that I will get the reply “it depends” and this is my biggest fear. So I’ll preemptively reply to this answer with “depends on what?” :-\

As always, I appreciate all replies. Sorry if this post sounded a little exasperated, I promise I am usually a jolly fellow. ;D

The only universal convention I’m aware of is the scenegraph, which is a hierarchical collection of Nodes, each of which has a position and orientation that is relative to the position and orientation of its parent Node. A Node might be as abstract as defining a general location on the map, a collection of other nodes that can be operated on at once, or a single drawable manipulable object in the scene. How properties such as meshes, materials, animations and so on are attached to Nodes differs for different engines.

I would recommend reading the docs (user and API) for an engine like jMonkeyEngine, Xith3d, or Ardor3d to get an idea of how different engines do it.

Thank you very much for the info. I’ll look up about scene graphs and try to make some sense out of this. :slight_smile:

Let us (me) know if you find a comparable solution for how you’re currently drawing in openGL.:}

There are at least two other implementations on top of OpenGL that abstract it away with an OO framework - Java2D itself, and Slick. And of course my own SPGL library but that’s gathering dust in some corner of the internet.

Cas :slight_smile:

Java2D uses OpenGL? Why is it so slow compared to, say, Slick?

It’s not. Unless you do something it’s not expecting. Most of the trouble with Java2D was that it was never very obvious what it was expecting, and quite often did things slowly unexpectedly. Java 7’s implementation is pretty fast, though the OpenGL implementations generally need enabling with a commandline switch, and the default Windows implementation uses D3D instead.

Cas :slight_smile:

Is the OpenGL pipeline preferable to the D3D pipleine on Windows?

In general, no. The D3D pipeline on Windows has been under longer development, and D3D drivers themselves are more reliable (on any given random OEM machine).

Cas :slight_smile:

libgdx has classes that you can use instead of direct OpenGL. Mesh, Camera, etc.

I’ll look into learning about that. Is libgdx just a Java library? Actually, as I’m typing this I realized I can look this up myself :slight_smile:

Yeah, but currently it’s working to convert into html5.

Not sure what this means. libgdx has multiple backends, eg desktop, Android, HTML5. Your same code can run with any of the backends.

LoL my bad. Yes I meant that since OP’s “Is libgdx just a Java library?” is kinda ambigue.