-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewpoint.cpp
107 lines (75 loc) · 3.64 KB
/
Viewpoint.cpp
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "Viewpoint.h"
void CViewpoint::SetProjections( ID3D10Effect * effect, int width, int height )
{
/*
=====
4D equivalent of 'view' matrix
=====
*/
D3DXVECTOR2 dim = D3DXVECTOR2(width,height);
/* Calculate Vd, the 4th coordinate basis vector and line-of-sight. */
Vd = To4 - From4;
D3DXVec4Normalize(&Vd,&Vd);
/* Calculate Va, the X-axis basis vector. */
D3DXVec4Normalize(&Va,D3DXVec4Cross(&Va,&Up4,&Over4,&Vd));
/* Calculate Vb, the perpendicularized Up vector. */
D3DXVec4Normalize(&Vb,D3DXVec4Cross(&Vb,&Over4,&Vd,&Va));
/* Calculate Vc, the perpendicularized Over vector*/
D3DXVec4Normalize(&Vc,D3DXVec4Cross(&Vc,&Vd,&Va,&Vb));
effect->GetVariableByName("Va")->AsVector()->SetFloatVector((float*)Va);
effect->GetVariableByName("Vb")->AsVector()->SetFloatVector((float*)Vb);
effect->GetVariableByName("Vc")->AsVector()->SetFloatVector((float*)Vc);
effect->GetVariableByName("Vd")->AsVector()->SetFloatVector((float*)Vd);
effect->GetVariableByName("From4")->AsVector()->SetFloatVector((float*)From4);
effect->GetVariableByName("RenderTargetSize")->AsVector()->SetFloatVector((float*)dim);
effect->GetVariableByName("ViewangleTanInv")->AsScalar()->SetFloat(1/tan(Viewangle/2));
effect->GetVariableByName("bProject4D")->AsScalar()->SetBool(Hardware4DProjection);
/*
=====
Classic 3D projection stuff
=====
*/
// Initialize the projection matrix
D3DXMatrixPerspectiveFovLH( &Projection3d, Viewangle, width / (float)height, 0.1f, 100.0f );
D3DXMatrixLookAtLH( &View3d, &From3, &To3, &Up3);
effect->GetVariableByName( "CameraPosition" )->AsVector()->SetFloatVector(( float* )From3);
effect->GetVariableByName( "World" )->AsMatrix()->SetMatrix((float*)&World3d);
effect->GetVariableByName( "View" )->AsMatrix()->SetMatrix((float*)&View3d);
effect->GetVariableByName( "Projection" )->AsMatrix()->SetMatrix((float*)&Projection3d);
}
D3DXVECTOR4 CViewpoint::ProjectVertexTo3D( D3DXVECTOR4 vertex )
{
D3DXVECTOR4 t = vertex - From4;
float pc = 1 / tan (Viewangle/2);
float rTmp = pc / D3DXVec4Dot(&t,&Vd);
return D3DXVECTOR4(D3DXVec4Dot(&t,&Va)*rTmp,D3DXVec4Dot(&t,&Vb)*rTmp,D3DXVec4Dot(&t,&Vc)*rTmp,1);
}
void CViewpoint::Rotate4DBasis( double angle, int plane_coord_index1,int plane_coord_index2 )
{
double t1,t2;
double cos_a = cos(angle);
double sin_a = sin(angle);
/* Rotate the from-vector. */
t1 = cos_a * (((float*)From4)[plane_coord_index1]-((float*)To4)[plane_coord_index1]) + sin_a * (((float*)From4)[plane_coord_index2]-((float*)To4)[plane_coord_index2]);
t2 = cos_a * (((float*)From4)[plane_coord_index2]-((float*)To4)[plane_coord_index2]) - sin_a * (((float*)From4)[plane_coord_index1]-((float*)To4)[plane_coord_index1]);
((float*)From4)[plane_coord_index1] = t1 + ((float*)To4)[plane_coord_index1];
((float*)From4)[plane_coord_index2] = t2 + ((float*)To4)[plane_coord_index2];
/* Rotate the Up Vector. */
t1 = cos_a * ((float*)Up4)[plane_coord_index1] + sin_a * ((float*)Up4)[plane_coord_index2];
t2 = cos_a * ((float*)Up4)[plane_coord_index2] - sin_a * ((float*)Up4)[plane_coord_index1];
((float*)Up4)[plane_coord_index1] = t1;
((float*)Up4)[plane_coord_index2] = t2;
/* Rotate the Over Vector */
t1 = cos_a * ((float*)Over4)[plane_coord_index1] + sin_a * ((float*)Over4)[plane_coord_index2];
t2 = cos_a * ((float*)Over4)[plane_coord_index2] - sin_a * ((float*)Over4)[plane_coord_index1];
((float*)Over4)[plane_coord_index1] = t1;
((float*)Over4)[plane_coord_index2] = t2;
}
void CViewpoint::Rotate4DBasisAbsolute(std::vector<RotationToken4D> rotations )
{
From4 = From4Original;
Over4 = Over4Original;
Up4 = Up4Original;
for (auto & rot : rotations)
Rotate4DBasis(rot.angle,rot.plane1,rot.plane2);
}