ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Texture.pm
Revision: 1.35
Committed: Wed Sep 3 06:07:39 2008 UTC (15 years, 10 months ago) by root
Branch: MAIN
CVS Tags: rel-0_9976
Changes since 1.34: +9 -7 lines
Log Message:
implement delayed-loading for theme textures

File Contents

# User Rev Content
1 root 1.1 =head1 NAME
2    
3 elmex 1.33 DC::Texture - tetxure class for Deliantra-Client
4 root 1.1
5     =head1 SYNOPSIS
6    
7 root 1.31 use DC::Texture;
8 root 1.1
9     =head1 DESCRIPTION
10    
11     =over 4
12    
13     =cut
14    
15 root 1.31 package DC::Texture;
16 root 1.1
17     use strict;
18    
19 root 1.14 use List::Util qw(max min);
20 root 1.31 use DC::OpenGL;
21 root 1.1
22     my %TEXTURES;
23 root 1.14 my ($MAX_W, $MAX_H) = (4096, 4096); # maximum texture size attempted by this module
24 root 1.1
25     sub new {
26     my ($class, %data) = @_;
27    
28     my $self = bless {
29     internalformat => GL_RGBA,
30     format => GL_RGBA,
31     type => GL_UNSIGNED_BYTE,
32     %data,
33     }, $class;
34    
35 root 1.31 DC::weaken ($TEXTURES{$self+0} = $self);
36 root 1.1
37 root 1.25 $self->upload
38     unless delete $self->{delay};
39 root 1.1
40     $self
41     }
42    
43     sub new_from_image {
44     my ($class, $image, %arg) = @_;
45    
46 root 1.24 Carp::confess "tried to create texture from undefined image"
47     unless defined $image;
48    
49 root 1.7 $class->new (image => $image, internalformat => undef, %arg)
50 root 1.1 }
51    
52 root 1.35 sub new_from_resource {
53 root 1.1 my ($class, $path, %arg) = @_;
54    
55 root 1.35 $class->new (resource_path => $path, internalformat => undef, %arg)
56 root 1.1 }
57    
58     #sub new_from_surface {
59     # my ($class, $surface) = @_;
60     #
61     # $surface->rgba;
62     #
63     # $class->new (
64     # data => $surface->pixels,
65     # w => $surface->width,
66     # h => $surface->height,
67     # )
68     #}
69    
70 root 1.10 #sub new_from_layout {
71     # my ($class, $layout, %arg) = @_;
72     #
73     # my ($w, $h, $data, $format, $internalformat) = $layout->render;
74     #
75     # $class->new (
76     # w => $w,
77     # h => $h,
78     # data => $data,
79     # format => $format,
80     # internalformat => $format,
81     # type => GL_UNSIGNED_BYTE,
82     # %arg,
83     # )
84     #}
85 root 1.1
86     sub new_from_opengl {
87     my ($class, $w, $h, $cb) = @_;
88    
89 root 1.12 $class->new (w => $w || 1, h => $h || 1, render_cb => $cb, nearest => 1)
90 root 1.1 }
91    
92 root 1.25 sub loading_done($$) {
93     my ($self, $data) = @_;
94    
95     delete $self->{loading};
96     ++$self->{delete_image};
97     $self->{image} = $data;
98     $self->upload;
99     }
100    
101 root 1.1 sub upload {
102 root 1.25 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 root 1.29 $self->{want_upload} = -1;
111 root 1.25 return;
112     }
113 root 1.1
114 root 1.25 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 root 1.35 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 root 1.27 if (defined $self->{data}) {
129 root 1.25 $data = $self->{data};
130     ($dw, $dh) = @$self{qw(w h)};
131    
132 root 1.27 } elsif ($self->{render_cb}) {
133 root 1.25 ($dw, $dh) = @$self{qw(w h)};
134    
135 root 1.27 } elsif (defined $self->{image}) {
136 root 1.25 ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type})
137 root 1.31 = DC::load_image_inline $self->{image};
138 root 1.34
139 root 1.25 $self->{internalformat} ||= $internalformat;
140     ($dw, $dh) = @$self{qw(w h)};
141    
142     delete $self->{image} if delete $self->{delete_image};
143    
144 root 1.27 } elsif (defined $self->{tile}) {
145 root 1.25 ++$self->{loading};
146 root 1.31 return DC::DB::get tilecache => $self->{tile}, sub {
147 root 1.25 $self->loading_done ($_[0]);
148     };
149    
150 root 1.27 } elsif (defined $self->{path}) {
151 root 1.25 ++$self->{loading};
152 root 1.31 return DC::DB::read_file $self->{path}, sub {
153 root 1.25 $self->loading_done ($_[0]);
154     };
155 root 1.1
156 root 1.25 } else {
157     Carp::confess "tried to create texture that is not data, render or image";
158     }
159 root 1.16
160 root 1.25 my ($tw, $th) = ($dw, $dh);
161 root 1.1
162 root 1.25 defined $data or $self->{render_cb} or die; # some sanity check
163 root 1.1
164 root 1.31 $self->{minified} ||= [DC::average $dw, $dh, $data]
165 root 1.25 if $self->{minify};
166 root 1.1
167 root 1.25 # against rather broken cards we enforce a maximum texture size
168 root 1.28 $tw = min $MAX_W, $tw;
169     $th = min $MAX_H, $th;
170 root 1.7
171 root 1.25 # if only pot-textures are allowed, pot'ify tw/th
172 root 1.28 unless ($GL_NPOT) {
173 root 1.31 $tw = DC::minpot $tw;
174     $th = DC::minpot $th;
175 root 1.25 }
176 root 1.1
177 root 1.25 # now further decrease texture size until the
178     # card does accept it
179     while (!texture_valid_2d $self->{internalformat}, $tw, $th, $self->{format}, $self->{type}) {
180     # quarter the texture size
181     $tw >>= 1;
182     $th >>= 1;
183     }
184 root 1.16
185 root 1.25 # decide the amount of space used in the texture
186     my ($rw, $rh);
187     my ($ox, $oy); # area shift to lessen effetc of buggy opengl implementations (nvida, ati)
188     my $render;
189 root 1.1
190 root 1.25 if ($self->{render_cb}) {
191     # use only part of the texture
192     #$rw >>= 1 while $rw > $tw;
193     #$rh >>= 1 while $rh > $th;
194     $rw = min $dw, $tw;
195     $rh = min $dh, $th;
196     ++$render;
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 {
203     # pad
204     pad $data, $dw, $dh, $tw, $th;
205     ($rw, $rh) = ($dw, $dh);
206     ($dw, $dh) = ($tw, $th);
207     }
208     }
209 root 1.1
210 root 1.25 if ($render) {
211     $ox = int .5 * ($::WIDTH - $rw);
212     $oy = int .5 * ($::HEIGHT - $rh);
213    
214     glViewport $ox, $oy, $tw, $th;
215     #glScissor 0, 0, $tw, $th;
216     #glEnable GL_SCISSOR_TEST;
217     glMatrixMode GL_PROJECTION;
218     glLoadIdentity;
219     glOrtho 0, $tw, 0, $th, -10000, 10000;
220     glMatrixMode GL_MODELVIEW;
221     glLoadIdentity;
222    
223     if ($self->{render_cb}) {
224     glScale $rw / $dw, $rh / $dh;
225     $self->{render_cb}->($self, $rw, $rh);
226     } else {
227     glClearColor 0, 0, 0, 0;
228     glClear GL_COLOR_BUFFER_BIT;
229     glPixelZoom $tw / $dw, $th / $dh;
230     glRasterPos 0, 0;
231     glDrawPixels $dw, $dh,
232     $self->{format},
233     $self->{type},
234     $data;
235     glPixelZoom 1, 1;
236     }
237     }
238 root 1.16
239 root 1.29 if ((my $name = delete $self->{want_upload}) > 0) {
240     $self->{name} = $name;
241     }
242    
243 root 1.25 glBindTexture GL_TEXTURE_2D, $self->{name} ||= glGenTexture;
244 root 1.14
245 root 1.25 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 root 1.14
253 root 1.25 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 root 1.16 } else {
261 root 1.25 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 root 1.16 }
265 root 1.25
266     glGetError;
267 root 1.16
268 root 1.25 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 root 1.1
277 root 1.25 #glDisable GL_SCISSOR_TEST;
278 root 1.14 } else {
279 root 1.26 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 root 1.25 glTexImage2D GL_TEXTURE_2D, 0,
290 root 1.26 $if,
291 root 1.25 $dw, $dh,
292     0,
293 root 1.14 $self->{format},
294     $self->{type},
295     $data;
296 root 1.25 gl_check "uploading texture %dx%d if=%x f=%x t=%x",
297     $tw, $th, $self->{internalformat}, $self->{format}, $self->{type};
298 root 1.14 }
299 root 1.1
300 root 1.25 $self->{s} = $rw / $tw;
301     $self->{t} = $rh / $th;
302 root 1.27
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;
308     }
309 root 1.1 }
310    
311 root 1.25 $_->($self)
312     for @{ (delete $self->{upload_done}) || [] };
313 root 1.1 }
314    
315 root 1.25 sub unload {
316 root 1.8 my ($self) = @_;
317    
318 root 1.25 glDeleteTexture delete $self->{name}
319 root 1.8 if $self->{name};
320     }
321    
322 root 1.1 sub DESTROY {
323     my ($self) = @_;
324    
325     delete $TEXTURES{$self+0};
326    
327 root 1.25 $self->unload;
328 root 1.1 }
329    
330 root 1.31 $DC::OpenGL::INIT_HOOK{"DC::Texture"} = sub {
331 root 1.32 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
337     glBindTexture GL_TEXTURE_2D, $name;
338     glTexImage2D GL_TEXTURE_2D, 0, GL_ALPHA, 0, 0, 0, GL_ALPHA, GL_UNSIGNED_BYTE;
339     }
340     }
341 root 1.25 }
342 root 1.1 };
343    
344 root 1.31 $DC::OpenGL::SHUTDOWN_HOOK{"DC::Texture"} = sub {
345 root 1.25 for (values %TEXTURES) {
346     next unless $_->{name};
347 root 1.29 $_->{want_upload} = $_->{name};
348 root 1.25 $_->unload;
349     }
350 root 1.8 };
351    
352 root 1.1 1;
353    
354     =back
355    
356     =head1 AUTHOR
357    
358     Marc Lehmann <schmorp@schmorp.de>
359     http://home.schmorp.de/
360    
361     =cut
362