Created a visual text mesh generator.

Created TextBuilder class
Created HudText class, extends Entity
Added HudText objects to GameInstance to show FPS
Removed some debug text from misc classes
master
aurailus 2018-12-25 23:20:15 -08:00
parent d55f8a78f5
commit ffdcf3d828
15 changed files with 168 additions and 57 deletions

View File

@ -66,6 +66,6 @@ add_executable(zeus
zeus/lua_api/LuaParser.h
zeus/lua_api/LRegisterBlock.cpp
zeus/lua_api/LRegisterBlock.h
zeus/engine/iVec3.h zeus/engine/GuiEntity.cpp zeus/engine/GuiEntity.h)
zeus/engine/iVec3.h zeus/mesh/TextBuilder.cpp zeus/mesh/TextBuilder.h zeus/mesh/TextBuilder.cpp zeus/engine/graphics/HudText.cpp zeus/engine/graphics/HudText.h)
target_link_libraries(zeus ${OPENGL_gl_LIBRARY} glfw libGLEW.so pthread lua dl)

View File

@ -40,9 +40,13 @@ int main(int argc, char* argv[]) {
game->draw();
if (t.elapsedNs() / (double)1000000 >= 20) {
t.printElapsedMs(); //Print frame time
}
float fps = 1000 / (t.elapsedNs() / 1000000.0f);
std::ostringstream out;
out.precision(2);
out << std::fixed << fps;
std::string s = out.str();
game->fpsText->set(s + " FPS");
}
return 0;

View File

@ -31,7 +31,7 @@ public:
glm::mat4 getModelMatrix();
~Entity();
private:
protected:
Mesh* mesh;
glm::vec3 position;

View File

@ -1,5 +0,0 @@
//
// Created by aurailus on 22/12/18.
//
#include "GuiEntity.h"

View File

@ -1,19 +0,0 @@
//
// Created by aurailus on 22/12/18.
//
#ifndef ZEUS_GUIIENTITY_H
#define ZEUS_GUIIENTITY_H
#include <ext.hpp>
class GuiEntity {
public:
glm::mat4 getModelMatrix() {
return glm::mat4();
}
};
#endif //ZEUS_GUIIENTITY_H

View File

@ -57,7 +57,7 @@ TextureAtlas::TextureAtlas(const char* directory) {
if (!file.is_dir && strcmp(file.ext, ".png") == 0) {
printf("Loading Texture: %s\n", file.name);
// printf("Loading Texture: %s\n", file.name);
auto ref = TextureRef();
strcpy(ref.path, file.path);

View File

@ -0,0 +1,21 @@
#include <utility>
//
// Created by aurailus on 25/12/18.
//
#include "HudText.h"
#include "../../mesh/TextBuilder.h"
HudText::HudText() {
mesh = new Mesh();
set("");
}
void HudText::set(std::string text) {
this->text = std::move(text);
cleanup();
delete mesh;
Mesh* mesh = TextBuilder().build(this->text);
create(mesh);
}

View File

@ -0,0 +1,21 @@
//
// Created by aurailus on 25/12/18.
//
#ifndef ZEUS_TEXTENTITY_H
#define ZEUS_TEXTENTITY_H
#include "../Entity.h"
class HudText : public Entity {
public:
HudText();
void set(std::string text);
private:
std::string text;
};
#endif //ZEUS_TEXTENTITY_H

View File

@ -20,11 +20,11 @@ Renderer::Renderer(GLint winWidth, GLint winHeight) {
guiShader = new Shader();
guiShader->createFromFile("../zeus/shader/gui.vs", "../zeus/shader/gui.fs");
uOrtho = guiShader->getUniformLocation("matrix");
uOrtho = guiShader->getUniformLocation("ortho");
uGuiModel = guiShader->getUniformLocation("model");
projectionMatrix = glm::perspective(45.0f, window->getBufferWidth() / window->getBufferHeight(), 0.1f, 1000.0f);
orthographicMatrix = glm::ortho(0, (int)window->getBufferWidth(), (int)window->getBufferHeight(), 0);
orthographicMatrix = glm::ortho(0.0f, window->getBufferWidth(), window->getBufferHeight(), 0.0f, 0.0f, 100.0f);
glEnable(GL_CULL_FACE);
glEnable(GL_BLEND);
@ -59,8 +59,9 @@ void Renderer::draw(Entity* entity) {
entity->draw();
}
void Renderer::drawGui(GuiEntity* entity) {
void Renderer::drawGui(Entity* entity) {
glUniformMatrix4fv(uGuiModel, 1, GL_FALSE, glm::value_ptr(entity->getModelMatrix()));
entity->draw();
}
void Renderer::end() {

View File

@ -9,7 +9,6 @@
#include "../Window.h"
#include "../Camera.h"
#include "../Entity.h"
#include "../GuiEntity.h"
#include <ext.hpp>
class Renderer {
@ -24,7 +23,7 @@ public:
void draw(Entity* entity);
void enableGuiShader();
void drawGui(GuiEntity* entity);
void drawGui(Entity* entity);
void end();

View File

@ -23,7 +23,7 @@ void LRegisterBlock::api(std::string identifier, sol::table data) {
auto texTable = data.get<sol::optional<sol::table>>("textures");
if (name && texTable) {
printf("Registering block %s!\n", name->c_str());
// printf("Registering block %s!\n", name->c_str());
auto textures = std::vector<std::string>();

49
zeus/mesh/TextBuilder.cpp Normal file
View File

@ -0,0 +1,49 @@
//
// Created by aurailus on 25/12/18.
//
#include "TextBuilder.h"
Mesh* TextBuilder::build(std::string text) {
auto textVertices = new std::vector<float>();
auto textIndices = new std::vector<unsigned int>();
float texPosWidth = 1/128.0f * (float)w;
float texPosHeight = 1/54.0f * (float)h;
for (unsigned int i = 0; i < text.length(); i++) {
char letter = text[i];
int baseIndex = (int)letter - 32;
float left = i; //Cast to float so the vector doesn't error
float texPosX = baseIndex%18 / 18.0f; //18 = Characters in row
float texPosY = baseIndex/18 / 6.0f; //6 = Number of columns
auto letterVerts = std::vector<float> {
left * w, 0, 0, texPosX, texPosY, 0, 0, 0,
left * w + w, 0, 0, texPosX + texPosWidth, texPosY, 0, 0, 0,
left * w + w, h, 0, texPosX + texPosWidth, texPosY + texPosHeight, 0, 0, 0,
left * w, h, 0, texPosX, texPosY + texPosHeight, 0, 0, 0,
};
for (float f : letterVerts) textVertices->push_back(f);
textIndices->push_back(i*4);
textIndices->push_back(3 + i*4);
textIndices->push_back(1 + i*4);
textIndices->push_back(3 + i*4);
textIndices->push_back(2 + i*4);
textIndices->push_back(1 + i*4);
}
Mesh* m = new Mesh();
m->create(textVertices, textIndices);
delete textVertices;
delete textIndices;
return m;
}

24
zeus/mesh/TextBuilder.h Normal file
View File

@ -0,0 +1,24 @@
//
// Created by aurailus on 25/12/18.
//
#ifndef ZEUS_TEXTBUILDER_H
#define ZEUS_TEXTBUILDER_H
#include "../engine/graphics/Mesh.h"
class TextBuilder {
public:
TextBuilder() = default;
Mesh* build(std::string text);
~TextBuilder() = default;
private:
const static int w = 7;
const static int h = 9;
};
#endif //ZEUS_TEXTBUILDER_H

View File

@ -26,16 +26,28 @@ void GameInstance::initialize(Renderer* renderer) {
//The world requires the blockAtlas for meshing and handling inputs.
world = new World(blockAtlas);
// world->genNewChunk(glm::vec3(0,0,0));
int SIZE = 32;
for (int i = -SIZE; i < SIZE; i++) {
for (int j = 0; j < 3; j++) {
for (int j = 0; j < 4; j++) {
for (int k = -SIZE; k < SIZE; k++) {
world->genNewChunk(glm::vec3(i, j, k));
}
}
}
fontTexture = Texture((char*)"../tex/font.png");
fontTexture.load();
alphaText = new HudText();
alphaText->set(std::string("Zeus ALPHA"));
alphaText->setScale(2.5);
alphaText->setPosition(glm::vec3(8, 4, 0));
guiEntities.push_back(alphaText);
fpsText = new HudText();
fpsText->setScale(2);
fpsText->setPosition(glm::vec3(8, 32, 0));
guiEntities.push_back(fpsText);
}
void GameInstance::update(GLfloat deltaTime) {
@ -48,37 +60,30 @@ void GameInstance::update(GLfloat deltaTime) {
camera->mouseControl(window->getDeltaX(), window->getDeltaY());
world->update();
glm::vec3 chunk = *camera->getPosition();
chunk.x = round(chunk.x / 16);
chunk.y = round(chunk.y / 16);
chunk.z = round(chunk.z / 16);
// int SIZE = 16;
// for (int i = -SIZE; i < SIZE; i++) {
// for (int j = -4; j < 4; j++) {
// for (int k = -SIZE; k < SIZE; k++) {
// glm::vec3 adjustedPos(i + chunk.x, j + chunk.y, k + chunk.z);
// world->genNewChunk(adjustedPos);
// }
// }
// }
}
void GameInstance::draw() {
textureAtlas->getTexture()->use();
Timer t("Drawing");
renderer->begin();
renderer->enableWorldShader();
textureAtlas->getTexture()->use();
for (auto &chunk : *world->getMeshChunks()) {
renderer->draw(chunk.second);
}
renderer->enableGuiShader();
fontTexture.use();
//TODO: gui rendering here
for (auto &gui : *getGuiEntities()) {
renderer->drawGui(gui);
}
renderer->end();
}
}
std::vector<Entity*>* GameInstance::getGuiEntities() {
return &guiEntities;
}

View File

@ -13,6 +13,7 @@
#include "../engine/graphics/Renderer.h"
#include "../engine/helpers/ArrayTrans3D.h"
#include "../lua_api/LuaParser.h"
#include "../engine/graphics/HudText.h"
class GameInstance {
public:
@ -23,6 +24,10 @@ public:
void update(GLfloat deltaTime);
void draw();
std::vector<Entity*>* getGuiEntities();
HudText* fpsText;
public:
//The renderer contains the camera, window, and draw methods.
Renderer* renderer;
@ -35,6 +40,12 @@ public:
//The block atlas holds block definitions and models.
BlockAtlas* blockAtlas;
//GUI Entities (Things to draw on the screen
std::vector<Entity*> guiEntities;
Texture fontTexture;
HudText* alphaText;
};