--- deliantra/Deliantra-Client/DC.pm 2006/04/10 11:55:16 1.18 +++ deliantra/Deliantra-Client/DC.pm 2006/04/10 19:34:03 1.19 @@ -21,6 +21,22 @@ XSLoader::load "Crossfire::Client", $VERSION; } +our %GL_EXT; +our $GL_VERSION; + +our $GL_NPOT; + +sub gl_init { + $GL_VERSION = gl_version * 1; + %GL_EXT = map +($_ => 1), split /\s+/, gl_extensions; + + $GL_NPOT = $GL_EXT{GL_ARB_texture_non_power_of_two} || $GL_VERSION >= 2; + + Crossfire::Client::Texture::restore_state (); + +# $GL_NPOT=0;#d# +} + sub find_rcfile($) { my $path; @@ -137,6 +153,10 @@ $class->new (width => $w, height => $h, rendercb => $cb) } +sub topot { + (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0] +} + sub upload { my ($self) = @_; @@ -171,7 +191,22 @@ $data = $pb->get_pixels; } - ($self->{name}) = @{glGenTextures 1}; + my ($tw, $th) = @$self{qw(width height)}; + + unless ($NPOT) { + $tw = topot $tw; + $th = topot $th; + + if (defined $data) { + $data = pack "(a$tw)*", (unpack "(a$self->{width})*", $data); + $data .= ("\x00" x $tw) x ($th - $self->{height}); + } + } + + $self->{u} = $self->{width} / $tw; + $self->{v} = $self->{height} / $th; + + $self->{name} ||= (glGenTextures 1)->[0]; glBindTexture GL_TEXTURE_2D, $self->{name}; @@ -184,7 +219,7 @@ if (defined $data) { glTexImage2D GL_TEXTURE_2D, 0, $self->{internalformat}, - $self->{width}, $self->{height}, + $tw, $th, # need to pad texture first 0, $self->{format}, GL_UNSIGNED_BYTE, @@ -194,7 +229,7 @@ glCopyTexImage2D GL_TEXTURE_2D, 0, $self->{internalformat}, 0, 0, - $self->{width}, $self->{height}, + $tw, $th, 0; glPopmatrix; } @@ -208,7 +243,7 @@ glDeleteTextures delete $self->{name}; } -push @::GL_INIT, sub { +sub restore_state{ $_->upload for grep $_, @textures; };