Reputation: 320
I am following this guide http://open.gl/ to learn how to program using modern opengl. I am stuck at one thing. Earlier with older opengl Api, I used to create different functions for drawing different types of shapes. Like drawRect(), drawCircle(), etc. All I needed to do was to use glVertex3f() in different combinations inside glBegin() and glEnd().
But in OpenGl3.2+, even to draw a rectangle, we have to write hell lot of code. I understand that it gives you more control over rendering, but I am confused at what to do when you are rendering multiple stuff like drawing rectangles, circles, etc together. Do I need to write all that code multiple times including shaders or I can reuse the code.
I have implemented this code http://open.gl/content/code/c2_triangle_elements.txt This example draws a rectangle, but what If I wanted to draw a triangle along with this rectangle, then how do I do it.
Upvotes: 1
Views: 1470
Reputation: 16774
You can reuse most of the code. All the shaders can be loaded once and the reused by calling glUseProgram
. As for all the buffers you can create them once and then reuse them as many times as you want.
You might want to create some classes like ShaderClass
and ShapeClass
.
A base shader class will usually have 2 or 3 parameters like program
, vertexShader
and fragmentShader
(all being some IDs). When you extend them also add all the uniforms and attributes from the shaders so you can later use them to modify your drawing (change the colour for instance).
A ShapeClass
will usually contain vertex buffer ID (and optional index buffer ID) and number of vertices to draw.
So in the end when wanting do draw some shape you only use a shader you already created and compiled, bind and set the vertex data from your shape buffer and call the draw.
Upvotes: 2