1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | CFPlus::Texture - tetxure class for CFPlus |
3 | DC::Texture - tetxure class for CFPlus |
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 |
… | |
… | |
110 | if $cb; |
110 | if $cb; |
111 | |
111 | |
112 | return if $self->{loading}; |
112 | return if $self->{loading}; |
113 | |
113 | |
114 | unless ($GL_VERSION) { |
114 | unless ($GL_VERSION) { |
115 | $self->{was_loaded} = 1; |
115 | $self->{want_upload} = -1; |
116 | return; |
116 | return; |
117 | } |
117 | } |
118 | |
118 | |
119 | unless ($self->{name}) { |
119 | unless ($self->{name}) { |
120 | # $tw,$th texture |
120 | # $tw,$th texture |
121 | # $rw,$rh rendered/used size |
121 | # $rw,$rh rendered/used size |
122 | # $dw,$dh $data |
122 | # $dw,$dh $data |
123 | |
123 | |
124 | my ($data, $dw, $dh); |
124 | my ($data, $dw, $dh); |
125 | |
125 | |
126 | if (exists $self->{data}) { |
126 | if (defined $self->{data}) { |
127 | $data = $self->{data}; |
127 | $data = $self->{data}; |
128 | ($dw, $dh) = @$self{qw(w h)}; |
128 | ($dw, $dh) = @$self{qw(w h)}; |
129 | |
129 | |
130 | } elsif (exists $self->{render_cb}) { |
130 | } elsif ($self->{render_cb}) { |
131 | ($dw, $dh) = @$self{qw(w h)}; |
131 | ($dw, $dh) = @$self{qw(w h)}; |
132 | |
132 | |
133 | } elsif (exists $self->{image}) { |
133 | } elsif (defined $self->{image}) { |
134 | ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type}) |
134 | ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type}) |
135 | = CFPlus::load_image_inline $self->{image}; |
135 | = DC::load_image_inline $self->{image}; |
136 | |
136 | |
137 | $self->{internalformat} ||= $internalformat; |
137 | $self->{internalformat} ||= $internalformat; |
138 | ($dw, $dh) = @$self{qw(w h)}; |
138 | ($dw, $dh) = @$self{qw(w h)}; |
139 | |
139 | |
140 | delete $self->{image} if delete $self->{delete_image}; |
140 | delete $self->{image} if delete $self->{delete_image}; |
141 | |
141 | |
142 | } elsif (exists $self->{tile}) { |
142 | } elsif (defined $self->{tile}) { |
143 | ++$self->{loading}; |
143 | ++$self->{loading}; |
144 | return CFPlus::DB::get tilecache => $self->{tile}, sub { |
144 | return DC::DB::get tilecache => $self->{tile}, sub { |
145 | $self->loading_done ($_[0]); |
|
|
146 | |
|
|
147 | $::MAP->set_texture ($self->{tile}, @$self{qw(name w h s t)}, @{$self->{minified}}) |
|
|
148 | if $::MAP; |
|
|
149 | $::MAPWIDGET->update |
|
|
150 | if $::MAPWIDGET; |
|
|
151 | }; |
|
|
152 | |
|
|
153 | } elsif (exists $self->{path}) { |
|
|
154 | ++$self->{loading}; |
|
|
155 | return CFPlus::DB::read_file $self->{path}, sub { |
|
|
156 | $self->loading_done ($_[0]); |
145 | $self->loading_done ($_[0]); |
157 | }; |
146 | }; |
158 | |
147 | |
|
|
148 | } elsif (defined $self->{path}) { |
|
|
149 | ++$self->{loading}; |
|
|
150 | return DC::DB::read_file $self->{path}, sub { |
|
|
151 | $self->loading_done ($_[0]); |
|
|
152 | }; |
|
|
153 | |
159 | } else { |
154 | } else { |
160 | Carp::confess "tried to create texture that is not data, render or image"; |
155 | Carp::confess "tried to create texture that is not data, render or image"; |
161 | } |
156 | } |
162 | |
157 | |
163 | my ($tw, $th) = ($dw, $dh); |
158 | my ($tw, $th) = ($dw, $dh); |
164 | |
159 | |
165 | defined $data or $self->{render_cb} or die; # some sanity check |
160 | defined $data or $self->{render_cb} or die; # some sanity check |
166 | |
161 | |
167 | $self->{minified} ||= [CFPlus::average $dw, $dh, $data] |
162 | $self->{minified} ||= [DC::average $dw, $dh, $data] |
168 | if $self->{minify}; |
163 | if $self->{minify}; |
169 | |
164 | |
170 | # against rather broken cards we enforce a maximum texture size |
165 | # against rather broken cards we enforce a maximum texture size |
171 | $tw = min $MAX_W, minpot $tw; |
166 | $tw = min $MAX_W, $tw; |
172 | $th = min $MAX_H, minpot $th; |
167 | $th = min $MAX_H, $th; |
173 | |
168 | |
174 | # if only pot-textures are allowed, pot'ify tw/th |
169 | # if only pot-textures are allowed, pot'ify tw/th |
175 | unless ($GL_NPOT && 0) {#d# |
170 | unless ($GL_NPOT) { |
176 | $tw = minpot $tw; |
171 | $tw = DC::minpot $tw; |
177 | $th = minpot $th; |
172 | $th = DC::minpot $th; |
178 | } |
173 | } |
179 | |
174 | |
180 | # now further decrease texture size until the |
175 | # now further decrease texture size until the |
181 | # card does accept it |
176 | # card does accept it |
182 | while (!texture_valid_2d $self->{internalformat}, $tw, $th, $self->{format}, $self->{type}) { |
177 | while (!texture_valid_2d $self->{internalformat}, $tw, $th, $self->{format}, $self->{type}) { |
… | |
… | |
237 | $data; |
232 | $data; |
238 | glPixelZoom 1, 1; |
233 | glPixelZoom 1, 1; |
239 | } |
234 | } |
240 | } |
235 | } |
241 | |
236 | |
|
|
237 | if ((my $name = delete $self->{want_upload}) > 0) { |
|
|
238 | $self->{name} = $name; |
|
|
239 | } |
|
|
240 | |
242 | glBindTexture GL_TEXTURE_2D, $self->{name} ||= glGenTexture; |
241 | glBindTexture GL_TEXTURE_2D, $self->{name} ||= glGenTexture; |
243 | |
242 | |
244 | if ($self->{wrap}) { |
243 | if ($self->{wrap}) { |
245 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
244 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
246 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
245 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
… | |
… | |
296 | $tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; |
295 | $tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; |
297 | } |
296 | } |
298 | |
297 | |
299 | $self->{s} = $rw / $tw; |
298 | $self->{s} = $rw / $tw; |
300 | $self->{t} = $rh / $th; |
299 | $self->{t} = $rh / $th; |
|
|
300 | |
|
|
301 | if ($self->{tile}) { |
|
|
302 | $::MAP->set_texture ($self->{tile}, @$self{qw(name w h s t)}, @{$self->{minified}}) |
|
|
303 | if $::MAP; |
|
|
304 | $::MAPWIDGET->update |
|
|
305 | if $::MAPWIDGET; |
|
|
306 | } |
301 | } |
307 | } |
302 | |
308 | |
303 | $_->($self) |
309 | $_->($self) |
304 | for @{ (delete $self->{upload_done}) || [] }; |
310 | for @{ (delete $self->{upload_done}) || [] }; |
305 | } |
311 | } |
… | |
… | |
317 | delete $TEXTURES{$self+0}; |
323 | delete $TEXTURES{$self+0}; |
318 | |
324 | |
319 | $self->unload; |
325 | $self->unload; |
320 | } |
326 | } |
321 | |
327 | |
322 | $CFPlus::OpenGL::INIT_HOOK{"CFPlus::Texture"} = sub { |
328 | $DC::OpenGL::INIT_HOOK{"DC::Texture"} = sub { |
323 | for (values %TEXTURES) { |
329 | for (values %TEXTURES) { |
324 | next unless delete $_->{was_loaded}; |
330 | $_->upload if $_->{want_upload}; |
325 | $_->upload; |
|
|
326 | } |
331 | } |
327 | }; |
332 | }; |
328 | |
333 | |
329 | $CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Texture"} = sub { |
334 | $DC::OpenGL::SHUTDOWN_HOOK{"DC::Texture"} = sub { |
330 | for (values %TEXTURES) { |
335 | for (values %TEXTURES) { |
331 | next unless $_->{name}; |
336 | next unless $_->{name}; |
332 | $_->{was_loaded} = 1; |
337 | $_->{want_upload} = $_->{name}; |
333 | $_->unload; |
338 | $_->unload; |
334 | } |
339 | } |
335 | }; |
340 | }; |
336 | |
341 | |
337 | 1; |
342 | 1; |