1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | CFClient::Texture - tetxure class for CFClient |
3 | CFPlus::Texture - tetxure class for CFPlus |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use CFClient::Texture; |
7 | use CFPlus::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 | |
15 | package CFClient::Texture; |
15 | package CFPlus::Texture; |
16 | |
16 | |
17 | use strict; |
17 | use strict; |
18 | |
18 | |
19 | use Scalar::Util; |
19 | use List::Util qw(max min); |
20 | |
|
|
21 | use CFClient::OpenGL; |
20 | use CFPlus::OpenGL; |
22 | |
21 | |
23 | my %TEXTURES; |
22 | my %TEXTURES; |
|
|
23 | my ($MAX_W, $MAX_H) = (4096, 4096); # maximum texture size attempted by this module |
24 | |
24 | |
25 | sub new { |
25 | sub 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 | CFPlus::weaken ($TEXTURES{$self+0} = $self); |
36 | |
36 | |
37 | $self->upload; |
37 | $self->upload; |
38 | |
38 | |
39 | $self |
39 | $self |
40 | } |
40 | } |
41 | |
41 | |
42 | sub new_from_image { |
42 | sub new_from_image { |
43 | my ($class, $image, %arg) = @_; |
43 | my ($class, $image, %arg) = @_; |
44 | |
44 | |
|
|
45 | Carp::confess "tried to create texture from undefined image" |
|
|
46 | unless defined $image; |
|
|
47 | |
45 | $class->new (image => $image, %arg) |
48 | $class->new (image => $image, internalformat => undef, %arg) |
46 | } |
49 | } |
47 | |
50 | |
48 | sub new_from_file { |
51 | sub new_from_file { |
49 | my ($class, $path, %arg) = @_; |
52 | my ($class, $path, %arg) = @_; |
50 | |
53 | |
… | |
… | |
65 | # w => $surface->width, |
68 | # w => $surface->width, |
66 | # h => $surface->height, |
69 | # h => $surface->height, |
67 | # ) |
70 | # ) |
68 | #} |
71 | #} |
69 | |
72 | |
70 | sub new_from_layout { |
73 | #sub new_from_layout { |
71 | my ($class, $layout, %arg) = @_; |
74 | # my ($class, $layout, %arg) = @_; |
72 | |
75 | # |
73 | my ($w, $h, $data, $format, $internalformat) = $layout->render; |
76 | # my ($w, $h, $data, $format, $internalformat) = $layout->render; |
74 | |
77 | # |
75 | $class->new ( |
78 | # $class->new ( |
76 | w => $w, |
79 | # w => $w, |
77 | h => $h, |
80 | # h => $h, |
78 | data => $data, |
81 | # data => $data, |
79 | format => $format, |
82 | # format => $format, |
80 | internalformat => $format, |
83 | # internalformat => $format, |
81 | type => GL_UNSIGNED_BYTE, |
84 | # type => GL_UNSIGNED_BYTE, |
82 | %arg, |
85 | # %arg, |
83 | ) |
86 | # ) |
84 | } |
87 | #} |
85 | |
88 | |
86 | sub new_from_opengl { |
89 | sub new_from_opengl { |
87 | my ($class, $w, $h, $cb) = @_; |
90 | my ($class, $w, $h, $cb) = @_; |
88 | |
91 | |
89 | $class->new (w => $w || 1, h => $h || 1, render_cb => $cb) |
92 | $class->new (w => $w || 1, h => $h || 1, render_cb => $cb, nearest => 1) |
90 | } |
|
|
91 | |
|
|
92 | sub topot { |
|
|
93 | (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0] |
|
|
94 | } |
93 | } |
95 | |
94 | |
96 | sub upload { |
95 | sub upload { |
97 | my ($self) = @_; |
96 | my ($self) = @_; |
98 | |
97 | |
99 | return unless $GL_VERSION; |
98 | return unless $GL_VERSION; |
100 | |
99 | |
101 | my $data; |
100 | # $tw,$th texture |
|
|
101 | # $rw,$rh rendered/used size |
|
|
102 | # $dw,$dh $data |
|
|
103 | |
|
|
104 | my ($data, $dw, $dh); |
102 | |
105 | |
103 | if (exists $self->{data}) { |
106 | if (exists $self->{data}) { |
104 | $data = $self->{data}; |
107 | $data = $self->{data}; |
|
|
108 | ($dw, $dh) = @$self{qw(w h)}; |
105 | |
109 | |
106 | } elsif (exists $self->{render_cb}) { |
110 | } elsif (exists $self->{render_cb}) { |
107 | glViewport 0, 0, $self->{w}, $self->{h}; |
111 | ($dw, $dh) = @$self{qw(w h)}; |
|
|
112 | |
|
|
113 | } elsif (exists $self->{image}) { |
|
|
114 | ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type}) |
|
|
115 | = CFPlus::load_image_inline $self->{image}; |
|
|
116 | |
|
|
117 | $self->{internalformat} ||= $internalformat; |
|
|
118 | ($dw, $dh) = @$self{qw(w h)}; |
|
|
119 | } else { |
|
|
120 | Carp::confess "tried to create texture that is not data, render or image"; |
|
|
121 | } |
|
|
122 | |
|
|
123 | my ($tw, $th) = ($dw, $dh); |
|
|
124 | |
|
|
125 | defined $data or $self->{render_cb} or die; # some sanity check |
|
|
126 | |
|
|
127 | $self->{minified} ||= [CFPlus::average $dw, $dh, $data] |
|
|
128 | if $self->{minify}; |
|
|
129 | |
|
|
130 | # against rather broken cards we enforce a maximum texture size |
|
|
131 | $tw = min $MAX_W, minpot $tw; |
|
|
132 | $th = min $MAX_H, minpot $th; |
|
|
133 | |
|
|
134 | # if only pot-textures are allowed, pot'ify tw/th |
|
|
135 | unless ($GL_NPOT && 0) {#d# |
|
|
136 | $tw = minpot $tw; |
|
|
137 | $th = minpot $th; |
|
|
138 | } |
|
|
139 | |
|
|
140 | # now further decrease texture size until the |
|
|
141 | # card does accept it |
|
|
142 | while (!texture_valid_2d $self->{internalformat}, $tw, $th, $self->{format}, $self->{type}) { |
|
|
143 | # quarter the texture size |
|
|
144 | $tw >>= 1; |
|
|
145 | $th >>= 1; |
|
|
146 | } |
|
|
147 | |
|
|
148 | # decide the amount of space used in the texture |
|
|
149 | my ($rw, $rh); |
|
|
150 | my ($ox, $oy); # area shift to lessen effetc of buggy opengl implementations (nvida, ati) |
|
|
151 | my $render; |
|
|
152 | |
|
|
153 | if ($self->{render_cb}) { |
|
|
154 | # use only part of the texture |
|
|
155 | #$rw >>= 1 while $rw > $tw; |
|
|
156 | #$rh >>= 1 while $rh > $th; |
|
|
157 | $rw = min $dw, $tw; |
|
|
158 | $rh = min $dh, $th; |
|
|
159 | ++$render; |
|
|
160 | } else { |
|
|
161 | if ($self->{wrap} || $tw < $dw || $th < $dh) { |
|
|
162 | # scale to the full texture size |
|
|
163 | ($rw, $rh) = ($tw, $th); |
|
|
164 | ++$render; |
|
|
165 | } else { |
|
|
166 | # pad |
|
|
167 | pad $data, $dw, $dh, $tw, $th; |
|
|
168 | ($rw, $rh) = ($dw, $dh); |
|
|
169 | ($dw, $dh) = ($tw, $th); |
|
|
170 | } |
|
|
171 | } |
|
|
172 | |
|
|
173 | if ($render) { |
|
|
174 | $ox = int .5 * ($::WIDTH - $rw); |
|
|
175 | $oy = int .5 * ($::HEIGHT - $rh); |
|
|
176 | |
|
|
177 | glViewport $ox, $oy, $tw, $th; |
|
|
178 | #glScissor 0, 0, $tw, $th; |
|
|
179 | #glEnable GL_SCISSOR_TEST; |
108 | glMatrixMode GL_PROJECTION; |
180 | glMatrixMode GL_PROJECTION; |
109 | glLoadIdentity; |
181 | glLoadIdentity; |
110 | glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000; |
182 | glOrtho 0, $tw, 0, $th, -10000, 10000; |
111 | glMatrixMode GL_MODELVIEW; |
183 | glMatrixMode GL_MODELVIEW; |
112 | glLoadIdentity; |
184 | glLoadIdentity; |
|
|
185 | |
|
|
186 | if ($self->{render_cb}) { |
|
|
187 | glScale $rw / $dw, $rh / $dh; |
113 | $self->{render_cb}->($self, $self->{w}, $self->{h}); |
188 | $self->{render_cb}->($self, $rw, $rh); |
114 | |
|
|
115 | } else { |
189 | } else { |
116 | ($self->{w}, $self->{h}, $data, $self->{internalformat}, $self->{format}, $self->{type}) |
190 | glClearColor 0, 0, 0, 0; |
117 | = CFClient::load_image_inline $self->{image}; |
191 | glClear GL_COLOR_BUFFER_BIT; |
118 | } |
192 | glPixelZoom $tw / $dw, $th / $dh; |
119 | |
193 | glRasterPos 0, 0; |
120 | my ($tw, $th) = @$self{qw(w h)}; |
194 | glDrawPixels $dw, $dh, |
121 | |
195 | $self->{format}, |
122 | unless ($tw > 0 && $th > 0) { |
196 | $self->{type}, |
123 | $tw = $th = 1; |
197 | $data; |
124 | $data = "\x00" x 64; |
198 | glPixelZoom 1, 1; |
125 | } |
|
|
126 | |
|
|
127 | $self->{minified} = [CFClient::average $tw, $th, $data] |
|
|
128 | if $self->{minify}; |
|
|
129 | |
|
|
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 | } |
199 | } |
141 | } |
200 | } |
142 | |
201 | |
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}; |
202 | glBindTexture GL_TEXTURE_2D, $self->{name} ||= glGenTexture; |
149 | |
203 | |
|
|
204 | if ($self->{wrap}) { |
150 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
205 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
151 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
206 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
|
|
207 | } else { |
|
|
208 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, $GL_VERSION >= 1.2 ? GL_CLAMP_TO_EDGE : GL_CLAMP; |
|
|
209 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, $GL_VERSION >= 1.2 ? GL_CLAMP_TO_EDGE : GL_CLAMP; |
|
|
210 | } |
152 | |
211 | |
153 | if ($::FAST) { |
212 | if ($::FAST || $self->{nearest}) { |
154 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; |
213 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; |
155 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST; |
214 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST; |
156 | } elsif ($self->{mipmap} && $GL_VERSION >= 1.4) { |
215 | } elsif ($self->{mipmap} && $GL_VERSION >= 1.4) { |
157 | # alternatively check for 0x8191 |
|
|
158 | glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1; |
216 | glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1; |
159 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
217 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
160 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR; |
218 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR; |
161 | } else { |
219 | } else { |
162 | glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, $self->{mipmap}; |
220 | glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, $self->{mipmap}; |
… | |
… | |
164 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR; |
222 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR; |
165 | } |
223 | } |
166 | |
224 | |
167 | glGetError; |
225 | glGetError; |
168 | |
226 | |
169 | if (defined $data) { |
227 | if ($render) { |
|
|
228 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
|
|
229 | $self->{internalformat}, |
|
|
230 | $ox, $oy, |
|
|
231 | $tw, $th, |
|
|
232 | 0; |
|
|
233 | gl_check "copying to texture %dx%d if=%x", |
|
|
234 | $tw, $th, $self->{internalformat}; |
|
|
235 | |
|
|
236 | #glDisable GL_SCISSOR_TEST; |
|
|
237 | } else { |
170 | glTexImage2D GL_TEXTURE_2D, 0, |
238 | glTexImage2D GL_TEXTURE_2D, 0, |
171 | $self->{internalformat}, |
239 | $self->{internalformat}, |
172 | $tw, $th, |
240 | $dw, $dh, |
173 | 0, |
241 | 0, |
174 | $self->{format}, |
242 | $self->{format}, |
175 | $self->{type}, |
243 | $self->{type}, |
176 | $data; |
244 | $data; |
177 | gl_check "uploading texture %dx%d if=%x f=%x t=%x", |
245 | gl_check "uploading texture %dx%d if=%x f=%x t=%x", |
178 | $tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; |
246 | $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 | } |
247 | } |
188 | |
248 | |
189 | glBindTexture GL_TEXTURE_2D, 0; # just to be on the safe side |
249 | $self->{s} = $rw / $tw; |
|
|
250 | $self->{t} = $rh / $th; |
190 | } |
251 | } |
|
|
252 | |
|
|
253 | sub shutdown { |
|
|
254 | my ($self) = @_; |
|
|
255 | |
|
|
256 | glDeleteTexture $self->{name} |
|
|
257 | if $self->{name}; |
|
|
258 | } |
191 | |
259 | |
192 | sub DESTROY { |
260 | sub DESTROY { |
193 | my ($self) = @_; |
261 | my ($self) = @_; |
194 | |
262 | |
195 | delete $TEXTURES{$self+0}; |
263 | delete $TEXTURES{$self+0}; |
196 | |
264 | |
197 | glDeleteTexture delete $self->{name} |
265 | $self->shutdown; |
198 | if $self->{name}; |
|
|
199 | } |
266 | } |
200 | |
267 | |
201 | $CFClient::OpenGL::INIT_HOOK{"CFClient::Texture"} = sub { |
268 | $CFPlus::OpenGL::INIT_HOOK{"CFPlus::Texture"} = sub { |
|
|
269 | # first mark all existing texture names as in-use, in case the context lost textures |
|
|
270 | glBindTexture GL_TEXTURE_2D, $_->{name} |
|
|
271 | for values %TEXTURES; |
202 | $_->upload |
272 | $_->upload |
203 | for values %TEXTURES; |
273 | for values %TEXTURES; |
204 | }; |
274 | }; |
205 | |
275 | |
|
|
276 | $CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Texture"} = sub { |
|
|
277 | $_->shutdown |
|
|
278 | for values %TEXTURES; |
|
|
279 | }; |
|
|
280 | |
206 | 1; |
281 | 1; |
207 | |
282 | |
208 | =back |
283 | =back |
209 | |
284 | |
210 | =head1 AUTHOR |
285 | =head1 AUTHOR |