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 Scalar::Util; |
20 | |
20 | |
21 | use CFClient::OpenGL; |
21 | use CFPlus::OpenGL; |
22 | |
22 | |
23 | my %TEXTURES; |
23 | my %TEXTURES; |
24 | |
24 | |
25 | sub new { |
25 | sub new { |
26 | my ($class, %data) = @_; |
26 | my ($class, %data) = @_; |
… | |
… | |
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 | } |
|
|
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 | } |
90 | } |
95 | |
91 | |
96 | sub upload { |
92 | sub upload { |
97 | my ($self) = @_; |
93 | my ($self) = @_; |
98 | |
94 | |
… | |
… | |
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 | = CFPlus::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 | |
119 | |
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] |
120 | $self->{minified} ||= [CFPlus::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; |
… | |
… | |
153 | } else { |
135 | } else { |
154 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, $GL_VERSION >= 1.2 ? GL_CLAMP_TO_EDGE : GL_CLAMP; |
136 | 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; |
137 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, $GL_VERSION >= 1.2 ? GL_CLAMP_TO_EDGE : GL_CLAMP; |
156 | } |
138 | } |
157 | |
139 | |
158 | if ($::FAST) { |
140 | if ($::FAST || $self->{nearest}) { |
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 | glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1; |
144 | glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1; |
163 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
145 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
… | |
… | |
179 | $self->{type}, |
161 | $self->{type}, |
180 | $data; |
162 | $data; |
181 | gl_check "uploading texture %dx%d if=%x f=%x t=%x", |
163 | gl_check "uploading texture %dx%d if=%x f=%x t=%x", |
182 | $tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; |
164 | $tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; |
183 | } else { |
165 | } else { |
|
|
166 | exists $self->{render_cb} or die; |
184 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
167 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
185 | $self->{internalformat}, |
168 | $self->{internalformat}, |
186 | 0, 0, |
169 | 0, 0, |
187 | $tw, $th, |
170 | $tw, $th, |
188 | 0; |
171 | 0; |
189 | gl_check "copying to texture %dx%d if=%x", |
172 | gl_check "copying to texture %dx%d if=%x", |
190 | $tw, $th, $self->{internalformat}; |
173 | $tw, $th, $self->{internalformat}; |
191 | } |
174 | } |
192 | |
|
|
193 | glBindTexture GL_TEXTURE_2D, 0; # just to be on the safe side |
|
|
194 | } |
175 | } |
|
|
176 | |
|
|
177 | sub shutdown { |
|
|
178 | my ($self) = @_; |
|
|
179 | |
|
|
180 | glDeleteTexture $self->{name} |
|
|
181 | if $self->{name}; |
|
|
182 | } |
195 | |
183 | |
196 | sub DESTROY { |
184 | sub DESTROY { |
197 | my ($self) = @_; |
185 | my ($self) = @_; |
198 | |
186 | |
199 | delete $TEXTURES{$self+0}; |
187 | delete $TEXTURES{$self+0}; |
200 | |
188 | |
201 | glDeleteTexture delete $self->{name} |
189 | $self->shutdown; |
202 | if $self->{name}; |
|
|
203 | } |
190 | } |
204 | |
191 | |
205 | $CFClient::OpenGL::INIT_HOOK{"CFClient::Texture"} = sub { |
192 | $CFPlus::OpenGL::INIT_HOOK{"CFPlus::Texture"} = sub { |
|
|
193 | # first mark all existing texture names as in-use, in case the context lost textures |
|
|
194 | glBindTexture GL_TEXTURE_2D, $_->{name} |
|
|
195 | for values %TEXTURES; |
206 | $_->upload |
196 | $_->upload |
207 | for values %TEXTURES; |
197 | for values %TEXTURES; |
208 | }; |
198 | }; |
209 | |
199 | |
|
|
200 | $CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Texture"} = sub { |
|
|
201 | $_->shutdown |
|
|
202 | for values %TEXTURES; |
|
|
203 | }; |
|
|
204 | |
210 | 1; |
205 | 1; |
211 | |
206 | |
212 | =back |
207 | =back |
213 | |
208 | |
214 | =head1 AUTHOR |
209 | =head1 AUTHOR |