… | |
… | |
110 | if $cb; |
110 | if $cb; |
111 | |
111 | |
112 | return if $self->{loading}; |
112 | return if $self->{loading}; |
113 | |
113 | |
114 | unless ($GL_VERSION) { |
114 | unless ($GL_VERSION) { |
115 | $self->{was_loaded} = 1; |
115 | $self->{want_upload} = -1; |
116 | return; |
116 | return; |
117 | } |
117 | } |
118 | |
118 | |
119 | unless ($self->{name}) { |
119 | unless ($self->{name}) { |
120 | # $tw,$th texture |
120 | # $tw,$th texture |
121 | # $rw,$rh rendered/used size |
121 | # $rw,$rh rendered/used size |
122 | # $dw,$dh $data |
122 | # $dw,$dh $data |
123 | |
123 | |
124 | my ($data, $dw, $dh); |
124 | my ($data, $dw, $dh); |
125 | |
125 | |
126 | if (exists $self->{data}) { |
126 | if (defined $self->{data}) { |
127 | $data = $self->{data}; |
127 | $data = $self->{data}; |
128 | ($dw, $dh) = @$self{qw(w h)}; |
128 | ($dw, $dh) = @$self{qw(w h)}; |
129 | |
129 | |
130 | } elsif (exists $self->{render_cb}) { |
130 | } elsif ($self->{render_cb}) { |
131 | ($dw, $dh) = @$self{qw(w h)}; |
131 | ($dw, $dh) = @$self{qw(w h)}; |
132 | |
132 | |
133 | } elsif (exists $self->{image}) { |
133 | } elsif (defined $self->{image}) { |
134 | ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type}) |
134 | ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type}) |
135 | = CFPlus::load_image_inline $self->{image}; |
135 | = CFPlus::load_image_inline $self->{image}; |
136 | |
136 | |
137 | $self->{internalformat} ||= $internalformat; |
137 | $self->{internalformat} ||= $internalformat; |
138 | ($dw, $dh) = @$self{qw(w h)}; |
138 | ($dw, $dh) = @$self{qw(w h)}; |
139 | |
139 | |
140 | delete $self->{image} if delete $self->{delete_image}; |
140 | delete $self->{image} if delete $self->{delete_image}; |
141 | |
141 | |
142 | } elsif (exists $self->{tile}) { |
142 | } elsif (defined $self->{tile}) { |
143 | ++$self->{loading}; |
143 | ++$self->{loading}; |
144 | return CFPlus::DB::get tilecache => $self->{tile}, sub { |
144 | return CFPlus::DB::get tilecache => $self->{tile}, sub { |
145 | $self->loading_done ($_[0]); |
145 | $self->loading_done ($_[0]); |
146 | |
|
|
147 | $::MAP->set_texture ($self->{tile}, @$self{qw(name w h s t)}, @{$self->{minified}}) |
|
|
148 | if $::MAP; |
|
|
149 | $::MAPWIDGET->update |
|
|
150 | if $::MAPWIDGET; |
|
|
151 | }; |
146 | }; |
152 | |
147 | |
153 | } elsif (exists $self->{path}) { |
148 | } elsif (defined $self->{path}) { |
154 | ++$self->{loading}; |
149 | ++$self->{loading}; |
155 | return CFPlus::DB::read_file $self->{path}, sub { |
150 | return CFPlus::DB::read_file $self->{path}, sub { |
156 | $self->loading_done ($_[0]); |
151 | $self->loading_done ($_[0]); |
157 | }; |
152 | }; |
158 | |
153 | |
… | |
… | |
166 | |
161 | |
167 | $self->{minified} ||= [CFPlus::average $dw, $dh, $data] |
162 | $self->{minified} ||= [CFPlus::average $dw, $dh, $data] |
168 | if $self->{minify}; |
163 | if $self->{minify}; |
169 | |
164 | |
170 | # against rather broken cards we enforce a maximum texture size |
165 | # against rather broken cards we enforce a maximum texture size |
171 | $tw = min $MAX_W, minpot $tw; |
166 | $tw = min $MAX_W, $tw; |
172 | $th = min $MAX_H, minpot $th; |
167 | $th = min $MAX_H, $th; |
173 | |
168 | |
174 | # if only pot-textures are allowed, pot'ify tw/th |
169 | # if only pot-textures are allowed, pot'ify tw/th |
175 | unless ($GL_NPOT && 0) {#d# |
170 | unless ($GL_NPOT) { |
176 | $tw = minpot $tw; |
171 | $tw = CFPlus::minpot $tw; |
177 | $th = minpot $th; |
172 | $th = CFPlus::minpot $th; |
178 | } |
173 | } |
179 | |
174 | |
180 | # now further decrease texture size until the |
175 | # now further decrease texture size until the |
181 | # card does accept it |
176 | # card does accept it |
182 | 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}) { |
… | |
… | |
237 | $data; |
232 | $data; |
238 | glPixelZoom 1, 1; |
233 | glPixelZoom 1, 1; |
239 | } |
234 | } |
240 | } |
235 | } |
241 | |
236 | |
|
|
237 | if ((my $name = delete $self->{want_upload}) > 0) { |
|
|
238 | $self->{name} = $name; |
|
|
239 | } |
|
|
240 | |
242 | glBindTexture GL_TEXTURE_2D, $self->{name} ||= glGenTexture; |
241 | glBindTexture GL_TEXTURE_2D, $self->{name} ||= glGenTexture; |
243 | |
242 | |
244 | if ($self->{wrap}) { |
243 | if ($self->{wrap}) { |
245 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
244 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
246 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
245 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
… | |
… | |
296 | $tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; |
295 | $tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; |
297 | } |
296 | } |
298 | |
297 | |
299 | $self->{s} = $rw / $tw; |
298 | $self->{s} = $rw / $tw; |
300 | $self->{t} = $rh / $th; |
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; |
|
|
306 | } |
301 | } |
307 | } |
302 | |
308 | |
303 | $_->($self) |
309 | $_->($self) |
304 | for @{ (delete $self->{upload_done}) || [] }; |
310 | for @{ (delete $self->{upload_done}) || [] }; |
305 | } |
311 | } |
… | |
… | |
319 | $self->unload; |
325 | $self->unload; |
320 | } |
326 | } |
321 | |
327 | |
322 | $CFPlus::OpenGL::INIT_HOOK{"CFPlus::Texture"} = sub { |
328 | $CFPlus::OpenGL::INIT_HOOK{"CFPlus::Texture"} = sub { |
323 | for (values %TEXTURES) { |
329 | for (values %TEXTURES) { |
324 | next unless delete $_->{was_loaded}; |
330 | $_->upload if $_->{want_upload}; |
325 | $_->upload; |
|
|
326 | } |
331 | } |
327 | }; |
332 | }; |
328 | |
333 | |
329 | $CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Texture"} = sub { |
334 | $CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Texture"} = sub { |
330 | for (values %TEXTURES) { |
335 | for (values %TEXTURES) { |
331 | next unless $_->{name}; |
336 | next unless $_->{name}; |
332 | $_->{was_loaded} = 1; |
337 | $_->{want_upload} = $_->{name}; |
333 | $_->unload; |
338 | $_->unload; |
334 | } |
339 | } |
335 | }; |
340 | }; |
336 | |
341 | |
337 | 1; |
342 | 1; |