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 |
|
|
$self->upload; |
38 |
|
|
|
39 |
|
|
$self |
40 |
|
|
} |
41 |
|
|
|
42 |
|
|
sub new_from_image { |
43 |
|
|
my ($class, $image, %arg) = @_; |
44 |
|
|
|
45 |
root |
1.7 |
$class->new (image => $image, internalformat => undef, %arg) |
46 |
root |
1.1 |
} |
47 |
|
|
|
48 |
|
|
sub new_from_file { |
49 |
|
|
my ($class, $path, %arg) = @_; |
50 |
|
|
|
51 |
|
|
open my $fh, "<:raw", $path |
52 |
|
|
or die "$path: $!"; |
53 |
|
|
|
54 |
|
|
local $/; |
55 |
|
|
$class->new_from_image (<$fh>, %arg) |
56 |
|
|
} |
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 |
|
|
sub upload { |
93 |
|
|
my ($self) = @_; |
94 |
|
|
|
95 |
|
|
return unless $GL_VERSION; |
96 |
|
|
|
97 |
root |
1.16 |
# $tw,$th texture |
98 |
|
|
# $rw,$rh rendered/used size |
99 |
|
|
# $dw,$dh $data |
100 |
|
|
|
101 |
|
|
my ($data, $dw, $dh); |
102 |
root |
1.1 |
|
103 |
|
|
if (exists $self->{data}) { |
104 |
|
|
$data = $self->{data}; |
105 |
root |
1.16 |
($dw, $dh) = @$self{qw(w h)}; |
106 |
root |
1.1 |
|
107 |
|
|
} elsif (exists $self->{render_cb}) { |
108 |
root |
1.16 |
($dw, $dh) = @$self{qw(w h)}; |
109 |
root |
1.1 |
|
110 |
|
|
} else { |
111 |
root |
1.7 |
($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type}) |
112 |
root |
1.11 |
= CFPlus::load_image_inline $self->{image}; |
113 |
root |
1.7 |
|
114 |
|
|
$self->{internalformat} ||= $internalformat; |
115 |
root |
1.16 |
($dw, $dh) = @$self{qw(w h)}; |
116 |
root |
1.1 |
} |
117 |
|
|
|
118 |
root |
1.16 |
my ($tw, $th) = ($dw, $dh); |
119 |
|
|
|
120 |
root |
1.14 |
defined $data or $self->{render_cb} or die; # some sanity check |
121 |
root |
1.1 |
|
122 |
root |
1.16 |
$self->{minified} ||= [CFPlus::average $dw, $dh, $data] |
123 |
root |
1.1 |
if $self->{minify}; |
124 |
|
|
|
125 |
root |
1.16 |
# against rather broken cards we enforce a maximum texture size |
126 |
|
|
$tw = min $MAX_W, minpot $tw; |
127 |
|
|
$th = min $MAX_H, minpot $th; |
128 |
|
|
|
129 |
|
|
# if only pot-textures are allowed, pot'ify tw/th |
130 |
|
|
unless ($GL_NPOT && 0) {#d# |
131 |
|
|
$tw = minpot $tw; |
132 |
|
|
$th = minpot $th; |
133 |
|
|
} |
134 |
root |
1.14 |
|
135 |
root |
1.16 |
# now further decrease texture size until the |
136 |
|
|
# card does accept it |
137 |
root |
1.14 |
while (!texture_valid_2d $self->{internalformat}, $tw, $th, $self->{format}, $self->{type}) { |
138 |
|
|
# quarter the texture size |
139 |
|
|
$tw >>= 1; |
140 |
|
|
$th >>= 1; |
141 |
|
|
} |
142 |
|
|
|
143 |
root |
1.18 |
# decide the amount of space used in the texture |
144 |
|
|
my ($rw, $rh); |
145 |
root |
1.19 |
my $render; |
146 |
root |
1.18 |
|
147 |
root |
1.16 |
if ($self->{render_cb}) { |
148 |
|
|
# use only part of the texture |
149 |
|
|
#$rw >>= 1 while $rw > $tw; |
150 |
|
|
#$rh >>= 1 while $rh > $th; |
151 |
root |
1.18 |
$rw = min $dw, $tw; |
152 |
|
|
$rh = min $dh, $th; |
153 |
root |
1.19 |
++$render; |
154 |
root |
1.16 |
} else { |
155 |
|
|
if ($self->{wrap} || $tw < $dw || $th < $dh) { |
156 |
|
|
# scale to the full texture size |
157 |
|
|
($rw, $rh) = ($tw, $th); |
158 |
root |
1.19 |
++$render; |
159 |
root |
1.16 |
} else { |
160 |
|
|
# pad |
161 |
|
|
pad $data, $dw, $dh, $tw, $th; |
162 |
|
|
($rw, $rh) = ($dw, $dh); |
163 |
|
|
($dw, $dh) = ($tw, $th); |
164 |
|
|
} |
165 |
|
|
} |
166 |
|
|
|
167 |
root |
1.15 |
if ($render) { |
168 |
root |
1.14 |
glViewport 0, 0, $tw, $th; |
169 |
|
|
glMatrixMode GL_PROJECTION; |
170 |
|
|
glLoadIdentity; |
171 |
root |
1.16 |
glOrtho 0, $tw, 0, $th, -10000, 10000; |
172 |
root |
1.14 |
glMatrixMode GL_MODELVIEW; |
173 |
|
|
glLoadIdentity; |
174 |
root |
1.1 |
|
175 |
root |
1.14 |
if ($self->{render_cb}) { |
176 |
root |
1.16 |
glScale $rw / $dw, $rh / $dh; |
177 |
|
|
$self->{render_cb}->($self, $rw, $rh); |
178 |
root |
1.14 |
} else { |
179 |
root |
1.20 |
glClearColor 0, 0, 0, 0; |
180 |
|
|
glClear GL_COLOR_BUFFER_BIT; |
181 |
root |
1.16 |
glPixelZoom $tw / $dw, $th / $dh; |
182 |
|
|
glDrawPixels $dw, $dh, |
183 |
root |
1.14 |
$self->{format}, |
184 |
|
|
$self->{type}, |
185 |
|
|
$data; |
186 |
|
|
glPixelZoom 1, 1; |
187 |
|
|
} |
188 |
|
|
} |
189 |
root |
1.1 |
|
190 |
root |
1.14 |
glBindTexture GL_TEXTURE_2D, $self->{name} ||= glGenTexture; |
191 |
root |
1.1 |
|
192 |
root |
1.2 |
if ($self->{wrap}) { |
193 |
|
|
glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
194 |
|
|
glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
195 |
|
|
} else { |
196 |
|
|
glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, $GL_VERSION >= 1.2 ? GL_CLAMP_TO_EDGE : GL_CLAMP; |
197 |
|
|
glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, $GL_VERSION >= 1.2 ? GL_CLAMP_TO_EDGE : GL_CLAMP; |
198 |
|
|
} |
199 |
root |
1.1 |
|
200 |
root |
1.12 |
if ($::FAST || $self->{nearest}) { |
201 |
root |
1.1 |
glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; |
202 |
|
|
glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST; |
203 |
|
|
} elsif ($self->{mipmap} && $GL_VERSION >= 1.4) { |
204 |
|
|
glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1; |
205 |
|
|
glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
206 |
|
|
glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR; |
207 |
|
|
} else { |
208 |
|
|
glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, $self->{mipmap}; |
209 |
|
|
glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
210 |
|
|
glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR; |
211 |
|
|
} |
212 |
|
|
|
213 |
|
|
glGetError; |
214 |
|
|
|
215 |
root |
1.15 |
if ($render) { |
216 |
root |
1.14 |
glCopyTexImage2D GL_TEXTURE_2D, 0, |
217 |
|
|
$self->{internalformat}, |
218 |
|
|
0, 0, |
219 |
|
|
$tw, $th, |
220 |
|
|
0; |
221 |
|
|
gl_check "copying to texture %dx%d if=%x", |
222 |
|
|
$tw, $th, $self->{internalformat}; |
223 |
|
|
} else { |
224 |
root |
1.1 |
glTexImage2D GL_TEXTURE_2D, 0, |
225 |
|
|
$self->{internalformat}, |
226 |
root |
1.16 |
$dw, $dh, |
227 |
root |
1.1 |
0, |
228 |
|
|
$self->{format}, |
229 |
|
|
$self->{type}, |
230 |
|
|
$data; |
231 |
|
|
gl_check "uploading texture %dx%d if=%x f=%x t=%x", |
232 |
|
|
$tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; |
233 |
|
|
} |
234 |
root |
1.14 |
|
235 |
root |
1.16 |
$self->{s} = $rw / $tw; |
236 |
|
|
$self->{t} = $rh / $th; |
237 |
root |
1.1 |
} |
238 |
|
|
|
239 |
root |
1.8 |
sub shutdown { |
240 |
|
|
my ($self) = @_; |
241 |
|
|
|
242 |
root |
1.9 |
glDeleteTexture $self->{name} |
243 |
root |
1.8 |
if $self->{name}; |
244 |
|
|
} |
245 |
|
|
|
246 |
root |
1.1 |
sub DESTROY { |
247 |
|
|
my ($self) = @_; |
248 |
|
|
|
249 |
|
|
delete $TEXTURES{$self+0}; |
250 |
|
|
|
251 |
root |
1.8 |
$self->shutdown; |
252 |
root |
1.1 |
} |
253 |
|
|
|
254 |
root |
1.11 |
$CFPlus::OpenGL::INIT_HOOK{"CFPlus::Texture"} = sub { |
255 |
root |
1.9 |
# first mark all existing texture names as in-use, in case the context lost textures |
256 |
|
|
glBindTexture GL_TEXTURE_2D, $_->{name} |
257 |
|
|
for values %TEXTURES; |
258 |
root |
1.1 |
$_->upload |
259 |
|
|
for values %TEXTURES; |
260 |
|
|
}; |
261 |
|
|
|
262 |
root |
1.11 |
$CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Texture"} = sub { |
263 |
root |
1.8 |
$_->shutdown |
264 |
|
|
for values %TEXTURES; |
265 |
|
|
}; |
266 |
|
|
|
267 |
root |
1.1 |
1; |
268 |
|
|
|
269 |
|
|
=back |
270 |
|
|
|
271 |
|
|
=head1 AUTHOR |
272 |
|
|
|
273 |
|
|
Marc Lehmann <schmorp@schmorp.de> |
274 |
|
|
http://home.schmorp.de/ |
275 |
|
|
|
276 |
|
|
=cut |
277 |
|
|
|