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 | $self->{image} = <$fh>; |
|
|
125 | $self->{delete_image} = 1; |
|
|
126 | } |
|
|
127 | |
126 | if (exists $self->{data}) { |
128 | if (defined $self->{data}) { |
127 | $data = $self->{data}; |
129 | $data = $self->{data}; |
128 | ($dw, $dh) = @$self{qw(w h)}; |
130 | ($dw, $dh) = @$self{qw(w h)}; |
129 | |
131 | |
130 | } elsif (exists $self->{render_cb}) { |
132 | } elsif ($self->{render_cb}) { |
131 | ($dw, $dh) = @$self{qw(w h)}; |
133 | ($dw, $dh) = @$self{qw(w h)}; |
132 | |
134 | |
133 | } elsif (exists $self->{image}) { |
135 | } elsif (defined $self->{image}) { |
134 | ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type}) |
136 | ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type}) |
135 | = CFPlus::load_image_inline $self->{image}; |
137 | = DC::load_image_inline $self->{image}; |
136 | |
138 | |
137 | $self->{internalformat} ||= $internalformat; |
139 | $self->{internalformat} ||= $internalformat; |
138 | ($dw, $dh) = @$self{qw(w h)}; |
140 | ($dw, $dh) = @$self{qw(w h)}; |
139 | |
141 | |
140 | delete $self->{image} if delete $self->{delete_image}; |
142 | delete $self->{image} if delete $self->{delete_image}; |
141 | |
143 | |
142 | } elsif (exists $self->{tile}) { |
144 | } elsif (defined $self->{tile}) { |
143 | ++$self->{loading}; |
145 | ++$self->{loading}; |
144 | return CFPlus::DB::get tilecache => $self->{tile}, sub { |
146 | 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]); |
147 | $self->loading_done ($_[0]); |
157 | }; |
148 | }; |
158 | |
149 | |
|
|
150 | } elsif (defined $self->{path}) { |
|
|
151 | ++$self->{loading}; |
|
|
152 | return DC::DB::read_file $self->{path}, sub { |
|
|
153 | $self->loading_done ($_[0]); |
|
|
154 | }; |
|
|
155 | |
159 | } else { |
156 | } else { |
160 | Carp::confess "tried to create texture that is not data, render or image"; |
157 | Carp::confess "tried to create texture that is not data, render or image"; |
161 | } |
158 | } |
162 | |
159 | |
163 | my ($tw, $th) = ($dw, $dh); |
160 | my ($tw, $th) = ($dw, $dh); |
164 | |
161 | |
165 | defined $data or $self->{render_cb} or die; # some sanity check |
162 | defined $data or $self->{render_cb} or die; # some sanity check |
166 | |
163 | |
167 | $self->{minified} ||= [CFPlus::average $dw, $dh, $data] |
164 | $self->{minified} ||= [DC::average $dw, $dh, $data] |
168 | if $self->{minify}; |
165 | if $self->{minify}; |
169 | |
166 | |
170 | # against rather broken cards we enforce a maximum texture size |
167 | # against rather broken cards we enforce a maximum texture size |
171 | $tw = min $MAX_W, minpot $tw; |
168 | $tw = min $MAX_W, $tw; |
172 | $th = min $MAX_H, minpot $th; |
169 | $th = min $MAX_H, $th; |
173 | |
170 | |
174 | # if only pot-textures are allowed, pot'ify tw/th |
171 | # if only pot-textures are allowed, pot'ify tw/th |
175 | unless ($GL_NPOT && 0) {#d# |
172 | unless ($GL_NPOT) { |
176 | $tw = minpot $tw; |
173 | $tw = DC::minpot $tw; |
177 | $th = minpot $th; |
174 | $th = DC::minpot $th; |
178 | } |
175 | } |
179 | |
176 | |
180 | # now further decrease texture size until the |
177 | # now further decrease texture size until the |
181 | # card does accept it |
178 | # card does accept it |
182 | while (!texture_valid_2d $self->{internalformat}, $tw, $th, $self->{format}, $self->{type}) { |
179 | while (!texture_valid_2d $self->{internalformat}, $tw, $th, $self->{format}, $self->{type}) { |
… | |
… | |
237 | $data; |
234 | $data; |
238 | glPixelZoom 1, 1; |
235 | glPixelZoom 1, 1; |
239 | } |
236 | } |
240 | } |
237 | } |
241 | |
238 | |
|
|
239 | if ((my $name = delete $self->{want_upload}) > 0) { |
|
|
240 | $self->{name} = $name; |
|
|
241 | } |
|
|
242 | |
242 | glBindTexture GL_TEXTURE_2D, $self->{name} ||= glGenTexture; |
243 | glBindTexture GL_TEXTURE_2D, $self->{name} ||= glGenTexture; |
243 | |
244 | |
244 | if ($self->{wrap}) { |
245 | if ($self->{wrap}) { |
245 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
246 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
246 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
247 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
… | |
… | |
273 | gl_check "copying to texture %dx%d if=%x", |
274 | gl_check "copying to texture %dx%d if=%x", |
274 | $tw, $th, $self->{internalformat}; |
275 | $tw, $th, $self->{internalformat}; |
275 | |
276 | |
276 | #glDisable GL_SCISSOR_TEST; |
277 | #glDisable GL_SCISSOR_TEST; |
277 | } else { |
278 | } else { |
|
|
279 | my $if = $self->{internalformat}; |
|
|
280 | |
|
|
281 | if ($GL_COMPRESS && $::CFG->{texture_compression}) { |
|
|
282 | if ($if == GL_RGB) { |
|
|
283 | $if = GL_COMPRESSED_RGB_ARB; |
|
|
284 | } elsif ($if == GL_RGBA) { |
|
|
285 | $if = GL_COMPRESSED_RGBA_ARB; |
|
|
286 | } |
|
|
287 | } |
|
|
288 | |
278 | glTexImage2D GL_TEXTURE_2D, 0, |
289 | glTexImage2D GL_TEXTURE_2D, 0, |
279 | $self->{internalformat}, |
290 | $if, |
280 | $dw, $dh, |
291 | $dw, $dh, |
281 | 0, |
292 | 0, |
282 | $self->{format}, |
293 | $self->{format}, |
283 | $self->{type}, |
294 | $self->{type}, |
284 | $data; |
295 | $data; |
… | |
… | |
286 | $tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; |
297 | $tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; |
287 | } |
298 | } |
288 | |
299 | |
289 | $self->{s} = $rw / $tw; |
300 | $self->{s} = $rw / $tw; |
290 | $self->{t} = $rh / $th; |
301 | $self->{t} = $rh / $th; |
|
|
302 | |
|
|
303 | if ($self->{tile}) { |
|
|
304 | $::MAP->set_texture ($self->{tile}, @$self{qw(name w h s t)}, @{$self->{minified}}) |
|
|
305 | if $::MAP; |
|
|
306 | $::MAPWIDGET->update |
|
|
307 | if $::MAPWIDGET; |
|
|
308 | } |
291 | } |
309 | } |
292 | |
310 | |
293 | $_->($self) |
311 | $_->($self) |
294 | for @{ (delete $self->{upload_done}) || [] }; |
312 | for @{ (delete $self->{upload_done}) || [] }; |
295 | } |
313 | } |
… | |
… | |
307 | delete $TEXTURES{$self+0}; |
325 | delete $TEXTURES{$self+0}; |
308 | |
326 | |
309 | $self->unload; |
327 | $self->unload; |
310 | } |
328 | } |
311 | |
329 | |
312 | $CFPlus::OpenGL::INIT_HOOK{"CFPlus::Texture"} = sub { |
330 | $DC::OpenGL::INIT_HOOK{"DC::Texture"} = sub { |
313 | for (values %TEXTURES) { |
331 | for my $tex (values %TEXTURES) { |
314 | next unless delete $_->{was_loaded}; |
332 | if (my $name = $tex->{want_upload}) { |
315 | $_->upload; |
333 | $tex->upload; |
|
|
334 | |
|
|
335 | if ($tex->{loading} && $name > 0) { |
|
|
336 | # if loading is delayed we still have to allocate the texture name |
|
|
337 | glBindTexture GL_TEXTURE_2D, $name; |
|
|
338 | glTexImage2D GL_TEXTURE_2D, 0, GL_ALPHA, 0, 0, 0, GL_ALPHA, GL_UNSIGNED_BYTE; |
|
|
339 | } |
|
|
340 | } |
316 | } |
341 | } |
317 | }; |
342 | }; |
318 | |
343 | |
319 | $CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Texture"} = sub { |
344 | $DC::OpenGL::SHUTDOWN_HOOK{"DC::Texture"} = sub { |
320 | for (values %TEXTURES) { |
345 | for (values %TEXTURES) { |
321 | next unless $_->{name}; |
346 | next unless $_->{name}; |
322 | $_->{was_loaded} = 1; |
347 | $_->{want_upload} = $_->{name}; |
323 | $_->unload; |
348 | $_->unload; |
324 | } |
349 | } |
325 | }; |
350 | }; |
326 | |
351 | |
327 | 1; |
352 | 1; |