… | |
… | |
124 | # |
124 | # |
125 | # $surface->rgba; |
125 | # $surface->rgba; |
126 | # |
126 | # |
127 | # $class->new ( |
127 | # $class->new ( |
128 | # data => $surface->pixels, |
128 | # data => $surface->pixels, |
129 | # width => $surface->width, |
129 | # w => $surface->width, |
130 | # height => $surface->height, |
130 | # h => $surface->height, |
131 | # ) |
131 | # ) |
132 | #} |
132 | #} |
133 | |
133 | |
134 | sub new_from_layout { |
134 | sub new_from_layout { |
135 | my ($class, $layout) = @_; |
135 | my ($class, $layout) = @_; |
136 | |
136 | |
137 | my ($w, $h, $data) = $layout->render; |
137 | my ($w, $h, $data) = $layout->render; |
138 | |
138 | |
139 | $class->new ( |
139 | $class->new ( |
140 | width => $w, |
140 | w => $w, |
141 | height => $h, |
141 | h => $h, |
142 | data => $data, |
142 | data => $data, |
143 | internalformat => GL_ALPHA4, |
143 | internalformat => GL_ALPHA4, |
144 | format => GL_ALPHA, |
144 | format => GL_ALPHA, |
145 | ) |
145 | ) |
146 | } |
146 | } |
147 | |
147 | |
148 | sub new_from_opengl { |
148 | sub new_from_opengl { |
149 | my ($class, $w, $h, $cb) = @_; |
149 | my ($class, $w, $h, $cb) = @_; |
150 | |
150 | |
151 | $class->new (width => $w, height => $h, rendercb => $cb) |
151 | $class->new (w => $w, h => $h, render_cb => $cb) |
152 | } |
152 | } |
153 | |
153 | |
154 | sub topot { |
154 | sub topot { |
155 | (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0] |
155 | (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0] |
156 | } |
156 | } |
… | |
… | |
162 | |
162 | |
163 | my $data; |
163 | my $data; |
164 | |
164 | |
165 | if (exists $self->{data}) { |
165 | if (exists $self->{data}) { |
166 | $data = $self->{data}; |
166 | $data = $self->{data}; |
167 | } elsif (exists $self->{rendercb}) { |
167 | } elsif (exists $self->{render_cb}) { |
168 | glViewport 0, 0, $self->{width}, $self->{height}; |
168 | glViewport 0, 0, $self->{w}, $self->{h}; |
169 | glMatrixMode GL_PROJECTION; |
169 | glMatrixMode GL_PROJECTION; |
170 | glLoadIdentity; |
170 | glLoadIdentity; |
171 | glOrtho 0, $self->{width}, 0, $self->{height}, -10000, 10000; |
171 | glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000; |
172 | glMatrixMode GL_MODELVIEW; |
172 | glMatrixMode GL_MODELVIEW; |
173 | glPushmatrix; |
173 | glPushmatrix; |
174 | glLoadIdentity; |
174 | glLoadIdentity; |
175 | glClear GL_COLOR_BUFFER_BIT; |
175 | glClear GL_COLOR_BUFFER_BIT; |
176 | |
176 | |
177 | $self->{rendercb}->($self, $self->{width}, $self->{height}); |
177 | $self->{render_cb}->($self, $self->{w}, $self->{h}); |
178 | } else { |
178 | } else { |
179 | my $pb = new Gtk2::Gdk::PixbufLoader; |
179 | my $pb = new Gtk2::Gdk::PixbufLoader; |
180 | $pb->write ($self->{image}); |
180 | $pb->write ($self->{image}); |
181 | $pb->close; |
181 | $pb->close; |
182 | |
182 | |
183 | $pb = $pb->get_pixbuf; |
183 | $pb = $pb->get_pixbuf; |
184 | $pb = $pb->add_alpha (0, 0, 0, 0); |
184 | $pb = $pb->add_alpha (0, 0, 0, 0); |
185 | |
185 | |
186 | $self->{width} = $pb->get_width; |
186 | $self->{w} = $pb->get_width; |
187 | $self->{height} = $pb->get_height; |
187 | $self->{h} = $pb->get_height; |
188 | |
188 | |
189 | $data = $pb->get_pixels; |
189 | $data = $pb->get_pixels; |
190 | } |
190 | } |
191 | |
191 | |
192 | my ($tw, $th) = @$self{qw(width height)}; |
192 | my ($tw, $th) = @$self{qw(w h)}; |
193 | |
193 | |
|
|
194 | unless ($tw || $th) { |
|
|
195 | $tw = $th = 1; |
|
|
196 | $data = "\x00" x 64; |
|
|
197 | } |
|
|
198 | |
194 | unless ($GL_NPOT && $tw && $th) { |
199 | unless ($GL_NPOT) { |
|
|
200 | # TODO: does not work for zero-sized textures |
195 | $tw = topot $tw; |
201 | $tw = topot $tw; |
196 | $th = topot $th; |
202 | $th = topot $th; |
197 | |
203 | |
198 | if (defined $data) { |
204 | if (defined $data) { |
199 | my $bpp = (length $data) / ($self->{width} * $self->{height}); |
205 | my $bpp = (length $data) / ($self->{w} * $self->{h}); |
200 | $data = pack "(a" . ($tw * $bpp) . ")*", |
206 | $data = pack "(a" . ($tw * $bpp) . ")*", |
201 | unpack "(a" . ($self->{width} * $bpp) . ")*", $data; |
207 | unpack "(a" . ($self->{w} * $bpp) . ")*", $data; |
202 | $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{height}); |
208 | $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h}); |
203 | } |
209 | } |
204 | } |
210 | } |
205 | |
211 | |
206 | $self->{s} = $self->{width} / $tw; |
212 | $self->{s} = $self->{w} / $tw; |
207 | $self->{t} = $self->{height} / $th; |
213 | $self->{t} = $self->{h} / $th; |
208 | |
214 | |
209 | $self->{name} ||= (glGenTextures 1)->[0]; |
215 | $self->{name} ||= (glGenTextures 1)->[0]; |
210 | |
216 | |
211 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
217 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
212 | |
218 | |