ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Texture.pm
Revision: 1.15
Committed: Tue Jul 17 16:02:14 2007 UTC (16 years, 11 months ago) by root
Branch: MAIN
Changes since 1.14: +3 -3 lines
Log Message:
support non-32bpp displays and non-alpha displays with lower quality (hopefully)

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 my ($tw, $th, $data);
98
99 if (exists $self->{data}) {
100 $data = $self->{data};
101 ($tw, $th) = @$self{qw(w h)};
102
103 } elsif (exists $self->{render_cb}) {
104 ($tw, $th) = @$self{qw(w h)};
105
106 } else {
107 ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type})
108 = CFPlus::load_image_inline $self->{image};
109
110 $self->{internalformat} ||= $internalformat;
111 ($tw, $th) = @$self{qw(w h)};
112 }
113
114 defined $data or $self->{render_cb} or die; # some sanity check
115
116 $self->{minified} ||= [CFPlus::average $tw, $th, $data]
117 if $self->{minify};
118
119 pad2pot $data, $tw, $th unless $GL_NPOT;
120 $tw = min $MAX_W, $tw;
121 $th = min $MAX_H, $th;
122
123 # now further decrease texture size
124 while (!texture_valid_2d $self->{internalformat}, $tw, $th, $self->{format}, $self->{type}) {
125 # quarter the texture size
126 $tw >>= 1;
127 $th >>= 1;
128 }
129
130 my $render = $self->{render_cb} || $tw < $self->{w} || $th < $self->{h};
131
132 if ($render) {
133 glViewport 0, 0, $tw, $th;
134 glMatrixMode GL_PROJECTION;
135 glLoadIdentity;
136 glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000;
137 glMatrixMode GL_MODELVIEW;
138 glLoadIdentity;
139
140 if ($self->{render_cb}) {
141 $self->{render_cb}->($self, $self->{w}, $self->{h});
142 } else {
143 glPixelZoom $tw / $self->{w}, $th / $self->{h};
144 glDrawPixels $self->{w}, $self->{h},
145 $self->{format},
146 $self->{type},
147 $data;
148 glPixelZoom 1, 1;
149 }
150 }
151
152 glBindTexture GL_TEXTURE_2D, $self->{name} ||= glGenTexture;
153
154 if ($self->{wrap}) {
155 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
156 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
157 } else {
158 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, $GL_VERSION >= 1.2 ? GL_CLAMP_TO_EDGE : GL_CLAMP;
159 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, $GL_VERSION >= 1.2 ? GL_CLAMP_TO_EDGE : GL_CLAMP;
160 }
161
162 if ($::FAST || $self->{nearest}) {
163 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST;
164 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST;
165 } elsif ($self->{mipmap} && $GL_VERSION >= 1.4) {
166 glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1;
167 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
168 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR;
169 } else {
170 glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, $self->{mipmap};
171 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
172 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;
173 }
174
175 glGetError;
176
177 if ($render) {
178 glCopyTexImage2D GL_TEXTURE_2D, 0,
179 $self->{internalformat},
180 0, 0,
181 $tw, $th,
182 0;
183 gl_check "copying to texture %dx%d if=%x",
184 $tw, $th, $self->{internalformat};
185 } else {
186 glTexImage2D GL_TEXTURE_2D, 0,
187 $self->{internalformat},
188 $tw, $th,
189 0,
190 $self->{format},
191 $self->{type},
192 $data;
193 gl_check "uploading texture %dx%d if=%x f=%x t=%x",
194 $tw, $th, $self->{internalformat}, $self->{format}, $self->{type};
195 }
196
197 $self->{s} = min 1, $self->{w} / $tw;
198 $self->{t} = min 1, $self->{h} / $th;
199 }
200
201 sub shutdown {
202 my ($self) = @_;
203
204 glDeleteTexture $self->{name}
205 if $self->{name};
206 }
207
208 sub DESTROY {
209 my ($self) = @_;
210
211 delete $TEXTURES{$self+0};
212
213 $self->shutdown;
214 }
215
216 $CFPlus::OpenGL::INIT_HOOK{"CFPlus::Texture"} = sub {
217 # first mark all existing texture names as in-use, in case the context lost textures
218 glBindTexture GL_TEXTURE_2D, $_->{name}
219 for values %TEXTURES;
220 $_->upload
221 for values %TEXTURES;
222 };
223
224 $CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Texture"} = sub {
225 $_->shutdown
226 for values %TEXTURES;
227 };
228
229 1;
230
231 =back
232
233 =head1 AUTHOR
234
235 Marc Lehmann <schmorp@schmorp.de>
236 http://home.schmorp.de/
237
238 =cut
239