3DWorld/Actor/GridSelect.pm untested

filter: Statementstmt 0.0% 0 / 94 Branchbran 0.0% 0 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 114 CC 2 SCAR 9.4
CC
2
Cov
0%
move
6.0
new
2.0
init
2.0
CRAP 2.6
1 package SDL::Tutorial::3DWorld::Actor::GridSelect;
2  
3 use 5.008;
4 use strict;
5 use warnings;
6 use SDL::Tutorial::3DWorld ();
7 use SDL::Tutorial::3DWorld::OpenGL ();
8 use SDL::Tutorial::3DWorld::Actor ();
9 use OpenGL::List ();
10  
11 # Use proper POSIX math rather than playing games with Perl's int()
12 use POSIX ();
13  
14 our $VERSION = '0.33';
15 our @ISA = 'SDL::Tutorial::3DWorld::Actor';
16  
17 sub new {
18 my $self = shift->SUPER::new(@_);
19  
20 # Gridcubes need blending
21 $self->{blending} = 1;
22  
23 # The select box starts 2.5 metres in front of the camera
24 $self->{distance} = 5;
25  
26 # The select box is a 1 metre cube
27 # $self->{box} = [ -0.05, -0.05, -0.05, 1.05, 1.05, 1.05 ];
28  
29 # We must have some notional velocity to be considered for movement.
30 # This is an artifact of the movement optimisation and a bit of a
31 # cludge really.
32 $self->{velocity} = [ 0, 0, 0 ];
33  
34 return $self;
35 }
36  
37  
38  
39  
40  
41 ######################################################################
42 # Engine Interface
43  
44 sub init {
45 my $self = shift;
46 $self->SUPER::init(@_);
47  
48 # Compile the point and cube lists
49 $self->{list} = OpenGL::List::glpList {
50 $self->compile;
51 };
52  
53 return 1;
54 }
55  
56 sub move {
57 my $self = shift;
58 my $camera = SDL::Tutorial::3DWorld->current->camera;
59  
60 # Project the location of the notional point out of the camera
61 # to find the camera-relative position of the box.
62 my $distance = $self->{distance};
63 my $direction = $camera->{direction};
64 my @position = (
65 $distance * $direction->[0],
66 $distance * $direction->[1],
67 $distance * $direction->[2],
68 );
69  
70 # Limit the selector position to one unit below the floor
71 # for a minecraft-inspired selection effect.
72 # When applying the limitation we want to constrain on all
73 # three dimensions. This has the effect of making the selected
74 # grid position still in front of the camera, which looks more
75 # natural than simply moving the selection grid up on the Y axis.
76 # Set the Y to a slightly larger value rather than multiplying by
77 # the ratio to prevent any floating point issues when we do the
78 # POSIX::ceil grid-snapping call later.
79 my $lowest_y = 0;
80 if ( ($camera->Y + $position[1]) < $lowest_y ) {
81 my $have_y = $position[1];
82 my $want_y = $lowest_y - $camera->Y;
83 my $ratio = $want_y / $have_y;
84 $position[0] *= $ratio;
85 $position[1] *= $ratio;
86 $position[1] += 0.1;
87 $position[2] *= $ratio;
88 }
89  
90 # Apply the final position to the camera position and snap to the
91 # grid on the negative side on all three dimensions, as we will be
92 # drawing the grid outwards on the positive side on all three.
93 $self->{position} = [
94 POSIX::floor( $camera->X + $position[0] ),
95 POSIX::floor( $camera->Y + $position[1] ),
96 POSIX::floor( $camera->Z + $position[2] ),
97 ];
98  
99 return 1;
100 }
101  
102 sub display {
103 my $self = shift;
104  
105 # Translate to the correct location
106 $self->SUPER::display(@_);
107  
108 # Draw the 1m cube at the location
109 OpenGL::glCallList( $self->{list} );
110 }
111  
112 # The compilable section of the grid cube display logic
113 sub compile {
114 # The cube is plain opaque full-bright white and ignores lighting
115 OpenGL::glDisable( OpenGL::GL_LIGHTING );
116 OpenGL::glDisable( OpenGL::GL_TEXTURE_2D );
117  
118 # Enable line smoothing
119 OpenGL::glBlendFunc( OpenGL::GL_SRC_ALPHA, OpenGL::GL_ONE_MINUS_SRC_ALPHA );
120 OpenGL::glEnable( OpenGL::GL_BLEND );
121 OpenGL::glEnable( OpenGL::GL_LINE_SMOOTH );
122  
123 # Draw all the lines in the cube
124 OpenGL::glLineWidth(1);
125 OpenGL::glColor4f( 1.0, 1.0, 1.0, 1.0 );
126 OpenGL::glBegin( OpenGL::GL_LINES );
127 OpenGL::glVertex3f( -0.001, -0.001, -0.001 ); OpenGL::glVertex3f( 1.001, -0.001, -0.001 );
128 OpenGL::glVertex3f( -0.001, -0.001, -0.001 ); OpenGL::glVertex3f( -0.001, 1.001, -0.001 );
129 OpenGL::glVertex3f( -0.001, -0.001, -0.001 ); OpenGL::glVertex3f( -0.001, -0.001, 1 );
130 OpenGL::glVertex3f( 1.001, -0.001, -0.001 ); OpenGL::glVertex3f( 1.001, 1.001, -0.001 );
131 OpenGL::glVertex3f( 1.001, -0.001, -0.001 ); OpenGL::glVertex3f( 1.001, -0.001, 1.001 );
132 OpenGL::glVertex3f( -0.001, 1.001, -0.001 ); OpenGL::glVertex3f( 1.001, 1.001, -0.001 );
133 OpenGL::glVertex3f( -0.001, 1.001, -0.001 ); OpenGL::glVertex3f( -0.001, 1.001, 1.001 );
134 OpenGL::glVertex3f( -0.001, -0.001, 1.001 ); OpenGL::glVertex3f( 1.001, -0.001, 1.001 );
135 OpenGL::glVertex3f( -0.001, -0.001, 1.001 ); OpenGL::glVertex3f( -0.001, 1.001, 1.001 );
136 OpenGL::glVertex3f( 1.001, 1.001, -0.001 ); OpenGL::glVertex3f( 1.001, 1.001, 1.001 );
137 OpenGL::glVertex3f( 1.001, -0.001, 1.001 ); OpenGL::glVertex3f( 1.001, 1.001, 1.001 );
138 OpenGL::glVertex3f( -0.001, 1.001, 1.001 ); OpenGL::glVertex3f( 1.001, 1.001, 1.001 );
139 OpenGL::glEnd();
140  
141 # Disable line smoothing
142 OpenGL::glDisable( OpenGL::GL_LINE_SMOOTH );
143 OpenGL::glDisable( OpenGL::GL_BLEND );
144  
145 # Restore lighting
146 OpenGL::glEnable( OpenGL::GL_LIGHTING );
147 }
148  
149 1;