Python Modern Opengl Drawing Triangle

In this Python Opengl article we are going to talk about Modern Opengl Programming in Python. also i want to show you Drawing of Triangle where we have vertex shaders and fragment shaders that runs on GPU (Graphic Process Unit).

 

 

So before this we had two articles on Python Opengl Programming

1: Introduction To Python Opengl Programming

2: Python Opengl Programming Drawing Teapot

 

 

 

 

Why Modern Opengl Programming ?

The first question I would like to answer is why there is a term called ‘modern OpenGL’. Is there ‘old OpenGL ‘ as well? Basically modern means “using programmable graphics pipeline”, old means “fixed pipeline”.

What is the difference? This is quite a broad topic, but for now I think it is good to know that fixed pipeline was like a black box. You inserted the vertex data in the front (at the input) and you got triangles written to the framebuffer (screen) at the end. You could of course tweak this box to your needs but at some point, it was not enough.

Programmable pipeline is more like a white, transparent box where not only you put input data and wait for the result, but you can insert/change internal mechanisms as well. There is a general form of the pipeline, but a lot of parts are fully controlled by you – thanks to shaders.

 

Python Modern Opengl Pipeline
Python Modern Opengl Pipeline

 

 

 

 

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 in that they’re not allowed to communicate with each other; the only communication they have is via their inputs and outputs.

 

 

 

 

What Is GLSL ?

Shaders are written in the C-like language GLSL. GLSL is tailored for use with graphics and contains useful features specifically targeted at vector and matrix manipulation.

Also Shaders always begin with a version declaration, followed by a list of input and output variables, uniforms and its main function.

Each shader’s entry point is at its main function where we process any input variables and output the results in its output variables.

Don’t worry if you don’t know what uniforms are, we’ll get to those shortly.

 

 

 

 

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

 

 

 

 

 

So at the top these are Vertex and Fragment shaders

 

 

 

These lines are for compiling the shaders

 

 

 

After compiling the shaders we need to compile the program

 

 

 

These are the triangle position

 

 

 

 

So in modern opengl programming you need create VBO (Vertex Buffer Object).

A vertex buffer object (VBO) is an OpenGL feature that provides methods for uploading vertex data (position, normal vector, color, etc.) to the video device for non-immediate-mode rendering.

 

 

 

In here we need to enable the array and set up the data

 

 

 

 

This is our render method and i have commented the code

 

 

 

 

 

 

Run the code and this will be the result for Python Modern Opengl Drawing Triangle

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
×