ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC.pm (file contents):
Revision 1.69 by root, Thu May 25 23:06:05 2006 UTC vs.
Revision 1.70 by root, Fri May 26 18:28:23 2006 UTC

24use utf8; 24use utf8;
25 25
26use Carp (); 26use Carp ();
27use AnyEvent (); 27use AnyEvent ();
28use BerkeleyDB; 28use BerkeleyDB;
29
30use CFClient::OpenGL;
31
32our %GL_EXT;
33our $GL_VERSION;
34
35our $GL_NPOT;
36our $GL_DEBUG = 1;
37
38sub gl_init {
39 $GL_VERSION = gl_version * 1;
40 %GL_EXT = map +($_ => 1), split /\s+/, gl_extensions;
41
42 $GL_NPOT = $GL_EXT{GL_ARB_texture_non_power_of_two} || $GL_VERSION >= 2;
43 $GL_NPOT = 0 if gl_vendor =~ /ATI Technologies/; # ATI doesn't get it right...
44
45 glDisable GL_COLOR_MATERIAL;
46 glShadeModel GL_FLAT;
47 glDisable GL_DITHER;
48 glDisable GL_DEPTH_TEST;
49 glDepthMask 0;
50 glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST;
51
52 CFClient::Texture::restore_state ();
53}
54
55sub gl_check {
56 return unless $GL_DEBUG;
57
58 if (my $error = glGetError) {
59 my ($format, @args) = @_;
60 Carp::cluck sprintf "opengl error %x while $format", $error, @args;
61 }
62}
63 29
64sub find_rcfile($) { 30sub find_rcfile($) {
65 my $path; 31 my $path;
66 32
67 for (grep !ref, @INC) { 33 for (grep !ref, @INC) {
351 . ($self->{nrof} ? "You have $self->{nrof} of it. " : "Item cannot stack with others of it's kind. ") 317 . ($self->{nrof} ? "You have $self->{nrof} of it. " : "Item cannot stack with others of it's kind. ")
352 . "\n\n$tooltip_std" 318 . "\n\n$tooltip_std"
353 ); 319 );
354} 320}
355 321
356package CFClient::Texture;
357
358use strict;
359
360use Scalar::Util;
361
362use CFClient::OpenGL;
363
364my %TEXTURES;
365
366sub new {
367 my ($class, %data) = @_;
368
369 my $self = bless {
370 internalformat => GL_RGBA,
371 format => GL_RGBA,
372 type => GL_UNSIGNED_BYTE,
373 %data,
374 }, $class;
375
376 Scalar::Util::weaken ($TEXTURES{$self+0} = $self);
377
378 $self->upload;
379
380 $self
381}
382
383sub new_from_image {
384 my ($class, $image, %arg) = @_;
385
386 $class->new (image => $image, %arg)
387}
388
389sub new_from_file {
390 my ($class, $path, %arg) = @_;
391
392 open my $fh, "<:raw", $path
393 or die "$path: $!";
394
395 local $/;
396 $class->new_from_image (<$fh>, %arg)
397}
398
399#sub new_from_surface {
400# my ($class, $surface) = @_;
401#
402# $surface->rgba;
403#
404# $class->new (
405# data => $surface->pixels,
406# w => $surface->width,
407# h => $surface->height,
408# )
409#}
410
411sub new_from_layout {
412 my ($class, $layout, %arg) = @_;
413
414 my ($w, $h, $data, $format, $internalformat) = $layout->render;
415
416 $class->new (
417 w => $w,
418 h => $h,
419 data => $data,
420 format => $format,
421 internalformat => $format,
422 type => GL_UNSIGNED_BYTE,
423 %arg,
424 )
425}
426
427sub new_from_opengl {
428 my ($class, $w, $h, $cb) = @_;
429
430 $class->new (w => $w || 1, h => $h || 1, render_cb => $cb)
431}
432
433sub topot {
434 (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0]
435}
436
437sub upload {
438 my ($self) = @_;
439
440 return unless $GL_VERSION;
441
442 my $data;
443
444 if (exists $self->{data}) {
445 $data = $self->{data};
446
447 } elsif (exists $self->{render_cb}) {
448 glViewport 0, 0, $self->{w}, $self->{h};
449 glMatrixMode GL_PROJECTION;
450 glLoadIdentity;
451 glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000;
452 glMatrixMode GL_MODELVIEW;
453 glLoadIdentity;
454 $self->{render_cb}->($self, $self->{w}, $self->{h});
455
456 } else {
457 ($self->{w}, $self->{h}, $data, $self->{internalformat}, $self->{format}, $self->{type})
458 = CFClient::load_image_inline $self->{image};
459 }
460
461 my ($tw, $th) = @$self{qw(w h)};
462
463 unless ($tw > 0 && $th > 0) {
464 $tw = $th = 1;
465 $data = "\x00" x 64;
466 }
467
468 $self->{minified} = [CFClient::average $tw, $th, $data]
469 if $self->{minify};
470
471 unless ($GL_NPOT) {
472 # TODO: does not work for zero-sized textures
473 $tw = topot $tw;
474 $th = topot $th;
475
476 if (($tw != $self->{w} || $th != $self->{h}) && defined $data) {
477 my $bpp = (length $data) / ($self->{w} * $self->{h});
478 $data = pack "(a" . ($tw * $bpp) . ")*",
479 unpack "(a" . ($self->{w} * $bpp) . ")*", $data;
480 $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h});
481 }
482 }
483
484 $self->{s} = $self->{w} / $tw;
485 $self->{t} = $self->{h} / $th;
486
487 $self->{name} ||= glGenTexture;
488
489 glBindTexture GL_TEXTURE_2D, $self->{name};
490
491 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
492 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
493
494 if ($::FAST) {
495 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST;
496 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST;
497 } elsif ($self->{mipmap} && $GL_VERSION >= 1.4) {
498 # alternatively check for 0x8191
499 glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1;
500 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
501 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR;
502 } else {
503 glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, $self->{mipmap};
504 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
505 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;
506 }
507
508 glGetError;
509
510 if (defined $data) {
511 glTexImage2D GL_TEXTURE_2D, 0,
512 $self->{internalformat},
513 $tw, $th,
514 0,
515 $self->{format},
516 $self->{type},
517 $data;
518 CFClient::gl_check "uploading texture %dx%d if=%x f=%x t=%x",
519 $tw, $th, $self->{internalformat}, $self->{format}, $self->{type};
520 } else {
521 glCopyTexImage2D GL_TEXTURE_2D, 0,
522 $self->{internalformat},
523 0, 0,
524 $tw, $th,
525 0;
526 CFClient::gl_check "copying to texture %dx%d if=%x",
527 $tw, $th, $self->{internalformat};
528 }
529
530 glBindTexture GL_TEXTURE_2D, 0; # just to be on the safe side
531}
532
533sub DESTROY {
534 my ($self) = @_;
535
536 delete $TEXTURES{$self+0};
537
538 glDeleteTexture delete $self->{name}
539 if $self->{name};
540}
541
542sub restore_state {
543 $_->upload
544 for values %TEXTURES;
545}
546
5471; 3221;
548 323
549=back 324=back
550 325
551=head1 AUTHOR 326=head1 AUTHOR

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines