1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | CFPlus::Texture - tetxure class for CFPlus |
3 | DC::Texture - tetxure class for Deliantra-Client |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use CFPlus::Texture; |
7 | use DC::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 CFPlus::Texture; |
15 | package DC::Texture; |
16 | |
16 | |
17 | use strict; |
17 | use strict; |
18 | |
18 | |
19 | use List::Util qw(max min); |
19 | use List::Util qw(max min); |
20 | use CFPlus::OpenGL; |
20 | use DC::OpenGL; |
21 | |
21 | |
22 | my %TEXTURES; |
22 | my %TEXTURES; |
23 | my ($MAX_W, $MAX_H) = (4096, 4096); # maximum texture size attempted by this module |
23 | my ($MAX_W, $MAX_H) = (4096, 4096); # maximum texture size attempted by this module |
24 | |
24 | |
25 | sub new { |
25 | sub new { |
… | |
… | |
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 | CFPlus::weaken ($TEXTURES{$self+0} = $self); |
35 | DC::weaken ($TEXTURES{$self+0} = $self); |
36 | |
36 | |
37 | $self->upload; |
37 | $self->upload |
|
|
38 | unless delete $self->{delay}; |
38 | |
39 | |
39 | $self |
40 | $self |
40 | } |
41 | } |
41 | |
42 | |
42 | sub new_from_image { |
43 | sub new_from_image { |
43 | my ($class, $image, %arg) = @_; |
44 | my ($class, $image, %arg) = @_; |
44 | |
45 | |
|
|
46 | Carp::confess "tried to create texture from undefined image" |
|
|
47 | unless defined $image; |
|
|
48 | |
45 | $class->new (image => $image, internalformat => undef, %arg) |
49 | $class->new (image => $image, internalformat => undef, %arg) |
46 | } |
50 | } |
|
|
51 | |
47 | |
52 | |
48 | sub new_from_file { |
53 | sub new_from_file { |
49 | my ($class, $path, %arg) = @_; |
54 | my ($class, $path, %arg) = @_; |
50 | |
55 | |
51 | open my $fh, "<:raw", $path |
56 | open my $fh, "<:raw", $path |
… | |
… | |
87 | my ($class, $w, $h, $cb) = @_; |
92 | my ($class, $w, $h, $cb) = @_; |
88 | |
93 | |
89 | $class->new (w => $w || 1, h => $h || 1, render_cb => $cb, nearest => 1) |
94 | $class->new (w => $w || 1, h => $h || 1, render_cb => $cb, nearest => 1) |
90 | } |
95 | } |
91 | |
96 | |
|
|
97 | 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 | |
92 | sub upload { |
106 | sub upload { |
93 | my ($self) = @_; |
107 | my ($self, $cb) = @_; |
94 | |
108 | |
|
|
109 | push @{ $self->{upload_done} }, $cb |
|
|
110 | if $cb; |
|
|
111 | |
|
|
112 | return if $self->{loading}; |
|
|
113 | |
95 | return unless $GL_VERSION; |
114 | unless ($GL_VERSION) { |
96 | |
115 | $self->{want_upload} = -1; |
97 | # $tw,$th texture |
116 | return; |
98 | # $rw,$rh rendered/used size |
|
|
99 | # $dw,$dh $data |
|
|
100 | |
|
|
101 | my ($data, $dw, $dh); |
|
|
102 | |
|
|
103 | if (exists $self->{data}) { |
|
|
104 | $data = $self->{data}; |
|
|
105 | ($dw, $dh) = @$self{qw(w h)}; |
|
|
106 | |
|
|
107 | } elsif (exists $self->{render_cb}) { |
|
|
108 | ($dw, $dh) = @$self{qw(w h)}; |
|
|
109 | |
|
|
110 | } else { |
|
|
111 | ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type}) |
|
|
112 | = CFPlus::load_image_inline $self->{image}; |
|
|
113 | |
|
|
114 | $self->{internalformat} ||= $internalformat; |
|
|
115 | ($dw, $dh) = @$self{qw(w h)}; |
|
|
116 | } |
117 | } |
117 | |
118 | |
|
|
119 | 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 | if (defined $self->{data}) { |
|
|
127 | $data = $self->{data}; |
|
|
128 | ($dw, $dh) = @$self{qw(w h)}; |
|
|
129 | |
|
|
130 | } elsif ($self->{render_cb}) { |
|
|
131 | ($dw, $dh) = @$self{qw(w h)}; |
|
|
132 | |
|
|
133 | } elsif (defined $self->{image}) { |
|
|
134 | ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type}) |
|
|
135 | = DC::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 | } elsif (defined $self->{tile}) { |
|
|
143 | ++$self->{loading}; |
|
|
144 | return DC::DB::get tilecache => $self->{tile}, sub { |
|
|
145 | $self->loading_done ($_[0]); |
|
|
146 | }; |
|
|
147 | |
|
|
148 | } elsif (defined $self->{path}) { |
|
|
149 | ++$self->{loading}; |
|
|
150 | return DC::DB::read_file $self->{path}, sub { |
|
|
151 | $self->loading_done ($_[0]); |
|
|
152 | }; |
|
|
153 | |
|
|
154 | } else { |
|
|
155 | Carp::confess "tried to create texture that is not data, render or image"; |
|
|
156 | } |
|
|
157 | |
118 | my ($tw, $th) = ($dw, $dh); |
158 | my ($tw, $th) = ($dw, $dh); |
119 | my ($rw, $rh) = ($dw, $dh); |
|
|
120 | |
159 | |
121 | defined $data or $self->{render_cb} or die; # some sanity check |
160 | defined $data or $self->{render_cb} or die; # some sanity check |
122 | |
161 | |
123 | $self->{minified} ||= [CFPlus::average $dw, $dh, $data] |
162 | $self->{minified} ||= [DC::average $dw, $dh, $data] |
124 | if $self->{minify}; |
163 | if $self->{minify}; |
125 | |
164 | |
126 | # against rather broken cards we enforce a maximum texture size |
165 | # against rather broken cards we enforce a maximum texture size |
127 | $tw = min $MAX_W, minpot $tw; |
166 | $tw = min $MAX_W, $tw; |
128 | $th = min $MAX_H, minpot $th; |
167 | $th = min $MAX_H, $th; |
129 | |
168 | |
130 | # if only pot-textures are allowed, pot'ify tw/th |
169 | # if only pot-textures are allowed, pot'ify tw/th |
131 | unless ($GL_NPOT && 0) {#d# |
170 | unless ($GL_NPOT) { |
132 | $tw = minpot $tw; |
171 | $tw = DC::minpot $tw; |
133 | $th = minpot $th; |
172 | $th = DC::minpot $th; |
134 | } |
173 | } |
135 | |
174 | |
136 | # now further decrease texture size until the |
175 | # now further decrease texture size until the |
137 | # card does accept it |
176 | # card does accept it |
138 | while (!texture_valid_2d $self->{internalformat}, $tw, $th, $self->{format}, $self->{type}) { |
177 | while (!texture_valid_2d $self->{internalformat}, $tw, $th, $self->{format}, $self->{type}) { |
139 | # quarter the texture size |
178 | # quarter the texture size |
140 | $tw >>= 1; |
179 | $tw >>= 1; |
141 | $th >>= 1; |
180 | $th >>= 1; |
142 | } |
|
|
143 | |
|
|
144 | if ($self->{render_cb}) { |
|
|
145 | # use only part of the texture |
|
|
146 | #$rw >>= 1 while $rw > $tw; |
|
|
147 | #$rh >>= 1 while $rh > $th; |
|
|
148 | $rw = min $rw, $tw; |
|
|
149 | $rh = min $rh, $th; |
|
|
150 | } else { |
|
|
151 | if ($self->{wrap} || $tw < $dw || $th < $dh) { |
|
|
152 | # scale to the full texture size |
|
|
153 | ($rw, $rh) = ($tw, $th); |
|
|
154 | } else { |
|
|
155 | # pad |
|
|
156 | pad $data, $dw, $dh, $tw, $th; |
|
|
157 | ($rw, $rh) = ($dw, $dh); |
|
|
158 | ($dw, $dh) = ($tw, $th); |
|
|
159 | } |
181 | } |
160 | } |
|
|
161 | |
182 | |
162 | # rendering means we need to scale in some way |
183 | # decide the amount of space used in the texture |
163 | my $render = $self->{render_cb} || $rw != $dw || $th != $dh; |
184 | my ($rw, $rh); |
164 | |
185 | my ($ox, $oy); # area shift to lessen effetc of buggy opengl implementations (nvida, ati) |
165 | if ($render) { |
186 | my $render; |
166 | glViewport 0, 0, $tw, $th; |
|
|
167 | glClear 0, 0, 0, 0; |
|
|
168 | glMatrixMode GL_PROJECTION; |
|
|
169 | glLoadIdentity; |
|
|
170 | glOrtho 0, $tw, 0, $th, -10000, 10000; |
|
|
171 | glMatrixMode GL_MODELVIEW; |
|
|
172 | glLoadIdentity; |
|
|
173 | |
187 | |
174 | if ($self->{render_cb}) { |
188 | 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 | |
|
|
208 | 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}) { |
175 | glScale $rw / $dw, $rh / $dh; |
222 | glScale $rw / $dw, $rh / $dh; |
176 | $self->{render_cb}->($self, $rw, $rh); |
223 | $self->{render_cb}->($self, $rw, $rh); |
177 | } else { |
224 | } else { |
|
|
225 | glClearColor 0, 0, 0, 0; |
|
|
226 | glClear GL_COLOR_BUFFER_BIT; |
178 | glPixelZoom $tw / $dw, $th / $dh; |
227 | glPixelZoom $tw / $dw, $th / $dh; |
|
|
228 | glRasterPos 0, 0; |
179 | glDrawPixels $dw, $dh, |
229 | glDrawPixels $dw, $dh, |
|
|
230 | $self->{format}, |
|
|
231 | $self->{type}, |
|
|
232 | $data; |
|
|
233 | glPixelZoom 1, 1; |
|
|
234 | } |
|
|
235 | } |
|
|
236 | |
|
|
237 | if ((my $name = delete $self->{want_upload}) > 0) { |
|
|
238 | $self->{name} = $name; |
|
|
239 | } |
|
|
240 | |
|
|
241 | glBindTexture GL_TEXTURE_2D, $self->{name} ||= glGenTexture; |
|
|
242 | |
|
|
243 | if ($self->{wrap}) { |
|
|
244 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
|
|
245 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
|
|
246 | } else { |
|
|
247 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, $GL_VERSION >= 1.2 ? GL_CLAMP_TO_EDGE : GL_CLAMP; |
|
|
248 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, $GL_VERSION >= 1.2 ? GL_CLAMP_TO_EDGE : GL_CLAMP; |
|
|
249 | } |
|
|
250 | |
|
|
251 | if ($::FAST || $self->{nearest}) { |
|
|
252 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; |
|
|
253 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST; |
|
|
254 | } elsif ($self->{mipmap} && $GL_VERSION >= 1.4) { |
|
|
255 | glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1; |
|
|
256 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
|
|
257 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR; |
|
|
258 | } else { |
|
|
259 | glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, $self->{mipmap}; |
|
|
260 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
|
|
261 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR; |
|
|
262 | } |
|
|
263 | |
|
|
264 | glGetError; |
|
|
265 | |
|
|
266 | if ($render) { |
|
|
267 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
|
|
268 | $self->{internalformat}, |
|
|
269 | $ox, $oy, |
|
|
270 | $tw, $th, |
|
|
271 | 0; |
|
|
272 | gl_check "copying to texture %dx%d if=%x", |
|
|
273 | $tw, $th, $self->{internalformat}; |
|
|
274 | |
|
|
275 | #glDisable GL_SCISSOR_TEST; |
|
|
276 | } else { |
|
|
277 | my $if = $self->{internalformat}; |
|
|
278 | |
|
|
279 | if ($GL_COMPRESS && $::CFG->{texture_compression}) { |
|
|
280 | if ($if == GL_RGB) { |
|
|
281 | $if = GL_COMPRESSED_RGB_ARB; |
|
|
282 | } elsif ($if == GL_RGBA) { |
|
|
283 | $if = GL_COMPRESSED_RGBA_ARB; |
|
|
284 | } |
|
|
285 | } |
|
|
286 | |
|
|
287 | glTexImage2D GL_TEXTURE_2D, 0, |
|
|
288 | $if, |
|
|
289 | $dw, $dh, |
|
|
290 | 0, |
180 | $self->{format}, |
291 | $self->{format}, |
181 | $self->{type}, |
292 | $self->{type}, |
182 | $data; |
293 | $data; |
183 | glPixelZoom 1, 1; |
294 | gl_check "uploading texture %dx%d if=%x f=%x t=%x", |
|
|
295 | $tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; |
|
|
296 | } |
|
|
297 | |
|
|
298 | $self->{s} = $rw / $tw; |
|
|
299 | $self->{t} = $rh / $th; |
|
|
300 | |
|
|
301 | if ($self->{tile}) { |
|
|
302 | $::MAP->set_texture ($self->{tile}, @$self{qw(name w h s t)}, @{$self->{minified}}) |
|
|
303 | if $::MAP; |
|
|
304 | $::MAPWIDGET->update |
|
|
305 | if $::MAPWIDGET; |
184 | } |
306 | } |
185 | } |
307 | } |
186 | |
308 | |
187 | glBindTexture GL_TEXTURE_2D, $self->{name} ||= glGenTexture; |
309 | $_->($self) |
188 | |
310 | for @{ (delete $self->{upload_done}) || [] }; |
189 | if ($self->{wrap}) { |
|
|
190 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
|
|
191 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
|
|
192 | } else { |
|
|
193 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, $GL_VERSION >= 1.2 ? GL_CLAMP_TO_EDGE : GL_CLAMP; |
|
|
194 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, $GL_VERSION >= 1.2 ? GL_CLAMP_TO_EDGE : GL_CLAMP; |
|
|
195 | } |
|
|
196 | |
|
|
197 | if ($::FAST || $self->{nearest}) { |
|
|
198 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; |
|
|
199 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST; |
|
|
200 | } elsif ($self->{mipmap} && $GL_VERSION >= 1.4) { |
|
|
201 | glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1; |
|
|
202 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
|
|
203 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR; |
|
|
204 | } else { |
|
|
205 | glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, $self->{mipmap}; |
|
|
206 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
|
|
207 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR; |
|
|
208 | } |
|
|
209 | |
|
|
210 | glGetError; |
|
|
211 | |
|
|
212 | if ($render) { |
|
|
213 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
|
|
214 | $self->{internalformat}, |
|
|
215 | 0, 0, |
|
|
216 | $tw, $th, |
|
|
217 | 0; |
|
|
218 | gl_check "copying to texture %dx%d if=%x", |
|
|
219 | $tw, $th, $self->{internalformat}; |
|
|
220 | } else { |
|
|
221 | glTexImage2D GL_TEXTURE_2D, 0, |
|
|
222 | $self->{internalformat}, |
|
|
223 | $dw, $dh, |
|
|
224 | 0, |
|
|
225 | $self->{format}, |
|
|
226 | $self->{type}, |
|
|
227 | $data; |
|
|
228 | gl_check "uploading texture %dx%d if=%x f=%x t=%x", |
|
|
229 | $tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; |
|
|
230 | } |
|
|
231 | |
|
|
232 | $self->{s} = $rw / $tw; |
|
|
233 | $self->{t} = $rh / $th; |
|
|
234 | } |
311 | } |
235 | |
312 | |
236 | sub shutdown { |
313 | sub unload { |
237 | my ($self) = @_; |
314 | my ($self) = @_; |
238 | |
315 | |
239 | glDeleteTexture $self->{name} |
316 | glDeleteTexture delete $self->{name} |
240 | if $self->{name}; |
317 | if $self->{name}; |
241 | } |
318 | } |
242 | |
319 | |
243 | sub DESTROY { |
320 | sub DESTROY { |
244 | my ($self) = @_; |
321 | my ($self) = @_; |
245 | |
322 | |
246 | delete $TEXTURES{$self+0}; |
323 | delete $TEXTURES{$self+0}; |
247 | |
324 | |
248 | $self->shutdown; |
325 | $self->unload; |
249 | } |
326 | } |
250 | |
327 | |
251 | $CFPlus::OpenGL::INIT_HOOK{"CFPlus::Texture"} = sub { |
328 | $DC::OpenGL::INIT_HOOK{"DC::Texture"} = sub { |
252 | # first mark all existing texture names as in-use, in case the context lost textures |
329 | for my $tex (values %TEXTURES) { |
|
|
330 | if (my $name = $tex->{want_upload}) { |
|
|
331 | $tex->upload; |
|
|
332 | |
|
|
333 | if ($tex->{loading} && $name > 0) { |
|
|
334 | # if loading is delayed we still have to allocate the texture name |
253 | glBindTexture GL_TEXTURE_2D, $_->{name} |
335 | glBindTexture GL_TEXTURE_2D, $name; |
254 | for values %TEXTURES; |
336 | glTexImage2D GL_TEXTURE_2D, 0, GL_ALPHA, 0, 0, 0, GL_ALPHA, GL_UNSIGNED_BYTE; |
255 | $_->upload |
337 | } |
256 | for values %TEXTURES; |
338 | } |
|
|
339 | } |
257 | }; |
340 | }; |
258 | |
341 | |
259 | $CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Texture"} = sub { |
342 | $DC::OpenGL::SHUTDOWN_HOOK{"DC::Texture"} = sub { |
260 | $_->shutdown |
|
|
261 | for values %TEXTURES; |
343 | for (values %TEXTURES) { |
|
|
344 | next unless $_->{name}; |
|
|
345 | $_->{want_upload} = $_->{name}; |
|
|
346 | $_->unload; |
|
|
347 | } |
262 | }; |
348 | }; |
263 | |
349 | |
264 | 1; |
350 | 1; |
265 | |
351 | |
266 | =back |
352 | =back |