Flipped UI, minor changes

Flipped UI, removed extra Batch2D, added comments and minor changes
This commit is contained in:
MihailRis
2022-06-29 14:03:36 +03:00
committed by GitHub
parent c3b4357852
commit 88c1695903
7 changed files with 67 additions and 66 deletions
+6 -6
View File
@@ -74,13 +74,13 @@ void Batch2D::rect(float x, float y, float w, float h){
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){
vertex(x, y, u, v, r,g,b,a); vertex(x, y, u, v+ty, r,g,b,a);
vertex(x+w, y+h, u+tx, v+ty, r,g,b,a); vertex(x+w, y+h, u+tx, v, r,g,b,a);
vertex(x, y+h, u, v+ty, r,g,b,a); vertex(x, y+h, u, v, r,g,b,a);
vertex(x, y, u, v, r,g,b,a); vertex(x, y, u, v+ty, r,g,b,a);
vertex(x+w, y, u+tx, v, r,g,b,a); vertex(x+w, y, u+tx, v+ty, r,g,b,a);
vertex(x+w, y+h, u+tx, v+ty, r,g,b,a); vertex(x+w, y+h, u+tx, v, r,g,b,a);
} }
void Batch2D::render() { void Batch2D::render() {
+16 -20
View File
@@ -9,15 +9,8 @@ Font::~Font(){
delete texture; delete texture;
} }
int Font::getGlyphWidth(char c) {
void Font::draw(Batch2D* batch, std::string text, int x, int y) { switch (c){
for (char c : text){
float u = (c % 16) / 16.0f;
float v = 1.0f - ((c / 16) / 16.0f) - 1.0f/16.0f;
batch->rect(x, y, 8, 8, u, v, 1.0f/16.0f, 1.0f/16.0f, 1,1,1,1);
int gw = 7;
switch (c){
case 'l': case 'l':
case 'i': case 'i':
case 'j': case 'j':
@@ -25,16 +18,19 @@ void Font::draw(Batch2D* batch, std::string text, int x, int y) {
case '.': case '.':
case ',': case ',':
case ':': case ':':
case ';': case ';': return 3;
gw = 3; case 't': return 5;
break; case ' ': return 3;
case 't': }
gw = 5; return 7;
break; }
case ' ':
gw = 3;
break; void Font::draw(Batch2D* batch, std::string text, int x, int y) {
} for (char c : text){
x += gw; float u = (c % 16) / 16.0f;
float v = 1.0f - ((c / 16) / 16.0f) - 1.0f/16.0f;
batch->rect(x, y, 8, 8, u, v, 1.0f/16.0f, 1.0f/16.0f, 1,1,1,1);
x += getGlyphWidth(c);
} }
} }
+1
View File
@@ -12,6 +12,7 @@ public:
Font(Texture* texture); Font(Texture* texture);
~Font(); ~Font();
int getGlyphWidth(char c);
void draw(Batch2D* batch, std::string text, int x, int y); void draw(Batch2D* batch, std::string text, int x, int y);
}; };
+2 -1
View File
@@ -333,7 +333,8 @@ int main() {
for (int i = 0; i < freeLoaders; i++) for (int i = 0; i < freeLoaders; i++)
chunksController.loadVisible(wfile); chunksController.loadVisible(wfile);
draw_world(player, camera, assets, chunks, occlusion, devdata, fps); draw_world(player, camera, assets, chunks, occlusion);
draw_hud(player, assets, devdata, fps);
Window::swapBuffers(); Window::swapBuffers();
Events::pullEvents(); Events::pullEvents();
+8 -2
View File
@@ -40,9 +40,15 @@ mat4 Camera::getProjection(){
if (perspective) if (perspective)
return glm::perspective(fov*zoom, aspect, 0.05f, 1500.0f); return glm::perspective(fov*zoom, aspect, 0.05f, 1500.0f);
else else
return glm::ortho(0.0f, fov*aspect, 0.0f, fov); if (flipped)
return glm::ortho(0.0f, fov*aspect, fov, 0.0f);
else
return glm::ortho(0.0f, fov*aspect, 0.0f, fov);
} }
mat4 Camera::getView(){ mat4 Camera::getView(){
return glm::lookAt(position, position+front, up); if (perspective)
return glm::lookAt(position, position+front, up);
else
return glm::mat4(1.0f);
} }
+1
View File
@@ -24,6 +24,7 @@ public:
float zoom; float zoom;
mat4 rotation; mat4 rotation;
bool perspective = true; bool perspective = true;
bool flipped = false;
Camera(vec3 position, float fov); Camera(vec3 position, float fov);
void rotate(float x, float y, float z); void rotate(float x, float y, float z);
+33 -37
View File
@@ -45,7 +45,6 @@ int uiscale = 2;
LineBatch *lineBatch; LineBatch *lineBatch;
Batch2D *batch; Batch2D *batch;
Batch2D *activeBlockBatch;
Camera *uicamera; Camera *uicamera;
void init_renderer(){ void init_renderer(){
@@ -53,9 +52,9 @@ void init_renderer(){
lineBatch = new LineBatch(4096); lineBatch = new LineBatch(4096);
batch = new Batch2D(1024); batch = new Batch2D(1024);
activeBlockBatch = new Batch2D(1024);
uicamera = new Camera(glm::vec3(), Window::height / uiscale); uicamera = new Camera(glm::vec3(), Window::height / uiscale);
uicamera->perspective = false; uicamera->perspective = false;
uicamera->flipped = true;
} }
@@ -63,7 +62,6 @@ void finalize_renderer(){
delete crosshair; delete crosshair;
delete lineBatch; delete lineBatch;
delete batch; delete batch;
delete activeBlockBatch;
} }
void draw_chunk(size_t index, Camera* camera, Shader* shader, bool occlusion){ void draw_chunk(size_t index, Camera* camera, Shader* shader, bool occlusion){
@@ -112,17 +110,45 @@ bool chunks_comparator(size_t i, size_t j) {
} }
void draw_world(Player* player, Camera* camera, Assets* assets, Chunks* chunks, void draw_hud(Player* player, Assets* assets, bool devdata, int fps){
bool occlusion, bool devdata, int fps){ glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
Shader* uishader = assets->getShader("ui");
uishader->use();
uishader->uniformMatrix("u_projview", uicamera->getProjection()*uicamera->getView());
// draw debug info
Font* font = assets->getFont("normal");
batch->begin();
batch->texture(font->texture);
if (devdata){
font->draw(batch, "devdata does not exist", 16, 16);
font->draw(batch, std::to_string((int)player->camera->position.x), 10, 30);
font->draw(batch, std::to_string((int)player->camera->position.y), 50, 30);
font->draw(batch, std::to_string((int)player->camera->position.z), 90, 30);
font->draw(batch, "fps:", 16, 42);
font->draw(batch, std::to_string(fps), 40, 42);
}
batch->render();
// choosen block preview
Texture* blocks = assets->getTexture("block_select");
batch->texture(blocks);
float u = (player->choosenBlock % 16) / 16.0f;
float v = 1.0f - ((player->choosenBlock / 16) / 16.0f) - 1.0f/16.0f;
batch->rect(16, 280, 64, 64, u, v, 1.0f/16.0f, 1.0f/16.0f, 1,1,1,1);
batch->render();
}
void draw_world(Player* player, Camera* camera, Assets* assets, Chunks* chunks, bool occlusion){
glClearColor(0.7f,0.81f,1.0f,1); glClearColor(0.7f,0.81f,1.0f,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
_chunks = chunks; _chunks = chunks;
// Draw VAO
Texture* texture = assets->getTexture("block"); Texture* texture = assets->getTexture("block");
Texture* blocks = assets->getTexture("block_select");
Shader* shader = assets->getShader("main"); Shader* shader = assets->getShader("main");
Shader* crosshairShader = assets->getShader("crosshair"); Shader* crosshairShader = assets->getShader("crosshair");
Shader* linesShader = assets->getShader("lines"); Shader* linesShader = assets->getShader("lines");
@@ -134,7 +160,6 @@ void draw_world(Player* player, Camera* camera, Assets* assets, Chunks* chunks,
shader->uniform3f("u_fogColor", 0.7f,0.71f,0.73f); shader->uniform3f("u_fogColor", 0.7f,0.71f,0.73f);
shader->uniform3f("u_cameraPos", camera->position.x,camera->position.y,camera->position.z); shader->uniform3f("u_cameraPos", camera->position.x,camera->position.y,camera->position.z);
texture->bind(); texture->bind();
// blocks->bind();
std::vector<size_t> indices; std::vector<size_t> indices;
@@ -171,35 +196,6 @@ void draw_world(Player* player, Camera* camera, Assets* assets, Chunks* chunks,
lineBatch->line(camera->position.x, camera->position.y-0.1f, camera->position.z, camera->position.x, camera->position.y-0.1f, camera->position.z+0.01f, 0, 0, 1, 1); lineBatch->line(camera->position.x, camera->position.y-0.1f, camera->position.z, camera->position.x, camera->position.y-0.1f, camera->position.z+0.01f, 0, 0, 1, 1);
lineBatch->line(camera->position.x, camera->position.y-0.1f, camera->position.z, camera->position.x, camera->position.y-0.1f+0.01f, camera->position.z, 0, 1, 0, 1); lineBatch->line(camera->position.x, camera->position.y-0.1f, camera->position.z, camera->position.x, camera->position.y-0.1f+0.01f, camera->position.z, 0, 1, 0, 1);
lineBatch->render(); lineBatch->render();
glDisable(GL_DEPTH_TEST);
Shader* uishader = assets->getShader("ui");
uishader->use();
uishader->uniformMatrix("u_projview", uicamera->getProjection());
Font* font = assets->getFont("normal");
// Texture* blocks = assets->getTexture("blocks")
batch->begin();
batch->texture(font->texture);
// font->draw(batch, "void Font::draw(Batch2D* batch, std::string text, int x, int y) {", 10, 10);
if (devdata){
font->draw(batch, "devdata does not exist", 16, 16);
font->draw(batch, std::to_string((int)player->camera->position.x), 10, 30);
font->draw(batch, std::to_string((int)player->camera->position.y), 50, 30);
font->draw(batch, std::to_string((int)player->camera->position.z), 90, 30);
font->draw(batch, "fps:", 16, 42);
font->draw(batch, std::to_string(fps), 40, 42);
}
//batch->rect(0, 0, 256, 256);
batch->render();
activeBlockBatch->begin();
activeBlockBatch->texture(blocks);
float u = (player->choosenBlock % 16) / 16.0f;
float v = 1.0f - ((player->choosenBlock / 16) / 16.0f) - 1.0f/16.0f;
activeBlockBatch->rect(16, 280, 64, 64, u, v, 1.0f/16.0f, 1.0f/16.0f, 1,1,1,1);
activeBlockBatch->render();
} }
#endif // WORLD_RENDERER_CPP #endif // WORLD_RENDERER_CPP