Introduction+to+PyOpenGL+using+Pygame

=__**PyOpenGL**__=

-What is OpenGL?
OpenGL (Open Graphics Library)

-How does this relate to Python?
PyOpenGL is an opensource raw OpenGL binding library written as a port from traditional C++ into python. This python library allows the use of OpenGL as is within python code.

-So... What do I do with it?
Using PyOpenGL, and a window handler like pygame or pyqt, you can write 2D and 3D algorithms. "But pygame already lets me do 2D stuff"; well, yes, it does. In a sense. pygame, mostly, allows the use of pre-built functions to display sprites (normally just images). Pygame also allows more advanced functions, but OpenGL lets you control all the processes of Graphics Rendering and allows you to create everything exactly the way you want to. Of course, there is also 3D, which is more complicated, but more fun.

-Example
To give you an idea of what OpenGL is capable of, here is a simple example of a rotating, wire-frame cube:

[|Cupe.py]

To run this code, you will also need to download the following python libraries from [|here] : -pygame -pyopengl

-The code
Looking through the code, you probably think something like "There is a LOT of code here, where do I start?". Well, its simpler than you think. Lets break it down into sections.

code format="python" import pygame from pygame.locals import *

from OpenGL.GL import * from OpenGL.GLU import *

import time, random, sys, os code

This section simply organises which libraries are needed to run the rest of the code. pygame gets pygame, OpenGL gets the bindings for OpenGL in python, then I also use **time**, **random**, **sys** and **os**. I will go into detail what I use where later on.

code format="python" os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%s" % (0,0) code This one line defines where the windows creates the next 'window'. When we call the pygame screen drawing, we need it to start at (0,0).

The next part of the code involves setting up parameters for OpenGL, all contained within a new function called **gl_init**.

Here we define the pygame screen with **pygame.display.set_mode**. I have defined the pygame window to use **OPENGL** to draw elements on the screen, and **NOFRAME** so that the window created has, well, no frame. As the pygame window cannot draw OpenGL in fullscreen, I have the next best thing by creating a window to the size of the monitor, starting it at (0,0) and having no frame.

I won't go into detail of all that's happening here, maybe I will on another page. The next important part I will mention is:

code format="python" glDisable(GL_CULL_FACE) code This disables OpenGLs automatic face culling. By disabling this, we can see geometric faces drawn behind the model as well ans in front of it.

Ok, now onto the main program function.

code format="python" pygame.init gl_init([1366,768]) pygame.mouse.set_visible(False) code This initialises the pygame window, then initialises the OpenGL function we looked at earlier. the numbers within the gl_init initialisation dictate the size of the window, change this to you monitor resolution if you wish. As a separate option, I disable the mouse visibility when the mouse is over the pygame screen.

The While loop I have set up is the main program loop. All rendering takes place here, and is repeated as each 'pass'.