4
# Copyright (C) 2010, 2011 Levi D. Smith
6
# TetraCity is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
11
# TetraCity is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with TetraCity. If not, see <http://www.gnu.org/licenses/gpl.txt>.
21
LIGHT_SPEED = 0.05 #the speed at which the flashlight moves
23
class BuildingViewerOptions
38
@rotate_toggle = false
39
@light_control_toggle = false
41
@x_translate_vel = 0.0
42
@y_translate_vel = 0.0
43
@z_translate_vel = 0.0
131
attr_accessor :x_translate
132
attr_accessor :y_translate
133
attr_accessor :z_translate
134
attr_accessor :x_rotate
135
attr_accessor :y_rotate
136
attr_accessor :z_rotate
137
attr_accessor :x_light
138
attr_accessor :y_light
139
attr_accessor :z_light
141
attr_accessor :rotate_toggle
142
attr_accessor :light_control_toggle
144
attr_accessor :x_translate_vel
145
attr_accessor :y_translate_vel
146
attr_accessor :z_translate_vel
147
attr_accessor :x_rotate_vel
148
attr_accessor :y_rotate_vel
149
attr_accessor :z_rotate_vel
150
attr_accessor :x_light_vel
151
attr_accessor :y_light_vel
152
attr_accessor :z_light_vel
156
$theBuildingViewerOptions = BuildingViewerOptions.new()
159
def buildingViewerLoop
169
while event = SDL::Event2.poll
171
when SDL::Event::KeyUp
173
$theBuildingViewerOptions.x_rotate_vel = 0.0
174
$theBuildingViewerOptions.y_rotate_vel = 0.0
175
$theBuildingViewerOptions.z_rotate_vel = 0.0
177
$theBuildingViewerOptions.x_translate_vel = 0.0
178
$theBuildingViewerOptions.y_translate_vel = 0.0
179
$theBuildingViewerOptions.z_translate_vel = 0.0
181
$theBuildingViewerOptions.x_light_vel = 0.0
182
$theBuildingViewerOptions.y_light_vel = 0.0
183
$theBuildingViewerOptions.z_light_vel = 0.0
186
when SDL::Event::KeyDown
191
if ($theBuildingViewerOptions.rotate_toggle)
192
$theBuildingViewerOptions.x_rotate_vel -= 1
193
elsif ($theBuildingViewerOptions.light_control_toggle)
194
$theBuildingViewerOptions.z_light_vel -= LIGHT_SPEED
196
$theBuildingViewerOptions.z_translate_vel -= 0.05
203
if ($theBuildingViewerOptions.rotate_toggle)
204
$theBuildingViewerOptions.x_rotate_vel += 1
205
elsif ($theBuildingViewerOptions.light_control_toggle)
206
$theBuildingViewerOptions.z_light_vel += LIGHT_SPEED
208
$theBuildingViewerOptions.z_translate_vel += 0.05
215
if ($theBuildingViewerOptions.rotate_toggle)
216
$theBuildingViewerOptions.y_rotate_vel -= 1
217
elsif ($theBuildingViewerOptions.light_control_toggle)
218
$theBuildingViewerOptions.x_light_vel -= LIGHT_SPEED
220
$theBuildingViewerOptions.x_translate_vel -= 0.05
228
if ($theBuildingViewerOptions.rotate_toggle)
229
$theBuildingViewerOptions.y_rotate_vel += 1
230
elsif ($theBuildingViewerOptions.light_control_toggle)
231
$theBuildingViewerOptions.x_light_vel += LIGHT_SPEED
234
$theBuildingViewerOptions.x_translate_vel += 0.05
240
if ($theBuildingViewerOptions.rotate_toggle)
241
$theBuildingViewerOptions.z_rotate_vel -= 1
242
elsif ($theBuildingViewerOptions.light_control_toggle)
243
$theBuildingViewerOptions.y_light_vel -= LIGHT_SPEED
246
$theBuildingViewerOptions.y_translate_vel -= 0.05
252
if ($theBuildingViewerOptions.rotate_toggle)
253
$theBuildingViewerOptions.z_rotate_vel += 1
255
elsif ($theBuildingViewerOptions.light_control_toggle)
256
$theBuildingViewerOptions.y_light_vel += LIGHT_SPEED
259
$theBuildingViewerOptions.y_translate_vel += 0.05
262
when SDL::Key::BACKSPACE
271
if ($viewingObject > 8)
275
puts "Viewing object #{$viewingObject}"
277
if ($viewingObject == 0)
279
elsif ($viewingObject == 1)
281
elsif ($viewingObject == 2)
283
elsif ($viewingObject == 3)
285
elsif ($viewingObject == 4)
287
elsif ($viewingObject == 5)
289
elsif ($viewingObject == 6)
291
elsif ($viewingObject == 7)
293
elsif ($viewingObject == 8)
300
$theBuildingViewerOptions.rotate_toggle = true
301
$theBuildingViewerOptions.light_control_toggle = false
305
$theBuildingViewerOptions.rotate_toggle = false
306
$theBuildingViewerOptions.light_control_toggle = true
310
$theBuildingViewerOptions.rotate_toggle = false
311
$theBuildingViewerOptions.light_control_toggle = false
315
puts "Display coordinates"
317
puts "translate: #{$theBuildingViewerOptions.x_translate}, #{$theBuildingViewerOptions.y_translate}, #{$theBuildingViewerOptions.z_translate}"
318
puts "rotate: #{$theBuildingViewerOptions.x_rotate}, #{$theBuildingViewerOptions.y_rotate}, #{$theBuildingViewerOptions.z_rotate}"
319
puts "light: #{$theBuildingViewerOptions.x_light}, #{$theBuildingViewerOptions.y_light}, #{$theBuildingViewerOptions.z_light}"
337
$theBuildingViewerOptions.x_translate = 0.0
338
$theBuildingViewerOptions.y_translate = 0.0
339
$theBuildingViewerOptions.z_translate = -22.0
341
$theBuildingViewerOptions.x_rotate = 33.0
342
$theBuildingViewerOptions.y_rotate = 33.0
343
$theBuildingViewerOptions.z_rotate = 0.0
345
$theBuildingViewerOptions.x_light = 6.0
346
$theBuildingViewerOptions.y_light = 0.0
347
$theBuildingViewerOptions.z_light = 0.0
351
def updateCoordinates
352
$theBuildingViewerOptions.x_translate += $theBuildingViewerOptions.x_translate_vel
353
$theBuildingViewerOptions.y_translate += $theBuildingViewerOptions.y_translate_vel
354
$theBuildingViewerOptions.z_translate += $theBuildingViewerOptions.z_translate_vel
356
$theBuildingViewerOptions.x_rotate += $theBuildingViewerOptions.x_rotate_vel
357
$theBuildingViewerOptions.y_rotate += $theBuildingViewerOptions.y_rotate_vel
358
$theBuildingViewerOptions.z_rotate += $theBuildingViewerOptions.z_rotate_vel
360
$theBuildingViewerOptions.x_light += $theBuildingViewerOptions.x_light_vel
361
$theBuildingViewerOptions.y_light += $theBuildingViewerOptions.y_light_vel
362
$theBuildingViewerOptions.z_light += $theBuildingViewerOptions.z_light_vel
366
def initBuildingViewer
367
glViewport( 0, 0, 1024, 768 );
368
glMatrixMode( GL_PROJECTION );
371
gluPerspective(45.0, 1024.0/768.0, 0.1, 50.0)
373
glMatrixMode( GL_MODELVIEW );
376
glEnable(GL_DEPTH_TEST);
378
glDepthFunc(GL_LESS);
380
glShadeModel(GL_SMOOTH);
384
ambientLight = [0.2, 0.2, 0.2, 1.0]
385
diffuseLight = [0.8, 0.8, 0.8, 1.0]
386
specularLight = [0.5, 0.5, 0.5, 1.0]
388
position = [ 0.0, 0.0, 8.0, 1.0]
390
glLight(GL_LIGHT0, GL_AMBIENT, ambientLight)
391
glLight(GL_LIGHT0, GL_DIFFUSE, diffuseLight)
392
glLight(GL_LIGHT0, GL_SPECULAR, specularLight)
394
glEnable(GL_COLOR_MATERIAL)
395
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
400
def drawBuildingViewer
401
useMultipleColors = false
414
glClearColor(0.0, 0.0, 0.0, 1.0);
415
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
419
position = [$theBuildingViewerOptions.x_light, $theBuildingViewerOptions.y_light, $theBuildingViewerOptions.z_light]
421
glLight(GL_LIGHT0, GL_POSITION, position)
425
glTranslate($theBuildingViewerOptions.x_translate, $theBuildingViewerOptions.y_translate, $theBuildingViewerOptions.z_translate)
426
glRotate($theBuildingViewerOptions.x_rotate, 1.0, 0.0, 0.0)
427
glRotate($theBuildingViewerOptions.y_rotate, 0.0, 1.0, 0.0)
428
glRotate($theBuildingViewerOptions.z_rotate, 0.0, 0.0, 1.0)
432
glEnable(GL_LIGHTING)
435
glTranslate(-1.0, 0.0, 0.0)
436
quadratic = gluNewQuadric()
437
gluSphere(quadratic, 1.3, 16, 16)
439
glTranslate(5.0, 0.0, 0.0)
441
buildingColor = [1.0, 1.0, 1.0]
443
if ($viewingObject == 0)
445
theDisplayList = $displayListR1
446
buildingColor = [1.0, 0.0, 0.0]
447
elsif ($viewingObject == 1)
449
theDisplayList = $displayListR2
450
buildingColor = [1.0, 0.0, 0.0]
451
elsif ($viewingObject == 2)
453
theDisplayList = $displayListR3
454
buildingColor = [1.0, 0.0, 0.0]
455
elsif ($viewingObject == 3)
457
theDisplayList = $displayListC1
458
buildingColor = [0.0, 0.0, 1.0]
459
elsif ($viewingObject == 4)
461
theDisplayList = $displayListC2
462
buildingColor = [0.0, 0.0, 1.0]
463
elsif ($viewingObject == 5)
465
theDisplayList = $displayListC3
466
buildingColor = [0.0, 0.0, 1.0]
467
elsif ($viewingObject == 6)
469
theDisplayList = $displayListI1
470
buildingColor = [1.0, 1.0, 0.0]
471
elsif ($viewingObject == 7)
473
theDisplayList = $displayListI2
474
buildingColor = [1.0, 1.0, 0.0]
475
elsif ($viewingObject == 8)
477
theDisplayList = $displayListI3
478
buildingColor = [1.0, 1.0, 0.0]
481
theDisplayList = $displayListR1
486
glColor(buildingColor)
487
glCallList(theDisplayList)
490
glDisable(GL_LIGHTING)
492
glMatrixMode(GL_MODELVIEW);
498
def drawBuilding(theModel, xTranslate, yTranslate, zTranslate)
501
glTranslate(xTranslate, yTranslate, zTranslate)
503
for iModelFace in 0 .. (theModel.faceVertexArray.length - 1)
508
for iVertexIndex in 0 .. (theModel.faceVertexArray[iModelFace].length - 1)
510
iVertex = theModel.faceVertexArray[iModelFace][iVertexIndex]
511
iNormal = theModel.faceNormalArray[iModelFace][iVertexIndex]
513
glNormal(theModel.normalArray[iNormal][0], theModel.normalArray[iNormal][1], theModel.normalArray[iNormal][2])
514
glVertex(theModel.vertexArray[iVertex][0], theModel.vertexArray[iVertex][1], theModel.vertexArray[iVertex][2])
526
def calculateNormal(p1, p2, p3)
528
normalVector = [0.0, 0.0, 0.0]
530
v1 = [ p2[0] - p1[0], p2[1] - p1[1], p2[2] - p1[2] ]
531
v2 = [ p3[0] - p1[0], p3[1] - p1[1], p3[2] - p1[2] ]
535
crossX = (v1[1] * v2[2]) - (v1[2] * v2[1])
536
crossY = -((v2[2] * v1[0]) - (v2[0] * v1[2]))
537
crossZ = (v1[0] * v2[1]) - (v1[1] * v2[0])
539
normal = [crossX, crossY, crossZ]
541
normalFactor = ( (crossX ** 2) + (crossY ** 2) + (crossZ ** 2)) ** 0.5
543
if (normalFactor != 0.0)
544
normal[0] = normal[0] / normalFactor
545
normal[1] = normal[1] / normalFactor
546
normal[2] = normal[2] / normalFactor
553
def drawCoordinatesText()
559
colorFgSDL = [255, 255, 255]
560
colorBkgSDL = [192, 192, 192]
562
glOrtho(0, 1024, 768, 0, -1, 1)
563
glColor(1.0, 1.0, 1.0)
565
colorBkgSDL = [192, 192, 192]
566
if (!$theBuildingViewerOptions.rotate_toggle && !$theBuildingViewerOptions.light_control_toggle)
567
colorBkgSDL = [0, 0, 255]
570
drawMenuText( "TranslateX: #{$theBuildingViewerOptions.x_translate}", 0, (16 * 0), 128, 16, colorFgSDL, colorBkgSDL )
571
drawMenuText( "TranslateY: #{$theBuildingViewerOptions.y_translate}", 0, (16 * 1), 128, 16, colorFgSDL, colorBkgSDL )
572
drawMenuText( "TranslateZ: #{$theBuildingViewerOptions.z_translate}", 0, (16 * 2), 128, 16, colorFgSDL, colorBkgSDL )
574
colorBkgSDL = [192, 192, 192]
575
if ($theBuildingViewerOptions.rotate_toggle)
576
colorBkgSDL = [0, 0, 255]
579
drawMenuText( "RotateX: #{$theBuildingViewerOptions.x_rotate}", 0, (16 * 3), 128, 16, colorFgSDL, colorBkgSDL )
580
drawMenuText( "RotateY: #{$theBuildingViewerOptions.y_rotate}", 0, (16 * 4), 128, 16, colorFgSDL, colorBkgSDL )
581
drawMenuText( "RotateZ: #{$theBuildingViewerOptions.z_rotate}", 0, (16 * 5), 128, 16, colorFgSDL, colorBkgSDL )
583
colorBkgSDL = [192, 192, 192]
584
if (!$theBuildingViewerOptions.rotate_toggle && $theBuildingViewerOptions.light_control_toggle)
585
colorBkgSDL = [0, 0, 255]
588
drawMenuText( "LightX: #{$theBuildingViewerOptions.x_light}", 0, (16 * 6), 128, 16, colorFgSDL, colorBkgSDL )
589
drawMenuText( "LightY: #{$theBuildingViewerOptions.y_light}", 0, (16 * 7), 128, 16, colorFgSDL, colorBkgSDL )
590
drawMenuText( "LightZ: #{$theBuildingViewerOptions.z_light}", 0, (16 * 8), 128, 16, colorFgSDL, colorBkgSDL )
598
glColor(1.0, 1.0, 0.0)
599
glVertex($theBuildingViewerOptions.x_light, $theBuildingViewerOptions.y_light + 0.1, $theBuildingViewerOptions.z_light)
600
glVertex($theBuildingViewerOptions.x_light, $theBuildingViewerOptions.y_light, $theBuildingViewerOptions.z_light)
601
glVertex($theBuildingViewerOptions.x_light + 0.1, $theBuildingViewerOptions.y_light, $theBuildingViewerOptions.z_light)
602
glVertex($theBuildingViewerOptions.x_light + 0.1, $theBuildingViewerOptions.y_light + 0.1, $theBuildingViewerOptions.z_light)
604
glColor(1.0, 1.0, 1.0)
605
glVertex($theBuildingViewerOptions.x_light, $theBuildingViewerOptions.y_light + 0.1, $theBuildingViewerOptions.z_light - 0.1)
606
glVertex($theBuildingViewerOptions.x_light, $theBuildingViewerOptions.y_light, $theBuildingViewerOptions.z_light - 0.1)
607
glVertex($theBuildingViewerOptions.x_light + 0.1, $theBuildingViewerOptions.y_light, $theBuildingViewerOptions.z_light - 0.1)
608
glVertex($theBuildingViewerOptions.x_light + 0.1, $theBuildingViewerOptions.y_light + 0.1, $theBuildingViewerOptions.z_light - 0.1)