Support window resize, Histogram class.

* Resize buffers when the window resizes.
* Histogram class for displaying per-frame data.
* Histogram can either push data to the left when new data arrives
  (false) or Overwrite with a moving insertion point (true) based on the
  editInPlace variable.
master
aurailus 2019-02-10 00:28:39 -08:00
parent df0e74b3e7
commit 3c14ca14da
14 changed files with 201 additions and 72 deletions

View File

@ -4,6 +4,7 @@
<inspection_tool class="ClangTidyInspection" enabled="true" level="WARNING" enabled_by_default="true">
<option name="clangTidyChecks" value="*,-android-*,-bugprone-bool-pointer-implicit-conversion,-cert-env33-c,-cert-dcl50-cpp,-cert-dcl59-cpp,-cppcoreguidelines-no-malloc,-cppcoreguidelines-owning-memory,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-cppcoreguidelines-pro-bounds-constant-array-index,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-cstyle-cast,-cppcoreguidelines-pro-type-reinterpret-cast,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-special-member-functions,-fuchsia-*,-google-*,google-default-arguments,google-explicit-constructor,google-runtime-member-string-references,google-runtime-operator,-hicpp-braces-around-statements,-hicpp-named-parameter,-hicpp-no-array-decay,-hicpp-no-assembler,-hicpp-no-malloc,-hicpp-function-size,-hicpp-special-member-functions,-hicpp-vararg,-llvm-*,-objc-*,-readability-else-after-return,-readability-implicit-bool-conversion,-readability-named-parameter,-readability-simplify-boolean-expr,-readability-braces-around-statements,-readability-identifier-naming,-readability-function-size,-readability-redundant-member-init,-misc-bool-pointer-implicit-conversion,-misc-definitions-in-headers,-misc-unused-alias-decls,-misc-unused-parameters,-misc-unused-using-decls,-modernize-use-using,-modernize-use-default-member-init,-clang-diagnostic-*,-clang-analyzer-*,-cert-msc30-c,-cert-msc50-cpp" />
</inspection_tool>
<inspection_tool class="OCDFAInspection" enabled="false" level="WARNING" enabled_by_default="false" />
<inspection_tool class="SpellCheckingInspection" enabled="false" level="TYPO" enabled_by_default="false">
<option name="processCode" value="true" />
<option name="processLiterals" value="true" />

Binary file not shown.

Before

Width:  |  Height:  |  Size: 206 B

After

Width:  |  Height:  |  Size: 1018 B

View File

@ -90,6 +90,6 @@ set(ZEUS_SRC_FILES
server/ServerState.h
server/ConnectionList.cpp
server/ConnectionList.h
server/ServerPeer.h generic/network/PacketType.h generic/network/PacketType.h generic/network/PacketChannel.h client/game/gameworld/PlayerEntity.cpp client/game/gameworld/PlayerEntity.h)
server/ServerPeer.h generic/network/PacketType.h generic/network/PacketType.h generic/network/PacketChannel.h client/game/gameworld/PlayerEntity.cpp client/game/gameworld/PlayerEntity.h client/engine/graphics/Histogram.cpp client/engine/graphics/Histogram.h)
add_library (zeusCore ${ZEUS_SRC_FILES})

View File

@ -10,7 +10,7 @@
struct ClientState {
Renderer* renderer;
double fps;
double fps = 0;
double deltaTime;
};

View File

@ -16,6 +16,8 @@ Window::Window(GLint windowWidth, GLint windowHeight) {
mouseLocked = true;
mouseDown = false;
resized = false;
for (bool &key : keys) {
key = false;
}
@ -79,8 +81,11 @@ int Window::initialize() {
glfwSetWindowUserPointer(mainWindow, this);
glfwSetKeyCallback(mainWindow, handleKeys);
glfwSetWindowSizeCallback(mainWindow, handleResize);
glfwSetInputMode(mainWindow, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
glfwMaximizeWindow(mainWindow);
return 0;
}
@ -138,9 +143,37 @@ void Window::handleKeys(GLFWwindow* glfwWindow, int key, int code, int action, i
}
}
}
void Window::handleResize(GLFWwindow *glfwWindow, int width, int height) {
auto window = static_cast<Window*>(glfwGetWindowUserPointer(glfwWindow));
glfwGetFramebufferSize(glfwWindow, &window->bufferWidth, &window->bufferHeight);
glViewport(0, 0, window->bufferWidth, window->bufferHeight);
window->resized = true;
}
#pragma clang diagnostic pop
Window::~Window() {
glfwDestroyWindow(mainWindow);
glfwTerminate();
}
GLfloat Window::getBufferWidth() {
return bufferWidth;
}
GLfloat Window::getBufferHeight() {
return bufferHeight;
}
bool Window::getShouldClose() {
return (bool)glfwWindowShouldClose(mainWindow);
}
bool *Window::getKeysArray() {
return keys;
}
void Window::swapBuffers() {
glfwSwapBuffers(mainWindow);
}

View File

@ -18,18 +18,19 @@ public:
void update();
GLfloat getBufferWidth() { return bufferWidth; }
GLfloat getBufferHeight() { return bufferHeight; }
GLfloat getBufferWidth();
GLfloat getBufferHeight();
bool resized;
bool getShouldClose() { return (bool)glfwWindowShouldClose(mainWindow); }
bool getShouldClose();
bool* getKeysArray() { return keys; }
bool* getKeysArray();
bool mouseIsDown();
double getDeltaX();
double getDeltaY();
void swapBuffers() { glfwSwapBuffers(mainWindow); }
void swapBuffers();
~Window();
@ -47,6 +48,7 @@ private:
//Static so that GLFW can run callback
static void handleKeys(GLFWwindow* glfwWindow, int key, int code, int action, int mode);
static void handleResize(GLFWwindow* glfwWindow, int width, int height);
double deltaX;
double deltaY;

View File

@ -0,0 +1,81 @@
//
// Created by aurailus on 08/02/19.
//
#include "Histogram.h"
Histogram::Histogram(Texture *texture, int length, float maxVal, bool editInPlace) {
this->length = length;
this->maxVal = maxVal;
this->editInPlace = editInPlace;
this->history = std::vector<float>((unsigned long)length);
this->insertionPoint = 0;
setTexture(texture);
mesh = new Mesh();
}
void Histogram::push_back(float value) {
if (editInPlace) {
insertionPoint++;
if (insertionPoint >= length) insertionPoint = 0;
history[insertionPoint] = value;
}
else {
if (insertionPoint < length - 1) insertionPoint++;
else {
for (int i = 0; i < length; i++) {
history[i] = history[i + 1];
}
}
history[insertionPoint] = value;
}
cleanup();
delete mesh;
Mesh* mesh = buildHistogramMesh();
create(mesh);
}
Mesh* Histogram::buildHistogramMesh() {
std::vector<float> vertices;
std::vector<unsigned int> indices;
unsigned int indOffset = 0;
float xOffset = 0;
float i = 0.10;
for (float num : history) {
float distFromPointer = (xOffset <= insertionPoint) ? insertionPoint - xOffset : insertionPoint + 120 - xOffset;
float h = num / maxVal;
float section = std::round(9 - (float)fmax(maxVal - num, 0.0)/6) / 10.0f;
float age = std::round((90 - (distFromPointer / length)*90)) / 100.0f;
auto columnVerts = std::vector<float> {
xOffset, -h, 0, age, section, 0, 0, 0,
xOffset + 1, -h, 0, age+0.01f, section, 0, 0, 0,
xOffset + 1, 0, 0, age+0.01f, section+i, 0, 0, 0,
xOffset, 0, 0, age, section+i, 0, 0, 0,
};
for (float f : columnVerts) vertices.push_back(f);
indices.push_back( indOffset);
indices.push_back(3 + indOffset);
indices.push_back(1 + indOffset);
indices.push_back(3 + indOffset);
indices.push_back(2 + indOffset);
indices.push_back(1 + indOffset);
xOffset ++;
indOffset += 4;
}
auto m = new Mesh();
m->create(&vertices, &indices);
return m;
}

View File

@ -0,0 +1,31 @@
//
// Created by aurailus on 08/02/19.
//
#ifndef ZEUS_HISTOGRAM_H
#define ZEUS_HISTOGRAM_H
#include "../Entity.h"
class Histogram : public Entity {
public:
Histogram() = default;
Histogram(Texture *texture, int length, float maxVal, bool editInPlace);
void push_back(float value);
private:
Mesh* buildHistogramMesh();
float maxVal = 1;
int length = 60;
int insertionPoint = 0;
bool editInPlace = false;
std::vector<float> history;
};
#endif //ZEUS_HISTOGRAM_H

View File

@ -27,8 +27,7 @@ Renderer::Renderer(GLint winWidth, GLint winHeight) {
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.0f, window->getBufferWidth(), window->getBufferHeight(), 0.0f, 0.0f, 100.0f);
createMatrices();
glEnable(GL_CULL_FACE);
glEnable(GL_BLEND);
@ -37,6 +36,10 @@ Renderer::Renderer(GLint winWidth, GLint winHeight) {
void Renderer::update() {
window->update();
if (window->resized) {
createMatrices();
window->resized = false;
}
}
void Renderer::begin() {
@ -87,3 +90,8 @@ Window *Renderer::getWindow() {
Camera *Renderer::getCamera() {
return camera;
}
void Renderer::createMatrices() {
projectionMatrix = glm::perspective(45.0f, window->getBufferWidth() / window->getBufferHeight(), 0.1f, 1000.0f);
orthographicMatrix = glm::ortho(0.0f, window->getBufferWidth(), window->getBufferHeight(), 0.0f, 0.0f, 100.0f);
}

View File

@ -27,6 +27,8 @@ public:
void enableGuiShader();
void drawGui(Entity* entity);
void createMatrices();
void end();
Window* getWindow();

View File

@ -87,7 +87,7 @@ public:
return out;
}
int lastGenUpdates, lastMeshUpdates;
int lastGenUpdates = 0, lastMeshUpdates = 0;
private:
//Global lists for storing blockChunks and meshChunks
std::unordered_map<glm::vec3, BlockChunk*, vec3cmp> blockChunks;

View File

@ -3,16 +3,19 @@
//
#include "DebugGui.h"
#include "../../engine/graphics/Histogram.h"
DebugGui::DebugGui() {
fontTexture = new Texture((char*)"../res/tex/gui/font.png");
fontTexture->load();
histogramTexture = new Texture((char*)"../res/tex/gui/histogram.png");
histogramTexture->load();
fpsHist = new Texture((char*)"../res/tex/gui/histogram.png");
fpsHist->load();
genHist = new Texture((char*)"../res/tex/gui/histogram_white.png");
genHist->load();
alphaText = new HudText(fontTexture);
alphaText->set("Zeus Alpha 0.01");
alphaText->set("Zeus Alpha");
alphaText->setScale(3);
alphaText->setPosition(glm::vec3(8, 4, 0));
@ -24,59 +27,29 @@ DebugGui::DebugGui() {
playerText->setScale(2);
playerText->setPosition(glm::vec3(8, 42, 0));
fpsHistogram = new Entity();
fpsHistogram->create(new Mesh(), histogramTexture);
fpsHistogram->setPosition(glm::vec3(8, 764, 0));
chunkUpdateHistogram = new Histogram(genHist, 120, 64, true);
chunkUpdateHistogram->setScale(glm::vec3(2, 32, 1));
chunkUpdateHistogram->setPosition(glm::vec3(870, 760, 0));
meshUpdateHistogram = new Histogram(genHist, 120, 64, true);
meshUpdateHistogram->setScale(glm::vec3(2, 32, 1));
meshUpdateHistogram->setPosition(glm::vec3(1118, 760, 0));
fpsHistogram = new Histogram(fpsHist, 120, 60, true);
fpsHistogram->setScale(glm::vec3(2, 20, 1));
fpsHistogram->setPosition(glm::vec3(8, 760, 0));
}
void DebugGui::pushGuiObjects(std::vector<Entity*> &list) {
list.push_back(alphaText);
list.push_back(fpsText);
// list.push_back(fpsText);
list.push_back(playerText);
list.push_back(chunkUpdateHistogram);
list.push_back(meshUpdateHistogram);
list.push_back(fpsHistogram);
}
void DebugGui::fpsHistUpdate() {
std::vector<float> vertices;
std::vector<unsigned int> indices;
unsigned int indOffset = 0;
float xOffset = 0;
float width = 6;
float height = 0.5;
float i = 0.10;
for (double num : fpsHistory) {
float h = (float)num * height;
float sev = (float)std::round(9 - std::max(60 - num, 0.0)/6) / 10.0f;
auto columnVerts = std::vector<float> {
xOffset, -h, 0, sev , sev, 0, 0, 0,
xOffset + width, -h, 0, sev+i, sev, 0, 0, 0,
xOffset + width, 0, 0, sev+i, sev+i, 0, 0, 0,
xOffset, 0, 0, sev , sev+i, 0, 0, 0,
};
for (float f : columnVerts) vertices.push_back(f);
indices.push_back( indOffset);
indices.push_back(3 + indOffset);
indices.push_back(1 + indOffset);
indices.push_back(3 + indOffset);
indices.push_back(2 + indOffset);
indices.push_back(1 + indOffset);
xOffset += width;
indOffset += 4;
}
auto m = new Mesh();
m->create(&vertices, &indices);
fpsHistogram->cleanup();
fpsHistogram->create(m);
}
std::string string_float(float val) {
std::ostringstream out;
out.precision(2);
@ -119,11 +92,11 @@ void DebugGui::update(Player* player, World* world, Window* window, BlockAtlas*
glm::vec3 chk = World::chunkVec(*player->getPos());
glm::vec3 loc = World::localVec(*player->getPos());
if (fpsHistory.size() > FPS_HISTOGRAM_SIZE) fpsHistory.erase(fpsHistory.begin());
fpsHistory.push_back(fps);
fpsText->set(string_double(fps) + " FPS");
fpsHistUpdate();
fpsHistogram->push_back((float)fps);
meshUpdateHistogram->push_back((float)world->lastMeshUpdates);
chunkUpdateHistogram->push_back((float)world->lastGenUpdates);
using namespace std; //Temporary, delete when refactoring this
playerText->set(

View File

@ -5,6 +5,7 @@
#ifndef ZEUS_DEBUGGUI_H
#define ZEUS_DEBUGGUI_H
#include "../../engine/graphics/Histogram.h"
#include "../../engine/graphics/HudText.h"
#include "../../engine/Window.h"
#include "../../engine/Ray.h"
@ -21,18 +22,13 @@ public:
~DebugGui();
private:
void fpsHistUpdate();
Texture* fontTexture;
Texture* histogramTexture;
Texture *fpsHist, *genHist;
HudText* fpsText;
HudText* alphaText;
HudText* playerText;
Entity* fpsHistogram;
const int FPS_HISTOGRAM_SIZE = 120;
std::vector<double> fpsHistory;
HudText* fpsText;
HudText* alphaText;
HudText* playerText;
Histogram *chunkUpdateHistogram, *meshUpdateHistogram, *fpsHistogram;
};

View File

@ -25,6 +25,8 @@ void MapGen::getElevation(MapGen::MapGenJob &j) {
localPos = ArrayTrans3D::indToVec(i);
globalPos = glm::vec3(j.pos.x * 16 + localPos->x, j.pos.y * 16 + localPos->y, j.pos.z * 16 + localPos->z);
// j.elevation[i] = globalPos.y > 8 ? -1 : 2;
int val = (int)floor(p.noise(globalPos.x / 16, 0, globalPos.z / 16) * 32);
val /= (int)floor(p.noise(globalPos.x / 32, 0, globalPos.z / 32) * 16);