Skip to content

Instantly share code, notes, and snippets.

@blink1073
Created November 11, 2013 13:04
Show Gist options
  • Save blink1073/7412894 to your computer and use it in GitHub Desktop.
Save blink1073/7412894 to your computer and use it in GitHub Desktop.
Demonstration of PyQt4.OpenGL.QGLWidget in enaml
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 11 04:16:39 2013
@author: Steven Silvester (steven.silvester@iee.org)
@license: Public Domain
Based on: http://www.siafoo.net/snippet/316
Created on Jul 7, 2009
@author: Stou Sandalski (stou@icapsid.net)
@license: Public Domain
"""
from enaml.qt import QtCore, QtGui
from enaml.widgets.api import *
import math
from OpenGL.GL import *
from OpenGL.GLU import *
from PyQt4.QtOpenGL import *
class SpiralWidget(QGLWidget):
'''
Widget for drawing two spirals.
'''
def __init__(self, parent):
QGLWidget.__init__(self, parent)
self.setMinimumSize(500, 500)
def paintGL(self):
'''
Drawing routine
'''
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
# Draw the spiral in 'immediate mode'
# WARNING: You should not be doing the spiral calculation inside the loop
# even if you are using glBegin/glEnd, sin/cos are fairly expensive functions
# I've left it here as is to make the code simpler.
radius = 1.0
x = radius*math.sin(0)
y = radius*math.cos(0)
glColor(0.0, 1.0, 0.0)
glBegin(GL_LINE_STRIP)
for deg in xrange(1000):
glVertex(x, y, 0.0)
rad = math.radians(deg)
radius -= 0.001
x = radius*math.sin(rad)
y = radius*math.cos(rad)
glEnd()
glEnableClientState(GL_VERTEX_ARRAY)
spiral_array = []
# Second Spiral using "array immediate mode" (i.e. Vertex Arrays)
radius = 0.8
x = radius*math.sin(0)
y = radius*math.cos(0)
glColor(1.0, 0.0, 0.0)
for deg in xrange(820):
spiral_array.append([x, y])
rad = math.radians(deg)
radius -= 0.001
x = radius*math.sin(rad)
y = radius*math.cos(rad)
glVertexPointerf(spiral_array)
glDrawArrays(GL_LINE_STRIP, 0, len(spiral_array))
glFlush()
def resizeGL(self, w, h):
'''
Resize the GL window
'''
glViewport(0, 0, w, h)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(40.0, 1.0, 1.0, 30.0)
def initializeGL(self):
'''
Initialize GL
'''
# set viewing projection
glClearColor(0.0, 0.0, 0.0, 1.0)
glClearDepth(1.0)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(40.0, 1.0, 1.0, 30.0)
class GLWidget(RawWidget):
def create_widget(self, parent):
""" Create the toolkit widget for the control.
This method should create and initialize the widget.
Parameters
----------
parent : toolkit widget or None
The parent toolkit widget for the control.
Returns
-------
result : toolkit widget
The toolkit specific widget for the control.
"""
return SpiralWidget(parent)
enamldef Main(MainWindow):
attr model
Container:
GLWidget:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment