1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|
#!/usr/bin/env python
# A simple example to demonstrate the use of PyGtkGLExt library.
# This program has been mapped to Python from C. The orginal C
# program 'coolwave.c' is distributed with GtkGLExt. It is based
# on Erik Larsen's demo 'newave'.
#
# Note that due to the slow nature of nested loops in Python,
# this program will not run very well in slow machines.
#
# Alif Wahid, <awah005@users.sourceforge.net>
# August 2003.
import math
import array
import pygtk
pygtk.require('2.0')
from gtk.gtkgl.apputils import *
from OpenGL.GL import *
from OpenGL.GLU import *
try:
GL_VERSION_1_1
except:
from OpenGL.GL.EXT.polygon_offset import *
# Some constants.
MAXGRID = 64
M_PI = math.pi
SQRTOFTWOINV = 0.707106781
class CoolWave (GLScene,
GLSceneButton,
GLSceneButtonMotion,
GLSceneKey,
GLSceneIdle):
''' An implementation of the GLScene and
related mixin interfaces. It renders an
animating 3D waveform. The waveform shows
up as a wireframe.
'''
def __init__ (self):
GLScene.__init__(self,
gtk.gdkgl.MODE_RGB |
gtk.gdkgl.MODE_DEPTH |
gtk.gdkgl.MODE_DOUBLE)
# Some private attributes needed
# for rendering purposes.
self.__lightPosition = [0.0, 0.0, 1.0, 1.0]
self.__grid = (MAXGRID/2)
self.__dt = 0.025
self.__sphi = 180.0
self.__stheta = 80.0
self.__sdepth = 1.25 * (MAXGRID/2)
self.__zNear = (MAXGRID/2)/10.0
self.__zFar = (MAXGRID/2)*3.0
self.__aspect = 1.25
self.__beginX = 0
self.__beginY = 0
self.__setup_arrays()
def __setup_arrays (self):
''' Creates the three matrices for
storing vertex data of the rendering waveform.
The matrices are pure python lists containing a
number of arrays. Each array forms one row.
'''
self.__force = []
self.__veloc = []
self.__posit = []
for i in xrange(0, MAXGRID):
self.__force.append(array.array('f', range(0, MAXGRID)))
self.__veloc.append(array.array('f', range(0, MAXGRID)))
self.__posit.append(array.array('f', range(0, MAXGRID)))
def __getforce (self, gridSize):
''' The force derivative of the transcendental
waveform is changed in order to cause a spatial
propagation of the 3D waveform.
'''
for i in xrange(0, gridSize):
for j in xrange(0, gridSize):
self.__force[i][j] = 0.0
for i in xrange(2, gridSize-2):
for j in xrange(2, gridSize-2):
d = self.__posit[i][j] - self.__posit[i][j-1]
self.__force[i][j] -= d
self.__force[i][j-1] += d
d = self.__posit[i][j] - self.__posit[i-1][j]
self.__force[i][j] -= d
self.__force[i-1][j] += d
d = self.__posit[i][j] - self.__posit[i][j+1]
self.__force[i][j] -= d
self.__force[i][j+1] += d
d = self.__posit[i][j] - self.__posit[i+1][j]
self.__force[i][j] -= d
self.__force[i+1][j] += d
d = (self.__posit[i][j] - self.__posit[i+1][j+1]) * SQRTOFTWOINV
self.__force[i][j] -= d
self.__force[i+1][j+1] += d
d = (self.__posit[i][j] - self.__posit[i-1][j-1]) * SQRTOFTWOINV
self.__force[i][j] -= d
self.__force[i-1][j-1] += d
d = (self.__posit[i][j] - self.__posit[i+1][j-1]) * SQRTOFTWOINV
self.__force[i][j] -= d
self.__force[i+1][j-1] += d
d = (self.__posit[i][j] - self.__posit[i-1][j+1]) * SQRTOFTWOINV
self.__force[i][j] -= d
self.__force[i-1][j+1] += d
def __getveloc (self, gridSize, dt):
''' Based on the force derivative calculate the
velocity of each vertex in the waveform.
'''
for i in xrange(0, gridSize):
for j in xrange(0, gridSize): self.__veloc[i][j] += self.__force[i][j] * dt
def __getposit (self, gridSize):
''' Based on the velocity calculate the
position of each vertex in the waveform.
'''
for i in xrange(0, gridSize):
for j in xrange(0, gridSize): self.__posit[i][j] += self.__veloc[i][j]
def __draw_wireframe (self, gridSize):
''' Use the vertex data to render the
waveform in 3D space.
'''
glColor3f(1.0, 1.0, 1.0)
for i in xrange(0, gridSize):
glBegin(GL_LINE_STRIP)
for j in xrange(0, gridSize): glVertex3f(float(i), float(j), self.__posit[i][j])
glEnd()
for i in xrange(0, gridSize):
glBegin(GL_LINE_STRIP)
for j in xrange(0, gridSize): glVertex3f(float(j), float(i), self.__posit[j][i])
glEnd()
def __init_wireframe (self, gridSize):
''' Initial shape of the waveform
is based on a transcendental function.
'''
for i in xrange(0, gridSize):
for j in xrange(0, gridSize):
self.__force[i][j] = 0.0
self.__veloc[i][j] = 0.0
self.__posit[i][j] = (math.sin(M_PI * 2 * i/gridSize) + math.sin(M_PI * 2 * j/gridSize)) * (gridSize/6.0)
if (i==0) or (j==0) or (i==(gridSize-1)) or (j==(gridSize-1)): self.__posit[i][j] = 0.0
# Following methods are implementation of
# the GLScene and related mixin interfaces.
def init (self):
glClearColor(0.0, 0.0, 0.0, 0.0)
glClearDepth(1.0)
glDepthFunc(GL_LEQUAL)
glEnable(GL_DEPTH_TEST)
glEnable(GL_CULL_FACE)
glEnable(GL_COLOR_MATERIAL)
glEnable(GL_LIGHT0)
if GL_VERSION_1_1:
glPolygonOffset(1.0, 1.0)
else:
# Check for the PolygonOffset extension.
if not glInitPolygonOffsetEXT():
print "Need glPolygonOffsetEXT()"
raise SystemExit
glPolygonOffsetEXT(1.0, 1.0)
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST)
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST)
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
glColorMaterial(GL_FRONT, GL_DIFFUSE)
glLightfv(GL_LIGHT0, GL_POSITION, self.__lightPosition)
glShadeModel(GL_FLAT)
glDisable(GL_LIGHTING)
self.__init_wireframe(self.__grid)
def display (self, width, height):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(64.0, self.__aspect, self.__zNear, self.__zFar)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glTranslatef(0.0,0.0,-self.__sdepth)
glRotatef(-self.__stheta, 1.0, 0.0, 0.0)
glRotatef(self.__sphi, 0.0, 0.0, 1.0)
glTranslatef(-float((self.__grid+1)/2 - 1), -float((self.__grid+1)/2 - 1), 0.0)
self.__draw_wireframe(self.__grid)
def reshape (self, width, height):
self.__aspect = float(width)/float(height)
glViewport(0,0, width, height)
def key_press (self, width, height, event):
if event.keyval == gtk.keysyms.i:
# Toggle animation.
self.toggle_idle()
elif event.keyval == gtk.keysyms.r:
# Reset the wave shape.
self.__init_wireframe(self.__grid)
elif event.keyval == gtk.keysyms.plus:
# Zoom in.
self.__sdepth -= 2.0
elif event.keyval == gtk.keysyms.minus:
# Zoom out.
self.__sdepth += 2.0
self.invalidate()
def key_release (self, width, height, event):
pass
def button_press (self, width, height, event):
if (event.button == 1) or (event.button == 2):
self.__beginX = event.x
self.__beginY = event.y
def button_release (self, width, height, event):
pass
def button_motion (self, width, height, event):
if event.state & gtk.gdk.BUTTON1_MASK:
self.__sphi += (event.x - self.__beginX)/4.0
self.__stheta += (self.__beginY - event.y)/4.0
elif event.state & gtk.gdk.BUTTON2_MASK:
self.__sdepth += (self.__beginY - event.y)/10.0
self.__beginX = event.x
self.__beginY = event.y
self.invalidate()
def idle (self, width, height):
self.__getforce(self.__grid)
self.__getveloc(self.__grid, self.__dt)
self.__getposit(self.__grid)
# Invalidate whole window.
self.invalidate()
# Update window synchronously (fast).
self.update()
if __name__ == '__main__':
glscene = CoolWave()
glapp = GLApplication(glscene)
glapp.set_title('CoolWave')
glapp.set_size_request(400, 250)
glapp.run()
|