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.35 by root, Wed Sep 3 06:07:39 2008 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines