… | |
… | |
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 | |
|
|
45 | Carp::confess "tried to create texture from undefined image" |
|
|
46 | unless defined $image; |
44 | |
47 | |
45 | $class->new (image => $image, internalformat => undef, %arg) |
48 | $class->new (image => $image, internalformat => undef, %arg) |
46 | } |
49 | } |
47 | |
50 | |
48 | sub new_from_file { |
51 | sub new_from_file { |
… | |
… | |
105 | ($dw, $dh) = @$self{qw(w h)}; |
108 | ($dw, $dh) = @$self{qw(w h)}; |
106 | |
109 | |
107 | } elsif (exists $self->{render_cb}) { |
110 | } elsif (exists $self->{render_cb}) { |
108 | ($dw, $dh) = @$self{qw(w h)}; |
111 | ($dw, $dh) = @$self{qw(w h)}; |
109 | |
112 | |
110 | } else { |
113 | } elsif (exists $self->{image}) { |
111 | ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type}) |
114 | ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type}) |
112 | = CFPlus::load_image_inline $self->{image}; |
115 | = CFPlus::load_image_inline $self->{image}; |
113 | |
116 | |
114 | $self->{internalformat} ||= $internalformat; |
117 | $self->{internalformat} ||= $internalformat; |
115 | ($dw, $dh) = @$self{qw(w h)}; |
118 | ($dw, $dh) = @$self{qw(w h)}; |
|
|
119 | } else { |
|
|
120 | Carp::confess "tried to create texture that is not data, render or image"; |
116 | } |
121 | } |
117 | |
122 | |
118 | my ($tw, $th) = ($dw, $dh); |
123 | my ($tw, $th) = ($dw, $dh); |
119 | |
124 | |
120 | defined $data or $self->{render_cb} or die; # some sanity check |
125 | defined $data or $self->{render_cb} or die; # some sanity check |
… | |
… | |
140 | $th >>= 1; |
145 | $th >>= 1; |
141 | } |
146 | } |
142 | |
147 | |
143 | # decide the amount of space used in the texture |
148 | # decide the amount of space used in the texture |
144 | my ($rw, $rh); |
149 | my ($rw, $rh); |
|
|
150 | my ($ox, $oy); # area shift to lessen effetc of buggy opengl implementations (nvida, ati) |
145 | my $render; |
151 | my $render; |
146 | |
152 | |
147 | if ($self->{render_cb}) { |
153 | if ($self->{render_cb}) { |
148 | # use only part of the texture |
154 | # use only part of the texture |
149 | #$rw >>= 1 while $rw > $tw; |
155 | #$rw >>= 1 while $rw > $tw; |
… | |
… | |
163 | ($dw, $dh) = ($tw, $th); |
169 | ($dw, $dh) = ($tw, $th); |
164 | } |
170 | } |
165 | } |
171 | } |
166 | |
172 | |
167 | if ($render) { |
173 | if ($render) { |
|
|
174 | $ox = int .5 * ($::WIDTH - $rw); |
|
|
175 | $oy = int .5 * ($::HEIGHT - $rh); |
|
|
176 | |
168 | glViewport 0, 0, $tw, $th; |
177 | glViewport $ox, $oy, $tw, $th; |
169 | glClear 0, 0, 0, 0; |
178 | #glScissor 0, 0, $tw, $th; |
|
|
179 | #glEnable GL_SCISSOR_TEST; |
170 | glMatrixMode GL_PROJECTION; |
180 | glMatrixMode GL_PROJECTION; |
171 | glLoadIdentity; |
181 | glLoadIdentity; |
172 | glOrtho 0, $tw, 0, $th, -10000, 10000; |
182 | glOrtho 0, $tw, 0, $th, -10000, 10000; |
173 | glMatrixMode GL_MODELVIEW; |
183 | glMatrixMode GL_MODELVIEW; |
174 | glLoadIdentity; |
184 | glLoadIdentity; |
175 | |
185 | |
176 | if ($self->{render_cb}) { |
186 | if ($self->{render_cb}) { |
177 | glScale $rw / $dw, $rh / $dh; |
187 | glScale $rw / $dw, $rh / $dh; |
178 | $self->{render_cb}->($self, $rw, $rh); |
188 | $self->{render_cb}->($self, $rw, $rh); |
179 | } else { |
189 | } else { |
|
|
190 | glClearColor 0, 0, 0, 0; |
|
|
191 | glClear GL_COLOR_BUFFER_BIT; |
180 | glPixelZoom $tw / $dw, $th / $dh; |
192 | glPixelZoom $tw / $dw, $th / $dh; |
|
|
193 | glRasterPos 0, 0; |
181 | glDrawPixels $dw, $dh, |
194 | glDrawPixels $dw, $dh, |
182 | $self->{format}, |
195 | $self->{format}, |
183 | $self->{type}, |
196 | $self->{type}, |
184 | $data; |
197 | $data; |
185 | glPixelZoom 1, 1; |
198 | glPixelZoom 1, 1; |
… | |
… | |
212 | glGetError; |
225 | glGetError; |
213 | |
226 | |
214 | if ($render) { |
227 | if ($render) { |
215 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
228 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
216 | $self->{internalformat}, |
229 | $self->{internalformat}, |
217 | 0, 0, |
230 | $ox, $oy, |
218 | $tw, $th, |
231 | $tw, $th, |
219 | 0; |
232 | 0; |
220 | gl_check "copying to texture %dx%d if=%x", |
233 | gl_check "copying to texture %dx%d if=%x", |
221 | $tw, $th, $self->{internalformat}; |
234 | $tw, $th, $self->{internalformat}; |
|
|
235 | |
|
|
236 | #glDisable GL_SCISSOR_TEST; |
222 | } else { |
237 | } else { |
223 | glTexImage2D GL_TEXTURE_2D, 0, |
238 | glTexImage2D GL_TEXTURE_2D, 0, |
224 | $self->{internalformat}, |
239 | $self->{internalformat}, |
225 | $dw, $dh, |
240 | $dw, $dh, |
226 | 0, |
241 | 0, |