-
Notifications
You must be signed in to change notification settings - Fork 0
/
polygonnode.cpp
52 lines (40 loc) · 1.44 KB
/
polygonnode.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
#include "polygonnode.h"
PolygonNode::PolygonNode(vec4 color) :
m_color(color)
{
std::vector<const char *> attrsV;
attrsV.push_back("aV");
m_shaderProgram.initialize(openglrenderer_vsh_solid(), openglrenderer_fsh_solid(), attrsV);
m_shaderProgram.matrix = m_shaderProgram.resolve("m");
m_shaderProgram.color = m_shaderProgram.resolve("color");
glGenBuffers(1, &m_vertexBuffer);
}
PolygonNode::~PolygonNode()
{
glDeleteBuffers(1, &m_vertexBuffer);
}
void PolygonNode::render(const mat4 &proj)
{
if (m_points.empty()) {
return;
}
glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer);
glUseProgram(m_shaderProgram.id());
for (int i=0; i<m_shaderProgram.attributeCount(); ++i) {
glEnableVertexAttribArray(i);
}
glUniformMatrix4fv(m_shaderProgram.matrix, 1, true, proj.m);
glUniform4f(m_shaderProgram.color, m_color.x * m_color.w, m_color.y * m_color.w, m_color.z * m_color.w, m_color.w);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
glDrawArrays(GL_TRIANGLE_FAN, 0, m_points.size());
glUseProgram(0);
glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer);
// glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void PolygonNode::setPoints(const vector<vec2> &points)
{
m_points = points;
glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, m_points.size() * sizeof(vec2), m_points.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}