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.24 by root, Sun Jul 29 03:58:26 2007 UTC vs.
Revision 1.27 by root, Tue Aug 21 00:59:34 2007 UTC

32 %data, 32 %data,
33 }, $class; 33 }, $class;
34 34
35 CFPlus::weaken ($TEXTURES{$self+0} = $self); 35 CFPlus::weaken ($TEXTURES{$self+0} = $self);
36 36
37 $self->upload; 37 $self->upload
38 unless delete $self->{delay};
38 39
39 $self 40 $self
40} 41}
41 42
42sub new_from_image { 43sub new_from_image {
45 Carp::confess "tried to create texture from undefined image" 46 Carp::confess "tried to create texture from undefined image"
46 unless defined $image; 47 unless defined $image;
47 48
48 $class->new (image => $image, internalformat => undef, %arg) 49 $class->new (image => $image, internalformat => undef, %arg)
49} 50}
51
50 52
51sub new_from_file { 53sub new_from_file {
52 my ($class, $path, %arg) = @_; 54 my ($class, $path, %arg) = @_;
53 55
54 open my $fh, "<:raw", $path 56 open my $fh, "<:raw", $path
90 my ($class, $w, $h, $cb) = @_; 92 my ($class, $w, $h, $cb) = @_;
91 93
92 $class->new (w => $w || 1, h => $h || 1, render_cb => $cb, nearest => 1) 94 $class->new (w => $w || 1, h => $h || 1, render_cb => $cb, nearest => 1)
93} 95}
94 96
97sub loading_done($$) {
98 my ($self, $data) = @_;
99
100 delete $self->{loading};
101 ++$self->{delete_image};
102 $self->{image} = $data;
103 $self->upload;
104}
105
95sub upload { 106sub upload {
96 my ($self) = @_; 107 my ($self, $cb) = @_;
97 108
109 push @{ $self->{upload_done} }, $cb
110 if $cb;
111
112 return if $self->{loading};
113
98 return unless $GL_VERSION; 114 unless ($GL_VERSION) {
99 115 $self->{was_loaded} = 1;
100 # $tw,$th texture 116 return;
101 # $rw,$rh rendered/used size
102 # $dw,$dh $data
103
104 my ($data, $dw, $dh);
105
106 if (exists $self->{data}) {
107 $data = $self->{data};
108 ($dw, $dh) = @$self{qw(w h)};
109
110 } elsif (exists $self->{render_cb}) {
111 ($dw, $dh) = @$self{qw(w h)};
112
113 } elsif (exists $self->{image}) {
114 ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type})
115 = CFPlus::load_image_inline $self->{image};
116
117 $self->{internalformat} ||= $internalformat;
118 ($dw, $dh) = @$self{qw(w h)};
119 } else {
120 Carp::confess "tried to create texture that is not data, render or image";
121 } 117 }
122 118
119 unless ($self->{name}) {
120 # $tw,$th texture
121 # $rw,$rh rendered/used size
122 # $dw,$dh $data
123
124 my ($data, $dw, $dh);
125
126 if (defined $self->{data}) {
127 $data = $self->{data};
128 ($dw, $dh) = @$self{qw(w h)};
129
130 } elsif ($self->{render_cb}) {
131 ($dw, $dh) = @$self{qw(w h)};
132
133 } elsif (defined $self->{image}) {
134 ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type})
135 = CFPlus::load_image_inline $self->{image};
136
137 $self->{internalformat} ||= $internalformat;
138 ($dw, $dh) = @$self{qw(w h)};
139
140 delete $self->{image} if delete $self->{delete_image};
141
142 } elsif (defined $self->{tile}) {
143 ++$self->{loading};
144 return CFPlus::DB::get tilecache => $self->{tile}, sub {
145 $self->loading_done ($_[0]);
146 };
147
148 } elsif (defined $self->{path}) {
149 ++$self->{loading};
150 return CFPlus::DB::read_file $self->{path}, sub {
151 $self->loading_done ($_[0]);
152 };
153
154 } else {
155 Carp::confess "tried to create texture that is not data, render or image";
156 }
157
123 my ($tw, $th) = ($dw, $dh); 158 my ($tw, $th) = ($dw, $dh);
124 159
125 defined $data or $self->{render_cb} or die; # some sanity check 160 defined $data or $self->{render_cb} or die; # some sanity check
126 161
127 $self->{minified} ||= [CFPlus::average $dw, $dh, $data] 162 $self->{minified} ||= [CFPlus::average $dw, $dh, $data]
128 if $self->{minify}; 163 if $self->{minify};
129 164
130 # against rather broken cards we enforce a maximum texture size 165 # against rather broken cards we enforce a maximum texture size
131 $tw = min $MAX_W, minpot $tw; 166 $tw = min $MAX_W, minpot $tw;
132 $th = min $MAX_H, minpot $th; 167 $th = min $MAX_H, minpot $th;
133 168
134 # if only pot-textures are allowed, pot'ify tw/th 169 # if only pot-textures are allowed, pot'ify tw/th
135 unless ($GL_NPOT && 0) {#d# 170 unless ($GL_NPOT && 0) {#d#
136 $tw = minpot $tw; 171 $tw = minpot $tw;
137 $th = minpot $th; 172 $th = minpot $th;
138 } 173 }
139 174
140 # now further decrease texture size until the 175 # now further decrease texture size until the
141 # card does accept it 176 # card does accept it
142 while (!texture_valid_2d $self->{internalformat}, $tw, $th, $self->{format}, $self->{type}) { 177 while (!texture_valid_2d $self->{internalformat}, $tw, $th, $self->{format}, $self->{type}) {
143 # quarter the texture size 178 # quarter the texture size
144 $tw >>= 1; 179 $tw >>= 1;
145 $th >>= 1; 180 $th >>= 1;
146 } 181 }
147 182
148 # decide the amount of space used in the texture 183 # decide the amount of space used in the texture
149 my ($rw, $rh); 184 my ($rw, $rh);
150 my ($ox, $oy); # area shift to lessen effetc of buggy opengl implementations (nvida, ati) 185 my ($ox, $oy); # area shift to lessen effetc of buggy opengl implementations (nvida, ati)
151 my $render; 186 my $render;
152 187
153 if ($self->{render_cb}) { 188 if ($self->{render_cb}) {
154 # use only part of the texture 189 # use only part of the texture
155 #$rw >>= 1 while $rw > $tw; 190 #$rw >>= 1 while $rw > $tw;
156 #$rh >>= 1 while $rh > $th; 191 #$rh >>= 1 while $rh > $th;
157 $rw = min $dw, $tw; 192 $rw = min $dw, $tw;
158 $rh = min $dh, $th; 193 $rh = min $dh, $th;
159 ++$render;
160 } else {
161 if ($self->{wrap} || $tw < $dw || $th < $dh) {
162 # scale to the full texture size
163 ($rw, $rh) = ($tw, $th);
164 ++$render; 194 ++$render;
165 } else { 195 } else {
196 if ($self->{wrap} || $tw < $dw || $th < $dh) {
197 # scale to the full texture size
198 ($rw, $rh) = ($tw, $th);
199 ++$render;
200 } else {
166 # pad 201 # pad
167 pad $data, $dw, $dh, $tw, $th; 202 pad $data, $dw, $dh, $tw, $th;
168 ($rw, $rh) = ($dw, $dh); 203 ($rw, $rh) = ($dw, $dh);
169 ($dw, $dh) = ($tw, $th); 204 ($dw, $dh) = ($tw, $th);
170 } 205 }
171 } 206 }
172 207
173 if ($render) { 208 if ($render) {
174 $ox = int .5 * ($::WIDTH - $rw); 209 $ox = int .5 * ($::WIDTH - $rw);
175 $oy = int .5 * ($::HEIGHT - $rh); 210 $oy = int .5 * ($::HEIGHT - $rh);
176 211
177 glViewport $ox, $oy, $tw, $th; 212 glViewport $ox, $oy, $tw, $th;
178 #glScissor 0, 0, $tw, $th; 213 #glScissor 0, 0, $tw, $th;
179 #glEnable GL_SCISSOR_TEST; 214 #glEnable GL_SCISSOR_TEST;
180 glMatrixMode GL_PROJECTION; 215 glMatrixMode GL_PROJECTION;
181 glLoadIdentity; 216 glLoadIdentity;
182 glOrtho 0, $tw, 0, $th, -10000, 10000; 217 glOrtho 0, $tw, 0, $th, -10000, 10000;
183 glMatrixMode GL_MODELVIEW; 218 glMatrixMode GL_MODELVIEW;
184 glLoadIdentity; 219 glLoadIdentity;
185 220
186 if ($self->{render_cb}) { 221 if ($self->{render_cb}) {
187 glScale $rw / $dw, $rh / $dh; 222 glScale $rw / $dw, $rh / $dh;
188 $self->{render_cb}->($self, $rw, $rh); 223 $self->{render_cb}->($self, $rw, $rh);
189 } else { 224 } else {
190 glClearColor 0, 0, 0, 0; 225 glClearColor 0, 0, 0, 0;
191 glClear GL_COLOR_BUFFER_BIT; 226 glClear GL_COLOR_BUFFER_BIT;
192 glPixelZoom $tw / $dw, $th / $dh; 227 glPixelZoom $tw / $dw, $th / $dh;
193 glRasterPos 0, 0; 228 glRasterPos 0, 0;
194 glDrawPixels $dw, $dh, 229 glDrawPixels $dw, $dh,
230 $self->{format},
231 $self->{type},
232 $data;
233 glPixelZoom 1, 1;
234 }
235 }
236
237 glBindTexture GL_TEXTURE_2D, $self->{name} ||= glGenTexture;
238
239 if ($self->{wrap}) {
240 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
241 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
242 } else {
243 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, $GL_VERSION >= 1.2 ? GL_CLAMP_TO_EDGE : GL_CLAMP;
244 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, $GL_VERSION >= 1.2 ? GL_CLAMP_TO_EDGE : GL_CLAMP;
245 }
246
247 if ($::FAST || $self->{nearest}) {
248 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST;
249 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST;
250 } elsif ($self->{mipmap} && $GL_VERSION >= 1.4) {
251 glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1;
252 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
253 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR;
254 } else {
255 glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, $self->{mipmap};
256 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
257 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;
258 }
259
260 glGetError;
261
262 if ($render) {
263 glCopyTexImage2D GL_TEXTURE_2D, 0,
264 $self->{internalformat},
265 $ox, $oy,
266 $tw, $th,
267 0;
268 gl_check "copying to texture %dx%d if=%x",
269 $tw, $th, $self->{internalformat};
270
271 #glDisable GL_SCISSOR_TEST;
272 } else {
273 my $if = $self->{internalformat};
274
275 if ($GL_COMPRESS && $::CFG->{texture_compression}) {
276 if ($if == GL_RGB) {
277 $if = GL_COMPRESSED_RGB_ARB;
278 } elsif ($if == GL_RGBA) {
279 $if = GL_COMPRESSED_RGBA_ARB;
280 }
281 }
282
283 glTexImage2D GL_TEXTURE_2D, 0,
284 $if,
285 $dw, $dh,
286 0,
195 $self->{format}, 287 $self->{format},
196 $self->{type}, 288 $self->{type},
197 $data; 289 $data;
198 glPixelZoom 1, 1; 290 gl_check "uploading texture %dx%d if=%x f=%x t=%x",
291 $tw, $th, $self->{internalformat}, $self->{format}, $self->{type};
292 }
293
294 $self->{s} = $rw / $tw;
295 $self->{t} = $rh / $th;
296
297 if ($self->{tile}) {
298 $::MAP->set_texture ($self->{tile}, @$self{qw(name w h s t)}, @{$self->{minified}})
299 if $::MAP;
300 $::MAPWIDGET->update
301 if $::MAPWIDGET;
199 } 302 }
200 } 303 }
201 304
202 glBindTexture GL_TEXTURE_2D, $self->{name} ||= glGenTexture; 305 $_->($self)
203 306 for @{ (delete $self->{upload_done}) || [] };
204 if ($self->{wrap}) {
205 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
206 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
207 } else {
208 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, $GL_VERSION >= 1.2 ? GL_CLAMP_TO_EDGE : GL_CLAMP;
209 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, $GL_VERSION >= 1.2 ? GL_CLAMP_TO_EDGE : GL_CLAMP;
210 }
211
212 if ($::FAST || $self->{nearest}) {
213 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST;
214 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST;
215 } elsif ($self->{mipmap} && $GL_VERSION >= 1.4) {
216 glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1;
217 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
218 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR;
219 } else {
220 glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, $self->{mipmap};
221 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
222 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;
223 }
224
225 glGetError;
226
227 if ($render) {
228 glCopyTexImage2D GL_TEXTURE_2D, 0,
229 $self->{internalformat},
230 $ox, $oy,
231 $tw, $th,
232 0;
233 gl_check "copying to texture %dx%d if=%x",
234 $tw, $th, $self->{internalformat};
235
236 #glDisable GL_SCISSOR_TEST;
237 } else {
238 glTexImage2D GL_TEXTURE_2D, 0,
239 $self->{internalformat},
240 $dw, $dh,
241 0,
242 $self->{format},
243 $self->{type},
244 $data;
245 gl_check "uploading texture %dx%d if=%x f=%x t=%x",
246 $tw, $th, $self->{internalformat}, $self->{format}, $self->{type};
247 }
248
249 $self->{s} = $rw / $tw;
250 $self->{t} = $rh / $th;
251} 307}
252 308
253sub shutdown { 309sub unload {
254 my ($self) = @_; 310 my ($self) = @_;
255 311
256 glDeleteTexture $self->{name} 312 glDeleteTexture delete $self->{name}
257 if $self->{name}; 313 if $self->{name};
258} 314}
259 315
260sub DESTROY { 316sub DESTROY {
261 my ($self) = @_; 317 my ($self) = @_;
262 318
263 delete $TEXTURES{$self+0}; 319 delete $TEXTURES{$self+0};
264 320
265 $self->shutdown; 321 $self->unload;
266} 322}
267 323
268$CFPlus::OpenGL::INIT_HOOK{"CFPlus::Texture"} = sub { 324$CFPlus::OpenGL::INIT_HOOK{"CFPlus::Texture"} = sub {
269 # first mark all existing texture names as in-use, in case the context lost textures
270 glBindTexture GL_TEXTURE_2D, $_->{name}
271 for values %TEXTURES; 325 for (values %TEXTURES) {
326 next unless delete $_->{was_loaded};
272 $_->upload 327 $_->upload;
273 for values %TEXTURES; 328 }
274}; 329};
275 330
276$CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Texture"} = sub { 331$CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Texture"} = sub {
277 $_->shutdown
278 for values %TEXTURES; 332 for (values %TEXTURES) {
333 next unless $_->{name};
334 $_->{was_loaded} = 1;
335 $_->unload;
336 }
279}; 337};
280 338
2811; 3391;
282 340
283=back 341=back

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines