| 1 | package SDL::Tutorial::3DWorld::Actor::Debug; | ||
| 2 | |||
| 3 | use 5.008; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use SDL::Tutorial::3DWorld::Bound; | ||
| 7 | use SDL::Tutorial::3DWorld::Actor (); | ||
| 8 | use SDL::Tutorial::3DWorld::OpenGL (); | ||
| 9 | use OpenGL::List (); | ||
| 10 | |||
| 11 | our $VERSION = '0.33'; | ||
| 12 | our @ISA = 'SDL::Tutorial::3DWorld::Actor'; | ||
| 13 | |||
| 14 | sub new { | ||
| 15 | my $class = shift; | ||
| 16 | my $self = bless { @_ }, $class; | ||
| 17 | |||
| 18 | # Do we have a parent? | ||
| 19 | unless ( $self->{parent} ) { | ||
| 20 | die "Did not provide a parent actor"; | ||
| 21 | } | ||
| 22 | |||
| 23 | # We blend, but don't move | ||
| 24 | $self->{blending} = 0; | ||
| 25 | |||
| 26 | # Ensure our move method is called | ||
| 27 | $self->{velocity} = [ 0, 0, 0 ]; | ||
| 28 | |||
| 29 | return $self; | ||
| 30 | } | ||
| 31 | |||
| 32 | |||
| 33 | |||
| 34 | |||
| 35 | |||
| 36 | ###################################################################### | ||
| 37 | # Engine Interface | ||
| 38 | |||
| 39 | sub init { | ||
| 40 | my $self = shift; | ||
| 41 | |||
| 42 | # Generate the cube and axis display lists | ||
| 43 | $self->{boxlist} = OpenGL::List::glpList { | ||
| 44 | $self->compile_box; | ||
| 45 | }; | ||
| 46 | $self->{axislist} = OpenGL::List::glpList { | ||
| 47 | $self->compile_axis; | ||
| 48 | }; | ||
| 49 | |||
| 50 | # Set our initial position, box and boundary data to that of | ||
| 51 | # our parents like our normal per-move sync. | ||
| 52 | $self->move; | ||
| 53 | |||
| 54 | return 1; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub move { | ||
| 58 | my $self = shift; | ||
| 59 | |||
| 60 | # Update our position and size to that of our parent | ||
| 61 | $self->{position} = $self->{parent}->{position}; | ||
| 62 | $self->{bound} = $self->{parent}->{bound}; | ||
| 63 | $self->{box} = $self->{parent}->{box}; | ||
| 64 | $self->{orient} = $self->{parent}->{orient}; | ||
| 65 | $self->{rotate} = $self->{parent}->{rotate}; | ||
| 66 | |||
| 67 | return 1; | ||
| 68 | } | ||
| 69 | |||
| 70 | sub display { | ||
| 71 | my $self = shift; | ||
| 72 | my $position = $self->{position} or return; | ||
| 73 | my $bound = $self->{bound} or return; | ||
| 74 | |||
| 75 | # Axis lines extend to 20% of the length of an | ||
| 76 | # object along a dimension past the edge. | ||
| 77 | my $XL = $bound->[BOX_X2] + ($bound->[BOX_X2] - $bound->[BOX_X1]) * 0.2; | ||
| 78 | my $YL = $bound->[BOX_Y2] + ($bound->[BOX_Y2] - $bound->[BOX_Y1]) * 0.2; | ||
| 79 | my $ZL = $bound->[BOX_Z2] + ($bound->[BOX_Z2] - $bound->[BOX_Z1]) * 0.2; | ||
| 80 | |||
| 81 | # Translate to the model origin and call the axis display list, | ||
| 82 | # even if the model doesn't have an actual bounding box. | ||
| 83 | # Scale to the bounding box if we have one, or just draw it | ||
| 84 | # plain and 1 metre on a side otherwise. | ||
| 85 | OpenGL::glPushMatrix(); | ||
| 86 | OpenGL::glTranslatef( @$position ); | ||
| 87 | OpenGL::glScalef( $XL, $YL, $ZL ); | ||
| 88 | OpenGL::glRotatef( @{$self->{orient}} ) if $self->{orient}; | ||
| 89 | OpenGL::glCallList( $self->{axislist} ); | ||
| 90 | OpenGL::glPopMatrix(); | ||
| 91 | |||
| 92 | # Translate to the negative corner | ||
| 93 | OpenGL::glTranslatef( @$position ); | ||
| 94 | OpenGL::glRotatef( @{$self->{orient}} ) if $self->{orient}; | ||
| 95 | OpenGL::glTranslatef( | ||
| 96 | $bound->[BOX_X1], | ||
| 97 | $bound->[BOX_Y1], | ||
| 98 | $bound->[BOX_Z1], | ||
| 99 | ); | ||
| 100 | |||
| 101 | # Scale so that the resulting 1 metre cube becomes the right size | ||
| 102 | OpenGL::glScalef( | ||
| 103 | $bound->[BOX_X2] - $bound->[BOX_X1], | ||
| 104 | $bound->[BOX_Y2] - $bound->[BOX_Y1], | ||
| 105 | $bound->[BOX_Z2] - $bound->[BOX_Z1], | ||
| 106 | ); | ||
| 107 | |||
| 108 | # Call the display list to render the cube | ||
| 109 | OpenGL::glCallList( $self->{boxlist} ); | ||
| 110 | } | ||
| 111 | |||
| 112 | sub compile_axis { | ||
| 113 | my $self = shift; | ||
| 114 | |||
| 115 | # The axis is plain opaque full-bright and ignores lighting | ||
| 116 | OpenGL::glDisable( OpenGL::GL_LIGHTING ); | ||
| 117 | OpenGL::glDisable( OpenGL::GL_TEXTURE_2D ); | ||
| 118 | |||
| 119 | # Enable line smoothing | ||
| 120 | #OpenGL::glBlendFunc( OpenGL::GL_SRC_ALPHA, OpenGL::GL_ONE_MINUS_SRC_ALPHA ); | ||
| 121 | OpenGL::glDisable( OpenGL::GL_BLEND ); | ||
| 122 | OpenGL::glDisable( OpenGL::GL_LINE_SMOOTH ); | ||
| 123 | |||
| 124 | # Draw the (R)ed X Axis | ||
| 125 | OpenGL::glLineWidth(1); | ||
| 126 | OpenGL::glColor4f( 1.0, 0.0, 0.0, 1.0 ); | ||
| 127 | OpenGL::glBegin( OpenGL::GL_LINES ); | ||
| 128 | OpenGL::glVertex3f( 0, 0, 0 ); | ||
| 129 | OpenGL::glVertex3f( 1.1, 0, 0 ); | ||
| 130 | OpenGL::glEnd(); | ||
| 131 | |||
| 132 | # Draw the (G)reen Y Axis | ||
| 133 | OpenGL::glColor4f( 0.0, 1.0, 0.0, 1.0 ); | ||
| 134 | OpenGL::glBegin( OpenGL::GL_LINES ); | ||
| 135 | OpenGL::glVertex3f( 0, 0, 0 ); | ||
| 136 | OpenGL::glVertex3f( 0, 1.1, 0 ); | ||
| 137 | OpenGL::glEnd(); | ||
| 138 | |||
| 139 | # Draw the (B)lue Z Axis | ||
| 140 | OpenGL::glColor4f( 0.0, 0.0, 1.0, 1.0 ); | ||
| 141 | OpenGL::glBegin( OpenGL::GL_LINES ); | ||
| 142 | OpenGL::glVertex3f( 0, 0, 0 ); | ||
| 143 | OpenGL::glVertex3f( 0, 0, 1.1 ); | ||
| 144 | OpenGL::glEnd(); | ||
| 145 | |||
| 146 | # Revert the light disable | ||
| 147 | OpenGL::glEnable( OpenGL::GL_LIGHTING ); | ||
| 148 | } | ||
| 149 | |||
| 150 | sub compile_box { | ||
| 151 | my $self = shift; | ||
| 152 | |||
| 153 | # The cube is plain opaque full-bright white and ignores lighting | ||
| 154 | OpenGL::glDisable( OpenGL::GL_LIGHTING ); | ||
| 155 | OpenGL::glDisable( OpenGL::GL_TEXTURE_2D ); | ||
| 156 | |||
| 157 | # Enable line smoothing | ||
| 158 | #OpenGL::glBlendFunc( OpenGL::GL_SRC_ALPHA, OpenGL::GL_ONE_MINUS_SRC_ALPHA ); | ||
| 159 | OpenGL::glDisable( OpenGL::GL_BLEND ); | ||
| 160 | OpenGL::glDisable( OpenGL::GL_LINE_SMOOTH ); | ||
| 161 | |||
| 162 | # Draw all the lines in the cube | ||
| 163 | OpenGL::glColor4f( 0.5, 1.0, 0.50, 1.0 ); | ||
| 164 | OpenGL::glLineWidth(1); | ||
| 165 | OpenGL::glBegin( OpenGL::GL_LINES ); | ||
| 166 | OpenGL::glVertex3f( 0, 0, 0 ); OpenGL::glVertex3f( 1, 0, 0 ); | ||
| 167 | OpenGL::glVertex3f( 0, 0, 0 ); OpenGL::glVertex3f( 0, 1, 0 ); | ||
| 168 | OpenGL::glVertex3f( 0, 0, 0 ); OpenGL::glVertex3f( 0, 0, 1 ); | ||
| 169 | OpenGL::glVertex3f( 1, 0, 0 ); OpenGL::glVertex3f( 1, 1, 0 ); | ||
| 170 | OpenGL::glVertex3f( 1, 0, 0 ); OpenGL::glVertex3f( 1, 0, 1 ); | ||
| 171 | OpenGL::glVertex3f( 0, 1, 0 ); OpenGL::glVertex3f( 1, 1, 0 ); | ||
| 172 | OpenGL::glVertex3f( 0, 1, 0 ); OpenGL::glVertex3f( 0, 1, 1 ); | ||
| 173 | OpenGL::glVertex3f( 0, 0, 1 ); OpenGL::glVertex3f( 1, 0, 1 ); | ||
| 174 | OpenGL::glVertex3f( 0, 0, 1 ); OpenGL::glVertex3f( 0, 1, 1 ); | ||
| 175 | OpenGL::glVertex3f( 1, 1, 0 ); OpenGL::glVertex3f( 1, 1, 1 ); | ||
| 176 | OpenGL::glVertex3f( 1, 0, 1 ); OpenGL::glVertex3f( 1, 1, 1 ); | ||
| 177 | OpenGL::glVertex3f( 0, 1, 1 ); OpenGL::glVertex3f( 1, 1, 1 ); | ||
| 178 | OpenGL::glEnd(); | ||
| 179 | |||
| 180 | # Revert the light disable | ||
| 181 | OpenGL::glEnable( OpenGL::GL_LIGHTING ); | ||
| 182 | } | ||
| 183 | |||
| 184 | 1; |