diff --git a/assets/glsl/batch-quad.frag b/assets/glsl/batch-2d.frag similarity index 100% rename from assets/glsl/batch-quad.frag rename to assets/glsl/batch-2d.frag diff --git a/assets/glsl/batch-quad.vert b/assets/glsl/batch-2d.vert similarity index 65% rename from assets/glsl/batch-quad.vert rename to assets/glsl/batch-2d.vert index 840a032..4a6ab7f 100644 --- a/assets/glsl/batch-quad.vert +++ b/assets/glsl/batch-2d.vert @@ -9,16 +9,11 @@ out vec4 v_color; out vec2 v_textureCoordinates; out flat uint v_textureIndex; -layout(std140, binding = 0) uniform Camera -{ - 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); + // Vclip = Model transform * Vlocal + gl_Position = vec4(a_position, 1.0f); } diff --git a/assets/glsl/batch-3d.frag b/assets/glsl/batch-3d.frag index a957283..1a3c725 100644 --- a/assets/glsl/batch-3d.frag +++ b/assets/glsl/batch-3d.frag @@ -3,6 +3,7 @@ layout(location = 0) out vec4 color; in vec3 v_normal; +in vec4 v_color; in vec2 v_textureCoordinates; in flat uint v_textureIndex; @@ -10,7 +11,7 @@ uniform sampler2D u_textures[32]; void main() { - vec4 textureColor = vec4(1.0f); + vec4 textureColor = v_color; switch(v_textureIndex) { case 0: break; // Texture unit 0 is reserved for no texture case 1: textureColor *= texture(u_textures[1], v_textureCoordinates); break; diff --git a/assets/glsl/batch-3d.vert b/assets/glsl/batch-3d.vert index 1af2246..d33fc16 100644 --- a/assets/glsl/batch-3d.vert +++ b/assets/glsl/batch-3d.vert @@ -2,10 +2,12 @@ layout(location = 0) in vec3 a_position; layout(location = 1) in vec3 a_normal; -layout(location = 2) in vec2 a_textureCoordinates; -layout(location = 3) in uint a_textureIndex; +layout(location = 2) in vec4 a_color; +layout(location = 3) in vec2 a_textureCoordinates; +layout(location = 4) in uint a_textureIndex; out vec3 v_normal; +out vec4 v_color; out vec2 v_textureCoordinates; out flat uint v_textureIndex; @@ -17,6 +19,7 @@ layout(std140, binding = 0) uniform Camera void main() { v_normal = a_normal; + v_color = a_color; v_textureCoordinates = a_textureCoordinates; v_textureIndex = a_textureIndex; // Vclip = Camera projection * Camera view * Model transform * Vlocal diff --git a/assets/model/quad.blend b/assets/model/quad.blend new file mode 100644 index 0000000..6b4e034 Binary files /dev/null and b/assets/model/quad.blend differ diff --git a/assets/model/quad.obj b/assets/model/quad.obj new file mode 100644 index 0000000..f385366 --- /dev/null +++ b/assets/model/quad.obj @@ -0,0 +1,15 @@ +# Blender 4.2.0 +# www.blender.org +usemtl (null) +o Plane +v -1.000000 -1.000000 -0.000000 +v 1.000000 -1.000000 -0.000000 +v -1.000000 1.000000 0.000000 +v 1.000000 1.000000 0.000000 +vn -0.0000 -0.0000 1.0000 +vt 0.000000 0.000000 +vt 1.000000 0.000000 +vt 1.000000 1.000000 +vt 0.000000 1.000000 +s 0 +f 1/1/1 2/2/1 4/3/1 3/4/1 diff --git a/assets/scene/scene1.json b/assets/scene/scene1.json index e7016b2..94ce56d 100644 --- a/assets/scene/scene1.json +++ b/assets/scene/scene1.json @@ -35,8 +35,9 @@ "rotate": [ 0.0, 0.0, 0.0 ], "scale": [ 1.0, 1.0, 1.0 ] }, - "sprite": { + "model": { "color": [ 1.0, 1.0, 1.0, 1.0 ], + "model": "assets/model/quad.obj", "texture": "assets/gfx/test.png" } }, @@ -48,8 +49,9 @@ "rotate": [ 0.0, 0.0, 0.0 ], "scale": [ 1.0, 1.0, 1.0 ] }, - "sprite": { + "model": { "color": [ 0.5, 0.6, 0.8, 1.0 ], + "model": "assets/model/quad.obj", "texture": "assets/gfx/test.png" } }, @@ -61,8 +63,9 @@ "rotate": [ 0.0, 0.0, -20.0 ], "scale": [ 1.0, 1.0, 1.0 ] }, - "sprite": { + "model": { "color": [ 1.0, 1.0, 1.0, 1.0 ], + "model": "assets/model/quad.obj", "texture": "assets/gfx/test-inverted.png" }, "children": [ @@ -74,8 +77,9 @@ "rotate": [ 0.0, 0.0, 0.0 ], "scale": [ 0.5, 0.5, 1.0 ] }, - "sprite": { + "model": { "color": [ 1.0, 1.0, 1.0, 1.0 ], + "model": "assets/model/quad.obj", "texture": "assets/gfx/test-inverted.png" }, "children": [ @@ -87,8 +91,9 @@ "rotate": [ 0.0, 0.0, -20.0 ], "scale": [ 0.5, 0.5, 1.0 ] }, - "sprite": { + "model": { "color": [ 1.0, 1.0, 1.0, 1.0 ], + "model": "assets/model/quad.obj", "texture": "assets/gfx/test-inverted.png" } } diff --git a/src/inferno/component/model-component.cpp b/src/inferno/component/model-component.cpp index 04d593d..bb6b44f 100644 --- a/src/inferno/component/model-component.cpp +++ b/src/inferno/component/model-component.cpp @@ -8,6 +8,7 @@ #include "inferno/asset/asset-manager.h" #include "inferno/asset/model.h" #include "inferno/asset/texture.h" +#include "inferno/component/spritecomponent.h" // TODO: Move glm::x toJson/fromJson to separate file namespace Inferno { @@ -15,6 +16,9 @@ void fromJson(const ruc::Json& json, ModelComponent& value) { VERIFY(json.type() == ruc::Json::Type::Object); + if (json.exists("color")) { + json.at("color").getTo(value.color); + } if (json.exists("model") && json.at("model").type() == ruc::Json::Type::String) { value.model = AssetManager::the().load(json.at("model").asString()); } diff --git a/src/inferno/component/model-component.h b/src/inferno/component/model-component.h index b7f1605..afbda33 100644 --- a/src/inferno/component/model-component.h +++ b/src/inferno/component/model-component.h @@ -16,6 +16,7 @@ namespace Inferno { struct ModelComponent { + glm::vec4 color { 1.0f }; std::shared_ptr model; std::shared_ptr texture; }; diff --git a/src/inferno/render/renderer.cpp b/src/inferno/render/renderer.cpp index 1625347..bac6ea0 100644 --- a/src/inferno/render/renderer.cpp +++ b/src/inferno/render/renderer.cpp @@ -8,6 +8,7 @@ #include #include "glad/glad.h" +#include "glm/ext/vector_float4.hpp" // glm::vec4 #include "ruc/format/log.h" #include "inferno/asset/asset-manager.h" @@ -274,7 +275,7 @@ void Renderer2D::drawQuad(const TransformComponent& transform, glm::mat4 color, void Renderer2D::loadShader() { - m_shader = AssetManager::the().load("assets/glsl/batch-quad"); + m_shader = AssetManager::the().load("assets/glsl/batch-2d"); } // ----------------------------------------- @@ -487,6 +488,7 @@ Renderer3D::Renderer3D(s) vertexBuffer->setLayout({ { BufferElementType::Vec3, "a_position" }, { BufferElementType::Vec3, "a_normal" }, + { BufferElementType::Vec4, "a_color" }, { BufferElementType::Vec2, "a_textureCoordinates" }, { BufferElementType::Uint, "a_textureIndex" }, }); @@ -495,7 +497,7 @@ Renderer3D::Renderer3D(s) ruc::info("Renderer3D initialized"); } -void Renderer3D::drawModel(std::span vertices, std::span elements, const TransformComponent& transform, std::shared_ptr texture) +void Renderer3D::drawModel(std::span vertices, std::span elements, const TransformComponent& transform, glm::vec4 color, std::shared_ptr texture) { // ruc::error("drawModel"); @@ -513,6 +515,7 @@ void Renderer3D::drawModel(std::span vertices, std::spanposition = transform.transform * glm::vec4(vertex.position, 1.0f); m_vertexBufferPtr->normal = vertex.normal; + m_vertexBufferPtr->color = color; m_vertexBufferPtr->textureCoordinates = vertex.textureCoordinates; m_vertexBufferPtr->textureIndex = textureUnitIndex; m_vertexBufferPtr++; diff --git a/src/inferno/render/renderer.h b/src/inferno/render/renderer.h index 022741f..a2dd0be 100644 --- a/src/inferno/render/renderer.h +++ b/src/inferno/render/renderer.h @@ -24,16 +24,16 @@ class TransformComponent; class VertexArray; struct QuadVertex { - glm::vec3 position { 0.0f, 0.0f, 0.0f }; - glm::vec4 color { 1.0f, 1.0f, 1.0f, 1.0f }; - glm::vec2 textureCoordinates { 0.0f, 0.0f }; - uint32_t textureIndex = 0; + glm::vec3 position { 0.0f }; + glm::vec4 color { 1.0f }; + glm::vec2 textureCoordinates { 0.0f }; + uint32_t textureIndex { 0 }; }; struct CubemapVertex { - glm::vec3 position { 0.0f, 0.0f, 0.0f }; - glm::vec4 color { 1.0f, 1.0f, 1.0f, 1.0f }; - uint32_t textureIndex = 0; + glm::vec3 position { 0.0f }; + glm::vec4 color { 1.0f }; + uint32_t textureIndex { 0 }; }; struct SymbolVertex { @@ -45,16 +45,17 @@ struct SymbolVertex { // Outline float borderWidth = 0.7f; float borderEdge = 0.1f; - glm::vec4 borderColor { 1.0f, 1.0f, 1.0f, 1.0f }; + glm::vec4 borderColor { 1.0f }; // Dropshadow float offset = 0.0f; }; struct Vertex { - glm::vec3 position { 0.0f, 0.0f, 0.0f }; - glm::vec3 normal { 1.0f, 1.0f, 1.0f }; - glm::vec2 textureCoordinates { 0.0f, 0.0f }; - uint32_t textureIndex = 0; + glm::vec3 position { 0.0f }; + glm::vec3 normal { 1.0f }; + glm::vec4 color { 1.0f }; + glm::vec2 textureCoordinates { 0.0f }; + uint32_t textureIndex { 0 }; }; // ------------------------------------- @@ -197,7 +198,7 @@ public: using Singleton::destroy; - void drawModel(std::span vertices, std::span indices, const TransformComponent& transform, std::shared_ptr texture); + void drawModel(std::span vertices, std::span indices, const TransformComponent& transform, glm::vec4 color, std::shared_ptr texture); private: void createElementBuffer() override; diff --git a/src/inferno/system/rendersystem.cpp b/src/inferno/system/rendersystem.cpp index 53986dc..1cf7529 100644 --- a/src/inferno/system/rendersystem.cpp +++ b/src/inferno/system/rendersystem.cpp @@ -46,6 +46,7 @@ void RenderSystem::render() Renderer3D::the().drawModel(model.model->vertices(), model.model->elements(), transform, + model.color, model.model->texture() ? model.model->texture() : model.texture); } }