comparison lightsim/openglsim.py @ 345:839545f174d3

add drawPixels version
author Drew Perttula <drewp@bigasterisk.com>
date Sat, 09 Jun 2007 06:02:52 +0000
parents f866d4dec57b
children a08882a05d29
comparison
equal deleted inserted replaced
344:28b6e0dc31b7 345:839545f174d3
1 # see http://www.sgi.com/software/opengl/advanced97/notes/node57.html for accum notes 1 # see http://www.sgi.com/software/opengl/advanced97/notes/node57.html for accum notes
2 2
3 import sys, time 3 import sys, time
4 import numpy as num 4 import numarray as num
5
6
7 sys.path.append("/usr/lib/python2.4/site-packages/OpenGL/Tk/linux2-tk8.4")
5 from OpenGL.GL import * 8 from OpenGL.GL import *
6 from OpenGL.Tk import * 9 import Togl
10 import Tkinter as tk
11
7 import Image 12 import Image
8 13
9 def drawWithAlpha(imgString, w, h, alpha): 14 def drawWithAlpha(imgString, w, h, alpha):
10 """without opengl extensions""" 15 """without opengl extensions"""
11 t = time.time() 16 t = time.time()
18 glDrawPixels(w, h, 23 glDrawPixels(w, h,
19 GL_RGBA, GL_UNSIGNED_BYTE, ar.tostring()) 24 GL_RGBA, GL_UNSIGNED_BYTE, ar.tostring())
20 print " draw", time.time() - t 25 print " draw", time.time() - t
21 26
22 27
28 def drawWithAlpha(imgString, w, h, alpha):
29 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
30
31 glClear(GL_COLOR_BUFFER_BIT)
32 #glBlendColor(1, 1, 1, mag) # needs ARB_imaging
33 glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, imgString)
34
35
23 class Surface: 36 class Surface:
24 def Display(self, event=None): 37 def Display(self, event=None):
25 assert 'GL_ARB_imaging' in glGetString(GL_EXTENSIONS).split() 38 assert 'GL_ARB_imaging' in glGetString(GL_EXTENSIONS).split()
26 39
27 glClearColor(0.0, 0.0, 0.0, 0) 40 glClearColor(0.0, 0.0, 0.0, 0)
28 glClear( GL_COLOR_BUFFER_BIT |GL_ACCUM_BUFFER_BIT) 41 glClear( GL_COLOR_BUFFER_BIT |GL_ACCUM_BUFFER_BIT)
29 glEnable(GL_BLEND) 42 glEnable(GL_BLEND)
30 #glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
31 glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA) 43 glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA)
32 l=glGenLists(1) 44 l=glGenLists(1)
33 glNewList(l,GL_COMPILE) 45 glNewList(l,GL_COMPILE)
34 glEndList() 46 glEndList()
35 47
36 # glDrawBuffer(GL_BACK) 48 # glDrawBuffer(GL_BACK)
37 49
38 for x, img in enumerate(self.image): 50 for x, img in enumerate(self.image):
39 mag = self.scales[x].get() 51 mag = self.scales[x].get()
40 print "pic %i at %f" % (x,mag) 52 print "pic %i at %f" % (x,mag)
41 # glClear(GL_COLOR_BUFFER_BIT)
42 #glBlendColor(1, 1, 1, mag) # needs ARB_imaging
43 drawWithAlpha(img, self.imageWidth, self.imageHeight, mag) 53 drawWithAlpha(img, self.imageWidth, self.imageHeight, mag)
44 54
45 55
46 ## if x==0: 56 ## if x==0:
47 ## glAccum(GL_LOAD,mag) 57 ## glAccum(GL_LOAD,mag)
51 # glAccum(GL_ADD,self.x) 61 # glAccum(GL_ADD,self.x)
52 self.x=(self.x+.1)%2.0 62 self.x=(self.x+.1)%2.0
53 ## glAccum(GL_RETURN,1) 63 ## glAccum(GL_RETURN,1)
54 64
55 def SetupWindow(self): 65 def SetupWindow(self):
56 self.OglFrame = Frame() 66 self.OglFrame = tk.Frame()
57 self.OglFrame.pack(side = 'top',fill='both',expand=1) 67 self.OglFrame.pack(side = 'top',fill='both',expand=1)
58 self.QuitButton = Button(self.OglFrame, {'text':'Quit'}) 68 self.QuitButton = tk.Button(self.OglFrame, {'text':'Quit'})
59 self.QuitButton.bind('<ButtonRelease-1>', sys.exit) 69 self.QuitButton.bind('<ButtonRelease-1>', sys.exit)
60 self.QuitButton.pack({'side':'top'}) 70 self.QuitButton.pack({'side':'top'})
61 71
62 72
63 def SetupOpenGL(self): 73 def SetupOpenGL(self):
64 self.ogl = Opengl(master=self.OglFrame, width = 512, height = 270, double = 1, depth = 0) 74 self.ogl = Togl.Opengl(master=self.OglFrame, width = 512, height = 270, double = 1, depth = 0)
65 self.ogl.pack(side = 'top', expand = 1, fill = 'both') 75 self.ogl.pack(side = 'top', expand = 1, fill = 'both')
66 self.ogl.set_centerpoint(0, 0, 0) 76 self.ogl.set_centerpoint(0, 0, 0)
67 self.ogl.redraw = self.Display 77 self.ogl.redraw = self.Display
68 78
69 for x in range(0,2): 79 for x in range(0,2):
78 self.SetupWindow() 88 self.SetupWindow()
79 89
80 self.image=[] 90 self.image=[]
81 for filename in ('skyline/bg.png', 'skyline/cyc-lo-red.png'): 91 for filename in ('skyline/bg.png', 'skyline/cyc-lo-red.png'):
82 im = Image.open(filename) 92 im = Image.open(filename)
83 im.thumbnail((200, 200)) 93 #im.thumbnail((200, 200))
84 self.imageWidth = im.size[0] 94 self.imageWidth = im.size[0]
85 self.imageHeight = im.size[1] 95 self.imageHeight = im.size[1]
86 self.image.append(im.convert("RGBA").tostring())#"raw", "RGB", 0, -1)) 96 self.image.append(im.convert("RGBA").tostring())#"raw", "RGB", 0, -1))
87 print self.imageWidth, self.imageHeight, self.imageWidth * self.imageHeight*4, len(self.image) 97 print self.imageWidth, self.imageHeight, self.imageWidth * self.imageHeight*4, len(self.image)
88 98
90 100
91 glDisable(GL_CULL_FACE) 101 glDisable(GL_CULL_FACE)
92 # glEnable(GL_DEPTH_TEST) 102 # glEnable(GL_DEPTH_TEST)
93 # glEnable(GL_NORMALIZE) 103 # glEnable(GL_NORMALIZE)
94 glShadeModel(GL_FLAT) 104 glShadeModel(GL_FLAT)
105 print 'GL_ARB_imaging', 'GL_ARB_imaging' in glGetString(GL_EXTENSIONS)
106 import OpenGL
107 print OpenGL.__version__
95 108
96 self.ogl.tkRedraw() 109 self.ogl.tkRedraw()
97 self.ogl.mainloop() 110 self.ogl.mainloop()
98 111
99 if __name__ == '__main__': 112 if __name__ == '__main__':