aboutsummaryrefslogtreecommitdiff
path: root/src/texture_display_buffer.h
blob: 530b99d388d7ed387f89dcd7d9aae19b8ecaea15 (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
49
50
51
52
53
54
55
56
57
58
59
#pragma once

#include <vector>

class TextureDisplayBuffer {
private:
	const std::vector<GLfloat> _data;

	GLuint _texture;
	GLuint _array;
	GLuint _buffer;

public:
	TextureDisplayBuffer(GLuint texture):
		_data{
			-1.f,  1.f, 0.f, 1.f,
			-1.f, -1.f, 0.f, 0.f,
			 1.f, -1.f, 1.f, 0.f,

			-1.f,  1.f, 0.f, 1.f,
			 1.f, -1.f, 1.f, 0.f,
			 1.f,  1.f, 1.f, 1.f
		},
		_texture(texture) {
		glGenVertexArrays(1, &_array);
		glGenBuffers(1, &_buffer);

		glBindVertexArray(_array);
		glBindBuffer(GL_ARRAY_BUFFER, _buffer);
		glBufferData(
			GL_ARRAY_BUFFER,
			_data.size() * sizeof(GLfloat),
			_data.data(),
			GL_STATIC_DRAW
		);

		glEnableVertexAttribArray(0);
		glVertexAttribPointer(
			0, 2, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), (void*)0);
		glEnableVertexAttribArray(1);
		glVertexAttribPointer(
			1, 2, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), (void*)(2*sizeof(GLfloat)));
	}

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

	void draw() const {
		glBindVertexArray(_array);
		glBindTexture(GL_TEXTURE_2D, _texture);
		glDrawArrays(GL_TRIANGLES, 0, 6);
	}

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