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.13 by root, Sat Dec 9 21:26:46 2006 UTC vs.
Revision 1.18 by root, Thu Jul 19 21:02:02 2007 UTC

14 14
15package CFPlus::Texture; 15package CFPlus::Texture;
16 16
17use strict; 17use strict;
18 18
19use List::Util qw(max min);
19use CFPlus::OpenGL; 20use CFPlus::OpenGL;
20 21
21my %TEXTURES; 22my %TEXTURES;
23my ($MAX_W, $MAX_H) = (4096, 4096); # maximum texture size attempted by this module
22 24
23sub new { 25sub new {
24 my ($class, %data) = @_; 26 my ($class, %data) = @_;
25 27
26 my $self = bless { 28 my $self = bless {
90sub upload { 92sub upload {
91 my ($self) = @_; 93 my ($self) = @_;
92 94
93 return unless $GL_VERSION; 95 return unless $GL_VERSION;
94 96
95 my $data; 97 # $tw,$th texture
98 # $rw,$rh rendered/used size
99 # $dw,$dh $data
100
101 my ($data, $dw, $dh);
96 102
97 if (exists $self->{data}) { 103 if (exists $self->{data}) {
98 $data = $self->{data}; 104 $data = $self->{data};
105 ($dw, $dh) = @$self{qw(w h)};
99 106
100 } elsif (exists $self->{render_cb}) { 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
120 defined $data or $self->{render_cb} or die; # some sanity check
121
122 $self->{minified} ||= [CFPlus::average $dw, $dh, $data]
123 if $self->{minify};
124
125 # against rather broken cards we enforce a maximum texture size
126 $tw = min $MAX_W, minpot $tw;
127 $th = min $MAX_H, minpot $th;
128
129 # if only pot-textures are allowed, pot'ify tw/th
130 unless ($GL_NPOT && 0) {#d#
131 $tw = minpot $tw;
132 $th = minpot $th;
133 }
134
135 # now further decrease texture size until the
136 # card does accept it
137 while (!texture_valid_2d $self->{internalformat}, $tw, $th, $self->{format}, $self->{type}) {
138 # quarter the texture size
139 $tw >>= 1;
140 $th >>= 1;
141 }
142
143 # decide the amount of space used in the texture
144 my ($rw, $rh);
145
146 if ($self->{render_cb}) {
147 # use only part of the texture
148 #$rw >>= 1 while $rw > $tw;
149 #$rh >>= 1 while $rh > $th;
150 $rw = min $dw, $tw;
151 $rh = min $dh, $th;
152 } else {
153 if ($self->{wrap} || $tw < $dw || $th < $dh) {
154 # scale to the full texture size
155 ($rw, $rh) = ($tw, $th);
156 } else {
157 # pad
158 pad $data, $dw, $dh, $tw, $th;
159 ($rw, $rh) = ($dw, $dh);
160 ($dw, $dh) = ($tw, $th);
161 }
162 }
163
164 # rendering means we need to scale in some way
165 my $render = $self->{render_cb} || $rw != $dw || $th != $dh;
166
167 if ($render) {
101 glViewport 0, 0, $self->{w}, $self->{h}; 168 glViewport 0, 0, $tw, $th;
169 glClear 0, 0, 0, 0;
102 glMatrixMode GL_PROJECTION; 170 glMatrixMode GL_PROJECTION;
103 glLoadIdentity; 171 glLoadIdentity;
104 glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000; 172 glOrtho 0, $tw, 0, $th, -10000, 10000;
105 glMatrixMode GL_MODELVIEW; 173 glMatrixMode GL_MODELVIEW;
106 glLoadIdentity; 174 glLoadIdentity;
175
176 if ($self->{render_cb}) {
177 glScale $rw / $dw, $rh / $dh;
107 $self->{render_cb}->($self, $self->{w}, $self->{h}); 178 $self->{render_cb}->($self, $rw, $rh);
108
109 } else { 179 } else {
110 ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type}) 180 glPixelZoom $tw / $dw, $th / $dh;
111 = CFPlus::load_image_inline $self->{image}; 181 glDrawPixels $dw, $dh,
112 182 $self->{format},
113 $self->{internalformat} ||= $internalformat; 183 $self->{type},
184 $data;
185 glPixelZoom 1, 1;
186 }
114 } 187 }
115 188
116 my ($tw, $th) = @$self{qw(w h)};
117
118 $self->{minified} ||= [CFPlus::average $tw, $th, $data]
119 if $self->{minify};
120
121 pad2pot $data, $tw, $th unless $GL_NPOT;
122
123 $self->{s} = $self->{w} / $tw;
124 $self->{t} = $self->{h} / $th;
125
126 $self->{name} ||= glGenTexture;
127
128 glBindTexture GL_TEXTURE_2D, $self->{name}; 189 glBindTexture GL_TEXTURE_2D, $self->{name} ||= glGenTexture;
129 190
130 if ($self->{wrap}) { 191 if ($self->{wrap}) {
131 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 192 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
132 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 193 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
133 } else { 194 } else {
148 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR; 209 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;
149 } 210 }
150 211
151 glGetError; 212 glGetError;
152 213
153 if (defined $data) { 214 if ($render) {
154 glTexImage2D GL_TEXTURE_2D, 0,
155 $self->{internalformat},
156 $tw, $th,
157 0,
158 $self->{format},
159 $self->{type},
160 $data;
161 gl_check "uploading texture %dx%d if=%x f=%x t=%x",
162 $tw, $th, $self->{internalformat}, $self->{format}, $self->{type};
163 } else {
164 exists $self->{render_cb} or die;
165 glCopyTexImage2D GL_TEXTURE_2D, 0, 215 glCopyTexImage2D GL_TEXTURE_2D, 0,
166 $self->{internalformat}, 216 $self->{internalformat},
167 0, 0, 217 0, 0,
168 $tw, $th, 218 $tw, $th,
169 0; 219 0;
170 gl_check "copying to texture %dx%d if=%x", 220 gl_check "copying to texture %dx%d if=%x",
171 $tw, $th, $self->{internalformat}; 221 $tw, $th, $self->{internalformat};
222 } else {
223 glTexImage2D GL_TEXTURE_2D, 0,
224 $self->{internalformat},
225 $dw, $dh,
226 0,
227 $self->{format},
228 $self->{type},
229 $data;
230 gl_check "uploading texture %dx%d if=%x f=%x t=%x",
231 $tw, $th, $self->{internalformat}, $self->{format}, $self->{type};
172 } 232 }
233
234 $self->{s} = $rw / $tw;
235 $self->{t} = $rh / $th;
173} 236}
174 237
175sub shutdown { 238sub shutdown {
176 my ($self) = @_; 239 my ($self) = @_;
177 240

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines