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.gtkgl
from OpenGL.GL import *
from Line import Line
from Circle import Circle
class SketchWidget:
def __init__(self, sketch):
self.sketch = sketch
@ -51,17 +55,10 @@ class SketchWidget:
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)
glOrtho(-2, 2, -1.5, 1.5, 1.0, -1.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glTranslatef(0.0, 0.0, -40.0)
gldrawable.gl_end()
@ -77,16 +74,20 @@ class SketchWidget:
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)
for shape in self.sketch:
if isinstance(shape, Line):
glBegin(GL_LINES)
glVertex(shape.vars[0], shape.vars[1])
glVertex(shape.vars[2], shape.vars[3])
glEnd()
elif isinstance(shape, Circle):
glBegin(GL_LINE_LOOP)
for i in range(24):
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()
glPopMatrix()
if gldrawable.is_double_buffered():
gldrawable.swap_buffers()