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.15 by root, Sun Apr 9 00:12:58 2006 UTC vs.
Revision 1.19 by root, Mon Apr 10 19:34:03 2006 UTC

17BEGIN { 17BEGIN {
18 $VERSION = '0.1'; 18 $VERSION = '0.1';
19 19
20 use XSLoader; 20 use XSLoader;
21 XSLoader::load "Crossfire::Client", $VERSION; 21 XSLoader::load "Crossfire::Client", $VERSION;
22}
23
24our %GL_EXT;
25our $GL_VERSION;
26
27our $GL_NPOT;
28
29sub gl_init {
30 $GL_VERSION = gl_version * 1;
31 %GL_EXT = map +($_ => 1), split /\s+/, gl_extensions;
32
33 $GL_NPOT = $GL_EXT{GL_ARB_texture_non_power_of_two} || $GL_VERSION >= 2;
34
35 Crossfire::Client::Texture::restore_state ();
36
37# $GL_NPOT=0;#d#
22} 38}
23 39
24sub find_rcfile($) { 40sub find_rcfile($) {
25 my $path; 41 my $path;
26 42
124 140
125 $class->new ( 141 $class->new (
126 width => $w, 142 width => $w,
127 height => $h, 143 height => $h,
128 data => $data, 144 data => $data,
129 internalformat => GL_ALPHA8, 145 internalformat => GL_ALPHA4,
130 format => GL_ALPHA, 146 format => GL_ALPHA,
131 ) 147 )
132} 148}
133 149
134sub new_from_opengl { 150sub new_from_opengl {
135 my ($class, $w, $h, $cb) = @_; 151 my ($class, $w, $h, $cb) = @_;
136 152
137 $class->new (width => $w, height => $h, rendercb => $cb) 153 $class->new (width => $w, height => $h, rendercb => $cb)
154}
155
156sub topot {
157 (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0]
138} 158}
139 159
140sub upload { 160sub upload {
141 my ($self) = @_; 161 my ($self) = @_;
142 162
148 $data = $self->{data}; 168 $data = $self->{data};
149 } elsif (exists $self->{rendercb}) { 169 } elsif (exists $self->{rendercb}) {
150 glViewport 0, 0, $self->{width}, $self->{height}; 170 glViewport 0, 0, $self->{width}, $self->{height};
151 glMatrixMode GL_PROJECTION; 171 glMatrixMode GL_PROJECTION;
152 glLoadIdentity; 172 glLoadIdentity;
153 glOrtho 0, $self->{width}, 0, $self->{height}, -100, 100; 173 glOrtho 0, $self->{width}, 0, $self->{height}, -10000, 10000;
154 glMatrixMode GL_MODELVIEW; 174 glMatrixMode GL_MODELVIEW;
155 glPushmatrix; 175 glPushmatrix;
156 glLoadIdentity; 176 glLoadIdentity;
157 glClear GL_COLOR_BUFFER_BIT; 177 glClear GL_COLOR_BUFFER_BIT;
158 178
169 $self->{height} = $pb->get_height; 189 $self->{height} = $pb->get_height;
170 190
171 $data = $pb->get_pixels; 191 $data = $pb->get_pixels;
172 } 192 }
173 193
194 my ($tw, $th) = @$self{qw(width height)};
195
196 unless ($NPOT) {
197 $tw = topot $tw;
198 $th = topot $th;
199
200 if (defined $data) {
201 $data = pack "(a$tw)*", (unpack "(a$self->{width})*", $data);
202 $data .= ("\x00" x $tw) x ($th - $self->{height});
203 }
204 }
205
206 $self->{u} = $self->{width} / $tw;
207 $self->{v} = $self->{height} / $th;
208
174 ($self->{name}) = @{glGenTextures 1}; 209 $self->{name} ||= (glGenTextures 1)->[0];
175 210
176 glBindTexture GL_TEXTURE_2D, $self->{name}; 211 glBindTexture GL_TEXTURE_2D, $self->{name};
177 212
178 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; 213 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST;
179 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST;#_MIPMAP_LINEAR; 214 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST;#_MIPMAP_LINEAR;
180 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; 215 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
181 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; 216 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
182 217
218 glGetError;
183 if (defined $data) { 219 if (defined $data) {
184 glTexImage2D GL_TEXTURE_2D, 0, 220 glTexImage2D GL_TEXTURE_2D, 0,
185 $self->{internalformat}, 221 $self->{internalformat},
186 $self->{width}, $self->{height}, 222 $tw, $th, # need to pad texture first
187 0, 223 0,
188 $self->{format}, 224 $self->{format},
189 GL_UNSIGNED_BYTE, 225 GL_UNSIGNED_BYTE,
190 $data; 226 $data;
227 glGetError and die;
191 } else { 228 } else {
192 glCopyTexImage2D GL_TEXTURE_2D, 0, 229 glCopyTexImage2D GL_TEXTURE_2D, 0,
193 $self->{internalformat}, 230 $self->{internalformat},
194 0, 0, 231 0, 0,
195 $self->{width}, $self->{height}, 232 $tw, $th,
196 0; 233 0;
197 glPopmatrix; 234 glPopmatrix;
198 #SDL::GLSwapBuffers;
199 #sleep 1;
200 } 235 }
201} 236}
202 237
203sub DESTROY { 238sub DESTROY {
204 my ($self) = @_; 239 my ($self) = @_;
206 return unless exists $self->{name}; 241 return unless exists $self->{name};
207 242
208 glDeleteTextures delete $self->{name}; 243 glDeleteTextures delete $self->{name};
209} 244}
210 245
211push @::GLINIT, sub { 246sub restore_state{
212 $_->upload 247 $_->upload
213 for grep $_, @textures; 248 for grep $_, @textures;
214}; 249};
215 250
2161; 2511;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines