… | |
… | |
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 { |
… | |
… | |
92 | sub upload { |
95 | sub upload { |
93 | my ($self) = @_; |
96 | my ($self) = @_; |
94 | |
97 | |
95 | return unless $GL_VERSION; |
98 | return unless $GL_VERSION; |
96 | |
99 | |
97 | my ($tw, $th, $data); |
100 | # $tw,$th texture |
|
|
101 | # $rw,$rh rendered/used size |
|
|
102 | # $dw,$dh $data |
|
|
103 | |
|
|
104 | my ($data, $dw, $dh); |
98 | |
105 | |
99 | if (exists $self->{data}) { |
106 | if (exists $self->{data}) { |
100 | $data = $self->{data}; |
107 | $data = $self->{data}; |
101 | ($tw, $th) = @$self{qw(w h)}; |
108 | ($dw, $dh) = @$self{qw(w h)}; |
102 | |
109 | |
103 | } elsif (exists $self->{render_cb}) { |
110 | } elsif (exists $self->{render_cb}) { |
104 | ($tw, $th) = @$self{qw(w h)}; |
111 | ($dw, $dh) = @$self{qw(w h)}; |
105 | |
112 | |
106 | } else { |
113 | } elsif (exists $self->{image}) { |
107 | ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type}) |
114 | ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type}) |
108 | = CFPlus::load_image_inline $self->{image}; |
115 | = CFPlus::load_image_inline $self->{image}; |
109 | |
116 | |
110 | $self->{internalformat} ||= $internalformat; |
117 | $self->{internalformat} ||= $internalformat; |
111 | ($tw, $th) = @$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"; |
112 | } |
121 | } |
|
|
122 | |
|
|
123 | my ($tw, $th) = ($dw, $dh); |
113 | |
124 | |
114 | defined $data or $self->{render_cb} or die; # some sanity check |
125 | defined $data or $self->{render_cb} or die; # some sanity check |
115 | |
126 | |
116 | $self->{minified} ||= [CFPlus::average $tw, $th, $data] |
127 | $self->{minified} ||= [CFPlus::average $dw, $dh, $data] |
117 | if $self->{minify}; |
128 | if $self->{minify}; |
118 | |
129 | |
119 | pad2pot $data, $tw, $th unless $GL_NPOT; |
130 | # against rather broken cards we enforce a maximum texture size |
120 | $tw = min $MAX_W, $tw; |
131 | $tw = min $MAX_W, minpot $tw; |
121 | $th = min $MAX_H, $th; |
132 | $th = min $MAX_H, minpot $th; |
122 | |
133 | |
|
|
134 | # if only pot-textures are allowed, pot'ify tw/th |
|
|
135 | unless ($GL_NPOT && 0) {#d# |
|
|
136 | $tw = minpot $tw; |
|
|
137 | $th = minpot $th; |
|
|
138 | } |
|
|
139 | |
123 | # now further decrease texture size |
140 | # now further decrease texture size until the |
|
|
141 | # card does accept it |
124 | while (!texture_valid_2d $self->{internalformat}, $tw, $th, $self->{format}, $self->{type}) { |
142 | while (!texture_valid_2d $self->{internalformat}, $tw, $th, $self->{format}, $self->{type}) { |
125 | # quarter the texture size |
143 | # quarter the texture size |
126 | $tw >>= 1; |
144 | $tw >>= 1; |
127 | $th >>= 1; |
145 | $th >>= 1; |
128 | } |
146 | } |
129 | |
147 | |
130 | my $need_render = $self->{render_cb} || $tw < $self->{w} || $th < $self->{h}; |
148 | # decide the amount of space used in the texture |
|
|
149 | my ($rw, $rh); |
|
|
150 | my ($ox, $oy); # area shift to lessen effetc of buggy opengl implementations (nvida, ati) |
|
|
151 | my $render; |
131 | |
152 | |
|
|
153 | if ($self->{render_cb}) { |
|
|
154 | # use only part of the texture |
|
|
155 | #$rw >>= 1 while $rw > $tw; |
|
|
156 | #$rh >>= 1 while $rh > $th; |
|
|
157 | $rw = min $dw, $tw; |
|
|
158 | $rh = min $dh, $th; |
|
|
159 | ++$render; |
|
|
160 | } else { |
|
|
161 | if ($self->{wrap} || $tw < $dw || $th < $dh) { |
|
|
162 | # scale to the full texture size |
|
|
163 | ($rw, $rh) = ($tw, $th); |
|
|
164 | ++$render; |
|
|
165 | } else { |
|
|
166 | # pad |
|
|
167 | pad $data, $dw, $dh, $tw, $th; |
|
|
168 | ($rw, $rh) = ($dw, $dh); |
|
|
169 | ($dw, $dh) = ($tw, $th); |
|
|
170 | } |
|
|
171 | } |
|
|
172 | |
132 | if ($need_render) { |
173 | if ($render) { |
|
|
174 | $ox = int .5 * ($::WIDTH - $rw); |
|
|
175 | $oy = int .5 * ($::HEIGHT - $rh); |
|
|
176 | |
133 | glViewport 0, 0, $tw, $th; |
177 | glViewport $ox, $oy, $tw, $th; |
|
|
178 | #glScissor 0, 0, $tw, $th; |
|
|
179 | #glEnable GL_SCISSOR_TEST; |
134 | glMatrixMode GL_PROJECTION; |
180 | glMatrixMode GL_PROJECTION; |
135 | glLoadIdentity; |
181 | glLoadIdentity; |
136 | glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000; |
182 | glOrtho 0, $tw, 0, $th, -10000, 10000; |
137 | glMatrixMode GL_MODELVIEW; |
183 | glMatrixMode GL_MODELVIEW; |
138 | glLoadIdentity; |
184 | glLoadIdentity; |
139 | |
185 | |
140 | if ($self->{render_cb}) { |
186 | if ($self->{render_cb}) { |
|
|
187 | glScale $rw / $dw, $rh / $dh; |
141 | $self->{render_cb}->($self, $self->{w}, $self->{h}); |
188 | $self->{render_cb}->($self, $rw, $rh); |
142 | } else { |
189 | } else { |
|
|
190 | glClearColor 0, 0, 0, 0; |
|
|
191 | glClear GL_COLOR_BUFFER_BIT; |
143 | glPixelZoom $tw / $self->{w}, $th / $self->{h}; |
192 | glPixelZoom $tw / $dw, $th / $dh; |
|
|
193 | glRasterPos 0, 0; |
144 | glDrawPixels $self->{w}, $self->{h}, |
194 | glDrawPixels $dw, $dh, |
145 | $self->{format}, |
195 | $self->{format}, |
146 | $self->{type}, |
196 | $self->{type}, |
147 | $data; |
197 | $data; |
148 | glPixelZoom 1, 1; |
198 | glPixelZoom 1, 1; |
149 | } |
199 | } |
… | |
… | |
172 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR; |
222 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR; |
173 | } |
223 | } |
174 | |
224 | |
175 | glGetError; |
225 | glGetError; |
176 | |
226 | |
177 | if ($need_render) { |
227 | if ($render) { |
178 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
228 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
179 | $self->{internalformat}, |
229 | $self->{internalformat}, |
180 | 0, 0, |
230 | $ox, $oy, |
181 | $tw, $th, |
231 | $tw, $th, |
182 | 0; |
232 | 0; |
183 | gl_check "copying to texture %dx%d if=%x", |
233 | gl_check "copying to texture %dx%d if=%x", |
184 | $tw, $th, $self->{internalformat}; |
234 | $tw, $th, $self->{internalformat}; |
|
|
235 | |
|
|
236 | #glDisable GL_SCISSOR_TEST; |
185 | } else { |
237 | } else { |
186 | glTexImage2D GL_TEXTURE_2D, 0, |
238 | glTexImage2D GL_TEXTURE_2D, 0, |
187 | $self->{internalformat}, |
239 | $self->{internalformat}, |
188 | $tw, $th, |
240 | $dw, $dh, |
189 | 0, |
241 | 0, |
190 | $self->{format}, |
242 | $self->{format}, |
191 | $self->{type}, |
243 | $self->{type}, |
192 | $data; |
244 | $data; |
193 | gl_check "uploading texture %dx%d if=%x f=%x t=%x", |
245 | gl_check "uploading texture %dx%d if=%x f=%x t=%x", |
194 | $tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; |
246 | $tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; |
195 | } |
247 | } |
196 | |
248 | |
197 | $self->{s} = min 1, $self->{w} / $tw; |
249 | $self->{s} = $rw / $tw; |
198 | $self->{t} = min 1, $self->{h} / $th; |
250 | $self->{t} = $rh / $th; |
199 | } |
251 | } |
200 | |
252 | |
201 | sub shutdown { |
253 | sub shutdown { |
202 | my ($self) = @_; |
254 | my ($self) = @_; |
203 | |
255 | |