2D graphics performance fix

This commit is contained in:
MihailRis
2023-11-06 02:49:05 +03:00
parent 01eb264f44
commit 6c79f3da18
5 changed files with 124 additions and 146 deletions
+95 -115
View File
@@ -20,20 +20,19 @@
#include "../objects/Player.h" #include "../objects/Player.h"
HudRenderer::HudRenderer() { HudRenderer::HudRenderer(Assets* assets) : assets(assets) {
batch = new Batch2D(1024); batch = new Batch2D(1024);
uicamera = new Camera(glm::vec3(), Window::height / 1.0f); uicamera = new Camera(glm::vec3(), Window::height);
uicamera->perspective = false; uicamera->perspective = false;
uicamera->flipped = true; uicamera->flipped = true;
} }
HudRenderer::~HudRenderer() { HudRenderer::~HudRenderer() {
// delete crosshair;
delete batch; delete batch;
delete uicamera; delete uicamera;
} }
void HudRenderer::drawDebug(Level* level, Assets* assets, int fps, bool occlusion){ void HudRenderer::drawDebug(Level* level, int fps, bool occlusion){
Chunks* chunks = level->chunks; Chunks* chunks = level->chunks;
Player* player = level->player; Player* player = level->player;
@@ -56,11 +55,91 @@ void HudRenderer::drawDebug(Level* level, Assets* assets, int fps, bool occlusio
stream << std::hex << player->selectedVoxel.states; stream << std::hex << player->selectedVoxel.states;
font->draw(batch, L"block-selected: "+std::to_wstring(player->selectedVoxel.id)+L" "+stream.str(), 16, 78, STYLE_OUTLINE); font->draw(batch, L"block-selected: "+std::to_wstring(player->selectedVoxel.id)+L" "+stream.str(), 16, 78, STYLE_OUTLINE);
font->draw(batch, L"meshes: " + std::to_wstring(Mesh::meshesCount), 16, 102, STYLE_OUTLINE); font->draw(batch, L"meshes: " + std::to_wstring(Mesh::meshesCount), 16, 102, STYLE_OUTLINE);
// batch->render(); batch->render();
} }
void HudRenderer::drawInventory(Player* player) {
Texture* blocks = assets->getTexture("block");
uint size = 48;
uint step = 64;
uint inv_wm = step*10;
uint inv_hm = step*8;
uint inv_w = inv_wm - (step - size);
uint inv_h = inv_hm - (step - size);
int inv_x = (Window::width - (inv_w)) / 2;
int inv_y = (Window::height - (inv_h)) / 2;
int xs = (Window::width - inv_w + step)/2;
int ys = (Window::height - inv_h + step)/2;
if (Window::width > inv_w*3){
inv_x = (Window::width + (inv_w)) / 2;
inv_y = (Window::height - (inv_h)) / 2;
xs = (Window::width + inv_w + step)/2;
ys = (Window::height - inv_h + step)/2;
}
int x = 0;
int y = 0;
vec4 tint = vec4(1.0f);
int mx = Events::x;
int my = Events::y;
uint count = (inv_w / step) * (inv_h / step) + 1;
void HudRenderer::draw(Level* level, Assets* assets){ //back
batch->texture(nullptr);
batch->color = vec4(0.0f, 0.0f, 0.0f, 0.3f);
batch->rect(0, 0, Window::width, Window::height);
batch->rect(inv_x - 4, inv_y - 4, inv_w+8, inv_h+8,
0.95f, 0.95f, 0.95f, 0.85f, 0.85f, 0.85f,
0.7f, 0.7f, 0.7f,
0.55f, 0.55f, 0.55f, 0.45f, 0.45f, 0.45f, 4);
batch->rect(inv_x, inv_y, inv_w, inv_h,
0.75f, 0.75f, 0.75f, 0.75f, 0.75f, 0.75f,
0.75f, 0.75f, 0.75f,
0.75f, 0.75f, 0.75f, 0.75f, 0.75f, 0.75f, 4);
batch->color = vec4(0.35f, 0.35f, 0.35f, 1.0f);
for (uint i = 1; i < count; i++) {
x = xs + step * ((i-1) % (inv_w / step));
y = ys + step * ((i-1) / (inv_w / step));
batch->rect(x-2, y-2, size+4, size+4,
0.45f, 0.45f, 0.45f, 0.55f, 0.55f, 0.55f,
0.7f, 0.7f, 0.7f,
0.85f, 0.85f, 0.85f, 0.95f, 0.95f, 0.95f, 2);
batch->rect(x, y, size, size,
0.65f, 0.65f, 0.65f, 0.65f, 0.65f, 0.65f,
0.65f, 0.65f, 0.65f,
0.65f, 0.65f, 0.65f, 0.65f, 0.65f, 0.65f, 2);
}
//front
batch->texture(blocks);
for (uint i = 1; i < count; i++) {
Block* cblock = Block::blocks[i];
if (cblock == nullptr)
break;
x = xs + step * ((i-1) % (inv_w / step));
y = ys + step * ((i-1) / (inv_w / step));
if (mx > x && mx < x + (int)size && my > y && my < y + (int)size) {
tint.r *= 1.2f;
tint.g *= 1.2f;
tint.b *= 1.2f;
if (Events::jclicked(GLFW_MOUSE_BUTTON_LEFT)) {
player->choosenBlock = i;
}
} else
{
tint = vec4(1.0f);
}
if (cblock->model == BLOCK_MODEL_CUBE){
batch->blockSprite(x, y, size, size, 16, cblock->textureFaces, tint);
} else if (cblock->model == BLOCK_MODEL_X_SPRITE){
batch->sprite(x, y, size, size, 16, cblock->textureFaces[3], tint);
}
}
}
void HudRenderer::draw(Level* level){
glDisable(GL_MULTISAMPLE);
uicamera->fov = Window::height; uicamera->fov = Window::height;
glDisable(GL_DEPTH_TEST); glDisable(GL_DEPTH_TEST);
@@ -72,24 +151,19 @@ void HudRenderer::draw(Level* level, Assets* assets){
// Chosen block preview // Chosen block preview
Texture* blocks = assets->getTexture("block"); Texture* blocks = assets->getTexture("block");
Texture* sprite = assets->getTexture("slot");
batch->texture(nullptr); batch->texture(nullptr);
batch->color = vec4(1.0f); batch->color = vec4(1.0f);
if (Events::_cursor_locked && !level->player->debug) { if (Events::_cursor_locked && !level->player->debug) {
glLineWidth(2); glLineWidth(2);
batch->line(Window::width/2, Window::height/2-6, Window::width/2, Window::height/2+6, 0.2f, 0.2f, 0.2f, 1.0f); const uint width = Window::width;
batch->line(Window::width/2+6, Window::height/2, Window::width/2-6, Window::height/2, 0.2f, 0.2f, 0.2f, 1.0f); const uint height = Window::height;
batch->line(Window::width/2-5, Window::height/2-5, Window::width/2+5, Window::height/2+5, 0.9f, 0.9f, 0.9f, 1.0f); batch->line(width/2, height/2-6, width/2, height/2+6, 0.2f, 0.2f, 0.2f, 1.0f);
batch->line(Window::width/2+5, Window::height/2-5, Window::width/2-5, Window::height/2+5, 0.9f, 0.9f, 0.9f, 1.0f); batch->line(width/2+6, height/2, width/2-6, height/2, 0.2f, 0.2f, 0.2f, 1.0f);
batch->line(width/2-5, height/2-5, width/2+5, height/2+5, 0.9f, 0.9f, 0.9f, 1.0f);
batch->line(width/2+5, height/2-5, width/2-5, height/2+5, 0.9f, 0.9f, 0.9f, 1.0f);
} }
// batch->texture(sprite);
// batch->sprite(Window::width/2-32, uicamera->fov - 80, 64, 64, 16, 0, vec4(1.0f));
// batch->rect(Window::width/2-128-4, Window::height-80-4, 256+8, 64+8,
// 0.85f, 0.85f, 0.85f, 0.95f, 0.95f, 0.95f,
// 0.55f, 0.55f, 0.55f,
// 0.45f, 0.45f, 0.45f, 0.7f, 0.7f, 0.7f, 2);
batch->rect(Window::width/2-128-4, Window::height-80-4, 256+8, 64+8, batch->rect(Window::width/2-128-4, Window::height-80-4, 256+8, 64+8,
0.95f, 0.95f, 0.95f, 0.85f, 0.85f, 0.85f, 0.95f, 0.95f, 0.95f, 0.85f, 0.85f, 0.85f,
0.7f, 0.7f, 0.7f, 0.7f, 0.7f, 0.7f,
@@ -119,103 +193,9 @@ void HudRenderer::draw(Level* level, Assets* assets){
} }
} }
if (!Events::_cursor_locked) { //inventory if (!Events::_cursor_locked) {
uint size = 48; drawInventory(player);
uint step = 64;
uint inv_wm = step*10;
uint inv_hm = step*8;
uint inv_w = inv_wm - (step - size);
uint inv_h = inv_hm - (step - size);
int inv_x = (Window::width - (inv_w)) / 2;
int inv_y = (Window::height - (inv_h)) / 2;
int xs = (Window::width - inv_w + step)/2;
int ys = (Window::height - inv_h + step)/2;
if (Window::width > inv_w*3){
inv_x = (Window::width + (inv_w)) / 2;
inv_y = (Window::height - (inv_h)) / 2;
xs = (Window::width + inv_w + step)/2;
ys = (Window::height - inv_h + step)/2;
}
int x = 0;
int y = 0;
vec4 tint = vec4(1.0f);
int mx = Events::x;
int my = Events::y;
uint count = (inv_w / step) * (inv_h / step) + 1;
//back
batch->texture(nullptr);
batch->color = vec4(0.0f, 0.0f, 0.0f, 0.3f);
batch->rect(0, 0, Window::width, Window::height);
batch->rect(inv_x - 4, inv_y - 4, inv_w+8, inv_h+8,
0.95f, 0.95f, 0.95f, 0.85f, 0.85f, 0.85f,
0.7f, 0.7f, 0.7f,
0.55f, 0.55f, 0.55f, 0.45f, 0.45f, 0.45f, 4);
batch->rect(inv_x, inv_y, inv_w, inv_h,
0.75f, 0.75f, 0.75f, 0.75f, 0.75f, 0.75f,
0.75f, 0.75f, 0.75f,
0.75f, 0.75f, 0.75f, 0.75f, 0.75f, 0.75f, 4);
batch->color = vec4(0.35f, 0.35f, 0.35f, 1.0f);
for (uint i = 1; i < count; i++) {
x = xs + step * ((i-1) % (inv_w / step));
y = ys + step * ((i-1) / (inv_w / step));
// batch->rect(x-2, y-2, size+4, size+4);
batch->rect(x-2, y-2, size+4, size+4,
0.45f, 0.45f, 0.45f, 0.55f, 0.55f, 0.55f,
0.7f, 0.7f, 0.7f,
0.85f, 0.85f, 0.85f, 0.95f, 0.95f, 0.95f, 2);
batch->rect(x, y, size, size,
0.65f, 0.65f, 0.65f, 0.65f, 0.65f, 0.65f,
0.65f, 0.65f, 0.65f,
0.65f, 0.65f, 0.65f, 0.65f, 0.65f, 0.65f, 2);
}
// batch->color = vec4(0.5f, 0.5f, 0.5f, 1.0f);
// for (unsigned i = 1; i < count; i++) {
// x = xs + step * ((i-1) % (inv_w / step));
// y = ys + step * ((i-1) / (inv_w / step));
// batch->rect(x, y, size, size);
// }
//front
batch->texture(blocks);
for (uint i = 1; i < count; i++) {
Block* cblock = Block::blocks[i];
if (cblock == nullptr)
break;
x = xs + step * ((i-1) % (inv_w / step));
y = ys + step * ((i-1) / (inv_w / step));
if (mx > x && mx < x + (int)size && my > y && my < y + (int)size) {
tint.r *= 1.2f;
tint.g *= 1.2f;
tint.b *= 1.2f;
if (Events::jclicked(GLFW_MOUSE_BUTTON_LEFT)) {
player->choosenBlock = i;
}
// size = 50;
} else
{
// size = 48;
tint = vec4(1.0f);
}
if (cblock->model == BLOCK_MODEL_CUBE){
batch->blockSprite(x, y, size, size, 16, cblock->textureFaces, tint);
} else if (cblock->model == BLOCK_MODEL_X_SPRITE){
batch->sprite(x, y, size, size, 16, cblock->textureFaces[3], tint);
}
}
}
// batch->render();
if (Events::_cursor_locked && !level->player->debug){
// Shader* crosshairShader = assets->getShader("crosshair");
// crosshairShader->use();
// crosshairShader->uniform1f("u_ar", (float)Window::height / (float)Window::width);
// crosshairShader->uniform1f("u_scale", 1.0f / ((float)Window::height / 1000.0f));
// glLineWidth(2.0f);
// crosshair->draw(GL_LINES);
} }
batch->render();
glEnable(GL_MULTISAMPLE);
} }
+6 -5
View File
@@ -5,17 +5,18 @@ class Batch2D;
class Camera; class Camera;
class Level; class Level;
class Assets; class Assets;
class Mesh; class Player;
class HudRenderer { class HudRenderer {
Assets* assets;
Batch2D* batch; Batch2D* batch;
Camera* uicamera; Camera* uicamera;
// Mesh* crosshair;
public: public:
HudRenderer(); HudRenderer(Assets* assets);
~HudRenderer(); ~HudRenderer();
void draw(Level* level, Assets* assets); void drawInventory(Player* player);
void drawDebug(Level* level, Assets* assets, int fps, bool occlusion); void draw(Level* level);
void drawDebug(Level* level, int fps, bool occlusion);
}; };
#endif /* SRC_HUD_RENDER_H_ */ #endif /* SRC_HUD_RENDER_H_ */
+16 -20
View File
@@ -71,16 +71,16 @@ void Batch2D::texture(Texture* new_texture){
} }
void Batch2D::point(float x, float y, float r, float g, float b, float a){ void Batch2D::point(float x, float y, float r, float g, float b, float a){
// if (index + 6*VERTEX_SIZE >= capacity) if (index + 6*VERTEX_SIZE >= capacity)
// render(GL_TRIANGLES); render(GL_TRIANGLES);
vertex(x, y, 0, 0, r,g,b,a); vertex(x, y, 0, 0, r,g,b,a);
render(GL_POINTS); render(GL_POINTS);
} }
void Batch2D::line(float x1, float y1, float x2, float y2, float r, float g, float b, float a){ void Batch2D::line(float x1, float y1, float x2, float y2, float r, float g, float b, float a){
// if (index + 6*VERTEX_SIZE >= capacity) if (index + 6*VERTEX_SIZE >= capacity)
// render(GL_TRIANGLES); render(GL_TRIANGLES);
vertex(x1, y1, 0, 0, r,g,b,a); vertex(x1, y1, 0, 0, r,g,b,a);
vertex(x2, y2, 1, 1, r,g,b,a); vertex(x2, y2, 1, 1, r,g,b,a);
@@ -92,8 +92,8 @@ void Batch2D::rect(float x, float y, float w, float h){
const float g = color.g; const float g = color.g;
const float b = color.b; const float b = color.b;
const float a = color.a; const float a = color.a;
// if (index + 6*VERTEX_SIZE >= capacity) if (index + 6*VERTEX_SIZE >= capacity)
// render(GL_TRIANGLES); render(GL_TRIANGLES);
vertex(x, y, 0, 0, r,g,b,a); vertex(x, y, 0, 0, r,g,b,a);
vertex(x, y+h, 0, 1, r,g,b,a); vertex(x, y+h, 0, 1, r,g,b,a);
@@ -102,7 +102,6 @@ void Batch2D::rect(float x, float y, float w, float h){
vertex(x, y, 0, 0, r,g,b,a); vertex(x, y, 0, 0, r,g,b,a);
vertex(x+w, y+h, 1, 1, r,g,b,a); vertex(x+w, y+h, 1, 1, r,g,b,a);
vertex(x+w, y, 1, 0, r,g,b,a); vertex(x+w, y, 1, 0, r,g,b,a);
render(GL_TRIANGLES);
} }
void Batch2D::rect( void Batch2D::rect(
@@ -114,8 +113,8 @@ void Batch2D::rect(
bool flippedX, bool flippedX,
bool flippedY, bool flippedY,
vec4 tint) { vec4 tint) {
// if (index + 6*VERTEX_SIZE >= capacity) if (index + 6*VERTEX_SIZE >= capacity)
// render(GL_TRIANGLES); render(GL_TRIANGLES);
float centerX = w*ox; float centerX = w*ox;
float centerY = h*oy; float centerY = h*oy;
@@ -197,14 +196,13 @@ void Batch2D::rect(
vertex(x1, y1, u1, v1, tint.r, tint.g, tint.b, tint.a); vertex(x1, y1, u1, v1, tint.r, tint.g, tint.b, tint.a);
vertex(x3, y3, u3, v3, tint.r, tint.g, tint.b, tint.a); vertex(x3, y3, u3, v3, tint.r, tint.g, tint.b, tint.a);
vertex(x4, y4, u4, v4, tint.r, tint.g, tint.b, tint.a); vertex(x4, y4, u4, v4, tint.r, tint.g, tint.b, tint.a);
render(GL_TRIANGLES);
} }
void Batch2D::rect(float x, float y, float w, float h, void Batch2D::rect(float x, float y, float w, float h,
float u, float v, float tx, float ty, float u, float v, float tx, float ty,
float r, float g, float b, float a){ float r, float g, float b, float a){
// if (index + 6*VERTEX_SIZE >= capacity) if (index + 6*VERTEX_SIZE >= capacity)
// render(GL_TRIANGLES); render(GL_TRIANGLES);
vertex(x, y, u, v+ty, r,g,b,a); vertex(x, y, u, v+ty, r,g,b,a);
vertex(x+w, y+h, u+tx, v, r,g,b,a); vertex(x+w, y+h, u+tx, v, r,g,b,a);
vertex(x, y+h, u, v, r,g,b,a); vertex(x, y+h, u, v, r,g,b,a);
@@ -212,7 +210,6 @@ void Batch2D::rect(float x, float y, float w, float h,
vertex(x, y, u, v+ty, r,g,b,a); vertex(x, y, u, v+ty, r,g,b,a);
vertex(x+w, y, u+tx, v+ty, r,g,b,a); vertex(x+w, y, u+tx, v+ty, r,g,b,a);
vertex(x+w, y+h, u+tx, v, r,g,b,a); vertex(x+w, y+h, u+tx, v, r,g,b,a);
render(GL_TRIANGLES);
} }
void Batch2D::rect(float x, float y, float w, float h, void Batch2D::rect(float x, float y, float w, float h,
@@ -221,8 +218,8 @@ void Batch2D::rect(float x, float y, float w, float h,
float r2, float g2, float b2, float r2, float g2, float b2,
float r3, float g3, float b3, float r3, float g3, float b3,
float r4, float g4, float b4, int sh){ float r4, float g4, float b4, int sh){
// if (index + 30*VERTEX_SIZE >= capacity) if (index + 30*VERTEX_SIZE >= capacity)
// render(GL_TRIANGLES); render(GL_TRIANGLES);
vec2 v0 = vec2(x+h/2,y+h/2); vec2 v0 = vec2(x+h/2,y+h/2);
vec2 v1 = vec2(x+w-sh,y); vec2 v1 = vec2(x+w-sh,y);
vec2 v2 = vec2(x+sh,y); vec2 v2 = vec2(x+sh,y);
@@ -273,7 +270,6 @@ void Batch2D::rect(float x, float y, float w, float h,
vertex(v6, vec2(0, 0), r2,g2,b2,1.0f); vertex(v6, vec2(0, 0), r2,g2,b2,1.0f);
vertex(v9, vec2(0, 0), r2,g2,b2,1.0f); vertex(v9, vec2(0, 0), r2,g2,b2,1.0f);
vertex(v1, vec2(0, 0), r2,g2,b2,1.0f); vertex(v1, vec2(0, 0), r2,g2,b2,1.0f);
render(GL_TRIANGLES);
} }
void Batch2D::sprite(Sprite* sprite) { void Batch2D::sprite(Sprite* sprite) {
@@ -353,10 +349,6 @@ void Batch2D::blockSprite(float x, float y, float w, float h, int atlasRes, int
vertex(points[0], uvpoints[6], tint.r, tint.g, tint.b, tint.a); vertex(points[0], uvpoints[6], tint.r, tint.g, tint.b, tint.a);
vertex(points[6], uvpoints[4], tint.r, tint.g, tint.b, tint.a); vertex(points[6], uvpoints[4], tint.r, tint.g, tint.b, tint.a);
vertex(points[1], uvpoints[7], tint.r, tint.g, tint.b, tint.a); vertex(points[1], uvpoints[7], tint.r, tint.g, tint.b, tint.a);
glDisable(GL_MULTISAMPLE);
render(GL_TRIANGLES);
glEnable(GL_MULTISAMPLE);
} }
void Batch2D::render(unsigned int gl_primitive) { void Batch2D::render(unsigned int gl_primitive) {
@@ -364,3 +356,7 @@ void Batch2D::render(unsigned int gl_primitive) {
mesh->draw(gl_primitive); mesh->draw(gl_primitive);
index = 0; index = 0;
} }
void Batch2D::render() {
render(GL_TRIANGLES);
}
+1
View File
@@ -61,6 +61,7 @@ public:
float r3, float g3, float b3, float r3, float g3, float b3,
float r4, float g4, float b4, int sh); float r4, float g4, float b4, int sh);
void render(unsigned int gl_primitive); void render(unsigned int gl_primitive);
void render();
}; };
#endif /* SRC_GRAPHICS_BATCH2D_H_ */ #endif /* SRC_GRAPHICS_BATCH2D_H_ */
+6 -6
View File
@@ -45,9 +45,10 @@ public:
struct EngineSettings { struct EngineSettings {
/* Window width (pixels) */
int displayWidth; int displayWidth;
/* Window height (pixels) */
int displayHeight; int displayHeight;
/* Anti-aliasing samples */ /* Anti-aliasing samples */
int displaySamples; int displaySamples;
/* Window title */ /* Window title */
@@ -144,12 +145,11 @@ void Engine::updateHotkeys() {
void Engine::mainloop() { void Engine::mainloop() {
Camera* camera = level->player->camera; Camera* camera = level->player->camera;
std::cout << "-- preparing systems" << std::endl; std::cout << "-- preparing systems" << std::endl;
World* world = level->world;
WorldRenderer worldRenderer(level, assets); WorldRenderer worldRenderer(level, assets);
HudRenderer hud; HudRenderer hud(assets);
lastTime = glfwGetTime(); lastTime = glfwGetTime();
Window::swapInterval(1); Window::swapInterval(0);
while (!Window::isShouldClose()){ while (!Window::isShouldClose()){
updateTimers(); updateTimers();
updateHotkeys(); updateHotkeys();
@@ -158,9 +158,9 @@ void Engine::mainloop() {
level->chunksController->update(settings.chunksLoadSpeed); level->chunksController->update(settings.chunksLoadSpeed);
worldRenderer.draw(camera, occlusion); worldRenderer.draw(camera, occlusion);
hud.draw(level, assets); hud.draw(level);
if (level->player->debug) { if (level->player->debug) {
hud.drawDebug(level, assets, 1 / delta, occlusion); hud.drawDebug(level, 1 / delta, occlusion);
} }
Window::swapBuffers(); Window::swapBuffers();