ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Texture.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/Texture.pm (file contents):
Revision 1.4 by root, Tue Jun 6 02:55:50 2006 UTC vs.
Revision 1.23 by root, Tue Jul 24 01:24:27 2007 UTC

1=head1 NAME 1=head1 NAME
2 2
3CFClient::Texture - tetxure class for CFClient 3CFPlus::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
15package CFClient::Texture; 15package CFPlus::Texture;
16 16
17use strict; 17use strict;
18 18
19use Scalar::Util; 19use List::Util qw(max min);
20
21use CFClient::OpenGL; 20use CFPlus::OpenGL;
22 21
23my %TEXTURES; 22my %TEXTURES;
23my ($MAX_W, $MAX_H) = (4096, 4096); # maximum texture size attempted by this module
24 24
25sub new { 25sub 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
42sub new_from_image { 42sub 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
48sub new_from_file { 48sub 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
70sub 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
86sub new_from_opengl { 86sub 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
92sub 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
96sub upload { 92sub upload {
97 my ($self) = @_; 93 my ($self) = @_;
98 94
99 return unless $GL_VERSION; 95 return unless $GL_VERSION;
100 96
101 my $data; 97 # $tw,$th texture
98 # $rw,$rh rendered/used size
99 # $dw,$dh $data
100
101 my ($data, $dw, $dh);
102 102
103 if (exists $self->{data}) { 103 if (exists $self->{data}) {
104 $data = $self->{data}; 104 $data = $self->{data};
105 ($dw, $dh) = @$self{qw(w h)};
105 106
106 } elsif (exists $self->{render_cb}) { 107 } elsif (exists $self->{render_cb}) {
107 glViewport 0, 0, $self->{w}, $self->{h}; 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 ($ox, $oy); # area shift to lessen effetc of buggy opengl implementations (nvida, ati)
146 my $render;
147
148 if ($self->{render_cb}) {
149 # use only part of the texture
150 #$rw >>= 1 while $rw > $tw;
151 #$rh >>= 1 while $rh > $th;
152 $rw = min $dw, $tw;
153 $rh = min $dh, $th;
154 ++$render;
155 } else {
156 if ($self->{wrap} || $tw < $dw || $th < $dh) {
157 # scale to the full texture size
158 ($rw, $rh) = ($tw, $th);
159 ++$render;
160 } else {
161 # pad
162 pad $data, $dw, $dh, $tw, $th;
163 ($rw, $rh) = ($dw, $dh);
164 ($dw, $dh) = ($tw, $th);
165 }
166 }
167
168 if ($render) {
169 $ox = int .5 * ($::WIDTH - $rw);
170 $oy = int .5 * ($::HEIGHT - $rh);
171
172 glViewport $ox, $oy, $tw, $th;
173 #glScissor 0, 0, $tw, $th;
174 #glEnable GL_SCISSOR_TEST;
108 glMatrixMode GL_PROJECTION; 175 glMatrixMode GL_PROJECTION;
109 glLoadIdentity; 176 glLoadIdentity;
110 glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000; 177 glOrtho 0, $tw, 0, $th, -10000, 10000;
111 glMatrixMode GL_MODELVIEW; 178 glMatrixMode GL_MODELVIEW;
112 glLoadIdentity; 179 glLoadIdentity;
180
181 if ($self->{render_cb}) {
182 glScale $rw / $dw, $rh / $dh;
113 $self->{render_cb}->($self, $self->{w}, $self->{h}); 183 $self->{render_cb}->($self, $rw, $rh);
114
115 } else { 184 } else {
116 ($self->{w}, $self->{h}, $data, $self->{internalformat}, $self->{format}, $self->{type}) 185 glClearColor 0, 0, 0, 0;
117 = CFClient::load_image_inline $self->{image}; 186 glClear GL_COLOR_BUFFER_BIT;
118 } 187 glPixelZoom $tw / $dw, $th / $dh;
119 188 glRasterPos 0, 0;
120 my ($tw, $th) = @$self{qw(w h)}; 189 glDrawPixels $dw, $dh,
121 190 $self->{format},
122 unless ($tw > 0 && $th > 0) { 191 $self->{type},
123 $tw = $th = 1; 192 $data;
124 $data = "\x00" x 64; 193 glPixelZoom 1, 1;
125 }
126
127 $self->{minified} ||= [CFClient::average $tw, $th, $data]
128 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 } 194 }
141 } 195 }
142 196
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}; 197 glBindTexture GL_TEXTURE_2D, $self->{name} ||= glGenTexture;
149 198
150 if ($self->{wrap}) { 199 if ($self->{wrap}) {
151 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 200 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
152 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 201 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
153 } else { 202 } else {
154 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, $GL_VERSION >= 1.2 ? GL_CLAMP_TO_EDGE : GL_CLAMP; 203 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; 204 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, $GL_VERSION >= 1.2 ? GL_CLAMP_TO_EDGE : GL_CLAMP;
156 } 205 }
157 206
158 if ($::FAST) { 207 if ($::FAST || $self->{nearest}) {
159 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; 208 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST;
160 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST; 209 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST;
161 } elsif ($self->{mipmap} && $GL_VERSION >= 1.4) { 210 } elsif ($self->{mipmap} && $GL_VERSION >= 1.4) {
162 glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1; 211 glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1;
163 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; 212 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
168 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR; 217 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;
169 } 218 }
170 219
171 glGetError; 220 glGetError;
172 221
173 if (defined $data) { 222 if ($render) {
223 glCopyTexImage2D GL_TEXTURE_2D, 0,
224 $self->{internalformat},
225 $ox, $oy,
226 $tw, $th,
227 0;
228 gl_check "copying to texture %dx%d if=%x",
229 $tw, $th, $self->{internalformat};
230
231 #glDisable GL_SCISSOR_TEST;
232 } else {
174 glTexImage2D GL_TEXTURE_2D, 0, 233 glTexImage2D GL_TEXTURE_2D, 0,
175 $self->{internalformat}, 234 $self->{internalformat},
176 $tw, $th, 235 $dw, $dh,
177 0, 236 0,
178 $self->{format}, 237 $self->{format},
179 $self->{type}, 238 $self->{type},
180 $data; 239 $data;
181 gl_check "uploading texture %dx%d if=%x f=%x t=%x", 240 gl_check "uploading texture %dx%d if=%x f=%x t=%x",
182 $tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; 241 $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 } 242 }
192 243
193 glBindTexture GL_TEXTURE_2D, 0; # just to be on the safe side 244 $self->{s} = $rw / $tw;
245 $self->{t} = $rh / $th;
194} 246}
247
248sub shutdown {
249 my ($self) = @_;
250
251 glDeleteTexture $self->{name}
252 if $self->{name};
253}
195 254
196sub DESTROY { 255sub DESTROY {
197 my ($self) = @_; 256 my ($self) = @_;
198 257
199 delete $TEXTURES{$self+0}; 258 delete $TEXTURES{$self+0};
200 259
201 glDeleteTexture delete $self->{name} 260 $self->shutdown;
202 if $self->{name};
203} 261}
204 262
205$CFClient::OpenGL::INIT_HOOK{"CFClient::Texture"} = sub { 263$CFPlus::OpenGL::INIT_HOOK{"CFPlus::Texture"} = sub {
264 # first mark all existing texture names as in-use, in case the context lost textures
265 glBindTexture GL_TEXTURE_2D, $_->{name}
266 for values %TEXTURES;
206 $_->upload 267 $_->upload
207 for values %TEXTURES; 268 for values %TEXTURES;
208}; 269};
209 270
271$CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Texture"} = sub {
272 $_->shutdown
273 for values %TEXTURES;
274};
275
2101; 2761;
211 277
212=back 278=back
213 279
214=head1 AUTHOR 280=head1 AUTHOR

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines