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.1 by root, Fri May 26 18:28:23 2006 UTC vs.
Revision 1.39 by root, Wed Jan 18 00:51:23 2012 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines