… | |
… | |
18 | $VERSION = '0.1'; |
18 | $VERSION = '0.1'; |
19 | |
19 | |
20 | use XSLoader; |
20 | use XSLoader; |
21 | XSLoader::load "CFClient", $VERSION; |
21 | XSLoader::load "CFClient", $VERSION; |
22 | } |
22 | } |
|
|
23 | |
|
|
24 | use SDL::OpenGL; |
23 | |
25 | |
24 | our %GL_EXT; |
26 | our %GL_EXT; |
25 | our $GL_VERSION; |
27 | our $GL_VERSION; |
26 | |
28 | |
27 | our $GL_NPOT; |
29 | our $GL_NPOT; |
… | |
… | |
29 | sub gl_init { |
31 | sub gl_init { |
30 | $GL_VERSION = gl_version * 1; |
32 | $GL_VERSION = gl_version * 1; |
31 | %GL_EXT = map +($_ => 1), split /\s+/, gl_extensions; |
33 | %GL_EXT = map +($_ => 1), split /\s+/, gl_extensions; |
32 | |
34 | |
33 | $GL_NPOT = $GL_EXT{GL_ARB_texture_non_power_of_two} || $GL_VERSION >= 2; |
35 | $GL_NPOT = $GL_EXT{GL_ARB_texture_non_power_of_two} || $GL_VERSION >= 2; |
|
|
36 | |
|
|
37 | glEnable GL_TEXTURE_2D; |
|
|
38 | glEnable GL_COLOR_MATERIAL; |
|
|
39 | glShadeModel GL_FLAT; |
|
|
40 | glDisable GL_DEPTH_TEST; |
|
|
41 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
42 | |
|
|
43 | glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST; |
34 | |
44 | |
35 | CFClient::Texture::restore_state (); |
45 | CFClient::Texture::restore_state (); |
36 | } |
46 | } |
37 | |
47 | |
38 | sub find_rcfile($) { |
48 | sub find_rcfile($) { |
… | |
… | |
92 | my ($class, %data) = @_; |
102 | my ($class, %data) = @_; |
93 | |
103 | |
94 | my $self = bless { |
104 | my $self = bless { |
95 | internalformat => GL_RGBA, |
105 | internalformat => GL_RGBA, |
96 | format => GL_RGBA, |
106 | format => GL_RGBA, |
|
|
107 | type => GL_UNSIGNED_BYTE, |
97 | %data, |
108 | %data, |
98 | }, $class; |
109 | }, $class; |
99 | |
110 | |
100 | push @textures, $self; |
111 | push @textures, $self; |
101 | Scalar::Util::weaken $textures[-1]; |
112 | Scalar::Util::weaken $textures[-1]; |
… | |
… | |
142 | w => $w, |
153 | w => $w, |
143 | h => $h, |
154 | h => $h, |
144 | data => $data, |
155 | data => $data, |
145 | internalformat => GL_ALPHA4, |
156 | internalformat => GL_ALPHA4, |
146 | format => GL_ALPHA, |
157 | format => GL_ALPHA, |
|
|
158 | type => GL_UNSIGNED_BYTE, |
147 | ) |
159 | ) |
148 | } |
160 | } |
149 | |
161 | |
150 | sub new_from_opengl { |
162 | sub new_from_opengl { |
151 | my ($class, $w, $h, $cb) = @_; |
163 | my ($class, $w, $h, $cb) = @_; |
… | |
… | |
158 | } |
170 | } |
159 | |
171 | |
160 | sub upload { |
172 | sub upload { |
161 | my ($self) = @_; |
173 | my ($self) = @_; |
162 | |
174 | |
163 | return unless $SDL::App::USING_OPENGL; |
175 | return unless $GL_VERSION; |
164 | |
176 | |
165 | my $data; |
177 | my $data; |
166 | |
178 | |
167 | if (exists $self->{data}) { |
179 | if (exists $self->{data}) { |
168 | $data = $self->{data}; |
180 | $data = $self->{data}; |
… | |
… | |
176 | glLoadIdentity; |
188 | glLoadIdentity; |
177 | glClear GL_COLOR_BUFFER_BIT; |
189 | glClear GL_COLOR_BUFFER_BIT; |
178 | $self->{render_cb}->($self, $self->{w}, $self->{h}); |
190 | $self->{render_cb}->($self, $self->{w}, $self->{h}); |
179 | |
191 | |
180 | } else { |
192 | } else { |
181 | my $pb = new Gtk2::Gdk::PixbufLoader; |
193 | ($self->{w}, $self->{h}, $data, $self->{internalformat}, $self->{format}, $self->{type}) |
182 | $pb->write ($self->{image}); |
194 | = CFClient::load_image_inline $self->{image}; |
183 | $pb->close; |
|
|
184 | |
|
|
185 | $pb = $pb->get_pixbuf; |
|
|
186 | $pb = $pb->add_alpha (0, 0, 0, 0); |
|
|
187 | |
|
|
188 | $self->{w} = $pb->get_width; |
|
|
189 | $self->{h} = $pb->get_height; |
|
|
190 | |
|
|
191 | $data = $pb->get_pixels; |
|
|
192 | } |
195 | } |
193 | |
196 | |
194 | my ($tw, $th) = @$self{qw(w h)}; |
197 | my ($tw, $th) = @$self{qw(w h)}; |
195 | |
198 | |
196 | unless ($tw && $th) { |
199 | unless ($tw && $th) { |
… | |
… | |
216 | |
219 | |
217 | $self->{name} ||= (glGenTextures 1)->[0]; |
220 | $self->{name} ||= (glGenTextures 1)->[0]; |
218 | |
221 | |
219 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
222 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
220 | |
223 | |
221 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
224 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR; |
222 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR; |
225 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR; |
223 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
226 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
224 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
227 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
225 | |
228 | |
226 | if (defined $data) { |
229 | if (defined $data) { |
227 | glTexImage2D GL_TEXTURE_2D, 0, |
230 | glTexImage2D GL_TEXTURE_2D, 0, |
228 | $self->{internalformat}, |
231 | $self->{internalformat}, |
229 | $tw, $th, # need to pad texture first |
232 | $tw, $th, # need to pad texture first |
230 | 0, |
233 | 0, |
231 | $self->{format}, |
234 | $self->{format}, |
232 | GL_UNSIGNED_BYTE, |
235 | $self->{type}, |
233 | $data; |
236 | $data; |
234 | glGetError and die; |
237 | glGetError and die; |
235 | } else { |
238 | } else { |
236 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
239 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
237 | $self->{internalformat}, |
240 | $self->{internalformat}, |