| 1 | package SDL::Tutorial::3DWorld::OBJ; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | SDL::Tutorial::3DWorld::OBJ - Support for loading 3D models from OBJ files | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | # Create the object but don't load anything | ||
| 12 | my $model = SDL::Tutorial::3DWorld::OBJ->new( | ||
| 13 | file => 'mymodel.obj', | ||
| 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::OBJ> provides a basic implementation of a OBJ 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 OBJ 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 SDL::Tutorial::3DWorld::Mesh (); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 46 | use SDL::Tutorial::3DWorld::Asset (); | ||
| 47 | use SDL::Tutorial::3DWorld::Model (); | ||
| 48 | |||
| 49 | our $VERSION = '0.33'; | ||
| 50 | our @ISA = 'SDL::Tutorial::3DWorld::Model'; | ||
| 51 | |||
| 52 | |||
| 53 | |||
| 54 | |||
| 55 | |||
| 56 | ###################################################################### | ||
| 57 | # Parsing Methods | ||
| 58 | |||
| 59 | sub parse { | ||
| 60 | my $self = shift; | ||
| 61 | my $handle = shift; | ||
| 62 | my $mesh = SDL::Tutorial::3DWorld::Mesh->new; | ||
| 63 | my $m = 0; # Material index position | ||
| 64 | |||
| 65 | # Fill the mesh | ||
| 66 | while ( 1 ) { | ||
| 67 | my $line = $handle->getline; | ||
| 68 | last unless defined $line; | ||
| 69 | |||
| 70 | # Remove blank lines, trailing whitespace and comments | ||
| 71 | $line =~ s/\s*(?:#.+)[\012\015]*\z//; | ||
| 72 | $line =~ m/\S/ or next; | ||
| 73 | |||
| 74 | # Parse the dispatch the line | ||
| 75 | my @words = split /\s+/, $line; | ||
| 76 | my $command = lc shift @words; | ||
| 77 | if ( $command eq 'v' ) { | ||
| 78 | # Create the vertex | ||
| 79 | $mesh->add_vertex( @words ); | ||
| 80 | |||
| 81 | } elsif ( $command eq 'vt' ) { | ||
| 82 | # Create the texture location. | ||
| 83 | # We only support two-dimensional textures, | ||
| 84 | # so don't pass more than two params. | ||
| 85 | $mesh->add_uv( @words[0,1] ); | ||
| 86 | |||
| 87 | } elsif ( $command eq 'vn' ) { | ||
| 88 | # Normal vectors might not be unit so we need to | ||
| 89 | # unitise before we add it to the mesh. | ||
| 90 | my $l = sqrt( $words[0] ** 2 + $words[1] ** 2 + $words[2] ** 2 ); | ||
| 91 | $mesh->add_normal( | ||
| 92 | $words[0] / $l, | ||
| 93 | $words[1] / $l, | ||
| 94 | $words[2] / $l, | ||
| 95 | ); | ||
| 96 | |||
| 97 | } elsif ( $command eq 'f' ) { | ||
| 98 | if ( @words == 3 ) { | ||
| 99 | my @i = map { split /\//, $_ } @words; | ||
| 100 | if ( @i == 3 ) { | ||
| 101 | # f v1 v2 v3 | ||
| 102 | $mesh->add_triangle( $m, @i ); | ||
| 103 | } elsif ( @i == 6 ) { | ||
| 104 | # f v1/vt1 v2/vt2 v3/vt3 | ||
| 105 | $mesh->add_triangle( $m, @i[0,2,4,1,3,5] ); | ||
| 106 | } elsif ( @i == 9 ) { | ||
| 107 | # f v1/vt1/vn1 v2/vt2/vn2 v3/vt3/vn3 | ||
| 108 | # f v1//vn1 v2//vn2 v3//vn3 | ||
| 109 | $mesh->add_triangle( $m, @i[0,3,6,1,4,7,2,5,8] ); | ||
| 110 | } else { | ||
| 111 | die "Unsuppored face '$line'"; | ||
| 112 | } | ||
| 113 | } elsif ( @words == 4 ) { | ||
| 114 | my @i = map { split /\//, $_ } @words; | ||
| 115 | if ( @i == 4 ) { | ||
| 116 | # f v1 v2 v3 v4 | ||
| 117 | $mesh->add_quad( $m, @i ); | ||
| 118 | } elsif ( @i == 8 ) { | ||
| 119 | # f v1/vt1 v2/vt2 v3/vt3 v4/vt4 | ||
| 120 | $mesh->add_quad( $m, @i[0,2,4,6,1,3,5,7] ); | ||
| 121 | } elsif ( @i == 12 ) { | ||
| 122 | # f v1/vt1/vn1 v2/vt2/vn2 v3/vt3/vn3 v4/vt4/vn4 | ||
| 123 | # f v1//vn1 v2//vn2 v3//vn3 /v4//vn4 | ||
| 124 | $mesh->add_quad( $m, @i[0,3,6,9,1,4,7,10,2,5,8,11] ); | ||
| 125 | } else { | ||
| 126 | die "Unsuppored face '$line'"; | ||
| 127 | } | ||
| 128 | } else { | ||
| 129 | die "Unsuppored face '$line'"; | ||
| 130 | } | ||
| 131 | |||
| 132 | } elsif ( $command eq 'mtllib' and not $self->{plain} ) { | ||
| 133 | # Load the mtllib | ||
| 134 | my $mtl = $self->asset->mtl( $words[0] ); | ||
| 135 | $mtl->init; | ||
| 136 | |||
| 137 | } elsif ( $command eq 'usemtl' and not $self->{plain} ) { | ||
| 138 | # Load a material from the mtl file | ||
| 139 | my $name = shift @words; | ||
| 140 | my $object = $self->asset->material($name); | ||
| 141 | $m = $mesh->add_material($object); | ||
| 142 | |||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | # Initialise the mesh elements that need it | ||
| 147 | $mesh->init; | ||
| 148 | $self->{box} = [ $mesh->box ]; | ||
| 149 | |||
| 150 | # Generate the display list | ||
| 151 | return $mesh->as_list; | ||
| 152 | } | ||
| 153 | |||
| 154 | 1; | ||
| 155 | |||
| 156 | =pod | ||
| 157 | |||
| 158 | =head1 SUPPORT | ||
| 159 | |||
| 160 | Bugs should be reported via the CPAN bug tracker at | ||
| 161 | |||
| 162 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=OpenGL-RWX> | ||
| 163 | |||
| 164 | =head1 AUTHOR | ||
| 165 | |||
| 166 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||
| 167 | |||
| 168 | =head1 SEE ALSO | ||
| 169 | |||
| 170 | L<OpenGL> | ||
| 171 | |||
| 172 | =head1 COPYRIGHT | ||
| 173 | |||
| 174 | Copyright 2010 Adam Kennedy. | ||
| 175 | |||
| 176 | This program is free software; you can redistribute | ||
| 177 | it and/or modify it under the same terms as Perl itself. | ||
| 178 | |||
| 179 | The full text of the license can be found in the | ||
| 180 | LICENSE file included with this module. | ||
| 181 | |||
| 182 | =cut | ||