Use vertex colors, use normal maps

This commit is contained in:
Guillaume Meunier
2025-12-15 19:00:23 +01:00
parent a32a4e4182
commit 06ad530e2f
6 changed files with 86 additions and 15 deletions
+74 -6
View File
@@ -18,12 +18,10 @@
#include "scene_loader.h"
#include "application.h"
#include "gpu_buffer.h"
#include "image_loader.h"
#include "render/scene_components.h"
#include "render/vertex_layout.h"
#include "utils/contains.h"
#include "utils/files.h"
#include "utils/json_string.h"
#include "utils/mapped_file.h"
@@ -273,19 +271,45 @@ void write_vertex_attribute_aux(std::byte * output, size_t stride, int component
{
case 1:
for (size_t i = 0, n = input.size(); i < n; i++, output += stride)
*reinterpret_cast<glm::vec<1, T> *>(output) = glm::vec<1, T>(input[i].x);
*reinterpret_cast<glm::vec<1, T> *>(output) = input[i];
break;
case 2:
for (size_t i = 0, n = input.size(); i < n; i++, output += stride)
*reinterpret_cast<glm::vec<2, T> *>(output) = glm::vec<2, T>(input[i].x, input[i].y);
*reinterpret_cast<glm::vec<2, T> *>(output) = input[i];
break;
case 3:
for (size_t i = 0, n = input.size(); i < n; i++, output += stride)
*reinterpret_cast<glm::vec<3, T> *>(output) = glm::vec<3, T>(input[i].x, input[i].y, input[i].z);
*reinterpret_cast<glm::vec<3, T> *>(output) = input[i];
break;
case 4:
for (size_t i = 0, n = input.size(); i < n; i++, output += stride)
*reinterpret_cast<glm::vec<3, T> *>(output) = glm::vec<4, T>(input[i].x, input[i].y, input[i].z, input[i].w);
*reinterpret_cast<glm::vec<4, T> *>(output) = input[i];
break;
default:
abort();
}
}
template <typename T>
void write_vertex_attribute_aux(std::byte * output, size_t stride, int components, glm::vec4 value, size_t size)
{
switch (components)
{
case 1:
for (size_t i = 0, n = size; i < n; i++, output += stride)
*reinterpret_cast<glm::vec<1, T> *>(output) = value;
break;
case 2:
for (size_t i = 0, n = size; i < n; i++, output += stride)
*reinterpret_cast<glm::vec<2, T> *>(output) = value;
break;
case 3:
for (size_t i = 0, n = size; i < n; i++, output += stride)
*reinterpret_cast<glm::vec<3, T> *>(output) = value;
break;
case 4:
for (size_t i = 0, n = size; i < n; i++, output += stride)
*reinterpret_cast<glm::vec<4, T> *>(output) = value;
break;
default:
abort();
@@ -310,6 +334,24 @@ void write_vertex_attribute(std::vector<std::byte> & output, size_t offset, size
}
}
void write_vertex_attribute(std::vector<std::byte> & output, size_t offset, size_t stride, vk::Format format, glm::vec4 value, size_t size)
{
assert(output.size() >= size * stride);
switch (format)
{
case vk::Format::eR32Sfloat:
case vk::Format::eR32G32Sfloat:
case vk::Format::eR32G32B32Sfloat:
case vk::Format::eR32G32B32A32Sfloat:
write_vertex_attribute_aux<float>(output.data() + offset, stride, vk::componentCount(format), value, size);
return;
default:
return;
}
}
void copy_vertex_attributes(
const fastgltf::Asset & asset,
const fastgltf::Accessor & accessor,
@@ -322,6 +364,18 @@ void copy_vertex_attributes(
write_vertex_attribute(buffer, attribute.offset, binding.stride, attribute.format, read_vertex_attribute(asset, accessor));
}
void fill_vertex_attributes(
glm::vec4 value,
size_t size,
std::vector<std::byte> & buffer,
const vk::VertexInputBindingDescription & binding,
const vk::VertexInputAttributeDescription & attribute)
{
assert(buffer.size() >= size * binding.stride);
write_vertex_attribute(buffer, attribute.offset, binding.stride, attribute.format, value, size);
}
std::pair<size_t, std::vector<std::vector<std::byte>>> create_vertex_buffers(
const renderer::vertex_layout & layout,
const fastgltf::Asset & asset,
@@ -370,7 +424,20 @@ std::pair<size_t, std::vector<std::vector<std::byte>>> create_vertex_buffers(
gltf_attribute = primitive.findAttribute(name + "_0");
if (gltf_attribute == primitive.attributes.cend())
{
// If the attribute is not specified in the glTF, use the default value or 0 if none is found
static const std::unordered_map<std::string_view, glm::vec4> default_values = {
std::pair{"COLOR", glm::vec4{1, 1, 1, 1}}};
if (auto iter = default_values.find(name); iter != default_values.end())
fill_vertex_attributes(iter->second, vertex_count, buffer, binding, attribute);
else
{
// The buffer is already initialized at 0
}
continue;
}
}
const fastgltf::Accessor & accessor = asset.accessors.at(gltf_attribute->accessorIndex);
@@ -842,6 +909,7 @@ public:
int binding;
// Attributes that affect the vertices position go in a different buffer
if (semantic == "POSITION" or
semantic == "JOINTS" or
semantic == "WEIGHTS")
+2 -1
View File
@@ -5,7 +5,7 @@ const int nb_clipping = 4;
const int nb_texcoords = 2;
const bool use_pbr = false;
const bool use_normal_maps = false;
const bool use_normal_maps = true;
layout(set = 0, binding = 0) uniform scene_ubo
{
@@ -136,3 +136,4 @@ layout(location = 5) VS_TO_FS vec2 texcoord_metallic_roughness;
layout(location = 6) VS_TO_FS vec2 texcoord_occlusion;
layout(location = 7) VS_TO_FS vec2 texcoord_emissive;
layout(location = 8) VS_TO_FS vec2 texcoord_normal;
layout(location = 9) VS_TO_FS vec4 vertex_color;
+3 -3
View File
@@ -30,7 +30,7 @@ layout(early_fragment_tests) in;
void main()
{
vec4 albedo = material.base_color_factor * texture(base_color_texture, texcoord_base_color);
vec4 albedo = vertex_color * material.base_color_factor * texture(base_color_texture, texcoord_base_color);
vec3 emissive_color = (material.base_emissive_factor * texture(emissive_texture, texcoord_emissive)).rgb;
vec3 normal_unit;
@@ -41,10 +41,10 @@ void main()
// glTF spec §3.7.2.1
vec3 bitangent = cross(normal, tangent.xyz) * tangent.w;
mat3 TBN = mat3(tangent.xyz, bitangent, normal); // View space to tangent space?
mat3 TBN = mat3(tangent.xyz, bitangent, normal);
// Normal in view space
normal_unit = TBN * sampled_normal;
normal_unit = normalize(TBN * sampled_normal);
}
else
normal_unit = normalize(normal);
+1
View File
@@ -58,6 +58,7 @@ void main()
frag_pos = mesh.modelview[gl_ViewIndex] * vec4(in_position, 1.0);
light_pos = scene.view[gl_ViewIndex] * scene.light_position;
vertex_color = in_color;
for (int i = 0; i < nb_clipping; i++)
{
+5 -5
View File
@@ -30,7 +30,7 @@ layout(location = 0) out vec4 out_color;
void main()
{
vec4 albedo = material.base_color_factor * texture(base_color_texture, texcoord_base_color);
vec4 albedo = vertex_color * material.base_color_factor * texture(base_color_texture, texcoord_base_color);
vec3 emissive_color = (material.base_emissive_factor * texture(emissive_texture, texcoord_emissive)).rgb;
vec3 normal_unit;
@@ -41,10 +41,10 @@ void main()
// glTF spec §3.7.2.1
vec3 bitangent = cross(normal, tangent.xyz) * tangent.w;
mat3 TBN = mat3(tangent.xyz, bitangent, normal); // View space to tangent space?
mat3 TBN = mat3(tangent.xyz, bitangent, normal);
// Normal in view space
normal_unit = TBN * sampled_normal;
normal_unit = normalize(TBN * sampled_normal);
}
else
normal_unit = normalize(normal);
@@ -65,8 +65,8 @@ void main()
vec3 light_colour = scene.light_color.rgb;
float attenuation = 1;
vec3 radiance =
per_light_radiance(albedo.rgb, metallic, roughness, light_colour, light_dir, normal_unit, view_dir, half_vec, attenuation) +
vec3(scene.ambient_color) * albedo.rgb * ao;
per_light_radiance(albedo.rgb, metallic, roughness, light_colour, light_dir, normal_unit, view_dir, half_vec, attenuation) +
vec3(scene.ambient_color) * albedo.rgb * ao;
vec3 tmp = radiance + emissive_color;
+1
View File
@@ -67,6 +67,7 @@ void main()
frag_pos = mesh.modelview[gl_ViewIndex] * vec4(in_position, 1.0);
light_pos = scene.view[gl_ViewIndex] * scene.light_position;
vertex_color = in_color;
for(int i = 0; i < nb_clipping; i++)
{