ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Texture.pm
Revision: 1.17
Committed: Thu Jul 19 20:12:06 2007 UTC (16 years, 10 months ago) by root
Branch: MAIN
Changes since 1.16: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

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