-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPoints.h
124 lines (101 loc) · 2.37 KB
/
Points.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
#pragma once
#include <GL/glew.h>
#include <vector>
#include <glm/glm.hpp>
class Points
{
private:
GLuint vao;
GLuint *VBOs;
public:
std::vector<glm::vec3> points;
std::vector<glm::vec3> colors;
Points(glm::vec3 p, glm::vec3 c = { 0, 0, 0 }) :
Points(std::vector<glm::vec3>(1, p), std::vector<glm::vec3>(1, c))
{ }
Points(const std::vector<glm::vec3>& p, const std::vector<glm::vec3>& c) :
points(p), colors(c)
{
colors.resize(p.size());
glGenVertexArrays(1, &vao);
VBOs = new GLuint[2];
glGenBuffers(2, VBOs);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, VBOs[0]);
glBufferData(
GL_ARRAY_BUFFER,
points.size() * sizeof(glm::vec3),
points.data(),
GL_STREAM_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
glBindBuffer(GL_ARRAY_BUFFER, VBOs[1]);
glBufferData(
GL_ARRAY_BUFFER,
colors.size() * sizeof(glm::vec3),
colors.data(),
GL_STREAM_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
~Points()
{
glDeleteVertexArrays(1, &vao);
glDeleteBuffers(2, VBOs);
vao = 0;
delete[] VBOs;
}
Points(const Points&) = delete;
Points& operator=(const Points&) = delete;
Points(Points&& other) :
vao(0), VBOs(new GLuint[2] { 0 })
{
*this = std::move(other);
}
Points& operator=(Points&& other)
{
if (this != &other)
{
glDeleteVertexArrays(1, &vao);
glDeleteBuffers(2, VBOs);
vao = other.vao;
other.vao = 0;
for (int i = 0; i < 2; i++)
{
VBOs[i] = other.VBOs[i];
other.VBOs[i] = 0;
}
points = std::move(other.points);
colors = std::move(other.colors);
}
}
void Update(
const std::vector<glm::vec3>& p,
const std::vector<glm::vec3>& c = std::vector<glm::vec3>())
{
points = p;
colors.resize(p.size());
glBindBuffer(GL_ARRAY_BUFFER, VBOs[0]);
glBufferData(
GL_ARRAY_BUFFER,
points.size() * sizeof(glm::vec3),
points.data(),
GL_STREAM_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, VBOs[1]);
glBufferData(
GL_ARRAY_BUFFER,
colors.size() * sizeof(glm::vec3),
colors.data(),
GL_STREAM_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void Render(float size)
{
glBindVertexArray(vao);
glPointSize(size);
glDrawArrays(GL_POINTS, 0, points.size());
glBindVertexArray(0);
}
};