1

Say I wanted to draw just a simple OpenGL triangle. I know that I can draw a triangle in the main file, where all my OpenGL stuff is setup by doing something along the lines of:

glBegin( GL_TRIANGLES );
    glVertex3f( 0.0f, 1.0f, 0.0f );
    glVertex3f( -1.0f,-1.0f, 0.0f );
    glVertex3f( 1.0f,-1.0f, 0.0f);
glEnd();

But instead of having all that clutter in my main file, I would like to draw a triangle instead by using a class named "Triangle" with a "Draw" function, so my code would look something like this:

Triangle TheTriangle;
TheTriangle.draw();

In short, how can I make a class with some OpenGL shapes that can be drawed by using a function?

genpfault
  • 51,148
  • 11
  • 85
  • 139
August
  • 12,410
  • 3
  • 35
  • 51

4 Answers4

4

Usual way is as follows:

TriangleArray tri;
tri.push_back(...);
tri.prepare();
while(1) {
  clear();
  tri.draw();
  swapbuffers();
}

But usually the same class should handle array of objects, not just one object. So TriangleArray is good class name. prepare() is for setting up textures or vertex arrays. (Note: if your world is built from cubes, you'll create CubeArray instead.)

tp1
  • 1,197
  • 10
  • 17
  • Thank you! I'm still a little unclear about how to implement that... What exactly do I push back into the array? – August Jul 13 '13 at 02:02
  • API definition is largely up to you. If I were doing this, as a first iteration, I'd have a push_back() that takes three 3-tuples or Vector3D objects (or 9 real numbers >_>), and appends the coordinates to a memory buffer that I'd eventually upload to the GPU using glBindBuffer, glBufferData, etc. Read up on vertex buffers - they're much faster than defining individual vertices with glVertex3f() etc. – atomicinf Jul 13 '13 at 02:24
2

Like a few have said, OpenGL doesn't go well with object oriented programming, but that doesn't mean it can't be done. To be a little more theoretical, simply put, you could have a container of "Meshes" in which every frame you loop through and render each to the screen. The Render class could be thought of as a manager of the states, and the container of the various scene modules. In reality, most systems are much more complex than this and implement structures such as the scene graph.

To get started, try creating a mesh class and an object class (that maybe points to a mesh to be drawn) Add functionality to add and remove objects from a container. Every frame, loop through it and render each triangle(or whatever else you want) and there you have a very simple OO architecture. This would be a way to get you started.

Its very normal to find it odd to wrap very functional architecture with OOP but you do get used to it, and if done correctly, it can make your code much more maintainable and scalable. Having said that, the example I gave was quite simple, so here is an architecture that you may want to explore once you have that down.

The following link gives some useful info on what exactly a scene graph is: Chapter 6 covers the scene graph

Its a very powerful architecture that will allow you to partition and order your scenes in a very complex and efficient (if you take advantage of the benefits) manner. There are many other techniques but I find this one to be the overall most powerful for game dev. It can totally depend on what type of application you are seeking to create. Having said all this though, I would not advise making a FULLY object oriented renderer. Depending on your application, an OO scene graph could be enough. Anyways, Good Luck!

Joseph Pla
  • 1,600
  • 1
  • 10
  • 21
0

You can just put the OpenGL code in the Triangle::draw() function:

void Triangle::draw() {
    glBegin( GL_TRIANGLES );
        glVertex3f( 0.0f, 1.0f, 0.0f );
        glVertex3f( -1.0f,-1.0f, 0.0f );
        glVertex3f( 1.0f,-1.0f, 0.0f);
    glEnd();
}

Of course, this assumes that you have correctly declared the draw() method in the Triangle class and that you have initialized the OpenGL enviornment.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

OpenGL doesn't really map to well into OOP paradigms. It's perfectly possible to implement a object oriented rendering system, but the OpenGL API and many of its lower level concepts are very hard, to impossible to cast into classes.

See this answer to a similar question for details: https://stackoverflow.com/a/12091766/524368

Community
  • 1
  • 1
datenwolf
  • 159,371
  • 13
  • 185
  • 298