<commit_before>//https://code.google.com/p/nya-engine/

#include "mesh.h"
#include "scene.h"
#include "camera.h"
#include "shader.h"
#include "render/render.h"
#include "memory/tmp_buffer.h"
#include "memory/memory_reader.h"
#include "math/constants.h"

namespace
{
    struct mmd_material_params
    {
        float diffuse[4];
        float shininess;        
        float specular[3];
        float ambient[3];
    };

    std::string read_string(nya_memory::memory_reader &reader)
    {
        unsigned short size=reader.read<unsigned short>();
        if(!size || !reader.check_remained(size))
            return "";

        return std::string((const char *)reader.get_data(),size);
    }
}

namespace nya_scene
{

bool mesh::load_nms_mesh_section(shared_mesh &res,const void *data,size_t size)
{
    nya_memory::memory_reader reader(data,size);

    typedef unsigned int uint;
    typedef unsigned short ushort;
    typedef unsigned char uchar;

    const nya_math::vec3 aabb_min(reader.read<float>(),reader.read<float>(),reader.read<float>());
    const nya_math::vec3 aabb_max(reader.read<float>(),reader.read<float>(),reader.read<float>());
    res.aabb.delta=(aabb_max-aabb_min)*0.5f;
    res.aabb.origin=aabb_min+res.aabb.delta;

    uint vertex_stride=0;
    const uchar el_count=reader.read<uchar>();
    for(uchar i=0;i<el_count;++i)
    {
        enum el_type
        {
            pos,
            normal,
            color,
            tc0=100
        };

        const uchar type=reader.read<uchar>();
        const uchar dimension=reader.read<uchar>();
        read_string(reader); //semantics

        switch(type)
        {
            case pos: res.vbo.set_vertices(vertex_stride,dimension); break;
            case normal: res.vbo.set_normals(vertex_stride); break;
            case color: res.vbo.set_colors(vertex_stride,dimension); break;
            default: res.vbo.set_tc(type-tc0,vertex_stride,dimension); break;
        };

        vertex_stride+=dimension*sizeof(float);
    }

    if(!vertex_stride)
        return false;

    const uint verts_count=reader.read<uint>();
    if(!reader.check_remained(verts_count*vertex_stride))
        return false;

    res.vbo.set_vertex_data(reader.get_data(),vertex_stride,verts_count);

    const uchar index_size=reader.read<uchar>();
    if(index_size)
    {
        uint inds_count=reader.read<uint>();
        if(!reader.check_remained(inds_count*sizeof(ushort)))
            return false;

        switch(index_size)
        {
            case 2: res.vbo.set_index_data(reader.get_data(),nya_render::vbo::index2b,verts_count); break;
            case 4: res.vbo.set_index_data(reader.get_data(),nya_render::vbo::index4b,verts_count); break;
            default: return false;
        }
    }

    const ushort lods_count=reader.read<ushort>();
    for(ushort i=0;i<lods_count;++i)
    {
        const ushort groups_count=reader.read<ushort>();
        res.groups.resize(groups_count);
        for(ushort j=0;j<groups_count;++j)
        {
            shared_mesh::group &g=res.groups[j];
            read_string(reader); //ToDo: group name

            const nya_math::vec3 aabb_min(reader.read<float>(),reader.read<float>(),reader.read<float>()); //ToDo
            const nya_math::vec3 aabb_max(reader.read<float>(),reader.read<float>(),reader.read<float>());

            g.material_idx=reader.read<ushort>();
            g.offset=reader.read<uint>();
            g.count=reader.read<uint>();
        }

        break; //ToDo: load all lods
    }<commit_msg>nms fix<commit_after>//https://code.google.com/p/nya-engine/

#include "mesh.h"
#include "scene.h"
#include "camera.h"
#include "shader.h"
#include "render/render.h"
#include "memory/tmp_buffer.h"
#include "memory/memory_reader.h"
#include "math/constants.h"

namespace
{
    struct mmd_material_params
    {
        float diffuse[4];
        float shininess;        
        float specular[3];
        float ambient[3];
    };

    std::string read_string(nya_memory::memory_reader &reader)
    {
        unsigned short size=reader.read<unsigned short>();
        const char *str=(const char *)reader.get_data();
        reader.skip(size);
        if(!size || !str || !reader.check_remained(size))
            return "";

        return std::string(str,size);
    }
}

namespace nya_scene
{

bool mesh::load_nms_mesh_section(shared_mesh &res,const void *data,size_t size)
{
    nya_memory::memory_reader reader(data,size);

    typedef unsigned int uint;
    typedef unsigned short ushort;
    typedef unsigned char uchar;

    const nya_math::vec3 aabb_min(reader.read<float>(),reader.read<float>(),reader.read<float>());
    const nya_math::vec3 aabb_max(reader.read<float>(),reader.read<float>(),reader.read<float>());
    res.aabb.delta=(aabb_max-aabb_min)*0.5f;
    res.aabb.origin=aabb_min+res.aabb.delta;

    uint vertex_stride=0;
    const uchar el_count=reader.read<uchar>();
    for(uchar i=0;i<el_count;++i)
    {
        enum el_type
        {
            pos,
            normal,
            color,
            tc0=100
        };

        const uchar type=reader.read<uchar>();
        const uchar dimension=reader.read<uchar>();
        read_string(reader); //semantics

        switch(type)
        {
            case pos: res.vbo.set_vertices(vertex_stride,dimension); break;
            case normal: res.vbo.set_normals(vertex_stride); break;
            case color: res.vbo.set_colors(vertex_stride,dimension); break;
            default: res.vbo.set_tc(type-tc0,vertex_stride,dimension); break;
        };

        vertex_stride+=dimension*sizeof(float);
    }

    if(!vertex_stride)
        return false;

    const uint verts_count=reader.read<uint>();
    if(!reader.check_remained(verts_count*vertex_stride))
        return false;

    res.vbo.set_vertex_data(reader.get_data(),vertex_stride,verts_count);
    reader.skip(verts_count*vertex_stride);

    const uchar index_size=reader.read<uchar>();
    if(index_size)
    {
        uint inds_count=reader.read<uint>();
        if(!reader.check_remained(inds_count*sizeof(ushort)))
            return false;

        switch(index_size)
        {
            case 2: res.vbo.set_index_data(reader.get_data(),nya_render::vbo::index2b,verts_count); break;
            case 4: res.vbo.set_index_data(reader.get_data(),nya_render::vbo::index4b,verts_count); break;
            default: return false;
        }

        reader.skip(index_size*inds_count);
    }

    const ushort lods_count=reader.read<ushort>();
    for(ushort i=0;i<lods_count;++i)
    {
        const ushort groups_count=reader.read<ushort>();
        res.groups.resize(groups_count);
        for(ushort j=0;j<groups_count;++j)
        {
            shared_mesh::group &g=res.groups[j];
            read_string(reader); //ToDo: group name

            const nya_math::vec3 aabb_min(reader.read<float>(),reader.read<float>(),reader.read<float>()); //ToDo
            const nya_math::vec3 aabb_max(reader.read<float>(),reader.read<float>(),reader.read<float>());

            g.material_idx=reader.read<ushort>();
            g.offset=reader.read<uint>();
            g.count=reader.read<uint>();
        }

        break; //ToDo: load all lods
    }
{'fraction_non_alphanumeric': 0.13971503665790566, 'fraction_numerical': 0.006086595656384009, 'mean_word_length': 2.845744680851064, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 51, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4461576', 'n_tokens_mistral': 2435, 'n_tokens_neox': 2368, 'n_words': 354}