my openGL API need threads!!! But how to ,ake it work???
Laurent
l.exsteens at gmail.com
Thu Feb 2 09:16:59 EST 2006
Here are my codes:
it doesn't use threading...!
# ------------------------------------------------------------
# test_pyoogl.py
# ------------------------------------------------------------
#!/usr/bin/env python
from pyoogl import *
import unittest
class test(unittest.TestCase):
def testWindow(self):
global Scene
print 'Test 1:'
print '-------'
print 'This test simply verify if a GL window can be created.'
print
Scene = ooglScene()
Scene.run()
print 'Test 2:'
print '-------'
print 'Verify if a Point can ben drawed, and then if the
ooglBaseObject inheritance scheme is in order.'
print
Scene.append(ooglPoint())
if __name__ == '__main__':
Scene = None
unittest.main()
# ------------------------------------------------------------
# pyoogl.py
# ------------------------------------------------------------
#!/usr/bin/env python
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import math
import sys
import threading
#-------------------------------------------------------------------------------
class ooglBaseObject(object):
def __init__(self):
pass
#-------------------------------------------------------------------------------
class ooglObject(ooglBaseObject, list):
"""
An ooglObject represent a object in a OpenGL scene.
This is based on a Composite Pattern: an ooglObject is a list of
ooglObjects, and an ooglObject can ben specialised in ooglPoint,
ooglPolygon, ...
"""
def __init__(self):
ooglBaseObject.__init__(self)
list.__init__(self)
def ooglDraw(self):
for obj in self:
obj.ooglDraw()
#-------------------------------------------------------------------------------
class ooglPoint(ooglBaseObject):
"""
An ooglPoint is the simpliest stuff you can creat into your scene.
It inherits directly from ooglObject.
"""
def __init__(self, **kwargs):
ooglBaseObject.__init__(self)
if 'x' in kwargs:
self.__x = kargs['x']
else:
self.__x = 0
if 'y' in kwargs:
self.__y = kargs['y']
else:
self.__y = 0
if 'z' in kwargs:
self.__z = kargs['z']
else:
self.__z = 0
def ooglSend(self):
glVertex3f(self.__x, self.__y, self.__z)
def ooglDraw(self):
glBegin(GL_POINTS)
self.ooglSend()
glEnd()
glFlush()
#-------------------------------------------------------------------------------
class ooglScene(list):
"""
An ooglScene describe all that is needed to represent a OpenGL scene.
Fundamentaly, this is a list of ooglObjects.
"""
def __init__(self):
list.__init__(self)
# GLUT variables:
self.__argv = sys.argv
self.__DisplayMode = GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE
self.__WindowPosition = {'x': 200, 'y': 200}
self.__WindowSize = {'height': 250, 'width': 250}
self.__WindowName = 'Untitled'
self.__ClearColor = {'red': 0, 'green': 0, 'blue': 0, 'alpha': 0}
# GL variables:
self.__PointSize = 2
self.__Enable = GL_DEPTH_TEST
# Callback functions:
self.__DisplayFunc = self.display
self.__KeyboardFunc = self.keyboard
# Display variables:
self.__Clear = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
self.__DefaultColor = {'red': 1, 'green': 1, 'blue': 1}
def run(self):
"""
Make the GL, GLUT initialisations & launch the GLUT main loop.
"""
# GLUT initialisation + window creation:
glutInit(self.__argv)
glutInitDisplayMode(self.__DisplayMode)
glutInitWindowPosition(self.__WindowPosition['x'],
self.__WindowPosition['y'])
glutInitWindowSize(self.__WindowSize['height'],
self.__WindowSize['width'])
glutCreateWindow(self.__WindowName)
# GL initialisation:
glClearColor(self.__ClearColor['red'], self.__ClearColor['green'],
self.__ClearColor['blue'], self.__ClearColor['alpha'])
glPointSize(self.__PointSize)
glEnable(self.__Enable)
# setting callback functions:
glutDisplayFunc(self.__DisplayFunc)
glutKeyboardFunc(self.__KeyboardFunc)
# GLUT main loop:
glutMainLoop()
sys.exit(0)
def display(self):
"""
The default for glutDisplayFunc.
"""
glClear(self.__Clear)
glColor3f(self.__DefaultColor['red'], self.__DefaultColor['green'],
self.__DefaultColor['blue'])
for obj in self:
obj.ooglDraw()
glFlush()
glutSwapBuffers()
def keyboard(self, key, x, y):
"""
The default for glutKeyboardFunc.
"""
if key == 'q':
sys.exit(1)
More information about the Python-list
mailing list