forked from yiming-cai/OursCraft
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLightDisplay.cpp
More file actions
99 lines (88 loc) · 2.47 KB
/
Copy pathLightDisplay.cpp
File metadata and controls
99 lines (88 loc) · 2.47 KB
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
#include "LightDisplay.h"
LightDisplay::LightDisplay(Light * light, Camera * cam) :
lights(light), camera(cam)
{
pointLight = new Model("../assets/lights/pointlight.obj");
spotLight = new Model("../assets/lights/spotlight.obj");
pointLight->setCamera(cam);
spotLight->setCamera(cam);
spotLight->centerAndScale(1.0f);
pointLight->centerAndScale(1.0f);
}
LightDisplay::~LightDisplay()
{
if (pointLight != nullptr)
{
delete pointLight;
}
if (spotLight != nullptr)
{
delete spotLight;
}
}
void LightDisplay::initShader(GLuint shaderProgram)
{
if (camera == nullptr || lights == nullptr)
{
std::cerr << "Didn't initialize camera and lights" << std::endl;
return;
}
pointLight->initShader(shaderProgram);
spotLight->initShader(shaderProgram);
lights->initializeShader(shaderProgram);
lights->updateShader(shaderProgram);
light_loc[shaderProgram] = glGetUniformLocation(shaderProgram, "current_light_index");
}
void LightDisplay::update(GLuint shaderProgram)
{
if (camera == nullptr || lights == nullptr)
{
std::cerr << "Didn't initialize camera and lights" << std::endl;
return;
}
positions_and_types = lights->getAllPositionsAndTypes();
coneDirections = lights->getAllConeDirections();
lightStatus = lights->getAllStatus();
needUpdate = true;
}
void LightDisplay::render(GLuint shaderProgram)
{
if (needUpdate)
{
lights->updateShader(shaderProgram);
}
if (camera == nullptr || lights == nullptr)
{
std::cerr << "Didn't initialize camera and lights" << std::endl;
return;
}
//glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
int i = 0;
for (auto const& pair : positions_and_types)
{
glm::vec3 position = pair.first;
int type = pair.second;
if (lightStatus[i] == Light::STATUS_OFF || type == Light::TYPE_DIRECTIONAL)
{
glUniform1i(light_loc[shaderProgram], i);
i++;
continue;
}
glUniform1i(light_loc[shaderProgram], i);
if (type == Light::TYPE_POINT)
{
pointLight->draw(glm::translate(glm::mat4(1.0f), position), shaderProgram);
}
else if (type == Light::TYPE_SPOT)
{
glm::vec3 defaultAxis = glm::vec3(0, -1.0f, 0);
glm::vec3 axis = glm::cross(defaultAxis, glm::normalize(coneDirections[i]));
float angle = acos(glm::dot(defaultAxis, glm::normalize(coneDirections[i])));
glm::mat4 rot = glm::rotate(glm::mat4(1.0f), angle, axis);
spotLight->draw( glm::translate(glm::mat4(1.0f), position) * rot, shaderProgram);
}
i++;
}
glUniform1i(light_loc[shaderProgram], 0);
//glDisable(GL_BLEND);
}