… | |
… | |
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 | |
44 | |
45 | $class->new (image => $image, %arg) |
45 | $class->new (image => $image, internalformat => undef, %arg) |
46 | } |
46 | } |
47 | |
47 | |
48 | sub new_from_file { |
48 | sub new_from_file { |
49 | my ($class, $path, %arg) = @_; |
49 | my ($class, $path, %arg) = @_; |
50 | |
50 | |
… | |
… | |
87 | my ($class, $w, $h, $cb) = @_; |
87 | my ($class, $w, $h, $cb) = @_; |
88 | |
88 | |
89 | $class->new (w => $w || 1, h => $h || 1, render_cb => $cb) |
89 | $class->new (w => $w || 1, h => $h || 1, render_cb => $cb) |
90 | } |
90 | } |
91 | |
91 | |
92 | sub topot { |
|
|
93 | (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0] |
|
|
94 | } |
|
|
95 | |
|
|
96 | sub upload { |
92 | sub upload { |
97 | my ($self) = @_; |
93 | my ($self) = @_; |
98 | |
94 | |
99 | return unless $GL_VERSION; |
95 | return unless $GL_VERSION; |
100 | |
96 | |
… | |
… | |
111 | glMatrixMode GL_MODELVIEW; |
107 | glMatrixMode GL_MODELVIEW; |
112 | glLoadIdentity; |
108 | glLoadIdentity; |
113 | $self->{render_cb}->($self, $self->{w}, $self->{h}); |
109 | $self->{render_cb}->($self, $self->{w}, $self->{h}); |
114 | |
110 | |
115 | } else { |
111 | } else { |
116 | ($self->{w}, $self->{h}, $data, $self->{internalformat}, $self->{format}, $self->{type}) |
112 | ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type}) |
117 | = CFClient::load_image_inline $self->{image}; |
113 | = CFClient::load_image_inline $self->{image}; |
|
|
114 | |
|
|
115 | $self->{internalformat} ||= $internalformat; |
118 | } |
116 | } |
119 | |
117 | |
120 | my ($tw, $th) = @$self{qw(w h)}; |
118 | my ($tw, $th) = @$self{qw(w h)}; |
121 | |
|
|
122 | unless ($tw > 0 && $th > 0) { |
|
|
123 | $tw = $th = 1; |
|
|
124 | $data = "\x00" x 64; |
|
|
125 | } |
|
|
126 | |
119 | |
127 | $self->{minified} ||= [CFClient::average $tw, $th, $data] |
120 | $self->{minified} ||= [CFClient::average $tw, $th, $data] |
128 | if $self->{minify}; |
121 | if $self->{minify}; |
129 | |
122 | |
130 | unless ($GL_NPOT) { |
123 | pad2pot $data, $tw, $th unless $GL_NPOT; |
131 | # TODO: does not work for zero-sized textures |
|
|
132 | $tw = topot $tw; |
|
|
133 | $th = topot $th; |
|
|
134 | |
|
|
135 | if (($tw != $self->{w} || $th != $self->{h}) && defined $data) { |
|
|
136 | my $bpp = (length $data) / ($self->{w} * $self->{h}); |
|
|
137 | $data = pack "(a" . ($tw * $bpp) . ")*", |
|
|
138 | unpack "(a" . ($self->{w} * $bpp) . ")*", $data; |
|
|
139 | $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h}); |
|
|
140 | } |
|
|
141 | } |
|
|
142 | |
124 | |
143 | $self->{s} = $self->{w} / $tw; |
125 | $self->{s} = $self->{w} / $tw; |
144 | $self->{t} = $self->{h} / $th; |
126 | $self->{t} = $self->{h} / $th; |
145 | |
127 | |
146 | $self->{name} ||= glGenTexture; |
128 | $self->{name} ||= glGenTexture; |
… | |
… | |
157 | |
139 | |
158 | if ($::FAST) { |
140 | if ($::FAST) { |
159 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; |
141 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; |
160 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST; |
142 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST; |
161 | } elsif ($self->{mipmap} && $GL_VERSION >= 1.4) { |
143 | } elsif ($self->{mipmap} && $GL_VERSION >= 1.4) { |
162 | # alternatively check for 0x8191 |
|
|
163 | glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1; |
144 | glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1; |
164 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
145 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
165 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR; |
146 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR; |
166 | } else { |
147 | } else { |
167 | glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, $self->{mipmap}; |
148 | glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, $self->{mipmap}; |
… | |
… | |
188 | $tw, $th, |
169 | $tw, $th, |
189 | 0; |
170 | 0; |
190 | gl_check "copying to texture %dx%d if=%x", |
171 | gl_check "copying to texture %dx%d if=%x", |
191 | $tw, $th, $self->{internalformat}; |
172 | $tw, $th, $self->{internalformat}; |
192 | } |
173 | } |
193 | |
|
|
194 | glBindTexture GL_TEXTURE_2D, 0; # just to be on the safe side |
|
|
195 | } |
174 | } |
|
|
175 | |
|
|
176 | sub shutdown { |
|
|
177 | my ($self) = @_; |
|
|
178 | |
|
|
179 | glDeleteTexture delete $self->{name} |
|
|
180 | if $self->{name}; |
|
|
181 | } |
196 | |
182 | |
197 | sub DESTROY { |
183 | sub DESTROY { |
198 | my ($self) = @_; |
184 | my ($self) = @_; |
199 | |
185 | |
200 | delete $TEXTURES{$self+0}; |
186 | delete $TEXTURES{$self+0}; |
201 | |
187 | |
202 | glDeleteTexture delete $self->{name} |
188 | $self->shutdown; |
203 | if $self->{name}; |
|
|
204 | } |
189 | } |
205 | |
190 | |
206 | $CFClient::OpenGL::INIT_HOOK{"CFClient::Texture"} = sub { |
191 | $CFClient::OpenGL::INIT_HOOK{"CFClient::Texture"} = sub { |
207 | $_->upload |
192 | $_->upload |
208 | for values %TEXTURES; |
193 | for values %TEXTURES; |
209 | }; |
194 | }; |
210 | |
195 | |
|
|
196 | $CFClient::OpenGL::SHUTDOWN_HOOK{"CFClient::Texture"} = sub { |
|
|
197 | $_->shutdown |
|
|
198 | for values %TEXTURES; |
|
|
199 | }; |
|
|
200 | |
211 | 1; |
201 | 1; |
212 | |
202 | |
213 | =back |
203 | =back |
214 | |
204 | |
215 | =head1 AUTHOR |
205 | =head1 AUTHOR |