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 | } |
… | |
… | |
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 | } |
88 | } |
91 | |
89 | |
92 | sub upload { |
90 | sub upload { |
93 | my ($self) = @_; |
91 | my ($self) = @_; |
94 | |
92 | |
… | |
… | |
108 | glLoadIdentity; |
106 | glLoadIdentity; |
109 | $self->{render_cb}->($self, $self->{w}, $self->{h}); |
107 | $self->{render_cb}->($self, $self->{w}, $self->{h}); |
110 | |
108 | |
111 | } else { |
109 | } else { |
112 | ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type}) |
110 | ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type}) |
113 | = CFClient::load_image_inline $self->{image}; |
111 | = CFPlus::load_image_inline $self->{image}; |
114 | |
112 | |
115 | $self->{internalformat} ||= $internalformat; |
113 | $self->{internalformat} ||= $internalformat; |
116 | } |
114 | } |
117 | |
115 | |
118 | my ($tw, $th) = @$self{qw(w h)}; |
116 | my ($tw, $th) = @$self{qw(w h)}; |
119 | |
117 | |
120 | $self->{minified} ||= [CFClient::average $tw, $th, $data] |
118 | $self->{minified} ||= [CFPlus::average $tw, $th, $data] |
121 | if $self->{minify}; |
119 | if $self->{minify}; |
122 | |
120 | |
123 | pad2pot $data, $tw, $th unless $GL_NPOT; |
121 | pad2pot $data, $tw, $th unless $GL_NPOT; |
124 | |
122 | |
125 | $self->{s} = $self->{w} / $tw; |
123 | $self->{s} = $self->{w} / $tw; |
… | |
… | |
135 | } else { |
133 | } else { |
136 | 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; |
137 | 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; |
138 | } |
136 | } |
139 | |
137 | |
140 | if ($::FAST) { |
138 | if ($::FAST || $self->{nearest}) { |
141 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; |
139 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; |
142 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST; |
140 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST; |
143 | } elsif ($self->{mipmap} && $GL_VERSION >= 1.4) { |
141 | } elsif ($self->{mipmap} && $GL_VERSION >= 1.4) { |
144 | glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1; |
142 | glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1; |
145 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
143 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
… | |
… | |
187 | delete $TEXTURES{$self+0}; |
185 | delete $TEXTURES{$self+0}; |
188 | |
186 | |
189 | $self->shutdown; |
187 | $self->shutdown; |
190 | } |
188 | } |
191 | |
189 | |
192 | $CFClient::OpenGL::INIT_HOOK{"CFClient::Texture"} = sub { |
190 | $CFPlus::OpenGL::INIT_HOOK{"CFPlus::Texture"} = sub { |
193 | # first mark all existing texture names as in-use, in case the context lost textures |
191 | # first mark all existing texture names as in-use, in case the context lost textures |
194 | glBindTexture GL_TEXTURE_2D, $_->{name} |
192 | glBindTexture GL_TEXTURE_2D, $_->{name} |
195 | for values %TEXTURES; |
193 | for values %TEXTURES; |
196 | $_->upload |
194 | $_->upload |
197 | for values %TEXTURES; |
195 | for values %TEXTURES; |
198 | }; |
196 | }; |
199 | |
197 | |
200 | $CFClient::OpenGL::SHUTDOWN_HOOK{"CFClient::Texture"} = sub { |
198 | $CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Texture"} = sub { |
201 | $_->shutdown |
199 | $_->shutdown |
202 | for values %TEXTURES; |
200 | for values %TEXTURES; |
203 | }; |
201 | }; |
204 | |
202 | |
205 | 1; |
203 | 1; |