34 |
#include <vob/glerr.hxx> |
#include <vob/glerr.hxx> |
35 |
#include <vob/text/QuadFont.hxx> |
#include <vob/text/QuadFont.hxx> |
36 |
#include <vob/Types.hxx> |
#include <vob/Types.hxx> |
37 |
|
#include <vob/VecGL.hxx> |
38 |
|
|
39 |
namespace Vob { |
namespace Vob { |
40 |
namespace Vobs { |
namespace Vobs { |
41 |
|
using namespace VecGL; |
42 |
|
|
43 |
PREDBGVAR(dbg_text); |
PREDBGVAR(dbg_text); |
44 |
|
|
146 |
|
|
147 |
VOB_DEFINED(Text1); |
VOB_DEFINED(Text1); |
148 |
|
|
149 |
|
/** In a triangle with the given vertices, find |
150 |
|
* the change of t at unit change of v in x or y direction. |
151 |
|
*/ |
152 |
|
std::pair<Pt,Pt> texDerivs(Pt v00, Pt v01, Pt v10, Pt t00, Pt t01, Pt t10) { |
153 |
|
// Now, find out the correct offsets to the texture |
154 |
|
// coordinates. |
155 |
|
Pt va = v10-v00; Pt ta = t10-t00; |
156 |
|
Pt vb = v01-v00; Pt tb = t01-t00; |
157 |
|
|
158 |
|
if(fabs(vb.x) > fabs(va.x)) { |
159 |
|
std::swap(va,vb); |
160 |
|
std::swap(ta,tb); |
161 |
|
} |
162 |
|
// va now greater in x dir |
163 |
|
if(fabs(va.x) < 0.000001) va.x = 1; // avoid inf |
164 |
|
float mul = - vb.x / va.x; |
165 |
|
// This should now have zero x coordinate |
166 |
|
Pt vy = vb + mul*va; |
167 |
|
if(fabs(vy.y) < 0.000001) vy.y = 1; // avoid inf |
168 |
|
Pt ty = (tb + mul*ta); |
169 |
|
|
170 |
|
// Zero y coordinate |
171 |
|
Pt vx = va - (va.y / vy.y)*vy; |
172 |
|
Pt tx = ta - (va.y / vy.y)*ty; |
173 |
|
|
174 |
|
DBG(dbg_text) << "texDerivs calc: " |
175 |
|
<<va<<vb<<ta<<tb<<mul<<"\n"; |
176 |
|
DBG(dbg_text) << "texDerivs vxy: " |
177 |
|
<<vx<<vy<<tx<<ty<<"\n"; |
178 |
|
|
179 |
|
|
180 |
|
return std::pair<Pt,Pt>(ty * (1/vy.y),tx * (1/vx.x)); |
181 |
|
} |
182 |
|
|
183 |
|
|
184 |
|
/** An implemenentation of text rendering |
185 |
|
* using 4 texunits for supersampling. |
186 |
|
* This vob only sets up the texture coordinates. |
187 |
|
*/ |
188 |
|
template<class str> struct TextSuper4Base { |
189 |
|
enum { NTrans = 1 }; |
190 |
|
|
191 |
|
|
192 |
|
Text::QuadFont *font; |
193 |
|
str text; |
194 |
|
float yoffs; |
195 |
|
int flags; |
196 |
|
|
197 |
|
template<class F> void params(F &f) { |
198 |
|
f(font, text, yoffs, flags); |
199 |
|
} |
200 |
|
|
201 |
|
template<class T> void render(const T &t) const { |
202 |
|
int curTexInd = -1; |
203 |
|
int minGlyph = 0; int nGlyphs = font->textureIndex.size(); |
204 |
|
GLERR; |
205 |
|
|
206 |
|
float x = 0; |
207 |
|
float y = yoffs; |
208 |
|
glBegin(GL_QUADS); |
209 |
|
for (typename str::const_iterator it = text.begin(); |
210 |
|
it != text.end(); ++it) { |
211 |
|
int glyph = *it; |
212 |
|
if(glyph < minGlyph || glyph >= nGlyphs) |
213 |
|
continue; |
214 |
|
int texInd = font->textureIndex[glyph]; |
215 |
|
if(texInd < 0) continue; |
216 |
|
if(curTexInd != texInd) { |
217 |
|
glEnd(); |
218 |
|
GLERR; |
219 |
|
font->bindTextures(texInd); |
220 |
|
GLERR; |
221 |
|
glBegin(GL_QUADS); |
222 |
|
curTexInd = texInd; |
223 |
|
} |
224 |
|
// Won't use font->texCoords since |
225 |
|
// it depends on the transformation |
226 |
|
|
227 |
|
#define do_corner(ver, tex, a, b) \ |
228 |
|
Pt tex = Pt( \ |
229 |
|
font->coordinates[8*glyph + 4 + a], \ |
230 |
|
font->coordinates[8*glyph + 4 + b]); \ |
231 |
|
ZPt ver = t.transform(ZPt( \ |
232 |
|
x + font->coordinates[8*glyph + 0 + a], \ |
233 |
|
y + font->coordinates[8*glyph + 0 + b], 0)); |
234 |
|
|
235 |
|
do_corner(v00, t00, 0,1); |
236 |
|
do_corner(v10, t10, 2,1); |
237 |
|
do_corner(v11, t11, 2,3); |
238 |
|
do_corner(v01, t01, 0,3); |
239 |
|
#undef do_corner |
240 |
|
|
241 |
|
std::pair<Pt,Pt> deriv0 = texDerivs(v00,v01,v10, t00,t01,t10); |
242 |
|
|
243 |
|
#define do_vertex(ver, tex) \ |
244 |
|
glMultiTexCoord(GL_TEXTURE0, tex - .25 * deriv0.first - .25*deriv0.second); \ |
245 |
|
glMultiTexCoord(GL_TEXTURE1, tex + .25 * deriv0.first - .25*deriv0.second); \ |
246 |
|
glMultiTexCoord(GL_TEXTURE2, tex - .25 * deriv0.first + .25*deriv0.second); \ |
247 |
|
glMultiTexCoord(GL_TEXTURE3, tex + .25 * deriv0.first + .25*deriv0.second); \ |
248 |
|
glVertex(ver); |
249 |
|
|
250 |
|
do_vertex(v00, t00); |
251 |
|
do_vertex(v10, t10); |
252 |
|
do_vertex(v11, t11); |
253 |
|
do_vertex(v01, t01); |
254 |
|
|
255 |
|
DBG(dbg_text)<<"Coords: "<<v00<<v10<<v11<<v01<<"\n"; |
256 |
|
DBG(dbg_text)<<"Tex: "<<t00<<t10<<t11<<t01<<"\n"; |
257 |
|
DBG(dbg_text)<<"derivs: "<<deriv0.first<<deriv0.second<<"\n"; |
258 |
|
|
259 |
|
// Assume the transformation is the same all through the quad. |
260 |
|
// pair<Pt,Pt> deriv1 = texDerivs(v11,v01,v10, t11,t01,t10); |
261 |
|
|
262 |
|
x += font->advances[glyph]; |
263 |
|
|
264 |
|
|
265 |
|
} |
266 |
|
glEnd(); |
267 |
|
font->unbindTextures(); |
268 |
|
GLERR; |
269 |
|
|
270 |
|
DBG(dbg_text) << "End text render\n"; |
271 |
|
|
272 |
|
} |
273 |
|
|
274 |
|
}; |
275 |
|
|
276 |
|
typedef TextSuper4Base<unicodecharvector> TextSuper4; |
277 |
|
|
278 |
|
VOB_DEFINED(TextSuper4); |
279 |
|
|
280 |
|
|
281 |
} |
} |
282 |
} |
} |
283 |
|
|