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.33 by root, Sat Apr 15 01:13:45 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 glEnable GL_TEXTURE_2D;
38 glEnable GL_COLOR_MATERIAL;
39 glShadeModel GL_FLAT;
40 glDisable GL_DEPTH_TEST;
41 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
42
43 glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST;
34 44
35 CFClient::Texture::restore_state (); 45 CFClient::Texture::restore_state ();
36} 46}
37 47
38sub find_rcfile($) { 48sub find_rcfile($) {
78 close CFG; 88 close CFG;
79} 89}
80 90
81package CFClient::Texture; 91package CFClient::Texture;
82 92
93use strict;
94
83use Scalar::Util; 95use Scalar::Util;
84 96
85use SDL::OpenGL; 97use SDL::OpenGL;
86 98
87my @textures; 99my @textures;
90 my ($class, %data) = @_; 102 my ($class, %data) = @_;
91 103
92 my $self = bless { 104 my $self = bless {
93 internalformat => GL_RGBA, 105 internalformat => GL_RGBA,
94 format => GL_RGBA, 106 format => GL_RGBA,
107 type => GL_UNSIGNED_BYTE,
95 %data, 108 %data,
96 }, $class; 109 }, $class;
97 110
98 push @textures, $self; 111 push @textures, $self;
99 Scalar::Util::weaken $textures[-1]; 112 Scalar::Util::weaken $textures[-1];
102 115
103 $self 116 $self
104} 117}
105 118
106sub new_from_image { 119sub new_from_image {
107 my ($class, $image) = @_; 120 my ($class, $image, %arg) = @_;
108 121
109 $class->new (image => $image) 122 $class->new (image => $image, %arg)
110} 123}
111 124
112sub new_from_file { 125sub new_from_file {
113 my ($class, $path) = @_; 126 my ($class, $path) = @_;
114 127
140 w => $w, 153 w => $w,
141 h => $h, 154 h => $h,
142 data => $data, 155 data => $data,
143 internalformat => GL_ALPHA4, 156 internalformat => GL_ALPHA4,
144 format => GL_ALPHA, 157 format => GL_ALPHA,
158 type => GL_UNSIGNED_BYTE,
145 ) 159 )
146} 160}
147 161
148sub new_from_opengl { 162sub new_from_opengl {
149 my ($class, $w, $h, $cb) = @_; 163 my ($class, $w, $h, $cb) = @_;
156} 170}
157 171
158sub upload { 172sub upload {
159 my ($self) = @_; 173 my ($self) = @_;
160 174
161 return unless $SDL::App::USING_OPENGL; 175 return unless $GL_VERSION;
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};
169 glMatrixMode GL_PROJECTION; 184 glMatrixMode GL_PROJECTION;
170 glLoadIdentity; 185 glLoadIdentity;
171 glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000; 186 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;
176
177 $self->{render_cb}->($self, $self->{w}, $self->{h}); 189 $self->{render_cb}->($self, $self->{w}, $self->{h});
190
178 } else { 191 } else {
179 my $pb = new Gtk2::Gdk::PixbufLoader; 192 ($self->{w}, $self->{h}, $data, $self->{internalformat}, $self->{format}, $self->{type})
180 $pb->write ($self->{image}); 193 = CFClient::load_image_inline $self->{image};
181 $pb->close;
182
183 $pb = $pb->get_pixbuf;
184 $pb = $pb->add_alpha (0, 0, 0, 0);
185
186 $self->{w} = $pb->get_width;
187 $self->{h} = $pb->get_height;
188
189 $data = $pb->get_pixels;
190 } 194 }
191 195
192 my ($tw, $th) = @$self{qw(w h)}; 196 my ($tw, $th) = @$self{qw(w h)};
193 197
194 unless ($tw || $th) { 198 unless ($tw && $th) {
195 $tw = $th = 1; 199 $tw = $th = 1;
196 $data = "\x00" x 64; 200 $data = "\x00" x 64;
197 } 201 }
202
203 $self->{minified} = [CFClient::average $tw, $th, $data]
204 if $self->{minify};
198 205
199 unless ($GL_NPOT) { 206 unless ($GL_NPOT) {
200 # TODO: does not work for zero-sized textures 207 # TODO: does not work for zero-sized textures
201 $tw = topot $tw; 208 $tw = topot $tw;
202 $th = topot $th; 209 $th = topot $th;
203 210
204 if (defined $data) { 211 if ($tw != $self->{w} || $th != $self->{h} && defined $data) {
205 my $bpp = (length $data) / ($self->{w} * $self->{h}); 212 my $bpp = (length $data) / ($self->{w} * $self->{h});
206 $data = pack "(a" . ($tw * $bpp) . ")*", 213 $data = pack "(a" . ($tw * $bpp) . ")*",
207 unpack "(a" . ($self->{w} * $bpp) . ")*", $data; 214 unpack "(a" . ($self->{w} * $bpp) . ")*", $data;
208 $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h}); 215 $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h});
209 } 216 }
214 221
215 $self->{name} ||= (glGenTextures 1)->[0]; 222 $self->{name} ||= (glGenTextures 1)->[0];
216 223
217 glBindTexture GL_TEXTURE_2D, $self->{name}; 224 glBindTexture GL_TEXTURE_2D, $self->{name};
218 225
219 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; 226 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; 227 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR;
221 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; 228 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
222 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; 229 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
223 230
224 glGetError;
225 if (defined $data) { 231 if (defined $data) {
226 glTexImage2D GL_TEXTURE_2D, 0, 232 glTexImage2D GL_TEXTURE_2D, 0,
227 $self->{internalformat}, 233 $self->{internalformat},
228 $tw, $th, # need to pad texture first 234 $tw, $th, # need to pad texture first
229 0, 235 0,
230 $self->{format}, 236 $self->{format},
231 GL_UNSIGNED_BYTE, 237 $self->{type},
232 $data; 238 $data;
233 glGetError and die; 239 glGetError and die;
234 } else { 240 } else {
235 glCopyTexImage2D GL_TEXTURE_2D, 0, 241 glCopyTexImage2D GL_TEXTURE_2D, 0,
236 $self->{internalformat}, 242 $self->{internalformat},
237 0, 0, 243 0, 0,
238 $tw, $th, 244 $tw, $th,
239 0; 245 0;
240 glPopmatrix; 246 glGetError and die "glCopyTexImage2D $tw,$th";
241 } 247 }
242} 248}
243 249
244sub DESTROY { 250sub DESTROY {
245 my ($self) = @_; 251 my ($self) = @_;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines