Object Oriented Programming and OpenGL

I want to use OpenGL as the schedule of my project. But I really want to do it in a good style. How can I declare a draw () member function for each class to call it in an OpenGL mapping function?

For example, I want something like this:

class Triangle { public: void draw() { glBegin(GL_TRIANGLES); ... glEnd(); } }; 
+4
source share
4 answers

Well, it also depends on how much time you have and what it takes. However, your approach is not bad, but a little old-fashioned. Modern OpenGL uses shaders, but I donโ€™t think this is your project (school?). For this purpose and for beginners, your approach should be completely OK .

In addition to shaders, if you want to move a little further, you can also go towards the use of more general polygon objects by simply saving the vertex list and combining it with a separate Renderer class that could display polygons consisting of triangles. The code will look like this:

 renderer.draw(triangle); 

Of course, a polygon can have some additional attributes, such as color, texture, transparency, etc. In addition, you can have some more specific polygon classes such as TriangleStrip , TriangleFan , etc. Then all you have to do is write a generic draw() method in your OpenGL Renderer that can set all the states and direct the vertices to the rendering pipeline.

+2
source

When I was working on my Ph.D., I wrote a simulator that did what you wanted to do. Just remember that while your code may look object oriented, the OpenGL engine still does things in a consistent manner. Also, the consistent nature of the matrix algrebra under the hood in OpenGL is sometimes not in the order you think logically (when will I translate, when will I draw, when will I rotate, etc.?).

Remember LOGO in the old days? He had a turtle that was a pen, and you moved the turtle, and she drew lines. If the pen was down, she painted, if the pen had risen, this is not so. So I thought about my work on this program. I would start the โ€œturtleโ€ at the familiar coordinate (0, 0, 0) and use the math to translate it (move it to the center of the object I want to draw), and then call the draw () methods trying to write to draw my shape on basis of the relative coordinate system, where the "turtle" and not absolute (0, 0, 0). Then I would move, tortoise, draw, etc. Hope this helps ...

+1
source

No, that will not work. The problem is that the GLUT Display function is exactly one function. Therefore, if you want to draw a bunch of triangles, you can only register one of your draw () functions as a GLUT mapping function. (In addition, pointers to member functions in C ++ are also a complex topic.)

So, as suggested above, go to the highlighted Renderer class. This class will know about all the available objects in your application.

 class Renderer { std::list<Drawable> _objects; public: drawAllObjects() { // iterate through _objects and call the respective draw() functions } }; 

The GLUT mapping function will then be a static function that calls drawAllObjects () on the visualization object (global or not).

+1
source

Ah, good old OpenGL immediate mode. :) This procedure looks fine.

I would probably make the draw method virtual, and inherit from the base type Drawable, which defines the methods that such classes have.

0
source

All Articles