From 6ecf2011bca2b80bdb3265a75118b87147d49b78 Mon Sep 17 00:00:00 2001 From: Derek Christ Date: Sat, 9 Jan 2021 20:15:01 +0100 Subject: [PATCH] Add loading screen --- imgui.ini | 2 +- res/shaders/menu.frag | 15 +++++++++++++++ res/shaders/menu.vert | 12 ++++++++++++ src/Controller.cpp | 34 ++++++++++++++++++++++++++++------ src/Controller.h | 17 +++++++++++++++++ src/Framebuffer.cpp | 8 ++++++-- src/Framebuffer.h | 2 +- src/Texture.h | 4 ++++ src/VertexArray.cpp | 18 ++++++++++++++++++ src/VertexArray.h | 4 ++++ 10 files changed, 106 insertions(+), 10 deletions(-) create mode 100644 res/shaders/menu.frag create mode 100644 res/shaders/menu.vert diff --git a/imgui.ini b/imgui.ini index d3c73fe..d71e993 100644 --- a/imgui.ini +++ b/imgui.ini @@ -9,7 +9,7 @@ Size=894,195 Collapsed=0 [Window][Debug Utils] -Pos=12,15 +Pos=13,7 Size=871,365 Collapsed=0 diff --git a/res/shaders/menu.frag b/res/shaders/menu.frag new file mode 100644 index 0000000..0e1eaa4 --- /dev/null +++ b/res/shaders/menu.frag @@ -0,0 +1,15 @@ +#version 330 core + +layout(location = 0) out vec4 f_color; + +in vec2 v_texCoord; + +struct Material { + sampler2D texture_diffuse0; +}; +uniform Material u_material; + +void main() +{ + f_color = texture(u_material.texture_diffuse0, v_texCoord); +} diff --git a/res/shaders/menu.vert b/res/shaders/menu.vert new file mode 100644 index 0000000..f0a0da7 --- /dev/null +++ b/res/shaders/menu.vert @@ -0,0 +1,12 @@ +#version 330 core + +layout(location = 0) in vec3 a_position; +layout(location = 2) in vec2 a_texCoord; + +out vec2 v_texCoord; + +void main() +{ + v_texCoord = a_texCoord; + gl_Position = vec4(a_position, 1.0); +} diff --git a/src/Controller.cpp b/src/Controller.cpp index b2751b2..2133571 100644 --- a/src/Controller.cpp +++ b/src/Controller.cpp @@ -20,6 +20,7 @@ #endif #include "Controller.h" +#include "VertexArray.h" #include "Texture.h" #include "Model.h" #include "Entity.h" @@ -84,10 +85,30 @@ void Controller::run() ShaderProgram lightProgram("res/shaders/light.vert", "res/shaders/light.frag"); ShaderProgram skyboxProgram("res/shaders/skybox.vert", "res/shaders/skybox.frag"); ShaderProgram postProcessingProgram("res/shaders/postprocessing.vert", "res/shaders/postprocessing.frag"); + ShaderProgram menuProgram("res/shaders/menu.vert", "res/shaders/menu.frag"); ShaderProgram directionalShadowDepthProgram("res/shaders/directionalShadowDepth.vert", "res/shaders/directionalShadowDepth.frag"); ShaderProgram pointShadowDepthProgram("res/shaders/pointShadowDepth.vert", "res/shaders/pointShadowDepth.geom", "res/shaders/pointShadowDepth.frag"); updateExposure(&postProcessingProgram); + pp_framebuffer = new Framebuffer(INIT_WINDOW_WIDTH, INIT_WINDOW_HEIGHT, &postProcessingProgram); + + // Show loading screen... + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + Texture loadingScreenTex("res/textures/loading.png", textureType::texture_diffuse); + std::vector loadingScreenVertices = VertexArray::createVertices(loadingScreenVerticesData, 12, loadingScreenTextureCoordinates); + std::vector loadingScreenIndices; + std::vector loadingScreenTextures; + loadingScreenTextures.push_back(&loadingScreenTex); + loadingScreenIndices.assign(loadingScreenIndicesData, loadingScreenIndicesData + 6); + Mesh loadingScreenMesh(loadingScreenVertices, loadingScreenIndices, loadingScreenTextures); + pp_framebuffer->bind(); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + menuProgram.bind(); + loadingScreenMesh.draw(&menuProgram); + menuProgram.unbind(); + pp_framebuffer->unbind(); + pp_framebuffer->render(); + glfwSwapBuffers(gameWindow->getGLFWwindow()); //Model model_backpack("res/models/backpack.ffo"); Model model_cube("res/models/cube.ffo"); @@ -114,8 +135,6 @@ void Controller::run() camera->translate(glm::vec3(0.0f, 1.5f, 5.0f)); - pp_framebuffer = new Framebuffer(INIT_WINDOW_WIDTH, INIT_WINDOW_HEIGHT, &postProcessingProgram); - // This is the game loop while (!glfwWindowShouldClose(gameWindow->getGLFWwindow())) { // --- Timing --- @@ -141,17 +160,20 @@ void Controller::run() lightProgram.unbind(); // --- Render and buffer swap --- - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - + // Calc shadows - static bool drawShadows = true; + static bool drawShadows = false; + static bool firstRun = true; shaderProgram.bind(); shaderProgram.setUniform("b_drawShadows", (int)drawShadows); shaderProgram.unbind(); - if (drawShadows) { + if (drawShadows || firstRun) { + firstRun = false; world.calculateShadows(&directionalShadowDepthProgram, &pointShadowDepthProgram); } + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + pp_framebuffer->bind(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); diff --git a/src/Controller.h b/src/Controller.h index c99673b..c34ed6a 100644 --- a/src/Controller.h +++ b/src/Controller.h @@ -42,4 +42,21 @@ private: bool wireFrameMode = 0; float exposure = 1.0f; + + float loadingScreenVerticesData[12] = { + 1.0f, -1.0f, 0.0f, + -1.0f, 1.0f, 0.0f, + -1.0f, -1.0f, 0.0f, + 1.0f, 1.0f, 0.0f + }; + unsigned int loadingScreenIndicesData[6] = { + 0, 1, 2, + 0, 3, 1 + }; + float loadingScreenTextureCoordinates[8] = { + 1.0f, 0.0f, + 0.0f, 1.0f, + 0.0f, 0.0f, + 1.0f, 1.0f + }; }; diff --git a/src/Framebuffer.cpp b/src/Framebuffer.cpp index cf3d440..422b1fc 100644 --- a/src/Framebuffer.cpp +++ b/src/Framebuffer.cpp @@ -45,7 +45,7 @@ void Framebuffer::unbind() glBindFramebuffer(GL_FRAMEBUFFER, 0); } -void Framebuffer::render() +void Framebuffer::render(GLuint customTextureId) { // Disable wireframe mode GLint wireframe; @@ -54,7 +54,11 @@ void Framebuffer::render() shaderProgram->bind(); glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, getTextureId()); + if(customTextureId) { + glBindTexture(GL_TEXTURE_2D, customTextureId); + } else { + glBindTexture(GL_TEXTURE_2D, getTextureId()); + } GLint location = glGetUniformLocation(shaderProgram->getShaderProgramId(), "u_texture"); glUniform1i(location, 0); diff --git a/src/Framebuffer.h b/src/Framebuffer.h index a0bf68d..7ae29a8 100644 --- a/src/Framebuffer.h +++ b/src/Framebuffer.h @@ -13,7 +13,7 @@ public: void bind(); void unbind(); - void render(); + void render(GLuint customTextureId = 0); GLuint getTextureId() { diff --git a/src/Texture.h b/src/Texture.h index 094a168..3aa08b8 100644 --- a/src/Texture.h +++ b/src/Texture.h @@ -29,6 +29,10 @@ public: { return texturePath; } + GLuint getTextureId() + { + return textureId; + } private: std::string texturePath; diff --git a/src/VertexArray.cpp b/src/VertexArray.cpp index 05b7523..05032df 100644 --- a/src/VertexArray.cpp +++ b/src/VertexArray.cpp @@ -1,4 +1,5 @@ #include +#include #include "VertexArray.h" #include "defines.h" @@ -46,3 +47,20 @@ void VertexArray::unbind() { glBindVertexArray(0); } + +std::vector VertexArray::createVertices(float *vertices, uint32_t numVertices, float *textureCoordinates) { + std::vector vertexVec; + uint32_t i = 0; + uint32_t k = 0; + for(; i < numVertices; i+=3) { + Vertex currentVertex = {}; + currentVertex.position.x = vertices[i]; + currentVertex.position.y = vertices[i+1]; + currentVertex.position.z = vertices[i+2]; + currentVertex.textureCoords.x = textureCoordinates[k]; + currentVertex.textureCoords.y = textureCoordinates[k+1]; + k+=2; + vertexVec.push_back(currentVertex); + } + return vertexVec; +} diff --git a/src/VertexArray.h b/src/VertexArray.h index 5a88215..a04a54a 100644 --- a/src/VertexArray.h +++ b/src/VertexArray.h @@ -2,6 +2,8 @@ #include +#include "defines.h" + class VertexArray { public: @@ -10,6 +12,8 @@ public: void bind(); void unbind(); + + static std::vector createVertices(float *vertices, uint32_t numVertices, float *textureCoordinates); private: GLuint VAO;