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; |
|
|
20 | |
|
|
21 | use CFClient::OpenGL; |
19 | use CFPlus::OpenGL; |
22 | |
20 | |
23 | my %TEXTURES; |
21 | my %TEXTURES; |
24 | |
22 | |
25 | sub new { |
23 | sub new { |
26 | my ($class, %data) = @_; |
24 | my ($class, %data) = @_; |
… | |
… | |
30 | format => GL_RGBA, |
28 | format => GL_RGBA, |
31 | type => GL_UNSIGNED_BYTE, |
29 | type => GL_UNSIGNED_BYTE, |
32 | %data, |
30 | %data, |
33 | }, $class; |
31 | }, $class; |
34 | |
32 | |
35 | Scalar::Util::weaken ($TEXTURES{$self+0} = $self); |
33 | CFPlus::weaken ($TEXTURES{$self+0} = $self); |
36 | |
34 | |
37 | $self->upload; |
35 | $self->upload; |
38 | |
36 | |
39 | $self |
37 | $self |
40 | } |
38 | } |
41 | |
39 | |
42 | sub new_from_image { |
40 | sub new_from_image { |
43 | my ($class, $image, %arg) = @_; |
41 | my ($class, $image, %arg) = @_; |
44 | |
42 | |
45 | $class->new (image => $image, %arg) |
43 | $class->new (image => $image, internalformat => undef, %arg) |
46 | } |
44 | } |
47 | |
45 | |
48 | sub new_from_file { |
46 | sub new_from_file { |
49 | my ($class, $path, %arg) = @_; |
47 | my ($class, $path, %arg) = @_; |
50 | |
48 | |
… | |
… | |
65 | # w => $surface->width, |
63 | # w => $surface->width, |
66 | # h => $surface->height, |
64 | # h => $surface->height, |
67 | # ) |
65 | # ) |
68 | #} |
66 | #} |
69 | |
67 | |
70 | sub new_from_layout { |
68 | #sub new_from_layout { |
71 | my ($class, $layout, %arg) = @_; |
69 | # my ($class, $layout, %arg) = @_; |
72 | |
70 | # |
73 | my ($w, $h, $data, $format, $internalformat) = $layout->render; |
71 | # my ($w, $h, $data, $format, $internalformat) = $layout->render; |
74 | |
72 | # |
75 | $class->new ( |
73 | # $class->new ( |
76 | w => $w, |
74 | # w => $w, |
77 | h => $h, |
75 | # h => $h, |
78 | data => $data, |
76 | # data => $data, |
79 | format => $format, |
77 | # format => $format, |
80 | internalformat => $format, |
78 | # internalformat => $format, |
81 | type => GL_UNSIGNED_BYTE, |
79 | # type => GL_UNSIGNED_BYTE, |
82 | %arg, |
80 | # %arg, |
83 | ) |
81 | # ) |
84 | } |
82 | #} |
85 | |
83 | |
86 | sub new_from_opengl { |
84 | sub new_from_opengl { |
87 | my ($class, $w, $h, $cb) = @_; |
85 | my ($class, $w, $h, $cb) = @_; |
88 | |
86 | |
89 | $class->new (w => $w || 1, h => $h || 1, render_cb => $cb) |
87 | $class->new (w => $w || 1, h => $h || 1, render_cb => $cb, nearest => 1) |
90 | } |
|
|
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 | } |
88 | } |
95 | |
89 | |
96 | sub upload { |
90 | sub upload { |
97 | my ($self) = @_; |
91 | my ($self) = @_; |
98 | |
92 | |
… | |
… | |
111 | glMatrixMode GL_MODELVIEW; |
105 | glMatrixMode GL_MODELVIEW; |
112 | glLoadIdentity; |
106 | glLoadIdentity; |
113 | $self->{render_cb}->($self, $self->{w}, $self->{h}); |
107 | $self->{render_cb}->($self, $self->{w}, $self->{h}); |
114 | |
108 | |
115 | } else { |
109 | } else { |
116 | ($self->{w}, $self->{h}, $data, $self->{internalformat}, $self->{format}, $self->{type}) |
110 | ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type}) |
117 | = CFClient::load_image_inline $self->{image}; |
111 | = CFPlus::load_image_inline $self->{image}; |
|
|
112 | |
|
|
113 | $self->{internalformat} ||= $internalformat; |
118 | } |
114 | } |
119 | |
115 | |
120 | my ($tw, $th) = @$self{qw(w h)}; |
116 | my ($tw, $th) = @$self{qw(w h)}; |
121 | |
117 | |
122 | unless ($tw > 0 && $th > 0) { |
|
|
123 | $tw = $th = 1; |
|
|
124 | $data = "\x00" x 64; |
|
|
125 | } |
|
|
126 | |
|
|
127 | $self->{minified} ||= [CFClient::average $tw, $th, $data] |
118 | $self->{minified} ||= [CFPlus::average $tw, $th, $data] |
128 | if $self->{minify}; |
119 | if $self->{minify}; |
129 | |
120 | |
130 | unless ($GL_NPOT) { |
121 | 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 | |
122 | |
143 | $self->{s} = $self->{w} / $tw; |
123 | $self->{s} = $self->{w} / $tw; |
144 | $self->{t} = $self->{h} / $th; |
124 | $self->{t} = $self->{h} / $th; |
145 | |
125 | |
146 | $self->{name} ||= glGenTexture; |
126 | $self->{name} ||= glGenTexture; |
… | |
… | |
153 | } else { |
133 | } else { |
154 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, $GL_VERSION >= 1.2 ? GL_CLAMP_TO_EDGE : GL_CLAMP; |
134 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, $GL_VERSION >= 1.2 ? GL_CLAMP_TO_EDGE : GL_CLAMP; |
155 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, $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; |
156 | } |
136 | } |
157 | |
137 | |
158 | if ($::FAST) { |
138 | if ($::FAST || $self->{nearest}) { |
159 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; |
139 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; |
160 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST; |
140 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST; |
161 | } elsif ($self->{mipmap} && $GL_VERSION >= 1.4) { |
141 | } elsif ($self->{mipmap} && $GL_VERSION >= 1.4) { |
162 | glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1; |
142 | glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1; |
163 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
143 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
… | |
… | |
179 | $self->{type}, |
159 | $self->{type}, |
180 | $data; |
160 | $data; |
181 | gl_check "uploading texture %dx%d if=%x f=%x t=%x", |
161 | gl_check "uploading texture %dx%d if=%x f=%x t=%x", |
182 | $tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; |
162 | $tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; |
183 | } else { |
163 | } else { |
|
|
164 | exists $self->{render_cb} or die; |
184 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
165 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
185 | $self->{internalformat}, |
166 | $self->{internalformat}, |
186 | 0, 0, |
167 | 0, 0, |
187 | $tw, $th, |
168 | $tw, $th, |
188 | 0; |
169 | 0; |
189 | gl_check "copying to texture %dx%d if=%x", |
170 | gl_check "copying to texture %dx%d if=%x", |
190 | $tw, $th, $self->{internalformat}; |
171 | $tw, $th, $self->{internalformat}; |
191 | } |
172 | } |
192 | |
|
|
193 | glBindTexture GL_TEXTURE_2D, 0; # just to be on the safe side |
|
|
194 | } |
173 | } |
|
|
174 | |
|
|
175 | sub shutdown { |
|
|
176 | my ($self) = @_; |
|
|
177 | |
|
|
178 | glDeleteTexture $self->{name} |
|
|
179 | if $self->{name}; |
|
|
180 | } |
195 | |
181 | |
196 | sub DESTROY { |
182 | sub DESTROY { |
197 | my ($self) = @_; |
183 | my ($self) = @_; |
198 | |
184 | |
199 | delete $TEXTURES{$self+0}; |
185 | delete $TEXTURES{$self+0}; |
200 | |
186 | |
201 | glDeleteTexture delete $self->{name} |
187 | $self->shutdown; |
202 | if $self->{name}; |
|
|
203 | } |
188 | } |
204 | |
189 | |
205 | $CFClient::OpenGL::INIT_HOOK{"CFClient::Texture"} = sub { |
190 | $CFPlus::OpenGL::INIT_HOOK{"CFPlus::Texture"} = sub { |
|
|
191 | # first mark all existing texture names as in-use, in case the context lost textures |
|
|
192 | glBindTexture GL_TEXTURE_2D, $_->{name} |
|
|
193 | for values %TEXTURES; |
206 | $_->upload |
194 | $_->upload |
207 | for values %TEXTURES; |
195 | for values %TEXTURES; |
208 | }; |
196 | }; |
209 | |
197 | |
|
|
198 | $CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Texture"} = sub { |
|
|
199 | $_->shutdown |
|
|
200 | for values %TEXTURES; |
|
|
201 | }; |
|
|
202 | |
210 | 1; |
203 | 1; |
211 | |
204 | |
212 | =back |
205 | =back |
213 | |
206 | |
214 | =head1 AUTHOR |
207 | =head1 AUTHOR |