… | |
… | |
17 | BEGIN { |
17 | BEGIN { |
18 | $VERSION = '0.1'; |
18 | $VERSION = '0.1'; |
19 | |
19 | |
20 | use XSLoader; |
20 | use XSLoader; |
21 | XSLoader::load "Crossfire::Client", $VERSION; |
21 | XSLoader::load "Crossfire::Client", $VERSION; |
|
|
22 | } |
|
|
23 | |
|
|
24 | our %GL_EXT; |
|
|
25 | our $GL_VERSION; |
|
|
26 | |
|
|
27 | our $GL_NPOT; |
|
|
28 | |
|
|
29 | sub gl_init { |
|
|
30 | $GL_VERSION = gl_version * 1; |
|
|
31 | %GL_EXT = map +($_ => 1), split /\s+/, gl_extensions; |
|
|
32 | |
|
|
33 | $GL_NPOT = $GL_EXT{GL_ARB_texture_non_power_of_two} || $GL_VERSION >= 2; |
|
|
34 | |
|
|
35 | Crossfire::Client::Texture::restore_state (); |
|
|
36 | |
|
|
37 | # $GL_NPOT=0;#d# |
22 | } |
38 | } |
23 | |
39 | |
24 | sub find_rcfile($) { |
40 | sub find_rcfile($) { |
25 | my $path; |
41 | my $path; |
26 | |
42 | |
… | |
… | |
124 | |
140 | |
125 | $class->new ( |
141 | $class->new ( |
126 | width => $w, |
142 | width => $w, |
127 | height => $h, |
143 | height => $h, |
128 | data => $data, |
144 | data => $data, |
129 | internalformat => GL_ALPHA8, |
145 | internalformat => GL_ALPHA4, |
130 | format => GL_ALPHA, |
146 | format => GL_ALPHA, |
131 | ) |
147 | ) |
132 | } |
148 | } |
133 | |
149 | |
134 | sub new_from_opengl { |
150 | sub new_from_opengl { |
135 | my ($class, $w, $h, $cb) = @_; |
151 | my ($class, $w, $h, $cb) = @_; |
136 | |
152 | |
137 | $class->new (width => $w, height => $h, rendercb => $cb) |
153 | $class->new (width => $w, height => $h, rendercb => $cb) |
|
|
154 | } |
|
|
155 | |
|
|
156 | sub topot { |
|
|
157 | (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0] |
138 | } |
158 | } |
139 | |
159 | |
140 | sub upload { |
160 | sub upload { |
141 | my ($self) = @_; |
161 | my ($self) = @_; |
142 | |
162 | |
… | |
… | |
148 | $data = $self->{data}; |
168 | $data = $self->{data}; |
149 | } elsif (exists $self->{rendercb}) { |
169 | } elsif (exists $self->{rendercb}) { |
150 | glViewport 0, 0, $self->{width}, $self->{height}; |
170 | glViewport 0, 0, $self->{width}, $self->{height}; |
151 | glMatrixMode GL_PROJECTION; |
171 | glMatrixMode GL_PROJECTION; |
152 | glLoadIdentity; |
172 | glLoadIdentity; |
153 | glOrtho 0, $self->{width}, 0, $self->{height}, -100, 100; |
173 | glOrtho 0, $self->{width}, 0, $self->{height}, -10000, 10000; |
154 | glMatrixMode GL_MODELVIEW; |
174 | glMatrixMode GL_MODELVIEW; |
155 | glPushmatrix; |
175 | glPushmatrix; |
156 | glLoadIdentity; |
176 | glLoadIdentity; |
157 | glClear GL_COLOR_BUFFER_BIT; |
177 | glClear GL_COLOR_BUFFER_BIT; |
158 | |
178 | |
… | |
… | |
169 | $self->{height} = $pb->get_height; |
189 | $self->{height} = $pb->get_height; |
170 | |
190 | |
171 | $data = $pb->get_pixels; |
191 | $data = $pb->get_pixels; |
172 | } |
192 | } |
173 | |
193 | |
|
|
194 | my ($tw, $th) = @$self{qw(width height)}; |
|
|
195 | |
|
|
196 | unless ($NPOT) { |
|
|
197 | $tw = topot $tw; |
|
|
198 | $th = topot $th; |
|
|
199 | |
|
|
200 | if (defined $data) { |
|
|
201 | $data = pack "(a$tw)*", (unpack "(a$self->{width})*", $data); |
|
|
202 | $data .= ("\x00" x $tw) x ($th - $self->{height}); |
|
|
203 | } |
|
|
204 | } |
|
|
205 | |
|
|
206 | $self->{u} = $self->{width} / $tw; |
|
|
207 | $self->{v} = $self->{height} / $th; |
|
|
208 | |
174 | ($self->{name}) = @{glGenTextures 1}; |
209 | $self->{name} ||= (glGenTextures 1)->[0]; |
175 | |
210 | |
176 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
211 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
177 | |
212 | |
178 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; |
213 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; |
179 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST;#_MIPMAP_LINEAR; |
214 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST;#_MIPMAP_LINEAR; |
180 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
215 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
181 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
216 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
182 | |
217 | |
|
|
218 | glGetError; |
183 | if (defined $data) { |
219 | if (defined $data) { |
184 | glTexImage2D GL_TEXTURE_2D, 0, |
220 | glTexImage2D GL_TEXTURE_2D, 0, |
185 | $self->{internalformat}, |
221 | $self->{internalformat}, |
186 | $self->{width}, $self->{height}, |
222 | $tw, $th, # need to pad texture first |
187 | 0, |
223 | 0, |
188 | $self->{format}, |
224 | $self->{format}, |
189 | GL_UNSIGNED_BYTE, |
225 | GL_UNSIGNED_BYTE, |
190 | $data; |
226 | $data; |
|
|
227 | glGetError and die; |
191 | } else { |
228 | } else { |
192 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
229 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
193 | $self->{internalformat}, |
230 | $self->{internalformat}, |
194 | 0, 0, |
231 | 0, 0, |
195 | $self->{width}, $self->{height}, |
232 | $tw, $th, |
196 | 0; |
233 | 0; |
197 | glPopmatrix; |
234 | glPopmatrix; |
198 | #SDL::GLSwapBuffers; |
|
|
199 | #sleep 1; |
|
|
200 | } |
235 | } |
201 | } |
236 | } |
202 | |
237 | |
203 | sub DESTROY { |
238 | sub DESTROY { |
204 | my ($self) = @_; |
239 | my ($self) = @_; |
… | |
… | |
206 | return unless exists $self->{name}; |
241 | return unless exists $self->{name}; |
207 | |
242 | |
208 | glDeleteTextures delete $self->{name}; |
243 | glDeleteTextures delete $self->{name}; |
209 | } |
244 | } |
210 | |
245 | |
211 | push @::GLINIT, sub { |
246 | sub restore_state{ |
212 | $_->upload |
247 | $_->upload |
213 | for grep $_, @textures; |
248 | for grep $_, @textures; |
214 | }; |
249 | }; |
215 | |
250 | |
216 | 1; |
251 | 1; |