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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#!/usr/bin/python

import sys
import numpy as np
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *

# if using a spaceball in Windows
MOUSE3d = False

if MOUSE3d:
    import win32com.client
# end if

win = None
height = 0
width = 0

btn = -1
mode = -1

origin = np.zeros(3)

AXES = 10

x1 = 0
y1 = 0
rx = 0.0
ry = 0.0
rz = 0.0
tx = 0
ty = 0
tz = 0
zoom = 1


def create_line(v1, v2, color):

    glLineWidth(1.0)

    glColor4f(color[0], color[1], color[2], color[3])
    glBegin(GL_LINES)
    glVertex3f(v1[0], v1[1], v1[2])
    glVertex3f(v2[0], v2[1], v2[2])
    glEnd()

# end def create_line


def drawBitmapText(text, x, y, z, color):

    #~ GLUT_BITMAP_8_BY_13
    #~ GLUT_BITMAP_9_BY_15
    #~ GLUT_BITMAP_TIMES_ROMAN_10
    #~ GLUT_BITMAP_TIMES_ROMAN_24
    #~ GLUT_BITMAP_HELVETICA_10
    #~ GLUT_BITMAP_HELVETICA_12
    #~ GLUT_BITMAP_HELVETICA_18
    glColor4f(color[0], color[1], color[2], color[3])
    glRasterPos3f(x, y, z)
    for c in text:
        glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, ord(c))
    # end for

# end def drawBitmapText


def create_point(text, indx, v, dim, color):

    glColor4f(color[0], color[1], color[2], color[3])
    glPushMatrix()
    glTranslatef(v[0], v[1], v[2])
    if indx != 0:
        glPushName(indx)
    # end if
    glutSolidSphere(dim / 10.0, 20, 20)
    if indx != 0:
        glPopName()
    # end if
    glPopMatrix()
    drawBitmapText(text, v[0], v[1], v[2] - 0.5, np.array([1.0, 1.0, 1.0, 1.0]))

# end def create_point


def crea_axes():

    glNewList(AXES, GL_COMPILE)

    glLineWidth(1.0)
    glBegin(GL_LINES)
    glColor4f(1.0, 0.0, 0.0, 1.0)
    glVertex3f(0.0, 0.0, 0.0)
    glVertex3f(7.0, 0.0, 0.0)
    glColor4f(0.0, 1.0, 0.0, 1.0)
    glVertex3f(0.0, 0.0, 0.0)
    glVertex3f(0.0, 7.0, 0.0)
    glColor4f(0.0, 0.0, 1.0, 1.0)
    glVertex3f(0.0, 0.0, 0.0)
    glVertex3f(0.0, 0.0, 7.0)
    glEnd()

    glEndList()

# end def create_axes


def InitGL():

    glClearColor(0.0, 0.0, 0.0, 0.0)
    glClearDepth(1.0)
    glDepthFunc(GL_LESS)
    glEnable(GL_DEPTH_TEST)
    glShadeModel(GL_SMOOTH)
    glEnable(GL_BLEND)
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
    glShadeModel(GL_SMOOTH)
    #~ glEnable(GL_RESCALE_NORMAL)
    glEnable(GL_NORMALIZE)
    glHint(GL_LINE_SMOOTH_HINT, GL_NICEST)
    glHint(GL_POINT_SMOOTH_HINT, GL_NICEST)
    glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST)
    glShadeModel(GL_SMOOTH)
    glEnable(GL_POINT_SMOOTH)
    glEnable(GL_LINE_SMOOTH)
    glEnable(GL_POLYGON_SMOOTH)

# end def InitGL


def resize(w, h):

    global width, height

    width = w
    height = h
    glViewport(0, 0, w, h)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(-25.0, 25.0, -25.0 * h / w, 25.0 * h / w, -50.0, 50.0)
    glMatrixMode(GL_MODELVIEW)
    gluLookAt(
    5, 5,  -5, # eyepoint
    0, 0, 0, # center-of-view
    0, 1, 0, # up-vector
    )

# end def resize


def processHits(ptr):
    
    if len(ptr) > 0:
        print "hits = ", [p.names for p in ptr]
    # end if
    
    for (near, far, names) in ptr:
        for n in names:
            print n
        # end for
    # end for
    
# end def processHits


def mouse(button, state, x, y):

    global btn, mode, width, height

    if state == GLUT_DOWN:
        btn = button
    else:
        btn = 0
    # end if

    viewport = np.zeros(4)

    if (button != GLUT_MIDDLE_BUTTON or state != GLUT_DOWN):
        return
    # end if

    viewport = glGetIntegerv(GL_VIEWPORT)

    glSelectBuffer(np.BUFSIZE)
    glRenderMode(GL_SELECT)
    mode = GL_SELECT

    glInitNames()

    glMatrixMode(GL_PROJECTION)
    glPushMatrix()
    glLoadIdentity()
    gluPickMatrix(x, viewport[3] - y, 20.0, 20.0, viewport)
    glOrtho(-100, 100.0, -100.0, 100.0 * height / width, -3000, 3000)
    glMatrixMode(GL_MODELVIEW)
    draw()
    glMatrixMode(GL_PROJECTION) # push and pop must be in the same context!
    glPopMatrix()

    hits = glRenderMode(GL_RENDER)
    mode = GL_RENDER
    processHits(hits)
    glMatrixMode(GL_MODELVIEW)

    glutPostRedisplay()

# end def mouse


def move(x, y):

    global x1, y1, rx, ry, tx, ty

    if btn == GLUT_RIGHT_BUTTON:
        if y - y1 < -10:
            ty+= 1
            y1 = y
        # end if
        if y - y1 > 10:
            ty -= 1
            y1 = y
        # end if
        if x - x1 < -10:
            tx -= 1
            x1 = x
        # end if
        if x - x1 > 10:
            tx += 1
            x1 = x
        # end if
    elif btn == GLUT_LEFT_BUTTON:
        if y - y1 > 10:
            rx += 2
            rx %= 360
            y1 = y
        # end if
        if y - y1 < -10:
            rx -= 2
            if rx < -360:
                rx = 360
            # end if
            y1 = y
        # end if
        if x - x1 > 10:
            ry += 2
            ry %= 360
            x1 = x
        # end if
        if x - x1 < -10:
            ry -= 2
            if ry < -360:
                ry = 360
            # end if
            x1 = x
        # end if
    # end if

    glutPostRedisplay()

# end def move


# works with FreeGLUT library only!
def wheel(button, direction, x, y):

    global zoom

    zoom += -direction
    if zoom < 1:
        zoom = 1
    # end if

# end def wheel


def keyb(arg, s, c):

    global zoom, tx, ty, tz, rx, ry, rz

    if arg == chr(27):
        sys.exit()
    # end if

    (w, h) = (glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT))

    if arg == chr(32):
        rx = 0
        ry = 0
        rz = 0
        tx = 0
        ty = 0
        tz = 0
        zoom = 1
        glutPostRedisplay()
    # end if

# end def keyb


def updateSensorData():

    global tx, zoom, ty, tz, rx, ry, rz

    Tx = sn.Sensor.Translation.X / 10.0
    Ty = sn.Sensor.Translation.Y / 10.0
    Tz = sn.Sensor.Translation.Z / 10.0
    Rx = float(sn.Sensor.Rotation.X) / 10.0
    Ry = float(sn.Sensor.Rotation.Y) / 10.0
    Rz = float(sn.Sensor.Rotation.Z) / 10.0

    #~ print 'Tx', Tx,
    #~ print 'Ty', Ty,
    #~ print 'Tz', Tz,
    #~ print 'Rx', Rx,
    #~ print 'Ry', Ry,
    #~ print 'Rz', Rz

    tx += 0.05 * Tx
    zoom -= 0.001 * Ty
    ty -= 0.05 * Tz

    rx += Rx
    ry += Ry
    rz += Rz

# end def updateSensorData


def display():

    glClearColor(0.0, 0.0, 0.0, 0)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    draw()
    glutPostRedisplay()

# end def display


def draw():

    if MOUSE3d:
        updateSensorData()
    # end if
    glPushMatrix()
    glTranslatef(tx, ty, tz)
    glRotatef(ry, 0.0, 1.0, 0.0)
    glRotatef(rx, 1.0, 0.0, 0.0)
    glRotatef(rz, 0.0, 0.0, 1.0)
    glScalef(zoom, zoom, zoom)
    glTranslatef(-origin[0], -origin[1], -origin[2])

    glCallList(AXES)

    create_line([0.0, 0.0, 0.0], [1.0, 1.0, 1.0], [1.0, 1.0, 0.0, 1.0])
    
    create_point("(1,1,1)", 1, [1.0, 1.0, 1.0], 2.0, [0.0, 1.0, 0.0, 1.0])
    
    glPopMatrix()

    glutSwapBuffers()

# end def draw


def init():

    create_axes()

# end def init


def main(args):

    global sn, win

    glutInit(sys.argv)
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH | GLUT_ALPHA)
    glutInitWindowSize(640, 480)
    glutInitWindowPosition(100, 200)
    win = glutCreateWindow('Precursors')
    glutReshapeFunc(resize)
    glutDisplayFunc(display)
    #glutIdleFunc(DrawGLScene)
    glutMouseWheelFunc(wheel)
    glutKeyboardFunc(keyb)
    glutMotionFunc(move)
    glutMouseFunc(mouse)
    InitGL()
    init()
    if MOUSE3d:
        sn = win32com.client.Dispatch("TDxInput.Device")
        print "SN = ", sn
        sn.Connect()
    # end if

    glutMainLoop()

# end def main


if __name__ == '__main__':

    main(sys.argv)

# end if