Class: OpenGlViewer

Inherits:
Object
  • Object
show all
Defined in:
lib/b1trackerdata/openglviewer.rb

Overview

include Gl,Glu,Glut

Constant Summary collapse

POS =
[0, 0, 0, 0.0]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ OpenGlViewer

Returns a new instance of OpenGlViewer.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/b1trackerdata/openglviewer.rb', line 15

def initialize data
  @data = data
  @base = NMatrix.float(4,4).unit
  @matrices = {}
  @matrices[:base] = @base
  @currenttimestamp = 0.0
  @current = 0
  @timestamps = {}
  @pause = true
  @forward = true
  @leftboh = "none"
  @rightboh = "none"
  filltimestamps
  glShadeModel(GL::SMOOTH)
  glNormal(0.0, 0.0, 1.0)
  glLightfv(GL::LIGHT0, GL::POSITION, POS)
  glEnable(GL::CULL_FACE)
  glEnable(GL::LIGHTING)
  glEnable(GL::LIGHT0)
  glEnable(GL::DEPTH_TEST)
  glEnable(GL::LINE_SMOOTH)
  glEnable(GL::POINT_SMOOTH)
  glClearColor(0.0, 0.0, 0.0, 0.0)
end

Instance Attribute Details

#currenttimestampObject

Returns the value of attribute currenttimestamp.



10
11
12
# File 'lib/b1trackerdata/openglviewer.rb', line 10

def currenttimestamp
  @currenttimestamp
end

#pauseObject

Returns the value of attribute pause.



10
11
12
# File 'lib/b1trackerdata/openglviewer.rb', line 10

def pause
  @pause
end

#timestampsObject

Returns the value of attribute timestamps.



10
11
12
# File 'lib/b1trackerdata/openglviewer.rb', line 10

def timestamps
  @timestamps
end

Instance Method Details

#displayObject

gl display method



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
# File 'lib/b1trackerdata/openglviewer.rb', line 79

def display
  fillmatrices
  setcurrenttimestamp
  fps_control(60)
  glClear(GL::COLOR_BUFFER_BIT | GL::DEPTH_BUFFER_BIT)
  glColor(0.0, 0.0, 0.0)
  glPushMatrix()
  glRotate(@view_rotx, 1.0, 0.0, 0.0)
  glRotate(@view_roty, 0.0, 1.0, 0.0)
  glRotate(@view_rotz, 0.0, 0.0, 1.0)
  @matrices.each { |k,v| draw_object k,v }
  displayinfo
  glPopMatrix()
  glutSwapBuffers()
  @frames = 0 if not defined? @frames
  @t0 = 0 if not defined? @t0
  @frames += 1
  t = GLUT.Get(GLUT::ELAPSED_TIME)
  if t - @t0 >= 5000
    seconds = (t - @t0) / 1000.0
    fps = @frames / seconds
    @t0, @frames = t, 0
    exit if defined? @autoexit and t >= 999.0 * @autoexit
  end
end

#displayinfoObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/b1trackerdata/openglviewer.rb', line 140

def displayinfo
  info = "Information:"
  info += " timestamp: #{(@timestamps[@current]).to_s}"
  info += " PAUSED" if @pause
  info += " FORWARD" if (!@pause && @forward)
  info += " REVERSE" if (!@pause && !@forward)
  info += " BOH left:#{@leftboh} right:#{@rightboh}"
  scale = 10
  scale2 = scale*scale
  glPushMatrix()
  glTranslate((scale),(scale),(scale))
  glScale(1.0/(scale2),1.0/(scale2),1.0/(scale2))
  info.each_byte { |x| glutStrokeCharacter(GLUT_STROKE_ROMAN, x) }
  glScale(scale2,scale2,scale2)
  glTranslate(-(scale),-(scale),-(scale))
  glPopMatrix()
end

#draw_object(key, matrix) ⇒ Object

draw an imd and its childs



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/b1trackerdata/openglviewer.rb', line 125

def draw_object key,matrix
  glPushMatrix()
  scale = 10
  translation_scale = 2
  glTranslate(matrix[3,0]*scale*translation_scale,matrix[3,1]*scale*translation_scale,matrix[3,2]*scale*translation_scale)
  glColor(1,0,0)
  drawLineWithText "#{key} x",scale,matrix[0,0],matrix[0,1],matrix[0,2]
  glColor(0,1,0)
  drawLineWithText "#{key} y",scale,matrix[1,0],matrix[1,1],matrix[1,2]
  glColor(0,0,1)
  drawLineWithText "#{key} z",scale,matrix[2,0],matrix[2,1],matrix[2,2]
  glTranslate(-matrix[3,0]*scale*translation_scale,-matrix[3,1]*scale*translation_scale,-matrix[3,2]*scale*translation_scale)
  glPopMatrix()
end

#drawLineWithText(text, scale, x, y, z) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/b1trackerdata/openglviewer.rb', line 112

def drawLineWithText text,scale,x,y,z
  scale2 = scale*scale
  glPushMatrix()
  drawOneLine(0,0,0,scale*x,scale*y,scale*z)
  glTranslate((scale*x),(scale*y),(scale*z))
  glScale(1.0/(scale2),1.0/(scale2),1.0/(scale2))
  text.each_byte { |x| glutStrokeCharacter(GLUT_STROKE_ROMAN, x) }
  glScale(scale2,scale2,scale2)
  glTranslate(-(scale*x),-(scale*y),-(scale*z))
  glPopMatrix()
end

#drawOneLine(x1, y1, z1, x2, y2, z2) ⇒ Object



105
106
107
108
109
110
# File 'lib/b1trackerdata/openglviewer.rb', line 105

def drawOneLine x1,y1,z1,x2,y2,z2
  glBegin(GL_LINES)
  glVertex(x1,y1,z1)
  glVertex(x2,y2,z2)
  glEnd()
end

#fillmatricesObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/b1trackerdata/openglviewer.rb', line 50

def fillmatrices
  unless @data[:left][@currenttimestamp].nil?
    #@matrices[:reference] = @data[:left][@currenttimestamp][:reference]
    #@matrices[:left_hand_data] = @data[:left][@currenttimestamp][:data]
    @matrices[:left_hand_relative] = @data[:left][@currenttimestamp][:relative]
    @leftboh = @data[:left][@currenttimestamp][:boh]
  end
  unless @data[:right][@currenttimestamp].nil?
    #@matrices[:reference] = @data[:right][@currenttimestamp][:reference]
    #@matrices[:right_hand_data] = @data[:right][@currenttimestamp][:data]
    @matrices[:right_hand_relative] = @data[:right][@currenttimestamp][:relative]
    @rightboh = @data[:right][@currenttimestamp][:boh]
  end
end

#filltimestampsObject



40
41
42
43
44
45
46
47
48
# File 'lib/b1trackerdata/openglviewer.rb', line 40

def filltimestamps
  i = 0
  @data[:reference].each do |timestamp,value|
    @timestamps[i] = timestamp
    i += 1
  end
  ilog "Found '#{@timestamps.count}'"
  ilog "Will now try to display '#{@data[:left].count}' left and '#{@data[:right].count}' right hand values"
end

#fps_control(fps) ⇒ Object



158
159
160
161
162
163
164
# File 'lib/b1trackerdata/openglviewer.rb', line 158

def fps_control(fps)
  t = GLUT.Get(GLUT::ELAPSED_TIME)
  @t0_idle = t if !defined? @t0_idle
  time_to_sleep = (1000./fps) - (t - @t0_idle)
  sleep(time_to_sleep.to_f/1000) if time_to_sleep > 0
  @t0_idle = t
end

#idleObject



177
178
179
# File 'lib/b1trackerdata/openglviewer.rb', line 177

def idle
  GLUT.PostRedisplay()
end

#keyboard(key, x, y) ⇒ Object

keyboard control handling



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/b1trackerdata/openglviewer.rb', line 226

def keyboard(key, x, y)
  case (key)
  when ?s
    @znear = @znear - 1
  when ?w
    @znear = @znear + 1
  when ?a
    @eyex = @eyex - 1
  when ?d
    @eyex = @eyex + 1
  when ?f
    @forward = true
  when ?r
    @forward = false
  when ?p
    togglepause
  when 27 # Escape
    exit
  end
  setViewpoint
end

#motion(x, y) ⇒ Object

motion control



216
217
218
219
220
221
222
223
# File 'lib/b1trackerdata/openglviewer.rb', line 216

def motion x, y
  if @mouse == GLUT::DOWN then
    @view_rotz += @x0 - x
    @view_roty += @y0 - y
    setViewpoint
  end
  @x0, @y0 = x, y
end

#mouse(button, state, x, y) ⇒ Object

mouse ;)



204
205
206
207
208
209
210
211
212
213
# File 'lib/b1trackerdata/openglviewer.rb', line 204

def mouse button, state, x, y
  changed = false
  case button
  when 3 then @eyex -= 5; changed = true;
  when 4 then @eyex += 5; changed = true
  end
  setViewpoint if changed
  @mouse = state
  @x0, @y0 = x, y
end

#reshape(width, height) ⇒ Object

gets called when window is resized etc.



167
168
169
170
171
172
173
174
175
# File 'lib/b1trackerdata/openglviewer.rb', line 167

def reshape width, height
  h = height.to_f / width.to_f
  glViewport(0, 0, width, height)
  glMatrixMode(GL::PROJECTION)
  glLoadIdentity()
  glFrustum(-1.0, 1.0, -h, h, 1.0, 1000.0)
  glMatrixMode(GL::MODELVIEW)
  setViewpoint
end

#runObject

the loop



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/b1trackerdata/openglviewer.rb', line 257

def run
  @znear = 30
  @eyex = 0
  @view_rotx, @view_roty, @view_rotz = 0, 0, 0
  glutInit
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB)
  glutInitWindowSize(640, 480)
  glutInitWindowPosition(0, 0)
  glutCreateWindow($0)
  glutDisplayFunc(method(:display).to_proc)
  glutReshapeFunc(method(:reshape).to_proc)
  glutSpecialFunc(method(:special).to_proc)
  glutKeyboardFunc(method(:keyboard).to_proc)
  glutMouseFunc(method(:mouse).to_proc)
  glutMotionFunc(method(:motion).to_proc)
  glutVisibilityFunc(method(:visible).to_proc)
  glShadeModel(GL::SMOOTH)
  glNormal(0.0, 0.0, 1.0)
  glClearColor(0.0, 0.0, 0.0, 0.0)
  glutMainLoop()
end

#setcurrenttimestampObject



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/b1trackerdata/openglviewer.rb', line 65

def setcurrenttimestamp
  unless @pause
    if @forward
      @current += 1
      @currenttimestamp = @timestamps[@current]
    end
    if !@forward
      @current -= 1 unless (@current.eql? 0)
      @currenttimestamp = @timestamps[@current]
    end
  end
end

#setViewpointObject



181
182
183
184
185
186
# File 'lib/b1trackerdata/openglviewer.rb', line 181

def setViewpoint
  glMatrixMode(GL_MODELVIEW)
  glLoadIdentity()
  gluLookAt(@eyex, 0, @znear, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
  glutPostRedisplay()
end

#special(k, x, y) ⇒ Object

Change view angle



189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/b1trackerdata/openglviewer.rb', line 189

def special (k, x, y)
  case k
  when GLUT::KEY_UP
    @view_rotx += 5.0
  when GLUT::KEY_DOWN
    @view_rotx -= 5.0
  when GLUT::KEY_LEFT
    @view_roty += 5.0
  when GLUT::KEY_RIGHT
    @view_roty -= 5.0
  end
  GLUT.PostRedisplay()
end

#togglepauseObject



248
249
250
251
# File 'lib/b1trackerdata/openglviewer.rb', line 248

def togglepause
  @pause = !@pause
  puts "Toggle pause now '#{@pause}'"
end

#visible(vis) ⇒ Object



253
254
255
# File 'lib/b1trackerdata/openglviewer.rb', line 253

def visible(vis)
  GLUT.IdleFunc((vis == GLUT::VISIBLE ? method(:idle).to_proc : nil))
end