1 |
root |
1.1 |
=head1 NAME |
2 |
|
|
|
3 |
root |
1.11 |
CFPlus::Texture - tetxure class for CFPlus |
4 |
root |
1.1 |
|
5 |
|
|
=head1 SYNOPSIS |
6 |
|
|
|
7 |
root |
1.11 |
use CFPlus::Texture; |
8 |
root |
1.1 |
|
9 |
|
|
=head1 DESCRIPTION |
10 |
|
|
|
11 |
|
|
=over 4 |
12 |
|
|
|
13 |
|
|
=cut |
14 |
|
|
|
15 |
root |
1.11 |
package CFPlus::Texture; |
16 |
root |
1.1 |
|
17 |
|
|
use strict; |
18 |
|
|
|
19 |
root |
1.14 |
use List::Util qw(max min); |
20 |
root |
1.11 |
use CFPlus::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.13 |
CFPlus::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.25 |
|
53 |
root |
1.1 |
sub new_from_file { |
54 |
|
|
my ($class, $path, %arg) = @_; |
55 |
|
|
|
56 |
|
|
open my $fh, "<:raw", $path |
57 |
|
|
or die "$path: $!"; |
58 |
|
|
|
59 |
|
|
local $/; |
60 |
|
|
$class->new_from_image (<$fh>, %arg) |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
#sub new_from_surface { |
64 |
|
|
# my ($class, $surface) = @_; |
65 |
|
|
# |
66 |
|
|
# $surface->rgba; |
67 |
|
|
# |
68 |
|
|
# $class->new ( |
69 |
|
|
# data => $surface->pixels, |
70 |
|
|
# w => $surface->width, |
71 |
|
|
# h => $surface->height, |
72 |
|
|
# ) |
73 |
|
|
#} |
74 |
|
|
|
75 |
root |
1.10 |
#sub new_from_layout { |
76 |
|
|
# my ($class, $layout, %arg) = @_; |
77 |
|
|
# |
78 |
|
|
# my ($w, $h, $data, $format, $internalformat) = $layout->render; |
79 |
|
|
# |
80 |
|
|
# $class->new ( |
81 |
|
|
# w => $w, |
82 |
|
|
# h => $h, |
83 |
|
|
# data => $data, |
84 |
|
|
# format => $format, |
85 |
|
|
# internalformat => $format, |
86 |
|
|
# type => GL_UNSIGNED_BYTE, |
87 |
|
|
# %arg, |
88 |
|
|
# ) |
89 |
|
|
#} |
90 |
root |
1.1 |
|
91 |
|
|
sub new_from_opengl { |
92 |
|
|
my ($class, $w, $h, $cb) = @_; |
93 |
|
|
|
94 |
root |
1.12 |
$class->new (w => $w || 1, h => $h || 1, render_cb => $cb, nearest => 1) |
95 |
root |
1.1 |
} |
96 |
|
|
|
97 |
root |
1.25 |
sub loading_done($$) { |
98 |
|
|
my ($self, $data) = @_; |
99 |
|
|
|
100 |
|
|
delete $self->{loading}; |
101 |
|
|
++$self->{delete_image}; |
102 |
|
|
$self->{image} = $data; |
103 |
|
|
$self->upload; |
104 |
|
|
} |
105 |
|
|
|
106 |
root |
1.1 |
sub upload { |
107 |
root |
1.25 |
my ($self, $cb) = @_; |
108 |
|
|
|
109 |
|
|
push @{ $self->{upload_done} }, $cb |
110 |
|
|
if $cb; |
111 |
|
|
|
112 |
|
|
return if $self->{loading}; |
113 |
|
|
|
114 |
|
|
unless ($GL_VERSION) { |
115 |
|
|
$self->{was_loaded} = 1; |
116 |
|
|
return; |
117 |
|
|
} |
118 |
root |
1.1 |
|
119 |
root |
1.25 |
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 |
root |
1.27 |
if (defined $self->{data}) { |
127 |
root |
1.25 |
$data = $self->{data}; |
128 |
|
|
($dw, $dh) = @$self{qw(w h)}; |
129 |
|
|
|
130 |
root |
1.27 |
} elsif ($self->{render_cb}) { |
131 |
root |
1.25 |
($dw, $dh) = @$self{qw(w h)}; |
132 |
|
|
|
133 |
root |
1.27 |
} elsif (defined $self->{image}) { |
134 |
root |
1.25 |
($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 |
root |
1.27 |
} elsif (defined $self->{tile}) { |
143 |
root |
1.25 |
++$self->{loading}; |
144 |
|
|
return CFPlus::DB::get tilecache => $self->{tile}, sub { |
145 |
|
|
$self->loading_done ($_[0]); |
146 |
|
|
}; |
147 |
|
|
|
148 |
root |
1.27 |
} elsif (defined $self->{path}) { |
149 |
root |
1.25 |
++$self->{loading}; |
150 |
|
|
return CFPlus::DB::read_file $self->{path}, sub { |
151 |
|
|
$self->loading_done ($_[0]); |
152 |
|
|
}; |
153 |
root |
1.1 |
|
154 |
root |
1.25 |
} else { |
155 |
|
|
Carp::confess "tried to create texture that is not data, render or image"; |
156 |
|
|
} |
157 |
root |
1.16 |
|
158 |
root |
1.25 |
my ($tw, $th) = ($dw, $dh); |
159 |
root |
1.1 |
|
160 |
root |
1.25 |
defined $data or $self->{render_cb} or die; # some sanity check |
161 |
root |
1.1 |
|
162 |
root |
1.25 |
$self->{minified} ||= [CFPlus::average $dw, $dh, $data] |
163 |
|
|
if $self->{minify}; |
164 |
root |
1.1 |
|
165 |
root |
1.25 |
# against rather broken cards we enforce a maximum texture size |
166 |
root |
1.28 |
$tw = min $MAX_W, $tw; |
167 |
|
|
$th = min $MAX_H, $th; |
168 |
root |
1.7 |
|
169 |
root |
1.25 |
# if only pot-textures are allowed, pot'ify tw/th |
170 |
root |
1.28 |
unless ($GL_NPOT) { |
171 |
|
|
$tw = CFPlus::minpot $tw; |
172 |
|
|
$th = CFPlus::minpot $th; |
173 |
root |
1.25 |
} |
174 |
root |
1.1 |
|
175 |
root |
1.25 |
# now further decrease texture size until the |
176 |
|
|
# card does accept it |
177 |
|
|
while (!texture_valid_2d $self->{internalformat}, $tw, $th, $self->{format}, $self->{type}) { |
178 |
|
|
# quarter the texture size |
179 |
|
|
$tw >>= 1; |
180 |
|
|
$th >>= 1; |
181 |
|
|
} |
182 |
root |
1.16 |
|
183 |
root |
1.25 |
# decide the amount of space used in the texture |
184 |
|
|
my ($rw, $rh); |
185 |
|
|
my ($ox, $oy); # area shift to lessen effetc of buggy opengl implementations (nvida, ati) |
186 |
|
|
my $render; |
187 |
root |
1.1 |
|
188 |
root |
1.25 |
if ($self->{render_cb}) { |
189 |
|
|
# use only part of the texture |
190 |
|
|
#$rw >>= 1 while $rw > $tw; |
191 |
|
|
#$rh >>= 1 while $rh > $th; |
192 |
|
|
$rw = min $dw, $tw; |
193 |
|
|
$rh = min $dh, $th; |
194 |
|
|
++$render; |
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 { |
201 |
|
|
# pad |
202 |
|
|
pad $data, $dw, $dh, $tw, $th; |
203 |
|
|
($rw, $rh) = ($dw, $dh); |
204 |
|
|
($dw, $dh) = ($tw, $th); |
205 |
|
|
} |
206 |
|
|
} |
207 |
root |
1.1 |
|
208 |
root |
1.25 |
if ($render) { |
209 |
|
|
$ox = int .5 * ($::WIDTH - $rw); |
210 |
|
|
$oy = int .5 * ($::HEIGHT - $rh); |
211 |
|
|
|
212 |
|
|
glViewport $ox, $oy, $tw, $th; |
213 |
|
|
#glScissor 0, 0, $tw, $th; |
214 |
|
|
#glEnable GL_SCISSOR_TEST; |
215 |
|
|
glMatrixMode GL_PROJECTION; |
216 |
|
|
glLoadIdentity; |
217 |
|
|
glOrtho 0, $tw, 0, $th, -10000, 10000; |
218 |
|
|
glMatrixMode GL_MODELVIEW; |
219 |
|
|
glLoadIdentity; |
220 |
|
|
|
221 |
|
|
if ($self->{render_cb}) { |
222 |
|
|
glScale $rw / $dw, $rh / $dh; |
223 |
|
|
$self->{render_cb}->($self, $rw, $rh); |
224 |
|
|
} else { |
225 |
|
|
glClearColor 0, 0, 0, 0; |
226 |
|
|
glClear GL_COLOR_BUFFER_BIT; |
227 |
|
|
glPixelZoom $tw / $dw, $th / $dh; |
228 |
|
|
glRasterPos 0, 0; |
229 |
|
|
glDrawPixels $dw, $dh, |
230 |
|
|
$self->{format}, |
231 |
|
|
$self->{type}, |
232 |
|
|
$data; |
233 |
|
|
glPixelZoom 1, 1; |
234 |
|
|
} |
235 |
|
|
} |
236 |
root |
1.16 |
|
237 |
root |
1.25 |
glBindTexture GL_TEXTURE_2D, $self->{name} ||= glGenTexture; |
238 |
root |
1.14 |
|
239 |
root |
1.25 |
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 |
root |
1.14 |
|
247 |
root |
1.25 |
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 |
root |
1.16 |
} else { |
255 |
root |
1.25 |
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 |
root |
1.16 |
} |
259 |
root |
1.25 |
|
260 |
|
|
glGetError; |
261 |
root |
1.16 |
|
262 |
root |
1.25 |
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 |
root |
1.1 |
|
271 |
root |
1.25 |
#glDisable GL_SCISSOR_TEST; |
272 |
root |
1.14 |
} else { |
273 |
root |
1.26 |
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 |
root |
1.25 |
glTexImage2D GL_TEXTURE_2D, 0, |
284 |
root |
1.26 |
$if, |
285 |
root |
1.25 |
$dw, $dh, |
286 |
|
|
0, |
287 |
root |
1.14 |
$self->{format}, |
288 |
|
|
$self->{type}, |
289 |
|
|
$data; |
290 |
root |
1.25 |
gl_check "uploading texture %dx%d if=%x f=%x t=%x", |
291 |
|
|
$tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; |
292 |
root |
1.14 |
} |
293 |
root |
1.1 |
|
294 |
root |
1.25 |
$self->{s} = $rw / $tw; |
295 |
|
|
$self->{t} = $rh / $th; |
296 |
root |
1.27 |
|
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; |
302 |
|
|
} |
303 |
root |
1.1 |
} |
304 |
|
|
|
305 |
root |
1.25 |
$_->($self) |
306 |
|
|
for @{ (delete $self->{upload_done}) || [] }; |
307 |
root |
1.1 |
} |
308 |
|
|
|
309 |
root |
1.25 |
sub unload { |
310 |
root |
1.8 |
my ($self) = @_; |
311 |
|
|
|
312 |
root |
1.25 |
glDeleteTexture delete $self->{name} |
313 |
root |
1.8 |
if $self->{name}; |
314 |
|
|
} |
315 |
|
|
|
316 |
root |
1.1 |
sub DESTROY { |
317 |
|
|
my ($self) = @_; |
318 |
|
|
|
319 |
|
|
delete $TEXTURES{$self+0}; |
320 |
|
|
|
321 |
root |
1.25 |
$self->unload; |
322 |
root |
1.1 |
} |
323 |
|
|
|
324 |
root |
1.11 |
$CFPlus::OpenGL::INIT_HOOK{"CFPlus::Texture"} = sub { |
325 |
root |
1.25 |
for (values %TEXTURES) { |
326 |
|
|
next unless delete $_->{was_loaded}; |
327 |
|
|
$_->upload; |
328 |
|
|
} |
329 |
root |
1.1 |
}; |
330 |
|
|
|
331 |
root |
1.11 |
$CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Texture"} = sub { |
332 |
root |
1.25 |
for (values %TEXTURES) { |
333 |
|
|
next unless $_->{name}; |
334 |
|
|
$_->{was_loaded} = 1; |
335 |
|
|
$_->unload; |
336 |
|
|
} |
337 |
root |
1.8 |
}; |
338 |
|
|
|
339 |
root |
1.1 |
1; |
340 |
|
|
|
341 |
|
|
=back |
342 |
|
|
|
343 |
|
|
=head1 AUTHOR |
344 |
|
|
|
345 |
|
|
Marc Lehmann <schmorp@schmorp.de> |
346 |
|
|
http://home.schmorp.de/ |
347 |
|
|
|
348 |
|
|
=cut |
349 |
|
|
|