… | |
… | |
140 | $th >>= 1; |
140 | $th >>= 1; |
141 | } |
141 | } |
142 | |
142 | |
143 | # decide the amount of space used in the texture |
143 | # decide the amount of space used in the texture |
144 | my ($rw, $rh); |
144 | my ($rw, $rh); |
|
|
145 | my ($ox, $oy); # area shift to lessen effetc of buggy opengl implementations (nvida, ati) |
|
|
146 | my $render; |
145 | |
147 | |
146 | if ($self->{render_cb}) { |
148 | if ($self->{render_cb}) { |
147 | # use only part of the texture |
149 | # use only part of the texture |
148 | #$rw >>= 1 while $rw > $tw; |
150 | #$rw >>= 1 while $rw > $tw; |
149 | #$rh >>= 1 while $rh > $th; |
151 | #$rh >>= 1 while $rh > $th; |
150 | $rw = min $dw, $tw; |
152 | $rw = min $dw, $tw; |
151 | $rh = min $dh, $th; |
153 | $rh = min $dh, $th; |
|
|
154 | ++$render; |
152 | } else { |
155 | } else { |
153 | if ($self->{wrap} || $tw < $dw || $th < $dh) { |
156 | if ($self->{wrap} || $tw < $dw || $th < $dh) { |
154 | # scale to the full texture size |
157 | # scale to the full texture size |
155 | ($rw, $rh) = ($tw, $th); |
158 | ($rw, $rh) = ($tw, $th); |
|
|
159 | ++$render; |
156 | } else { |
160 | } else { |
157 | # pad |
161 | # pad |
158 | pad $data, $dw, $dh, $tw, $th; |
162 | pad $data, $dw, $dh, $tw, $th; |
159 | ($rw, $rh) = ($dw, $dh); |
163 | ($rw, $rh) = ($dw, $dh); |
160 | ($dw, $dh) = ($tw, $th); |
164 | ($dw, $dh) = ($tw, $th); |
161 | } |
165 | } |
162 | } |
166 | } |
163 | |
167 | |
164 | # rendering means we need to scale in some way |
|
|
165 | my $render = $self->{render_cb} || $rw != $dw || $th != $dh; |
|
|
166 | |
|
|
167 | if ($render) { |
168 | if ($render) { |
|
|
169 | $ox = int .5 * ($::WIDTH - $tw); |
|
|
170 | $oy = int .5 * ($::HEIGHT - $th); |
|
|
171 | |
168 | glViewport 0, 0, $tw, $th; |
172 | glViewport $ox, $oy, $tw, $th; |
169 | glClear 0, 0, 0, 0; |
173 | #glScissor 0, 0, $tw, $th; |
|
|
174 | #glEnable GL_SCISSOR_TEST; |
170 | glMatrixMode GL_PROJECTION; |
175 | glMatrixMode GL_PROJECTION; |
171 | glLoadIdentity; |
176 | glLoadIdentity; |
172 | glOrtho 0, $tw, 0, $th, -10000, 10000; |
177 | glOrtho 0, $tw, 0, $th, -10000, 10000; |
173 | glMatrixMode GL_MODELVIEW; |
178 | glMatrixMode GL_MODELVIEW; |
174 | glLoadIdentity; |
179 | glLoadIdentity; |
175 | |
180 | |
176 | if ($self->{render_cb}) { |
181 | if ($self->{render_cb}) { |
177 | glScale $rw / $dw, $rh / $dh; |
182 | glScale $rw / $dw, $rh / $dh; |
178 | $self->{render_cb}->($self, $rw, $rh); |
183 | $self->{render_cb}->($self, $rw, $rh); |
179 | } else { |
184 | } else { |
|
|
185 | glClearColor 0, 0, 0, 0; |
|
|
186 | glClear GL_COLOR_BUFFER_BIT; |
180 | glPixelZoom $tw / $dw, $th / $dh; |
187 | glPixelZoom $tw / $dw, $th / $dh; |
|
|
188 | glRasterPos 0, 0; |
181 | glDrawPixels $dw, $dh, |
189 | glDrawPixels $dw, $dh, |
182 | $self->{format}, |
190 | $self->{format}, |
183 | $self->{type}, |
191 | $self->{type}, |
184 | $data; |
192 | $data; |
185 | glPixelZoom 1, 1; |
193 | glPixelZoom 1, 1; |
… | |
… | |
212 | glGetError; |
220 | glGetError; |
213 | |
221 | |
214 | if ($render) { |
222 | if ($render) { |
215 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
223 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
216 | $self->{internalformat}, |
224 | $self->{internalformat}, |
217 | 0, 0, |
225 | $ox, $oy, |
218 | $tw, $th, |
226 | $tw, $th, |
219 | 0; |
227 | 0; |
220 | gl_check "copying to texture %dx%d if=%x", |
228 | gl_check "copying to texture %dx%d if=%x", |
221 | $tw, $th, $self->{internalformat}; |
229 | $tw, $th, $self->{internalformat}; |
|
|
230 | |
|
|
231 | #glDisable GL_SCISSOR_TEST; |
222 | } else { |
232 | } else { |
223 | glTexImage2D GL_TEXTURE_2D, 0, |
233 | glTexImage2D GL_TEXTURE_2D, 0, |
224 | $self->{internalformat}, |
234 | $self->{internalformat}, |
225 | $dw, $dh, |
235 | $dw, $dh, |
226 | 0, |
236 | 0, |