Welcome
Welcome to dinksoftware

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. In addition, registered members also see less advertisements. Registration is fast, simple, and absolutely free, so please, join our community today!

PyGame OpenGL

Holiverh's izHawt Python....

PyGame OpenGL

Postby Holiverh » Tue Dec 21, 2010 9:35 pm

I've been playing about a bit with multi-media lately --mostly 2D graphics. I intend to stick with 2D stuff for the foreseeable future as it's providing me with great oportunities to apply my 1337 maths skillz. However I decided to take a peek at 3D the other day, just to see what all the fuss was about.

Luckily Python has set of OpenGL bindings --PyOpenGL-- which magically (No seriously, I don't know how it works. :)) intergrates with PyGame, to provide a ready-to-roll three-dimensional enviroment.

When I say "ready-to-roll" what I really mean is an intimidating mess of undocumented arrgghh. There is no "PyOpenGL for Dummies" to guide you through the initial steps, which makes it very unappealing to those without any expirience with OpenGL. Thankfully I came across a PyGame-PyOpenGL sample which I was able to use to get the basics done (initalizing OpenGL and creating displays).

The Python binding's are --as far as I'm aware-- identical to the native OpenGL ones so I was able to use a great introductionary tutorial on GameDev.net to produce my first ever 3D... thingy!

The code doesn't reflect the tutorial as closely as my 2D game engine does, but it should still be easy to follow.
Code: Select all
#!/usr/bin/env python

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

import pygame

def pyramid():
   
   glBegin(GL_TRIANGLES)
   
   glColor3f(1.0, 0.0, 0.0)      #   // Red
   glVertex3f(0.0, 1.0, 0.0)      #   // Top Of Triangle (Front)
   glColor3f(0.0, 1.0, 0.0)      #   // Green
   glVertex3f(-1.0, -1.0, 1.0)      #      // Left Of Triangle (Front)
   glColor3f(0.0, 0.0, 1.0)      #   // Blue
   glVertex3f(1.0, -1.0, 1.0)      #   // Right Of Triangle (Front)
   
   glColor3f(1.0, 0.0, 0.0)      #   // Red
   glVertex3f(0.0, 1.0, 0.0)      #   // Top Of Triangle (Right)
   glColor3f(0.0,0.0, 1.0)         #   // Blue
   glVertex3f(1.0, -1.0, 1.0)      #   // Left Of Triangle (Right)
   glColor3f(0.0, 1.0, 0.0)      #   // Green
   glVertex3f(1.0, -1.0, -1.0)      #      // Right Of Triangle (Right)
   
   glColor3f(1.0, 0.0, 0.0)      #   // Red
   glVertex3f(0.0, 1.0, 0.0)      #   // Top Of Triangle (Back)
   glColor3f(0.0,1.0, 0.0)         #   // Green
   glVertex3f(1.0, -1.0, -1.0)      #   // Left Of Triangle (Back)
   glColor3f(0.0, 0.0, 1.0)      #   // Blue
   glVertex3f(-1.0, -1.0, -1.0)   #      // Right Of Triangle (Back)
   
   glColor3f(1.0, 0.0, 0.0)      #   // Red
   glVertex3f(0.0, 1.0, 0.0)      #   // Top Of Triangle (Left)
   glColor3f(0.0, 0.0, 1.0)      #   // Blue
   glVertex3f(-1.0, -1.0, -1.0)   #      // Left Of Triangle (Left)
   glColor3f(0.0, 1.0, 0.0)      #   // Green
   glVertex3f(-1.0, -1.0, 1.0)      #      // Right Of Triangle (Left)

   glEnd()
   
   glBegin(GL_QUADS)
   
   glColor3f(0.0, 0.0, 1.0)
   glVertex3f(-1.0, -1.0, -1.0)
   glColor3f(0.0, 1.0, 0.0)
   glVertex3f(1.0, -1.0, -1.0)
   glColor3f(0.0, 0.0, 1.0)
   glVertex3f(1.0, -1.0, 1.0)
   glColor3f(0.0, 1.0, 0.0)
   glVertex3f(-1.0, -1.0, 1.0)
   
   glEnd()
   
def cube():
   glBegin(GL_QUADS)
      
   glColor3f(0.0, 1.0, 0.0)      #   // Set The Color To Green
   glVertex3f(1.0, 1.0, -1.0)      #   // Top Right Of The Quad (Top)
   glVertex3f(-1.0, 1.0, -1.0)      #   // Top Left Of The Quad (Top)
   glVertex3f(-1.0, 1.0, 1.0)      #   // Bottom Left Of The Quad (Top)
   glVertex3f(1.0, 1.0, 1.0)      #   // Bottom Right Of The Quad (Top)
   
   glColor3f(1.0, 0.5, 0.0)      #   // Set The Color To Orange
   glVertex3f(1.0, -1.0, 1.0)      #   // Top Right Of The Quad (Bottom)
   glVertex3f(-1.0, -1.0, 1.0)      #      // Top Left Of The Quad (Bottom)
   glVertex3f(-1.0, -1.0, -1.0)   #      // Bottom Left Of The Quad (Bottom)
   glVertex3f(1.0, -1.0, -1.0)      #      // Bottom Right Of The Quad (Bottom)
   
   glColor3f(1.0, 0.0, 0.0)      #   // Set The Color To Red
   glVertex3f(1.0, 1.0, 1.0)      #   // Top Right Of The Quad (Front)
   glVertex3f(-1.0, 1.0, 1.0)      #   // Top Left Of The Quad (Front)
   glVertex3f(-1.0, -1.0, 1.0)      #      // Bottom Left Of The Quad (Front)
   glVertex3f(1.0, -1.0, 1.0)      #   // Bottom Right Of The Quad (Front)
   
   glColor3f(1.0, 1.0, 0.0)      #   // Set The Color To Red
   glVertex3f(1.0, -1.0, -1.0)      #   // Top Right Of The Quad (Back)
   glVertex3f(-1.0, -1.0, -1.0)   #   // Top Left Of The Quad (Back)
   glVertex3f(-1.0, 1.0, -1.0)      #      // Bottom Left Of The Quad (Back)
   glVertex3f(1.0, 1.0, -1.0)      #   // Bottom Right Of The Quad (Back)
   
   glColor3f(0.0, 0.0, 1.0)      #   // Set The Color To Blue
   glVertex3f(-1.0, 1.0, 1.0)      #   // Top Right Of The Quad (Left)
   glVertex3f(-1.0, 1.0, -1.0)      #   // Top Left Of The Quad (Left)
   glVertex3f(-1.0, -1.0, -1.0)   #      // Bottom Left Of The Quad (Left)
   glVertex3f(-1.0, -1.0, 1.0)      #   // Bottom Right Of The Quad (Left)
   
   glColor3f(1.0, 0.0, 1.0)      #   // Set The Color To Violet
   glVertex3f( 1.0, 1.0, -1.0)      #   // Top Right Of The Quad (Right)
   glVertex3f( 1.0, 1.0, 1.0)      #   // Top Left Of The Quad (Right)
   glVertex3f( 1.0, -1.0, 1.0)      #   // Bottom Left Of The Quad (Right)
   glVertex3f( 1.0, -1.0, -1.0)   #      // Bottom Right Of The Quad (Right)

   
   glEnd()
   

if __name__ == "__main__":
   
   from sys import exit
   
   print "Initalizing..."
   
   resolution = (400, 300)
   
   pygame.init()
   screen = pygame.display.set_mode(resolution, pygame.OPENGL|pygame.DOUBLEBUF)
   
   print "Doing GL stuff..."
   glViewport(0, 0, resolution[0], resolution[1])
   
   glMatrixMode(GL_PROJECTION)
   glLoadIdentity()
   gluPerspective(45.0, float(resolution[0]) / resolution[1], 0.1, 1000.0)
   
   glMatrixMode(GL_MODELVIEW)
   glLoadIdentity()
   
   print "More GL stuff..."
   glEnable(GL_DEPTH_TEST)
   
   glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
   
   glShadeModel(GL_SMOOTH)
   glClearColor(0.0, 0.0, 0.0, 0.0)
   
   print "Minor details..."
   clock = pygame.time.Clock()
   
   rot_tri = 0
   rot_quad = 0
   
   import random
   
   print "Mainloop..."
   while True:
      
      for event in pygame.event.get():
         
         if event.type == pygame.QUIT:
            pygame.quit()
            exit()

      glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
      
      glLoadIdentity()
      glTranslatef(-1.5, 0.0, -6.0)
      glRotatef(rot_tri, 0.0, 1.0, 0.0)
      pyramid()
      
      glLoadIdentity()
      glTranslate(1.5, 0.0, -6.0)
      glRotatef(rot_quad, 1.0, 0.0, 0.0)
      cube()
      
      rot_tri += 2.0
      rot_quad -= 1.5
      
      pygame.display.set_caption("hello_opengl.py FPS: %i" % clock.get_fps())
      
      pygame.display.flip()
      clock.tick()

For the above to work you'll need PyGame and PyOpenGL. If it works properly you should see two rotating polygons. A sqaure-based pyramid and cube. Warning: The acelleration package that is an optional extra with PyOpenGL may cause problems with certain configurations. To work around it, simply uninstall (or rename) the package in Lib/site-packages.
User avatar
Holiverh
 
Posts: 243
Joined: Sun Jul 12, 2009 10:37 am
Location: East Anglia, UK

 

Re: PyGame OpenGL

Postby Dink » Tue Dec 21, 2010 9:39 pm

Nice, I will have to try it some time (when I get around to reinstalling Python lol :p)!
User avatar
Dink
 
Posts: 531
Joined: Sat Jul 11, 2009 9:59 am
Location: Australia


Return to Python

Who is online

Users browsing this forum: No registered users and 0 guests

cron
suspicion-preferred