Browse Source

Asset+Component+Render: Render quads in the world as 3D objects

master
Riyyi 4 months ago
parent
commit
dcf2a85208
  1. 0
      assets/glsl/batch-2d.frag
  2. 9
      assets/glsl/batch-2d.vert
  3. 3
      assets/glsl/batch-3d.frag
  4. 7
      assets/glsl/batch-3d.vert
  5. BIN
      assets/model/quad.blend
  6. 15
      assets/model/quad.obj
  7. 15
      assets/scene/scene1.json
  8. 4
      src/inferno/component/model-component.cpp
  9. 1
      src/inferno/component/model-component.h
  10. 7
      src/inferno/render/renderer.cpp
  11. 27
      src/inferno/render/renderer.h
  12. 1
      src/inferno/system/rendersystem.cpp

0
assets/glsl/batch-quad.frag → assets/glsl/batch-2d.frag

9
assets/glsl/batch-quad.vert → 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);
}

3
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;

7
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

BIN
assets/model/quad.blend

Binary file not shown.

15
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

15
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"
}
}

4
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<Model>(json.at("model").asString());
}

1
src/inferno/component/model-component.h

@ -16,6 +16,7 @@
namespace Inferno {
struct ModelComponent {
glm::vec4 color { 1.0f };
std::shared_ptr<Model> model;
std::shared_ptr<Texture2D> texture;
};

7
src/inferno/render/renderer.cpp

@ -8,6 +8,7 @@
#include <span>
#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<Shader>("assets/glsl/batch-quad");
m_shader = AssetManager::the().load<Shader>("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<const Vertex> vertices, std::span<const uint32_t> elements, const TransformComponent& transform, std::shared_ptr<Texture> texture)
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)
{
// ruc::error("drawModel");
@ -513,6 +515,7 @@ void Renderer3D::drawModel(std::span<const Vertex> vertices, std::span<const uin
for (const auto& vertex : vertices) {
m_vertexBufferPtr->position = 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++;

27
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<Renderer3D>::destroy;
void drawModel(std::span<const Vertex> vertices, std::span<const uint32_t> indices, const TransformComponent& transform, std::shared_ptr<Texture> texture);
void drawModel(std::span<const Vertex> vertices, std::span<const uint32_t> indices, const TransformComponent& transform, glm::vec4 color, std::shared_ptr<Texture> texture);
private:
void createElementBuffer() override;

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

Loading…
Cancel
Save