3DWorld/Actor/GridCube.pm untested

filter: Statementstmt 0.0% 0 / 89 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 106 CC 1 SCAR 6.9
CC
1
Cov
0%
new
2.0
init
2.0
display
2.0
CRAP 2.0
1 package SDL::Tutorial::3DWorld::Actor::GridCube;
2  
3 =pod
4  
5 =head1 NAME
6  
7 SDL::Tutorial::3DWorld::Actor::GridCube - A grid-snapping 3D wireframe cube
8  
9 =head1 DESCRIPTION
10  
11 The B<GridCube> is a 1 metre white wireframe cube which will track it's
12 position in float terms and can be moved around the game world like any
13 other actor, but which will draw itself snapped to an imaginary 1 metre
14 grid.
15  
16 The position of the cube will be moved to ensure that the actual floating
17 point location of the actor is inside the cube.
18  
19 If the location of the cube is an exact integer, the cube will be located
20 on the positive axis side (in all three dimension) of the actor position.
21  
22 =cut
23  
24 use 5.008;
25 use strict;
26 use warnings;
27 use SDL::Tutorial::3DWorld::OpenGL ();
28 use SDL::Tutorial::3DWorld::Actor ();
29 use OpenGL::List ();
30  
31 # Use proper POSIX math rather than playing games with Perl's int()
32 use POSIX ();
33  
34 our $VERSION = '0.33';
35 our @ISA = 'SDL::Tutorial::3DWorld::Actor';
36  
37 sub new {
38 my $self = shift->SUPER::new(@_);
39  
40 # Gridcubes need blending
41 $self->{blending} = 1;
42  
43 return $self;
44 }
45  
46  
47  
48  
49  
50 ######################################################################
51 # Engine Interface
52  
53 sub init {
54 my $self = shift;
55 $self->SUPER::init(@_);
56  
57 # Compile the point and cube lists
58 $self->{point_list} = OpenGL::List::glpList {
59 $self->compile_point;
60 };
61 $self->{lines_list} = OpenGL::List::glpList {
62 $self->compile_lines;
63 };
64  
65 return 1;
66 }
67  
68 sub display {
69 my $self = shift;
70 my $position = $self->{position};
71  
72 # Translate to the correct location
73 $self->SUPER::display(@_);
74  
75 # The cube is plain opaque full-bright white and ignores lighting
76 OpenGL::glDisable( OpenGL::GL_LIGHTING );
77 OpenGL::glDisable( OpenGL::GL_TEXTURE_2D );
78  
79 # Draw a point at the exact X,Y,Z position and reset translation
80 OpenGL::glCallList( $self->{point_list} );
81  
82 # Translate down to the next lowest integer before drawing the cube
83 my @delta = map { POSIX::floor($_) - $_ } @$position;
84 OpenGL::glTranslatef( @delta );
85 OpenGL::glCallList( $self->{lines_list} );
86  
87 # Lighting is on by default in our 3DWorld application.
88 # Reenable it so each individual lit object doesn't have to
89 # explicitly turn it on.
90 OpenGL::glEnable( OpenGL::GL_TEXTURE_2D );
91 OpenGL::glEnable( OpenGL::GL_LIGHTING );
92
93 return;
94 }
95  
96 # The compilable section of the point display logic
97 sub compile_point {
98 OpenGL::glBlendFunc( OpenGL::GL_SRC_ALPHA, OpenGL::GL_ONE_MINUS_SRC_ALPHA );
99 OpenGL::glEnable( OpenGL::GL_BLEND );
100 OpenGL::glEnable( OpenGL::GL_POINT_SMOOTH );
101 OpenGL::glPointSize( 5 );
102 OpenGL::glColor4f( 1, 1, 1, 1 );
103 OpenGL::glBegin( OpenGL::GL_POINTS );
104 OpenGL::glVertex3f( 0, 0, 0 );
105 OpenGL::glEnd();
106 OpenGL::glDisable( OpenGL::GL_POINT_SMOOTH );
107 OpenGL::glDisable( OpenGL::GL_BLEND );
108 }
109  
110 # The compilable section of the grid cube display logic
111 sub compile_lines {
112 # Enable line smoothing
113 OpenGL::glBlendFunc( OpenGL::GL_SRC_ALPHA, OpenGL::GL_ONE_MINUS_SRC_ALPHA );
114 OpenGL::glEnable( OpenGL::GL_BLEND );
115 OpenGL::glEnable( OpenGL::GL_LINE_SMOOTH );
116  
117 # Draw all the lines in the cube
118 OpenGL::glLineWidth(1);
119 OpenGL::glColor4f( 1, 1, 1, 1 );
120 OpenGL::glBegin( OpenGL::GL_LINES );
121 OpenGL::glVertex3f( 0, 0, 0 ); OpenGL::glVertex3f( 1, 0, 0 );
122 OpenGL::glVertex3f( 0, 0, 0 ); OpenGL::glVertex3f( 0, 1, 0 );
123 OpenGL::glVertex3f( 0, 0, 0 ); OpenGL::glVertex3f( 0, 0, 1 );
124 OpenGL::glVertex3f( 1, 0, 0 ); OpenGL::glVertex3f( 1, 1, 0 );
125 OpenGL::glVertex3f( 1, 0, 0 ); OpenGL::glVertex3f( 1, 0, 1 );
126 OpenGL::glVertex3f( 0, 1, 0 ); OpenGL::glVertex3f( 1, 1, 0 );
127 OpenGL::glVertex3f( 0, 1, 0 ); OpenGL::glVertex3f( 0, 1, 1 );
128 OpenGL::glVertex3f( 0, 0, 1 ); OpenGL::glVertex3f( 1, 0, 1 );
129 OpenGL::glVertex3f( 0, 0, 1 ); OpenGL::glVertex3f( 0, 1, 1 );
130 OpenGL::glVertex3f( 1, 1, 0 ); OpenGL::glVertex3f( 1, 1, 1 );
131 OpenGL::glVertex3f( 1, 0, 1 ); OpenGL::glVertex3f( 1, 1, 1 );
132 OpenGL::glVertex3f( 0, 1, 1 ); OpenGL::glVertex3f( 1, 1, 1 );
133 OpenGL::glEnd();
134  
135 # Disable line smoothing
136 OpenGL::glDisable( OpenGL::GL_LINE_SMOOTH );
137 OpenGL::glDisable( OpenGL::GL_BLEND );
138 }
139  
140 1;
141  
142 =pod
143  
144 =head1 SUPPORT
145  
146 Bugs should be reported via the CPAN bug tracker at
147  
148 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SDL-Tutorial-3DWorld>
149  
150 =head1 AUTHOR
151  
152 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
153  
154 =head1 SEE ALSO
155  
156 L<SDL>, L<OpenGL>
157  
158 =head1 COPYRIGHT
159  
160 Copyright 2010 Adam Kennedy.
161  
162 This program is free software; you can redistribute
163 it and/or modify it under the same terms as Perl itself.
164  
165 The full text of the license can be found in the
166 LICENSE file included with this module.
167  
168 =cut