diff --git a/assets/glsl/batch-quad.frag b/assets/glsl/batch-quad.frag new file mode 100644 index 0000000..745e464 --- /dev/null +++ b/assets/glsl/batch-quad.frag @@ -0,0 +1,49 @@ +#version 450 core + +layout(location = 0) out vec4 color; + +in vec4 v_color; +in vec2 v_textureCoordinates; +in flat float v_textureIndex; + +uniform sampler2D u_textures[32]; + +void main() +{ + vec4 textureColor = v_color; + switch(int(v_textureIndex)) { + case 0: break; // Texture unit 0 is reserved for no texture + case 1: textureColor *= texture(u_textures[1], v_textureCoordinates); break; + case 2: textureColor *= texture(u_textures[2], v_textureCoordinates); break; + case 3: textureColor *= texture(u_textures[3], v_textureCoordinates); break; + case 4: textureColor *= texture(u_textures[4], v_textureCoordinates); break; + case 5: textureColor *= texture(u_textures[5], v_textureCoordinates); break; + case 6: textureColor *= texture(u_textures[6], v_textureCoordinates); break; + case 7: textureColor *= texture(u_textures[7], v_textureCoordinates); break; + case 8: textureColor *= texture(u_textures[8], v_textureCoordinates); break; + case 9: textureColor *= texture(u_textures[9], v_textureCoordinates); break; + case 10: textureColor *= texture(u_textures[10], v_textureCoordinates); break; + case 11: textureColor *= texture(u_textures[11], v_textureCoordinates); break; + case 12: textureColor *= texture(u_textures[12], v_textureCoordinates); break; + case 13: textureColor *= texture(u_textures[13], v_textureCoordinates); break; + case 14: textureColor *= texture(u_textures[14], v_textureCoordinates); break; + case 15: textureColor *= texture(u_textures[15], v_textureCoordinates); break; + case 16: textureColor *= texture(u_textures[16], v_textureCoordinates); break; + case 17: textureColor *= texture(u_textures[17], v_textureCoordinates); break; + case 18: textureColor *= texture(u_textures[18], v_textureCoordinates); break; + case 19: textureColor *= texture(u_textures[19], v_textureCoordinates); break; + case 20: textureColor *= texture(u_textures[20], v_textureCoordinates); break; + case 21: textureColor *= texture(u_textures[21], v_textureCoordinates); break; + case 22: textureColor *= texture(u_textures[22], v_textureCoordinates); break; + case 23: textureColor *= texture(u_textures[23], v_textureCoordinates); break; + case 24: textureColor *= texture(u_textures[24], v_textureCoordinates); break; + case 25: textureColor *= texture(u_textures[25], v_textureCoordinates); break; + case 26: textureColor *= texture(u_textures[26], v_textureCoordinates); break; + case 27: textureColor *= texture(u_textures[27], v_textureCoordinates); break; + case 28: textureColor *= texture(u_textures[28], v_textureCoordinates); break; + case 29: textureColor *= texture(u_textures[29], v_textureCoordinates); break; + case 30: textureColor *= texture(u_textures[30], v_textureCoordinates); break; + case 31: textureColor *= texture(u_textures[31], v_textureCoordinates); break; + } + color = textureColor; +} diff --git a/assets/glsl/batch-quad.vert b/assets/glsl/batch-quad.vert new file mode 100644 index 0000000..fa92a1e --- /dev/null +++ b/assets/glsl/batch-quad.vert @@ -0,0 +1,21 @@ +#version 450 core + +layout(location = 0) in vec3 a_position; +layout(location = 1) in vec4 a_color; +layout(location = 2) in vec2 a_textureCoordinates; +layout(location = 3) in float a_textureIndex; + +out vec4 v_color; +out vec2 v_textureCoordinates; +out flat float v_textureIndex; + +uniform mat4 u_projectionView; + +void main() +{ + v_color = a_color; + v_textureCoordinates = a_textureCoordinates; + v_textureIndex = a_textureIndex; + // Vclip = Camera projection * Camera view * Model transform * Vlocal + gl_Position = u_projectionView * vec4(a_position, 1.0f); +} diff --git a/assets/glsl/color.frag b/assets/glsl/color.frag deleted file mode 100644 index 40f755d..0000000 --- a/assets/glsl/color.frag +++ /dev/null @@ -1,10 +0,0 @@ -#version 450 core - -layout(location = 0) out vec4 color; - -in vec4 v_color; - -void main() -{ - color = v_color; -} diff --git a/assets/glsl/color.vert b/assets/glsl/color.vert deleted file mode 100644 index 6cc61c7..0000000 --- a/assets/glsl/color.vert +++ /dev/null @@ -1,16 +0,0 @@ -#version 450 core - -layout(location = 0) in vec3 a_position; -layout(location = 1) in vec4 a_color; - -out vec4 v_color; - -uniform mat4 u_projectionView; -uniform mat4 u_model; - -void main() -{ - v_color = a_color; - // Vclip = Camera projection * Camera view * Model transform * Vlocal - gl_Position = u_projectionView * u_model * vec4(a_position, 1.0f); -} diff --git a/assets/glsl/texture.frag b/assets/glsl/texture.frag deleted file mode 100644 index 493503a..0000000 --- a/assets/glsl/texture.frag +++ /dev/null @@ -1,13 +0,0 @@ -#version 450 core - -layout(location = 0) out vec4 color; - -in vec4 v_color; -in vec2 v_texCoord; - -uniform sampler2D u_texture; - -void main() -{ - color = v_color * texture(u_texture, v_texCoord); -} diff --git a/assets/glsl/texture.vert b/assets/glsl/texture.vert deleted file mode 100644 index a661497..0000000 --- a/assets/glsl/texture.vert +++ /dev/null @@ -1,19 +0,0 @@ -#version 450 core - -layout(location = 0) in vec3 a_position; -layout(location = 1) in vec4 a_color; -layout(location = 2) in vec2 a_texCoord; - -out vec4 v_color; -out vec2 v_texCoord; - -uniform mat4 u_projectionView; -uniform mat4 u_model; - -void main() -{ - v_color = a_color; - v_texCoord = a_texCoord; - // Vclip = Camera projection * Camera view * Model transform * Vlocal - gl_Position = u_projectionView * u_model * vec4(a_position, 1.0f); -} diff --git a/inferno/src/inferno/application.cpp b/inferno/src/inferno/application.cpp index 337f7e0..c5a00d2 100644 --- a/inferno/src/inferno/application.cpp +++ b/inferno/src/inferno/application.cpp @@ -1,18 +1,22 @@ #include "inferno/application.h" #include "inferno/assertions.h" +#include "inferno/component/transform.h" #include "inferno/core.h" #include "inferno/event/applicationevent.h" #include "inferno/event/event.h" #include "inferno/event/keyevent.h" +#include "inferno/event/mouseevent.h" #include "inferno/input.h" #include "inferno/inputcodes.h" #include "inferno/log.h" #include "inferno/settings.h" #include "inferno/render/buffer.h" +#include "inferno/render/camera.h" #include "inferno/render/context.h" #include "inferno/render/renderer.h" #include "inferno/render/shader.h" #include "inferno/render/texture.h" +#include "inferno/time.h" #include "inferno/window.h" namespace Inferno { @@ -33,99 +37,72 @@ namespace Inferno { Input::initialize(); + m_cameraO = std::make_shared(); + m_cameraP = std::make_shared(); + TextureManager textureManager; m_texture = textureManager.load("assets/gfx/test.png"); + m_texture2 = textureManager.load("assets/gfx/test-inverted.png"); -// ----------------------------------------- - - float verticesColor[] = { - -0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, - 0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, - 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, - -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, - }; - - uint32_t indicesColor[] = { - 0, 1, 2, 2, 3, 0 - }; - - m_vertexArrayColor = std::make_shared(); - - std::shared_ptr vertexBufferColor = std::make_shared(verticesColor, sizeof(verticesColor)); - vertexBufferColor->setLayout({ - { BufferElementType::Vec3, "a_position" }, - { BufferElementType::Vec4, "a_color" }, - }); - - m_vertexArrayColor->addVertexBuffer(vertexBufferColor); - - std::shared_ptr indexBufferColor = std::make_shared(indicesColor, sizeof(indicesColor)); - - m_vertexArrayColor->setIndexBuffer(indexBufferColor); - -// ----------------------------------------- + Renderer2D::initialize(); + } - m_vertexArrayTexture = std::make_shared(); + Application::~Application() + { + Renderer2D::destroy(); + // Input::destroy(); + Settings::destroy(); + } - float verticesTexture[] = { - -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, - 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, - 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, - -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, - }; + void Application::run() + { + dbg() << "Application startup"; - uint32_t indicesTexture[] = { - 0, 1, 2, 2, 3, 0 + glm::mat4 colors = { + 1.0f, 0.0f, 1.0f, 1.0f, // Lower left corner: purple + 1.0f, 1.0f, 0.0f, 1.0f, + 0.0f, 1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, 1.0f, }; - std::shared_ptr vertexBufferTexture = std::make_shared(verticesTexture, sizeof(verticesTexture)); - vertexBufferTexture->setLayout({ - { BufferElementType::Vec3, "a_position" }, - { BufferElementType::Vec2, "a_texCoord" }, - }); + Transform cube({0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {1.0f, 1.0f, 1.0f}); + cube.update(); - m_vertexArrayTexture->addVertexBuffer(vertexBufferTexture); + Transform cube2({1.1f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {1.0f, 1.0f, 1.0f}); + cube2.update(); - std::shared_ptr indexBufferTexture = std::make_shared(indicesTexture, sizeof(indicesTexture)); + Transform cube3({2.2f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {1.0f, 1.0f, 1.0f}); + cube3.update(); - m_vertexArrayTexture->setIndexBuffer(indexBufferTexture); + while(!m_window->shouldClose()) { -// ----------------------------------------- + float time = Time::time(); + float deltaTime = time - m_lastFrameTime; + m_lastFrameTime = time; + // dbg() << "Frametime " << deltaTime * 1000 << "ms"; - ShaderManager shaderManager; - m_shaderSimple = shaderManager.load("assets/glsl/simple"); - m_shaderTexture = shaderManager.load("assets/glsl/texture"); - m_shaderTexture->setInt("u_texture", m_texture->id()); - } + // Update - Application::~Application() - { - } - - void Application::run() - { - dbg() << "Application startup"; + Input::update(); + m_window->update(); + m_cameraO->update(deltaTime); + m_cameraP->update(deltaTime); - while(!m_window->shouldClose()) { + // Render - Command::clearColor({ 0.2f, 0.3f, 0.3f, 1.0f }); - Command::clear(); + RenderCommand::clearColor({ 0.2f, 0.3f, 0.3f, 1.0f }); + RenderCommand::clear(); - // Renderer::beginScene(); // camera, lights, environment + Renderer2D::beginScene(m_cameraP); // camera, lights, environment - // m_shaderSimple->bind(); - // Renderer::submit(m_vertexArrayColor); - // m_shaderSimple->unbind(); + Renderer2D::drawQuad(std::make_shared(cube), colors); + Renderer2D::drawQuad(std::make_shared(cube2), { 0.5f, 0.6f, 0.8f, 1.0f }, m_texture); + Renderer2D::drawQuad(std::make_shared(cube3), { 1.0f, 1.0f, 1.0f, 1.0f }, m_texture2); - m_shaderTexture->bind(); - m_texture->bind(); - Renderer::submit(m_vertexArrayTexture); - m_texture->unbind(); - m_shaderTexture->unbind(); + Renderer2D::endScene(); - // Renderer::endScene(); + m_window->render(); - m_window->update(); } dbg() << "Application shutdown"; @@ -137,6 +114,7 @@ namespace Inferno { dispatcher.dispatch(NF_BIND_EVENT(Application::onWindowClose)); dispatcher.dispatch(NF_BIND_EVENT(Application::onWindowResize)); dispatcher.dispatch(NF_BIND_EVENT(Application::onKeyPress)); + dispatcher.dispatch(NF_BIND_EVENT(Application::onMousePosition)); } bool Application::onWindowClose(WindowCloseEvent& e) @@ -179,4 +157,10 @@ namespace Inferno { return true; } + + bool Application::onMousePosition(MousePositionEvent& e) + { + return Input::onMousePosition(e); + } + } diff --git a/inferno/src/inferno/application.h b/inferno/src/inferno/application.h index 5f8eed1..e1dbfc6 100644 --- a/inferno/src/inferno/application.h +++ b/inferno/src/inferno/application.h @@ -7,6 +7,9 @@ namespace Inferno { class Event; class KeyPressEvent; + class MousePositionEvent; + class OrthographicCamera; + class PerspectiveCamera; class Texture; class TextureManager; class Window; @@ -27,6 +30,7 @@ namespace Inferno { bool onWindowClose(WindowCloseEvent& e); bool onWindowResize(WindowResizeEvent& e); bool onKeyPress(KeyPressEvent& e); + bool onMousePosition(MousePositionEvent& e); // ----------------------------------------- @@ -36,13 +40,15 @@ namespace Inferno { private: std::unique_ptr m_window; + // std::unique_ptr m_camera; + std::shared_ptr m_cameraO; + std::shared_ptr m_cameraP; + + float m_lastFrameTime = 0.0f; // - std::shared_ptr m_vertexArrayColor; - std::shared_ptr m_vertexArrayTexture; - std::shared_ptr m_shaderSimple; - std::shared_ptr m_shaderTexture; std::shared_ptr m_texture; + std::shared_ptr m_texture2; // static Application* s_instance; @@ -55,6 +61,13 @@ namespace Inferno { #endif // APPLICATION_H +// C++17 features used: +// - std::string:view, log.h +// - std::shared_ptr array management, renderer.h + +// OpenGL 4.5 features used: +// - + // Gameplan // v Entrypoint // v Logging @@ -81,3 +94,5 @@ namespace Inferno { // @Todo // - Settings should contain all file paths (ex: shaders) +// - RefPtr<> +// - Rename Application::get() to Application::the() for singleton diff --git a/inferno/src/inferno/time.cpp b/inferno/src/inferno/time.cpp new file mode 100644 index 0000000..74ddb61 --- /dev/null +++ b/inferno/src/inferno/time.cpp @@ -0,0 +1,12 @@ +#include + +#include "inferno/time.h" + +namespace Inferno { + + float Time::time() + { + return static_cast(glfwGetTime()); + } + +} diff --git a/inferno/src/inferno/time.h b/inferno/src/inferno/time.h new file mode 100644 index 0000000..67bdcbc --- /dev/null +++ b/inferno/src/inferno/time.h @@ -0,0 +1,13 @@ +#ifndef TIME_H +#define TIME_H + +namespace Inferno { + + class Time { + public: + static float time(); + }; + +} + +#endif // TIME_H