| 1 | package SDL::Tutorial::3DWorld::3DS; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | SDL::Tutorial::3DWorld::3DS - Support for loading 3D models from 3DS files | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | # Create the object but don't load anything | ||
| 12 | my $model = SDL::Tutorial::3DWorld::3DS->new( | ||
| 13 | file => 'mymodel.3ds', | ||
| 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::3DS> provides a basic implementation of a 3DS 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 3DS 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 | use 5.008; | ||
| 41 | use strict; | ||
| 42 | use warnings; | ||
| 43 | use IO::File (); | ||
| 44 | use File::Spec (); | ||
| 45 | use SDL::Tutorial::3DWorld::Mesh (); | ||
| 46 | use SDL::Tutorial::3DWorld::Asset (); | ||
| 47 | use SDL::Tutorial::3DWorld::Model (); | ||
| 48 | use SDL::Tutorial::3DWorld::3DS::Chunk (); | ||
| 49 | |||
| 50 | our $VERSION = '0.33'; | ||
| 51 | our @ISA = 'SDL::Tutorial::3DWorld::Model'; | ||
| 52 | |||
| 53 | # Chunk IDs | ||
| 54 | my %NAME2ID = ( | ||
| 55 | # Primary chunk | ||
| 56 | MAIN3DS => '4d4d', | ||
| 57 | ); | ||
| 58 | my %ID2NAME = map { | ||
| 59 | $NAME2ID{$_} => $_ | ||
| 60 | } %NAME2ID; | ||
| 61 | |||
| 62 | |||
| 63 | |||
| 64 | |||
| 65 | |||
| 66 | ###################################################################### | ||
| 67 | # Parsing Methods | ||
| 68 | |||
| 69 | sub parse { | ||
| 70 | my $self = shift; | ||
| 71 | my $mesh = SDL::Tutorial::3DWorld::Mesh->new; | ||
| 72 | my $handle = shift; | ||
| 73 | unless ( $handle->can('seek') ) { | ||
| 74 | die "File handle is not seekable"; | ||
| 75 | } | ||
| 76 | |||
| 77 | # Fetch the top chunk | ||
| 78 | my $chunk = $self->chunk( $handle, 0 ); | ||
| 79 | my @stack = ( $chunk ); | ||
| 80 | while ( @stack ) { | ||
| 81 | my $parent = $stack[-1]; | ||
| 82 | my $start = $parent->child_start; | ||
| 83 | my $child = $self->chunk( $handle, $start ); | ||
| 84 | 1; | ||
| 85 | } | ||
| 86 | |||
| 87 | # Initialise the mesh elements that need it | ||
| 88 | $mesh->init; | ||
| 89 | $self->{box} = [ $mesh->box ]; | ||
| 90 | |||
| 91 | # Generate the display list | ||
| 92 | $mesh->as_list; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub chunk { | ||
| 96 | my $self = shift; | ||
| 97 | my $handle = shift; | ||
| 98 | my $start = shift; | ||
| 99 | |||
| 100 | # Read the head block | ||
| 101 | my $buffer = ''; | ||
| 102 | $handle->seek( $start, 0 ); | ||
| 103 | $handle->sysread( $buffer, 6 ); | ||
| 104 | |||
| 105 | # Parse the head block | ||
| 106 | my ( $type, $bytes ) = unpack( 'H4 L', $buffer ); | ||
| 107 | return SDL::Tutorial::3DWorld::3DS::Chunk->new( | ||
| 108 | type => $type, | ||
| 109 | start => $start, | ||
| 110 | bytes => $bytes, | ||
| 111 | ); | ||
| 112 | } | ||
| 113 | |||
| 114 | 1; | ||
| 115 | |||
| 116 | =pod | ||
| 117 | |||
| 118 | =head1 SUPPORT | ||
| 119 | |||
| 120 | Bugs should be reported via the CPAN bug tracker at | ||
| 121 | |||
| 122 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=OpenGL-RWX> | ||
| 123 | |||
| 124 | =head1 AUTHOR | ||
| 125 | |||
| 126 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||
| 127 | |||
| 128 | =head1 SEE ALSO | ||
| 129 | |||
| 130 | L<OpenGL> | ||
| 131 | |||
| 132 | =head1 COPYRIGHT | ||
| 133 | |||
| 134 | Copyright 2010 Adam Kennedy. | ||
| 135 | |||
| 136 | This program is free software; you can redistribute | ||
| 137 | it and/or modify it under the same terms as Perl itself. | ||
| 138 | |||
| 139 | The full text of the license can be found in the | ||
| 140 | LICENSE file included with this module. | ||
| 141 | |||
| 142 | =cut |