Added example buffer function.

This commit is contained in:
2023-12-06 09:15:38 -06:00
parent eb21c04aa7
commit fb81ffb564
4 changed files with 94 additions and 8 deletions

View File

@ -6,6 +6,7 @@
#include "TrueTypeTexture.hpp"
#include "assert/assert.hpp"
#include "util/Math.hpp"
#include "display/mesh/QuadMesh.hpp"
using namespace Dawn;
@ -129,5 +130,66 @@ struct TrueTypeCharacter TrueTypeTexture::getCharacterData(wchar_t c) {
return this->characterData[c];
}
glm::vec2 TrueTypeTexture::bufferStringToMesh(
std::shared_ptr<Mesh> mesh,
const std::wstring text,
glm::vec2 position,
bool_t flipY
) {
assertNotNull(mesh, "Mesh must be supplied and not null");
assertTrue(text.size() > 0, "Text must be at least one character long.");
// Create mesh buffers
mesh->createBuffers(
text.length() * QUAD_VERTICE_COUNT,
text.length() * QUAD_INDICE_COUNT
);
// Foreach char
size_t i = 0;
glm::vec2 size = { 0, 0 };
for(wchar_t c : text) {
// Get the character data
auto info = this->getCharacterData(c);
// Buffer the quad
glm::vec4 quad = glm::vec4(
position.x,
position.y,
position.x + info.size.x,
position.y + info.size.y
);
if(flipY) {
QuadMesh::buffer(
mesh,
quad,
glm::vec4(
info.quad.x,
info.quad.w,
info.quad.z,
info.quad.y
),
i * QUAD_VERTICE_COUNT,
i * QUAD_INDICE_COUNT
);
} else {
QuadMesh::buffer(
mesh,
quad,
info.quad,
i * QUAD_VERTICE_COUNT,
i * QUAD_INDICE_COUNT
);
}
position += info.advance;
size += info.advance;
// Increment i
i++;
}
return size;
}
TrueTypeTexture::~TrueTypeTexture() {
}

View File

@ -6,6 +6,7 @@
#pragma once
#include "display/Texture.hpp"
#include "TrueTypeCharacter.hpp"
#include "display/mesh/Mesh.hpp"
#include <ft2build.h>
#include FT_FREETYPE_H
@ -41,6 +42,22 @@ namespace Dawn {
*/
struct TrueTypeCharacter getCharacterData(wchar_t c);
/**
* Buffers a string to the given mesh.
*
* @param mesh Mesh to buffer to.
* @param text Text to buffer.
* @param position Position to buffer to.
* @param flipY Whether or not to flip the Y axis.
* @return The size of the string.
*/
glm::vec2 bufferStringToMesh(
std::shared_ptr<Mesh> mesh,
const std::wstring text,
glm::vec2 &position,
bool_t flipY = false
);
/**
* Destroys this true type face texture.
*/

View File

@ -18,7 +18,7 @@ using namespace Dawn;
std::shared_ptr<TrueTypeTexture> texture;
void Dawn::helloWorldScene(Scene &s) {
texture = s.getGame()->assetManager.get<TrueTypeTexture>("ysabeau_regular", 12);
texture = s.getGame()->assetManager.get<TrueTypeTexture>("ysabeau_regular", 32);
while(!s.getGame()->assetManager.isLoaded("ysabeau_regular")) {
s.getGame()->assetManager.update();
@ -26,18 +26,19 @@ void Dawn::helloWorldScene(Scene &s) {
auto cameraItem = s.createSceneItem();
auto camera = cameraItem->addComponent<Camera>();
cameraItem->lookAt({ 5, 5, 5 }, { 0, 0, 0 }, { 0, 1, 0 });
cameraItem->lookAt({ 120, 120, 120 }, { 0, 0, 0 }, { 0, 1, 0 });
auto quad = s.createSceneItem();
auto quadMesh = std::make_shared<Mesh>();
quadMesh->createBuffers(QUAD_VERTICE_COUNT, QUAD_INDICE_COUNT);
QuadMesh::buffer(
glm::vec2 position = { 0, 0 };
glm::vec2 size = texture->bufferStringToMesh(
quadMesh,
glm::vec4(-1, -1, 1, 1),
glm::vec4(0, 1, 1, 0),
0,
0
L"Hello World!",
position,
true
);
auto quadRenderer = quad->addComponent<MeshRenderer>();
quadRenderer->mesh = quadMesh;

View File

@ -16,6 +16,12 @@ void Mesh::createBuffers(
assertTrue(verticeCount > 0, "Vertice count must be greater than zero.");
assertTrue(indiceCount > 0, "Indice count must be greater than zero.");
// Can we re-use the buffers?
if(
verticeCount <= this->verticeCount &&
indiceCount <= this->indiceCount
) return;
this->disposeBuffers();
this->verticeCount = verticeCount;