7 |
#define VOB_DEFINED(x) |
#define VOB_DEFINED(x) |
8 |
#endif |
#endif |
9 |
|
|
10 |
|
#include <boost/lambda/bind.hpp> |
11 |
|
|
12 |
|
#include <algorithm> |
13 |
|
|
14 |
|
#include <ext/slist> |
15 |
|
|
16 |
#include <vob/glerr.hxx> |
#include <vob/glerr.hxx> |
17 |
#include <vob/Debug.hxx> |
#include <vob/Debug.hxx> |
18 |
#include <vob/Transform.hxx> |
#include <vob/Transform.hxx> |
19 |
|
|
20 |
#include <vob/paper/Paper.hxx> |
#include <vob/paper/Paper.hxx> |
21 |
|
|
22 |
|
#include <vob/VecGL.hxx> |
23 |
|
|
24 |
|
#include <vob/poly/Dicer.hxx> |
25 |
|
|
26 |
namespace Vob { |
namespace Vob { |
27 |
namespace Vobs { |
namespace Vobs { |
28 |
|
|
29 |
PREDBGVAR(dbg_paperquad); |
PREDBGVAR(dbg_paperquad); |
30 |
|
|
31 |
|
class DiceTester { |
32 |
|
public: |
33 |
|
enum { NTrans = 1 }; |
34 |
|
|
35 |
|
float dicelen; |
36 |
|
int flags; |
37 |
|
int maxdepth; |
38 |
|
|
39 |
|
template<class F> void params(F &f) { |
40 |
|
f(dicelen, flags, maxdepth); |
41 |
|
} |
42 |
|
|
43 |
|
struct Verts { |
44 |
|
const Transform &t; |
45 |
|
Verts(const Transform &t) : t(t) { } |
46 |
|
vector<ZPt> finalPoints; |
47 |
|
vector<Pt> points; |
48 |
|
|
49 |
|
float dicelen; |
50 |
|
|
51 |
|
int append(Pt p) { |
52 |
|
DBG(dbg_paperquad) << "DiceTester append "<<p<<"\n"; |
53 |
|
int ind = points.size(); |
54 |
|
points.push_back(p); |
55 |
|
finalPoints.push_back(t.transform(p)); |
56 |
|
DBG(dbg_paperquad) << "DiceTester append ret "<<ind<<"\n"; |
57 |
|
return ind; |
58 |
|
} |
59 |
|
|
60 |
|
int operator() (int i, int j, float fract = .5) { |
61 |
|
DBG(dbg_paperquad) << "New vertex "<<i<<" "<<j<<" "<<fract<<"\n"; |
62 |
|
return append(lerp(points[i], points[j], fract) ); |
63 |
|
} |
64 |
|
|
65 |
|
float split(int i, int j) const { |
66 |
|
float ret = (finalPoints[i] - finalPoints[j]).xylength() / (float)dicelen - 1; |
67 |
|
DBG(dbg_paperquad) << "Split "<<i<<" "<<j<<" "<< |
68 |
|
finalPoints[i]<<" "<<finalPoints[j]<<" "<<ret<<"\n"; |
69 |
|
return ret; |
70 |
|
} |
71 |
|
}; |
72 |
|
|
73 |
|
struct Triangler { |
74 |
|
struct Tri { |
75 |
|
int v[3]; |
76 |
|
}; |
77 |
|
vector<Tri> tris; |
78 |
|
void operator()(int i, int j, int k) { |
79 |
|
DBG(dbg_paperquad) << "Triangler: "<<i<<" "<<j<<" "<<k<<"\n"; |
80 |
|
Tri t; |
81 |
|
t.v[0] = i; |
82 |
|
t.v[1] = j; |
83 |
|
t.v[2] = k; |
84 |
|
tris.push_back(t); |
85 |
|
} |
86 |
|
}; |
87 |
|
|
88 |
|
static float split(Verts &v, int i, int j) { return v.split(i,j); } |
89 |
|
|
90 |
|
template<class T> void render(const T &coords) const { |
91 |
|
::Vob::Dicer::InitialDicedTriangle<__gnu_cxx::slist<int> > tri; |
92 |
|
Triangler triangler; |
93 |
|
Verts verts(coords); |
94 |
|
verts.append(Pt(0,0)); |
95 |
|
verts.append(Pt(1,0)); |
96 |
|
verts.append(Pt(1,1)); |
97 |
|
verts.dicelen = dicelen; |
98 |
|
using namespace boost; |
99 |
|
using namespace boost::lambda; |
100 |
|
DBG(dbg_paperquad) << "Set_and_initial\n"; |
101 |
|
tri.set_and_initial_dice(verts, |
102 |
|
0, 1, 2, bind(split, ref(verts), _1, _2), maxdepth); |
103 |
|
DBG(dbg_paperquad) << "dice\n"; |
104 |
|
tri.dice(verts, triangler, |
105 |
|
bind(split, ref(verts), _1, _2), maxdepth); |
106 |
|
|
107 |
|
glBegin(GL_TRIANGLES); |
108 |
|
|
109 |
|
int ind = 0; |
110 |
|
for(vector<Triangler::Tri>::iterator i=triangler.tris.begin(); |
111 |
|
i != triangler.tris.end(); i++) { |
112 |
|
DBG(dbg_paperquad) << "tri \n"; |
113 |
|
if(flags & 1) { |
114 |
|
glColor3f(.5 + .5 * sin(.1 * ind), |
115 |
|
.5 + .5 * cos(.08751 * ind), |
116 |
|
.5 + .5 * sin(.07751 * ind)); |
117 |
|
} |
118 |
|
ind ++; |
119 |
|
for(int j=0; j<3; j++) |
120 |
|
VecGL::glVertex( verts.finalPoints[(*i).v[j]] ); |
121 |
|
} |
122 |
|
glEnd(); |
123 |
|
} |
124 |
|
|
125 |
|
}; |
126 |
|
|
127 |
|
VOB_DEFINED(DiceTester); |
128 |
|
|
129 |
const int PAPERQUAD_CS2_TO_SCREEN = 1; |
const int PAPERQUAD_CS2_TO_SCREEN = 1; |
130 |
const int PAPERQUAD_USE_VERTEX_PROGRAM = 2; |
const int PAPERQUAD_USE_VERTEX_PROGRAM = 2; |
131 |
|
const int PAPERQUAD_NONL_MAXLEN = 4; |
132 |
|
|
133 |
/** |
/** |
134 |
# PaperQuad is a bit complicated: there are three coordinate |
# PaperQuad is a bit complicated: there are three coordinate |
203 |
; |
; |
204 |
GLERR |
GLERR |
205 |
|
|
206 |
ZPt ctr = ZPt(lerp(x0, x1, 0.5), lerp(y0, y1, 0.5), 0); |
|
207 |
double len = hypot(x1-x0, y1-y0) / 2; |
int dice; |
208 |
double nonl = coords1.nonlinearity(ctr, len); |
|
209 |
|
if(flags & PAPERQUAD_NONL_MAXLEN) { |
210 |
int dice = (int)(len * nonl * dicefactor) + 2; |
Pt p1 = coords1.transform(Pt(x0,y0)); |
211 |
|
Pt p2 = coords1.transform(Pt(x0,y1)); |
212 |
|
Pt p3 = coords1.transform(Pt(x1,y0)); |
213 |
|
Pt p4 = coords1.transform(Pt(x1,y1)); |
214 |
|
float dist[4] = { |
215 |
|
(p2-p1).length(), |
216 |
|
(p3-p1).length(), |
217 |
|
(p4-p2).length(), |
218 |
|
(p4-p3).length() |
219 |
|
}; |
220 |
|
float m = *std::max_element(dist, dist+4); |
221 |
|
|
222 |
|
dice = (int)(m / dicefactor) + 2; |
223 |
|
} else { // old way |
224 |
|
|
225 |
|
ZPt ctr = ZPt(lerp(x0, x1, 0.5), lerp(y0, y1, 0.5), 0); |
226 |
|
double len = hypot(x1-x0, y1-y0) / 2; |
227 |
|
double nonl = coords1.nonlinearity(ctr, len); |
228 |
|
|
229 |
|
dice = (int)(len * nonl * dicefactor) + 2; |
230 |
|
} |
231 |
DBG(dbg_paperquad) << "Dice: " << dice <<"\\n"; |
DBG(dbg_paperquad) << "Dice: " << dice <<"\\n"; |
232 |
if(dice < 0 || dice > 100) dice = 100; |
// Cap it at a ridiculous value |
233 |
|
if( dice > 100) dice = 100; |
234 |
|
if(dice < 2 ) dice = 2; |
235 |
|
|
236 |
float *vertices = new float[dice * dice * 5]; |
float *vertices = new float[dice * dice * 5]; |
237 |
|
|