Python Modern Opengl Drawing Rectangle

In this Python Modern Opengl Programming iam going to show you Drawing Rectangle. also iam going to talk about EBO (Element Buffer Object ) in Opengl. so in the previous articles we have learned about Opengl alot , but we have not talked about EBO.

 

 

 

Before starting our main topic about EBO and drawing Rectangle in Opengl, you can check the previous articles.

1:  Python Opengl Introduction And Creating Window

2: Python Opengl Drawing Teapot

3: Python Modern Opengl Drawing Rectangle

4: Python Modern Opengl GLFW Window

5: Python Modern Opengl Triangle With GLFW Window

6: Python Modern Opengl Coloring Triangle

 

 

So first of all let me write the complete code for Python Modern Opengl Drawing Rectangle

 

So if you have followed my previous articles on Python Modern Opengl Programming, most of these codes will be familiar to you. but i will explain again some of them.

 

 

These are the rectangle vertices and also different color for the vertices, also we have converted the values to 32bit value.

 

 

 

 

Because we are working with EBO (Element Buffer Object) we need to create indices for our Rectangle, also we have converted that to 32bit int.

 

 

 

 

These are the vertex and fragment shaders

 

 

 

So in the vertex shader at the top we have the version of shader, after that we have two input values for our position and color.

also we have an output value for our color that we will use that in the fragment shader.
OK now in the fragment shader we have the same shader version and we are going to take the input value of new color in our fragment shader.

 

 

 

What Are Shaders ?

Shaders are little programs that rest on the GPU. These programs are run for each specific section of the graphics pipeline. So In a basic sense, shaders are nothing more than programs transforming inputs to outputs. Shaders are also very isolated programs

Vertex Shader

The vertex shader is a program on the graphics card that processes each vertex and its attributes as they appear in the vertex array. Its duty is to output the final vertex position in device coordinates and to output any data the fragment shader requires. That’s why the 3D transformation should take place here. The fragment shader depends on attributes like the color and texture coordinates, which will usually be passed from input to output without any calculations.

Remember that our vertex position is already specified as device coordinates and no other attributes exist, so the vertex shader will be fairly bare bones.

 

 

 

Fragment Shader 

the output from the vertex shader is interpolated over all the pixels on the screen covered by a primitive. These pixels are called fragments and this is what the fragment shader operates on. Just like the vertex shader it has one mandatory output, the final color of a fragment. It’s up to you to write the code for computing this color from vertex colors, texture coordinates and any other data coming from the vertex shader.

 

 

 

 

compile the program and shaders

 

The next step is to upload this vertex data to the graphics card. This is important because the memory on your graphics card is much faster and you won’t have to send the data again every time your scene needs to be rendered (about 60 times per second).

 

 

 

This is done by creating a Vertex Buffer Object (VBO):

 

 

 

 

Now it is time to create EBO

 

 

 

 

What is EBO ?

To explain how element buffer objects work it’s best to give an example: suppose we want to draw a rectangle instead of a triangle.

We can draw a rectangle using two triangles (OpenGL mainly works with triangles). This will generate the following set of vertices:

 

As you can see, there is some overlap on the vertices specified. We specify bottom right and top left twice! This is an overhead of 50% since the same rectangle could also be specified with only 4 vertices, instead of 6. This will only get worse as soon as we have more complex models that have over 1000s of triangles where there will be large chunks that overlap.

What would be a better solution is to store only the unique vertices and then specify the order at which we want to draw these vertices in.

In that case we would only have to store 4 vertices for the rectangle, and then just specify at which order we’d like to draw them.

Wouldn’t it be great if OpenGL provided us with a feature like that?

Thankfully, element buffer objects work exactly like that. An EBO is a buffer, just like a vertex buffer object, that stores indices that OpenGL uses to decide what vertices to draw. This so called indexed drawing is exactly the solution to our problem. To get started we first have to specify the (unique) vertices and the indices to draw them as a rectangle:

 

 

 

 

 

 

So run the code and this will be the result

Python Modern Opengl Drawing Rectangle
Python Modern Opengl Drawing Rectangle

Subscribe and Get Free Video Courses & Articles in your Email

 

Codeloop
Share via
Copy link
Powered by Social Snap
×