ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Texture.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/Texture.pm (file contents):
Revision 1.8 by root, Sat Jun 17 15:07:53 2006 UTC vs.
Revision 1.15 by root, Tue Jul 17 16:02:14 2007 UTC

1=head1 NAME 1=head1 NAME
2 2
3CFClient::Texture - tetxure class for CFClient 3CFPlus::Texture - tetxure class for CFPlus
4 4
5=head1 SYNOPSIS 5=head1 SYNOPSIS
6 6
7 use CFClient::Texture; 7 use CFPlus::Texture;
8 8
9=head1 DESCRIPTION 9=head1 DESCRIPTION
10 10
11=over 4 11=over 4
12 12
13=cut 13=cut
14 14
15package CFClient::Texture; 15package CFPlus::Texture;
16 16
17use strict; 17use strict;
18 18
19use Scalar::Util; 19use List::Util qw(max min);
20
21use CFClient::OpenGL; 20use CFPlus::OpenGL;
22 21
23my %TEXTURES; 22my %TEXTURES;
23my ($MAX_W, $MAX_H) = (4096, 4096); # maximum texture size attempted by this module
24 24
25sub new { 25sub new {
26 my ($class, %data) = @_; 26 my ($class, %data) = @_;
27 27
28 my $self = bless { 28 my $self = bless {
30 format => GL_RGBA, 30 format => GL_RGBA,
31 type => GL_UNSIGNED_BYTE, 31 type => GL_UNSIGNED_BYTE,
32 %data, 32 %data,
33 }, $class; 33 }, $class;
34 34
35 Scalar::Util::weaken ($TEXTURES{$self+0} = $self); 35 CFPlus::weaken ($TEXTURES{$self+0} = $self);
36 36
37 $self->upload; 37 $self->upload;
38 38
39 $self 39 $self
40} 40}
65# w => $surface->width, 65# w => $surface->width,
66# h => $surface->height, 66# h => $surface->height,
67# ) 67# )
68#} 68#}
69 69
70sub new_from_layout { 70#sub new_from_layout {
71 my ($class, $layout, %arg) = @_; 71# my ($class, $layout, %arg) = @_;
72 72#
73 my ($w, $h, $data, $format, $internalformat) = $layout->render; 73# my ($w, $h, $data, $format, $internalformat) = $layout->render;
74 74#
75 $class->new ( 75# $class->new (
76 w => $w, 76# w => $w,
77 h => $h, 77# h => $h,
78 data => $data, 78# data => $data,
79 format => $format, 79# format => $format,
80 internalformat => $format, 80# internalformat => $format,
81 type => GL_UNSIGNED_BYTE, 81# type => GL_UNSIGNED_BYTE,
82 %arg, 82# %arg,
83 ) 83# )
84} 84#}
85 85
86sub new_from_opengl { 86sub new_from_opengl {
87 my ($class, $w, $h, $cb) = @_; 87 my ($class, $w, $h, $cb) = @_;
88 88
89 $class->new (w => $w || 1, h => $h || 1, render_cb => $cb) 89 $class->new (w => $w || 1, h => $h || 1, render_cb => $cb, nearest => 1)
90} 90}
91 91
92sub upload { 92sub upload {
93 my ($self) = @_; 93 my ($self) = @_;
94 94
95 return unless $GL_VERSION; 95 return unless $GL_VERSION;
96 96
97 my $data; 97 my ($tw, $th, $data);
98 98
99 if (exists $self->{data}) { 99 if (exists $self->{data}) {
100 $data = $self->{data}; 100 $data = $self->{data};
101 ($tw, $th) = @$self{qw(w h)};
101 102
102 } elsif (exists $self->{render_cb}) { 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) {
103 glViewport 0, 0, $self->{w}, $self->{h}; 133 glViewport 0, 0, $tw, $th;
104 glMatrixMode GL_PROJECTION; 134 glMatrixMode GL_PROJECTION;
105 glLoadIdentity; 135 glLoadIdentity;
106 glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000; 136 glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000;
107 glMatrixMode GL_MODELVIEW; 137 glMatrixMode GL_MODELVIEW;
108 glLoadIdentity; 138 glLoadIdentity;
139
140 if ($self->{render_cb}) {
109 $self->{render_cb}->($self, $self->{w}, $self->{h}); 141 $self->{render_cb}->($self, $self->{w}, $self->{h});
110
111 } else { 142 } else {
112 ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type}) 143 glPixelZoom $tw / $self->{w}, $th / $self->{h};
113 = CFClient::load_image_inline $self->{image}; 144 glDrawPixels $self->{w}, $self->{h},
114 145 $self->{format},
115 $self->{internalformat} ||= $internalformat; 146 $self->{type},
147 $data;
148 glPixelZoom 1, 1;
149 }
116 } 150 }
117 151
118 my ($tw, $th) = @$self{qw(w h)};
119
120 $self->{minified} ||= [CFClient::average $tw, $th, $data]
121 if $self->{minify};
122
123 pad2pot $data, $tw, $th unless $GL_NPOT;
124
125 $self->{s} = $self->{w} / $tw;
126 $self->{t} = $self->{h} / $th;
127
128 $self->{name} ||= glGenTexture;
129
130 glBindTexture GL_TEXTURE_2D, $self->{name}; 152 glBindTexture GL_TEXTURE_2D, $self->{name} ||= glGenTexture;
131 153
132 if ($self->{wrap}) { 154 if ($self->{wrap}) {
133 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 155 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
134 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 156 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
135 } else { 157 } else {
136 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, $GL_VERSION >= 1.2 ? GL_CLAMP_TO_EDGE : GL_CLAMP; 158 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, $GL_VERSION >= 1.2 ? GL_CLAMP_TO_EDGE : GL_CLAMP;
137 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, $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;
138 } 160 }
139 161
140 if ($::FAST) { 162 if ($::FAST || $self->{nearest}) {
141 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; 163 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST;
142 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST; 164 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST;
143 } elsif ($self->{mipmap} && $GL_VERSION >= 1.4) { 165 } elsif ($self->{mipmap} && $GL_VERSION >= 1.4) {
144 glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1; 166 glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1;
145 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; 167 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
150 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR; 172 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;
151 } 173 }
152 174
153 glGetError; 175 glGetError;
154 176
155 if (defined $data) { 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 {
156 glTexImage2D GL_TEXTURE_2D, 0, 186 glTexImage2D GL_TEXTURE_2D, 0,
157 $self->{internalformat}, 187 $self->{internalformat},
158 $tw, $th, 188 $tw, $th,
159 0, 189 0,
160 $self->{format}, 190 $self->{format},
161 $self->{type}, 191 $self->{type},
162 $data; 192 $data;
163 gl_check "uploading texture %dx%d if=%x f=%x t=%x", 193 gl_check "uploading texture %dx%d if=%x f=%x t=%x",
164 $tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; 194 $tw, $th, $self->{internalformat}, $self->{format}, $self->{type};
165 } else {
166 glCopyTexImage2D GL_TEXTURE_2D, 0,
167 $self->{internalformat},
168 0, 0,
169 $tw, $th,
170 0;
171 gl_check "copying to texture %dx%d if=%x",
172 $tw, $th, $self->{internalformat};
173 } 195 }
196
197 $self->{s} = min 1, $self->{w} / $tw;
198 $self->{t} = min 1, $self->{h} / $th;
174} 199}
175 200
176sub shutdown { 201sub shutdown {
177 my ($self) = @_; 202 my ($self) = @_;
178 203
179 glDeleteTexture delete $self->{name} 204 glDeleteTexture $self->{name}
180 if $self->{name}; 205 if $self->{name};
181} 206}
182 207
183sub DESTROY { 208sub DESTROY {
184 my ($self) = @_; 209 my ($self) = @_;
186 delete $TEXTURES{$self+0}; 211 delete $TEXTURES{$self+0};
187 212
188 $self->shutdown; 213 $self->shutdown;
189} 214}
190 215
191$CFClient::OpenGL::INIT_HOOK{"CFClient::Texture"} = sub { 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;
192 $_->upload 220 $_->upload
193 for values %TEXTURES; 221 for values %TEXTURES;
194}; 222};
195 223
196$CFClient::OpenGL::SHUTDOWN_HOOK{"CFClient::Texture"} = sub { 224$CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Texture"} = sub {
197 $_->shutdown 225 $_->shutdown
198 for values %TEXTURES; 226 for values %TEXTURES;
199}; 227};
200 228
2011; 2291;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines