| 1 | package OpenGL::Simple::Viewer; | ||
| 2 | |||
| 3 | 1 | ▶ | use 5.006001; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | $^W=1; | ||
| 6 | 0 | ▶ | use OpenGL::Simple ':all'; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | use OpenGL::Simple::GLUT ':all'; | ||
| 8 | use Math::Quaternion; | ||
| 9 | |||
| 10 | require Exporter; | ||
| 11 | |||
| 12 | our @ISA = qw(Exporter); | ||
| 13 | |||
| 14 | # Items to export into callers namespace by default. Note: do not export | ||
| 15 | # names by default without a very good reason. Use EXPORT_OK instead. | ||
| 16 | # Do not simply export all your public functions/methods/constants. | ||
| 17 | |||
| 18 | # This allows declaration use OpenGL::Simple::Viewer ':all'; | ||
| 19 | # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK | ||
| 20 | # will save memory. | ||
| 21 | our %EXPORT_TAGS = ( | ||
| 22 | 'all' => [ qw( | ||
| 23 | basic_mousefunc | ||
| 24 | basic_reshapefunc | ||
| 25 | basic_motionfunc | ||
| 26 | basic_displayfunc | ||
| 27 | postredisplay | ||
| 28 | ) ], | ||
| 29 | 'basic' => [ qw( | ||
| 30 | basic_mousefunc | ||
| 31 | basic_reshapefunc | ||
| 32 | basic_motionfunc | ||
| 33 | basic_displayfunc | ||
| 34 | postredisplay | ||
| 35 | )], | ||
| 36 | |||
| 37 | ); | ||
| 38 | |||
| 39 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | ||
| 40 | |||
| 41 | our @EXPORT = qw( | ||
| 42 | |||
| 43 | ); | ||
| 44 | |||
| 45 | our $VERSION = '0.03'; | ||
| 46 | |||
| 47 | |||
| 48 | # These variables are for the old-style basic_* functions. | ||
| 49 | |||
| 50 | our ($clickx, $clicky, %buttonstate) = (0,0, | ||
| 51 | GLUT_LEFT_BUTTON(), 0, GLUT_MIDDLE_BUTTON(), 0, GLUT_RIGHT_BUTTON(), 0 ); | ||
| 52 | |||
| 53 | our $orientation = new Math::Quaternion; | ||
| 54 | our $mousescale = 0.01; | ||
| 55 | our $zoomscale = 0.1; | ||
| 56 | our ($geomx, $geomy, $geomz) = (0,0,0); | ||
| 57 | my $sphererad; | ||
| 58 | |||
| 59 | # Let's dump the old functions here as well. | ||
| 60 | |||
| 61 | sub basic_reshapefunc { | ||
| 62 | my ( $screenx, $screeny ) = @_; | ||
| 63 | $sphererad = $screeny * 0.5; | ||
| 64 | glViewport( 0, 0, $screenx, $screeny ); | ||
| 65 | } | ||
| 66 | |||
| 67 | sub basic_mousefunc { | ||
| 68 | my ( $button, $state, $x, $y ) = @_; | ||
| 69 | |||
| 70 | ( $clickx, $clicky ) = ( $x, $y ); | ||
| 71 | $buttonstate{$button} = ( GLUT_DOWN() == $state ) ? 1 : 0; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub basic_motionfunc { | ||
| 75 | my ( $x, $y ) = @_; | ||
| 76 | my ( $left, $mid, $right ) = | ||
| 77 | @buttonstate{ GLUT_LEFT_BUTTON(), GLUT_MIDDLE_BUTTON(), GLUT_RIGHT_BUTTON() }; | ||
| 78 | |||
| 79 | if ($left) { basic_mouserotatemotion( $clickx, $clicky, $x, $y ); } | ||
| 80 | elsif ($mid) { basic_mousezoommotion( $y - $clicky ); } | ||
| 81 | elsif ($right) { basic_mousetransmotion( $clickx, $clicky, $x, $y ); } | ||
| 82 | ( $clickx, $clicky ) = ( $x, $y ); | ||
| 83 | } | ||
| 84 | |||
| 85 | sub postredisplay { glutPostRedisplay(); } | ||
| 86 | |||
| 87 | sub basic_mouserotatemotion { | ||
| 88 | my ( $x0, $y0, $x1, $y1 ) = @_; | ||
| 89 | |||
| 90 | my $s = $sphererad; | ||
| 91 | my $my = $x1 - $x0; my $mx = $y1 - $y0; | ||
| 92 | my $m = sqrt( $mx * $mx + $my * $my ); | ||
| 93 | |||
| 94 | my $theta; | ||
| 95 | if ( ( $m > 0 ) && ( $m < $s ) ) { | ||
| 96 | $theta = $m / $s; | ||
| 97 | |||
| 98 | $mx /= $m; | ||
| 99 | $my /= $m; | ||
| 100 | |||
| 101 | my $rotquat = Math::Quaternion::rotation( $theta, $mx, $my, 0.0 ); | ||
| 102 | $orientation = $rotquat * $orientation; | ||
| 103 | } | ||
| 104 | |||
| 105 | postredisplay; | ||
| 106 | } | ||
| 107 | |||
| 108 | sub basic_mousetransmotion { | ||
| 109 | my ( $x0, $y0, $x1, $y1 ) = @_; | ||
| 110 | $geomx += $mousescale * ( $x1 - $x0 ); | ||
| 111 | $geomy += $mousescale * ( $y0 - $y1 ); | ||
| 112 | postredisplay; | ||
| 113 | } | ||
| 114 | |||
| 115 | sub basic_mousezoommotion { $geomz -= $zoomscale * shift; postredisplay; } | ||
| 116 | |||
| 117 | sub drawback { glClear( GL_COLOR_BUFFER_BIT() | GL_DEPTH_BUFFER_BIT() ); } | ||
| 118 | |||
| 119 | sub basic_displayfunc { | ||
| 120 | my $callback = shift; | ||
| 121 | return sub { | ||
| 122 | drawback(); | ||
| 123 | |||
| 124 | # Set up perspective projection | ||
| 125 | glMatrixMode(GL_PROJECTION()); | ||
| 126 | glLoadIdentity(); | ||
| 127 | gluPerspective( 45.0, 1.0, 0.1, 100.0 ); | ||
| 128 | glMatrixMode(GL_MODELVIEW()); | ||
| 129 | glLoadIdentity(); | ||
| 130 | |||
| 131 | # Position and orient geometry | ||
| 132 | |||
| 133 | glTranslate( $geomx, $geomy, $geomz ); | ||
| 134 | my @m = $orientation->matrix4x4; | ||
| 135 | glMultMatrix(@m); | ||
| 136 | $callback->(); | ||
| 137 | glFlush(); | ||
| 138 | glutSwapBuffers(); | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | =head1 NAME | ||
| 143 | |||
| 144 | OpenGL::Simple::Viewer - Simple 3D geometry viewer using GLUT | ||
| 145 | |||
| 146 | =head1 SYNOPSIS | ||
| 147 | |||
| 148 | use OpenGL::Simple::Viewer; | ||
| 149 | use OpenGL::Simple::GLUT qw(:all); | ||
| 150 | |||
| 151 | glutInit; | ||
| 152 | |||
| 153 | my $v = new OpenGL::Simple::Viewer( | ||
| 154 | draw_geometry => sub { glutSolidTeapot(1.0); } | ||
| 155 | ); | ||
| 156 | |||
| 157 | glutMainLoop; | ||
| 158 | |||
| 159 | |||
| 160 | =head1 ABSTRACT | ||
| 161 | |||
| 162 | This module uses OpenGL::Simple and OpenGL::Simple::GLUT to provide a | ||
| 163 | quick and simple geometry viewer. If you just want to view a single | ||
| 164 | biomolecule, or throw some polygons at the screen and make sure they | ||
| 165 | come out looking OK, then this module might be for you. If you want to | ||
| 166 | write a first-person-shooter or comprehensive visualization toolkit, | ||
| 167 | this module is probably not for you. | ||
| 168 | |||
| 169 | =head1 DESCRIPTION | ||
| 170 | |||
| 171 | This package provides a simple OpenGL geometry viewer, through the GLUT | ||
| 172 | library. An instance of OpenGL::Simple::Viewer opens a GLUT window, and | ||
| 173 | renders some geometry provided through a callback subroutine; the | ||
| 174 | geometry can be rotated, translated, and zoomed using the mouse. | ||
| 175 | |||
| 176 | When the viewer moves around, the window must be redrawn; this usually | ||
| 177 | entails clearing the window, redrawing the background, setting the | ||
| 178 | correct position and orientation, and then drawing the geometry. By | ||
| 179 | default, all you need to supply is a subroutine which draws the | ||
| 180 | geometry; everything else is taken care of. User-defined backgrounds can | ||
| 181 | be set through a callback. | ||
| 182 | |||
| 183 | An OpenGL::Simple::Viewer object can be treated as a hashref with | ||
| 184 | several user-adjustable properties: | ||
| 185 | |||
| 186 | =over 1 | ||
| 187 | |||
| 188 | =item B<position> | ||
| 189 | |||
| 190 | This is a reference to an array of three numbers, corresponding to the | ||
| 191 | position of the viewer with respect to the geometry in 3D space. | ||
| 192 | |||
| 193 | =item B<orientation> | ||
| 194 | |||
| 195 | This is a Math::Quaternion representing the orientation of the geometry. | ||
| 196 | |||
| 197 | =item B<translatescale>,B<zoomscale> | ||
| 198 | |||
| 199 | These control translation and zooming speeds. | ||
| 200 | |||
| 201 | =back | ||
| 202 | |||
| 203 | =head1 METHODS | ||
| 204 | |||
| 205 | =cut | ||
| 206 | |||
| 207 | =over 1 | ||
| 208 | |||
| 209 | =item B<new> | ||
| 210 | |||
| 211 | my $v = new OpenGL::Simple::Viewer; # Should Just Work. | ||
| 212 | |||
| 213 | my $v2 = new OpenGL::Simple::Viewer( | ||
| 214 | title => 'Shiny window', # Set window title | ||
| 215 | nearclip => 0.1, # Near clipping plane | ||
| 216 | translatescale => 0.01, # Mouse translation speed | ||
| 217 | zoomscale => 0.02, # Mouse zoom speed | ||
| 218 | screenx => 256, # Initial window dimensions | ||
| 219 | screeny => 256, | ||
| 220 | sphererad => 256*0.5, # Virtual trackball size | ||
| 221 | displaymode => GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH, | ||
| 222 | # Window display mode | ||
| 223 | |||
| 224 | initialize_gl => sub { | ||
| 225 | glClearColor(0,0,1,1); # Blue background | ||
| 226 | }, | ||
| 227 | |||
| 228 | draw_background => sub { | ||
| 229 | # Clear the window before drawing geometry | ||
| 230 | glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); | ||
| 231 | }, | ||
| 232 | |||
| 233 | # Draw a teapot. | ||
| 234 | draw_geometry => sub { glutSolidTeapot(1.0); }, | ||
| 235 | |||
| 236 | ); | ||
| 237 | |||
| 238 | This method opens up a new GLUT window with some useful event callbacks | ||
| 239 | set, and returns an OpenGL::Simple::Viewer object to represent it. | ||
| 240 | glutInit() should have been called beforehand, to set up the GLUT | ||
| 241 | library. | ||
| 242 | |||
| 243 | new() takes either a hash or a reference to a hash of arguments, which | ||
| 244 | can include: | ||
| 245 | |||
| 246 | =over 2 | ||
| 247 | |||
| 248 | =item B<title> | ||
| 249 | |||
| 250 | Sets the title of the window. | ||
| 251 | |||
| 252 | =item B<nearclip> | ||
| 253 | |||
| 254 | Sets the distance of the near clipping plane. Anything closer | ||
| 255 | to the viewer than this will not be displayed. | ||
| 256 | |||
| 257 | =item B<translatescale> | ||
| 258 | |||
| 259 | Sets the scale of mouse translation; the larger the scale, the faster | ||
| 260 | the geometry will move for a given mouse motion. | ||
| 261 | |||
| 262 | =item B<zoomscale> | ||
| 263 | |||
| 264 | Sets the scale of mouse zooming; the larger the scale, the faster | ||
| 265 | the geometry will move for a given mouse motion. | ||
| 266 | |||
| 267 | =item B<screenx>,B<screeny> | ||
| 268 | |||
| 269 | Sets the initial size of the window. | ||
| 270 | |||
| 271 | =item B<sphererad> | ||
| 272 | |||
| 273 | Sets the radius of the virtual trackball sphere. | ||
| 274 | |||
| 275 | =item B<displaymode> | ||
| 276 | |||
| 277 | Initial arguments to glutInitDisplayMode. | ||
| 278 | |||
| 279 | =item B<initialize_gl> | ||
| 280 | |||
| 281 | This is a subroutine which is called once the window has been created, | ||
| 282 | to set up initial GL state such as lighting, texture environment, | ||
| 283 | background colour, etc. By default it sets a black background and a | ||
| 284 | white light. If this argument is set to undef, then no GL state will be | ||
| 285 | changed. | ||
| 286 | |||
| 287 | =item B<draw_geometry> | ||
| 288 | |||
| 289 | Every time the viewer moves around, the geometry must be redrawn in its | ||
| 290 | new position. This argument is a coderef which is called to redraw the | ||
| 291 | geometry; you can put any GL calls you like in here. | ||
| 292 | |||
| 293 | =item B<draw_background> | ||
| 294 | |||
| 295 | When a redraw event occurs, this routine is called first, before the | ||
| 296 | viewer is oriented or the geometry drawn. It can be used to draw a | ||
| 297 | background image. | ||
| 298 | |||
| 299 | =back | ||
| 300 | |||
| 301 | =cut | ||
| 302 | |||
| 303 | sub new { | ||
| 304 | my $class = shift; | ||
| 305 | |||
| 306 | my %arg = (0==$#_) ? %{$#_} : @_; # Take a hash or hashref of args. | ||
| 307 | |||
| 308 | my $self = { | ||
| 309 | orientation => new Math::Quaternion, | ||
| 310 | position => [0,0,-5], | ||
| 311 | nearclip => 0.1, | ||
| 312 | translatescale => 0.01, | ||
| 313 | zoomscale =>0.02, | ||
| 314 | screenx => 256, | ||
| 315 | screeny => 256, | ||
| 316 | sphererad => 256*0.5, # Radius of trackball sphere | ||
| 317 | displaymode => GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH, | ||
| 318 | |||
| 319 | draw_background => sub { | ||
| 320 | glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) | ||
| 321 | }, | ||
| 322 | draw_geometry => sub { 1; }, # Do nothing by default | ||
| 323 | |||
| 324 | # The following are really default arguments, rather | ||
| 325 | # than properties which can be usefully modified later. | ||
| 326 | |||
| 327 | title => 'OpenGL::Simple::Viewer', | ||
| 328 | initialize_gl => sub { | ||
| 329 | my @LightAmbient = ( 0.1,0.1,0.1,1.0); | ||
| 330 | my @LightDiffuse = ( 0.5, 0.5, 0.5, 1.0); | ||
| 331 | my @LightSpecular = ( 0.1, 0.1, 0.1, 0.1); | ||
| 332 | my @LightPos = ( 0, 0, 2, 1.0); | ||
| 333 | glShadeModel(GL_SMOOTH); | ||
| 334 | |||
| 335 | glLight(GL_LIGHT1,GL_AMBIENT,@LightAmbient); | ||
| 336 | glLight(GL_LIGHT1,GL_DIFFUSE,@LightDiffuse); | ||
| 337 | glLight(GL_LIGHT1,GL_SPECULAR,@LightSpecular); | ||
| 338 | glLight(GL_LIGHT1,GL_POSITION,@LightPos); | ||
| 339 | glEnable(GL_LIGHT1); | ||
| 340 | glEnable(GL_LIGHTING); | ||
| 341 | glEnable(GL_DEPTH_TEST); | ||
| 342 | |||
| 343 | glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE); | ||
| 344 | glEnable(GL_COLOR_MATERIAL); | ||
| 345 | |||
| 346 | glClearColor(0,0,0,1); | ||
| 347 | }, | ||
| 348 | |||
| 349 | # The following are internal state variables. | ||
| 350 | |||
| 351 | _buttonstate => { GLUT_LEFT_BUTTON,0, | ||
| 352 | GLUT_MIDDLE_BUTTON,0, | ||
| 353 | GLUT_RIGHT_BUTTON,0 }, | ||
| 354 | # Current button state | ||
| 355 | _lastclick => [0,0], # Coordinates of last mouse click | ||
| 356 | |||
| 357 | |||
| 358 | }; | ||
| 359 | |||
| 360 | |||
| 361 | $self={%$self,%arg}; # Override defaults | ||
| 362 | bless $self,$class; | ||
| 363 | |||
| 364 | glutInitWindowSize(@$self{qw(screenx screeny)}); | ||
| 365 | glutInitDisplayMode($self->{'displaymode'}); | ||
| 366 | |||
| 367 | $self->{'window'} = glutCreateWindow($self->{'title'}); | ||
| 368 | |||
| 369 | # Create a list of slave viewer objects which change when the mouse | ||
| 370 | # is dragged in this viewer's window. | ||
| 371 | # | ||
| 372 | # Actually, use a hash instead of a list so it's easy to ungang | ||
| 373 | # a specific slave. | ||
| 374 | # | ||
| 375 | # A viewer is its own slave by default. | ||
| 376 | |||
| 377 | $self->{'slaves'} = { $self->{'window'} => $self }; | ||
| 378 | |||
| 379 | if (defined($self->{'initialize_gl'})) { $self->{'initialize_gl'}->(); } | ||
| 380 | |||
| 381 | glutDisplayFunc($self->make_displayfunc); | ||
| 382 | glutReshapeFunc($self->make_reshapefunc); | ||
| 383 | glutMouseFunc($self->make_mousefunc); | ||
| 384 | glutMotionFunc($self->make_motionfunc); | ||
| 385 | |||
| 386 | return $self; | ||
| 387 | |||
| 388 | } | ||
| 389 | |||
| 390 | =item B<make_reshapefunc> | ||
| 391 | |||
| 392 | This method returns a callback subroutine which can be passed to | ||
| 393 | glutReshapeFunc, and which sets the OpenGL::Viewer::Simple state after a | ||
| 394 | window is resized. You are free to set your own reshape callback by | ||
| 395 | calling glutReshapeFunc(); if you ever want the old one back, then simply | ||
| 396 | |||
| 397 | glutReshapeFunc($viewer->make_reshapefunc); | ||
| 398 | |||
| 399 | =cut | ||
| 400 | |||
| 401 | sub make_reshapefunc { | ||
| 402 | my $self = shift; | ||
| 403 | return sub { | ||
| 404 | my ($w,$h) = @_; | ||
| 405 | |||
| 406 | $self->{'screenx'} = $w; $self->{'screeny'} = $h; | ||
| 407 | $self->{'sphererad'} = 0.5*$w; | ||
| 408 | glViewport(0,0,$w,$h); | ||
| 409 | } | ||
| 410 | } | ||
| 411 | |||
| 412 | =item B<make_displayfunc> | ||
| 413 | |||
| 414 | Similarly to make_reshapefunc(), this returns the default display | ||
| 415 | callback subroutine. | ||
| 416 | |||
| 417 | =cut | ||
| 418 | |||
| 419 | sub make_displayfunc { | ||
| 420 | my $self = shift; | ||
| 421 | return sub { | ||
| 422 | # Draw background if required. | ||
| 423 | if (defined($self->{'draw_background'})) { | ||
| 424 | $self->{'draw_background'}->(); | ||
| 425 | } | ||
| 426 | # Set up perspective projection | ||
| 427 | glMatrixMode(GL_PROJECTION); | ||
| 428 | glLoadIdentity(); | ||
| 429 | gluPerspective(45.0,1.0,0.1,100.0); | ||
| 430 | glMatrixMode(GL_MODELVIEW); | ||
| 431 | glLoadIdentity(); | ||
| 432 | |||
| 433 | # Position and orient geometry | ||
| 434 | |||
| 435 | glTranslate(@{$self->{'position'}}); | ||
| 436 | glMultMatrix($self->{'orientation'}->matrix4x4); | ||
| 437 | |||
| 438 | $self->{'draw_geometry'}->(); | ||
| 439 | |||
| 440 | # Make sure it hits the screen. | ||
| 441 | glFlush(); | ||
| 442 | glutSwapBuffers(); | ||
| 443 | }; | ||
| 444 | } | ||
| 445 | |||
| 446 | =item B<make_mousefunc> | ||
| 447 | |||
| 448 | Similarly to make_reshapefunc(), this returns the default mouse click | ||
| 449 | callback subroutine. | ||
| 450 | |||
| 451 | =cut | ||
| 452 | |||
| 453 | |||
| 454 | sub make_mousefunc { | ||
| 455 | my $self = shift; | ||
| 456 | return sub { | ||
| 457 | my ($button,$state,$x,$y) = @_; | ||
| 458 | |||
| 459 | $self->{'_lastclick'} = [$x,$y]; | ||
| 460 | $self->{'_buttonstate'}->{$button} | ||
| 461 | = (GLUT_DOWN == $state) ? 1 : 0; | ||
| 462 | }; | ||
| 463 | } | ||
| 464 | |||
| 465 | =item B<make_motionfunc> | ||
| 466 | |||
| 467 | Similarly to make_reshapefunc(), this returns the default mouse motion | ||
| 468 | callback subroutine. | ||
| 469 | |||
| 470 | =cut | ||
| 471 | |||
| 472 | sub make_motionfunc { | ||
| 473 | my $self = shift; | ||
| 474 | return sub { | ||
| 475 | my ($x,$y) = @_; | ||
| 476 | my %buttonstate = %{$self->{'_buttonstate'}}; | ||
| 477 | my ($left,$mid,$right) = | ||
| 478 | @buttonstate{GLUT_LEFT_BUTTON, | ||
| 479 | GLUT_MIDDLE_BUTTON, | ||
| 480 | GLUT_RIGHT_BUTTON}; | ||
| 481 | my ($clickx,$clicky)=@{$self->{'_lastclick'}}; | ||
| 482 | |||
| 483 | # Invoke the appropriate motion method on each | ||
| 484 | # Viewer object which has registered to receive | ||
| 485 | # control from this one. | ||
| 486 | |||
| 487 | # Save current window (although it ought to be $self->{window}) | ||
| 488 | |||
| 489 | my $prevwin = glutGetWindow; | ||
| 490 | |||
| 491 | if ($left) { | ||
| 492 | while (my ($w,$v) = each %{$self->{'slaves'}}) { | ||
| 493 | glutSetWindow($w); | ||
| 494 | $v->mouserotatemotion($clickx,$clicky,$x,$y); | ||
| 495 | } | ||
| 496 | } elsif ($mid) { | ||
| 497 | while (my ($w,$v) = each %{$self->{'slaves'}}) { | ||
| 498 | glutSetWindow($w); | ||
| 499 | $v->mousetransmotion($clickx,$clicky,$x,$y); | ||
| 500 | } | ||
| 501 | } elsif ($right) { | ||
| 502 | while (my ($w,$v) = each %{$self->{'slaves'}}) { | ||
| 503 | glutSetWindow($w); | ||
| 504 | $v->mousezoommotion($y-$clicky); | ||
| 505 | } | ||
| 506 | } | ||
| 507 | $self->{'_lastclick'} = [$x,$y]; | ||
| 508 | |||
| 509 | glutSetWindow($prevwin); # Restore window | ||
| 510 | |||
| 511 | }; | ||
| 512 | |||
| 513 | } | ||
| 514 | |||
| 515 | =item B<mouserotatemotion> ($x0,$y0,$x1,$y1) | ||
| 516 | |||
| 517 | This method takes four arguments, corresponding to a motion from | ||
| 518 | ($x0,$y0) to ($x1,$y1). It interprets the motion as the user dragging on | ||
| 519 | a virtual trackball sitting on the window, and rotates the geometry | ||
| 520 | accordingly. The radius of the trackball is set through the B<sphererad> | ||
| 521 | property. | ||
| 522 | |||
| 523 | |||
| 524 | =cut | ||
| 525 | |||
| 526 | |||
| 527 | sub mouserotatemotion { | ||
| 528 | my $self = shift; | ||
| 529 | my ($x0,$y0,$x1,$y1) = @_; | ||
| 530 | |||
| 531 | my $s = $self->{'sphererad'}; | ||
| 532 | my $my = $x1-$x0; | ||
| 533 | my $mx = $y1-$y0; | ||
| 534 | my $m=sqrt($mx*$mx+$my*$my); | ||
| 535 | |||
| 536 | my $theta; | ||
| 537 | |||
| 538 | if (($m>0) && ($m<$s)) { | ||
| 539 | $theta = $m/$s; | ||
| 540 | |||
| 541 | $mx /= $m; | ||
| 542 | $my /= $m; | ||
| 543 | |||
| 544 | my $rotquat = Math::Quaternion::rotation($theta,$mx,$my,0.0); | ||
| 545 | $self->{'orientation'} = $rotquat * $self->{'orientation'}; | ||
| 546 | } | ||
| 547 | |||
| 548 | glutPostRedisplay(); | ||
| 549 | } | ||
| 550 | |||
| 551 | =item B<mousetransmotion> ($x0,$y0,$x1,$y1) | ||
| 552 | |||
| 553 | This method takes the coordinates of a mouse drag event, and interprets | ||
| 554 | it as a translation. The magnitude of the translation can be set through | ||
| 555 | the B<translatescale> property. | ||
| 556 | |||
| 557 | =cut | ||
| 558 | |||
| 559 | sub mousetransmotion { | ||
| 560 | my $self = shift; | ||
| 561 | my ($x0,$y0,$x1,$y1) = @_; | ||
| 562 | |||
| 563 | my ($oldx,$oldy,$oldz) = @{$self->{'position'}}; | ||
| 564 | $self->{'position'} = [ | ||
| 565 | $oldx + $self->{'translatescale'}*($x1-$x0), | ||
| 566 | $oldy + $self->{'translatescale'}*($y0-$y1), | ||
| 567 | $oldz | ||
| 568 | ]; | ||
| 569 | |||
| 570 | glutPostRedisplay(); | ||
| 571 | } | ||
| 572 | |||
| 573 | =item B<mousetransmotion> ($dz) | ||
| 574 | |||
| 575 | This method takes a single argument representing the length of a mouse | ||
| 576 | drag event, and zooms the geometry accordingly, controlled by the | ||
| 577 | B<zoomscale> property. | ||
| 578 | |||
| 579 | =cut | ||
| 580 | |||
| 581 | sub mousezoommotion { | ||
| 582 | my $self = shift; | ||
| 583 | my $dz = shift; | ||
| 584 | $self->{'position'}->[2] -= $self->{'zoomscale'}*$dz; | ||
| 585 | glutPostRedisplay(); | ||
| 586 | } | ||
| 587 | |||
| 588 | =item B<enslave> | ||
| 589 | |||
| 590 | This method takes a list of OpenGL::Simple::Viewer objects, and sets | ||
| 591 | them all to receive motion events from the object on which the method is | ||
| 592 | invoked. If you have two viewers, $v1 and $v2, then | ||
| 593 | |||
| 594 | $v1->enslave($v2); | ||
| 595 | |||
| 596 | means that dragging the mouse around in viewer $v1 will cause both $v1 | ||
| 597 | and $v2 to move; however, mouse-dragging in viewer $v2 will only | ||
| 598 | cause it to move, and not $v1. | ||
| 599 | |||
| 600 | =cut | ||
| 601 | |||
| 602 | sub enslave { | ||
| 603 | my $self = shift; | ||
| 604 | |||
| 605 | for my $slave (@_) { | ||
| 606 | $self->{'slaves'}->{$slave->{'window'}} = $slave; | ||
| 607 | } | ||
| 608 | } | ||
| 609 | |||
| 610 | =item B<decouple> | ||
| 611 | |||
| 612 | This method takes a list of Viewer objects, and decouples their motion | ||
| 613 | from that of the object on which it was invoked. | ||
| 614 | |||
| 615 | $v1->decouple($v2,$v3); | ||
| 616 | |||
| 617 | is the inverse of | ||
| 618 | |||
| 619 | $v1->enslave($v2,$v3); | ||
| 620 | |||
| 621 | =cut | ||
| 622 | |||
| 623 | sub decouple { | ||
| 624 | my $self = shift; | ||
| 625 | |||
| 626 | for my $slave(@_) { | ||
| 627 | delete $self->{'slaves'}->{$slave->{'window'}}; | ||
| 628 | } | ||
| 629 | } | ||
| 630 | |||
| 631 | =item B<gang_together> | ||
| 632 | |||
| 633 | This method takes a list of OpenGL::Simple::Viewer objects, and couples | ||
| 634 | together their motion, so that mouse dragging in any of them will cause | ||
| 635 | all of them to move. | ||
| 636 | |||
| 637 | $v1->gang_together($v2,$v3); | ||
| 638 | |||
| 639 | is the same as | ||
| 640 | |||
| 641 | OpenGL::Simple::Viewer::gang_together($v1,$v2,$v3) | ||
| 642 | |||
| 643 | and will couple the motion of $v1,$v2, and $v3. | ||
| 644 | |||
| 645 | =cut | ||
| 646 | |||
| 647 | sub gang_together { | ||
| 648 | my @viewers = @_; | ||
| 649 | |||
| 650 | for my $master (@viewers) { | ||
| 651 | $master->enslave(@viewers); | ||
| 652 | } | ||
| 653 | } | ||
| 654 | |||
| 655 | |||
| 656 | =back | ||
| 657 | |||
| 658 | =head1 SEE ALSO | ||
| 659 | |||
| 660 | OpenGL::Simple, OpenGL::Simple::GLUT | ||
| 661 | |||
| 662 | =head1 AUTHOR | ||
| 663 | |||
| 664 | Jonathan Chin, E<lt>jon-opengl-simple-viewer@earth.liE<gt> | ||
| 665 | |||
| 666 | =head1 COPYRIGHT AND LICENSE | ||
| 667 | |||
| 668 | Copyright 2004 by Jonathan Chin | ||
| 669 | |||
| 670 | This library is free software; you can redistribute it and/or modify | ||
| 671 | it under the same terms as Perl itself. | ||
| 672 | |||
| 673 | =cut | ||
| 674 | |||
| 675 | 1; | ||
| 676 | |||
| 677 | __END__ | ||