341
|
1 #!/usr/bin/python2.4
|
|
2 from __future__ import division
|
|
3 import sys, time, math
|
|
4 import Image, ImageFilter
|
|
5 import Numeric as num
|
|
6 from OpenGL import GL, GLUT, GLU
|
|
7 from OpenGL.GL import *
|
|
8
|
|
9 def openglSetup(width=512, height=256):
|
|
10 global cardList
|
|
11
|
|
12 glClearColor (0.0, 0.0, 0.0, 0.0)
|
|
13 glShadeModel (GL_SMOOTH)
|
|
14 glEnable(GL_COLOR_MATERIAL)
|
|
15
|
|
16 glLightfv(GL_LIGHT0, GL_AMBIENT, [0.0, 0.0, 0.0, 1.0])
|
|
17 glLightfv(GL_LIGHT0, GL_DIFFUSE, [1.0, 1.0, 1.0, 1.0])
|
|
18 glLightfv(GL_LIGHT0, GL_POSITION, [0.0, 3.0, 3.0, 0.0])
|
|
19 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, [0.2, 0.2, 0.2, 1.0])
|
|
20 glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, [0])
|
|
21
|
|
22 #glFrontFace(GL_CW)
|
|
23 glEnable(GL_LIGHTING)
|
|
24 glEnable(GL_LIGHT0)
|
|
25 #glEnable(GL_AUTO_NORMAL)
|
|
26 #glEnable(GL_NORMALIZE)
|
|
27 glEnable(GL_DEPTH_TEST)
|
|
28
|
|
29
|
|
30 glViewport (0, 0, width, height)
|
|
31 glMatrixMode (GL_PROJECTION)
|
|
32 glLoadIdentity ()
|
|
33 glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0)
|
|
34 glMatrixMode (GL_MODELVIEW)
|
|
35
|
|
36 cardList = glGenLists(1)
|
|
37 glNewList(cardList, GL_COMPILE)
|
|
38 glColor3f(1,1,1)
|
|
39 glBegin(GL_QUADS)
|
|
40 glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 0.0, 1.0)
|
|
41 glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 0.0, 1.0)
|
|
42 glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, 1.0, 1.0)
|
|
43 glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, 1.0, 1.0)
|
|
44 glEnd()
|
|
45 glEndList()
|
|
46
|
|
47
|
|
48
|
|
49 def imageCard(img):
|
|
50 """card facing +Z from -1<x<1 -1<y<1"""
|
|
51 textureData = img.convert(mode="RGBA").tostring()
|
|
52
|
|
53 glBindTexture(GL_TEXTURE_2D, 0)
|
|
54 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA,
|
|
55 512, #multiImage.size()[0],
|
|
56 256, #multiImage.size()[1],
|
|
57 0,
|
|
58 GL_RGBA, GL_UNSIGNED_BYTE, textureData)
|
|
59 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
|
|
60 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
|
|
61
|
|
62 glCallList(cardList)
|
|
63
|
|
64 def setupDraw():
|
|
65 """per-frame setup"""
|
|
66 glClearColor(0.0, 0.0, 0.0, 0.0)
|
|
67 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
|
|
68 glLoadIdentity ()
|
|
69 GLU.gluLookAt (0.5, 0.5, 8.0,
|
|
70 0.5, 0.5, 0.0,
|
|
71 0.0, 1.0, 0.0)
|
|
72
|
|
73 glDisable(GL_TEXTURE_2D)
|
|
74
|
|
75 correctFrameStart = None
|
|
76 def drawFrame():
|
|
77 global correctFrameStart
|
|
78 t1 = time.time()
|
|
79 setupDraw()
|
|
80
|
|
81 matching = False
|
|
82 glPushMatrix()
|
|
83 try:
|
|
84 glTranslatef(1, -.9, 3.2)
|
|
85 # flip to compensate for the camera facing the user
|
|
86 glScalef(-1, 1, 1)
|
|
87
|
|
88 glDisable(GL_LIGHTING)
|
|
89 glEnable(GL_TEXTURE_2D)
|
|
90
|
|
91 # imageCard(Image.open("skyline/bg.png"))
|
|
92 imageCard(Image.open("skyline/cyc-lo-red.png"))
|
|
93
|
|
94 ## if grab.lastFrame:
|
|
95 ## imageCard(grab.lastFrame)
|
|
96 ## glEnable(GL_LIGHTING)
|
|
97 ## glDisable(GL_TEXTURE_2D)
|
|
98 ## if grab.seenPose:
|
|
99 ## for colorName, pos in grab.seenPose.pos.items():
|
|
100 ## cube(color=(x / 255 for x in vision.colors[colorName]),
|
|
101 ## center=((1-pos[0]) * 2 - 1, pos[1] * 2 - 1, 1),
|
|
102 ## side=.1, wire=False)
|
|
103 ## matching = game.pose.compare(grab.seenPose)
|
|
104 glEnable(GL_LIGHTING)
|
|
105 glDisable(GL_TEXTURE_2D)
|
|
106 finally:
|
|
107 glPopMatrix()
|
|
108
|
|
109
|
|
110 glFlush()
|
|
111 #print "draw", time.time() - t1
|
|
112
|
|
113 GLUT.glutInit(sys.argv)
|
|
114 window = GLUT.glutCreateWindow("sim2")
|
|
115 openglSetup()
|
|
116 GLUT.glutDisplayFunc(drawFrame)
|
|
117 def menu(arg):
|
|
118 print "hi", arg
|
|
119 GLUT.glutCreateMenu(menu)
|
|
120 GLUT.glutAddMenuEntry("hi", 1)
|
|
121 GLUT.glutAttachMenu(GLUT.GLUT_RIGHT_BUTTON)
|
|
122
|
|
123 GLUT.glutMainLoop()
|