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