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 |
… | |
… | |
130 | } elsif ($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 (defined $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 (defined $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]); |
145 | $self->loading_done ($_[0]); |
146 | }; |
146 | }; |
147 | |
147 | |
148 | } elsif (defined $self->{path}) { |
148 | } elsif (defined $self->{path}) { |
149 | ++$self->{loading}; |
149 | ++$self->{loading}; |
150 | return CFPlus::DB::read_file $self->{path}, sub { |
150 | return DC::DB::read_file $self->{path}, sub { |
151 | $self->loading_done ($_[0]); |
151 | $self->loading_done ($_[0]); |
152 | }; |
152 | }; |
153 | |
153 | |
154 | } else { |
154 | } else { |
155 | 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"; |
… | |
… | |
157 | |
157 | |
158 | my ($tw, $th) = ($dw, $dh); |
158 | my ($tw, $th) = ($dw, $dh); |
159 | |
159 | |
160 | defined $data or $self->{render_cb} or die; # some sanity check |
160 | defined $data or $self->{render_cb} or die; # some sanity check |
161 | |
161 | |
162 | $self->{minified} ||= [CFPlus::average $dw, $dh, $data] |
162 | $self->{minified} ||= [DC::average $dw, $dh, $data] |
163 | if $self->{minify}; |
163 | if $self->{minify}; |
164 | |
164 | |
165 | # against rather broken cards we enforce a maximum texture size |
165 | # against rather broken cards we enforce a maximum texture size |
166 | $tw = min $MAX_W, $tw; |
166 | $tw = min $MAX_W, $tw; |
167 | $th = min $MAX_H, $th; |
167 | $th = min $MAX_H, $th; |
168 | |
168 | |
169 | # if only pot-textures are allowed, pot'ify tw/th |
169 | # if only pot-textures are allowed, pot'ify tw/th |
170 | unless ($GL_NPOT) { |
170 | unless ($GL_NPOT) { |
171 | $tw = CFPlus::minpot $tw; |
171 | $tw = DC::minpot $tw; |
172 | $th = CFPlus::minpot $th; |
172 | $th = DC::minpot $th; |
173 | } |
173 | } |
174 | |
174 | |
175 | # now further decrease texture size until the |
175 | # now further decrease texture size until the |
176 | # card does accept it |
176 | # card does accept it |
177 | 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}) { |
… | |
… | |
232 | $data; |
232 | $data; |
233 | glPixelZoom 1, 1; |
233 | glPixelZoom 1, 1; |
234 | } |
234 | } |
235 | } |
235 | } |
236 | |
236 | |
|
|
237 | if ((my $name = delete $self->{want_upload}) > 0) { |
|
|
238 | $self->{name} = $name; |
|
|
239 | } |
|
|
240 | |
237 | glBindTexture GL_TEXTURE_2D, $self->{name} ||= glGenTexture; |
241 | glBindTexture GL_TEXTURE_2D, $self->{name} ||= glGenTexture; |
238 | |
242 | |
239 | if ($self->{wrap}) { |
243 | if ($self->{wrap}) { |
240 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
244 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
241 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
245 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
… | |
… | |
319 | delete $TEXTURES{$self+0}; |
323 | delete $TEXTURES{$self+0}; |
320 | |
324 | |
321 | $self->unload; |
325 | $self->unload; |
322 | } |
326 | } |
323 | |
327 | |
324 | $CFPlus::OpenGL::INIT_HOOK{"CFPlus::Texture"} = sub { |
328 | $DC::OpenGL::INIT_HOOK{"DC::Texture"} = sub { |
325 | for (values %TEXTURES) { |
329 | for (values %TEXTURES) { |
326 | next unless delete $_->{was_loaded}; |
330 | $_->upload if $_->{want_upload}; |
327 | $_->upload; |
|
|
328 | } |
331 | } |
329 | }; |
332 | }; |
330 | |
333 | |
331 | $CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Texture"} = sub { |
334 | $DC::OpenGL::SHUTDOWN_HOOK{"DC::Texture"} = sub { |
332 | for (values %TEXTURES) { |
335 | for (values %TEXTURES) { |
333 | next unless $_->{name}; |
336 | next unless $_->{name}; |
334 | $_->{was_loaded} = 1; |
337 | $_->{want_upload} = $_->{name}; |
335 | $_->unload; |
338 | $_->unload; |
336 | } |
339 | } |
337 | }; |
340 | }; |
338 | |
341 | |
339 | 1; |
342 | 1; |