1 |
root |
1.1 |
=head1 NAME |
2 |
|
|
|
3 |
|
|
CFClient::Texture - tetxure class for CFClient |
4 |
|
|
|
5 |
|
|
=head1 SYNOPSIS |
6 |
|
|
|
7 |
|
|
use CFClient::Texture; |
8 |
|
|
|
9 |
|
|
=head1 DESCRIPTION |
10 |
|
|
|
11 |
|
|
=over 4 |
12 |
|
|
|
13 |
|
|
=cut |
14 |
|
|
|
15 |
|
|
package CFClient::Texture; |
16 |
|
|
|
17 |
|
|
use strict; |
18 |
|
|
|
19 |
|
|
use Scalar::Util; |
20 |
|
|
|
21 |
|
|
use CFClient::OpenGL; |
22 |
|
|
|
23 |
|
|
my %TEXTURES; |
24 |
|
|
|
25 |
|
|
sub new { |
26 |
|
|
my ($class, %data) = @_; |
27 |
|
|
|
28 |
|
|
my $self = bless { |
29 |
|
|
internalformat => GL_RGBA, |
30 |
|
|
format => GL_RGBA, |
31 |
|
|
type => GL_UNSIGNED_BYTE, |
32 |
|
|
%data, |
33 |
|
|
}, $class; |
34 |
|
|
|
35 |
|
|
Scalar::Util::weaken ($TEXTURES{$self+0} = $self); |
36 |
|
|
|
37 |
|
|
$self->upload; |
38 |
|
|
|
39 |
|
|
$self |
40 |
|
|
} |
41 |
|
|
|
42 |
|
|
sub new_from_image { |
43 |
|
|
my ($class, $image, %arg) = @_; |
44 |
|
|
|
45 |
|
|
$class->new (image => $image, %arg) |
46 |
|
|
} |
47 |
|
|
|
48 |
|
|
sub new_from_file { |
49 |
|
|
my ($class, $path, %arg) = @_; |
50 |
|
|
|
51 |
|
|
open my $fh, "<:raw", $path |
52 |
|
|
or die "$path: $!"; |
53 |
|
|
|
54 |
|
|
local $/; |
55 |
|
|
$class->new_from_image (<$fh>, %arg) |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
#sub new_from_surface { |
59 |
|
|
# my ($class, $surface) = @_; |
60 |
|
|
# |
61 |
|
|
# $surface->rgba; |
62 |
|
|
# |
63 |
|
|
# $class->new ( |
64 |
|
|
# data => $surface->pixels, |
65 |
|
|
# w => $surface->width, |
66 |
|
|
# h => $surface->height, |
67 |
|
|
# ) |
68 |
|
|
#} |
69 |
|
|
|
70 |
|
|
sub new_from_layout { |
71 |
|
|
my ($class, $layout, %arg) = @_; |
72 |
|
|
|
73 |
|
|
my ($w, $h, $data, $format, $internalformat) = $layout->render; |
74 |
|
|
|
75 |
|
|
$class->new ( |
76 |
|
|
w => $w, |
77 |
|
|
h => $h, |
78 |
|
|
data => $data, |
79 |
|
|
format => $format, |
80 |
|
|
internalformat => $format, |
81 |
|
|
type => GL_UNSIGNED_BYTE, |
82 |
|
|
%arg, |
83 |
|
|
) |
84 |
|
|
} |
85 |
|
|
|
86 |
|
|
sub new_from_opengl { |
87 |
|
|
my ($class, $w, $h, $cb) = @_; |
88 |
|
|
|
89 |
|
|
$class->new (w => $w || 1, h => $h || 1, render_cb => $cb) |
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 |
|
|
} |
95 |
|
|
|
96 |
|
|
sub upload { |
97 |
|
|
my ($self) = @_; |
98 |
|
|
|
99 |
|
|
return unless $GL_VERSION; |
100 |
|
|
|
101 |
|
|
my $data; |
102 |
|
|
|
103 |
|
|
if (exists $self->{data}) { |
104 |
|
|
$data = $self->{data}; |
105 |
|
|
|
106 |
|
|
} elsif (exists $self->{render_cb}) { |
107 |
|
|
glViewport 0, 0, $self->{w}, $self->{h}; |
108 |
|
|
glMatrixMode GL_PROJECTION; |
109 |
|
|
glLoadIdentity; |
110 |
|
|
glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000; |
111 |
|
|
glMatrixMode GL_MODELVIEW; |
112 |
|
|
glLoadIdentity; |
113 |
|
|
$self->{render_cb}->($self, $self->{w}, $self->{h}); |
114 |
|
|
|
115 |
|
|
} else { |
116 |
|
|
($self->{w}, $self->{h}, $data, $self->{internalformat}, $self->{format}, $self->{type}) |
117 |
|
|
= CFClient::load_image_inline $self->{image}; |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
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 |
|
|
|
127 |
root |
1.3 |
$self->{minified} ||= [CFClient::average $tw, $th, $data] |
128 |
root |
1.1 |
if $self->{minify}; |
129 |
|
|
|
130 |
|
|
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 |
|
|
|
143 |
|
|
$self->{s} = $self->{w} / $tw; |
144 |
|
|
$self->{t} = $self->{h} / $th; |
145 |
|
|
|
146 |
|
|
$self->{name} ||= glGenTexture; |
147 |
|
|
|
148 |
|
|
glBindTexture GL_TEXTURE_2D, $self->{name}; |
149 |
|
|
|
150 |
root |
1.2 |
if ($self->{wrap}) { |
151 |
|
|
glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
152 |
|
|
glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
153 |
|
|
} else { |
154 |
|
|
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; |
156 |
|
|
} |
157 |
root |
1.1 |
|
158 |
|
|
if ($::FAST) { |
159 |
|
|
glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; |
160 |
|
|
glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST; |
161 |
|
|
} elsif ($self->{mipmap} && $GL_VERSION >= 1.4) { |
162 |
|
|
glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1; |
163 |
|
|
glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
164 |
|
|
glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR; |
165 |
|
|
} else { |
166 |
|
|
glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, $self->{mipmap}; |
167 |
|
|
glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
168 |
|
|
glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR; |
169 |
|
|
} |
170 |
|
|
|
171 |
|
|
glGetError; |
172 |
|
|
|
173 |
|
|
if (defined $data) { |
174 |
|
|
glTexImage2D GL_TEXTURE_2D, 0, |
175 |
|
|
$self->{internalformat}, |
176 |
|
|
$tw, $th, |
177 |
|
|
0, |
178 |
|
|
$self->{format}, |
179 |
|
|
$self->{type}, |
180 |
|
|
$data; |
181 |
|
|
gl_check "uploading texture %dx%d if=%x f=%x t=%x", |
182 |
|
|
$tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; |
183 |
|
|
} else { |
184 |
|
|
glCopyTexImage2D GL_TEXTURE_2D, 0, |
185 |
|
|
$self->{internalformat}, |
186 |
|
|
0, 0, |
187 |
|
|
$tw, $th, |
188 |
|
|
0; |
189 |
|
|
gl_check "copying to texture %dx%d if=%x", |
190 |
|
|
$tw, $th, $self->{internalformat}; |
191 |
|
|
} |
192 |
|
|
|
193 |
|
|
glBindTexture GL_TEXTURE_2D, 0; # just to be on the safe side |
194 |
|
|
} |
195 |
|
|
|
196 |
|
|
sub DESTROY { |
197 |
|
|
my ($self) = @_; |
198 |
|
|
|
199 |
|
|
delete $TEXTURES{$self+0}; |
200 |
|
|
|
201 |
|
|
glDeleteTexture delete $self->{name} |
202 |
|
|
if $self->{name}; |
203 |
|
|
} |
204 |
|
|
|
205 |
|
|
$CFClient::OpenGL::INIT_HOOK{"CFClient::Texture"} = sub { |
206 |
|
|
$_->upload |
207 |
|
|
for values %TEXTURES; |
208 |
|
|
}; |
209 |
|
|
|
210 |
|
|
1; |
211 |
|
|
|
212 |
|
|
=back |
213 |
|
|
|
214 |
|
|
=head1 AUTHOR |
215 |
|
|
|
216 |
|
|
Marc Lehmann <schmorp@schmorp.de> |
217 |
|
|
http://home.schmorp.de/ |
218 |
|
|
|
219 |
|
|
=cut |
220 |
|
|
|