… | |
… | |
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 (); |
22 | } |
36 | } |
23 | |
37 | |
24 | sub find_rcfile($) { |
38 | sub find_rcfile($) { |
25 | my $path; |
39 | my $path; |
26 | |
40 | |
… | |
… | |
70 | |
84 | |
71 | use SDL::OpenGL; |
85 | use SDL::OpenGL; |
72 | |
86 | |
73 | my @textures; |
87 | my @textures; |
74 | |
88 | |
75 | sub _new { |
89 | sub new { |
76 | my ($class, %data) = @_; |
90 | my ($class, %data) = @_; |
77 | |
91 | |
78 | my $self = bless \%data, $class; |
92 | my $self = bless { |
|
|
93 | internalformat => GL_RGBA, |
|
|
94 | format => GL_RGBA, |
|
|
95 | %data, |
|
|
96 | }, $class; |
79 | |
97 | |
80 | push @textures, $self; |
98 | push @textures, $self; |
81 | Scalar::Util::weaken $textures[-1]; |
99 | Scalar::Util::weaken $textures[-1]; |
82 | |
100 | |
83 | $self->upload; |
101 | $self->upload; |
… | |
… | |
86 | } |
104 | } |
87 | |
105 | |
88 | sub new_from_image { |
106 | sub new_from_image { |
89 | my ($class, $image) = @_; |
107 | my ($class, $image) = @_; |
90 | |
108 | |
91 | $class->_new (image => $image) |
109 | $class->new (image => $image) |
92 | } |
110 | } |
93 | |
111 | |
94 | sub new_from_file { |
112 | sub new_from_file { |
95 | my ($class, $path) = @_; |
113 | my ($class, $path) = @_; |
96 | |
114 | |
… | |
… | |
99 | |
117 | |
100 | local $/; |
118 | local $/; |
101 | $class->new_from_image (<$fh>) |
119 | $class->new_from_image (<$fh>) |
102 | } |
120 | } |
103 | |
121 | |
104 | sub new_from_surface { |
122 | #sub new_from_surface { |
105 | my ($class, $surface) = @_; |
123 | # my ($class, $surface) = @_; |
106 | |
124 | # |
107 | $surface->rgba; |
125 | # $surface->rgba; |
108 | |
126 | # |
109 | $class->_new ( |
127 | # $class->new ( |
110 | data => $surface->pixels, |
128 | # data => $surface->pixels, |
111 | width => $surface->width, |
129 | # width => $surface->width, |
112 | height => $surface->height, |
130 | # height => $surface->height, |
|
|
131 | # ) |
|
|
132 | #} |
|
|
133 | |
|
|
134 | sub new_from_text { |
|
|
135 | my ($class, $text, $height) = @_; |
|
|
136 | |
|
|
137 | my ($w, $h, $data) = Crossfire::Client::font_render $text, $height; |
|
|
138 | |
|
|
139 | $class->new ( |
|
|
140 | width => $w, |
|
|
141 | height => $h, |
|
|
142 | data => $data, |
|
|
143 | internalformat => GL_ALPHA4, |
|
|
144 | format => GL_ALPHA, |
113 | ) |
145 | ) |
114 | } |
|
|
115 | |
|
|
116 | sub new_from_ttf { |
|
|
117 | my ($class, $ttf, $text) = @_; |
|
|
118 | |
|
|
119 | utf8::encode $text; |
|
|
120 | |
|
|
121 | my $surface = SDL::TTFRenderUTF8Blended $ttf, $text, |
|
|
122 | (new SDL::Color -r => 255, -g => 255, -b => 255); |
|
|
123 | |
|
|
124 | $class->new_from_surface (bless \$surface, SDL::Surface::) |
|
|
125 | } |
146 | } |
126 | |
147 | |
127 | sub new_from_opengl { |
148 | sub new_from_opengl { |
128 | my ($class, $w, $h, $cb) = @_; |
149 | my ($class, $w, $h, $cb) = @_; |
129 | |
150 | |
130 | $class->_new (width => $w, height => $h, rendercb => $cb) |
151 | $class->new (width => $w, height => $h, rendercb => $cb) |
|
|
152 | } |
|
|
153 | |
|
|
154 | sub topot { |
|
|
155 | (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0] |
131 | } |
156 | } |
132 | |
157 | |
133 | sub upload { |
158 | sub upload { |
134 | my ($self) = @_; |
159 | my ($self) = @_; |
135 | |
160 | |
… | |
… | |
139 | |
164 | |
140 | if (exists $self->{data}) { |
165 | if (exists $self->{data}) { |
141 | $data = $self->{data}; |
166 | $data = $self->{data}; |
142 | } elsif (exists $self->{rendercb}) { |
167 | } elsif (exists $self->{rendercb}) { |
143 | glViewport 0, 0, $self->{width}, $self->{height}; |
168 | glViewport 0, 0, $self->{width}, $self->{height}; |
|
|
169 | glMatrixMode GL_PROJECTION; |
|
|
170 | glLoadIdentity; |
|
|
171 | glOrtho 0, $self->{width}, 0, $self->{height}, -10000, 10000; |
|
|
172 | glMatrixMode GL_MODELVIEW; |
|
|
173 | glPushmatrix; |
|
|
174 | glLoadIdentity; |
144 | glClear GL_COLOR_BUFFER_BIT; |
175 | glClear GL_COLOR_BUFFER_BIT; |
145 | |
176 | |
146 | $self->{rendercb}->($self, $self->{width}, $self->{height}); |
177 | $self->{rendercb}->($self, $self->{width}, $self->{height}); |
147 | } else { |
178 | } else { |
148 | my $pb = new Gtk2::Gdk::PixbufLoader; |
179 | my $pb = new Gtk2::Gdk::PixbufLoader; |
… | |
… | |
156 | $self->{height} = $pb->get_height; |
187 | $self->{height} = $pb->get_height; |
157 | |
188 | |
158 | $data = $pb->get_pixels; |
189 | $data = $pb->get_pixels; |
159 | } |
190 | } |
160 | |
191 | |
|
|
192 | my ($tw, $th) = @$self{qw(width height)}; |
|
|
193 | |
|
|
194 | unless ($GL_NPOT) { |
|
|
195 | $tw = topot $tw; |
|
|
196 | $th = topot $th; |
|
|
197 | |
|
|
198 | if (defined $data) { |
|
|
199 | my $bpp = (length $data) / ($self->{width} * $self->{height}); |
|
|
200 | $data = pack "(a" . ($tw * $bpp) . ")*", |
|
|
201 | unpack "(a" . ($self->{width} * $bpp) . ")*", $data; |
|
|
202 | $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{height}); |
|
|
203 | } |
|
|
204 | } |
|
|
205 | |
|
|
206 | $self->{s} = $self->{width} / $tw; |
|
|
207 | $self->{t} = $self->{height} / $th; |
|
|
208 | |
161 | ($self->{name}) = @{glGenTextures 1}; |
209 | $self->{name} ||= (glGenTextures 1)->[0]; |
162 | |
210 | |
163 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
211 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
164 | |
212 | |
165 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
213 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
166 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR; |
214 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR; |
167 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
215 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
168 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
216 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
169 | |
217 | |
|
|
218 | glGetError; |
170 | if (defined $data) { |
219 | if (defined $data) { |
171 | glTexImage2D GL_TEXTURE_2D, 0, |
220 | glTexImage2D GL_TEXTURE_2D, 0, |
172 | GL_RGBA8, |
221 | $self->{internalformat}, |
173 | $self->{width}, $self->{height}, |
222 | $tw, $th, # need to pad texture first |
174 | 0, |
223 | 0, |
175 | GL_RGBA, |
224 | $self->{format}, |
176 | GL_UNSIGNED_BYTE, |
225 | GL_UNSIGNED_BYTE, |
177 | $data; |
226 | $data; |
|
|
227 | glGetError and die; |
178 | } else { |
228 | } else { |
179 | warn "I{DTEDRAW TEX: $self->{width} $self->{height} \n"; |
|
|
180 | warn "ERR: ". (glGetError). "\n"; |
|
|
181 | my ($bd, $br) = (glGet (GL_DRAW_BUFFER), glGet (GL_READ_BUFFER)); |
|
|
182 | warn "BUF:[$bd $br]\n"; |
|
|
183 | |
|
|
184 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
229 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
185 | GL_RGBA8, |
230 | $self->{internalformat}, |
186 | 0, 0, |
231 | 0, 0, |
187 | $self->{width}, $self->{height}, |
232 | $tw, $th, |
188 | 0; |
233 | 0; |
189 | warn "ERR: ". (glGetError). "\n"; |
234 | glPopmatrix; |
190 | } |
235 | } |
191 | } |
236 | } |
192 | |
237 | |
193 | sub DESTROY { |
238 | sub DESTROY { |
194 | my ($self) = @_; |
239 | my ($self) = @_; |
… | |
… | |
196 | return unless exists $self->{name}; |
241 | return unless exists $self->{name}; |
197 | |
242 | |
198 | glDeleteTextures delete $self->{name}; |
243 | glDeleteTextures delete $self->{name}; |
199 | } |
244 | } |
200 | |
245 | |
201 | push @::GLINIT, sub { |
246 | sub restore_state{ |
202 | $_->upload |
247 | $_->upload |
203 | for grep $_, @textures; |
248 | for grep $_, @textures; |
204 | }; |
249 | }; |
205 | |
250 | |
206 | 1; |
251 | 1; |