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.19 by root, Thu Jul 19 22:10:46 2007 UTC vs.
Revision 1.25 by root, Mon Aug 6 05:06:33 2007 UTC

32 %data, 32 %data,
33 }, $class; 33 }, $class;
34 34
35 CFPlus::weaken ($TEXTURES{$self+0} = $self); 35 CFPlus::weaken ($TEXTURES{$self+0} = $self);
36 36
37 $self->upload; 37 $self->upload
38 unless delete $self->{delay};
38 39
39 $self 40 $self
40} 41}
41 42
42sub new_from_image { 43sub new_from_image {
43 my ($class, $image, %arg) = @_; 44 my ($class, $image, %arg) = @_;
44 45
46 Carp::confess "tried to create texture from undefined image"
47 unless defined $image;
48
45 $class->new (image => $image, internalformat => undef, %arg) 49 $class->new (image => $image, internalformat => undef, %arg)
46} 50}
51
47 52
48sub new_from_file { 53sub new_from_file {
49 my ($class, $path, %arg) = @_; 54 my ($class, $path, %arg) = @_;
50 55
51 open my $fh, "<:raw", $path 56 open my $fh, "<:raw", $path
87 my ($class, $w, $h, $cb) = @_; 92 my ($class, $w, $h, $cb) = @_;
88 93
89 $class->new (w => $w || 1, h => $h || 1, render_cb => $cb, nearest => 1) 94 $class->new (w => $w || 1, h => $h || 1, render_cb => $cb, nearest => 1)
90} 95}
91 96
97sub loading_done($$) {
98 my ($self, $data) = @_;
99
100 delete $self->{loading};
101 ++$self->{delete_image};
102 $self->{image} = $data;
103 $self->upload;
104}
105
92sub upload { 106sub upload {
93 my ($self) = @_; 107 my ($self, $cb) = @_;
94 108
109 push @{ $self->{upload_done} }, $cb
110 if $cb;
111
112 return if $self->{loading};
113
95 return unless $GL_VERSION; 114 unless ($GL_VERSION) {
96 115 $self->{was_loaded} = 1;
97 # $tw,$th texture 116 return;
98 # $rw,$rh rendered/used size
99 # $dw,$dh $data
100
101 my ($data, $dw, $dh);
102
103 if (exists $self->{data}) {
104 $data = $self->{data};
105 ($dw, $dh) = @$self{qw(w h)};
106
107 } elsif (exists $self->{render_cb}) {
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 }
117 118
119 unless ($self->{name}) {
120 # $tw,$th texture
121 # $rw,$rh rendered/used size
122 # $dw,$dh $data
123
124 my ($data, $dw, $dh);
125
126 if (exists $self->{data}) {
127 $data = $self->{data};
128 ($dw, $dh) = @$self{qw(w h)};
129
130 } elsif (exists $self->{render_cb}) {
131 ($dw, $dh) = @$self{qw(w h)};
132
133 } elsif (exists $self->{image}) {
134 ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type})
135 = CFPlus::load_image_inline $self->{image};
136
137 $self->{internalformat} ||= $internalformat;
138 ($dw, $dh) = @$self{qw(w h)};
139
140 delete $self->{image} if delete $self->{delete_image};
141
142 } elsif (exists $self->{tile}) {
143 ++$self->{loading};
144 return CFPlus::DB::get tilecache => $self->{tile}, sub {
145 $self->loading_done ($_[0]);
146
147 $::MAP->set_texture ($self->{tile}, @$self{qw(name w h s t)}, @{$self->{minified}})
148 if $::MAP;
149 $::MAPWIDGET->update
150 if $::MAPWIDGET;
151 };
152
153 } elsif (exists $self->{path}) {
154 ++$self->{loading};
155 return CFPlus::DB::read_file $self->{path}, sub {
156 $self->loading_done ($_[0]);
157 };
158
159 } else {
160 Carp::confess "tried to create texture that is not data, render or image";
161 }
162
118 my ($tw, $th) = ($dw, $dh); 163 my ($tw, $th) = ($dw, $dh);
119 164
120 defined $data or $self->{render_cb} or die; # some sanity check 165 defined $data or $self->{render_cb} or die; # some sanity check
121 166
122 $self->{minified} ||= [CFPlus::average $dw, $dh, $data] 167 $self->{minified} ||= [CFPlus::average $dw, $dh, $data]
123 if $self->{minify}; 168 if $self->{minify};
124 169
125 # against rather broken cards we enforce a maximum texture size 170 # against rather broken cards we enforce a maximum texture size
126 $tw = min $MAX_W, minpot $tw; 171 $tw = min $MAX_W, minpot $tw;
127 $th = min $MAX_H, minpot $th; 172 $th = min $MAX_H, minpot $th;
128 173
129 # if only pot-textures are allowed, pot'ify tw/th 174 # if only pot-textures are allowed, pot'ify tw/th
130 unless ($GL_NPOT && 0) {#d# 175 unless ($GL_NPOT && 0) {#d#
131 $tw = minpot $tw; 176 $tw = minpot $tw;
132 $th = minpot $th; 177 $th = minpot $th;
133 } 178 }
134 179
135 # now further decrease texture size until the 180 # now further decrease texture size until the
136 # card does accept it 181 # card does accept it
137 while (!texture_valid_2d $self->{internalformat}, $tw, $th, $self->{format}, $self->{type}) { 182 while (!texture_valid_2d $self->{internalformat}, $tw, $th, $self->{format}, $self->{type}) {
138 # quarter the texture size 183 # quarter the texture size
139 $tw >>= 1; 184 $tw >>= 1;
140 $th >>= 1; 185 $th >>= 1;
141 } 186 }
142 187
143 # decide the amount of space used in the texture 188 # decide the amount of space used in the texture
144 my ($rw, $rh); 189 my ($rw, $rh);
190 my ($ox, $oy); # area shift to lessen effetc of buggy opengl implementations (nvida, ati)
145 my $render; 191 my $render;
146 192
147 if ($self->{render_cb}) { 193 if ($self->{render_cb}) {
148 # use only part of the texture 194 # use only part of the texture
149 #$rw >>= 1 while $rw > $tw; 195 #$rw >>= 1 while $rw > $tw;
150 #$rh >>= 1 while $rh > $th; 196 #$rh >>= 1 while $rh > $th;
151 $rw = min $dw, $tw; 197 $rw = min $dw, $tw;
152 $rh = min $dh, $th; 198 $rh = min $dh, $th;
153 ++$render;
154 } else {
155 if ($self->{wrap} || $tw < $dw || $th < $dh) {
156 # scale to the full texture size
157 ($rw, $rh) = ($tw, $th);
158 ++$render; 199 ++$render;
159 } else { 200 } else {
201 if ($self->{wrap} || $tw < $dw || $th < $dh) {
202 # scale to the full texture size
203 ($rw, $rh) = ($tw, $th);
204 ++$render;
205 } else {
160 # pad 206 # pad
161 pad $data, $dw, $dh, $tw, $th; 207 pad $data, $dw, $dh, $tw, $th;
162 ($rw, $rh) = ($dw, $dh); 208 ($rw, $rh) = ($dw, $dh);
163 ($dw, $dh) = ($tw, $th); 209 ($dw, $dh) = ($tw, $th);
164 } 210 }
165 } 211 }
166 212
167 if ($render) { 213 if ($render) {
214 $ox = int .5 * ($::WIDTH - $rw);
215 $oy = int .5 * ($::HEIGHT - $rh);
216
168 glViewport 0, 0, $tw, $th; 217 glViewport $ox, $oy, $tw, $th;
169 glClear 0, 0, 0, 0; 218 #glScissor 0, 0, $tw, $th;
219 #glEnable GL_SCISSOR_TEST;
170 glMatrixMode GL_PROJECTION; 220 glMatrixMode GL_PROJECTION;
171 glLoadIdentity; 221 glLoadIdentity;
172 glOrtho 0, $tw, 0, $th, -10000, 10000; 222 glOrtho 0, $tw, 0, $th, -10000, 10000;
173 glMatrixMode GL_MODELVIEW; 223 glMatrixMode GL_MODELVIEW;
174 glLoadIdentity; 224 glLoadIdentity;
175 225
176 if ($self->{render_cb}) { 226 if ($self->{render_cb}) {
177 glScale $rw / $dw, $rh / $dh; 227 glScale $rw / $dw, $rh / $dh;
178 $self->{render_cb}->($self, $rw, $rh); 228 $self->{render_cb}->($self, $rw, $rh);
179 } else { 229 } else {
230 glClearColor 0, 0, 0, 0;
231 glClear GL_COLOR_BUFFER_BIT;
180 glPixelZoom $tw / $dw, $th / $dh; 232 glPixelZoom $tw / $dw, $th / $dh;
233 glRasterPos 0, 0;
181 glDrawPixels $dw, $dh, 234 glDrawPixels $dw, $dh,
235 $self->{format},
236 $self->{type},
237 $data;
238 glPixelZoom 1, 1;
239 }
240 }
241
242 glBindTexture GL_TEXTURE_2D, $self->{name} ||= glGenTexture;
243
244 if ($self->{wrap}) {
245 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
246 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
247 } else {
248 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, $GL_VERSION >= 1.2 ? GL_CLAMP_TO_EDGE : GL_CLAMP;
249 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, $GL_VERSION >= 1.2 ? GL_CLAMP_TO_EDGE : GL_CLAMP;
250 }
251
252 if ($::FAST || $self->{nearest}) {
253 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST;
254 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST;
255 } elsif ($self->{mipmap} && $GL_VERSION >= 1.4) {
256 glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1;
257 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
258 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR;
259 } else {
260 glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, $self->{mipmap};
261 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
262 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;
263 }
264
265 glGetError;
266
267 if ($render) {
268 glCopyTexImage2D GL_TEXTURE_2D, 0,
269 $self->{internalformat},
270 $ox, $oy,
271 $tw, $th,
272 0;
273 gl_check "copying to texture %dx%d if=%x",
274 $tw, $th, $self->{internalformat};
275
276 #glDisable GL_SCISSOR_TEST;
277 } else {
278 glTexImage2D GL_TEXTURE_2D, 0,
279 $self->{internalformat},
280 $dw, $dh,
281 0,
182 $self->{format}, 282 $self->{format},
183 $self->{type}, 283 $self->{type},
184 $data; 284 $data;
185 glPixelZoom 1, 1; 285 gl_check "uploading texture %dx%d if=%x f=%x t=%x",
286 $tw, $th, $self->{internalformat}, $self->{format}, $self->{type};
186 } 287 }
288
289 $self->{s} = $rw / $tw;
290 $self->{t} = $rh / $th;
187 } 291 }
188 292
189 glBindTexture GL_TEXTURE_2D, $self->{name} ||= glGenTexture; 293 $_->($self)
190 294 for @{ (delete $self->{upload_done}) || [] };
191 if ($self->{wrap}) {
192 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
193 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
194 } else {
195 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, $GL_VERSION >= 1.2 ? GL_CLAMP_TO_EDGE : GL_CLAMP;
196 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, $GL_VERSION >= 1.2 ? GL_CLAMP_TO_EDGE : GL_CLAMP;
197 }
198
199 if ($::FAST || $self->{nearest}) {
200 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST;
201 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST;
202 } elsif ($self->{mipmap} && $GL_VERSION >= 1.4) {
203 glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1;
204 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
205 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR;
206 } else {
207 glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, $self->{mipmap};
208 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
209 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;
210 }
211
212 glGetError;
213
214 if ($render) {
215 glCopyTexImage2D GL_TEXTURE_2D, 0,
216 $self->{internalformat},
217 0, 0,
218 $tw, $th,
219 0;
220 gl_check "copying to texture %dx%d if=%x",
221 $tw, $th, $self->{internalformat};
222 } else {
223 glTexImage2D GL_TEXTURE_2D, 0,
224 $self->{internalformat},
225 $dw, $dh,
226 0,
227 $self->{format},
228 $self->{type},
229 $data;
230 gl_check "uploading texture %dx%d if=%x f=%x t=%x",
231 $tw, $th, $self->{internalformat}, $self->{format}, $self->{type};
232 }
233
234 $self->{s} = $rw / $tw;
235 $self->{t} = $rh / $th;
236} 295}
237 296
238sub shutdown { 297sub unload {
239 my ($self) = @_; 298 my ($self) = @_;
240 299
241 glDeleteTexture $self->{name} 300 glDeleteTexture delete $self->{name}
242 if $self->{name}; 301 if $self->{name};
243} 302}
244 303
245sub DESTROY { 304sub DESTROY {
246 my ($self) = @_; 305 my ($self) = @_;
247 306
248 delete $TEXTURES{$self+0}; 307 delete $TEXTURES{$self+0};
249 308
250 $self->shutdown; 309 $self->unload;
251} 310}
252 311
253$CFPlus::OpenGL::INIT_HOOK{"CFPlus::Texture"} = sub { 312$CFPlus::OpenGL::INIT_HOOK{"CFPlus::Texture"} = sub {
254 # first mark all existing texture names as in-use, in case the context lost textures
255 glBindTexture GL_TEXTURE_2D, $_->{name}
256 for values %TEXTURES; 313 for (values %TEXTURES) {
314 next unless delete $_->{was_loaded};
257 $_->upload 315 $_->upload;
258 for values %TEXTURES; 316 }
259}; 317};
260 318
261$CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Texture"} = sub { 319$CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Texture"} = sub {
262 $_->shutdown
263 for values %TEXTURES; 320 for (values %TEXTURES) {
321 next unless $_->{name};
322 $_->{was_loaded} = 1;
323 $_->unload;
324 }
264}; 325};
265 326
2661; 3271;
267 328
268=back 329=back

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines