Example Triangle Pog
This commit is contained in:
@ -7,4 +7,7 @@
|
||||
target_sources(${DAWN_TARGET_NAME}
|
||||
PRIVATE
|
||||
RenderManager.cpp
|
||||
)
|
||||
)
|
||||
|
||||
# Subdirs
|
||||
add_subdirectory(mesh)
|
11
src/dawnopengl/display/mesh/CMakeLists.txt
Normal file
11
src/dawnopengl/display/mesh/CMakeLists.txt
Normal file
@ -0,0 +1,11 @@
|
||||
# Copyright (c) 2022 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Sources
|
||||
target_sources(${DAWN_TARGET_NAME}
|
||||
PRIVATE
|
||||
Mesh.cpp
|
||||
TriangleMesh.cpp
|
||||
)
|
118
src/dawnopengl/display/mesh/Mesh.cpp
Normal file
118
src/dawnopengl/display/mesh/Mesh.cpp
Normal file
@ -0,0 +1,118 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "Mesh.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
void Mesh::createBuffers(
|
||||
int32_t verticeCount,
|
||||
int32_t indiceCount
|
||||
) {
|
||||
if(verticeCount <= 0) throw "Vertice count must be greater than zero.";
|
||||
if(indiceCount <= 0) throw "Indice count must be greater than zero.";
|
||||
|
||||
this->disposeBuffers();
|
||||
|
||||
this->verticeCount = verticeCount;
|
||||
this->indiceCount = indiceCount;
|
||||
|
||||
auto sizePos = sizeof(glm::vec3) * verticeCount;
|
||||
auto sizeInds = sizeof(meshindice_t) * indiceCount;
|
||||
auto sizeCoords = sizeof(glm::vec2) * verticeCount;
|
||||
|
||||
// Create some buffers, one for the vertex data, one for the indices
|
||||
GLuint buffer[2];
|
||||
glGenBuffers(2, buffer);
|
||||
this->vertexBuffer = buffer[0];
|
||||
if(this->vertexBuffer < 0) throw "Failed to create vertex buffer";
|
||||
this->indexBuffer = buffer[1];
|
||||
if(this->indexBuffer < 0) throw "Failed to create index buffer";
|
||||
|
||||
// Buffer an empty set of data then buffer each component
|
||||
glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->indexBuffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizePos+sizeCoords, 0, GL_DYNAMIC_DRAW);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeInds, 0, GL_DYNAMIC_DRAW);
|
||||
|
||||
// Setup the attrib pointers
|
||||
size_t offset = 0;
|
||||
glVertexAttribPointer(
|
||||
0, sizeof(glm::vec3) / sizeof(float_t),
|
||||
GL_FLOAT, GL_FALSE,
|
||||
0, (void *)offset
|
||||
);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
offset += sizePos;
|
||||
glVertexAttribPointer(
|
||||
1, sizeof(glm::vec2) / sizeof(float_t),
|
||||
GL_FLOAT, GL_FALSE,
|
||||
0, (void *)offset
|
||||
);
|
||||
glEnableVertexAttribArray(1);
|
||||
}
|
||||
|
||||
void Mesh::disposeBuffers() {
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
|
||||
if(this->vertexBuffer != -1) {
|
||||
glDeleteBuffers(1, &this->vertexBuffer);
|
||||
this->vertexBuffer = -1;
|
||||
this->verticeCount = -1;
|
||||
}
|
||||
|
||||
if(this->indexBuffer != -1) {
|
||||
glDeleteBuffers(1, &this->indexBuffer);
|
||||
this->indexBuffer = -1;
|
||||
this->indiceCount = -1;
|
||||
}
|
||||
}
|
||||
|
||||
void Mesh::draw(
|
||||
enum MeshDrawMode drawMode,
|
||||
int32_t start,
|
||||
int32_t count
|
||||
) {
|
||||
if(
|
||||
count == 0 ||
|
||||
this->vertexBuffer == -1 ||
|
||||
this->indexBuffer == -1
|
||||
) return;
|
||||
|
||||
if(count == -1) count = this->indiceCount;
|
||||
|
||||
|
||||
// Re-Bind the buffers
|
||||
glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->indexBuffer);
|
||||
|
||||
// Re-Calculate the attrib pointers.
|
||||
size_t offset = 0;
|
||||
glVertexAttribPointer(
|
||||
0, sizeof(glm::vec3) / sizeof(float_t),
|
||||
GL_FLOAT, GL_FALSE,
|
||||
0, (void *)offset
|
||||
);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
offset += sizeof(glm::vec3) * this->verticeCount;
|
||||
glVertexAttribPointer(
|
||||
1, sizeof(glm::vec2) / sizeof(float_t),
|
||||
GL_FLOAT, GL_FALSE,
|
||||
0, (void *)offset
|
||||
);
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
// Render the elements.
|
||||
glDrawElements(
|
||||
drawMode, count, GL_UNSIGNED_INT, (void *)(sizeof(meshindice_t) * start)
|
||||
);
|
||||
}
|
||||
|
||||
Mesh::~Mesh() {
|
||||
this->disposeBuffers();
|
||||
}
|
131
src/dawnopengl/display/mesh/Mesh.hpp
Normal file
131
src/dawnopengl/display/mesh/Mesh.hpp
Normal file
@ -0,0 +1,131 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "display/mesh/_Mesh.hpp"
|
||||
#include "dawnopengl.hpp"
|
||||
|
||||
/** Indice that references a specific vertice */
|
||||
typedef int32_t meshindice_t;
|
||||
|
||||
namespace Dawn {
|
||||
enum MeshDrawMode {
|
||||
MESH_DRAW_MODE_TRIANGLES = GL_TRIANGLES
|
||||
};
|
||||
|
||||
class Mesh {
|
||||
private:
|
||||
|
||||
protected:
|
||||
/** Pointer to the vertex buffer on the GPU */
|
||||
GLuint vertexBuffer = -1;
|
||||
/** Pointer to the index buffer on the GPU */
|
||||
GLuint indexBuffer = -1;
|
||||
|
||||
/** How many vertices are in the mesh */
|
||||
int32_t verticeCount = -1;
|
||||
/** How many indices are in the mesh */
|
||||
int32_t indiceCount = -1;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Create a new set of buffers for the mesh to use.
|
||||
*
|
||||
* @param verticeCount How many Vertices will this buffer support.
|
||||
* @param indiceCount How many Indices will this buffer support.
|
||||
*/
|
||||
void createBuffers(
|
||||
int32_t verticeCount,
|
||||
int32_t indiceCount
|
||||
);
|
||||
|
||||
/**
|
||||
* Cleanup the buffers on a given mesh. This is useful if you intend to
|
||||
* expand the count of vertices your mesh supports.
|
||||
*/
|
||||
void disposeBuffers();
|
||||
|
||||
/**
|
||||
* Write vertice positions to the mesh.
|
||||
*
|
||||
* @tparam N Size of the array, in terms of number of elements.
|
||||
* @param position Position, within the buffer, to write to.
|
||||
* @param vertices Array of positions to write.
|
||||
*/
|
||||
template<size_t N>
|
||||
void bufferPositions(
|
||||
int32_t position,
|
||||
std::array<glm::vec3, N> positions
|
||||
) {
|
||||
glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer);
|
||||
glBufferSubData(
|
||||
GL_ARRAY_BUFFER,
|
||||
sizeof(glm::vec3) * position,
|
||||
sizeof(positions),
|
||||
(void *)positions.data()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write vertice coordinates to the mesh.
|
||||
*
|
||||
* @tparam N Size of the array, in terms of number of elements.
|
||||
* @param position Position, within the buffer, to write to.
|
||||
* @param coordinates Array of coordinates to write.
|
||||
*/
|
||||
template<size_t N>
|
||||
void bufferCoordinates(
|
||||
int32_t position,
|
||||
std::array<glm::vec2, N> coordinates
|
||||
) {
|
||||
glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer);
|
||||
glBufferSubData(
|
||||
GL_ARRAY_BUFFER,
|
||||
sizeof(glm::vec2) * position,
|
||||
sizeof(coordinates),
|
||||
(void *)coordinates.data()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write indices to the mesh.
|
||||
*
|
||||
* @tparam N Size of the array, in terms of number of elements.
|
||||
* @param position Position, within the buffer, to write to.
|
||||
* @param indices Array of indices to write.
|
||||
*/
|
||||
template<size_t N>
|
||||
void bufferIndices(
|
||||
int32_t position,
|
||||
std::array<meshindice_t, N> indices
|
||||
) {
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->indexBuffer);
|
||||
glBufferSubData(
|
||||
GL_ELEMENT_ARRAY_BUFFER,
|
||||
sizeof(meshindice_t) * position,
|
||||
sizeof(indices),
|
||||
(void *)indices.data()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Draw a primitive. Primitives are drawn by their indices.
|
||||
*
|
||||
* @param drawMode Which drawing mode to use to draw the primitive.
|
||||
* @param start Start indice (index) to draw.
|
||||
* @param count Count of indices to draw. Use -1 to draw all.
|
||||
*/
|
||||
void draw(
|
||||
enum MeshDrawMode drawMode,
|
||||
int32_t start,
|
||||
int32_t count
|
||||
);
|
||||
|
||||
/**
|
||||
* Cleanup a previously initiated mesh.
|
||||
*/
|
||||
~Mesh();
|
||||
};
|
||||
}
|
26
src/dawnopengl/display/mesh/TriangleMesh.cpp
Normal file
26
src/dawnopengl/display/mesh/TriangleMesh.cpp
Normal file
@ -0,0 +1,26 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "display/mesh/Mesh.hpp"
|
||||
#include "display/mesh/TriangleMesh.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
void TriangleMesh::createTriangleMesh(std::shared_ptr<Mesh> mesh) {
|
||||
mesh->createBuffers(3, 3);
|
||||
mesh->bufferPositions(0, std::array<glm::vec3, 3>{{
|
||||
glm::vec3(-1, 0, 0),
|
||||
glm::vec3(0, 1, 0),
|
||||
glm::vec3(1, 0, 0)
|
||||
}});
|
||||
mesh->bufferCoordinates(0, std::array<glm::vec2, 3>{{
|
||||
glm::vec2(0, 0),
|
||||
glm::vec2(0, 1),
|
||||
glm::vec2(1, 0)
|
||||
}});
|
||||
mesh->bufferIndices(0, std::array<meshindice_t,3>{{
|
||||
0, 1, 2
|
||||
}});
|
||||
}
|
Reference in New Issue
Block a user