[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

@@ -19,7 +19,7 @@ namespace render_engine
static std::vector<GLuint> textures;
/*
This function will generate a Model from vertex positions, textureCoordinates and indices.
This function will generate a Model from vertex positions, textureCoordinates normals and indices.
*/
models::RawModel LoadToVAO(std::vector<float>& positions, std::vector<float>& texture_coords, std::vector<float>& normals, std::vector<unsigned int>& indices)
{
@@ -35,6 +35,17 @@ namespace render_engine
return { vao_id, static_cast<int>(indices.size()), model_size };
}
/*
This function will generate a Model from vertex positions.
*/
models::RawModel LoadToVAO(std::vector<float>& positions)
{
const GLuint vao_id = CreateVao();
StoreDataInAttributeList(0, 2, positions);
glBindVertexArray(0);
return { vao_id, static_cast<int>(positions.size()) / 2 };
}
/*
Loads an image as texture into openGL
*/

View File

@@ -9,24 +9,36 @@ namespace render_engine
namespace loader
{
/*
@brief: This function generates a model from model data.
@param position: The positions of each vertex (in order: x, y, z) in the model
@param texture_coords: The texture coordinates of the model
@param normals: The normals of each face of the model
@param indices: A list with a sort of lookup table to the positions parameter
* @brief: This function generates a model from model data.
*
* @param position: The positions of each vertex (in order: x, y, z) in the model
* @param texture_coords: The texture coordinates of the model
* @param normals: The normals of each face of the model
* @param indices: A list with a sort of lookup table to the positions parameter
*
* @return: A new rawmodel which represents al the parameters in one struct
*/
models::RawModel LoadToVAO(std::vector<float>& positions, std::vector<float>& texture_coords, std::vector<float>& normals, std::vector<unsigned int>& indices);
/*
@brief: Loads a texture from a file into openGL using stb_image.h
@param file_name: The filepath to the texture
* @brief: Overloaded function of the function above, but does not need normals and indices.
* Use this function to for example load GUI items to OpenGL.
*
* @param position: The positions of each vertex (in order: x, y, z) in the model
*
* @return: A new rawmodel which represents al the parameters in one struct
*/
models::RawModel LoadToVAO(std::vector<float>& positions);
/*
* @brief: Loads a texture from a file into openGL using stb_image.h
*
* @param file_name: The filepath to the texture
*/
GLuint LoadTexture(std::string file_name);
/*
@brief: Call this function when cleaning up all the meshes (when exiting the program).
* @brief: Call this function when cleaning up all the meshes (when exiting the program).
*/
void CleanUp();
}

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();
}
}
}
}

View File

@@ -1,7 +1,9 @@
#pragma once
#include "../gui/gui_texture.h"
#include "../entities/entity.h"
#include "../shaders/entity_shader.h"
#include "../shaders/gui_shader.h"
namespace render_engine
{
@@ -29,5 +31,13 @@ namespace render_engine
@param shader: The shader the entity needs to be rendered with
*/
void Render(entities::Entity& entity, shaders::EntityShader& shader);
/*
@brief: Call this function to render gui_textures on the screen
@param guis: A list with all the GUI textures you want to render
@param shade: The shader the GUI textures need to be rendered with
*/
void Render(std::vector<gui::GuiTexture>& guis, shaders::GuiShader& shader);
}
}