move GL widget code to SketchWidget
This commit is contained in:
parent
da1c47441e
commit
d33d9f0f1a
98
SketchWidget.py
Normal file
98
SketchWidget.py
Normal file
@ -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
|
98
Window.py
98
Window.py
@ -1,10 +1,8 @@
|
|||||||
|
|
||||||
import gtk
|
import gtk
|
||||||
import gtk.gtkgl
|
|
||||||
|
|
||||||
from OpenGL.GL import *
|
|
||||||
|
|
||||||
from Sketch import Sketch
|
from Sketch import Sketch
|
||||||
|
from SketchWidget import SketchWidget
|
||||||
|
|
||||||
class Window:
|
class Window:
|
||||||
def __init__(self, title):
|
def __init__(self, title):
|
||||||
@ -13,23 +11,9 @@ class Window:
|
|||||||
self.window.set_title(title)
|
self.window.set_title(title)
|
||||||
self.window.connect("destroy", self.destroy_event)
|
self.window.connect("destroy", self.destroy_event)
|
||||||
|
|
||||||
try:
|
s = Sketch()
|
||||||
# try double-buffered
|
sw = SketchWidget(s)
|
||||||
self.glconfig = gtk.gdkgl.Config(mode = (gtk.gdkgl.MODE_RGB |
|
self.window.add(sw.widget)
|
||||||
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)
|
|
||||||
self.window.show_all()
|
self.window.show_all()
|
||||||
|
|
||||||
def main(self):
|
def main(self):
|
||||||
@ -37,77 +21,3 @@ class Window:
|
|||||||
|
|
||||||
def destroy_event(self, widget, data=None):
|
def destroy_event(self, widget, data=None):
|
||||||
gtk.main_quit()
|
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
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user