2 |
|
|
3 |
#include <sstream> |
#include <sstream> |
4 |
|
|
5 |
/* the matrix that maps v[TEX0] into o[TEXi] is stored |
/** The matrix that maps v[TEX0] into o[TEXi] is stored |
6 |
* starting at c[tex_addr_base + 4*i] */ |
* starting at c[tex_addr_base + 4*i]. |
7 |
|
*/ |
8 |
#define tex_addr_base 60 |
#define tex_addr_base 60 |
9 |
|
|
10 |
namespace Paper { |
namespace Paper { |
11 |
|
|
12 |
TexGen::TexGen(const float *tex_mat) { |
TexGen::TexGen(const float *tex_mat) { |
13 |
this->tex_mat[0] = tex_mat[0]; |
this->tex_mat[0] = tex_mat[0]; |
14 |
this->tex_mat[1] = tex_mat[1]; |
this->tex_mat[1] = tex_mat[1]; |
41 |
glProgramParameter4fvNV(GL_VERTEX_PROGRAM_NV, base + 3, tex_mat + 12); |
glProgramParameter4fvNV(GL_VERTEX_PROGRAM_NV, base + 3, tex_mat + 12); |
42 |
#endif |
#endif |
43 |
} |
} |
44 |
|
|
45 |
string TexGen::getVPCode(int unit) { |
string TexGen::getVPCode(int unit) { |
46 |
std::ostringstream code; |
std::ostringstream code; |
47 |
|
|
54 |
return code.str(); |
return code.str(); |
55 |
} |
} |
56 |
|
|
57 |
void TexGen::setUp(LightParam *param) { |
void TexGen::setUp_explicit(LightParam * param) { |
58 |
|
explicit_mat = tex_mat; |
59 |
|
} |
60 |
|
|
61 |
|
void TexGen::setUp_texgen(LightParam *param) { |
62 |
glMatrixMode(GL_MODELVIEW); |
glMatrixMode(GL_MODELVIEW); |
63 |
glPushMatrix(); |
glPushMatrix(); |
64 |
|
|
82 |
} |
} |
83 |
|
|
84 |
void TexGenEmboss::setUp_VP(int unit, LightParam *param) { |
void TexGenEmboss::setUp_VP(int unit, LightParam *param) { |
85 |
// Suppose |
/** Suppose |
86 |
// x = vertex position |
* x = vertex position |
87 |
// p = paper coordinates |
* p = paper coordinates |
88 |
// s = texture coordinates |
* s = texture coordinates |
89 |
// A = paper-to-vertex mapping |
* A = paper-to-vertex mapping |
90 |
// E = embossing mapping (translates x towards light) |
* E = embossing mapping (translates x towards light) |
91 |
// M = modelview matrix |
* M = modelview matrix |
92 |
// T = texture matrix |
* T = texture matrix |
93 |
// |
* |
94 |
// The usual eye-linear embossing texgen computes |
* The usual eye-linear embossing texgen computes |
95 |
// s' = Tp' = T((MEA)^-1 M x) = T(A^-1 E^-1 x) |
* s' = Tp' = T((MEA)^-1 M x) = T(A^-1 E^-1 x) |
96 |
// The usual simple texgen computes |
* The usual simple texgen computes |
97 |
// s = Tp = T((MA)^-1 M x) = T(A^-1 x) ==> x = Ap |
* s = Tp = T((MA)^-1 M x) = T(A^-1 x) ==> x = Ap |
98 |
// Thus, the mapping from p to s' is |
* Thus, the mapping from p to s' is |
99 |
// s' = Tp' = T(A^-1 E^-1 x) = T A^-1 E^-1 A p |
* s' = Tp' = T(A^-1 E^-1 x) = T A^-1 E^-1 A p |
100 |
// |
* |
101 |
// The following code computes T A^-1 E^-1 A and stores it |
* The following code computes T A^-1 E^-1 A and stores it |
102 |
// in place of T in the constant registers |
* in place of T in the constant registers. |
103 |
|
*/ |
104 |
|
|
105 |
#ifdef GL_VERTEX_PROGRAM_NV |
#ifdef GL_VERTEX_PROGRAM_NV |
106 |
GLuint base = unit * 4 + tex_addr_base; |
GLuint base = unit * 4 + tex_addr_base; |
146 |
#endif |
#endif |
147 |
} |
} |
148 |
|
|
149 |
|
void TexGenEmboss::setUp_texgen(LightParam *param) { |
|
void TexGenEmboss::setUp(LightParam *param) { |
|
150 |
glMatrixMode(GL_MODELVIEW); |
glMatrixMode(GL_MODELVIEW); |
151 |
glPushMatrix(); |
glPushMatrix(); |
152 |
|
|
173 |
glPopMatrix(); |
glPopMatrix(); |
174 |
} |
} |
175 |
|
|
176 |
|
void TexGenEmboss::setUp_explicit(LightParam * param) { |
177 |
|
std::cerr << "Warning: setUp_explicit for TexGenEmboss not implemented yet"; |
178 |
|
explicit_mat = tex_mat; |
179 |
|
} |
180 |
|
|
181 |
TexGenLightmap::TexGenLightmap(const float *tex_mat, |
TexGenLightmap::TexGenLightmap(const float *tex_mat, |
182 |
const float *inv_mat) |
const float *inv_mat) |
183 |
: TexGen(tex_mat) { |
: TexGen(tex_mat) { |
219 |
glEnable(GL_TEXTURE_GEN_T); |
glEnable(GL_TEXTURE_GEN_T); |
220 |
glPopMatrix(); |
glPopMatrix(); |
221 |
} |
} |
222 |
|
|
|
|
|
223 |
LightDirSetup::LightDirSetup(const float *tex_mat) { |
LightDirSetup::LightDirSetup(const float *tex_mat) { |
224 |
mat[0] = tex_mat[0]; |
mat[0] = tex_mat[0]; |
225 |
mat[1] = tex_mat[1]; |
mat[1] = tex_mat[1]; |
226 |
mat[2] = tex_mat[4]; |
mat[2] = tex_mat[4]; |
227 |
mat[3] = tex_mat[5]; |
mat[3] = tex_mat[5]; |
228 |
} |
} |
229 |
|
|
230 |
void LightDirSetup::setUp(LightParam *param) { |
void LightDirSetup::setUp(LightParam *param) { |
231 |
//ZVec Li = inverse_map(e0, e1, e2, Light).normalize(); |
//ZVec Li = inverse_map(e0, e1, e2, Light).normalize(); |
232 |
ZVec Li = ZVec(param->e0.dot(param->Light), param->e1.dot(param->Light), param->e2.dot(param->Light)).normalize(); |
ZVec Li = ZVec(param->e0.dot(param->Light), param->e1.dot(param->Light), param->e2.dot(param->Light)).normalize(); |
235 |
mat[2] * Li.x + mat[3] * Li.y, Li.z); |
mat[2] * Li.x + mat[3] * Li.y, Li.z); |
236 |
glColor3f(.5 * L.x + .5, .5 * L.y + .5, .5 * L.z + .5); |
glColor3f(.5 * L.x + .5, .5 * L.y + .5, .5 * L.z + .5); |
237 |
} |
} |
|
|
|
238 |
|
|
239 |
void PaperPass::setUp_texgen(LightParam *param) { |
void PaperPass::setUp_texgen(LightParam *param) { |
240 |
setupcode(); |
setupcode(); |
243 |
GLenum unit = GL_TEXTURE0_ARB; |
GLenum unit = GL_TEXTURE0_ARB; |
244 |
for (vector<shared_ptr<TexGen> >::iterator it = texgen.begin(); it != texgen.end(); ++it) { |
for (vector<shared_ptr<TexGen> >::iterator it = texgen.begin(); it != texgen.end(); ++it) { |
245 |
glActiveTextureARB(unit++); |
glActiveTextureARB(unit++); |
246 |
if (it->get()) (*it)->setUp(param); |
if (it->get()) (*it)->setUp_texgen(param); |
247 |
else std::cerr << "Warning: ignoring null TexGen\n"; |
else std::cerr << "Warning: ignoring null TexGen\n"; |
248 |
} |
} |
249 |
|
|
306 |
glDisable(GL_VERTEX_PROGRAM_NV); |
glDisable(GL_VERTEX_PROGRAM_NV); |
307 |
#endif |
#endif |
308 |
} |
} |
309 |
|
|
310 |
|
void PaperPass::setUp_explicit(LightParam *param) { |
311 |
|
setupcode(); |
312 |
|
|
313 |
|
/* Set up explicit TexGen parameters for each texture unit */ |
314 |
|
for (vector<shared_ptr<TexGen> >::iterator it = texgen.begin(); it != texgen.end(); ++it) { |
315 |
|
if (it->get()) (*it)->setUp_explicit(param); |
316 |
|
else std::cerr << "Warning: ignoring null TexGen\n"; |
317 |
|
} |
318 |
|
|
319 |
|
/* Do general parametric setup */ |
320 |
|
for (vector<shared_ptr<LightSetup> >::iterator it = setup.begin(); it != setup.end(); ++it) { |
321 |
|
if (it->get()) (*it)->setUp(param); |
322 |
|
else std::cerr << "Warning: ignoring null LightSetup\n"; |
323 |
|
} |
324 |
|
} |
325 |
|
|
326 |
|
void PaperPass::vertex_explicit(float *ppos) { |
327 |
|
const float *mat; |
328 |
|
float texCoords[4]; |
329 |
|
|
330 |
|
GLenum unit = GL_TEXTURE0_ARB; |
331 |
|
|
332 |
|
/* Multiplicates TexGen's explicit_mat with ppos for proper texCoords */ |
333 |
|
for (vector<shared_ptr<TexGen> >::iterator it = texgen.begin(); it != texgen.end(); ++it) { |
334 |
|
if (it->get()) { |
335 |
|
mat = (*it)->explicit_mat; |
336 |
|
texCoords[0] = mat[0] * ppos[0] + mat[1] * ppos[1] + mat[2] * ppos[2] + mat[3] * ppos[3]; |
337 |
|
texCoords[1] = mat[4] * ppos[0] + mat[5] * ppos[1] + mat[6] * ppos[2] + mat[7] * ppos[3]; |
338 |
|
texCoords[2] = mat[8] * ppos[0] + mat[9] * ppos[1] + mat[10] * ppos[2] + mat[11] * ppos[3]; |
339 |
|
texCoords[3] = mat[12] * ppos[0] + mat[13] * ppos[1] + mat[14] * ppos[2] + mat[15] * ppos[3]; |
340 |
|
|
341 |
|
glMultiTexCoord4fvARB(unit, texCoords); |
342 |
|
} |
343 |
|
else std::cerr << "Warning: ignoring null TexGen\n"; |
344 |
|
unit++; |
345 |
|
} |
346 |
|
} |
347 |
} |
} |
348 |
|
|
349 |
|
|
350 |
|
|
351 |
|
|
352 |
|
|
353 |
|
|
354 |
|
|