From d33d9f0f1a8f4d9004c490a1e29bb05e51c87eb8 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Mon, 28 Mar 2011 15:26:14 -0400 Subject: [PATCH] move GL widget code to SketchWidget --- SketchWidget.py | 98 +++++++++++++++++++++++++++++++++++++++++++++++++ Window.py | 98 ++----------------------------------------------- 2 files changed, 102 insertions(+), 94 deletions(-) create mode 100644 SketchWidget.py diff --git a/SketchWidget.py b/SketchWidget.py new file mode 100644 index 0000000..a7640eb --- /dev/null +++ b/SketchWidget.py @@ -0,0 +1,98 @@ + +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 diff --git a/Window.py b/Window.py index 7df1cce..7d0eaea 100644 --- a/Window.py +++ b/Window.py @@ -1,10 +1,8 @@ import gtk -import gtk.gtkgl - -from OpenGL.GL import * from Sketch import Sketch +from SketchWidget import SketchWidget class Window: def __init__(self, title): @@ -13,23 +11,9 @@ class Window: self.window.set_title(title) self.window.connect("destroy", self.destroy_event) - 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)) - - glarea = gtk.gtkgl.DrawingArea(self.glconfig) - - glarea.connect_after('realize', self.init) - glarea.connect('configure_event', self.reshape) - glarea.connect('expose_event', self.draw) - - self.window.add(glarea) + s = Sketch() + sw = SketchWidget(s) + self.window.add(sw.widget) self.window.show_all() def main(self): @@ -37,77 +21,3 @@ class Window: def destroy_event(self, widget, data=None): gtk.main_quit() - - 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