… | |
… | |
19 | |
19 | |
20 | use XSLoader; |
20 | use XSLoader; |
21 | XSLoader::load "CFClient", $VERSION; |
21 | XSLoader::load "CFClient", $VERSION; |
22 | } |
22 | } |
23 | |
23 | |
|
|
24 | use Carp (); |
24 | use AnyEvent; |
25 | use AnyEvent; |
25 | use BerkeleyDB; |
26 | use BerkeleyDB; |
26 | use SDL::OpenGL; |
27 | use CFClient::OpenGL; |
27 | |
28 | |
28 | our %GL_EXT; |
29 | our %GL_EXT; |
29 | our $GL_VERSION; |
30 | our $GL_VERSION; |
30 | |
31 | |
31 | our $GL_NPOT; |
32 | our $GL_NPOT; |
… | |
… | |
141 | |
142 | |
142 | use strict; |
143 | use strict; |
143 | |
144 | |
144 | use Scalar::Util; |
145 | use Scalar::Util; |
145 | |
146 | |
146 | use SDL::OpenGL; |
147 | use CFClient::OpenGL; |
147 | |
148 | |
148 | my %TEXTURES; |
149 | my %TEXTURES; |
149 | |
150 | |
150 | sub new { |
151 | sub new { |
151 | my ($class, %data) = @_; |
152 | my ($class, %data) = @_; |
… | |
… | |
169 | |
170 | |
170 | $class->new (image => $image, %arg) |
171 | $class->new (image => $image, %arg) |
171 | } |
172 | } |
172 | |
173 | |
173 | sub new_from_file { |
174 | sub new_from_file { |
174 | my ($class, $path) = @_; |
175 | my ($class, $path, %arg) = @_; |
175 | |
176 | |
176 | open my $fh, "<:raw", $path |
177 | open my $fh, "<:raw", $path |
177 | or die "$path: $!"; |
178 | or die "$path: $!"; |
178 | |
179 | |
179 | local $/; |
180 | local $/; |
180 | $class->new_from_image (<$fh>) |
181 | $class->new_from_image (<$fh>, %arg) |
181 | } |
182 | } |
182 | |
183 | |
183 | #sub new_from_surface { |
184 | #sub new_from_surface { |
184 | # my ($class, $surface) = @_; |
185 | # my ($class, $surface) = @_; |
185 | # |
186 | # |
… | |
… | |
191 | # h => $surface->height, |
192 | # h => $surface->height, |
192 | # ) |
193 | # ) |
193 | #} |
194 | #} |
194 | |
195 | |
195 | sub new_from_layout { |
196 | sub new_from_layout { |
196 | my ($class, $layout) = @_; |
197 | my ($class, $layout, %arg) = @_; |
197 | |
198 | |
198 | my ($w, $h, $data) = $layout->render; |
199 | my ($w, $h, $data) = $layout->render; |
199 | |
200 | |
200 | $class->new ( |
201 | $class->new ( |
201 | w => $w, |
202 | w => $w, |
202 | h => $h, |
203 | h => $h, |
203 | data => $data, |
204 | data => $data, |
204 | internalformat => GL_ALPHA4, |
|
|
205 | format => GL_ALPHA, |
205 | format => GL_ALPHA, |
|
|
206 | internalformat => GL_ALPHA, |
206 | type => GL_UNSIGNED_BYTE, |
207 | type => GL_UNSIGNED_BYTE, |
|
|
208 | %arg, |
207 | ) |
209 | ) |
208 | } |
210 | } |
209 | |
211 | |
210 | sub new_from_opengl { |
212 | sub new_from_opengl { |
211 | my ($class, $w, $h, $cb) = @_; |
213 | my ($class, $w, $h, $cb) = @_; |
… | |
… | |
254 | unless ($GL_NPOT) { |
256 | unless ($GL_NPOT) { |
255 | # TODO: does not work for zero-sized textures |
257 | # TODO: does not work for zero-sized textures |
256 | $tw = topot $tw; |
258 | $tw = topot $tw; |
257 | $th = topot $th; |
259 | $th = topot $th; |
258 | |
260 | |
259 | if ($tw != $self->{w} || $th != $self->{h} && defined $data) { |
261 | if (($tw != $self->{w} || $th != $self->{h}) && defined $data) { |
260 | my $bpp = (length $data) / ($self->{w} * $self->{h}); |
262 | my $bpp = (length $data) / ($self->{w} * $self->{h}); |
261 | $data = pack "(a" . ($tw * $bpp) . ")*", |
263 | $data = pack "(a" . ($tw * $bpp) . ")*", |
262 | unpack "(a" . ($self->{w} * $bpp) . ")*", $data; |
264 | unpack "(a" . ($self->{w} * $bpp) . ")*", $data; |
263 | $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h}); |
265 | $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h}); |
264 | } |
266 | } |
265 | } |
267 | } |
266 | |
268 | |
267 | $self->{s} = $self->{w} / $tw; |
269 | $self->{s} = $self->{w} / $tw; |
268 | $self->{t} = $self->{h} / $th; |
270 | $self->{t} = $self->{h} / $th; |
269 | |
271 | |
270 | $self->{name} ||= (glGenTextures 1)->[0]; |
272 | $self->{name} ||= glGenTexture; |
271 | |
273 | |
272 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
274 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
273 | |
275 | |
274 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR; |
|
|
275 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR; |
|
|
276 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
276 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
277 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
277 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
|
|
278 | |
|
|
279 | if ($::FAST) { |
|
|
280 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; |
|
|
281 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST; |
|
|
282 | } elsif ($self->{mipmap} && $GL_VERSION >= 1.4) { |
|
|
283 | # alternatively check for 0x8191 |
|
|
284 | glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1; |
|
|
285 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
|
|
286 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR; |
|
|
287 | } else { |
|
|
288 | glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, $self->{mipmap}; |
|
|
289 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
|
|
290 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR; |
|
|
291 | } |
278 | |
292 | |
|
|
293 | glGetError; |
|
|
294 | |
279 | if (defined $data) { |
295 | if (defined $data) { |
280 | glTexImage2D GL_TEXTURE_2D, 0, |
296 | glTexImage2D GL_TEXTURE_2D, 0, |
281 | $self->{internalformat}, |
297 | $self->{internalformat}, |
282 | $tw, $th, # need to pad texture first |
298 | $tw, $th, # need to pad texture first |
283 | 0, |
299 | 0, |
284 | $self->{format}, |
300 | $self->{format}, |
285 | $self->{type}, |
301 | $self->{type}, |
286 | $data; |
302 | $data; |
287 | if (my $error = glGetError) { |
303 | if (my $error = glGetError) { |
288 | warn sprintf "texture upload error: %x %dx%d i=%x f=%x t=%x\n", |
304 | Carp::cluck sprintf "texture upload error: %x %dx%d i=%x f=%x t=%x", |
289 | $error, $tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; |
305 | $error, $tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; |
290 | } |
306 | } |
291 | } else { |
307 | } else { |
292 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
308 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
293 | $self->{internalformat}, |
309 | $self->{internalformat}, |
294 | 0, 0, |
310 | 0, 0, |
295 | $tw, $th, |
311 | $tw, $th, |
296 | 0; |
312 | 0; |
297 | glGetError and die "glCopyTexImage2D $tw,$th"; |
313 | if (my $error = glGetError) { |
|
|
314 | Carp::cluck sprintf "texture upload error: %x %dx%d i=%x", |
|
|
315 | $error, $tw, $th, $self->{internalformat}; |
|
|
316 | } |
298 | } |
317 | } |
299 | } |
318 | } |
300 | |
319 | |
301 | sub DESTROY { |
320 | sub DESTROY { |
302 | my ($self) = @_; |
321 | my ($self) = @_; |
303 | |
322 | |
304 | delete $TEXTURES{$self+0}; |
323 | delete $TEXTURES{$self+0}; |
305 | |
324 | |
306 | glDeleteTextures delete $self->{name} |
325 | glDeleteTexture delete $self->{name} |
307 | if $self->{name}; |
326 | if $self->{name}; |
308 | } |
327 | } |
309 | |
328 | |
310 | sub restore_state{ |
329 | sub restore_state{ |
311 | $_->upload |
330 | $_->upload |