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

Comparing deliantra/Deliantra-Client/DC/Texture.pm (file contents):
Revision 1.15 by root, Tue Jul 17 16:02:14 2007 UTC vs.
Revision 1.21 by root, Thu Jul 19 23:29:17 2007 UTC

92sub upload { 92sub upload {
93 my ($self) = @_; 93 my ($self) = @_;
94 94
95 return unless $GL_VERSION; 95 return unless $GL_VERSION;
96 96
97 my ($tw, $th, $data); 97 # $tw,$th texture
98 # $rw,$rh rendered/used size
99 # $dw,$dh $data
100
101 my ($data, $dw, $dh);
98 102
99 if (exists $self->{data}) { 103 if (exists $self->{data}) {
100 $data = $self->{data}; 104 $data = $self->{data};
101 ($tw, $th) = @$self{qw(w h)}; 105 ($dw, $dh) = @$self{qw(w h)};
102 106
103 } elsif (exists $self->{render_cb}) { 107 } elsif (exists $self->{render_cb}) {
104 ($tw, $th) = @$self{qw(w h)}; 108 ($dw, $dh) = @$self{qw(w h)};
105 109
106 } else { 110 } else {
107 ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type}) 111 ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type})
108 = CFPlus::load_image_inline $self->{image}; 112 = CFPlus::load_image_inline $self->{image};
109 113
110 $self->{internalformat} ||= $internalformat; 114 $self->{internalformat} ||= $internalformat;
111 ($tw, $th) = @$self{qw(w h)}; 115 ($dw, $dh) = @$self{qw(w h)};
112 } 116 }
117
118 my ($tw, $th) = ($dw, $dh);
113 119
114 defined $data or $self->{render_cb} or die; # some sanity check 120 defined $data or $self->{render_cb} or die; # some sanity check
115 121
116 $self->{minified} ||= [CFPlus::average $tw, $th, $data] 122 $self->{minified} ||= [CFPlus::average $dw, $dh, $data]
117 if $self->{minify}; 123 if $self->{minify};
118 124
119 pad2pot $data, $tw, $th unless $GL_NPOT; 125 # against rather broken cards we enforce a maximum texture size
120 $tw = min $MAX_W, $tw; 126 $tw = min $MAX_W, minpot $tw;
121 $th = min $MAX_H, $th; 127 $th = min $MAX_H, minpot $th;
122 128
129 # if only pot-textures are allowed, pot'ify tw/th
130 unless ($GL_NPOT && 0) {#d#
131 $tw = minpot $tw;
132 $th = minpot $th;
133 }
134
123 # now further decrease texture size 135 # now further decrease texture size until the
136 # card does accept it
124 while (!texture_valid_2d $self->{internalformat}, $tw, $th, $self->{format}, $self->{type}) { 137 while (!texture_valid_2d $self->{internalformat}, $tw, $th, $self->{format}, $self->{type}) {
125 # quarter the texture size 138 # quarter the texture size
126 $tw >>= 1; 139 $tw >>= 1;
127 $th >>= 1; 140 $th >>= 1;
128 } 141 }
129 142
130 my $render = $self->{render_cb} || $tw < $self->{w} || $th < $self->{h}; 143 # decide the amount of space used in the texture
144 my ($rw, $rh);
145 my $render;
146
147 if ($self->{render_cb}) {
148 # use only part of the texture
149 #$rw >>= 1 while $rw > $tw;
150 #$rh >>= 1 while $rh > $th;
151 $rw = min $dw, $tw;
152 $rh = min $dh, $th;
153 ++$render;
154 } else {
155 if ($self->{wrap} || $tw < $dw || $th < $dh) {
156 # scale to the full texture size
157 ($rw, $rh) = ($tw, $th);
158 ++$render;
159 } else {
160 # pad
161 pad $data, $dw, $dh, $tw, $th;
162 ($rw, $rh) = ($dw, $dh);
163 ($dw, $dh) = ($tw, $th);
164 }
165 }
131 166
132 if ($render) { 167 if ($render) {
133 glViewport 0, 0, $tw, $th; 168 glViewport 0, 0, $tw, $th;
169 #glScissor 0, 0, $tw, $th;
170 #glEnable GL_SCISSOR_TEST;
134 glMatrixMode GL_PROJECTION; 171 glMatrixMode GL_PROJECTION;
135 glLoadIdentity; 172 glLoadIdentity;
136 glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000; 173 glOrtho 0, $tw, 0, $th, -10000, 10000;
137 glMatrixMode GL_MODELVIEW; 174 glMatrixMode GL_MODELVIEW;
138 glLoadIdentity; 175 glLoadIdentity;
139 176
140 if ($self->{render_cb}) { 177 if ($self->{render_cb}) {
178 glScale $rw / $dw, $rh / $dh;
141 $self->{render_cb}->($self, $self->{w}, $self->{h}); 179 $self->{render_cb}->($self, $rw, $rh);
142 } else { 180 } else {
181 glClearColor 0, 0, 0, 0;
182 glClear GL_COLOR_BUFFER_BIT;
143 glPixelZoom $tw / $self->{w}, $th / $self->{h}; 183 glPixelZoom $tw / $dw, $th / $dh;
144 glDrawPixels $self->{w}, $self->{h}, 184 glDrawPixels $dw, $dh,
145 $self->{format}, 185 $self->{format},
146 $self->{type}, 186 $self->{type},
147 $data; 187 $data;
148 glPixelZoom 1, 1; 188 glPixelZoom 1, 1;
149 } 189 }
180 0, 0, 220 0, 0,
181 $tw, $th, 221 $tw, $th,
182 0; 222 0;
183 gl_check "copying to texture %dx%d if=%x", 223 gl_check "copying to texture %dx%d if=%x",
184 $tw, $th, $self->{internalformat}; 224 $tw, $th, $self->{internalformat};
225
226 #glDisable GL_SCISSOR_TEST;
185 } else { 227 } else {
186 glTexImage2D GL_TEXTURE_2D, 0, 228 glTexImage2D GL_TEXTURE_2D, 0,
187 $self->{internalformat}, 229 $self->{internalformat},
188 $tw, $th, 230 $dw, $dh,
189 0, 231 0,
190 $self->{format}, 232 $self->{format},
191 $self->{type}, 233 $self->{type},
192 $data; 234 $data;
193 gl_check "uploading texture %dx%d if=%x f=%x t=%x", 235 gl_check "uploading texture %dx%d if=%x f=%x t=%x",
194 $tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; 236 $tw, $th, $self->{internalformat}, $self->{format}, $self->{type};
195 } 237 }
196 238
197 $self->{s} = min 1, $self->{w} / $tw; 239 $self->{s} = $rw / $tw;
198 $self->{t} = min 1, $self->{h} / $th; 240 $self->{t} = $rh / $th;
199} 241}
200 242
201sub shutdown { 243sub shutdown {
202 my ($self) = @_; 244 my ($self) = @_;
203 245

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines