[FEATURE] simple GUI support

This commit is contained in:
Menno
2021-05-25 12:36:58 +02:00
parent 97a7501cda
commit 21a7f4f4b2
15 changed files with 260 additions and 25 deletions

View File

@@ -1,8 +1,11 @@
#include <GL/glew.h>
#include <glm/gtc/matrix_transform.hpp>
#include "../models/model.h"
#include "renderer.h"
#include "loader.h"
#include "../toolbox/toolbox.h"
#include "renderer.h"
#include <iostream>
namespace render_engine
{
@@ -12,9 +15,10 @@ namespace render_engine
static const float NEAR_PLANE = 0.01f;
static const float FAR_PLANE = 1000.0f;
/*
This function will load the projectionMatrix into the shader
*/
// GUI variables
static models::RawModel quad;
void Init(shaders::EntityShader& shader)
{
// Faces which are not facing the camera are not rendered
@@ -28,6 +32,10 @@ namespace render_engine
shader.Start();
shader.LoadProjectionMatrix(projectionMatrix);
shader.Stop();
// Initialize the quad for the GUI
std::vector<float> quad_positions = { -1, 1, -1, -1, 1, 1, 1, -1 };
quad = loader::LoadToVAO(quad_positions);
}
/*
@@ -49,7 +57,7 @@ namespace render_engine
const models::RawModel raw_model = model.raw_model;
const models::ModelTexture texture = model.texture;
// Enable the model
// Enable the model (VAO)
glBindVertexArray(raw_model.vao_id);
// Enable the VBO's from the model (VAO)
@@ -67,11 +75,52 @@ namespace render_engine
glBindTexture(GL_TEXTURE_2D, model.texture.texture_id);
glDrawElements(GL_TRIANGLES, raw_model.vertex_count, GL_UNSIGNED_INT, 0);
// Disable the VBO's and model
// Disable the VBO's and model (VAO)
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
glBindVertexArray(0);
}
void Render(std::vector<gui::GuiTexture>& guis, shaders::GuiShader& shader)
{
shader.Start();
// Enable the VAO and the positions VBO
glBindVertexArray(quad.vao_id);
glEnableVertexAttribArray(0);
// Enable alpha blending (for transparency in the texture)
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Disable depth testing to textures with transparency can overlap
glDisable(GL_DEPTH_TEST);
// Render each gui to the screen
for (gui::GuiTexture& gui : guis)
{
// Bind the texture of the gui to the shader
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, gui.texture);
glm::mat4 matrix = toolbox::CreateModelMatrix(gui.position, gui.scale);
shader.LoadModelMatrix(matrix);
glDrawArrays(GL_TRIANGLE_STRIP, 0, quad.vertex_count);
}
// Enable depth test again
glEnable(GL_DEPTH_TEST);
// Disable alpha blending
glDisable(GL_BLEND);
// Disable the VBO and VAO
glDisableVertexAttribArray(0);
glBindVertexArray(0);
shader.Stop();
}
}
}
}