| 1 | package SDL::Tutorial::3DWorld::RWX; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | SDL::Tutorial::3DWorld::RWX - Support for loading 3D models from RWX files | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | # Create the object but don't load anything | ||
| 12 | my $model = SDL::Tutorial::3DWorld::RWX->new( | ||
| 13 | file => 'mymodel.rwx', | ||
| 14 | ); | ||
| 15 | |||
| 16 | # Load the model into OpenGL | ||
| 17 | $model->init; | ||
| 18 | |||
| 19 | # Render the model into the current scene | ||
| 20 | $model->display; | ||
| 21 | |||
| 22 | =head1 DESCRIPTION | ||
| 23 | |||
| 24 | B<SDL::Tutorial::3DWorld::RWX> provides a basic implementation of a RWX file | ||
| 25 | parser. | ||
| 26 | |||
| 27 | Given a file name, it will load the file and parse the contents directly | ||
| 28 | into a compiled OpenGL display list. | ||
| 29 | |||
| 30 | The OpenGL display list can then be executed directly from the RWX object. | ||
| 31 | |||
| 32 | The current implementation is extremely preliminary and functionality will | ||
| 33 | be gradually fleshed out over time. | ||
| 34 | |||
| 35 | In this initial test implementation, the model will only render as a set of | ||
| 36 | points in space using the pre-existing material settings. | ||
| 37 | |||
| 38 | =cut | ||
| 39 | |||
| 40 | 1 | ▶ | use 5.008; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 41 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 42 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 43 | 1 | ▶ | use IO::File (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 44 | 1 | ▶ | use File::Spec (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 45 | 0 | ▶ | use OpenGL ':all'; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 46 | use OpenGL::List (); | ||
| 47 | use SDL::Tutorial::3DWorld::Mesh (); | ||
| 48 | use SDL::Tutorial::3DWorld::Asset (); | ||
| 49 | use SDL::Tutorial::3DWorld::Model (); | ||
| 50 | |||
| 51 | our $VERSION = '0.33'; | ||
| 52 | our @ISA = 'SDL::Tutorial::3DWorld::Model'; | ||
| 53 | |||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | |||
| 58 | ###################################################################### | ||
| 59 | # Parsing Methods | ||
| 60 | |||
| 61 | sub parse { | ||
| 62 | my $self = shift; | ||
| 63 | my $handle = shift; | ||
| 64 | my $mesh = SDL::Tutorial::3DWorld::Mesh->new; | ||
| 65 | my $offset = 0; | ||
| 66 | my $material = 0; | ||
| 67 | my $ambient = undef; | ||
| 68 | my $diffuse = undef; | ||
| 69 | |||
| 70 | while ( 1 ) { | ||
| 71 | my $line = $handle->getline; | ||
| 72 | last unless defined $line; | ||
| 73 | |||
| 74 | # Remove blank lines, trailing whitespace and comments | ||
| 75 | $line =~ s/\s*(?:#.+)[\012\015]*\z//; | ||
| 76 | $line =~ m/\S/ or next; | ||
| 77 | |||
| 78 | # Parse the dispatch the line | ||
| 79 | my @words = split /\s+/, $line; | ||
| 80 | my $command = lc shift @words; | ||
| 81 | if ( $command eq 'vertex' or $command eq 'vertexext' ) { | ||
| 82 | # Create the vertex | ||
| 83 | my @vertex = map { $_ * 10 } splice @words, 0, 3; | ||
| 84 | my @normal = ( 0, 0, 0 ); | ||
| 85 | my @uv = ( 0, 0 ); | ||
| 86 | if ( @words and lc $words[0] eq 'uv' ) { | ||
| 87 | @uv = @words[1,2]; | ||
| 88 | } | ||
| 89 | $mesh->add_all( \@vertex, \@uv, \@normal ); | ||
| 90 | |||
| 91 | } elsif ( $command eq 'color' ) { | ||
| 92 | my %param = ( color => [ @words ] ); | ||
| 93 | $param{ambient} = $ambient if $ambient; | ||
| 94 | $param{diffuse} = $diffuse if $diffuse; | ||
| 95 | $material = $mesh->add_material( %param ); | ||
| 96 | |||
| 97 | } elsif ( $command eq 'ambient' ) { | ||
| 98 | $material = $mesh->add_material( | ||
| 99 | ambient => [ @words ], | ||
| 100 | ); | ||
| 101 | $ambient = [ @words ]; | ||
| 102 | |||
| 103 | } elsif ( $command eq 'diffuse' ) { | ||
| 104 | $material = $mesh->add_material( | ||
| 105 | diffuse => [ @words ], | ||
| 106 | ); | ||
| 107 | $diffuse = [ @words ], | ||
| 108 | |||
| 109 | } elsif ( $command eq 'opacity' ) { | ||
| 110 | $material = $mesh->add_material( | ||
| 111 | opacity => $words[0], | ||
| 112 | ); | ||
| 113 | if ( $words[0] < 1 ) { | ||
| 114 | $self->{blending} = 1; | ||
| 115 | } | ||
| 116 | |||
| 117 | } elsif ( $command eq 'texture' ) { | ||
| 118 | my $name = shift @words; | ||
| 119 | if ( $name eq 'null' ) { | ||
| 120 | $material = $mesh->add_material( | ||
| 121 | texture => undef, | ||
| 122 | ); | ||
| 123 | } else { | ||
| 124 | my $texture = $self->asset->texture($name); | ||
| 125 | unless ( $texture ) { | ||
| 126 | die "The texture '$name' does not exist"; | ||
| 127 | } | ||
| 128 | $material = $mesh->add_material( | ||
| 129 | texture => $texture, | ||
| 130 | ); | ||
| 131 | } | ||
| 132 | |||
| 133 | } elsif ( $command eq 'triangle' ) { | ||
| 134 | # Add the triangle with the right offset | ||
| 135 | @words = map { $_ + $offset } @words; | ||
| 136 | $mesh->add_triangle( | ||
| 137 | $material, | ||
| 138 | @words, | ||
| 139 | @words, | ||
| 140 | @words, | ||
| 141 | ); | ||
| 142 | |||
| 143 | } elsif ( $command eq 'quad' ) { | ||
| 144 | # Add the quad with the right offset | ||
| 145 | @words = map { $_ + $offset } @words; | ||
| 146 | $mesh->add_quad( | ||
| 147 | $material, | ||
| 148 | @words, | ||
| 149 | @words, | ||
| 150 | @words, | ||
| 151 | ); | ||
| 152 | |||
| 153 | } elsif ( $command eq 'protoend' ) { | ||
| 154 | # Correct the offset | ||
| 155 | $offset = $mesh->max_vertex; | ||
| 156 | |||
| 157 | } else { | ||
| 158 | # Unsupported command, silently ignore | ||
| 159 | } | ||
| 160 | }; | ||
| 161 | |||
| 162 | # Initialise the mesh elements that need it | ||
| 163 | $mesh->init; | ||
| 164 | $self->{box} = [ $mesh->box ]; | ||
| 165 | |||
| 166 | # Generate the display list | ||
| 167 | return OpenGL::List::glpList { | ||
| 168 | $mesh->display; | ||
| 169 | }; | ||
| 170 | } | ||
| 171 | |||
| 172 | 1; | ||
| 173 | |||
| 174 | =pod | ||
| 175 | |||
| 176 | =head1 SUPPORT | ||
| 177 | |||
| 178 | Bugs should be reported via the CPAN bug tracker at | ||
| 179 | |||
| 180 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=OpenGL-RWX> | ||
| 181 | |||
| 182 | =head1 AUTHOR | ||
| 183 | |||
| 184 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||
| 185 | |||
| 186 | =head1 SEE ALSO | ||
| 187 | |||
| 188 | L<OpenGL> | ||
| 189 | |||
| 190 | =head1 COPYRIGHT | ||
| 191 | |||
| 192 | Copyright 2010 Adam Kennedy. | ||
| 193 | |||
| 194 | This program is free software; you can redistribute | ||
| 195 | it and/or modify it under the same terms as Perl itself. | ||
| 196 | |||
| 197 | The full text of the license can be found in the | ||
| 198 | LICENSE file included with this module. | ||
| 199 | |||
| 200 | =cut | ||