99 lines
2.7 KiB
Python
99 lines
2.7 KiB
Python
|
|
import gtk
|
|
import gtk.gtkgl
|
|
|
|
from OpenGL.GL import *
|
|
|
|
class SketchWidget:
|
|
def __init__(self, sketch):
|
|
self.sketch = sketch
|
|
try:
|
|
# try double-buffered
|
|
self.glconfig = gtk.gdkgl.Config(mode = (gtk.gdkgl.MODE_RGB |
|
|
gtk.gdkgl.MODE_DOUBLE |
|
|
gtk.gdkgl.MODE_DEPTH))
|
|
except gtk.gdkgl.NoMatches:
|
|
# try single-buffered
|
|
self.glconfig = gtk.gdkgl.Config(mode = (gtk.gdkgl.MODE_RGB |
|
|
gtk.gdkgl.MODE_DEPTH))
|
|
|
|
self.widget = gtk.gtkgl.DrawingArea(self.glconfig)
|
|
|
|
self.widget.connect_after('realize', self.init)
|
|
self.widget.connect('configure_event', self.reshape)
|
|
self.widget.connect('expose_event', self.draw)
|
|
|
|
def init(self, glarea):
|
|
# get GLContext and GLDrawable
|
|
glcontext = glarea.get_gl_context()
|
|
gldrawable = glarea.get_gl_drawable()
|
|
|
|
# GL calls
|
|
if not gldrawable.gl_begin(glcontext): return
|
|
|
|
glLightfv(GL_LIGHT0, GL_POSITION, (1, 1, 1, 0))
|
|
glEnable(GL_CULL_FACE)
|
|
glEnable(GL_LIGHTING)
|
|
glEnable(GL_LIGHT0)
|
|
glEnable(GL_DEPTH_TEST)
|
|
|
|
gldrawable.gl_end()
|
|
|
|
def reshape(self, glarea, event):
|
|
# get GLContext and GLDrawable
|
|
glcontext = glarea.get_gl_context()
|
|
gldrawable = glarea.get_gl_drawable()
|
|
|
|
# GL calls
|
|
if not gldrawable.gl_begin(glcontext): return
|
|
|
|
x, y, width, height = glarea.get_allocation()
|
|
|
|
glViewport(0, 0, width, height)
|
|
glMatrixMode(GL_PROJECTION)
|
|
glLoadIdentity()
|
|
if width > height:
|
|
w = float(width) / float(height)
|
|
glFrustum(-w, w, -1.0, 1.0, 5.0, 60.0)
|
|
else:
|
|
h = float(height) / float(width)
|
|
glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0)
|
|
|
|
glMatrixMode(GL_MODELVIEW)
|
|
glLoadIdentity()
|
|
glTranslatef(0.0, 0.0, -40.0)
|
|
|
|
gldrawable.gl_end()
|
|
|
|
return True
|
|
|
|
def draw(self, glarea, event):
|
|
# get GLContext and GLDrawable
|
|
glcontext = glarea.get_gl_context()
|
|
gldrawable = glarea.get_gl_drawable()
|
|
|
|
# GL calls
|
|
if not gldrawable.gl_begin(glcontext): return
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
|
|
|
|
glPushMatrix()
|
|
glTranslatef(-3.0, -2.0, -5.0)
|
|
glBegin(GL_QUADS)
|
|
glNormal(0, 0, 1)
|
|
glVertex(0, 0, 0)
|
|
glVertex(1, 0, 0)
|
|
glVertex(1, 1, 0)
|
|
glVertex(0, 1, 0)
|
|
glEnd()
|
|
glPopMatrix()
|
|
|
|
if gldrawable.is_double_buffered():
|
|
gldrawable.swap_buffers()
|
|
else:
|
|
glFlush()
|
|
|
|
gldrawable.gl_end()
|
|
|
|
return True
|