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

Comparing deliantra/Deliantra-Client/DC.pm (file contents):
Revision 1.44 by root, Mon Apr 24 08:22:20 2006 UTC vs.
Revision 1.50 by root, Thu May 11 23:41:45 2006 UTC

28 28
29our %GL_EXT; 29our %GL_EXT;
30our $GL_VERSION; 30our $GL_VERSION;
31 31
32our $GL_NPOT; 32our $GL_NPOT;
33our $GL_DEBUG = 1;
33 34
34sub gl_init { 35sub gl_init {
35 $GL_VERSION = gl_version * 1; 36 $GL_VERSION = gl_version * 1;
36 %GL_EXT = map +($_ => 1), split /\s+/, gl_extensions; 37 %GL_EXT = map +($_ => 1), split /\s+/, gl_extensions;
37 38
38 $GL_NPOT = $GL_EXT{GL_ARB_texture_non_power_of_two} || $GL_VERSION >= 2; 39 $GL_NPOT = $GL_EXT{GL_ARB_texture_non_power_of_two} || $GL_VERSION >= 2;
39 40
40 glEnable GL_TEXTURE_2D;
41 glEnable GL_COLOR_MATERIAL; 41 glDisable GL_COLOR_MATERIAL;
42 glShadeModel GL_FLAT; 42 glShadeModel GL_FLAT;
43 glDisable GL_DITHER;
43 glDisable GL_DEPTH_TEST; 44 glDisable GL_DEPTH_TEST;
44 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
45
46 glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST; 45 glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST;
47 46
48 CFClient::Texture::restore_state (); 47 CFClient::Texture::restore_state ();
48}
49
50sub gl_check {
51 return unless $GL_DEBUG;
52
53 if (my $error = glGetError) {
54 Carp::cluck sprintf "opengl error %x while %s", $error, sprintf @_;
55 }
49} 56}
50 57
51sub find_rcfile($) { 58sub find_rcfile($) {
52 my $path; 59 my $path;
53 60
54 for (@INC) { 61 for (grep !ref, @INC) {
55 $path = "$_/CFClient/resources/$_[0]"; 62 $path = "$_/CFClient/resources/$_[0]";
56 return $path if -r $path; 63 return $path if -r $path;
57 } 64 }
58 65
59 die "FATAL: can't find required file $_[0]\n"; 66 die "FATAL: can't find required file $_[0]\n";
194#} 201#}
195 202
196sub new_from_layout { 203sub new_from_layout {
197 my ($class, $layout, %arg) = @_; 204 my ($class, $layout, %arg) = @_;
198 205
199 my ($w, $h, $data) = $layout->render; 206 my ($w, $h, $data, $format, $internalformat) = $layout->render;
200 207
201 $class->new ( 208 $class->new (
202 w => $w, 209 w => $w,
203 h => $h, 210 h => $h,
204 data => $data, 211 data => $data,
205 format => GL_ALPHA, 212 format => $format,
206 internalformat => GL_ALPHA, 213 internalformat => $format,
207 type => GL_UNSIGNED_BYTE, 214 type => GL_UNSIGNED_BYTE,
208 %arg, 215 %arg,
209 ) 216 )
210} 217}
211 218
212sub new_from_opengl { 219sub new_from_opengl {
213 my ($class, $w, $h, $cb) = @_; 220 my ($class, $w, $h, $cb) = @_;
214 221
215 $class->new (w => $w, h => $h, render_cb => $cb) 222 $class->new (w => $w || 1, h => $h || 1, render_cb => $cb)
216} 223}
217 224
218sub topot { 225sub topot {
219 (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0] 226 (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0]
220} 227}
243 = CFClient::load_image_inline $self->{image}; 250 = CFClient::load_image_inline $self->{image};
244 } 251 }
245 252
246 my ($tw, $th) = @$self{qw(w h)}; 253 my ($tw, $th) = @$self{qw(w h)};
247 254
248 unless ($tw && $th) { 255 unless ($tw > 0 && $th > 0) {
249 $tw = $th = 1; 256 $tw = $th = 1;
250 $data = "\x00" x 64; 257 $data = "\x00" x 64;
251 } 258 }
252 259
253 $self->{minified} = [CFClient::average $tw, $th, $data] 260 $self->{minified} = [CFClient::average $tw, $th, $data]
267 } 274 }
268 275
269 $self->{s} = $self->{w} / $tw; 276 $self->{s} = $self->{w} / $tw;
270 $self->{t} = $self->{h} / $th; 277 $self->{t} = $self->{h} / $th;
271 278
272 glGetError;
273
274 $self->{name} ||= glGenTexture; 279 $self->{name} ||= glGenTexture;
275 280
276 glBindTexture GL_TEXTURE_2D, $self->{name}; 281 glBindTexture GL_TEXTURE_2D, $self->{name};
277 282
278 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; 283 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
279 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; 284 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
280 285
281 if ($::FAST) { 286 if ($::FAST) {
282 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; 287 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST;
283 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST; 288 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST;
289 } elsif ($self->{mipmap} && $GL_VERSION >= 1.4) {
290 # alternatively check for 0x8191
291 glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1;
292 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
293 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR;
284 } else { 294 } else {
285 glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, $self->{mipmap}; 295 glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, $self->{mipmap};
286 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; 296 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
287 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, $self->{mipmap} ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR; 297 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;
288 } 298 }
289 299
300 glGetError;
301
290 if (defined $data) { 302 if (defined $data) {
291 glTexImage2D GL_TEXTURE_2D, 0, 303 glTexImage2D GL_TEXTURE_2D, 0,
292 $self->{internalformat}, 304 $self->{internalformat},
293 $tw, $th, # need to pad texture first 305 $tw, $th, # need to pad texture first
294 0, 306 0,
295 $self->{format}, 307 $self->{format},
296 $self->{type}, 308 $self->{type},
297 $data; 309 $data;
298 if (my $error = glGetError) { 310 CFClient::gl_check "uploading texture %dx%d if=%x f=%x t=%x",
299 Carp::cluck sprintf "texture upload error: %x %dx%d i=%x f=%x t=%x",
300 $error, $tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; 311 $tw, $th, $self->{internalformat}, $self->{format}, $self->{type};
301 }
302 } else { 312 } else {
303 glCopyTexImage2D GL_TEXTURE_2D, 0, 313 glCopyTexImage2D GL_TEXTURE_2D, 0,
304 $self->{internalformat}, 314 $self->{internalformat},
305 0, 0, 315 0, 0,
306 $tw, $th, 316 $tw, $th,
307 0; 317 0;
308 if (my $error = glGetError) { 318 CFClient::gl_check "copying to texture %dx%d if=%x",
309 Carp::cluck sprintf "texture upload error: %x %dx%d i=%x",
310 $error, $tw, $th, $self->{internalformat}; 319 $tw, $th, $self->{internalformat};
311 }
312 } 320 }
313} 321}
314 322
315sub DESTROY { 323sub DESTROY {
316 my ($self) = @_; 324 my ($self) = @_;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines