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.17 by root, Thu Jul 19 20:12:06 2007 UTC vs.
Revision 1.24 by root, Sun Jul 29 03:58:26 2007 UTC

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 {
105 ($dw, $dh) = @$self{qw(w h)}; 108 ($dw, $dh) = @$self{qw(w h)};
106 109
107 } elsif (exists $self->{render_cb}) { 110 } elsif (exists $self->{render_cb}) {
108 ($dw, $dh) = @$self{qw(w h)}; 111 ($dw, $dh) = @$self{qw(w h)};
109 112
110 } else { 113 } elsif (exists $self->{image}) {
111 ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type}) 114 ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type})
112 = CFPlus::load_image_inline $self->{image}; 115 = CFPlus::load_image_inline $self->{image};
113 116
114 $self->{internalformat} ||= $internalformat; 117 $self->{internalformat} ||= $internalformat;
115 ($dw, $dh) = @$self{qw(w h)}; 118 ($dw, $dh) = @$self{qw(w h)};
119 } else {
120 Carp::confess "tried to create texture that is not data, render or image";
116 } 121 }
117 122
118 my ($tw, $th) = ($dw, $dh); 123 my ($tw, $th) = ($dw, $dh);
119 my ($rw, $rh) = ($dw, $dh);
120 124
121 defined $data or $self->{render_cb} or die; # some sanity check 125 defined $data or $self->{render_cb} or die; # some sanity check
122 126
123 $self->{minified} ||= [CFPlus::average $dw, $dh, $data] 127 $self->{minified} ||= [CFPlus::average $dw, $dh, $data]
124 if $self->{minify}; 128 if $self->{minify};
139 # quarter the texture size 143 # quarter the texture size
140 $tw >>= 1; 144 $tw >>= 1;
141 $th >>= 1; 145 $th >>= 1;
142 } 146 }
143 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
144 if ($self->{render_cb}) { 153 if ($self->{render_cb}) {
145 # use only part of the texture 154 # use only part of the texture
146 #$rw >>= 1 while $rw > $tw; 155 #$rw >>= 1 while $rw > $tw;
147 #$rh >>= 1 while $rh > $th; 156 #$rh >>= 1 while $rh > $th;
148 $rw = min $rw, $tw; 157 $rw = min $dw, $tw;
149 $rh = min $rh, $th; 158 $rh = min $dh, $th;
159 ++$render;
150 } else { 160 } else {
151 if ($self->{wrap} || $tw < $dw || $th < $dh) { 161 if ($self->{wrap} || $tw < $dw || $th < $dh) {
152 # scale to the full texture size 162 # scale to the full texture size
153 ($rw, $rh) = ($tw, $th); 163 ($rw, $rh) = ($tw, $th);
164 ++$render;
154 } else { 165 } else {
155 # pad 166 # pad
156 pad $data, $dw, $dh, $tw, $th; 167 pad $data, $dw, $dh, $tw, $th;
157 ($rw, $rh) = ($dw, $dh); 168 ($rw, $rh) = ($dw, $dh);
158 ($dw, $dh) = ($tw, $th); 169 ($dw, $dh) = ($tw, $th);
159 } 170 }
160 } 171 }
161 172
162 # rendering means we need to scale in some way
163 my $render = $self->{render_cb} || $rw != $dw || $th != $dh;
164
165 if ($render) { 173 if ($render) {
174 $ox = int .5 * ($::WIDTH - $rw);
175 $oy = int .5 * ($::HEIGHT - $rh);
176
166 glViewport 0, 0, $tw, $th; 177 glViewport $ox, $oy, $tw, $th;
167 glClear 0, 0, 0, 0; 178 #glScissor 0, 0, $tw, $th;
179 #glEnable GL_SCISSOR_TEST;
168 glMatrixMode GL_PROJECTION; 180 glMatrixMode GL_PROJECTION;
169 glLoadIdentity; 181 glLoadIdentity;
170 glOrtho 0, $tw, 0, $th, -10000, 10000; 182 glOrtho 0, $tw, 0, $th, -10000, 10000;
171 glMatrixMode GL_MODELVIEW; 183 glMatrixMode GL_MODELVIEW;
172 glLoadIdentity; 184 glLoadIdentity;
173 185
174 if ($self->{render_cb}) { 186 if ($self->{render_cb}) {
175 glScale $rw / $dw, $rh / $dh; 187 glScale $rw / $dw, $rh / $dh;
176 $self->{render_cb}->($self, $rw, $rh); 188 $self->{render_cb}->($self, $rw, $rh);
177 } else { 189 } else {
190 glClearColor 0, 0, 0, 0;
191 glClear GL_COLOR_BUFFER_BIT;
178 glPixelZoom $tw / $dw, $th / $dh; 192 glPixelZoom $tw / $dw, $th / $dh;
193 glRasterPos 0, 0;
179 glDrawPixels $dw, $dh, 194 glDrawPixels $dw, $dh,
180 $self->{format}, 195 $self->{format},
181 $self->{type}, 196 $self->{type},
182 $data; 197 $data;
183 glPixelZoom 1, 1; 198 glPixelZoom 1, 1;
210 glGetError; 225 glGetError;
211 226
212 if ($render) { 227 if ($render) {
213 glCopyTexImage2D GL_TEXTURE_2D, 0, 228 glCopyTexImage2D GL_TEXTURE_2D, 0,
214 $self->{internalformat}, 229 $self->{internalformat},
215 0, 0, 230 $ox, $oy,
216 $tw, $th, 231 $tw, $th,
217 0; 232 0;
218 gl_check "copying to texture %dx%d if=%x", 233 gl_check "copying to texture %dx%d if=%x",
219 $tw, $th, $self->{internalformat}; 234 $tw, $th, $self->{internalformat};
235
236 #glDisable GL_SCISSOR_TEST;
220 } else { 237 } else {
221 glTexImage2D GL_TEXTURE_2D, 0, 238 glTexImage2D GL_TEXTURE_2D, 0,
222 $self->{internalformat}, 239 $self->{internalformat},
223 $dw, $dh, 240 $dw, $dh,
224 0, 241 0,

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines