summary refs log tree commit diff
path: root/src/Math/Grid.hpp
AgeCommit message (Collapse)Author
2023-07-08Use own size typesMel
2023-07-07Fix minor quality issuesMel
2023-06-12Multithreaded world generation with PerlinMel
a id='n49' href='#n49'>49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
#pragma once

#include <utility>
#include <vector>
#include "../Common/Sizes.hpp"
#include "../Math/Common.hpp"

namespace MC::GFX {

class Mesh {
public:
    struct Attribute {
        template<typename T = F32>
        Attribute(
            std::vector<T> v
        ) : data_size(v.size()),
            attribute_size(1),
            type_size(sizeof(T)) {
            data = copy(v.data(), v.size() * type_size);
        }

        template<uint S = 3, typename T = F32>
        Attribute(
            std::vector<Vector<S, T>> v
        ) : data_size(v.size()),
            attribute_size(S),
            type_size(sizeof(T)) {
            data = copy(v.data(), v.size() * S * type_size);
        }

        Attribute(
            const Attribute& other
        ) : data(copy(other.data, other.data_size * other.attribute_size * other.type_size)),
            data_size(other.data_size),
            attribute_size(other.attribute_size),
            type_size(other.type_size) {}

        static void* copy(void* ptr, U32 size) {
            auto* buffer = new U8[size];
            std::copy_n((U8*)ptr, size, buffer);
            return buffer;
        }

        void* data;
        USize data_size;
        USize attribute_size;
        USize type_size;
    };

    Mesh(
        std::vector<Attribute> attributes,
        std::vector<U32> indices
    ) : m_attributes(std::move(attributes)),
        m_indices(std::move(indices)) {}

    explicit Mesh(
        std::vector<Attribute> attributes
    ) : m_attributes(std::move(attributes)) {}

    const std::vector<U32>& indices() const;
    const std::vector<Attribute>& attributes() const;

private:
    std::vector<Attribute> m_attributes;
    std::vector<U32> m_indices;
};

}