--- deliantra/Deliantra-Client/DC/Texture.pm 2007/07/17 16:02:14 1.15 +++ deliantra/Deliantra-Client/DC/Texture.pm 2007/07/19 22:38:53 1.20 @@ -94,54 +94,92 @@ return unless $GL_VERSION; - my ($tw, $th, $data); + # $tw,$th texture + # $rw,$rh rendered/used size + # $dw,$dh $data + + my ($data, $dw, $dh); if (exists $self->{data}) { $data = $self->{data}; - ($tw, $th) = @$self{qw(w h)}; + ($dw, $dh) = @$self{qw(w h)}; } elsif (exists $self->{render_cb}) { - ($tw, $th) = @$self{qw(w h)}; + ($dw, $dh) = @$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)}; + ($dw, $dh) = @$self{qw(w h)}; } + my ($tw, $th) = ($dw, $dh); + defined $data or $self->{render_cb} or die; # some sanity check - $self->{minified} ||= [CFPlus::average $tw, $th, $data] + $self->{minified} ||= [CFPlus::average $dw, $dh, $data] if $self->{minify}; - pad2pot $data, $tw, $th unless $GL_NPOT; - $tw = min $MAX_W, $tw; - $th = min $MAX_H, $th; + # against rather broken cards we enforce a maximum texture size + $tw = min $MAX_W, minpot $tw; + $th = min $MAX_H, minpot $th; + + # if only pot-textures are allowed, pot'ify tw/th + unless ($GL_NPOT && 0) {#d# + $tw = minpot $tw; + $th = minpot $th; + } - # now further decrease texture size + # now further decrease texture size until the + # card does accept it 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}; + # decide the amount of space used in the texture + my ($rw, $rh); + my $render; + + if ($self->{render_cb}) { + # use only part of the texture + #$rw >>= 1 while $rw > $tw; + #$rh >>= 1 while $rh > $th; + $rw = min $dw, $tw; + $rh = min $dh, $th; + ++$render; + } else { + if ($self->{wrap} || $tw < $dw || $th < $dh) { + # scale to the full texture size + ($rw, $rh) = ($tw, $th); + ++$render; + } else { + # pad + pad $data, $dw, $dh, $tw, $th; + ($rw, $rh) = ($dw, $dh); + ($dw, $dh) = ($tw, $th); + } + } if ($render) { glViewport 0, 0, $tw, $th; glMatrixMode GL_PROJECTION; glLoadIdentity; - glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000; + glOrtho 0, $tw, 0, $th, -10000, 10000; glMatrixMode GL_MODELVIEW; glLoadIdentity; if ($self->{render_cb}) { - $self->{render_cb}->($self, $self->{w}, $self->{h}); + glScale $rw / $dw, $rh / $dh; + $self->{render_cb}->($self, $rw, $rh); } else { - glPixelZoom $tw / $self->{w}, $th / $self->{h}; - glDrawPixels $self->{w}, $self->{h}, + glClearColor 0, 0, 0, 0; + glClear GL_COLOR_BUFFER_BIT; + glPixelZoom $tw / $dw, $th / $dh; + glDrawPixels $dw, $dh, $self->{format}, $self->{type}, $data; @@ -185,7 +223,7 @@ } else { glTexImage2D GL_TEXTURE_2D, 0, $self->{internalformat}, - $tw, $th, + $dw, $dh, 0, $self->{format}, $self->{type}, @@ -194,8 +232,8 @@ $tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; } - $self->{s} = min 1, $self->{w} / $tw; - $self->{t} = min 1, $self->{h} / $th; + $self->{s} = $rw / $tw; + $self->{t} = $rh / $th; } sub shutdown {