1 |
root |
1.14 |
package DC::OpenGL; |
2 |
root |
1.1 |
|
3 |
|
|
use strict; |
4 |
|
|
|
5 |
root |
1.2 |
use Carp (); |
6 |
root |
1.14 |
use DC; |
7 |
root |
1.2 |
|
8 |
|
|
our %GL_EXT; |
9 |
|
|
our $GL_VERSION; |
10 |
|
|
|
11 |
|
|
our $GL_NPOT; |
12 |
root |
1.9 |
our $GL_COMPRESS; |
13 |
root |
1.10 |
our $GL_BFSEP; # blendfuncseparate |
14 |
root |
1.17 |
our $GL_MULTITEX; |
15 |
root |
1.16 |
our $APPLE_NVIDIA_BUG; |
16 |
root |
1.2 |
|
17 |
|
|
our $DEBUG = 1; |
18 |
|
|
our %INIT_HOOK; |
19 |
root |
1.3 |
our %SHUTDOWN_HOOK; |
20 |
root |
1.1 |
|
21 |
|
|
sub import { |
22 |
|
|
my $caller = caller; |
23 |
|
|
|
24 |
|
|
no strict; |
25 |
|
|
|
26 |
root |
1.14 |
my $symtab = *{"main::DC::OpenGL::"}{HASH}; |
27 |
root |
1.1 |
|
28 |
|
|
for (keys %$symtab) { |
29 |
root |
1.2 |
*{"$caller\::$_"} = *$_ |
30 |
|
|
if /^(?:gl[A-Z_]|GL_)/; |
31 |
root |
1.1 |
} |
32 |
|
|
} |
33 |
|
|
|
34 |
root |
1.2 |
sub init { |
35 |
root |
1.10 |
$GL_VERSION = gl_version * 1; |
36 |
|
|
%GL_EXT = map +($_ => 1), split /\s+/, gl_extensions; |
37 |
|
|
|
38 |
|
|
unless (defined $::CFG->{force_opengl11}) { |
39 |
|
|
# try to find a suitable default |
40 |
|
|
if ( |
41 |
|
|
$GL_VERSION >= 2.0 |
42 |
|
|
&& (!$GL_EXT{GL_ARB_texture_non_power_of_two} |
43 |
|
|
|| !$GL_EXT{GL_EXT_blend_func_separate}) |
44 |
root |
1.17 |
) { |
45 |
|
|
$::CFG->{force_opengl11} = 1; |
46 |
|
|
} else { |
47 |
|
|
$::CFG->{force_opengl11} = 0; |
48 |
|
|
} |
49 |
root |
1.10 |
} |
50 |
|
|
|
51 |
root |
1.9 |
if ($::CFG->{force_opengl11}) { |
52 |
root |
1.8 |
$GL_VERSION = 1.1; |
53 |
|
|
%GL_EXT = (); |
54 |
|
|
} |
55 |
root |
1.2 |
|
56 |
root |
1.10 |
$GL_BFSEP = $GL_EXT{GL_EXT_blend_func_separate} || $GL_VERSION >= 2.0; |
57 |
root |
1.9 |
$GL_NPOT = $GL_EXT{GL_ARB_texture_non_power_of_two} || $GL_VERSION >= 2.0; |
58 |
|
|
$GL_COMPRESS = $GL_EXT{GL_ARB_texture_compression} || $GL_VERSION >= 1.3; |
59 |
root |
1.19 |
$GL_MULTITEX = $GL_EXT{GL_ARB_multitexture} || $GL_VERSION >= 1.3; |
60 |
root |
1.20 |
$GL_MULTITEX &&= 2 <= glGetInteger GL_MAX_TEXTURE_UNITS; |
61 |
root |
1.2 |
|
62 |
root |
1.15 |
$GL_COMPRESS = 0 if DC::OpenGL::gl_vendor eq "Apple Computer, Inc."; # there is no end to their suckage |
63 |
|
|
|
64 |
root |
1.16 |
$APPLE_NVIDIA_BUG = DC::OpenGL::gl_vendor eq "NVIDIA Corporation" && $^O eq "darwin"; |
65 |
|
|
apple_nvidia_bug $APPLE_NVIDIA_BUG; |
66 |
|
|
|
67 |
root |
1.10 |
disable_GL_EXT_blend_func_separate |
68 |
|
|
unless $GL_BFSEP; |
69 |
|
|
|
70 |
root |
1.2 |
glDisable GL_COLOR_MATERIAL; |
71 |
|
|
glShadeModel GL_FLAT; |
72 |
|
|
glDisable GL_DITHER; |
73 |
|
|
glDisable GL_DEPTH_TEST; |
74 |
|
|
glDepthMask 0; |
75 |
root |
1.5 |
|
76 |
|
|
my $hint = $::FAST ? GL_FASTEST : GL_NICEST; |
77 |
|
|
glHint GL_PERSPECTIVE_CORRECTION_HINT, $hint; |
78 |
root |
1.6 |
glHint GL_POINT_SMOOTH_HINT , $hint; |
79 |
root |
1.5 |
glHint GL_LINE_SMOOTH_HINT , $hint; |
80 |
|
|
glHint GL_POLYGON_SMOOTH_HINT , $hint; |
81 |
|
|
glHint GL_GENERATE_MIPMAP_HINT , $hint; |
82 |
root |
1.11 |
glHint GL_TEXTURE_COMPRESSION_HINT , $hint; |
83 |
root |
1.5 |
#glDrawBuffer GL_BACK; |
84 |
|
|
#glReadBuffer GL_BACK; |
85 |
root |
1.2 |
|
86 |
root |
1.18 |
c_init; |
87 |
root |
1.2 |
$_->() for values %INIT_HOOK; |
88 |
|
|
} |
89 |
|
|
|
90 |
root |
1.12 |
sub quit { |
91 |
|
|
undef $GL_VERSION; |
92 |
|
|
undef %GL_EXT; |
93 |
|
|
} |
94 |
|
|
|
95 |
root |
1.3 |
sub shutdown { |
96 |
|
|
$_->() for values %SHUTDOWN_HOOK; |
97 |
root |
1.12 |
|
98 |
|
|
quit; |
99 |
root |
1.3 |
} |
100 |
|
|
|
101 |
root |
1.2 |
sub gl_check { |
102 |
|
|
return unless $DEBUG; |
103 |
|
|
|
104 |
|
|
if (my $error = glGetError) { |
105 |
|
|
my ($format, @args) = @_; |
106 |
|
|
Carp::cluck sprintf "opengl error %x while $format", $error, @args; |
107 |
|
|
} |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
1; |
111 |
|
|
|