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.24 by root, Tue Apr 11 18:06:52 2006 UTC vs.
Revision 1.26 by root, Wed Apr 12 02:00:06 2006 UTC

18 $VERSION = '0.1'; 18 $VERSION = '0.1';
19 19
20 use XSLoader; 20 use XSLoader;
21 XSLoader::load "CFClient", $VERSION; 21 XSLoader::load "CFClient", $VERSION;
22} 22}
23
24use SDL::OpenGL;
23 25
24our %GL_EXT; 26our %GL_EXT;
25our $GL_VERSION; 27our $GL_VERSION;
26 28
27our $GL_NPOT; 29our $GL_NPOT;
29sub gl_init { 31sub gl_init {
30 $GL_VERSION = gl_version * 1; 32 $GL_VERSION = gl_version * 1;
31 %GL_EXT = map +($_ => 1), split /\s+/, gl_extensions; 33 %GL_EXT = map +($_ => 1), split /\s+/, gl_extensions;
32 34
33 $GL_NPOT = $GL_EXT{GL_ARB_texture_non_power_of_two} || $GL_VERSION >= 2; 35 $GL_NPOT = $GL_EXT{GL_ARB_texture_non_power_of_two} || $GL_VERSION >= 2;
36
37 glClearColor 0.45, 0.45, 0.45, 1;
38
39 glEnable GL_TEXTURE_2D;
40 glEnable GL_COLOR_MATERIAL;
41 glShadeModel GL_FLAT;
42 glDisable GL_DEPTH_TEST;
43 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
44
45 glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST;
34 46
35 CFClient::Texture::restore_state (); 47 CFClient::Texture::restore_state ();
36} 48}
37 49
38sub find_rcfile($) { 50sub find_rcfile($) {
77 89
78 close CFG; 90 close CFG;
79} 91}
80 92
81package CFClient::Texture; 93package CFClient::Texture;
94
95use strict;
82 96
83use Scalar::Util; 97use Scalar::Util;
84 98
85use SDL::OpenGL; 99use SDL::OpenGL;
86 100
162 176
163 my $data; 177 my $data;
164 178
165 if (exists $self->{data}) { 179 if (exists $self->{data}) {
166 $data = $self->{data}; 180 $data = $self->{data};
181
167 } elsif (exists $self->{render_cb}) { 182 } elsif (exists $self->{render_cb}) {
168 glViewport 0, 0, $self->{w}, $self->{h}; 183 glViewport 0, 0, $self->{w}, $self->{h};
184 glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000;
169 glMatrixMode GL_PROJECTION; 185 glMatrixMode GL_PROJECTION;
170 glLoadIdentity; 186 glLoadIdentity;
171 glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000;
172 glMatrixMode GL_MODELVIEW; 187 glMatrixMode GL_MODELVIEW;
173 glPushmatrix;
174 glLoadIdentity; 188 glLoadIdentity;
175 glClear GL_COLOR_BUFFER_BIT; 189 glClear GL_COLOR_BUFFER_BIT;
176
177 $self->{render_cb}->($self, $self->{w}, $self->{h}); 190 $self->{render_cb}->($self, $self->{w}, $self->{h});
191
178 } else { 192 } else {
179 my $pb = new Gtk2::Gdk::PixbufLoader; 193 my $pb = new Gtk2::Gdk::PixbufLoader;
180 $pb->write ($self->{image}); 194 $pb->write ($self->{image});
181 $pb->close; 195 $pb->close;
182 196
189 $data = $pb->get_pixels; 203 $data = $pb->get_pixels;
190 } 204 }
191 205
192 my ($tw, $th) = @$self{qw(w h)}; 206 my ($tw, $th) = @$self{qw(w h)};
193 207
194 unless ($tw || $th) { 208 unless ($tw && $th) {
195 $tw = $th = 1; 209 $tw = $th = 1;
196 $data = "\x00" x 64; 210 $data = "\x00" x 64;
197 } 211 }
198 212
199 unless ($GL_NPOT) { 213 unless ($GL_NPOT) {
200 # TODO: does not work for zero-sized textures 214 # TODO: does not work for zero-sized textures
201 $tw = topot $tw; 215 $tw = topot $tw;
202 $th = topot $th; 216 $th = topot $th;
203 217
204 if (defined $data) { 218 if ($tw != $self->{w} || $th != $self->{h} && defined $data) {
205 my $bpp = (length $data) / ($self->{w} * $self->{h}); 219 my $bpp = (length $data) / ($self->{w} * $self->{h});
206 $data = pack "(a" . ($tw * $bpp) . ")*", 220 $data = pack "(a" . ($tw * $bpp) . ")*",
207 unpack "(a" . ($self->{w} * $bpp) . ")*", $data; 221 unpack "(a" . ($self->{w} * $bpp) . ")*", $data;
208 $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h}); 222 $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h});
209 } 223 }
214 228
215 $self->{name} ||= (glGenTextures 1)->[0]; 229 $self->{name} ||= (glGenTextures 1)->[0];
216 230
217 glBindTexture GL_TEXTURE_2D, $self->{name}; 231 glBindTexture GL_TEXTURE_2D, $self->{name};
218 232
219 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; 233 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR;
220 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR; 234 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR;
221 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; 235 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
222 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; 236 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
223 237
224 glGetError;
225 if (defined $data) { 238 if (defined $data) {
226 glTexImage2D GL_TEXTURE_2D, 0, 239 glTexImage2D GL_TEXTURE_2D, 0,
227 $self->{internalformat}, 240 $self->{internalformat},
228 $tw, $th, # need to pad texture first 241 $tw, $th, # need to pad texture first
229 0, 242 0,
235 glCopyTexImage2D GL_TEXTURE_2D, 0, 248 glCopyTexImage2D GL_TEXTURE_2D, 0,
236 $self->{internalformat}, 249 $self->{internalformat},
237 0, 0, 250 0, 0,
238 $tw, $th, 251 $tw, $th,
239 0; 252 0;
240 glPopmatrix; 253 glGetError and die;
241 } 254 }
242} 255}
243 256
244sub DESTROY { 257sub DESTROY {
245 my ($self) = @_; 258 my ($self) = @_;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines