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.24 by root, Sun Jul 29 03:58:26 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}
41 41
42sub new_from_image { 42sub new_from_image {
43 my ($class, $image, %arg) = @_; 43 my ($class, $image, %arg) = @_;
44
45 Carp::confess "tried to create texture from undefined image"
46 unless defined $image;
44 47
45 $class->new (image => $image, internalformat => undef, %arg) 48 $class->new (image => $image, internalformat => undef, %arg)
46} 49}
47 50
48sub new_from_file { 51sub new_from_file {
92sub upload { 95sub upload {
93 my ($self) = @_; 96 my ($self) = @_;
94 97
95 return unless $GL_VERSION; 98 return unless $GL_VERSION;
96 99
97 my $data; 100 # $tw,$th texture
101 # $rw,$rh rendered/used size
102 # $dw,$dh $data
103
104 my ($data, $dw, $dh);
98 105
99 if (exists $self->{data}) { 106 if (exists $self->{data}) {
100 $data = $self->{data}; 107 $data = $self->{data};
108 ($dw, $dh) = @$self{qw(w h)};
101 109
102 } elsif (exists $self->{render_cb}) { 110 } elsif (exists $self->{render_cb}) {
103 glViewport 0, 0, $self->{w}, $self->{h}; 111 ($dw, $dh) = @$self{qw(w h)};
112
113 } elsif (exists $self->{image}) {
114 ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type})
115 = CFPlus::load_image_inline $self->{image};
116
117 $self->{internalformat} ||= $internalformat;
118 ($dw, $dh) = @$self{qw(w h)};
119 } else {
120 Carp::confess "tried to create texture that is not data, render or image";
121 }
122
123 my ($tw, $th) = ($dw, $dh);
124
125 defined $data or $self->{render_cb} or die; # some sanity check
126
127 $self->{minified} ||= [CFPlus::average $dw, $dh, $data]
128 if $self->{minify};
129
130 # against rather broken cards we enforce a maximum texture size
131 $tw = min $MAX_W, minpot $tw;
132 $th = min $MAX_H, minpot $th;
133
134 # if only pot-textures are allowed, pot'ify tw/th
135 unless ($GL_NPOT && 0) {#d#
136 $tw = minpot $tw;
137 $th = minpot $th;
138 }
139
140 # now further decrease texture size until the
141 # card does accept it
142 while (!texture_valid_2d $self->{internalformat}, $tw, $th, $self->{format}, $self->{type}) {
143 # quarter the texture size
144 $tw >>= 1;
145 $th >>= 1;
146 }
147
148 # decide the amount of space used in the texture
149 my ($rw, $rh);
150 my ($ox, $oy); # area shift to lessen effetc of buggy opengl implementations (nvida, ati)
151 my $render;
152
153 if ($self->{render_cb}) {
154 # use only part of the texture
155 #$rw >>= 1 while $rw > $tw;
156 #$rh >>= 1 while $rh > $th;
157 $rw = min $dw, $tw;
158 $rh = min $dh, $th;
159 ++$render;
160 } else {
161 if ($self->{wrap} || $tw < $dw || $th < $dh) {
162 # scale to the full texture size
163 ($rw, $rh) = ($tw, $th);
164 ++$render;
165 } else {
166 # pad
167 pad $data, $dw, $dh, $tw, $th;
168 ($rw, $rh) = ($dw, $dh);
169 ($dw, $dh) = ($tw, $th);
170 }
171 }
172
173 if ($render) {
174 $ox = int .5 * ($::WIDTH - $rw);
175 $oy = int .5 * ($::HEIGHT - $rh);
176
177 glViewport $ox, $oy, $tw, $th;
178 #glScissor 0, 0, $tw, $th;
179 #glEnable GL_SCISSOR_TEST;
104 glMatrixMode GL_PROJECTION; 180 glMatrixMode GL_PROJECTION;
105 glLoadIdentity; 181 glLoadIdentity;
106 glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000; 182 glOrtho 0, $tw, 0, $th, -10000, 10000;
107 glMatrixMode GL_MODELVIEW; 183 glMatrixMode GL_MODELVIEW;
108 glLoadIdentity; 184 glLoadIdentity;
185
186 if ($self->{render_cb}) {
187 glScale $rw / $dw, $rh / $dh;
109 $self->{render_cb}->($self, $self->{w}, $self->{h}); 188 $self->{render_cb}->($self, $rw, $rh);
110
111 } else { 189 } else {
112 ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type}) 190 glClearColor 0, 0, 0, 0;
113 = CFPlus::load_image_inline $self->{image}; 191 glClear GL_COLOR_BUFFER_BIT;
114 192 glPixelZoom $tw / $dw, $th / $dh;
115 $self->{internalformat} ||= $internalformat; 193 glRasterPos 0, 0;
194 glDrawPixels $dw, $dh,
195 $self->{format},
196 $self->{type},
197 $data;
198 glPixelZoom 1, 1;
199 }
116 } 200 }
117 201
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}; 202 glBindTexture GL_TEXTURE_2D, $self->{name} ||= glGenTexture;
131 203
132 if ($self->{wrap}) { 204 if ($self->{wrap}) {
133 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 205 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
134 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 206 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
135 } else { 207 } else {
150 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR; 222 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;
151 } 223 }
152 224
153 glGetError; 225 glGetError;
154 226
155 if (defined $data) { 227 if ($render) {
228 glCopyTexImage2D GL_TEXTURE_2D, 0,
229 $self->{internalformat},
230 $ox, $oy,
231 $tw, $th,
232 0;
233 gl_check "copying to texture %dx%d if=%x",
234 $tw, $th, $self->{internalformat};
235
236 #glDisable GL_SCISSOR_TEST;
237 } else {
156 glTexImage2D GL_TEXTURE_2D, 0, 238 glTexImage2D GL_TEXTURE_2D, 0,
157 $self->{internalformat}, 239 $self->{internalformat},
158 $tw, $th, 240 $dw, $dh,
159 0, 241 0,
160 $self->{format}, 242 $self->{format},
161 $self->{type}, 243 $self->{type},
162 $data; 244 $data;
163 gl_check "uploading texture %dx%d if=%x f=%x t=%x", 245 gl_check "uploading texture %dx%d if=%x f=%x t=%x",
164 $tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; 246 $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 } 247 }
248
249 $self->{s} = $rw / $tw;
250 $self->{t} = $rh / $th;
175} 251}
176 252
177sub shutdown { 253sub shutdown {
178 my ($self) = @_; 254 my ($self) = @_;
179 255

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines