forked from lunderot/3DProject
/
ShadowLight.cpp
104 lines (82 loc) · 2.06 KB
/
ShadowLight.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
#include "ShadowLight.h"
ShadowLight::ShadowLight()
{
//If default constructed object should be used, then all values have to be set by the SET functions.
}
ShadowLight::ShadowLight(XMFLOAT4 ambient, XMFLOAT4 diffuse, XMFLOAT3 position, XMFLOAT3 lookAt)
{
ambientColor = ambient;
diffuseColor = diffuse;
this->position = position;
this->lookAt = lookAt;
}
ShadowLight::~ShadowLight()
{
}
XMFLOAT4 ShadowLight::GetAmbientColor()
{
return ambientColor;
}
XMFLOAT4 ShadowLight::GetDiffuseColor()
{
return diffuseColor;
}
XMFLOAT3 ShadowLight::GetPosition()
{
return position;
}
void ShadowLight::SetAmbientColor(XMFLOAT4 ambient)
{
ambientColor = ambient;
}
void ShadowLight::SetDiffuseColor(XMFLOAT4 diffuse)
{
diffuseColor = diffuse;
}
void ShadowLight::SetPosition(XMFLOAT3 position)
{
this->position = position;
}
void ShadowLight::SetLookAt(XMFLOAT3 lookAt)
{
this->lookAt = lookAt;
}
void ShadowLight::CreateViewMatrix()
{
XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, .0f);
XMVECTOR pos = XMVectorSet(position.x, position.y, position.z, .0f);
XMVECTOR look = XMVectorSet(lookAt.x, lookAt.y, lookAt.z, .0f);
viewMatrix = XMMatrixLookAtLH(pos, look, up);
}
void ShadowLight::CreateProjectionMatrix(float screenDepth, float screenNear)
{
//Field of view and screen aspect for square light
float fov = (float)XM_PI / 2.0f;
float screenAspect = 1.0f;
projectionMatrix = XMMatrixPerspectiveFovLH(fov, screenAspect, screenNear, screenDepth);
}
void ShadowLight::GetViewMatrix(XMMATRIX& viewMatrix)
{
viewMatrix = this->viewMatrix;
}
void ShadowLight::GetProjectionMatrix(XMMATRIX& projectionMatrix)
{
projectionMatrix = this->projectionMatrix;
}
XMFLOAT3 ShadowLight::GetDirection()
{
XMFLOAT3 dir(lookAt.x - position.x, lookAt.y - position.y, lookAt.z - position.z);
float length = sqrt((dir.x * dir.x) + (dir.y * dir.y) + (dir.z * dir.z));
dir.x = dir.x / length;
dir.y = dir.y / length;
dir.z = dir.z / length;
return dir;
}
void* ShadowLight::operator new(size_t i)
{
return _mm_malloc(i, 16);
}
void ShadowLight::operator delete(void* p)
{
_mm_free(p);
}