… | |
… | |
31 | sub gl_init { |
31 | sub gl_init { |
32 | $GL_VERSION = gl_version * 1; |
32 | $GL_VERSION = gl_version * 1; |
33 | %GL_EXT = map +($_ => 1), split /\s+/, gl_extensions; |
33 | %GL_EXT = map +($_ => 1), split /\s+/, gl_extensions; |
34 | |
34 | |
35 | $GL_NPOT = $GL_EXT{GL_ARB_texture_non_power_of_two} || $GL_VERSION >= 2; |
35 | $GL_NPOT = $GL_EXT{GL_ARB_texture_non_power_of_two} || $GL_VERSION >= 2; |
36 | |
|
|
37 | glClearColor 0.45, 0.45, 0.45, 1; |
|
|
38 | |
36 | |
39 | glEnable GL_TEXTURE_2D; |
37 | glEnable GL_TEXTURE_2D; |
40 | glEnable GL_COLOR_MATERIAL; |
38 | glEnable GL_COLOR_MATERIAL; |
41 | glShadeModel GL_FLAT; |
39 | glShadeModel GL_FLAT; |
42 | glDisable GL_DEPTH_TEST; |
40 | glDisable GL_DEPTH_TEST; |
… | |
… | |
104 | my ($class, %data) = @_; |
102 | my ($class, %data) = @_; |
105 | |
103 | |
106 | my $self = bless { |
104 | my $self = bless { |
107 | internalformat => GL_RGBA, |
105 | internalformat => GL_RGBA, |
108 | format => GL_RGBA, |
106 | format => GL_RGBA, |
|
|
107 | type => GL_UNSIGNED_BYTE, |
109 | %data, |
108 | %data, |
110 | }, $class; |
109 | }, $class; |
111 | |
110 | |
112 | push @textures, $self; |
111 | push @textures, $self; |
113 | Scalar::Util::weaken $textures[-1]; |
112 | Scalar::Util::weaken $textures[-1]; |
… | |
… | |
154 | w => $w, |
153 | w => $w, |
155 | h => $h, |
154 | h => $h, |
156 | data => $data, |
155 | data => $data, |
157 | internalformat => GL_ALPHA4, |
156 | internalformat => GL_ALPHA4, |
158 | format => GL_ALPHA, |
157 | format => GL_ALPHA, |
|
|
158 | type => GL_UNSIGNED_BYTE, |
159 | ) |
159 | ) |
160 | } |
160 | } |
161 | |
161 | |
162 | sub new_from_opengl { |
162 | sub new_from_opengl { |
163 | my ($class, $w, $h, $cb) = @_; |
163 | my ($class, $w, $h, $cb) = @_; |
… | |
… | |
179 | if (exists $self->{data}) { |
179 | if (exists $self->{data}) { |
180 | $data = $self->{data}; |
180 | $data = $self->{data}; |
181 | |
181 | |
182 | } elsif (exists $self->{render_cb}) { |
182 | } elsif (exists $self->{render_cb}) { |
183 | glViewport 0, 0, $self->{w}, $self->{h}; |
183 | glViewport 0, 0, $self->{w}, $self->{h}; |
184 | glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000; |
|
|
185 | glMatrixMode GL_PROJECTION; |
184 | glMatrixMode GL_PROJECTION; |
186 | glLoadIdentity; |
185 | glLoadIdentity; |
|
|
186 | glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000; |
187 | glMatrixMode GL_MODELVIEW; |
187 | glMatrixMode GL_MODELVIEW; |
188 | glLoadIdentity; |
188 | glLoadIdentity; |
189 | glClear GL_COLOR_BUFFER_BIT; |
|
|
190 | $self->{render_cb}->($self, $self->{w}, $self->{h}); |
189 | $self->{render_cb}->($self, $self->{w}, $self->{h}); |
191 | |
190 | |
192 | } else { |
191 | } else { |
193 | my $pb = new Gtk2::Gdk::PixbufLoader; |
192 | ($self->{w}, $self->{h}, $data, $self->{internalformat}, $self->{format}, $self->{type}) |
194 | $pb->write ($self->{image}); |
193 | = CFClient::load_image_inline $self->{image}; |
195 | $pb->close; |
|
|
196 | |
|
|
197 | $pb = $pb->get_pixbuf; |
|
|
198 | $pb = $pb->add_alpha (0, 0, 0, 0); |
|
|
199 | |
|
|
200 | $self->{w} = $pb->get_width; |
|
|
201 | $self->{h} = $pb->get_height; |
|
|
202 | |
|
|
203 | $data = $pb->get_pixels; |
|
|
204 | } |
194 | } |
205 | |
195 | |
206 | my ($tw, $th) = @$self{qw(w h)}; |
196 | my ($tw, $th) = @$self{qw(w h)}; |
207 | |
197 | |
208 | unless ($tw && $th) { |
198 | unless ($tw && $th) { |
209 | $tw = $th = 1; |
199 | $tw = $th = 1; |
210 | $data = "\x00" x 64; |
200 | $data = "\x00" x 64; |
211 | } |
201 | } |
|
|
202 | |
|
|
203 | $self->{minified} = [CFClient::average $tw, $th, $data] |
|
|
204 | if defined $data; |
212 | |
205 | |
213 | unless ($GL_NPOT) { |
206 | unless ($GL_NPOT) { |
214 | # TODO: does not work for zero-sized textures |
207 | # TODO: does not work for zero-sized textures |
215 | $tw = topot $tw; |
208 | $tw = topot $tw; |
216 | $th = topot $th; |
209 | $th = topot $th; |
… | |
… | |
239 | glTexImage2D GL_TEXTURE_2D, 0, |
232 | glTexImage2D GL_TEXTURE_2D, 0, |
240 | $self->{internalformat}, |
233 | $self->{internalformat}, |
241 | $tw, $th, # need to pad texture first |
234 | $tw, $th, # need to pad texture first |
242 | 0, |
235 | 0, |
243 | $self->{format}, |
236 | $self->{format}, |
244 | GL_UNSIGNED_BYTE, |
237 | $self->{type}, |
245 | $data; |
238 | $data; |
246 | glGetError and die; |
239 | glGetError and die; |
247 | } else { |
240 | } else { |
248 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
241 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
249 | $self->{internalformat}, |
242 | $self->{internalformat}, |
250 | 0, 0, |
243 | 0, 0, |
251 | $tw, $th, |
244 | $tw, $th, |
252 | 0; |
245 | 0; |
253 | glGetError and die; |
246 | glGetError and die "glCopyTexImage2D $tw,$th"; |
254 | } |
247 | } |
255 | } |
248 | } |
256 | |
249 | |
257 | sub DESTROY { |
250 | sub DESTROY { |
258 | my ($self) = @_; |
251 | my ($self) = @_; |