aboutsummaryrefslogtreecommitdiff
path: root/src/buffer/vertex/fluid_cell_buffer.cc
blob: d9af989ca2c5b8046509bc7643541d179e0605a8 (plain)
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
#include "fluid_cell_buffer.h"

#include <vector>

FluidCellBuffer::FluidCellBuffer(GLuint nX, GLuint nY, std::function<int(int,int)>&& geometry):
	_nX(nX), _nY(nY) {
	glGenBuffers(1, &_buffer);
	enable();
	init(std::forward<decltype(geometry)>(geometry));
}

FluidCellBuffer::~FluidCellBuffer() {
	glDeleteBuffers(1, &_buffer);
	glDeleteVertexArrays(1, &_array);
}

void FluidCellBuffer::enable() {
	glBindVertexArray(_array);
	glBindBuffer(GL_ARRAY_BUFFER, _buffer);
	glEnableVertexAttribArray(0);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
}

void FluidCellBuffer::init(std::function<int(int,int)>&& geometry) {
	std::vector<GLfloat> data(3*_nX*_nY, GLfloat{});

	for ( int x = 0; x < _nX; ++x ) {
		for ( int y = 0; y < _nY; ++y ) {
			data[3*_nX*y + 3*x + 2] = geometry(x,y);
		}
	}

	glBufferData(
		GL_ARRAY_BUFFER,
		data.size() * sizeof(GLfloat),
		data.data(),
		GL_DYNAMIC_DRAW
	);
}

GLuint FluidCellBuffer::getBuffer() const {
	return _buffer;
}

void FluidCellBuffer::draw() const {
	glBindVertexArray(_array);
	glDrawArrays(GL_POINTS, 0, _nX*_nY);
}