-
Notifications
You must be signed in to change notification settings - Fork 0
/
thicker lines.rtf
78 lines (78 loc) · 4.08 KB
/
thicker lines.rtf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}}
{\*\generator Msftedit 5.41.15.1503;}\viewkind4\uc1\pard\f0\fs20 //////////////////////////////////////////////////////////////////////////////////////////////////////////\par
/**\par
* Computes a screen quad aligned with a world segment.\par
* \\param inverseview [in] inverse view matrix\par
* \\param view [in] view matrix\par
* \\param proj [in] projection matrix\par
* \\param verts [out] 4 quad vertices in world space (forming a tri-strip)\par
* \\param uvs [out] 4 quad uvs\par
* \\param stride [in] size of vertex (FVF stride)\par
* \\param p0 [in] segment's first point in world space\par
* \\param p1 [in] segment's second point in world space\par
* \\param size [in] size of segment/quad\par
* \\param constantsize [in] true to keep the quad's screen size constant (e.g. needed to emulate glLineWidth)\par
*/\par
//////////////////////////////////////////////////////////////////////////////////////////////////////////\par
void Renderer::ComputeScreenQuad(const ViewMatrix& inverseview, const ViewMatrix& view, const ProjMatrix& proj,\par
ubyte* verts, ubyte* uvs, udword stride, const Point& p0, const Point& p1, float size, bool constantsize)\par
\{\par
// Compute delta in camera space\par
Point Delta; TransformPoint3x3(Delta, p1-p0, view);\par
// Compute size factors\par
float SizeP0 = size;\par
float SizeP1 = size;\par
if(constantsize)\par
\{\par
// Compute scales so that screen-size is constant\par
SizeP0 *= ComputeConstantScale(p0, view, proj);\par
SizeP1 *= ComputeConstantScale(p1, view, proj);\par
\}\par
// Compute quad vertices\par
float Theta0 = atan2f(-Delta.x, -Delta.y);\par
float c0 = SizeP0 * cosf(Theta0);\par
float s0 = SizeP0 * sinf(Theta0);\par
ComputePoint(*((Point*)verts), c0, -s0, inverseview, p0); verts+=stride;\par
ComputePoint(*((Point*)verts), -c0, s0, inverseview, p0); verts+=stride;\par
float Theta1 = atan2f(Delta.x, Delta.y);\par
float c1 = SizeP1 * cosf(Theta1);\par
float s1 = SizeP1 * sinf(Theta1);\par
ComputePoint(*((Point*)verts), -c1, s1, inverseview, p1); verts+=stride;\par
ComputePoint(*((Point*)verts), c1, -s1, inverseview, p1); verts+=stride;\par
// Output uvs if needed\par
if(uvs)\par
\{\par
*((float*)uvs) = 0.0f; *((float*)(uvs+4)) = 1.0f; uvs+=stride;\par
*((float*)uvs) = 0.0f; *((float*)(uvs+4)) = 0.0f; uvs+=stride;\par
*((float*)uvs) = 1.0f; *((float*)(uvs+4)) = 1.0f; uvs+=stride;\par
*((float*)uvs) = 1.0f; *((float*)(uvs+4)) = 0.0f; uvs+=stride;\par
\}\par
\}\par
inline void ComputePoint(Point& dest, float x, float y, const Matrix4x4& rot, const Point& trans)\par
\{\par
dest.x = trans.x + x * rot.m[0][0] + y * rot.m[1][0];\par
dest.y = trans.y + x * rot.m[0][1] + y * rot.m[1][1];\par
dest.z = trans.z + x * rot.m[0][2] + y * rot.m[1][2];\par
\}\par
And extra used functions:\par
// Quickly rotates a vector, using the 3x3 part of a 4x4 matrix\par
inline void TransformPoint3x3(Point& dest, const Point& source, const Matrix4x4& rot)\par
\{\par
dest.x = source.x * rot.m[0][0] + source.y * rot.m[1][0] + source.z * rot.m[2][0];\par
dest.y = source.x * rot.m[0][1] + source.y * rot.m[1][1] + source.z * rot.m[2][1];\par
dest.z = source.x * rot.m[0][2] + source.y * rot.m[1][2] + source.z * rot.m[2][2];\par
\}\par
float Renderer::ComputeConstantScale(const Point& pos, const ViewMatrix& view, const ProjMatrix& proj)\par
\{\par
Point ppcam0 = pos * view;\par
Point ppcam1 = ppcam0;\par
ppcam1.x += 1.0f;\par
float l1 = 1.0f/(ppcam0.x*proj.m[0][3] + ppcam0.y*proj.m[1][3] + ppcam0.z*proj.m[2][3] + proj.m[3][3]);\par
float c1 = (ppcam0.x*proj.m[0][0] + ppcam0.y*proj.m[1][0] + ppcam0.z*proj.m[2][0] + proj.m[3][0])*l1;\par
float l2 = 1.0f/(ppcam1.x*proj.m[0][3] + ppcam1.y*proj.m[1][3] + ppcam1.z*proj.m[2][3] + proj.m[3][3]);\par
float c2 = (ppcam1.x*proj.m[0][0] + ppcam1.y*proj.m[1][0] + ppcam1.z*proj.m[2][0] + proj.m[3][0])*l2;\par
float CorrectScale = 1.0f/(c2 - c1);\par
return CorrectScale / float(mRenderWidth);\par
\}\par
}