1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | CFPlus::Texture - tetxure class for CFPlus |
3 | DC::Texture - tetxure class for Deliantra-Client |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use CFPlus::Texture; |
7 | use DC::Texture; |
8 | |
8 | |
9 | =head1 DESCRIPTION |
9 | =head1 DESCRIPTION |
10 | |
10 | |
11 | =over 4 |
11 | =over 4 |
12 | |
12 | |
13 | =cut |
13 | =cut |
14 | |
14 | |
15 | package CFPlus::Texture; |
15 | package DC::Texture; |
16 | |
16 | |
17 | use strict; |
17 | use strict; |
18 | |
18 | |
19 | use List::Util qw(max min); |
19 | use List::Util qw(max min); |
20 | use CFPlus::OpenGL; |
20 | use DC::OpenGL; |
21 | |
21 | |
22 | my %TEXTURES; |
22 | my %TEXTURES; |
23 | my ($MAX_W, $MAX_H) = (4096, 4096); # maximum texture size attempted by this module |
23 | my ($MAX_W, $MAX_H) = (4096, 4096); # maximum texture size attempted by this module |
24 | |
24 | |
25 | sub new { |
25 | sub new { |
… | |
… | |
30 | format => GL_RGBA, |
30 | format => GL_RGBA, |
31 | type => GL_UNSIGNED_BYTE, |
31 | type => GL_UNSIGNED_BYTE, |
32 | %data, |
32 | %data, |
33 | }, $class; |
33 | }, $class; |
34 | |
34 | |
35 | CFPlus::weaken ($TEXTURES{$self+0} = $self); |
35 | DC::weaken ($TEXTURES{$self+0} = $self); |
36 | |
36 | |
37 | $self->upload |
37 | $self->upload |
38 | unless delete $self->{delay}; |
38 | unless delete $self->{delay}; |
39 | |
39 | |
40 | $self |
40 | $self |
… | |
… | |
47 | unless defined $image; |
47 | unless defined $image; |
48 | |
48 | |
49 | $class->new (image => $image, internalformat => undef, %arg) |
49 | $class->new (image => $image, internalformat => undef, %arg) |
50 | } |
50 | } |
51 | |
51 | |
52 | |
|
|
53 | sub new_from_file { |
52 | sub new_from_resource { |
54 | my ($class, $path, %arg) = @_; |
53 | my ($class, $path, %arg) = @_; |
55 | |
54 | |
56 | open my $fh, "<:raw", $path |
55 | $class->new (resource_path => $path, internalformat => undef, %arg) |
57 | or die "$path: $!"; |
|
|
58 | |
|
|
59 | local $/; |
|
|
60 | $class->new_from_image (<$fh>, %arg) |
|
|
61 | } |
56 | } |
62 | |
57 | |
63 | #sub new_from_surface { |
58 | #sub new_from_surface { |
64 | # my ($class, $surface) = @_; |
59 | # my ($class, $surface) = @_; |
65 | # |
60 | # |
… | |
… | |
110 | if $cb; |
105 | if $cb; |
111 | |
106 | |
112 | return if $self->{loading}; |
107 | return if $self->{loading}; |
113 | |
108 | |
114 | unless ($GL_VERSION) { |
109 | unless ($GL_VERSION) { |
115 | $self->{was_loaded} = 1; |
110 | $self->{want_upload} = -1; |
116 | return; |
111 | return; |
117 | } |
112 | } |
118 | |
113 | |
119 | unless ($self->{name}) { |
114 | unless ($self->{name}) { |
120 | # $tw,$th texture |
115 | # $tw,$th texture |
121 | # $rw,$rh rendered/used size |
116 | # $rw,$rh rendered/used size |
122 | # $dw,$dh $data |
117 | # $dw,$dh $data |
123 | |
118 | |
124 | my ($data, $dw, $dh); |
119 | my ($data, $dw, $dh); |
125 | |
120 | |
|
|
121 | if (exists $self->{resource_path}) { |
|
|
122 | open my $fh, "<:raw", DC::find_rcfile $self->{resource_path}; |
|
|
123 | local $/; |
|
|
124 | delete $self->{internalformat}; |
|
|
125 | $self->{image} = <$fh>; |
|
|
126 | $self->{delete_image} = 1; |
|
|
127 | } |
|
|
128 | |
126 | if (defined $self->{data}) { |
129 | if (defined $self->{data}) { |
127 | $data = $self->{data}; |
130 | $data = $self->{data}; |
128 | ($dw, $dh) = @$self{qw(w h)}; |
131 | ($dw, $dh) = @$self{qw(w h)}; |
129 | |
132 | |
130 | } elsif ($self->{render_cb}) { |
133 | } elsif ($self->{render_cb}) { |
131 | ($dw, $dh) = @$self{qw(w h)}; |
134 | ($dw, $dh) = @$self{qw(w h)}; |
132 | |
135 | |
133 | } elsif (defined $self->{image}) { |
136 | } elsif (defined $self->{image}) { |
134 | ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type}) |
137 | ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type}) |
135 | = CFPlus::load_image_inline $self->{image}; |
138 | = DC::load_image_inline $self->{image}; |
136 | |
139 | |
137 | $self->{internalformat} ||= $internalformat; |
140 | $self->{internalformat} ||= $internalformat; |
138 | ($dw, $dh) = @$self{qw(w h)}; |
141 | ($dw, $dh) = @$self{qw(w h)}; |
139 | |
142 | |
140 | delete $self->{image} if delete $self->{delete_image}; |
143 | delete $self->{image} if delete $self->{delete_image}; |
141 | |
144 | |
142 | } elsif (defined $self->{tile}) { |
145 | } elsif (defined $self->{tile}) { |
143 | ++$self->{loading}; |
146 | ++$self->{loading}; |
144 | return CFPlus::DB::get tilecache => $self->{tile}, sub { |
147 | return DC::DB::get tilecache => $self->{tile}, sub { |
145 | $self->loading_done ($_[0]); |
148 | $self->loading_done ($_[0]); |
146 | }; |
149 | }; |
147 | |
150 | |
148 | } elsif (defined $self->{path}) { |
151 | } elsif (defined $self->{path}) { |
149 | ++$self->{loading}; |
152 | ++$self->{loading}; |
150 | return CFPlus::DB::read_file $self->{path}, sub { |
153 | return DC::DB::read_file $self->{path}, sub { |
151 | $self->loading_done ($_[0]); |
154 | $self->loading_done ($_[0]); |
152 | }; |
155 | }; |
153 | |
156 | |
154 | } else { |
157 | } else { |
155 | Carp::confess "tried to create texture that is not data, render or image"; |
158 | Carp::confess "tried to create texture that is not data, render or image"; |
… | |
… | |
157 | |
160 | |
158 | my ($tw, $th) = ($dw, $dh); |
161 | my ($tw, $th) = ($dw, $dh); |
159 | |
162 | |
160 | defined $data or $self->{render_cb} or die; # some sanity check |
163 | defined $data or $self->{render_cb} or die; # some sanity check |
161 | |
164 | |
162 | $self->{minified} ||= [CFPlus::average $dw, $dh, $data] |
165 | $self->{minified} ||= [DC::average $dw, $dh, $data] |
163 | if $self->{minify}; |
166 | if $self->{minify}; |
164 | |
167 | |
165 | # against rather broken cards we enforce a maximum texture size |
168 | # against rather broken cards we enforce a maximum texture size |
166 | $tw = min $MAX_W, $tw; |
169 | $tw = min $MAX_W, $tw; |
167 | $th = min $MAX_H, $th; |
170 | $th = min $MAX_H, $th; |
168 | |
171 | |
169 | # if only pot-textures are allowed, pot'ify tw/th |
172 | # if only pot-textures are allowed, pot'ify tw/th |
170 | unless ($GL_NPOT) { |
173 | unless ($GL_NPOT) { |
171 | $tw = CFPlus::minpot $tw; |
174 | $tw = DC::minpot $tw; |
172 | $th = CFPlus::minpot $th; |
175 | $th = DC::minpot $th; |
173 | } |
176 | } |
174 | |
177 | |
175 | # now further decrease texture size until the |
178 | # now further decrease texture size until the |
176 | # card does accept it |
179 | # card does accept it |
177 | while (!texture_valid_2d $self->{internalformat}, $tw, $th, $self->{format}, $self->{type}) { |
180 | while (!texture_valid_2d $self->{internalformat}, $tw, $th, $self->{format}, $self->{type}) { |
… | |
… | |
232 | $data; |
235 | $data; |
233 | glPixelZoom 1, 1; |
236 | glPixelZoom 1, 1; |
234 | } |
237 | } |
235 | } |
238 | } |
236 | |
239 | |
|
|
240 | if ((my $name = delete $self->{want_upload}) > 0) { |
|
|
241 | $self->{name} = $name; |
|
|
242 | } |
|
|
243 | |
237 | glBindTexture GL_TEXTURE_2D, $self->{name} ||= glGenTexture; |
244 | glBindTexture GL_TEXTURE_2D, $self->{name} ||= glGenTexture; |
238 | |
245 | |
239 | if ($self->{wrap}) { |
246 | if ($self->{wrap}) { |
240 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
247 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
241 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
248 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
… | |
… | |
319 | delete $TEXTURES{$self+0}; |
326 | delete $TEXTURES{$self+0}; |
320 | |
327 | |
321 | $self->unload; |
328 | $self->unload; |
322 | } |
329 | } |
323 | |
330 | |
324 | $CFPlus::OpenGL::INIT_HOOK{"CFPlus::Texture"} = sub { |
331 | $DC::OpenGL::INIT_HOOK{"DC::Texture"} = sub { |
325 | for (values %TEXTURES) { |
332 | for my $tex (values %TEXTURES) { |
326 | next unless delete $_->{was_loaded}; |
333 | if (my $name = $tex->{want_upload}) { |
327 | $_->upload; |
334 | $tex->upload; |
|
|
335 | |
|
|
336 | if ($tex->{loading} && $name > 0) { |
|
|
337 | # if loading is delayed we still have to allocate the texture name |
|
|
338 | glBindTexture GL_TEXTURE_2D, $name; |
|
|
339 | glTexImage2D GL_TEXTURE_2D, 0, GL_ALPHA, 0, 0, 0, GL_ALPHA, GL_UNSIGNED_BYTE; |
|
|
340 | } |
|
|
341 | } |
328 | } |
342 | } |
329 | }; |
343 | }; |
330 | |
344 | |
331 | $CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Texture"} = sub { |
345 | $DC::OpenGL::SHUTDOWN_HOOK{"DC::Texture"} = sub { |
332 | for (values %TEXTURES) { |
346 | for (values %TEXTURES) { |
333 | next unless $_->{name}; |
347 | next unless $_->{name}; |
334 | $_->{was_loaded} = 1; |
348 | $_->{want_upload} = $_->{name}; |
335 | $_->unload; |
349 | $_->unload; |
336 | } |
350 | } |
337 | }; |
351 | }; |
338 | |
352 | |
339 | 1; |
353 | 1; |