Python Modern Opengl Triangle With GLFW

In this Python Modern Opengl programming i want to show you creating Triangle with GLFW.

so before this we had some articles on Modern Opengl and also window creation with GLFW.

 

 

Check the previous articles on 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

 

 

 

So now this the complete code for Python Modern Opengl Triangle With GLFW

 

So in the above code iam not going to explain the window creation process in GLFW because i have already an

article about that you can check the above links.

 

 

These are the triangle vertices

 

 

 

And these the vertex and fragment shaders

 

 

 

 

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.

 

 

 

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 code and this will be the result

Python Modern Opengl Triangle With GLFW
Python Modern Opengl Triangle With GLFW

 

 

1 thought on “Python Modern Opengl Triangle With GLFW”

Comments are closed.

Share via
Copy link
Powered by Social Snap