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.12 by root, Sun Aug 13 16:29:36 2006 UTC vs.
Revision 1.22 by root, Tue Jul 24 00:11:18 2007 UTC

14 14
15package CFPlus::Texture; 15package CFPlus::Texture;
16 16
17use strict; 17use strict;
18 18
19use Scalar::Util; 19use List::Util qw(max min);
20
21use CFPlus::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}
92sub upload { 92sub upload {
93 my ($self) = @_; 93 my ($self) = @_;
94 94
95 return unless $GL_VERSION; 95 return unless $GL_VERSION;
96 96
97 my $data; 97 # $tw,$th texture
98 # $rw,$rh rendered/used size
99 # $dw,$dh $data
100
101 my ($data, $dw, $dh);
98 102
99 if (exists $self->{data}) { 103 if (exists $self->{data}) {
100 $data = $self->{data}; 104 $data = $self->{data};
105 ($dw, $dh) = @$self{qw(w h)};
101 106
102 } elsif (exists $self->{render_cb}) { 107 } elsif (exists $self->{render_cb}) {
103 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 - $tw);
170 $oy = int .5 * ($::HEIGHT - $th);
171
172 glViewport $ox, $oy, $tw, $th;
173 #glScissor 0, 0, $tw, $th;
174 #glEnable GL_SCISSOR_TEST;
104 glMatrixMode GL_PROJECTION; 175 glMatrixMode GL_PROJECTION;
105 glLoadIdentity; 176 glLoadIdentity;
106 glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000; 177 glOrtho 0, $tw, 0, $th, -10000, 10000;
107 glMatrixMode GL_MODELVIEW; 178 glMatrixMode GL_MODELVIEW;
108 glLoadIdentity; 179 glLoadIdentity;
180
181 if ($self->{render_cb}) {
182 glScale $rw / $dw, $rh / $dh;
109 $self->{render_cb}->($self, $self->{w}, $self->{h}); 183 $self->{render_cb}->($self, $rw, $rh);
110
111 } else { 184 } else {
112 ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type}) 185 glClearColor 0, 0, 0, 0;
113 = CFPlus::load_image_inline $self->{image}; 186 glClear GL_COLOR_BUFFER_BIT;
114 187 glPixelZoom $tw / $dw, $th / $dh;
115 $self->{internalformat} ||= $internalformat; 188 glRasterPos 0, 0;
189 glDrawPixels $dw, $dh,
190 $self->{format},
191 $self->{type},
192 $data;
193 glPixelZoom 1, 1;
194 }
116 } 195 }
117 196
118 my ($tw, $th) = @$self{qw(w h)};
119
120 $self->{minified} ||= [CFPlus::average $tw, $th, $data]
121 if $self->{minify};
122
123 pad2pot $data, $tw, $th unless $GL_NPOT;
124
125 $self->{s} = $self->{w} / $tw;
126 $self->{t} = $self->{h} / $th;
127
128 $self->{name} ||= glGenTexture;
129
130 glBindTexture GL_TEXTURE_2D, $self->{name}; 197 glBindTexture GL_TEXTURE_2D, $self->{name} ||= glGenTexture;
131 198
132 if ($self->{wrap}) { 199 if ($self->{wrap}) {
133 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 200 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
134 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 201 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
135 } else { 202 } else {
150 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR; 217 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;
151 } 218 }
152 219
153 glGetError; 220 glGetError;
154 221
155 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 {
156 glTexImage2D GL_TEXTURE_2D, 0, 233 glTexImage2D GL_TEXTURE_2D, 0,
157 $self->{internalformat}, 234 $self->{internalformat},
158 $tw, $th, 235 $dw, $dh,
159 0, 236 0,
160 $self->{format}, 237 $self->{format},
161 $self->{type}, 238 $self->{type},
162 $data; 239 $data;
163 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",
164 $tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; 241 $tw, $th, $self->{internalformat}, $self->{format}, $self->{type};
165 } else {
166 exists $self->{render_cb} or die;
167 glCopyTexImage2D GL_TEXTURE_2D, 0,
168 $self->{internalformat},
169 0, 0,
170 $tw, $th,
171 0;
172 gl_check "copying to texture %dx%d if=%x",
173 $tw, $th, $self->{internalformat};
174 } 242 }
243
244 $self->{s} = $rw / $tw;
245 $self->{t} = $rh / $th;
175} 246}
176 247
177sub shutdown { 248sub shutdown {
178 my ($self) = @_; 249 my ($self) = @_;
179 250

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines