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.22 by root, Tue Apr 11 13:14:36 2006 UTC vs.
Revision 1.31 by root, Fri Apr 14 14:55:27 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];
124# 137#
125# $surface->rgba; 138# $surface->rgba;
126# 139#
127# $class->new ( 140# $class->new (
128# data => $surface->pixels, 141# data => $surface->pixels,
129# width => $surface->width, 142# w => $surface->width,
130# height => $surface->height, 143# h => $surface->height,
131# ) 144# )
132#} 145#}
133 146
134sub new_from_layout { 147sub new_from_layout {
135 my ($class, $layout) = @_; 148 my ($class, $layout) = @_;
136 149
137 my ($w, $h, $data) = $layout->render; 150 my ($w, $h, $data) = $layout->render;
138 151
139 $class->new ( 152 $class->new (
140 width => $w, 153 w => $w,
141 height => $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) = @_;
150 164
151 $class->new (width => $w, height => $h, rendercb => $cb) 165 $class->new (w => $w, h => $h, render_cb => $cb)
152} 166}
153 167
154sub topot { 168sub topot {
155 (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0] 169 (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0]
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->{rendercb}) { 182 } elsif (exists $self->{render_cb}) {
168 glViewport 0, 0, $self->{width}, $self->{height}; 183 glViewport 0, 0, $self->{w}, $self->{h};
169 glMatrixMode GL_PROJECTION; 184 glMatrixMode GL_PROJECTION;
170 glLoadIdentity; 185 glLoadIdentity;
171 glOrtho 0, $self->{width}, 0, $self->{height}, -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->{rendercb}->($self, $self->{width}, $self->{height}); 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->{width} = $pb->get_width;
187 $self->{height} = $pb->get_height;
188
189 $data = $pb->get_pixels;
190 } 194 }
191 195
192 my ($tw, $th) = @$self{qw(width height)}; 196 my ($tw, $th) = @$self{qw(w h)};
197
198 unless ($tw && $th) {
199 $tw = $th = 1;
200 $data = "\x00" x 64;
201 }
193 202
194 unless ($GL_NPOT) { 203 unless ($GL_NPOT) {
204 # TODO: does not work for zero-sized textures
195 $tw = topot $tw; 205 $tw = topot $tw;
196 $th = topot $th; 206 $th = topot $th;
197 207
198 if (defined $data) { 208 if ($tw != $self->{w} || $th != $self->{h} && defined $data) {
199 my $bpp = (length $data) / ($self->{width} * $self->{height}); 209 my $bpp = (length $data) / ($self->{w} * $self->{h});
200 $data = pack "(a" . ($tw * $bpp) . ")*", 210 $data = pack "(a" . ($tw * $bpp) . ")*",
201 unpack "(a" . ($self->{width} * $bpp) . ")*", $data; 211 unpack "(a" . ($self->{w} * $bpp) . ")*", $data;
202 $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{height}); 212 $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h});
203 } 213 }
204 } 214 }
205 215
206 $self->{s} = $self->{width} / $tw; 216 $self->{s} = $self->{w} / $tw;
207 $self->{t} = $self->{height} / $th; 217 $self->{t} = $self->{h} / $th;
208 218
209 $self->{name} ||= (glGenTextures 1)->[0]; 219 $self->{name} ||= (glGenTextures 1)->[0];
210 220
211 glBindTexture GL_TEXTURE_2D, $self->{name}; 221 glBindTexture GL_TEXTURE_2D, $self->{name};
212 222
213 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; 223 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR;
214 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR; 224 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR;
215 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; 225 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
216 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; 226 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
217 227
218 glGetError;
219 if (defined $data) { 228 if (defined $data) {
220 glTexImage2D GL_TEXTURE_2D, 0, 229 glTexImage2D GL_TEXTURE_2D, 0,
221 $self->{internalformat}, 230 $self->{internalformat},
222 $tw, $th, # need to pad texture first 231 $tw, $th, # need to pad texture first
223 0, 232 0,
224 $self->{format}, 233 $self->{format},
225 GL_UNSIGNED_BYTE, 234 $self->{type},
226 $data; 235 $data;
227 glGetError and die; 236 glGetError and die;
228 } else { 237 } else {
229 glCopyTexImage2D GL_TEXTURE_2D, 0, 238 glCopyTexImage2D GL_TEXTURE_2D, 0,
230 $self->{internalformat}, 239 $self->{internalformat},
231 0, 0, 240 0, 0,
232 $tw, $th, 241 $tw, $th,
233 0; 242 0;
234 glPopmatrix; 243 glGetError and die "glCopyTexImage2D $tw,$th";
235 } 244 }
236} 245}
237 246
238sub DESTROY { 247sub DESTROY {
239 my ($self) = @_; 248 my ($self) = @_;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines