Example Triangle Pog

This commit is contained in:
2022-10-18 22:02:03 -07:00
parent 8698ef318b
commit e08684de19
21 changed files with 525 additions and 10 deletions

View File

@ -27,6 +27,7 @@ extern "C" {
#include <iostream>
#include <thread>
#include <map>
#include <array>
// #include <iterator>
// #include <algorithm>
// #include <string>

View File

@ -0,0 +1,11 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "dawnlibs.hpp"
namespace Dawn {
class Shader;
}

View File

@ -0,0 +1,14 @@
// 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"
namespace Dawn {
class TriangleMesh {
public:
static void createTriangleMesh(std::shared_ptr<Mesh> mesh);
};
}

View File

@ -0,0 +1,11 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "dawnlibs.hpp"
namespace Dawn {
class Mesh;
}

View File

@ -18,8 +18,8 @@ void Scene::update() {
auto it = this->itemsNotInitialized.begin();
while(it != this->itemsNotInitialized.end()) {
this->items[it->first] = it->second;
it++;
it->second->init();
++it;
}
this->itemsNotInitialized.clear();

View File

@ -28,6 +28,17 @@ namespace Dawn {
void removeSceneItem(SceneItem *item);
template<class T>
std::shared_ptr<T> findComponent() {
auto it = this->items.begin();
while(it != this->items.end()) {
auto component = it->second->getComponent<T>();
if(component != nullptr) return component;
++it;
}
return nullptr;
}
~Scene();
};
}

View File

@ -11,7 +11,9 @@ namespace Dawn {
class Scene;
class SceneItem {
class SceneItem :
public std::enable_shared_from_this<SceneItem>
{
private:
std::vector<std::shared_ptr<SceneItemComponent>> components;
@ -25,7 +27,7 @@ namespace Dawn {
template<class T>
std::shared_ptr<T> addComponent() {
auto component = std::make_unique<T>(this);
auto component = std::make_shared<T>(weak_from_this());
this->components.push_back(component);
return component;
}
@ -34,10 +36,9 @@ namespace Dawn {
std::shared_ptr<T> getComponent() {
auto it = this->components.begin();
while(it != this->components.end()) {
std::shared_ptr<T> castedAs = dynamic_cast<std::shared_ptr<T>>(*it);
auto castedAs = std::dynamic_pointer_cast<T>(*it);
if(castedAs != nullptr) return castedAs;
it++;
continue;
++it;
}
return nullptr;
}

View File

@ -7,4 +7,7 @@
target_sources(${DAWN_TARGET_NAME}
PRIVATE
DummyComponent.cpp
)
)
# Subdirs
add_subdirectory(display)

View File

@ -0,0 +1,9 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/components/DummyComponent.hpp"
#include "scene/components/display/Camera.hpp"
#include "scene/components/display/MeshRenderer.hpp"

View 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
Camera.cpp
MeshRenderer.cpp
)

View File

@ -0,0 +1,43 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "Camera.hpp"
using namespace Dawn;
Camera::Camera(std::weak_ptr<SceneItem> item) : SceneItemComponent(item) {
}
void Camera::updateProjection() {
switch(this->type) {
case CAMERA_TYPE_ORTHONOGRAPHIC:
this->projection = glm::ortho(
this->orthoLeft,
this->orthoRight,
this->orthoBottom,
this->orthoTop,
this->clipNear,
this->clipFar
);
break;
case CAMERA_TYPE_PERSPECTIVE:
this->projection = glm::perspective(
this->fov,
this->getAspect(),
this->clipNear,
this->clipFar
);
break;
}
}
float_t Camera::getAspect() {
return 16.0f / 9.0f;
}
void Camera::start() {
this->updateProjection();
}

View File

@ -0,0 +1,57 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/SceneItemComponent.hpp"
namespace Dawn {
enum CameraType {
CAMERA_TYPE_ORTHONOGRAPHIC,
CAMERA_TYPE_PERSPECTIVE
};
class Camera : public SceneItemComponent {
public:
glm::mat4 projection;
// Perspective
enum CameraType type = CAMERA_TYPE_PERSPECTIVE;
float_t fov = 0.785398f;// 45 degrees
// Ortho
float_t orthoLeft = 0.0f;
float_t orthoRight = 1.0f;
float_t orthoBottom = 0.0f;
float_t orthoTop = 1.0f;
// Shared
float_t clipNear = 0.001f;
float_t clipFar = 100.0f;
/**
* Create a new Camera Component.
*
* @param item SceneItem that this component belongs to.
*/
Camera(std::weak_ptr<SceneItem> item);
/**
* Updates the projection matrix.
*/
void updateProjection();
/**
* Returs the aspect ratio of the camera.
*
* @return The aspect ratio of the camera.
*/
float_t getAspect();
/**
* Event triggered by the scene item when the item is added to the scene.
*/
void start() override;
};
}

View File

@ -0,0 +1,18 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "MeshRenderer.hpp"
using namespace Dawn;
MeshRenderer::MeshRenderer(std::weak_ptr<SceneItem> item) :
SceneItemComponent(item)
{
}
void MeshRenderer::start() {
}

View File

@ -0,0 +1,19 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/SceneItemComponent.hpp"
#include "display/mesh/Mesh.hpp"
namespace Dawn {
class MeshRenderer : public SceneItemComponent {
public:
std::shared_ptr<Mesh> mesh = nullptr;
MeshRenderer(std::weak_ptr<SceneItem> item);
void start() override;
};
}

View File

@ -7,4 +7,7 @@
target_sources(${DAWN_TARGET_NAME}
PRIVATE
RenderManager.cpp
)
)
# Subdirs
add_subdirectory(mesh)

View 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
)

View 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();
}

View 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();
};
}

View 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
}});
}

View File

@ -16,13 +16,28 @@ DawnGame::DawnGame(std::weak_ptr<DawnHost> host) :
int32_t DawnGame::init() {
this->renderManager.init();
this->scene = std::make_unique<Scene>(weak_from_this());
this->scene = std::make_shared<Scene>(weak_from_this());
auto cameraObject = this->scene->createSceneItem();
auto camera = cameraObject->addComponent<Camera>();
auto cubeObject = this->scene->createSceneItem();
auto cubeMeshRenderer = cubeObject->addComponent<MeshRenderer>();
cubeMeshRenderer->mesh = std::make_shared<Mesh>();
TriangleMesh::createTriangleMesh(cubeMeshRenderer->mesh);
return DAWN_GAME_INIT_RESULT_SUCCESS;
}
int32_t DawnGame::update(float_t delta) {
this->renderManager.update();
if(this->scene != nullptr) this->scene->update();
auto meshRenderer = this->scene->findComponent<MeshRenderer>();
if(meshRenderer != nullptr) {
std::cout << "REndering" << std::endl;
meshRenderer->mesh->draw(MESH_DRAW_MODE_TRIANGLES, 0, -1);
}
return DAWN_GAME_UPDATE_RESULT_SUCCESS;
}

View File

@ -4,4 +4,6 @@
// https://opensource.org/licenses/MIT
#pragma once
#include "game/DawnGame.hpp"
#include "game/DawnGame.hpp"
#include "scene/components/Components.hpp"
#include "display/mesh/TriangleMesh.hpp"