| 1 | package Lego::Ldraw::Display; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; no warnings qw/void uninitialized/; | ||
| 5 | |||
| 6 | use Carp; | ||
| 7 | |||
| 8 | use Math::Trig; | ||
| 9 | use Math::Trig ':radial'; | ||
| 10 | use Math::VectorReal; | ||
| 11 | |||
| 12 | use OpenGL qw/ :all /; | ||
| 13 | |||
| 14 | use Lego::Ldraw::Line; | ||
| 15 | use Lego::Ldraw; | ||
| 16 | |||
| 17 | my $self = {}; | ||
| 18 | |||
| 19 | ########################################################## | ||
| 20 | # stuff for playing around | ||
| 21 | ########################################################## | ||
| 22 | |||
| 23 | ########################################################## | ||
| 24 | # end of stuff for playing around | ||
| 25 | ########################################################## | ||
| 26 | |||
| 27 | sub new { | ||
| 28 | my $proto = shift; | ||
| 29 | my $class = ref $proto || $proto; | ||
| 30 | |||
| 31 | $self->{width} = shift || 300; | ||
| 32 | $self->{height} = shift || 300; | ||
| 33 | $self->{name} = 'LDraw OpenGL Display'; | ||
| 34 | |||
| 35 | my $ldraw = shift; | ||
| 36 | $self->{ldraw} = \$ldraw | ||
| 37 | if $ldraw; | ||
| 38 | |||
| 39 | $self->{changed} = undef; # whether model has been changed | ||
| 40 | |||
| 41 | $self->{camera} = [400, 0, -270]; | ||
| 42 | $self->{lookat} = [0, 0, 0]; | ||
| 43 | |||
| 44 | $self->{cameramode} = undef; # how camera is moved around | ||
| 45 | $self->{gl_init} = {}; # gl variables; | ||
| 46 | $self->{bindings} = {}; # bindings | ||
| 47 | |||
| 48 | $self->{light_ambient} = [ 0.5, 0.5, 0.5, 1.0 ]; | ||
| 49 | $self->{light_diffuse} = [ 1, 1, 1, 1.0 ]; | ||
| 50 | $self->{light_position} = [ 2000.0, 2000.0, 1000.0, 1.0 ]; | ||
| 51 | $self->{clearcolor} = [0.9, 0.9, 1, 0.0]; | ||
| 52 | |||
| 53 | $self->{specialkeypressed} = {}; | ||
| 54 | $self->{keypressed} = {}; | ||
| 55 | |||
| 56 | $self->{nostuds} = 1; | ||
| 57 | |||
| 58 | bless ($self, $class); | ||
| 59 | return $self; | ||
| 60 | } | ||
| 61 | |||
| 62 | ###################################################### | ||
| 63 | # start of field access functions | ||
| 64 | ###################################################### | ||
| 65 | |||
| 66 | sub camera { | ||
| 67 | my $self = shift; | ||
| 68 | if (@_) { $self->{camera} = [@_] } | ||
| 69 | return @{ $self->{camera} }; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub lookat { | ||
| 73 | my $self = shift; | ||
| 74 | if (@_) { $self->{lookat} = [@_] } | ||
| 75 | return @{ $self->{lookat} }; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub move_camera { | ||
| 79 | shift->move('camera', @_); | ||
| 80 | } | ||
| 81 | |||
| 82 | sub move { | ||
| 83 | my $self = shift; | ||
| 84 | my ($point, $how, $what, $qty) = @_; | ||
| 85 | for ($how) { | ||
| 86 | /^x/ && do { | ||
| 87 | $qty = $qty || 8; | ||
| 88 | for ($what) { | ||
| 89 | (/x/ || /1/) && do { $self->{$point}->[0] += $qty; }; | ||
| 90 | (/y/ || /2/) && do { $self->{$point}->[1] += $qty; }; | ||
| 91 | (/z/ || /3/) && do { $self->{$point}->[2] += $qty; }; | ||
| 92 | }; | ||
| 93 | last; | ||
| 94 | }; | ||
| 95 | /^s/ && do { | ||
| 96 | my ($x, $y, $z) = @{ $self->{$point} }; | ||
| 97 | $qty = $qty || 12; | ||
| 98 | my ($rho, $theta, $phi) = cartesian_to_spherical ($x, $z, $y); # rotate around y axis | ||
| 99 | for ($what) { | ||
| 100 | (/r/ || /1/) && do { $rho += $qty ; }; | ||
| 101 | (/t/ || /2/) && do { $qty = deg2rad($qty); $theta += $qty; }; | ||
| 102 | (/p/ || /3/) && do { $qty = deg2rad($qty); $phi += $qty; }; | ||
| 103 | }; | ||
| 104 | ($x, $z, $y) = spherical_to_cartesian($rho, $theta, $phi); | ||
| 105 | $self->{$point} = [ $x, $y, $z ]; | ||
| 106 | last; | ||
| 107 | }; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | ###################################################### | ||
| 112 | # end of field access functions | ||
| 113 | ###################################################### | ||
| 114 | |||
| 115 | sub load { | ||
| 116 | } | ||
| 117 | |||
| 118 | sub display { | ||
| 119 | my $ldraw; | ||
| 120 | $self->prepare_display; | ||
| 121 | unless (eval { $ldraw = ${ $self->{ldraw} }->copy }) { | ||
| 122 | glutSwapBuffers(); | ||
| 123 | return; | ||
| 124 | }; | ||
| 125 | glutSwapBuffers() unless $self->{count}; | ||
| 126 | |||
| 127 | my @parts = @{$ldraw}; | ||
| 128 | local $, = " "; local $\ = "\n"; | ||
| 129 | if (!$self->{count} || $self->{changed}) { | ||
| 130 | $ldraw->build_gl_tree; | ||
| 131 | } | ||
| 132 | |||
| 133 | for my $part (@parts) { | ||
| 134 | next unless $part->type; | ||
| 135 | if ($part->type == 1) { | ||
| 136 | $self->display_part($part); | ||
| 137 | } else { | ||
| 138 | $self->display_primitive($part); | ||
| 139 | } | ||
| 140 | } | ||
| 141 | glutSwapBuffers(); | ||
| 142 | $self->{count}++; | ||
| 143 | } | ||
| 144 | |||
| 145 | sub build_list { | ||
| 146 | shift; | ||
| 147 | my $part = shift; | ||
| 148 | unless (ref $part) { | ||
| 149 | $part = Lego::Ldraw::Line->new_from_part_name($part); | ||
| 150 | #$part->model(${$self->{ldraw}}); | ||
| 151 | } | ||
| 152 | my $lcolor = shift; $lcolor = $part->color unless defined $lcolor; | ||
| 153 | return if defined $self->{GL_LISTS}->{$part->name}->{$lcolor}; | ||
| 154 | |||
| 155 | my $data = $part->explode->display_struct; | ||
| 156 | |||
| 157 | #------------------------------------------- | ||
| 158 | # first, build lists for all colored parts | ||
| 159 | #------------------------------------------- | ||
| 160 | if ($data->{1}) { | ||
| 161 | for my $line (@{$data->{1}->{16}}) { | ||
| 162 | $self->build_list($line, $lcolor) | ||
| 163 | } | ||
| 164 | } | ||
| 165 | |||
| 166 | #------------------------------------------- | ||
| 167 | # then start generating the list | ||
| 168 | #------------------------------------------- | ||
| 169 | my $ln = glGenLists(1); | ||
| 170 | |||
| 171 | return unless $ln; | ||
| 172 | glNewList($ln, GL_COMPILE); | ||
| 173 | |||
| 174 | #------------------------------------------- | ||
| 175 | # with subparts... | ||
| 176 | #------------------------------------------- | ||
| 177 | if ($data->{1}) { | ||
| 178 | for my $color (keys %{$data->{1}}) { | ||
| 179 | for my $line (@{$data->{1}->{$color}}) { | ||
| 180 | my $col = $color == 16 ? $lcolor : $color; | ||
| 181 | glColor4f(gl_color($col)); | ||
| 182 | $self->display_part($line, $col); | ||
| 183 | } | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | #------------------------------------------- | ||
| 188 | # ...and primitives | ||
| 189 | #------------------------------------------- | ||
| 190 | for my $type (2, 3, 4) { | ||
| 191 | for my $color (keys %{$data->{$type}}) { | ||
| 192 | for my $line (@{$data->{$type}->{$color}}) { | ||
| 193 | $self->display_primitive($line); | ||
| 194 | } | ||
| 195 | } | ||
| 196 | } | ||
| 197 | glEndList(); | ||
| 198 | |||
| 199 | $self->{GL_LISTS}->{$part->name}->{16} = $ln; | ||
| 200 | return $ln; | ||
| 201 | } | ||
| 202 | |||
| 203 | sub display_part { | ||
| 204 | shift; | ||
| 205 | |||
| 206 | my $part = shift; | ||
| 207 | my $col = shift; $col = $part->color unless defined $col; | ||
| 208 | |||
| 209 | #------------------------------------------- | ||
| 210 | # first, check if a display list is | ||
| 211 | # defined, and define one if not so... | ||
| 212 | #------------------------------------------- | ||
| 213 | my $ln; | ||
| 214 | unless (defined $self->{GL_LISTS}->{$part->name}->{$part->color}) { | ||
| 215 | $self->{GL_LISTS}->{$part->name}->{$part->color} = $self->build_list($part); | ||
| 216 | } | ||
| 217 | $ln = $self->{GL_LISTS}->{$part->name}->{$part->color}; | ||
| 218 | |||
| 219 | #------------------------------------------- | ||
| 220 | # ...then call list | ||
| 221 | #------------------------------------------- | ||
| 222 | glPushMatrix(); | ||
| 223 | my @matrix = ($part->values(qw/a d g '' b e h '' c f i '' x y z/), 1); | ||
| 224 | glMultMatrixd_p(@matrix); | ||
| 225 | glCallList($ln); | ||
| 226 | glPopMatrix(); | ||
| 227 | } | ||
| 228 | |||
| 229 | sub display_primitive { | ||
| 230 | shift; | ||
| 231 | my $part = shift; | ||
| 232 | my $gl_type; | ||
| 233 | my @d = $part->values; | ||
| 234 | |||
| 235 | for ($part->type) { | ||
| 236 | /^5$/ && do { return; $gl_type = GL_LINES; @d = splice @d, 2, 6; last; }; | ||
| 237 | /^2$/ && do { return; $gl_type = GL_LINES; splice @d, 0, 2; last; }; | ||
| 238 | /^3$/ && do { $gl_type = GL_TRIANGLES; splice @d, 0, 2; last; }; | ||
| 239 | /^4$/ && do { $gl_type = GL_QUADS; splice @d, 0, 2; last; }; | ||
| 240 | } | ||
| 241 | |||
| 242 | glBegin($gl_type); | ||
| 243 | |||
| 244 | #----------------------------------------------- | ||
| 245 | # normals are supposed to be important for | ||
| 246 | # lighting, but I can't manage to make lighting | ||
| 247 | # work, and besides I get divisions by zero | ||
| 248 | #----------------------------------------------- | ||
| 249 | #my @normal = $part->normal; | ||
| 250 | #print @normal; | ||
| 251 | #glNormal3f(@normal) | ||
| 252 | # if (($part->type == 3) or ($part->type == 4)); | ||
| 253 | #----------------------------------------------- | ||
| 254 | # end of normal handling, here for the future | ||
| 255 | #----------------------------------------------- | ||
| 256 | |||
| 257 | while (@d) { | ||
| 258 | my @c = splice @d, 0, 3; | ||
| 259 | carp "Wrong part $part" unless (scalar @c == 3); | ||
| 260 | glVertex3f(@c); | ||
| 261 | } | ||
| 262 | glEnd(); | ||
| 263 | } | ||
| 264 | |||
| 265 | ########################################################## | ||
| 266 | # initialization etc. | ||
| 267 | ########################################################## | ||
| 268 | sub resize { | ||
| 269 | my ($width, $height) = @_; | ||
| 270 | |||
| 271 | # Let's not core dump, no matter what. | ||
| 272 | $height = 1 if ($height == 0); | ||
| 273 | |||
| 274 | glViewport(0, 0, $width, $height); | ||
| 275 | |||
| 276 | glMatrixMode(GL_PROJECTION); | ||
| 277 | glLoadIdentity(); | ||
| 278 | gluPerspective(45.0,$width/$height,0.1,4000.0); | ||
| 279 | |||
| 280 | glMatrixMode(GL_MODELVIEW); | ||
| 281 | |||
| 282 | $self->{width} = $width; | ||
| 283 | $self->{height} = $height; | ||
| 284 | } | ||
| 285 | |||
| 286 | sub _specialkey { | ||
| 287 | my $key = shift; | ||
| 288 | my $mod = glutGetModifiers(); | ||
| 289 | |||
| 290 | return unless defined $self->{specialkeypressed}->{$mod}; | ||
| 291 | return unless $self->{specialkeypressed}->{$mod}->{$key}; | ||
| 292 | |||
| 293 | my $sub = $self->{specialkeypressed}->{$mod}->{$key}; | ||
| 294 | &$sub($self); | ||
| 295 | } | ||
| 296 | |||
| 297 | sub bindspec { | ||
| 298 | shift; | ||
| 299 | my $key = shift; | ||
| 300 | my $sub = pop; | ||
| 301 | my $mod = shift || 0; | ||
| 302 | $self->{specialkeypressed}->{$mod}->{$key} = $sub; | ||
| 303 | } | ||
| 304 | |||
| 305 | sub init { | ||
| 306 | shift; | ||
| 307 | my $idlefunc = shift; | ||
| 308 | glutInit(); | ||
| 309 | glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); | ||
| 310 | glutInitWindowSize($self->{width}, $self->{height}); | ||
| 311 | |||
| 312 | $self->{id} = glutCreateWindow($self->{name}); | ||
| 313 | |||
| 314 | glutDisplayFunc(\&display); | ||
| 315 | glutIdleFunc( sub { &{$idlefunc}; &display } ); | ||
| 316 | glutReshapeFunc(\&resize); | ||
| 317 | glutSpecialFunc(\&_specialkey); | ||
| 318 | |||
| 319 | ourInit($self->{width}, $self->{height}); | ||
| 320 | glutMainLoop(); | ||
| 321 | } | ||
| 322 | |||
| 323 | sub ourInit { | ||
| 324 | my ($Width, $Height) = @_; | ||
| 325 | |||
| 326 | # Color to clear color buffer to. | ||
| 327 | glClearColor(@{$self->{clearcolor}}); | ||
| 328 | |||
| 329 | # Depth to clear depth buffer to; type of test. | ||
| 330 | glClearDepth(1.0); | ||
| 331 | glDepthFunc(GL_LESS); | ||
| 332 | |||
| 333 | # Enables Smooth Color Shading; try GL_FLAT for (lack of) fun. | ||
| 334 | glShadeModel(GL_SMOOTH); | ||
| 335 | glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); | ||
| 336 | |||
| 337 | # Load up the correct perspective matrix; using a callback directly. | ||
| 338 | resize($self->{width}, $self->{height}); | ||
| 339 | |||
| 340 | # Set up a light, turn it on. | ||
| 341 | glLightfv_p(GL_LIGHT1, GL_POSITION, @{$self->{light_position}}); | ||
| 342 | glLightfv_p(GL_LIGHT1, GL_AMBIENT, @{$self->{light_ambient}}); | ||
| 343 | glLightfv_p(GL_LIGHT1, GL_DIFFUSE, @{$self->{light_diffuse}}); | ||
| 344 | glEnable (GL_LIGHT1); | ||
| 345 | |||
| 346 | # A handy trick -- have surface material mirror the color. | ||
| 347 | |||
| 348 | glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE); | ||
| 349 | #glColorMaterial(GL_FRONT_AND_BACK, GL_SPECULAR); | ||
| 350 | glEnable(GL_COLOR_MATERIAL); | ||
| 351 | } | ||
| 352 | |||
| 353 | sub prepare_display { | ||
| 354 | shift; | ||
| 355 | glDisable(GL_TEXTURE_2D); | ||
| 356 | glDisable(GL_LIGHTING); | ||
| 357 | |||
| 358 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | ||
| 359 | glEnable(GL_BLEND); | ||
| 360 | glEnable(GL_DEPTH_TEST); | ||
| 361 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST); | ||
| 362 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | ||
| 363 | |||
| 364 | # Need to manipulate the ModelView matrix to move our model around. | ||
| 365 | glMatrixMode(GL_MODELVIEW); | ||
| 366 | |||
| 367 | # Reset to 0,0,0; no rotation, no scaling. | ||
| 368 | glLoadIdentity(); | ||
| 369 | |||
| 370 | # Move the object back from the screen. | ||
| 371 | glTranslatef(0.0, 0.0, 0); | ||
| 372 | # move the camera away | ||
| 373 | gluLookAt($self->camera, $self->lookat, 0, -1, 0); | ||
| 374 | |||
| 375 | # Clear the color and depth buffers. | ||
| 376 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | ||
| 377 | } | ||
| 378 | |||
| 379 | ########################################################## | ||
| 380 | # color handling | ||
| 381 | ########################################################## | ||
| 382 | |||
| 383 | my $color_matrix = | ||
| 384 | { | ||
| 385 | 117440511 => [qw/153 192 240 148/], | ||
| 386 | 0 => [qw/34 34 34/], | ||
| 387 | 3 => [qw//], | ||
| 388 | 431 => [qw//], | ||
| 389 | 379 => [qw/159 178 191/], | ||
| 390 | 10 => [qw/51 255 102/], | ||
| 391 | 33 => [qw/0 0 153/], | ||
| 392 | 46 => [qw/240 196 0/], | ||
| 393 | 32 => [qw//], | ||
| 394 | 335 => [qw/212 163 157/], | ||
| 395 | 378 => [qw/159 204 180/], | ||
| 396 | 462 => [qw//], | ||
| 397 | 5 => [qw/255 51 153/], | ||
| 398 | 42 => [qw/204 255 0/], | ||
| 399 | 382 => [qw/204 170 102/], | ||
| 400 | 383 => [qw/204 204 204/], | ||
| 401 | 418 => [qw/0 191 89/], | ||
| 402 | 495 => [qw/255 255 128/], | ||
| 403 | 6 => [qw/102 51 0/], | ||
| 404 | 14 => [qw/255 229 0/], | ||
| 405 | 503 => [qw/230 227 218/], | ||
| 406 | 4 => [qw/204 0 0/], | ||
| 407 | 373 => [qw/175 150 180/], | ||
| 408 | 1 => [qw/0 51 178/], | ||
| 409 | 9 => [qw/0 128 255/], | ||
| 410 | 3 => [qw/48 128 48/], | ||
| 411 | 5 => [qw/255 51 153/], | ||
| 412 | 334 => [qw/240 176 51/], | ||
| 413 | 12 => [qw/255 201 196/], | ||
| 414 | 11 => [qw/48 255 48/], | ||
| 415 | 494 => [qw/204 204 204/], | ||
| 416 | 2 => [qw/0 127 51/], | ||
| 417 | 383 => [qw/204 204 204/], | ||
| 418 | 36 => [qw/204 0 0/], | ||
| 419 | 15 => [qw/255 255 255/], | ||
| 420 | 41 => [qw/153 192 240/], | ||
| 421 | 34 => [qw/0 80 24/], | ||
| 422 | 13 => [qw/255 176 204/], | ||
| 423 | 47 => [qw/255 255 255/], | ||
| 424 | 7 => [qw/153 153 153/], | ||
| 425 | 8 => [qw/102 102 88/], | ||
| 426 | }; | ||
| 427 | |||
| 428 | |||
| 429 | sub gl_color { | ||
| 430 | my $col = shift; | ||
| 431 | my $linetype = shift; | ||
| 432 | |||
| 433 | $col = 0 unless defined $color_matrix->{$col}; | ||
| 434 | my @color = @{$color_matrix->{$col}}; $_ /= 256 for @color; | ||
| 435 | @color = (@color, 1) unless scalar @color == 4; | ||
| 436 | return (@color); | ||
| 437 | } | ||
| 438 | |||
| 439 | |||
| 440 | sub Lego::Ldraw::Line::gl_color { | ||
| 441 | my $self = shift; | ||
| 442 | my $ld = shift; # line or colour | ||
| 443 | my $tp = shift; # false if triang or quad (types 3 and 4) , true if line (types 2 and 5) | ||
| 444 | |||
| 445 | $ld = ref $ld ? $ld->color : $ld; | ||
| 446 | $tp = ref $ld ? (($ld->type != 3) && ($ld->type != 4)) : $tp; | ||
| 447 | |||
| 448 | } | ||
| 449 | |||
| 450 | sub Lego::Ldraw::build_gl_tree { | ||
| 451 | my $self = shift; | ||
| 452 | my $callback = sub { Lego::Ldraw::Display->build_list( shift ) }; | ||
| 453 | $self->build_tree($callback); | ||
| 454 | } | ||
| 455 | |||
| 456 | sub Lego::Ldraw::display_struct { | ||
| 457 | my $self = shift; | ||
| 458 | my $data; | ||
| 459 | |||
| 460 | for my $line (@$self) { | ||
| 461 | if ($line->type == 5) { | ||
| 462 | $line = $line->five2two | ||
| 463 | } | ||
| 464 | push @{$data->{$line->type}->{$line->color}}, $line | ||
| 465 | if $line->type; | ||
| 466 | } | ||
| 467 | return $data; | ||
| 468 | } | ||
| 469 | |||
| 470 | sub Lego::Ldraw::Line::normal { | ||
| 471 | my $self = shift; | ||
| 472 | return unless $self->type == 3 or $self->type == 4; | ||
| 473 | my @d = $self->values; | ||
| 474 | splice @d, 0, 2; | ||
| 475 | my ($a, $b, $c, $d, $n); | ||
| 476 | for ($a, $b, $c) { | ||
| 477 | $_ = vector(splice @d, 0, 3); | ||
| 478 | } | ||
| 479 | |||
| 480 | ($n, $d) = plane($a, $b, $c); | ||
| 481 | return $n->array; | ||
| 482 | } | ||
| 483 | |||
| 484 | sub Lego::Ldraw::Line::five2two { | ||
| 485 | my $self = shift; | ||
| 486 | return $self unless $self->type == 5; | ||
| 487 | |||
| 488 | $self = $self->copy; | ||
| 489 | $self->{type} = 2; | ||
| 490 | for (qw/x3 y3 z3 x4 y4 z4/) { | ||
| 491 | delete $self->{$_} | ||
| 492 | } | ||
| 493 | return $self; | ||
| 494 | } | ||
| 495 | |||
| 496 | 1; |