ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Texture.pm
Revision: 1.24
Committed: Sun Jul 29 03:58:26 2007 UTC (16 years, 10 months ago) by root
Branch: MAIN
Changes since 1.23: +6 -1 lines
Log Message:
much improved sdl_mixer support

- abstracted rwops
- enabled (experimental?) Mix_LoadMUS_RW
- abstracted channels
- implemented effects api
- partial rewrite on perl side only (no need
  to store resources as separate data files anymore).
- catch more bugs w.r.t. textures.

File Contents

# User Rev Content
1 root 1.1 =head1 NAME
2    
3 root 1.11 CFPlus::Texture - tetxure class for CFPlus
4 root 1.1
5     =head1 SYNOPSIS
6    
7 root 1.11 use CFPlus::Texture;
8 root 1.1
9     =head1 DESCRIPTION
10    
11     =over 4
12    
13     =cut
14    
15 root 1.11 package CFPlus::Texture;
16 root 1.1
17     use strict;
18    
19 root 1.14 use List::Util qw(max min);
20 root 1.11 use CFPlus::OpenGL;
21 root 1.1
22     my %TEXTURES;
23 root 1.14 my ($MAX_W, $MAX_H) = (4096, 4096); # maximum texture size attempted by this module
24 root 1.1
25     sub new {
26     my ($class, %data) = @_;
27    
28     my $self = bless {
29     internalformat => GL_RGBA,
30     format => GL_RGBA,
31     type => GL_UNSIGNED_BYTE,
32     %data,
33     }, $class;
34    
35 root 1.13 CFPlus::weaken ($TEXTURES{$self+0} = $self);
36 root 1.1
37     $self->upload;
38    
39     $self
40     }
41    
42     sub new_from_image {
43     my ($class, $image, %arg) = @_;
44    
45 root 1.24 Carp::confess "tried to create texture from undefined image"
46     unless defined $image;
47    
48 root 1.7 $class->new (image => $image, internalformat => undef, %arg)
49 root 1.1 }
50    
51     sub new_from_file {
52     my ($class, $path, %arg) = @_;
53    
54     open my $fh, "<:raw", $path
55     or die "$path: $!";
56    
57     local $/;
58     $class->new_from_image (<$fh>, %arg)
59     }
60    
61     #sub new_from_surface {
62     # my ($class, $surface) = @_;
63     #
64     # $surface->rgba;
65     #
66     # $class->new (
67     # data => $surface->pixels,
68     # w => $surface->width,
69     # h => $surface->height,
70     # )
71     #}
72    
73 root 1.10 #sub new_from_layout {
74     # my ($class, $layout, %arg) = @_;
75     #
76     # my ($w, $h, $data, $format, $internalformat) = $layout->render;
77     #
78     # $class->new (
79     # w => $w,
80     # h => $h,
81     # data => $data,
82     # format => $format,
83     # internalformat => $format,
84     # type => GL_UNSIGNED_BYTE,
85     # %arg,
86     # )
87     #}
88 root 1.1
89     sub new_from_opengl {
90     my ($class, $w, $h, $cb) = @_;
91    
92 root 1.12 $class->new (w => $w || 1, h => $h || 1, render_cb => $cb, nearest => 1)
93 root 1.1 }
94    
95     sub upload {
96     my ($self) = @_;
97    
98     return unless $GL_VERSION;
99    
100 root 1.16 # $tw,$th texture
101     # $rw,$rh rendered/used size
102     # $dw,$dh $data
103    
104     my ($data, $dw, $dh);
105 root 1.1
106     if (exists $self->{data}) {
107     $data = $self->{data};
108 root 1.16 ($dw, $dh) = @$self{qw(w h)};
109 root 1.1
110     } elsif (exists $self->{render_cb}) {
111 root 1.16 ($dw, $dh) = @$self{qw(w h)};
112 root 1.1
113 root 1.24 } elsif (exists $self->{image}) {
114 root 1.7 ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type})
115 root 1.11 = CFPlus::load_image_inline $self->{image};
116 root 1.7
117     $self->{internalformat} ||= $internalformat;
118 root 1.16 ($dw, $dh) = @$self{qw(w h)};
119 root 1.24 } else {
120     Carp::confess "tried to create texture that is not data, render or image";
121 root 1.1 }
122    
123 root 1.16 my ($tw, $th) = ($dw, $dh);
124    
125 root 1.14 defined $data or $self->{render_cb} or die; # some sanity check
126 root 1.1
127 root 1.16 $self->{minified} ||= [CFPlus::average $dw, $dh, $data]
128 root 1.1 if $self->{minify};
129    
130 root 1.16 # against rather broken cards we enforce a maximum texture size
131     $tw = min $MAX_W, minpot $tw;
132     $th = min $MAX_H, minpot $th;
133    
134     # if only pot-textures are allowed, pot'ify tw/th
135     unless ($GL_NPOT && 0) {#d#
136     $tw = minpot $tw;
137     $th = minpot $th;
138     }
139 root 1.14
140 root 1.16 # now further decrease texture size until the
141     # card does accept it
142 root 1.14 while (!texture_valid_2d $self->{internalformat}, $tw, $th, $self->{format}, $self->{type}) {
143     # quarter the texture size
144     $tw >>= 1;
145     $th >>= 1;
146     }
147    
148 root 1.18 # decide the amount of space used in the texture
149     my ($rw, $rh);
150 root 1.22 my ($ox, $oy); # area shift to lessen effetc of buggy opengl implementations (nvida, ati)
151 root 1.19 my $render;
152 root 1.18
153 root 1.16 if ($self->{render_cb}) {
154     # use only part of the texture
155     #$rw >>= 1 while $rw > $tw;
156     #$rh >>= 1 while $rh > $th;
157 root 1.18 $rw = min $dw, $tw;
158     $rh = min $dh, $th;
159 root 1.19 ++$render;
160 root 1.16 } else {
161     if ($self->{wrap} || $tw < $dw || $th < $dh) {
162     # scale to the full texture size
163     ($rw, $rh) = ($tw, $th);
164 root 1.19 ++$render;
165 root 1.16 } else {
166     # pad
167     pad $data, $dw, $dh, $tw, $th;
168     ($rw, $rh) = ($dw, $dh);
169     ($dw, $dh) = ($tw, $th);
170     }
171     }
172    
173 root 1.15 if ($render) {
174 root 1.23 $ox = int .5 * ($::WIDTH - $rw);
175     $oy = int .5 * ($::HEIGHT - $rh);
176 root 1.22
177     glViewport $ox, $oy, $tw, $th;
178 root 1.21 #glScissor 0, 0, $tw, $th;
179     #glEnable GL_SCISSOR_TEST;
180 root 1.14 glMatrixMode GL_PROJECTION;
181     glLoadIdentity;
182 root 1.16 glOrtho 0, $tw, 0, $th, -10000, 10000;
183 root 1.14 glMatrixMode GL_MODELVIEW;
184     glLoadIdentity;
185 root 1.1
186 root 1.14 if ($self->{render_cb}) {
187 root 1.16 glScale $rw / $dw, $rh / $dh;
188     $self->{render_cb}->($self, $rw, $rh);
189 root 1.14 } else {
190 root 1.20 glClearColor 0, 0, 0, 0;
191     glClear GL_COLOR_BUFFER_BIT;
192 root 1.16 glPixelZoom $tw / $dw, $th / $dh;
193 root 1.22 glRasterPos 0, 0;
194 root 1.16 glDrawPixels $dw, $dh,
195 root 1.14 $self->{format},
196     $self->{type},
197     $data;
198     glPixelZoom 1, 1;
199     }
200     }
201 root 1.1
202 root 1.14 glBindTexture GL_TEXTURE_2D, $self->{name} ||= glGenTexture;
203 root 1.1
204 root 1.2 if ($self->{wrap}) {
205     glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
206     glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
207     } else {
208     glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, $GL_VERSION >= 1.2 ? GL_CLAMP_TO_EDGE : GL_CLAMP;
209     glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, $GL_VERSION >= 1.2 ? GL_CLAMP_TO_EDGE : GL_CLAMP;
210     }
211 root 1.1
212 root 1.12 if ($::FAST || $self->{nearest}) {
213 root 1.1 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST;
214     glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST;
215     } elsif ($self->{mipmap} && $GL_VERSION >= 1.4) {
216     glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1;
217     glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
218     glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR;
219     } else {
220     glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, $self->{mipmap};
221     glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
222     glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;
223     }
224    
225     glGetError;
226    
227 root 1.15 if ($render) {
228 root 1.14 glCopyTexImage2D GL_TEXTURE_2D, 0,
229     $self->{internalformat},
230 root 1.22 $ox, $oy,
231 root 1.14 $tw, $th,
232     0;
233     gl_check "copying to texture %dx%d if=%x",
234     $tw, $th, $self->{internalformat};
235 root 1.21
236     #glDisable GL_SCISSOR_TEST;
237 root 1.14 } else {
238 root 1.1 glTexImage2D GL_TEXTURE_2D, 0,
239     $self->{internalformat},
240 root 1.16 $dw, $dh,
241 root 1.1 0,
242     $self->{format},
243     $self->{type},
244     $data;
245     gl_check "uploading texture %dx%d if=%x f=%x t=%x",
246     $tw, $th, $self->{internalformat}, $self->{format}, $self->{type};
247     }
248 root 1.14
249 root 1.16 $self->{s} = $rw / $tw;
250     $self->{t} = $rh / $th;
251 root 1.1 }
252    
253 root 1.8 sub shutdown {
254     my ($self) = @_;
255    
256 root 1.9 glDeleteTexture $self->{name}
257 root 1.8 if $self->{name};
258     }
259    
260 root 1.1 sub DESTROY {
261     my ($self) = @_;
262    
263     delete $TEXTURES{$self+0};
264    
265 root 1.8 $self->shutdown;
266 root 1.1 }
267    
268 root 1.11 $CFPlus::OpenGL::INIT_HOOK{"CFPlus::Texture"} = sub {
269 root 1.9 # first mark all existing texture names as in-use, in case the context lost textures
270     glBindTexture GL_TEXTURE_2D, $_->{name}
271     for values %TEXTURES;
272 root 1.1 $_->upload
273     for values %TEXTURES;
274     };
275    
276 root 1.11 $CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Texture"} = sub {
277 root 1.8 $_->shutdown
278     for values %TEXTURES;
279     };
280    
281 root 1.1 1;
282    
283     =back
284    
285     =head1 AUTHOR
286    
287     Marc Lehmann <schmorp@schmorp.de>
288     http://home.schmorp.de/
289    
290     =cut
291