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.25 by root, Tue Apr 11 19:31:17 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($) {
92 my ($class, %data) = @_; 102 my ($class, %data) = @_;
93 103
94 my $self = bless { 104 my $self = bless {
95 internalformat => GL_RGBA, 105 internalformat => GL_RGBA,
96 format => GL_RGBA, 106 format => GL_RGBA,
107 type => GL_UNSIGNED_BYTE,
97 %data, 108 %data,
98 }, $class; 109 }, $class;
99 110
100 push @textures, $self; 111 push @textures, $self;
101 Scalar::Util::weaken $textures[-1]; 112 Scalar::Util::weaken $textures[-1];
104 115
105 $self 116 $self
106} 117}
107 118
108sub new_from_image { 119sub new_from_image {
109 my ($class, $image) = @_; 120 my ($class, $image, %arg) = @_;
110 121
111 $class->new (image => $image) 122 $class->new (image => $image, %arg)
112} 123}
113 124
114sub new_from_file { 125sub new_from_file {
115 my ($class, $path) = @_; 126 my ($class, $path) = @_;
116 127
142 w => $w, 153 w => $w,
143 h => $h, 154 h => $h,
144 data => $data, 155 data => $data,
145 internalformat => GL_ALPHA4, 156 internalformat => GL_ALPHA4,
146 format => GL_ALPHA, 157 format => GL_ALPHA,
158 type => GL_UNSIGNED_BYTE,
147 ) 159 )
148} 160}
149 161
150sub new_from_opengl { 162sub new_from_opengl {
151 my ($class, $w, $h, $cb) = @_; 163 my ($class, $w, $h, $cb) = @_;
158} 170}
159 171
160sub upload { 172sub upload {
161 my ($self) = @_; 173 my ($self) = @_;
162 174
163 return unless $SDL::App::USING_OPENGL; 175 return unless $GL_VERSION;
164 176
165 my $data; 177 my $data;
166 178
167 if (exists $self->{data}) { 179 if (exists $self->{data}) {
168 $data = $self->{data}; 180 $data = $self->{data};
169 181
170 } elsif (exists $self->{render_cb}) { 182 } elsif (exists $self->{render_cb}) {
171 glViewport 0, 0, $self->{w}, $self->{h}; 183 glViewport 0, 0, $self->{w}, $self->{h};
172 glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000;
173 glMatrixMode GL_PROJECTION; 184 glMatrixMode GL_PROJECTION;
174 glLoadIdentity; 185 glLoadIdentity;
186 glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000;
175 glMatrixMode GL_MODELVIEW; 187 glMatrixMode GL_MODELVIEW;
176 glLoadIdentity; 188 glLoadIdentity;
177 glClear GL_COLOR_BUFFER_BIT;
178 $self->{render_cb}->($self, $self->{w}, $self->{h}); 189 $self->{render_cb}->($self, $self->{w}, $self->{h});
179 190
180 } else { 191 } else {
181 my $pb = new Gtk2::Gdk::PixbufLoader; 192 ($self->{w}, $self->{h}, $data, $self->{internalformat}, $self->{format}, $self->{type})
182 $pb->write ($self->{image}); 193 = CFClient::load_image_inline $self->{image};
183 $pb->close;
184
185 $pb = $pb->get_pixbuf;
186 $pb = $pb->add_alpha (0, 0, 0, 0);
187
188 $self->{w} = $pb->get_width;
189 $self->{h} = $pb->get_height;
190
191 $data = $pb->get_pixels;
192 } 194 }
193 195
194 my ($tw, $th) = @$self{qw(w h)}; 196 my ($tw, $th) = @$self{qw(w h)};
195 197
196 unless ($tw && $th) { 198 unless ($tw && $th) {
197 $tw = $th = 1; 199 $tw = $th = 1;
198 $data = "\x00" x 64; 200 $data = "\x00" x 64;
199 } 201 }
202
203 $self->{minified} = [CFClient::average $tw, $th, $data]
204 if $self->{minify};
200 205
201 unless ($GL_NPOT) { 206 unless ($GL_NPOT) {
202 # TODO: does not work for zero-sized textures 207 # TODO: does not work for zero-sized textures
203 $tw = topot $tw; 208 $tw = topot $tw;
204 $th = topot $th; 209 $th = topot $th;
216 221
217 $self->{name} ||= (glGenTextures 1)->[0]; 222 $self->{name} ||= (glGenTextures 1)->[0];
218 223
219 glBindTexture GL_TEXTURE_2D, $self->{name}; 224 glBindTexture GL_TEXTURE_2D, $self->{name};
220 225
221 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; 226 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR;
222 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;
223 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; 228 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
224 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; 229 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
225 230
226 if (defined $data) { 231 if (defined $data) {
227 glTexImage2D GL_TEXTURE_2D, 0, 232 glTexImage2D GL_TEXTURE_2D, 0,
228 $self->{internalformat}, 233 $self->{internalformat},
229 $tw, $th, # need to pad texture first 234 $tw, $th, # need to pad texture first
230 0, 235 0,
231 $self->{format}, 236 $self->{format},
232 GL_UNSIGNED_BYTE, 237 $self->{type},
233 $data; 238 $data;
234 glGetError and die; 239 glGetError and die;
235 } else { 240 } else {
236 glCopyTexImage2D GL_TEXTURE_2D, 0, 241 glCopyTexImage2D GL_TEXTURE_2D, 0,
237 $self->{internalformat}, 242 $self->{internalformat},
238 0, 0, 243 0, 0,
239 $tw, $th, 244 $tw, $th,
240 0; 245 0;
241 glGetError and die; 246 glGetError and die "glCopyTexImage2D $tw,$th";
242 } 247 }
243} 248}
244 249
245sub DESTROY { 250sub DESTROY {
246 my ($self) = @_; 251 my ($self) = @_;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines