SketchWidget: render basic lines and circles

This commit is contained in:
Josh Holtrop 2011-03-29 14:06:18 -04:00
parent 9e3ca181c7
commit 8901c23af7

View File

@ -1,9 +1,13 @@
import math
import gtk import gtk
import gtk.gtkgl import gtk.gtkgl
from OpenGL.GL import * from OpenGL.GL import *
from Line import Line
from Circle import Circle
class SketchWidget: class SketchWidget:
def __init__(self, sketch): def __init__(self, sketch):
self.sketch = sketch self.sketch = sketch
@ -51,17 +55,10 @@ class SketchWidget:
glViewport(0, 0, width, height) glViewport(0, 0, width, height)
glMatrixMode(GL_PROJECTION) glMatrixMode(GL_PROJECTION)
glLoadIdentity() glOrtho(-2, 2, -1.5, 1.5, 1.0, -1.0)
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) glMatrixMode(GL_MODELVIEW)
glLoadIdentity() glLoadIdentity()
glTranslatef(0.0, 0.0, -40.0)
gldrawable.gl_end() gldrawable.gl_end()
@ -77,16 +74,20 @@ class SketchWidget:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glPushMatrix() for shape in self.sketch:
glTranslatef(-3.0, -2.0, -5.0) if isinstance(shape, Line):
glBegin(GL_QUADS) glBegin(GL_LINES)
glNormal(0, 0, 1) glVertex(shape.vars[0], shape.vars[1])
glVertex(0, 0, 0) glVertex(shape.vars[2], shape.vars[3])
glVertex(1, 0, 0) glEnd()
glVertex(1, 1, 0) elif isinstance(shape, Circle):
glVertex(0, 1, 0) glBegin(GL_LINE_LOOP)
glEnd() for i in range(24):
glPopMatrix() glVertex(shape.vars[0] +
shape.vars[2] * math.sin(i * 2 * math.pi / 24.0),
shape.vars[1] +
shape.vars[2] * math.cos(i * 2 * math.pi / 24.0))
glEnd()
if gldrawable.is_double_buffered(): if gldrawable.is_double_buffered():
gldrawable.swap_buffers() gldrawable.swap_buffers()