summaryrefslogtreecommitdiff
path: root/gfx/models/mesh.h
blob: 71f6dce6e65315ee2493a6bd660d5819049043af (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
#pragma once

#include "gfx/gl/vertexArrayObject.h"
#include <glArrays.h>
#include <glad/gl.h>
#include <memory>
#include <span>
#include <stdTypeDefs.h>

class Vertex;

class MeshBase {
public:
	void Draw() const;
	void DrawInstanced(GLuint vao, GLsizei count) const;

protected:
	MeshBase(GLsizei m_numIndices, GLenum mode);

	glVertexArray m_vertexArrayObject;
	glBuffers<2> m_vertexArrayBuffers;
	GLsizei m_numIndices;
	GLenum mode;
};

template<typename V> class MeshT : public MeshBase, public ConstTypeDefs<MeshT<V>> {
public:
	MeshT(const std::span<const V> vertices, const std::span<const unsigned int> indices, GLenum mode = GL_TRIANGLES) :
		MeshBase {static_cast<GLsizei>(indices.size()), mode}
	{
		VertexArrayObject::data(vertices, m_vertexArrayBuffers[0], GL_ARRAY_BUFFER);
		VertexArrayObject::data(indices, m_vertexArrayBuffers[1], GL_ARRAY_BUFFER);
		configureVAO(m_vertexArrayObject);
	}

	VertexArrayObject &
	configureVAO(VertexArrayObject && vao) const
	{
		return vao.addAttribsFor<V>(m_vertexArrayBuffers[0]).addIndices(m_vertexArrayBuffers[1]);
	}
};

using Mesh = MeshT<Vertex>;