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.30 by root, Wed Apr 12 21:35:10 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];
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};
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 ($self->{w}, $self->{h}, $data, $self->{internalformat}, $self->{format}, $self->{type})
180 $pb->write ($self->{image}); 194 = 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 } 195 }
191 196
192 my ($tw, $th) = @$self{qw(w h)}; 197 my ($tw, $th) = @$self{qw(w h)};
193 198
194 unless ($tw || $th) { 199 unless ($tw && $th) {
195 $tw = $th = 1; 200 $tw = $th = 1;
196 $data = "\x00" x 64; 201 $data = "\x00" x 64;
197 } 202 }
198 203
199 unless ($GL_NPOT) { 204 unless ($GL_NPOT) {
200 # TODO: does not work for zero-sized textures 205 # TODO: does not work for zero-sized textures
201 $tw = topot $tw; 206 $tw = topot $tw;
202 $th = topot $th; 207 $th = topot $th;
203 208
204 if (defined $data) { 209 if ($tw != $self->{w} || $th != $self->{h} && defined $data) {
205 my $bpp = (length $data) / ($self->{w} * $self->{h}); 210 my $bpp = (length $data) / ($self->{w} * $self->{h});
206 $data = pack "(a" . ($tw * $bpp) . ")*", 211 $data = pack "(a" . ($tw * $bpp) . ")*",
207 unpack "(a" . ($self->{w} * $bpp) . ")*", $data; 212 unpack "(a" . ($self->{w} * $bpp) . ")*", $data;
208 $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h}); 213 $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h});
209 } 214 }
214 219
215 $self->{name} ||= (glGenTextures 1)->[0]; 220 $self->{name} ||= (glGenTextures 1)->[0];
216 221
217 glBindTexture GL_TEXTURE_2D, $self->{name}; 222 glBindTexture GL_TEXTURE_2D, $self->{name};
218 223
219 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; 224 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; 225 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR;
221 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; 226 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
222 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; 227 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
223 228
224 glGetError;
225 if (defined $data) { 229 if (defined $data) {
226 glTexImage2D GL_TEXTURE_2D, 0, 230 glTexImage2D GL_TEXTURE_2D, 0,
227 $self->{internalformat}, 231 $self->{internalformat},
228 $tw, $th, # need to pad texture first 232 $tw, $th, # need to pad texture first
229 0, 233 0,
230 $self->{format}, 234 $self->{format},
231 GL_UNSIGNED_BYTE, 235 $self->{type},
232 $data; 236 $data;
233 glGetError and die; 237 glGetError and die;
234 } else { 238 } else {
235 glCopyTexImage2D GL_TEXTURE_2D, 0, 239 glCopyTexImage2D GL_TEXTURE_2D, 0,
236 $self->{internalformat}, 240 $self->{internalformat},
237 0, 0, 241 0, 0,
238 $tw, $th, 242 $tw, $th,
239 0; 243 0;
240 glPopmatrix; 244 glGetError and die;
241 } 245 }
242} 246}
243 247
244sub DESTROY { 248sub DESTROY {
245 my ($self) = @_; 249 my ($self) = @_;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines