-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparticle_buffer.cpp
77 lines (59 loc) · 2.38 KB
/
particle_buffer.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
//
// Created by James Anderson on 9/29/16.
//
#include <cmath> // sqrt
#include "particle_buffer.h"
#include "vertex_format.h"
using namespace gl;
particle_buffer::particle_buffer(int a) {
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
area = a;
int field_width = (int)std::sqrt(area);
orig_points = new vert[area];
point_data = new vert[area];
feedback_buffer = new fb_vert[area];
for (int y = 0; y < field_width; y++) {
for (int x = 0; x < field_width; x++) {
glm::vec2 position = {(1.5 / field_width) * x - 0.7f, (1.5 / field_width) * y - 0.7f};
point_data[field_width * y + x] = {position, {0.0, 0.0}, position};
orig_points[field_width * y + x] = {position, {0.0, 0.0}, position};
}
}
glGenBuffers(1, &in_vbo);
glBindBuffer(GL_ARRAY_BUFFER, in_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vert) * area, point_data, GL_STREAM_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), 0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (void *)sizeof(glm::vec2));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (void *)(sizeof(glm::vec2) * 2));
glGenBuffers(1, &out_vbo);
glBindBuffer(GL_ARRAY_BUFFER, out_vbo);
glBufferData(GL_ARRAY_BUFFER, area * sizeof(fb_vert), nullptr, GL_STATIC_READ);
glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, out_vbo);
glBindBuffer(GL_ARRAY_BUFFER, in_vbo);
}
void particle_buffer::draw() {
glBindVertexArray(vao);
glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, out_vbo);
glBindBuffer(GL_ARRAY_BUFFER, in_vbo);
glBeginTransformFeedback(GL_POINTS);
glDrawArrays(GL_POINTS, 0, area);
glEndTransformFeedback();
// feed vertex output back into input
glGetBufferSubData(GL_TRANSFORM_FEEDBACK_BUFFER, 0,
sizeof(fb_vert) * area, feedback_buffer);
for (int i = 0; i < area; i++) {
point_data[i].position = feedback_buffer[i].outPosition;
point_data[i].velocity = feedback_buffer[i].outVelocity;
}
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vert) * area, point_data);
}
void particle_buffer::seed() {
glBindVertexArray(vao);
glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, out_vbo);
glBindBuffer(GL_ARRAY_BUFFER, in_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vert) * area, orig_points, GL_STREAM_DRAW);
}