1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | CFClient::Texture - tetxure class for CFClient |
3 | CFPlus::Texture - tetxure class for CFPlus |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use CFClient::Texture; |
7 | use CFPlus::Texture; |
8 | |
8 | |
9 | =head1 DESCRIPTION |
9 | =head1 DESCRIPTION |
10 | |
10 | |
11 | =over 4 |
11 | =over 4 |
12 | |
12 | |
13 | =cut |
13 | =cut |
14 | |
14 | |
15 | package CFClient::Texture; |
15 | package CFPlus::Texture; |
16 | |
16 | |
17 | use strict; |
17 | use strict; |
18 | |
18 | |
19 | use Scalar::Util; |
19 | use List::Util qw(max min); |
20 | |
|
|
21 | use CFClient::OpenGL; |
20 | use CFPlus::OpenGL; |
22 | |
21 | |
23 | my %TEXTURES; |
22 | my %TEXTURES; |
|
|
23 | my ($MAX_W, $MAX_H) = (4096, 4096); # maximum texture size attempted by this module |
24 | |
24 | |
25 | sub new { |
25 | sub new { |
26 | my ($class, %data) = @_; |
26 | my ($class, %data) = @_; |
27 | |
27 | |
28 | my $self = bless { |
28 | my $self = bless { |
… | |
… | |
30 | format => GL_RGBA, |
30 | format => GL_RGBA, |
31 | type => GL_UNSIGNED_BYTE, |
31 | type => GL_UNSIGNED_BYTE, |
32 | %data, |
32 | %data, |
33 | }, $class; |
33 | }, $class; |
34 | |
34 | |
35 | Scalar::Util::weaken ($TEXTURES{$self+0} = $self); |
35 | CFPlus::weaken ($TEXTURES{$self+0} = $self); |
36 | |
36 | |
37 | $self->upload; |
37 | $self->upload; |
38 | |
38 | |
39 | $self |
39 | $self |
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 | |
… | |
… | |
65 | # w => $surface->width, |
65 | # w => $surface->width, |
66 | # h => $surface->height, |
66 | # h => $surface->height, |
67 | # ) |
67 | # ) |
68 | #} |
68 | #} |
69 | |
69 | |
70 | sub new_from_layout { |
70 | #sub new_from_layout { |
71 | my ($class, $layout, %arg) = @_; |
71 | # my ($class, $layout, %arg) = @_; |
72 | |
72 | # |
73 | my ($w, $h, $data, $format, $internalformat) = $layout->render; |
73 | # my ($w, $h, $data, $format, $internalformat) = $layout->render; |
74 | |
74 | # |
75 | $class->new ( |
75 | # $class->new ( |
76 | w => $w, |
76 | # w => $w, |
77 | h => $h, |
77 | # h => $h, |
78 | data => $data, |
78 | # data => $data, |
79 | format => $format, |
79 | # format => $format, |
80 | internalformat => $format, |
80 | # internalformat => $format, |
81 | type => GL_UNSIGNED_BYTE, |
81 | # type => GL_UNSIGNED_BYTE, |
82 | %arg, |
82 | # %arg, |
83 | ) |
83 | # ) |
84 | } |
84 | #} |
85 | |
85 | |
86 | sub new_from_opengl { |
86 | sub new_from_opengl { |
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, nearest => 1) |
90 | } |
90 | } |
91 | |
91 | |
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 $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}; |
|
|
105 | ($dw, $dh) = @$self{qw(w h)}; |
101 | |
106 | |
102 | } elsif (exists $self->{render_cb}) { |
107 | } elsif (exists $self->{render_cb}) { |
|
|
108 | ($dw, $dh) = @$self{qw(w h)}; |
|
|
109 | |
|
|
110 | } else { |
|
|
111 | ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type}) |
|
|
112 | = CFPlus::load_image_inline $self->{image}; |
|
|
113 | |
|
|
114 | $self->{internalformat} ||= $internalformat; |
|
|
115 | ($dw, $dh) = @$self{qw(w h)}; |
|
|
116 | } |
|
|
117 | |
|
|
118 | my ($tw, $th) = ($dw, $dh); |
|
|
119 | |
|
|
120 | defined $data or $self->{render_cb} or die; # some sanity check |
|
|
121 | |
|
|
122 | $self->{minified} ||= [CFPlus::average $dw, $dh, $data] |
|
|
123 | if $self->{minify}; |
|
|
124 | |
|
|
125 | # against rather broken cards we enforce a maximum texture size |
|
|
126 | $tw = min $MAX_W, minpot $tw; |
|
|
127 | $th = min $MAX_H, minpot $th; |
|
|
128 | |
|
|
129 | # if only pot-textures are allowed, pot'ify tw/th |
|
|
130 | unless ($GL_NPOT && 0) {#d# |
|
|
131 | $tw = minpot $tw; |
|
|
132 | $th = minpot $th; |
|
|
133 | } |
|
|
134 | |
|
|
135 | # now further decrease texture size until the |
|
|
136 | # card does accept it |
|
|
137 | while (!texture_valid_2d $self->{internalformat}, $tw, $th, $self->{format}, $self->{type}) { |
|
|
138 | # quarter the texture size |
|
|
139 | $tw >>= 1; |
|
|
140 | $th >>= 1; |
|
|
141 | } |
|
|
142 | |
|
|
143 | # decide the amount of space used in the texture |
|
|
144 | my ($rw, $rh); |
|
|
145 | my $render; |
|
|
146 | |
|
|
147 | if ($self->{render_cb}) { |
|
|
148 | # use only part of the texture |
|
|
149 | #$rw >>= 1 while $rw > $tw; |
|
|
150 | #$rh >>= 1 while $rh > $th; |
|
|
151 | $rw = min $dw, $tw; |
|
|
152 | $rh = min $dh, $th; |
|
|
153 | ++$render; |
|
|
154 | } else { |
|
|
155 | if ($self->{wrap} || $tw < $dw || $th < $dh) { |
|
|
156 | # scale to the full texture size |
|
|
157 | ($rw, $rh) = ($tw, $th); |
|
|
158 | ++$render; |
|
|
159 | } else { |
|
|
160 | # pad |
|
|
161 | pad $data, $dw, $dh, $tw, $th; |
|
|
162 | ($rw, $rh) = ($dw, $dh); |
|
|
163 | ($dw, $dh) = ($tw, $th); |
|
|
164 | } |
|
|
165 | } |
|
|
166 | |
|
|
167 | if ($render) { |
103 | glViewport 0, 0, $self->{w}, $self->{h}; |
168 | glViewport 0, 0, $tw, $th; |
104 | glMatrixMode GL_PROJECTION; |
169 | glMatrixMode GL_PROJECTION; |
105 | glLoadIdentity; |
170 | glLoadIdentity; |
106 | glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000; |
171 | glOrtho 0, $tw, 0, $th, -10000, 10000; |
107 | glMatrixMode GL_MODELVIEW; |
172 | glMatrixMode GL_MODELVIEW; |
108 | glLoadIdentity; |
173 | glLoadIdentity; |
|
|
174 | |
|
|
175 | if ($self->{render_cb}) { |
|
|
176 | glScale $rw / $dw, $rh / $dh; |
109 | $self->{render_cb}->($self, $self->{w}, $self->{h}); |
177 | $self->{render_cb}->($self, $rw, $rh); |
110 | |
|
|
111 | } else { |
178 | } else { |
112 | ($self->{w}, $self->{h}, $data, $self->{internalformat}, $self->{format}, $self->{type}) |
179 | glClearColor 0, 0, 0, 0; |
113 | = CFClient::load_image_inline $self->{image}; |
180 | glClear GL_COLOR_BUFFER_BIT; |
|
|
181 | glPixelZoom $tw / $dw, $th / $dh; |
|
|
182 | glDrawPixels $dw, $dh, |
|
|
183 | $self->{format}, |
|
|
184 | $self->{type}, |
|
|
185 | $data; |
|
|
186 | glPixelZoom 1, 1; |
|
|
187 | } |
114 | } |
188 | } |
115 | |
189 | |
116 | my ($tw, $th) = @$self{qw(w h)}; |
|
|
117 | |
|
|
118 | $self->{minified} ||= [CFClient::average $tw, $th, $data] |
|
|
119 | if $self->{minify}; |
|
|
120 | |
|
|
121 | pad2pot $data, $tw, $th unless $GL_NPOT; |
|
|
122 | |
|
|
123 | $self->{s} = $self->{w} / $tw; |
|
|
124 | $self->{t} = $self->{h} / $th; |
|
|
125 | |
|
|
126 | $self->{name} ||= glGenTexture; |
|
|
127 | |
|
|
128 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
190 | glBindTexture GL_TEXTURE_2D, $self->{name} ||= glGenTexture; |
129 | |
191 | |
130 | if ($self->{wrap}) { |
192 | if ($self->{wrap}) { |
131 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
193 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
132 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
194 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
133 | } else { |
195 | } else { |
134 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, $GL_VERSION >= 1.2 ? GL_CLAMP_TO_EDGE : GL_CLAMP; |
196 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, $GL_VERSION >= 1.2 ? GL_CLAMP_TO_EDGE : GL_CLAMP; |
135 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, $GL_VERSION >= 1.2 ? GL_CLAMP_TO_EDGE : GL_CLAMP; |
197 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, $GL_VERSION >= 1.2 ? GL_CLAMP_TO_EDGE : GL_CLAMP; |
136 | } |
198 | } |
137 | |
199 | |
138 | if ($::FAST) { |
200 | if ($::FAST || $self->{nearest}) { |
139 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; |
201 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; |
140 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST; |
202 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST; |
141 | } elsif ($self->{mipmap} && $GL_VERSION >= 1.4) { |
203 | } elsif ($self->{mipmap} && $GL_VERSION >= 1.4) { |
142 | glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1; |
204 | glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1; |
143 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
205 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
… | |
… | |
148 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR; |
210 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR; |
149 | } |
211 | } |
150 | |
212 | |
151 | glGetError; |
213 | glGetError; |
152 | |
214 | |
153 | if (defined $data) { |
215 | if ($render) { |
154 | glTexImage2D GL_TEXTURE_2D, 0, |
|
|
155 | $self->{internalformat}, |
|
|
156 | $tw, $th, |
|
|
157 | 0, |
|
|
158 | $self->{format}, |
|
|
159 | $self->{type}, |
|
|
160 | $data; |
|
|
161 | gl_check "uploading texture %dx%d if=%x f=%x t=%x", |
|
|
162 | $tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; |
|
|
163 | } else { |
|
|
164 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
216 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
165 | $self->{internalformat}, |
217 | $self->{internalformat}, |
166 | 0, 0, |
218 | 0, 0, |
167 | $tw, $th, |
219 | $tw, $th, |
168 | 0; |
220 | 0; |
169 | gl_check "copying to texture %dx%d if=%x", |
221 | gl_check "copying to texture %dx%d if=%x", |
170 | $tw, $th, $self->{internalformat}; |
222 | $tw, $th, $self->{internalformat}; |
|
|
223 | } else { |
|
|
224 | glTexImage2D GL_TEXTURE_2D, 0, |
|
|
225 | $self->{internalformat}, |
|
|
226 | $dw, $dh, |
|
|
227 | 0, |
|
|
228 | $self->{format}, |
|
|
229 | $self->{type}, |
|
|
230 | $data; |
|
|
231 | gl_check "uploading texture %dx%d if=%x f=%x t=%x", |
|
|
232 | $tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; |
171 | } |
233 | } |
|
|
234 | |
|
|
235 | $self->{s} = $rw / $tw; |
|
|
236 | $self->{t} = $rh / $th; |
172 | } |
237 | } |
|
|
238 | |
|
|
239 | sub shutdown { |
|
|
240 | my ($self) = @_; |
|
|
241 | |
|
|
242 | glDeleteTexture $self->{name} |
|
|
243 | if $self->{name}; |
|
|
244 | } |
173 | |
245 | |
174 | sub DESTROY { |
246 | sub DESTROY { |
175 | my ($self) = @_; |
247 | my ($self) = @_; |
176 | |
248 | |
177 | delete $TEXTURES{$self+0}; |
249 | delete $TEXTURES{$self+0}; |
178 | |
250 | |
179 | glDeleteTexture delete $self->{name} |
251 | $self->shutdown; |
180 | if $self->{name}; |
|
|
181 | } |
252 | } |
182 | |
253 | |
183 | $CFClient::OpenGL::INIT_HOOK{"CFClient::Texture"} = sub { |
254 | $CFPlus::OpenGL::INIT_HOOK{"CFPlus::Texture"} = sub { |
|
|
255 | # first mark all existing texture names as in-use, in case the context lost textures |
|
|
256 | glBindTexture GL_TEXTURE_2D, $_->{name} |
|
|
257 | for values %TEXTURES; |
184 | $_->upload |
258 | $_->upload |
185 | for values %TEXTURES; |
259 | for values %TEXTURES; |
186 | }; |
260 | }; |
187 | |
261 | |
|
|
262 | $CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Texture"} = sub { |
|
|
263 | $_->shutdown |
|
|
264 | for values %TEXTURES; |
|
|
265 | }; |
|
|
266 | |
188 | 1; |
267 | 1; |
189 | |
268 | |
190 | =back |
269 | =back |
191 | |
270 | |
192 | =head1 AUTHOR |
271 | =head1 AUTHOR |