-
Notifications
You must be signed in to change notification settings - Fork 0
/
LightManager.h
executable file
·124 lines (102 loc) · 3.14 KB
/
LightManager.h
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "DXUT.h"
#pragma once
#ifndef LIGHT_MANAGER_H
#define LIGHT_MANAGER_H
#include <vector>
#include <deque>
#include "Camera.h"
#include "Texture2D.h"
struct PointLight {
DOUBLE x;
DOUBLE y;
DOUBLE z;
FLOAT attenuationEnd;
DirectX::XMFLOAT3 colour;
FLOAT ambient;
};
struct PointLightGPU {
DirectX::XMFLOAT3 viewPos;
FLOAT attenuationEnd;
DirectX::XMFLOAT3 colour;
FLOAT ambient;
};
class LightManager {
private:
std::vector<PointLight*> lightVector;
std::deque<size_t> freeSlots;
public:
LightManager() {
lightVector.reserve(100000);
}
//Returns the index of the light in the manager
size_t addLight(PointLight* pLight) {
if (freeSlots.size() == 0) {
lightVector.push_back(pLight);
return lightVector.size()-1;
}
else {
size_t slot = freeSlots.front();
freeSlots.pop_front();
lightVector[slot] = pLight;
return slot;
}
}
//Doesnt range check, so don't be stupid
void removeLight(size_t lightIndex) {
lightVector[lightIndex] = NULL;
freeSlots.push_back(lightIndex);
}
//Returns the number of lights loaded into the buffer
UINT updateLightBuffer(ID3D11DeviceContext* pd3dContext, const Camera* pCamera,StructuredBuffer<PointLightGPU>* pLightListSB) {
//TODO: This is the most inefficient thing ever.
using namespace DirectX;
std::vector<std::pair<FLOAT,PointLightGPU> > tempList;
tempList.reserve(lightVector.size());
DirectX::XMINT3 offset = pCamera->mCoords;
DirectX::XMFLOAT3 eye;
XMStoreFloat3(&eye,pCamera->mEye);
for (int i = 0; i < lightVector.size(); i++) {
const PointLight* pl = lightVector[i];
if (pl != NULL) {
PointLightGPU plgpu;
plgpu.ambient = pl->ambient;
plgpu.attenuationEnd = pl->attenuationEnd;
plgpu.colour = pl->colour;
FLOAT posx = (FLOAT)(pl->x - offset.x);
FLOAT posy = (FLOAT)(pl->y - offset.y);
FLOAT posz = (FLOAT)(pl->z - offset.z);
plgpu.viewPos = DirectX::XMFLOAT3(posx,posy,posz);
posx -= eye.x;
posy -= eye.y;
posz -= eye.z;
FLOAT distsq = posx*posx+posy*posy+posz*posz;
tempList.push_back(std::pair<FLOAT,PointLightGPU>(distsq,plgpu));
}
}
//Sort by camera distance
std::sort(tempList.begin(),tempList.end(),LightManager::lengthCompare);
PointLightGPU* llist = pLightListSB->MapDiscard(pd3dContext);
UINT numLights = 0;
UINT maxLights = pLightListSB->mElements;
//Frustum test lights and put the untransformed lights into the structured buffer
for (int i = 0; i < tempList.size(); i++) {
if (numLights == maxLights) {
break;
}
PointLightGPU* plgpu = &tempList[i].second;
BOOL passed = pCamera->testFrustum(plgpu->viewPos,offset,plgpu->attenuationEnd);
if (passed) {
llist[numLights] = *plgpu;
numLights++;
}
}
XMVector3TransformCoordStream(&llist->viewPos,sizeof(PointLightGPU),&llist->viewPos,sizeof(PointLightGPU),numLights,pCamera->mViewMatrix);
pLightListSB->Unmap(pd3dContext);
return numLights;
}
private:
static BOOL lengthCompare(std::pair<FLOAT,PointLightGPU> a, std::pair<FLOAT,PointLightGPU> b) {
return a.first < b.first;
}
};
#endif