new interface may be unreadable, raise an appropriate IOError
[pyhdaps.git] / hdaps-gl
blob7246f11174949a93de5cdfe2956cba6cf0c40dac
1 #!/usr/bin/env python
3 '''
4 hdaps-gl - draw a laptop based on the hdaps position
5 '''
7 # Copyright: 2008-2009 Evgeni Golov <sargentd@die-welt.net>
8 # License: GPL-2
10 # The model of the simple laptop is created by
11 # Patrick Kilian <petschge@petschge.de>
12 # and licensed under GPL-2
14 import OpenGL
15 OpenGL.ERROR_CHECKING = False
16 from OpenGL.GL import *
17 from OpenGL.GLU import *
18 from OpenGL.GLUT import *
19 from time import sleep
20 import sys
21 from hdaps import *
23 oldX = 0
24 oldY = 0
25 calibX = 0
26 calibY = 0
27 WIDTH = 640
28 HEIGHT = 480
30 def drawLaptop( posX=0, posY=0, laptop=(0, 0, 0), screen=(0, 0, 1), trackpoint=(1, 0, 0), N=5 ):
31 glClear (GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
32 glPushMatrix ();
33 try:
34 glRotated (posX / 2.0, 0.0, 0.0, -1.0);
35 glRotated (posY / 2.0, 1.0, 0.0, 0.0);
37 glBegin (GL_QUADS); # start drawing the laptop.
39 # top of body
40 glColor3fv (laptop);
41 glVertex3d (N*1.0, N*0.0, N*1.6);
42 glVertex3d (N*(-1.0), N*0.0, N*1.6);
43 glVertex3d (N*(-1.0), N*0.0, N*0.0);
44 glVertex3d (N*1.0, N*0.0, N*0.0);
46 # trackpoint
47 glColor3fv (trackpoint);
48 glVertex3d (N*0.01, N*0.01, N*0.78);
49 glVertex3d (N*0.01, N*0.01, N*0.82);
50 glVertex3d (N*(-0.01), N*0.01, N*0.82);
51 glVertex3d (N*(-0.01), N*0.01, N*0.78);
53 glColor3fv (laptop);
54 glVertex3d (N*1.0, N*(-0.1), N*(-0.1));
55 glVertex3d (N*-1.0, N*-0.1, N*(-0.1));
56 glVertex3d (N*-1.0, N*-0.1, N*1.6);
57 glVertex3d (N*1.0, N*-0.1, N*1.6);
59 # front of body
60 glColor3fv (laptop);
61 glVertex3d (N*1.0, N*0.0, N*1.6);
62 glVertex3d (N*-1.0, N*0.0, N*1.6);
63 glVertex3d (N*-1.0, N*-0.1, N*1.6);
64 glVertex3d (N*1.0, N*-0.1, N*1.6);
66 # left of body
67 glColor3fv (laptop);
68 glVertex3d (N*-1.0, N*0.0, N*1.6);
69 glVertex3d (N*-1.0, N*0.0, N*0.0);
70 glVertex3d (N*-1.0, N*-0.1, N*0.0);
71 glVertex3d (N*-1.0, N*-0.1, N*1.6);
73 # Right of body
74 glColor3fv (laptop);
75 glVertex3d (N*1.0, N*0.0, N*0.0);
76 glVertex3d (N*1.0, N*0.0, N*1.6);
77 glVertex3d (N*1.0, N*-0.1, N*1.6);
78 glVertex3d (N*1.0, N*-0.1, N*0.0);
80 # top of screen
81 glColor3fv (laptop);
82 glVertex3d (N*1.0, N*1.6, N*-0.1);
83 glVertex3d (N*-1.0, N*1.6, N*-0.1);
84 glVertex3d (N*-1.0, N*1.6, N*0.0);
85 glVertex3d (N*1.0, N*1.6, N*0.0);
87 # front of screen
88 glColor3fv (laptop);
89 glVertex3d (N*1.0, N*1.6, N*0.0);
90 glVertex3d (N*-1.0, N*1.6, N*0.0);
91 glVertex3d (N*-1.0, N*0.0, N*0.0);
92 glVertex3d (N*1.0, N*0.0, N*0.0);
94 # screen
95 glColor3fv (screen);
96 glVertex3d (N*0.9, N*1.5, N*0.01);
97 glVertex3d (N*-0.9, N*1.5, N*0.01);
98 glVertex3d (N*-0.9, N*0.1, N*0.01);
99 glVertex3d (N*0.9, N*0.1, N*0.01);
101 # back of screen
102 glColor3fv (laptop);
103 glVertex3d (N*1.0, N*-0.1, N*-0.1);
104 glVertex3d (N*-1.0, N*-0.1, N*-0.1);
105 glVertex3d (N*-1.0, N*1.6, N*-0.1);
106 glVertex3d (N*1.0, N*1.6, N*-0.1);
108 # left of screen
109 glColor3fv (laptop);
110 glVertex3d (N*-1.0, N*1.6, N*0.0);
111 glVertex3d (N*-1.0, N*1.6, N*-0.1);
112 glVertex3d (N*-1.0, N*-0.1, N*-0.1);
113 glVertex3d (N*-1.0, N*-0.1, N*0.0);
115 # Right of screen
116 glColor3fv (laptop);
117 glVertex3d (N*1.0, N*1.6, N*-0.1);
118 glVertex3d (N*1.0, N*1.6, N*0.0);
119 glVertex3d (N*1.0, N*-0.1, N*0.0);
120 glVertex3d (N*1.0, N*-0.1, N*-0.1);
121 glEnd ();
122 finally:
123 glPopMatrix ();
124 glutSwapBuffers ();
126 def forceUpdateWindow(arg):
127 updateWindow(True)
129 def updateWindow(update=False):
130 global oldX,oldY
131 posX,posY = readPosition()
132 if (abs(posX - oldX) > 5 or abs(posY - oldY) > 5):
133 update = True
134 if (abs(posX) < 5 or abs(posY) < 5):
135 posX=0
136 posY=0
137 update=True
138 if update:
139 drawLaptop(posX-calibX, posY-calibY)
140 oldX=posX
141 oldY=posY
142 sleep(0.05)
144 def main():
145 global calibX,calibY
146 calibX,calibY=readPosition(True)
147 glutInit(sys.argv)
148 glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
149 glutInitWindowSize (WIDTH, HEIGHT);
150 glutInitWindowPosition (0, 0);
151 glutCreateWindow('HDAPS-GL')
152 glutDisplayFunc(updateWindow)
153 glutIdleFunc(updateWindow)
154 glutVisibilityFunc(forceUpdateWindow)
155 glClearColor (0.5, 0.5, 0.5, 0.0);
156 glClearDepth (1.0); # Enables Clearing Of The Depth Buffer
157 glDepthFunc (GL_LESS); # The Type Of Depth Test To Do
158 glEnable (GL_DEPTH_TEST); # Enables Depth Testing
159 glShadeModel (GL_SMOOTH); # Enables Smooth Color Shading
161 glMatrixMode (GL_PROJECTION);
162 glLoadIdentity ();
163 gluPerspective (45.0, float(WIDTH) / float(HEIGHT), 0.25, 100.0);
165 glMatrixMode (GL_MODELVIEW);
166 glLoadIdentity ();
167 gluLookAt(
168 0,1,20, # eyepoint
169 0,0,0, # center-of-view
170 0,1,0, # up-vector
172 #glTranslated (10.0, -0.5, -1.0);
174 drawLaptop(0,0)
175 glutMainLoop()
177 if __name__ == "__main__":
178 main()