creating a gtkgl DrawingArea and drawing a quad
This commit is contained in:
parent
9741db038e
commit
89486e1499
97
Window.py
97
Window.py
@ -1,12 +1,33 @@
|
||||
|
||||
import gtk
|
||||
import gtk.gtkgl
|
||||
|
||||
from OpenGL.GL import *
|
||||
|
||||
class Window:
|
||||
def __init__(self, title):
|
||||
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
|
||||
self.window.set_title(title)
|
||||
self.window.connect("destroy", self.destroy_event)
|
||||
self.window.add(gtk.Label('Hello World'))
|
||||
|
||||
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.set_size_request(300, 300)
|
||||
|
||||
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()
|
||||
|
||||
def main(self):
|
||||
@ -14,3 +35,77 @@ 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
|
||||
|
Loading…
x
Reference in New Issue
Block a user