2 |
|
|
3 |
#include <sstream> |
#include <sstream> |
4 |
|
|
5 |
|
/* define positions of tracked matrices inside the NV vertex program registers */ |
6 |
|
#define m_p_addr 0 // product of projection and modelview matrices |
7 |
|
#define paper_addr 4 // object-to-paper-coordinate mapping matrix |
8 |
|
|
9 |
namespace Paper { |
namespace Paper { |
10 |
TexGen::TexGen(const float *tex_mat) { |
TexGen::TexGen(const float *tex_mat) { |
11 |
svec[0] = tex_mat[0]; |
svec[0] = tex_mat[0]; |
22 |
rvec[1] = tex_mat[9]; |
rvec[1] = tex_mat[9]; |
23 |
rvec[2] = tex_mat[10]; |
rvec[2] = tex_mat[10]; |
24 |
rvec[3] = tex_mat[11]; |
rvec[3] = tex_mat[11]; |
|
} |
|
25 |
|
|
26 |
void TexGen::setUpVP(int unit) { |
qvec[0] = 0; |
27 |
GLuint base = unit * 4 + 60; |
qvec[0] = 0; |
28 |
|
qvec[0] = 0; |
29 |
|
qvec[0] = 1; |
30 |
|
} |
31 |
|
|
32 |
|
void TexGen::setUpVP(int unit, LightParam * param) { |
33 |
// XXX: This could also be implemented as CallGL code |
// XXX: This could also be implemented as CallGL code |
34 |
#ifdef GL_VERTEX_PROGRAM_NV |
#ifdef GL_VERTEX_PROGRAM_NV |
35 |
|
GLuint base = unit * 4 + 60; |
36 |
glProgramParameter4fvNV(GL_VERTEX_PROGRAM_NV, base + 0, svec); |
glProgramParameter4fvNV(GL_VERTEX_PROGRAM_NV, base + 0, svec); |
37 |
glProgramParameter4fvNV(GL_VERTEX_PROGRAM_NV, base + 1, tvec); |
glProgramParameter4fvNV(GL_VERTEX_PROGRAM_NV, base + 1, tvec); |
38 |
glProgramParameter4fvNV(GL_VERTEX_PROGRAM_NV, base + 2, rvec); |
glProgramParameter4fvNV(GL_VERTEX_PROGRAM_NV, base + 2, rvec); |
39 |
|
glProgramParameter4fvNV(GL_VERTEX_PROGRAM_NV, base + 3, qvec); |
|
//float qvec[4] = { 0, 0, 0, 1 }; |
|
|
//glProgramParameter4fvNV(GL_VERTEX_PROGRAM_NV, base + 3, qvec); |
|
40 |
#endif |
#endif |
41 |
} |
} |
42 |
|
|
48 |
code << "DP4 o[TEX" << unit << "].x, c[" << base + 0 << "], v[TEX0]\n" |
code << "DP4 o[TEX" << unit << "].x, c[" << base + 0 << "], v[TEX0]\n" |
49 |
<< "DP4 o[TEX" << unit << "].y, c[" << base + 1 << "], v[TEX0]\n" |
<< "DP4 o[TEX" << unit << "].y, c[" << base + 1 << "], v[TEX0]\n" |
50 |
<< "DP4 o[TEX" << unit << "].z, c[" << base + 2 << "], v[TEX0]\n" |
<< "DP4 o[TEX" << unit << "].z, c[" << base + 2 << "], v[TEX0]\n" |
51 |
<< "MOV o[TEX" << unit << "].w, v[TEX0].w\n"; |
<< "DP4 o[TEX" << unit << "].w, c[" << base + 3 << "], v[TEX0]\n"; |
|
// << "DP4 o[TEX" << unit << "].w, c[" << base + 3 << "], v[TEX0]\n"; |
|
52 |
|
|
53 |
return code.str(); |
return code.str(); |
54 |
} |
} |
76 |
glPopMatrix(); |
glPopMatrix(); |
77 |
} |
} |
78 |
|
|
79 |
|
void TexGenEmboss::setUpVP(int unit, LightParam * param) { |
80 |
|
TexGen::setUpVP(unit, param); |
81 |
|
|
82 |
|
float mat0[16]; |
83 |
|
glGetFloatv(GL_MODELVIEW_MATRIX, mat0); |
84 |
|
|
85 |
|
float mat[16] = { param->e0.x, param->e0.y, param->e0.z, 0, |
86 |
|
param->e1.x, param->e1.y, param->e1.z, 0, |
87 |
|
param->e2.x, param->e2.y, param->e2.z, 0, |
88 |
|
param->orig.x, param->orig.y, param->orig.z, 1 }; |
89 |
|
|
90 |
|
glMatrixMode(GL_MATRIX0_NV); |
91 |
|
glLoadMatrixf(mat0); |
92 |
|
glMultMatrixf(mat); |
93 |
|
glMatrixMode(GL_MODELVIEW); |
94 |
|
|
95 |
|
glTrackMatrixNV(GL_VERTEX_PROGRAM_NV, paper_addr, GL_MATRIX0_NV, GL_INVERSE_TRANSPOSE_NV); |
96 |
|
|
97 |
|
} |
98 |
|
|
99 |
|
|
100 |
|
string TexGenEmboss::getVPCode(int unit) { |
101 |
|
GLuint base = unit * 4 + 60; |
102 |
|
|
103 |
|
std::ostringstream code; |
104 |
|
|
105 |
|
code << "DP4 o[TEX" << unit << "].x, c[" << base + 0 << "], v[TEX0]\n" |
106 |
|
<< "DP4 o[TEX" << unit << "].y, c[" << base + 1 << "], v[TEX0]\n" |
107 |
|
<< "DP4 o[TEX" << unit << "].z, c[" << base + 2 << "], v[TEX0]\n" |
108 |
|
<< "DP4 o[TEX" << unit << "].w, c[" << base + 3 << "], v[TEX0]\n"; |
109 |
|
|
110 |
|
// XXX: compute embossing shift |
111 |
|
|
112 |
|
return code.str(); |
113 |
|
} |
114 |
|
|
115 |
|
|
116 |
void TexGenEmboss::setUp(LightParam *param) { |
void TexGenEmboss::setUp(LightParam *param) { |
117 |
glMatrixMode(GL_MODELVIEW); |
glMatrixMode(GL_MODELVIEW); |
118 |
glPushMatrix(); |
glPushMatrix(); |
219 |
} |
} |
220 |
|
|
221 |
void PaperPass::loadVP() { |
void PaperPass::loadVP() { |
222 |
string code = |
std::ostringstream code; |
223 |
"!!VP1.0\n" |
|
224 |
"MOV o[HPOS], v[OPOS]\n" |
code << "!!VP1.0\n" |
225 |
"MOV o[COL0], v[COL0]\n" |
<< "DP4 o[HPOS].x, v[OPOS], c[" << m_p_addr + 0 << "]\n" |
226 |
"MOV o[COL1], v[COL1]\n"; |
<< "DP4 o[HPOS].y, v[OPOS], c[" << m_p_addr + 1 << "]\n" |
227 |
|
<< "DP4 o[HPOS].z, v[OPOS], c[" << m_p_addr + 2 << "]\n" |
228 |
|
<< "DP4 o[HPOS].w, v[OPOS], c[" << m_p_addr + 3 << "]\n" |
229 |
|
<< "MOV o[COL0], v[COL0]\n" |
230 |
|
<< "MOV o[COL1], v[COL1]\n"; |
231 |
|
|
232 |
int unit = 0; |
int unit = 0; |
233 |
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) { |
234 |
if (it->get()) code += (*it)->getVPCode(unit); |
if (it->get()) code << (*it)->getVPCode(unit); |
235 |
else std::cerr << "Warning: ignoring null TexGen\n"; |
else std::cerr << "Warning: ignoring null TexGen\n"; |
236 |
unit++; |
unit++; |
237 |
} |
} |
238 |
|
|
239 |
std::cerr << "Creating VPCode with the source " << code << "\n"; |
std::cerr << "Creating VPCode with the source " << code.str() << "\n"; |
240 |
|
|
241 |
texgenvp = VPCode(code.c_str()); |
texgenvp = VPCode(code.str().c_str()); |
242 |
} |
} |
243 |
|
|
244 |
void PaperPass::setUpVP(LightParam *param) { |
void PaperPass::setUpVP(LightParam *param) { |
245 |
if (texgenvp.getSource().length() == 0) |
if (texgenvp.getSource().length() == 0) |
246 |
loadVP(); |
loadVP(); |
247 |
|
|
248 |
|
/* Track the product of modelview and projection matrices */ |
249 |
|
glTrackMatrixNV(GL_VERTEX_PROGRAM_NV, m_p_addr, GL_MATRIX0_NV, GL_MODELVIEW_PROJECTION_NV); |
250 |
|
|
251 |
/* Set up VP TexGen parameters for each texture unit */ |
/* Set up VP TexGen parameters for each texture unit */ |
252 |
int unit = 0; |
int unit = 0; |
253 |
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) { |
254 |
if (it->get()) (*it)->setUpVP(unit); |
if (it->get()) (*it)->setUpVP(unit, param); |
255 |
else std::cerr << "Warning: ignoring null TexGen\n"; |
else std::cerr << "Warning: ignoring null TexGen\n"; |
256 |
unit++; |
unit++; |
257 |
} |
} |