Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelarius committed Apr 5, 2024
1 parent d6351d0 commit 923d11a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 19 deletions.
65 changes: 46 additions & 19 deletions src/common/gltf_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
#include <stb_image.h>

#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <iterator>
#include <stdexcept>
#include <string_view>
#include <tuple>

namespace fs = std::filesystem;
Expand Down Expand Up @@ -120,8 +122,9 @@ GltfModel::GltfModel(const fs::path gltfPath)
std::vector<std::vector<glm::vec3>> meshNormals;
std::vector<std::vector<glm::vec2>> meshTexCoords;
std::vector<std::vector<std::uint32_t>> meshIndices;
std::vector<const cgltf_image*> meshBaseColorImageAttributes;
VectorSet<const cgltf_image*> uniqueBaseColorImages;
std::vector<Texture> baseColorTextures;
std::vector<std::size_t> baseColorTextureIds;
std::vector<std::size_t> meshBaseColorTextureIndices;

const std::size_t meshCount = data->meshes_count;
for (std::size_t meshIdx = 0; meshIdx < meshCount; ++meshIdx)
Expand All @@ -140,23 +143,47 @@ GltfModel::GltfModel(const fs::path gltfPath)
primitive.material->pbr_metallic_roughness;

NLRS_ASSERT(pbrMetallicRoughness.base_color_texture.texcoord == 0);
NLRS_ASSERT(pbrMetallicRoughness.base_color_texture.scale == 1.f);
NLRS_ASSERT(pbrMetallicRoughness.base_color_texture.texture);

const cgltf_texture& baseColorTexture =
*pbrMetallicRoughness.base_color_texture.texture;
// Look up OpenGL texture wrap modes:
// https://registry.khronos.org/OpenGL/api/GL/glcorearb.h
// GL_REPEAT: 10497
// GL_MIRRORED_REPEAT: 33648
// GL_CLAMP_TO_EDGE: 33071
// GL_CLAMP_TO_BORDER: 33069
NLRS_ASSERT(baseColorTexture.sampler->wrap_s == 10497);
NLRS_ASSERT(baseColorTexture.sampler->wrap_t == 10497);
NLRS_ASSERT(baseColorTexture.image);
const cgltf_image* const baseColorImage = baseColorTexture.image;
meshBaseColorImageAttributes.push_back(baseColorImage);
uniqueBaseColorImages.insert(baseColorImage);
NLRS_ASSERT(pbrMetallicRoughness.base_color_texture.has_transform == false);
// NLRS_ASSERT(pbrMetallicRoughness.base_color_texture.texture);

if (pbrMetallicRoughness.base_color_texture.texture != nullptr)
{
const cgltf_texture& baseColorTexture =
*pbrMetallicRoughness.base_color_texture.texture;
// Look up OpenGL texture wrap modes:
// https://registry.khronos.org/OpenGL/api/GL/glcorearb.h
// GL_REPEAT: 10497
// GL_MIRRORED_REPEAT: 33648
// GL_CLAMP_TO_EDGE: 33071
// GL_CLAMP_TO_BORDER: 33069
NLRS_ASSERT(baseColorTexture.sampler->wrap_s == 10497);
NLRS_ASSERT(baseColorTexture.sampler->wrap_t == 10497);
NLRS_ASSERT(baseColorTexture.image);

// TODO: which name is guaranteed to be defined? Does baseColorTexture.name
// always have a value? baseColorTexture.image.uri probably only exists if the
// image buffer is in an external file which not the case for a glb file.
NLRS_ASSERT(baseColorTexture.name != nullptr);
const std::string_view name(baseColorTexture.name);
const std::size_t textureId = std::hash<std::string_view>{}(name);
const auto id = std::find(
baseColorTextureIds.begin(), baseColorTextureIds.end(), textureId);
if (id == baseColorTextureIds.end())
{
baseColorTextureIds.push_back(textureId);
// TODO do all the work here
// load the image into a texture or the pixel value
}

const cgltf_image* const baseColorImage = baseColorTexture.image;
// meshBaseColorImageAttributes.push_back(baseColorImage);
// uniqueBaseColorImages.insert(baseColorImage);
}
else
{
// TODO: hash teh base color value
// try to see if it already exists in the baseColorTextures
}
}

// Indices
Expand Down
10 changes: 10 additions & 0 deletions src/common/texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,14 @@ Texture Texture::fromMemory(std::span<const std::uint8_t> data)
std::move(pixels),
Dimensions{static_cast<std::uint32_t>(width), static_cast<std::uint32_t>(height)});
}

Texture Texture::fromPixel(float r, float g, float b, float a)
{
const std::uint32_t r = static_cast<std::uint32_t>(r * 255.0f);
const std::uint32_t g = static_cast<std::uint32_t>(g * 255.0f);
const std::uint32_t b = static_cast<std::uint32_t>(b * 255.0f);
const std::uint32_t a = static_cast<std::uint32_t>(a * 255.0f);

return Texture(std::vector<BgraPixel>{b | (g << 8) | (r << 16) | (a << 24)}, Dimensions{1, 1});
}
} // namespace nlrs
1 change: 1 addition & 0 deletions src/common/texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Texture

// `data` is expected to be in RGBA or RGB format, with each component 8 bits.
static Texture fromMemory(std::span<const std::uint8_t> data);
static Texture fromPixel(float r, float g, float b, float a);

private:
std::vector<BgraPixel> mPixels;
Expand Down

0 comments on commit 923d11a

Please sign in to comment.