… | |
… | |
19 | |
19 | |
20 | use XSLoader; |
20 | use XSLoader; |
21 | XSLoader::load "CFClient", $VERSION; |
21 | XSLoader::load "CFClient", $VERSION; |
22 | } |
22 | } |
23 | |
23 | |
|
|
24 | use Carp (); |
24 | use AnyEvent; |
25 | use AnyEvent; |
25 | use BerkeleyDB; |
26 | use BerkeleyDB; |
26 | use CFClient::OpenGL; |
27 | use CFClient::OpenGL; |
27 | |
28 | |
28 | our %GL_EXT; |
29 | our %GL_EXT; |
… | |
… | |
48 | } |
49 | } |
49 | |
50 | |
50 | sub find_rcfile($) { |
51 | sub find_rcfile($) { |
51 | my $path; |
52 | my $path; |
52 | |
53 | |
53 | for (@INC) { |
54 | for (grep !ref, @INC) { |
54 | $path = "$_/CFClient/resources/$_[0]"; |
55 | $path = "$_/CFClient/resources/$_[0]"; |
55 | return $path if -r $path; |
56 | return $path if -r $path; |
56 | } |
57 | } |
57 | |
58 | |
58 | die "FATAL: can't find required file $_[0]\n"; |
59 | die "FATAL: can't find required file $_[0]\n"; |
… | |
… | |
199 | |
200 | |
200 | $class->new ( |
201 | $class->new ( |
201 | w => $w, |
202 | w => $w, |
202 | h => $h, |
203 | h => $h, |
203 | data => $data, |
204 | data => $data, |
204 | internalformat => GL_ALPHA4, |
|
|
205 | format => GL_ALPHA, |
205 | format => GL_ALPHA, |
|
|
206 | internalformat => GL_ALPHA, |
206 | type => GL_UNSIGNED_BYTE, |
207 | type => GL_UNSIGNED_BYTE, |
207 | %arg, |
208 | %arg, |
208 | ) |
209 | ) |
209 | } |
210 | } |
210 | |
211 | |
… | |
… | |
242 | = CFClient::load_image_inline $self->{image}; |
243 | = CFClient::load_image_inline $self->{image}; |
243 | } |
244 | } |
244 | |
245 | |
245 | my ($tw, $th) = @$self{qw(w h)}; |
246 | my ($tw, $th) = @$self{qw(w h)}; |
246 | |
247 | |
247 | unless ($tw && $th) { |
248 | unless ($tw > 0 && $th > 0) { |
248 | $tw = $th = 1; |
249 | $tw = $th = 1; |
249 | $data = "\x00" x 64; |
250 | $data = "\x00" x 64; |
250 | } |
251 | } |
251 | |
252 | |
252 | $self->{minified} = [CFClient::average $tw, $th, $data] |
253 | $self->{minified} = [CFClient::average $tw, $th, $data] |
… | |
… | |
255 | unless ($GL_NPOT) { |
256 | unless ($GL_NPOT) { |
256 | # TODO: does not work for zero-sized textures |
257 | # TODO: does not work for zero-sized textures |
257 | $tw = topot $tw; |
258 | $tw = topot $tw; |
258 | $th = topot $th; |
259 | $th = topot $th; |
259 | |
260 | |
260 | if ($tw != $self->{w} || $th != $self->{h} && defined $data) { |
261 | if (($tw != $self->{w} || $th != $self->{h}) && defined $data) { |
261 | my $bpp = (length $data) / ($self->{w} * $self->{h}); |
262 | my $bpp = (length $data) / ($self->{w} * $self->{h}); |
262 | $data = pack "(a" . ($tw * $bpp) . ")*", |
263 | $data = pack "(a" . ($tw * $bpp) . ")*", |
263 | unpack "(a" . ($self->{w} * $bpp) . ")*", $data; |
264 | unpack "(a" . ($self->{w} * $bpp) . ")*", $data; |
264 | $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h}); |
265 | $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h}); |
265 | } |
266 | } |
266 | } |
267 | } |
267 | |
268 | |
268 | $self->{s} = $self->{w} / $tw; |
269 | $self->{s} = $self->{w} / $tw; |
269 | $self->{t} = $self->{h} / $th; |
270 | $self->{t} = $self->{h} / $th; |
270 | |
271 | |
271 | glGetError; |
|
|
272 | |
|
|
273 | $self->{name} ||= glGenTexture; |
272 | $self->{name} ||= glGenTexture; |
274 | |
273 | |
275 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
274 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
276 | |
275 | |
277 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
276 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
278 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
277 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
279 | |
278 | |
280 | if ($::FAST) { |
279 | if ($::FAST) { |
281 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; |
280 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; |
282 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST; |
281 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST; |
|
|
282 | } elsif ($self->{mipmap} && $GL_VERSION >= 1.4) { |
|
|
283 | # alternatively check for 0x8191 |
|
|
284 | glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1; |
|
|
285 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
|
|
286 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR; |
283 | } else { |
287 | } else { |
284 | glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, $self->{mipmap}; |
288 | glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, $self->{mipmap}; |
285 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
289 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
286 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, $self->{mipmap} ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR; |
290 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR; |
287 | } |
291 | } |
288 | |
292 | |
|
|
293 | glGetError; |
|
|
294 | |
289 | if (defined $data) { |
295 | if (defined $data) { |
290 | glTexImage2D GL_TEXTURE_2D, 0, |
296 | glTexImage2D GL_TEXTURE_2D, 0, |
291 | $self->{internalformat}, |
297 | $self->{internalformat}, |
292 | $tw, $th, # need to pad texture first |
298 | $tw, $th, # need to pad texture first |
293 | 0, |
299 | 0, |
294 | $self->{format}, |
300 | $self->{format}, |
295 | $self->{type}, |
301 | $self->{type}, |
296 | $data; |
302 | $data; |
297 | if (my $error = glGetError) { |
303 | if (my $error = glGetError) { |
298 | warn sprintf "texture upload error: %x %dx%d i=%x f=%x t=%x\n", |
304 | Carp::cluck sprintf "texture upload error: %x %dx%d i=%x f=%x t=%x", |
299 | $error, $tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; |
305 | $error, $tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; |
300 | } |
306 | } |
301 | } else { |
307 | } else { |
302 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
308 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
303 | $self->{internalformat}, |
309 | $self->{internalformat}, |
304 | 0, 0, |
310 | 0, 0, |
305 | $tw, $th, |
311 | $tw, $th, |
306 | 0; |
312 | 0; |
307 | glGetError and die "glCopyTexImage2D $tw,$th"; |
313 | if (my $error = glGetError) { |
|
|
314 | Carp::cluck sprintf "texture upload error: %x %dx%d i=%x", |
|
|
315 | $error, $tw, $th, $self->{internalformat}; |
|
|
316 | } |
308 | } |
317 | } |
309 | } |
318 | } |
310 | |
319 | |
311 | sub DESTROY { |
320 | sub DESTROY { |
312 | my ($self) = @_; |
321 | my ($self) = @_; |