Python Modern Opengl Coloring Triangle

In this Python Modern Opengl programming i want to show you Coloring Triangle . so before this we had some article on Python Modern Opengl and also window creation with GLFW.

 

 

 

Check the previous articles for Python Modern Opengl

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

 

 

 

So now this is the complete code for Python Modern Opengl Coloring Triangle

 

 

 

These are the triangle vertices and also different color for the vertices

 

 

You need to convert your vertices to 32bit float using Numpy.

 

 

 

And these 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.

 

 

 

Fragment Shader

So 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.

 

 

 

In this line of code we 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):

 

 

To upload the actual data to it you first have to make it the active object by calling glBindBuffer()

GL_ARRAY_BUFFER this is a type of buffer and also there different types of buffer for right now the are not important.

 

 

Also Now it is time to copy the vertex data

 

 

  • GL_STATIC_DRAW: So the vertex data will be uploaded once and drawn many times (e.g. the world).
  • GL_DYNAMIC_DRAW: The vertex data will be created once, changed from time to time, but drawn many times more than that.
  • GL_STREAM_DRAW: The vertex data will be uploaded once and drawn once.

 

 

 

 

So run the complete code and this will be the result

Python Modern Opengl Coloring Triangle
Python Modern Opengl Coloring Triangle

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Codeloop
Share via
Copy link
Powered by Social Snap
×