Browse Source

Implement batch rendering in application.cpp, combine color / texure shader into one batch shader

master
Riyyi 3 years ago
parent
commit
b947dcf5a8
  1. 49
      assets/glsl/batch-quad.frag
  2. 21
      assets/glsl/batch-quad.vert
  3. 10
      assets/glsl/color.frag
  4. 16
      assets/glsl/color.vert
  5. 13
      assets/glsl/texture.frag
  6. 19
      assets/glsl/texture.vert
  7. 128
      inferno/src/inferno/application.cpp
  8. 23
      inferno/src/inferno/application.h
  9. 12
      inferno/src/inferno/time.cpp
  10. 13
      inferno/src/inferno/time.h

49
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;
}

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

10
assets/glsl/color.frag

@ -1,10 +0,0 @@
#version 450 core
layout(location = 0) out vec4 color;
in vec4 v_color;
void main()
{
color = v_color;
}

16
assets/glsl/color.vert

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

13
assets/glsl/texture.frag

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

19
assets/glsl/texture.vert

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

128
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<OrthographicCamera>();
m_cameraP = std::make_shared<PerspectiveCamera>();
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<VertexArray>();
std::shared_ptr<VertexBuffer> vertexBufferColor = std::make_shared<VertexBuffer>(verticesColor, sizeof(verticesColor));
vertexBufferColor->setLayout({
{ BufferElementType::Vec3, "a_position" },
{ BufferElementType::Vec4, "a_color" },
});
m_vertexArrayColor->addVertexBuffer(vertexBufferColor);
std::shared_ptr<IndexBuffer> indexBufferColor = std::make_shared<IndexBuffer>(indicesColor, sizeof(indicesColor));
m_vertexArrayColor->setIndexBuffer(indexBufferColor);
// -----------------------------------------
Renderer2D::initialize();
}
m_vertexArrayTexture = std::make_shared<VertexArray>();
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<VertexBuffer> vertexBufferTexture = std::make_shared<VertexBuffer>(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<IndexBuffer> indexBufferTexture = std::make_shared<IndexBuffer>(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<Transform>(cube), colors);
Renderer2D::drawQuad(std::make_shared<Transform>(cube2), { 0.5f, 0.6f, 0.8f, 1.0f }, m_texture);
Renderer2D::drawQuad(std::make_shared<Transform>(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<WindowCloseEvent>(NF_BIND_EVENT(Application::onWindowClose));
dispatcher.dispatch<WindowResizeEvent>(NF_BIND_EVENT(Application::onWindowResize));
dispatcher.dispatch<KeyPressEvent>(NF_BIND_EVENT(Application::onKeyPress));
dispatcher.dispatch<MousePositionEvent>(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);
}
}

23
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<Window> m_window;
// std::unique_ptr<Camera> m_camera;
std::shared_ptr<OrthographicCamera> m_cameraO;
std::shared_ptr<PerspectiveCamera> m_cameraP;
float m_lastFrameTime = 0.0f;
//
std::shared_ptr<VertexArray> m_vertexArrayColor;
std::shared_ptr<VertexArray> m_vertexArrayTexture;
std::shared_ptr<Shader> m_shaderSimple;
std::shared_ptr<Shader> m_shaderTexture;
std::shared_ptr<Texture> m_texture;
std::shared_ptr<Texture> 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

12
inferno/src/inferno/time.cpp

@ -0,0 +1,12 @@
#include <GLFW/glfw3.h>
#include "inferno/time.h"
namespace Inferno {
float Time::time()
{
return static_cast<float>(glfwGetTime());
}
}

13
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
Loading…
Cancel
Save