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.14 by root, Sun Apr 9 00:06:09 2006 UTC vs.
Revision 1.20 by root, Mon Apr 10 22:16:33 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 ();
22} 36}
23 37
24sub find_rcfile($) { 38sub find_rcfile($) {
25 my $path; 39 my $path;
26 40
74 88
75sub new { 89sub new {
76 my ($class, %data) = @_; 90 my ($class, %data) = @_;
77 91
78 my $self = bless { 92 my $self = bless {
79 internalFormat => GL_RGBA, 93 internalformat => GL_RGBA,
80 format => GL_RGBA8, 94 format => GL_RGBA,
81 %data, 95 %data,
82 }, $class; 96 }, $class;
83 97
84 push @textures, $self; 98 push @textures, $self;
85 Scalar::Util::weaken $textures[-1]; 99 Scalar::Util::weaken $textures[-1];
124 138
125 $class->new ( 139 $class->new (
126 width => $w, 140 width => $w,
127 height => $h, 141 height => $h,
128 data => $data, 142 data => $data,
129 internalFormat => GL_ALPHA8, 143 internalformat => GL_ALPHA4,
130 format => GL_ALPHA, 144 format => GL_ALPHA,
131 ) 145 )
132} 146}
133 147
134sub new_from_opengl { 148sub new_from_opengl {
135 my ($class, $w, $h, $cb) = @_; 149 my ($class, $w, $h, $cb) = @_;
136 150
137 $class->new (width => $w, height => $h, rendercb => $cb) 151 $class->new (width => $w, height => $h, rendercb => $cb)
152}
153
154sub topot {
155 (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0]
138} 156}
139 157
140sub upload { 158sub upload {
141 my ($self) = @_; 159 my ($self) = @_;
142 160
148 $data = $self->{data}; 166 $data = $self->{data};
149 } elsif (exists $self->{rendercb}) { 167 } elsif (exists $self->{rendercb}) {
150 glViewport 0, 0, $self->{width}, $self->{height}; 168 glViewport 0, 0, $self->{width}, $self->{height};
151 glMatrixMode GL_PROJECTION; 169 glMatrixMode GL_PROJECTION;
152 glLoadIdentity; 170 glLoadIdentity;
153 glOrtho 0, $self->{width}, 0, $self->{height}, -100, 100; 171 glOrtho 0, $self->{width}, 0, $self->{height}, -10000, 10000;
154 glMatrixMode GL_MODELVIEW; 172 glMatrixMode GL_MODELVIEW;
155 glPushmatrix; 173 glPushmatrix;
156 glLoadIdentity; 174 glLoadIdentity;
157 glClear GL_COLOR_BUFFER_BIT; 175 glClear GL_COLOR_BUFFER_BIT;
158 176
169 $self->{height} = $pb->get_height; 187 $self->{height} = $pb->get_height;
170 188
171 $data = $pb->get_pixels; 189 $data = $pb->get_pixels;
172 } 190 }
173 191
192 my ($tw, $th) = @$self{qw(width height)};
193
194 unless ($GL_NPOT) {
195 $tw = topot $tw;
196 $th = topot $th;
197
198 if (defined $data) {
199 my $bpp = (length $data) / ($self->{width} * $self->{height});
200 $data = pack "(a" . ($tw * $bpp) . ")*",
201 unpack "(a" . ($self->{width} * $bpp) . ")*", $data;
202 $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{height});
203 }
204 }
205
206 $self->{s} = $self->{width} / $tw;
207 $self->{t} = $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_LINEAR;
179 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST;#_MIPMAP_LINEAR; 214 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_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