1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | CFClient::Texture - tetxure class for CFClient |
3 | CFPlus::Texture - tetxure class for CFPlus |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use CFClient::Texture; |
7 | use CFPlus::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 CFClient::Texture; |
15 | package CFPlus::Texture; |
16 | |
16 | |
17 | use strict; |
17 | use strict; |
18 | |
18 | |
19 | use Scalar::Util; |
19 | use List::Util qw(max min); |
20 | |
|
|
21 | use CFClient::OpenGL; |
20 | use CFPlus::OpenGL; |
22 | |
21 | |
23 | my %TEXTURES; |
22 | my %TEXTURES; |
|
|
23 | my ($MAX_W, $MAX_H) = (4096, 4096); # maximum texture size attempted by this module |
24 | |
24 | |
25 | sub new { |
25 | sub new { |
26 | my ($class, %data) = @_; |
26 | my ($class, %data) = @_; |
27 | |
27 | |
28 | my $self = bless { |
28 | my $self = bless { |
… | |
… | |
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 | Scalar::Util::weaken ($TEXTURES{$self+0} = $self); |
35 | CFPlus::weaken ($TEXTURES{$self+0} = $self); |
36 | |
36 | |
37 | $self->upload; |
37 | $self->upload |
|
|
38 | unless delete $self->{delay}; |
38 | |
39 | |
39 | $self |
40 | $self |
40 | } |
41 | } |
41 | |
42 | |
42 | sub new_from_image { |
43 | sub new_from_image { |
43 | my ($class, $image, %arg) = @_; |
44 | my ($class, $image, %arg) = @_; |
44 | |
45 | |
|
|
46 | Carp::confess "tried to create texture from undefined image" |
|
|
47 | unless defined $image; |
|
|
48 | |
45 | $class->new (image => $image, %arg) |
49 | $class->new (image => $image, internalformat => undef, %arg) |
46 | } |
50 | } |
|
|
51 | |
47 | |
52 | |
48 | sub new_from_file { |
53 | sub new_from_file { |
49 | my ($class, $path, %arg) = @_; |
54 | my ($class, $path, %arg) = @_; |
50 | |
55 | |
51 | open my $fh, "<:raw", $path |
56 | open my $fh, "<:raw", $path |
… | |
… | |
65 | # w => $surface->width, |
70 | # w => $surface->width, |
66 | # h => $surface->height, |
71 | # h => $surface->height, |
67 | # ) |
72 | # ) |
68 | #} |
73 | #} |
69 | |
74 | |
70 | sub new_from_layout { |
75 | #sub new_from_layout { |
71 | my ($class, $layout, %arg) = @_; |
76 | # my ($class, $layout, %arg) = @_; |
72 | |
77 | # |
73 | my ($w, $h, $data, $format, $internalformat) = $layout->render; |
78 | # my ($w, $h, $data, $format, $internalformat) = $layout->render; |
74 | |
79 | # |
75 | $class->new ( |
80 | # $class->new ( |
76 | w => $w, |
81 | # w => $w, |
77 | h => $h, |
82 | # h => $h, |
78 | data => $data, |
83 | # data => $data, |
79 | format => $format, |
84 | # format => $format, |
80 | internalformat => $format, |
85 | # internalformat => $format, |
81 | type => GL_UNSIGNED_BYTE, |
86 | # type => GL_UNSIGNED_BYTE, |
82 | %arg, |
87 | # %arg, |
83 | ) |
88 | # ) |
84 | } |
89 | #} |
85 | |
90 | |
86 | sub new_from_opengl { |
91 | sub new_from_opengl { |
87 | my ($class, $w, $h, $cb) = @_; |
92 | my ($class, $w, $h, $cb) = @_; |
88 | |
93 | |
89 | $class->new (w => $w || 1, h => $h || 1, render_cb => $cb) |
94 | $class->new (w => $w || 1, h => $h || 1, render_cb => $cb, nearest => 1) |
|
|
95 | } |
|
|
96 | |
|
|
97 | sub loading_done($$) { |
|
|
98 | my ($self, $data) = @_; |
|
|
99 | |
|
|
100 | delete $self->{loading}; |
|
|
101 | ++$self->{delete_image}; |
|
|
102 | $self->{image} = $data; |
|
|
103 | $self->upload; |
90 | } |
104 | } |
91 | |
105 | |
92 | sub upload { |
106 | sub upload { |
|
|
107 | my ($self, $cb) = @_; |
|
|
108 | |
|
|
109 | push @{ $self->{upload_done} }, $cb |
|
|
110 | if $cb; |
|
|
111 | |
|
|
112 | return if $self->{loading}; |
|
|
113 | |
|
|
114 | unless ($GL_VERSION) { |
|
|
115 | $self->{was_loaded} = 1; |
|
|
116 | return; |
|
|
117 | } |
|
|
118 | |
|
|
119 | unless ($self->{name}) { |
|
|
120 | # $tw,$th texture |
|
|
121 | # $rw,$rh rendered/used size |
|
|
122 | # $dw,$dh $data |
|
|
123 | |
|
|
124 | my ($data, $dw, $dh); |
|
|
125 | |
|
|
126 | if (exists $self->{data}) { |
|
|
127 | $data = $self->{data}; |
|
|
128 | ($dw, $dh) = @$self{qw(w h)}; |
|
|
129 | |
|
|
130 | } elsif (exists $self->{render_cb}) { |
|
|
131 | ($dw, $dh) = @$self{qw(w h)}; |
|
|
132 | |
|
|
133 | } elsif (exists $self->{image}) { |
|
|
134 | ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type}) |
|
|
135 | = CFPlus::load_image_inline $self->{image}; |
|
|
136 | |
|
|
137 | $self->{internalformat} ||= $internalformat; |
|
|
138 | ($dw, $dh) = @$self{qw(w h)}; |
|
|
139 | |
|
|
140 | delete $self->{image} if delete $self->{delete_image}; |
|
|
141 | |
|
|
142 | } elsif (exists $self->{tile}) { |
|
|
143 | ++$self->{loading}; |
|
|
144 | return CFPlus::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]); |
|
|
157 | }; |
|
|
158 | |
|
|
159 | } else { |
|
|
160 | Carp::confess "tried to create texture that is not data, render or image"; |
|
|
161 | } |
|
|
162 | |
|
|
163 | my ($tw, $th) = ($dw, $dh); |
|
|
164 | |
|
|
165 | defined $data or $self->{render_cb} or die; # some sanity check |
|
|
166 | |
|
|
167 | $self->{minified} ||= [CFPlus::average $dw, $dh, $data] |
|
|
168 | if $self->{minify}; |
|
|
169 | |
|
|
170 | # against rather broken cards we enforce a maximum texture size |
|
|
171 | $tw = min $MAX_W, minpot $tw; |
|
|
172 | $th = min $MAX_H, minpot $th; |
|
|
173 | |
|
|
174 | # if only pot-textures are allowed, pot'ify tw/th |
|
|
175 | unless ($GL_NPOT && 0) {#d# |
|
|
176 | $tw = minpot $tw; |
|
|
177 | $th = minpot $th; |
|
|
178 | } |
|
|
179 | |
|
|
180 | # now further decrease texture size until the |
|
|
181 | # card does accept it |
|
|
182 | while (!texture_valid_2d $self->{internalformat}, $tw, $th, $self->{format}, $self->{type}) { |
|
|
183 | # quarter the texture size |
|
|
184 | $tw >>= 1; |
|
|
185 | $th >>= 1; |
|
|
186 | } |
|
|
187 | |
|
|
188 | # decide the amount of space used in the texture |
|
|
189 | my ($rw, $rh); |
|
|
190 | my ($ox, $oy); # area shift to lessen effetc of buggy opengl implementations (nvida, ati) |
|
|
191 | my $render; |
|
|
192 | |
|
|
193 | if ($self->{render_cb}) { |
|
|
194 | # use only part of the texture |
|
|
195 | #$rw >>= 1 while $rw > $tw; |
|
|
196 | #$rh >>= 1 while $rh > $th; |
|
|
197 | $rw = min $dw, $tw; |
|
|
198 | $rh = min $dh, $th; |
|
|
199 | ++$render; |
|
|
200 | } else { |
|
|
201 | if ($self->{wrap} || $tw < $dw || $th < $dh) { |
|
|
202 | # scale to the full texture size |
|
|
203 | ($rw, $rh) = ($tw, $th); |
|
|
204 | ++$render; |
|
|
205 | } else { |
|
|
206 | # pad |
|
|
207 | pad $data, $dw, $dh, $tw, $th; |
|
|
208 | ($rw, $rh) = ($dw, $dh); |
|
|
209 | ($dw, $dh) = ($tw, $th); |
|
|
210 | } |
|
|
211 | } |
|
|
212 | |
|
|
213 | if ($render) { |
|
|
214 | $ox = int .5 * ($::WIDTH - $rw); |
|
|
215 | $oy = int .5 * ($::HEIGHT - $rh); |
|
|
216 | |
|
|
217 | glViewport $ox, $oy, $tw, $th; |
|
|
218 | #glScissor 0, 0, $tw, $th; |
|
|
219 | #glEnable GL_SCISSOR_TEST; |
|
|
220 | glMatrixMode GL_PROJECTION; |
|
|
221 | glLoadIdentity; |
|
|
222 | glOrtho 0, $tw, 0, $th, -10000, 10000; |
|
|
223 | glMatrixMode GL_MODELVIEW; |
|
|
224 | glLoadIdentity; |
|
|
225 | |
|
|
226 | if ($self->{render_cb}) { |
|
|
227 | glScale $rw / $dw, $rh / $dh; |
|
|
228 | $self->{render_cb}->($self, $rw, $rh); |
|
|
229 | } else { |
|
|
230 | glClearColor 0, 0, 0, 0; |
|
|
231 | glClear GL_COLOR_BUFFER_BIT; |
|
|
232 | glPixelZoom $tw / $dw, $th / $dh; |
|
|
233 | glRasterPos 0, 0; |
|
|
234 | glDrawPixels $dw, $dh, |
|
|
235 | $self->{format}, |
|
|
236 | $self->{type}, |
|
|
237 | $data; |
|
|
238 | glPixelZoom 1, 1; |
|
|
239 | } |
|
|
240 | } |
|
|
241 | |
|
|
242 | glBindTexture GL_TEXTURE_2D, $self->{name} ||= glGenTexture; |
|
|
243 | |
|
|
244 | if ($self->{wrap}) { |
|
|
245 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
|
|
246 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
|
|
247 | } else { |
|
|
248 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, $GL_VERSION >= 1.2 ? GL_CLAMP_TO_EDGE : GL_CLAMP; |
|
|
249 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, $GL_VERSION >= 1.2 ? GL_CLAMP_TO_EDGE : GL_CLAMP; |
|
|
250 | } |
|
|
251 | |
|
|
252 | if ($::FAST || $self->{nearest}) { |
|
|
253 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; |
|
|
254 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST; |
|
|
255 | } elsif ($self->{mipmap} && $GL_VERSION >= 1.4) { |
|
|
256 | glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1; |
|
|
257 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
|
|
258 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR; |
|
|
259 | } else { |
|
|
260 | glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, $self->{mipmap}; |
|
|
261 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
|
|
262 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR; |
|
|
263 | } |
|
|
264 | |
|
|
265 | glGetError; |
|
|
266 | |
|
|
267 | if ($render) { |
|
|
268 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
|
|
269 | $self->{internalformat}, |
|
|
270 | $ox, $oy, |
|
|
271 | $tw, $th, |
|
|
272 | 0; |
|
|
273 | gl_check "copying to texture %dx%d if=%x", |
|
|
274 | $tw, $th, $self->{internalformat}; |
|
|
275 | |
|
|
276 | #glDisable GL_SCISSOR_TEST; |
|
|
277 | } else { |
|
|
278 | my $if = $self->{internalformat}; |
|
|
279 | |
|
|
280 | if ($GL_COMPRESS && $::CFG->{texture_compression}) { |
|
|
281 | if ($if == GL_RGB) { |
|
|
282 | $if = GL_COMPRESSED_RGB_ARB; |
|
|
283 | } elsif ($if == GL_RGBA) { |
|
|
284 | $if = GL_COMPRESSED_RGBA_ARB; |
|
|
285 | } |
|
|
286 | } |
|
|
287 | |
|
|
288 | glTexImage2D GL_TEXTURE_2D, 0, |
|
|
289 | $if, |
|
|
290 | $dw, $dh, |
|
|
291 | 0, |
|
|
292 | $self->{format}, |
|
|
293 | $self->{type}, |
|
|
294 | $data; |
|
|
295 | gl_check "uploading texture %dx%d if=%x f=%x t=%x", |
|
|
296 | $tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; |
|
|
297 | } |
|
|
298 | |
|
|
299 | $self->{s} = $rw / $tw; |
|
|
300 | $self->{t} = $rh / $th; |
|
|
301 | } |
|
|
302 | |
|
|
303 | $_->($self) |
|
|
304 | for @{ (delete $self->{upload_done}) || [] }; |
|
|
305 | } |
|
|
306 | |
|
|
307 | sub unload { |
93 | my ($self) = @_; |
308 | my ($self) = @_; |
94 | |
309 | |
95 | return unless $GL_VERSION; |
310 | glDeleteTexture delete $self->{name} |
96 | |
|
|
97 | my $data; |
|
|
98 | |
|
|
99 | if (exists $self->{data}) { |
|
|
100 | $data = $self->{data}; |
|
|
101 | |
|
|
102 | } elsif (exists $self->{render_cb}) { |
|
|
103 | glViewport 0, 0, $self->{w}, $self->{h}; |
|
|
104 | glMatrixMode GL_PROJECTION; |
|
|
105 | glLoadIdentity; |
|
|
106 | glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000; |
|
|
107 | glMatrixMode GL_MODELVIEW; |
|
|
108 | glLoadIdentity; |
|
|
109 | $self->{render_cb}->($self, $self->{w}, $self->{h}); |
|
|
110 | |
|
|
111 | } else { |
|
|
112 | ($self->{w}, $self->{h}, $data, $self->{internalformat}, $self->{format}, $self->{type}) |
|
|
113 | = CFClient::load_image_inline $self->{image}; |
|
|
114 | } |
|
|
115 | |
|
|
116 | my ($tw, $th) = @$self{qw(w h)}; |
|
|
117 | |
|
|
118 | $self->{minified} ||= [CFClient::average $tw, $th, $data] |
|
|
119 | if $self->{minify}; |
311 | if $self->{name}; |
120 | |
312 | } |
121 | pad2pot $data, $tw, $th unless $GL_NPOT; |
|
|
122 | |
|
|
123 | $self->{s} = $self->{w} / $tw; |
|
|
124 | $self->{t} = $self->{h} / $th; |
|
|
125 | |
|
|
126 | $self->{name} ||= glGenTexture; |
|
|
127 | |
|
|
128 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
|
|
129 | |
|
|
130 | if ($self->{wrap}) { |
|
|
131 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
|
|
132 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
|
|
133 | } else { |
|
|
134 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, $GL_VERSION >= 1.2 ? GL_CLAMP_TO_EDGE : GL_CLAMP; |
|
|
135 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, $GL_VERSION >= 1.2 ? GL_CLAMP_TO_EDGE : GL_CLAMP; |
|
|
136 | } |
|
|
137 | |
|
|
138 | if ($::FAST) { |
|
|
139 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; |
|
|
140 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST; |
|
|
141 | } elsif ($self->{mipmap} && $GL_VERSION >= 1.4) { |
|
|
142 | glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1; |
|
|
143 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
|
|
144 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR; |
|
|
145 | } else { |
|
|
146 | glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, $self->{mipmap}; |
|
|
147 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
|
|
148 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR; |
|
|
149 | } |
|
|
150 | |
|
|
151 | glGetError; |
|
|
152 | |
|
|
153 | if (defined $data) { |
|
|
154 | glTexImage2D GL_TEXTURE_2D, 0, |
|
|
155 | $self->{internalformat}, |
|
|
156 | $tw, $th, |
|
|
157 | 0, |
|
|
158 | $self->{format}, |
|
|
159 | $self->{type}, |
|
|
160 | $data; |
|
|
161 | gl_check "uploading texture %dx%d if=%x f=%x t=%x", |
|
|
162 | $tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; |
|
|
163 | } else { |
|
|
164 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
|
|
165 | $self->{internalformat}, |
|
|
166 | 0, 0, |
|
|
167 | $tw, $th, |
|
|
168 | 0; |
|
|
169 | gl_check "copying to texture %dx%d if=%x", |
|
|
170 | $tw, $th, $self->{internalformat}; |
|
|
171 | } |
|
|
172 | } |
|
|
173 | |
313 | |
174 | sub DESTROY { |
314 | sub DESTROY { |
175 | my ($self) = @_; |
315 | my ($self) = @_; |
176 | |
316 | |
177 | delete $TEXTURES{$self+0}; |
317 | delete $TEXTURES{$self+0}; |
178 | |
318 | |
179 | glDeleteTexture delete $self->{name} |
319 | $self->unload; |
180 | if $self->{name}; |
|
|
181 | } |
320 | } |
182 | |
321 | |
183 | $CFClient::OpenGL::INIT_HOOK{"CFClient::Texture"} = sub { |
322 | $CFPlus::OpenGL::INIT_HOOK{"CFPlus::Texture"} = sub { |
|
|
323 | for (values %TEXTURES) { |
|
|
324 | next unless delete $_->{was_loaded}; |
184 | $_->upload |
325 | $_->upload; |
|
|
326 | } |
|
|
327 | }; |
|
|
328 | |
|
|
329 | $CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Texture"} = sub { |
185 | for values %TEXTURES; |
330 | for (values %TEXTURES) { |
|
|
331 | next unless $_->{name}; |
|
|
332 | $_->{was_loaded} = 1; |
|
|
333 | $_->unload; |
|
|
334 | } |
186 | }; |
335 | }; |
187 | |
336 | |
188 | 1; |
337 | 1; |
189 | |
338 | |
190 | =back |
339 | =back |