|
|
@ -8,7 +8,6 @@ |
|
|
|
#include <span> |
|
|
|
#include <span> |
|
|
|
|
|
|
|
|
|
|
|
#include "glad/glad.h" |
|
|
|
#include "glad/glad.h" |
|
|
|
#include "glm/ext/vector_float4.hpp" // glm::vec4 |
|
|
|
|
|
|
|
#include "ruc/format/log.h" |
|
|
|
#include "ruc/format/log.h" |
|
|
|
|
|
|
|
|
|
|
|
#include "inferno/asset/asset-manager.h" |
|
|
|
#include "inferno/asset/asset-manager.h" |
|
|
@ -171,7 +170,6 @@ void Renderer<T>::flush() |
|
|
|
// Render
|
|
|
|
// Render
|
|
|
|
bool depthTest = RenderCommand::depthTest(); |
|
|
|
bool depthTest = RenderCommand::depthTest(); |
|
|
|
RenderCommand::setDepthTest(m_enableDepthBuffer); |
|
|
|
RenderCommand::setDepthTest(m_enableDepthBuffer); |
|
|
|
RenderCommand::setColorAttachmentCount(m_colorAttachmentCount); |
|
|
|
|
|
|
|
RenderCommand::drawIndexed(m_vertexArray, m_elementIndex); |
|
|
|
RenderCommand::drawIndexed(m_vertexArray, m_elementIndex); |
|
|
|
RenderCommand::setDepthTest(depthTest); |
|
|
|
RenderCommand::setDepthTest(depthTest); |
|
|
|
|
|
|
|
|
|
|
@ -198,11 +196,6 @@ void Renderer<T>::nextBatch() |
|
|
|
// -----------------------------------------
|
|
|
|
// -----------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
Renderer2D::Renderer2D(s) |
|
|
|
Renderer2D::Renderer2D(s) |
|
|
|
{ |
|
|
|
|
|
|
|
Renderer2D::initialize(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Renderer2D::initialize() |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
Renderer::initialize(); |
|
|
|
Renderer::initialize(); |
|
|
|
|
|
|
|
|
|
|
@ -222,21 +215,26 @@ void Renderer2D::initialize() |
|
|
|
// ---------------------------------
|
|
|
|
// ---------------------------------
|
|
|
|
// GPU
|
|
|
|
// GPU
|
|
|
|
|
|
|
|
|
|
|
|
m_enableDepthBuffer = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Create vertex buffer
|
|
|
|
// Create vertex buffer
|
|
|
|
auto vertexBuffer = std::make_shared<VertexBuffer>(sizeof(QuadVertex) * maxVertices); |
|
|
|
auto vertexBuffer = std::make_shared<VertexBuffer>(sizeof(QuadVertex) * maxVertices); |
|
|
|
vertexBuffer->setLayout({ |
|
|
|
vertexBuffer->setLayout({ |
|
|
|
{ BufferElementType::Vec3, "a_position" }, |
|
|
|
{ BufferElementType::Vec3, "a_position" }, |
|
|
|
{ BufferElementType::Vec4, "a_color" }, |
|
|
|
{ BufferElementType::Vec4, "a_color" }, |
|
|
|
{ BufferElementType::Vec2, "a_textureCoordinates" }, |
|
|
|
{ BufferElementType::Vec2, "a_textureCoordinates" }, |
|
|
|
{ BufferElementType::Uint, "a_textureIndex" }, |
|
|
|
{ BufferElementType::Float, "a_textureIndex" }, |
|
|
|
}); |
|
|
|
}); |
|
|
|
m_vertexArray->addVertexBuffer(vertexBuffer); |
|
|
|
m_vertexArray->addVertexBuffer(vertexBuffer); |
|
|
|
|
|
|
|
|
|
|
|
ruc::info("Renderer2D initialized"); |
|
|
|
ruc::info("Renderer2D initialized"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Renderer2D::beginScene(glm::mat4 cameraProjection, glm::mat4 cameraView) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
m_shader->bind(); |
|
|
|
|
|
|
|
m_shader->setFloat("u_projectionView", cameraProjection * cameraView); |
|
|
|
|
|
|
|
m_shader->unbind(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Renderer2D::drawQuad(const TransformComponent& transform, glm::vec4 color) |
|
|
|
void Renderer2D::drawQuad(const TransformComponent& transform, glm::vec4 color) |
|
|
|
{ |
|
|
|
{ |
|
|
|
drawQuad(transform, color, nullptr); |
|
|
|
drawQuad(transform, color, nullptr); |
|
|
@ -273,7 +271,7 @@ void Renderer2D::drawQuad(const TransformComponent& transform, glm::mat4 color, |
|
|
|
m_vertexBufferPtr->position = transform.transform * m_vertexPositions[i]; |
|
|
|
m_vertexBufferPtr->position = transform.transform * m_vertexPositions[i]; |
|
|
|
m_vertexBufferPtr->color = color[i]; |
|
|
|
m_vertexBufferPtr->color = color[i]; |
|
|
|
m_vertexBufferPtr->textureCoordinates = textureCoordinates[i]; |
|
|
|
m_vertexBufferPtr->textureCoordinates = textureCoordinates[i]; |
|
|
|
m_vertexBufferPtr->textureIndex = textureUnitIndex; |
|
|
|
m_vertexBufferPtr->textureIndex = static_cast<float>(textureUnitIndex); |
|
|
|
m_vertexBufferPtr++; |
|
|
|
m_vertexBufferPtr++; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -283,17 +281,12 @@ void Renderer2D::drawQuad(const TransformComponent& transform, glm::mat4 color, |
|
|
|
|
|
|
|
|
|
|
|
void Renderer2D::loadShader() |
|
|
|
void Renderer2D::loadShader() |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_shader = AssetManager::the().load<Shader>("assets/glsl/post-process"); |
|
|
|
m_shader = AssetManager::the().load<Shader>("assets/glsl/batch-quad"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------
|
|
|
|
// -----------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
RendererCubemap::RendererCubemap(s) |
|
|
|
RendererCubemap::RendererCubemap(s) |
|
|
|
{ |
|
|
|
|
|
|
|
RendererCubemap::initialize(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void RendererCubemap::initialize() |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
Renderer::initialize(); |
|
|
|
Renderer::initialize(); |
|
|
|
|
|
|
|
|
|
|
@ -352,7 +345,7 @@ void RendererCubemap::initialize() |
|
|
|
vertexBuffer->setLayout({ |
|
|
|
vertexBuffer->setLayout({ |
|
|
|
{ BufferElementType::Vec3, "a_position" }, |
|
|
|
{ BufferElementType::Vec3, "a_position" }, |
|
|
|
{ BufferElementType::Vec4, "a_color" }, |
|
|
|
{ BufferElementType::Vec4, "a_color" }, |
|
|
|
{ BufferElementType::Uint, "a_textureIndex" }, |
|
|
|
{ BufferElementType::Float, "a_textureIndex" }, |
|
|
|
}); |
|
|
|
}); |
|
|
|
m_vertexArray->addVertexBuffer(vertexBuffer); |
|
|
|
m_vertexArray->addVertexBuffer(vertexBuffer); |
|
|
|
|
|
|
|
|
|
|
@ -367,10 +360,10 @@ void RendererCubemap::beginScene(glm::mat4 cameraProjection, glm::mat4 cameraVie |
|
|
|
// x x x 0
|
|
|
|
// x x x 0
|
|
|
|
// x x x 0
|
|
|
|
// x x x 0
|
|
|
|
// 0 0 0 1
|
|
|
|
// 0 0 0 1
|
|
|
|
// cameraView = glm::mat4(glm::mat3(cameraView));
|
|
|
|
cameraView = glm::mat4(glm::mat3(cameraView)); |
|
|
|
|
|
|
|
|
|
|
|
m_shader->bind(); |
|
|
|
m_shader->bind(); |
|
|
|
m_shader->setFloat("u_projectionView2", cameraProjection * cameraView); |
|
|
|
m_shader->setFloat("u_projectionView", cameraProjection * cameraView); |
|
|
|
m_shader->unbind(); |
|
|
|
m_shader->unbind(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -393,7 +386,7 @@ void RendererCubemap::drawCubemap(const TransformComponent& transform, glm::mat4 |
|
|
|
for (uint32_t i = 0; i < vertexPerQuad * quadPerCube; i++) { |
|
|
|
for (uint32_t i = 0; i < vertexPerQuad * quadPerCube; i++) { |
|
|
|
m_vertexBufferPtr->position = transform.transform * m_vertexPositions[i]; |
|
|
|
m_vertexBufferPtr->position = transform.transform * m_vertexPositions[i]; |
|
|
|
m_vertexBufferPtr->color = color[i % 4]; |
|
|
|
m_vertexBufferPtr->color = color[i % 4]; |
|
|
|
m_vertexBufferPtr->textureIndex = textureUnitIndex; |
|
|
|
m_vertexBufferPtr->textureIndex = static_cast<float>(textureUnitIndex); |
|
|
|
m_vertexBufferPtr++; |
|
|
|
m_vertexBufferPtr++; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -430,7 +423,7 @@ RendererFont::RendererFont(s) |
|
|
|
{ BufferElementType::Vec3, "a_position" }, |
|
|
|
{ BufferElementType::Vec3, "a_position" }, |
|
|
|
{ BufferElementType::Vec4, "a_color" }, |
|
|
|
{ BufferElementType::Vec4, "a_color" }, |
|
|
|
{ BufferElementType::Vec2, "a_textureCoordinates" }, |
|
|
|
{ BufferElementType::Vec2, "a_textureCoordinates" }, |
|
|
|
{ BufferElementType::Uint, "a_textureIndex" }, |
|
|
|
{ BufferElementType::Float, "a_textureIndex" }, |
|
|
|
{ BufferElementType::Float, "a_width" }, |
|
|
|
{ BufferElementType::Float, "a_width" }, |
|
|
|
{ BufferElementType::Float, "a_edge" }, |
|
|
|
{ BufferElementType::Float, "a_edge" }, |
|
|
|
{ BufferElementType::Float, "a_borderWidth" }, |
|
|
|
{ BufferElementType::Float, "a_borderWidth" }, |
|
|
@ -457,7 +450,7 @@ void RendererFont::drawSymbol(std::array<SymbolVertex, vertexPerQuad>& symbolQua |
|
|
|
m_vertexBufferPtr->quad.position = symbolQuad[i].quad.position; |
|
|
|
m_vertexBufferPtr->quad.position = symbolQuad[i].quad.position; |
|
|
|
m_vertexBufferPtr->quad.color = symbolQuad[i].quad.color; |
|
|
|
m_vertexBufferPtr->quad.color = symbolQuad[i].quad.color; |
|
|
|
m_vertexBufferPtr->quad.textureCoordinates = symbolQuad[i].quad.textureCoordinates; |
|
|
|
m_vertexBufferPtr->quad.textureCoordinates = symbolQuad[i].quad.textureCoordinates; |
|
|
|
m_vertexBufferPtr->quad.textureIndex = textureUnitIndex; |
|
|
|
m_vertexBufferPtr->quad.textureIndex = static_cast<float>(textureUnitIndex); |
|
|
|
|
|
|
|
|
|
|
|
m_vertexBufferPtr->width = symbolQuad[i].width; |
|
|
|
m_vertexBufferPtr->width = symbolQuad[i].width; |
|
|
|
m_vertexBufferPtr->edge = symbolQuad[i].edge; |
|
|
|
m_vertexBufferPtr->edge = symbolQuad[i].edge; |
|
|
@ -495,28 +488,26 @@ Renderer3D::Renderer3D(s) |
|
|
|
// GPU
|
|
|
|
// GPU
|
|
|
|
|
|
|
|
|
|
|
|
m_enableDepthBuffer = true; |
|
|
|
m_enableDepthBuffer = true; |
|
|
|
m_colorAttachmentCount = 3; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Create vertex buffer
|
|
|
|
// Create vertex buffer
|
|
|
|
auto vertexBuffer = std::make_shared<VertexBuffer>(sizeof(Vertex) * maxVertices); |
|
|
|
auto vertexBuffer = std::make_shared<VertexBuffer>(sizeof(Vertex) * maxVertices); |
|
|
|
vertexBuffer->setLayout({ |
|
|
|
vertexBuffer->setLayout({ |
|
|
|
{ BufferElementType::Vec3, "a_position" }, |
|
|
|
{ BufferElementType::Vec3, "a_position" }, |
|
|
|
{ BufferElementType::Vec3, "a_normal" }, |
|
|
|
{ BufferElementType::Vec3, "a_normal" }, |
|
|
|
{ BufferElementType::Vec4, "a_color" }, |
|
|
|
|
|
|
|
{ BufferElementType::Vec2, "a_textureCoordinates" }, |
|
|
|
{ BufferElementType::Vec2, "a_textureCoordinates" }, |
|
|
|
{ BufferElementType::Uint, "a_textureIndex" }, |
|
|
|
{ BufferElementType::Float, "a_textureIndex" }, |
|
|
|
}); |
|
|
|
}); |
|
|
|
m_vertexArray->addVertexBuffer(vertexBuffer); |
|
|
|
m_vertexArray->addVertexBuffer(vertexBuffer); |
|
|
|
|
|
|
|
|
|
|
|
ruc::info("Renderer3D initialized"); |
|
|
|
ruc::info("Renderer3D initialized"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Renderer3D::drawModel(std::span<const Vertex> vertices, std::span<const uint32_t> elements, const TransformComponent& transform, glm::vec4 color, std::shared_ptr<Texture> texture) |
|
|
|
void Renderer3D::drawModel(std::span<const Vertex> vertices, std::span<const uint32_t> elements, const TransformComponent& transform, std::shared_ptr<Texture> texture) |
|
|
|
{ |
|
|
|
{ |
|
|
|
// ruc::error("drawModel");
|
|
|
|
// ruc::error("drawModel");
|
|
|
|
|
|
|
|
|
|
|
|
VERIFY(vertices.size() <= maxVertices, "model vertices too big for buffer, {}/{}", vertices.size(), maxVertices); |
|
|
|
VERIFY(vertices.size() <= maxVertices, "model vertices too big for buffer"); |
|
|
|
VERIFY(elements.size() <= maxElements, "model elements too big for buffer, {}/{}", elements.size(), maxElements); |
|
|
|
VERIFY(elements.size() <= maxElements, "model elements too big for buffer"); |
|
|
|
|
|
|
|
|
|
|
|
// Create a new batch if the quad limit has been reached
|
|
|
|
// Create a new batch if the quad limit has been reached
|
|
|
|
if (m_vertexIndex + vertices.size() > maxVertices || m_elementIndex + elements.size() > maxElements) { |
|
|
|
if (m_vertexIndex + vertices.size() > maxVertices || m_elementIndex + elements.size() > maxElements) { |
|
|
@ -526,13 +517,11 @@ void Renderer3D::drawModel(std::span<const Vertex> vertices, std::span<const uin |
|
|
|
uint32_t textureUnitIndex = addTextureUnit(texture); |
|
|
|
uint32_t textureUnitIndex = addTextureUnit(texture); |
|
|
|
|
|
|
|
|
|
|
|
// Add the vertices
|
|
|
|
// Add the vertices
|
|
|
|
glm::mat3 normalMatrix = glm::mat3(glm::transpose(glm::inverse(transform.transform))); |
|
|
|
|
|
|
|
for (const auto& vertex : vertices) { |
|
|
|
for (const auto& vertex : vertices) { |
|
|
|
m_vertexBufferPtr->position = transform.transform * glm::vec4(vertex.position, 1.0f); |
|
|
|
m_vertexBufferPtr->position = transform.transform * glm::vec4(vertex.position, 1.0f); |
|
|
|
m_vertexBufferPtr->normal = normalMatrix * vertex.normal; // take non-uniform scaling into consideration
|
|
|
|
m_vertexBufferPtr->normal = vertex.normal; |
|
|
|
m_vertexBufferPtr->color = color; |
|
|
|
|
|
|
|
m_vertexBufferPtr->textureCoordinates = vertex.textureCoordinates; |
|
|
|
m_vertexBufferPtr->textureCoordinates = vertex.textureCoordinates; |
|
|
|
m_vertexBufferPtr->textureIndex = textureUnitIndex; |
|
|
|
m_vertexBufferPtr->textureIndex = static_cast<float>(textureUnitIndex); |
|
|
|
m_vertexBufferPtr++; |
|
|
|
m_vertexBufferPtr++; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -547,6 +536,13 @@ void Renderer3D::drawModel(std::span<const Vertex> vertices, std::span<const uin |
|
|
|
m_elementIndex += elements.size(); |
|
|
|
m_elementIndex += elements.size(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Renderer3D::beginScene(glm::mat4 cameraProjection, glm::mat4 cameraView) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
m_shader->bind(); |
|
|
|
|
|
|
|
m_shader->setFloat("u_projectionView", cameraProjection * cameraView); |
|
|
|
|
|
|
|
m_shader->unbind(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Renderer3D::createElementBuffer() |
|
|
|
void Renderer3D::createElementBuffer() |
|
|
|
{ |
|
|
|
{ |
|
|
|
// ---------------------------------
|
|
|
|
// ---------------------------------
|
|
|
@ -566,7 +562,7 @@ void Renderer3D::createElementBuffer() |
|
|
|
|
|
|
|
|
|
|
|
void Renderer3D::uploadElementBuffer() |
|
|
|
void Renderer3D::uploadElementBuffer() |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_vertexArray->indexBuffer()->uploadData(m_elementBufferBase, m_elementIndex * sizeof(uint32_t)); |
|
|
|
m_vertexArray->getIndexBuffer()->uploadData(m_elementBufferBase, m_elementIndex * sizeof(uint32_t)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Renderer3D::loadShader() |
|
|
|
void Renderer3D::loadShader() |
|
|
@ -580,46 +576,4 @@ void Renderer3D::startBatch() |
|
|
|
m_elementBufferPtr = m_elementBufferBase; |
|
|
|
m_elementBufferPtr = m_elementBufferBase; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void RendererPostProcess::drawQuad(const TransformComponent& transform, std::shared_ptr<Texture> albedo, std::shared_ptr<Texture> position, std::shared_ptr<Texture> normal) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
nextBatch(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constexpr glm::vec2 textureCoordinates[] = { |
|
|
|
|
|
|
|
{ 0.0f, 0.0f }, |
|
|
|
|
|
|
|
{ 1.0f, 0.0f }, |
|
|
|
|
|
|
|
{ 1.0f, 1.0f }, |
|
|
|
|
|
|
|
{ 0.0f, 1.0f } |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t textureUnitIndex = addTextureUnit(albedo); |
|
|
|
|
|
|
|
addTextureUnit(position); |
|
|
|
|
|
|
|
addTextureUnit(normal); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Add the quads 4 vertices
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < vertexPerQuad; i++) { |
|
|
|
|
|
|
|
m_vertexBufferPtr->position = transform.transform * m_vertexPositions[i]; |
|
|
|
|
|
|
|
m_vertexBufferPtr->textureCoordinates = textureCoordinates[i]; |
|
|
|
|
|
|
|
m_vertexBufferPtr->textureIndex = textureUnitIndex; |
|
|
|
|
|
|
|
m_vertexBufferPtr++; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
m_vertexIndex += vertexPerQuad; |
|
|
|
|
|
|
|
m_elementIndex += elementPerQuad; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void RendererPostProcess::loadShader() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
ruc::error("POSTPROCESSING!"); |
|
|
|
|
|
|
|
m_shader = AssetManager::the().load<Shader>("assets/glsl/post-process"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void RendererLightCube::loadShader() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
m_shader = AssetManager::the().load<Shader>("assets/glsl/lightsource"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace Inferno
|
|
|
|
} // namespace Inferno
|
|
|
|