--- deliantra/Deliantra-Client/DC/Texture.pm 2006/12/09 21:26:46 1.13 +++ deliantra/Deliantra-Client/DC/Texture.pm 2007/07/17 16:02:14 1.15 @@ -16,9 +16,11 @@ use strict; +use List::Util qw(max min); use CFPlus::OpenGL; my %TEXTURES; +my ($MAX_W, $MAX_H) = (4096, 4096); # maximum texture size attempted by this module sub new { my ($class, %data) = @_; @@ -92,40 +94,62 @@ return unless $GL_VERSION; - my $data; + my ($tw, $th, $data); if (exists $self->{data}) { $data = $self->{data}; + ($tw, $th) = @$self{qw(w h)}; } elsif (exists $self->{render_cb}) { - glViewport 0, 0, $self->{w}, $self->{h}; - glMatrixMode GL_PROJECTION; - glLoadIdentity; - glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000; - glMatrixMode GL_MODELVIEW; - glLoadIdentity; - $self->{render_cb}->($self, $self->{w}, $self->{h}); + ($tw, $th) = @$self{qw(w h)}; } else { ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type}) = CFPlus::load_image_inline $self->{image}; $self->{internalformat} ||= $internalformat; + ($tw, $th) = @$self{qw(w h)}; } - my ($tw, $th) = @$self{qw(w h)}; + defined $data or $self->{render_cb} or die; # some sanity check $self->{minified} ||= [CFPlus::average $tw, $th, $data] if $self->{minify}; pad2pot $data, $tw, $th unless $GL_NPOT; + $tw = min $MAX_W, $tw; + $th = min $MAX_H, $th; + + # now further decrease texture size + while (!texture_valid_2d $self->{internalformat}, $tw, $th, $self->{format}, $self->{type}) { + # quarter the texture size + $tw >>= 1; + $th >>= 1; + } + + my $render = $self->{render_cb} || $tw < $self->{w} || $th < $self->{h}; - $self->{s} = $self->{w} / $tw; - $self->{t} = $self->{h} / $th; + if ($render) { + glViewport 0, 0, $tw, $th; + glMatrixMode GL_PROJECTION; + glLoadIdentity; + glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000; + glMatrixMode GL_MODELVIEW; + glLoadIdentity; - $self->{name} ||= glGenTexture; + if ($self->{render_cb}) { + $self->{render_cb}->($self, $self->{w}, $self->{h}); + } else { + glPixelZoom $tw / $self->{w}, $th / $self->{h}; + glDrawPixels $self->{w}, $self->{h}, + $self->{format}, + $self->{type}, + $data; + glPixelZoom 1, 1; + } + } - glBindTexture GL_TEXTURE_2D, $self->{name}; + glBindTexture GL_TEXTURE_2D, $self->{name} ||= glGenTexture; if ($self->{wrap}) { glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; @@ -150,7 +174,15 @@ glGetError; - if (defined $data) { + if ($render) { + glCopyTexImage2D GL_TEXTURE_2D, 0, + $self->{internalformat}, + 0, 0, + $tw, $th, + 0; + gl_check "copying to texture %dx%d if=%x", + $tw, $th, $self->{internalformat}; + } else { glTexImage2D GL_TEXTURE_2D, 0, $self->{internalformat}, $tw, $th, @@ -160,16 +192,10 @@ $data; gl_check "uploading texture %dx%d if=%x f=%x t=%x", $tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; - } else { - exists $self->{render_cb} or die; - glCopyTexImage2D GL_TEXTURE_2D, 0, - $self->{internalformat}, - 0, 0, - $tw, $th, - 0; - gl_check "copying to texture %dx%d if=%x", - $tw, $th, $self->{internalformat}; } + + $self->{s} = min 1, $self->{w} / $tw; + $self->{t} = min 1, $self->{h} / $th; } sub shutdown {