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.14 by root, Tue Jul 17 13:53: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 my ($tw, $th, $data);
96 98
97 if (exists $self->{data}) { 99 if (exists $self->{data}) {
98 $data = $self->{data}; 100 $data = $self->{data};
101 ($tw, $th) = @$self{qw(w h)};
99 102
100 } 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 $need_render = $self->{render_cb} || $tw < $self->{w} || $th < $self->{h};
131
132 if ($need_render) {
101 glViewport 0, 0, $self->{w}, $self->{h}; 133 glViewport 0, 0, $tw, $th;
102 glMatrixMode GL_PROJECTION; 134 glMatrixMode GL_PROJECTION;
103 glLoadIdentity; 135 glLoadIdentity;
104 glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000; 136 glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000;
105 glMatrixMode GL_MODELVIEW; 137 glMatrixMode GL_MODELVIEW;
106 glLoadIdentity; 138 glLoadIdentity;
139
140 if ($self->{render_cb}) {
107 $self->{render_cb}->($self, $self->{w}, $self->{h}); 141 $self->{render_cb}->($self, $self->{w}, $self->{h});
108
109 } else { 142 } else {
110 ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type}) 143 glPixelZoom $tw / $self->{w}, $th / $self->{h};
111 = CFPlus::load_image_inline $self->{image}; 144 glDrawPixels $self->{w}, $self->{h},
112 145 $self->{format},
113 $self->{internalformat} ||= $internalformat; 146 $self->{type},
147 $data;
148 glPixelZoom 1, 1;
149 }
114 } 150 }
115 151
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}; 152 glBindTexture GL_TEXTURE_2D, $self->{name} ||= glGenTexture;
129 153
130 if ($self->{wrap}) { 154 if ($self->{wrap}) {
131 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 155 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
132 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 156 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
133 } else { 157 } else {
148 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR; 172 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;
149 } 173 }
150 174
151 glGetError; 175 glGetError;
152 176
153 if (defined $data) { 177 if ($need_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 {
154 glTexImage2D GL_TEXTURE_2D, 0, 186 glTexImage2D GL_TEXTURE_2D, 0,
155 $self->{internalformat}, 187 $self->{internalformat},
156 $tw, $th, 188 $tw, $th,
157 0, 189 0,
158 $self->{format}, 190 $self->{format},
159 $self->{type}, 191 $self->{type},
160 $data; 192 $data;
161 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",
162 $tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; 194 $tw, $th, $self->{internalformat}, $self->{format}, $self->{type};
163 } else {
164 exists $self->{render_cb} or die;
165 glCopyTexImage2D GL_TEXTURE_2D, 0,
166 $self->{internalformat},
167 0, 0,
168 $tw, $th,
169 0;
170 gl_check "copying to texture %dx%d if=%x",
171 $tw, $th, $self->{internalformat};
172 } 195 }
196
197 $self->{s} = min 1, $self->{w} / $tw;
198 $self->{t} = min 1, $self->{h} / $th;
173} 199}
174 200
175sub shutdown { 201sub shutdown {
176 my ($self) = @_; 202 my ($self) = @_;
177 203

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines