-
Notifications
You must be signed in to change notification settings - Fork 0
/
LightRenderer.cpp
99 lines (78 loc) · 2.47 KB
/
LightRenderer.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
#include "LightRenderer.h"
LightRenderer::LightRenderer(MeshType meshType, Camera* camera)
{
this->camera = camera;
switch (meshType)
{
case kTriangle:
Mesh::setTriData(vertices, indices);
break;
case kQuad:
Mesh::setQuadData(vertices,indices);
break;
case kCube:
Mesh::setCubeData(vertices, indices);
break;
case kSphere:
Mesh::setSphereData(vertices, indices);
break;
}
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
// Generate VBO
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * vertices.size(), &vertices[0], GL_STATIC_DRAW);
// Enable Vertex Attributes: Position and Color
glEnableVertexAttribArray(0);
glVertexAttribPointer(0,3,GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(offsetof(Vertex, Vertex::color)));
// generate EBO
glGenBuffers(1, &ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * indices.size(), &indices[0], GL_STATIC_DRAW);
// unbind
glBindBuffer(GL_ARRAY_BUFFER,0);
glBindVertexArray(0);
}
void LightRenderer::draw()
{
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(glm::mat4(1.0), position);
glUseProgram(this->program);
GLint modelLoc = glGetUniformLocation(program, "model");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glm::mat4 view = camera->getViewMatrix();
GLint viewLoc = glGetUniformLocation(program, "view");
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glm::mat4 projection = camera->getProjectionMatrix();
GLint pLoc = glGetUniformLocation(program, "projection");
glUniformMatrix4fv(pLoc, 1, GL_FALSE, glm::value_ptr(projection));
glBindVertexArray(vao);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
glUseProgram(0);
}
LightRenderer::~LightRenderer() {
}
void LightRenderer::setPosition(glm::vec3 _position)
{
position = _position;
}
void LightRenderer::setColor(glm::vec3 _color)
{
this->color =_color;
}
void LightRenderer::setProgram(GLuint program)
{
this->program = program;
}
glm::vec3 LightRenderer::getPosition()
{
return position;
}
glm::vec3 LightRenderer::getColor()
{
return color;
}