<commit_before>/*

Copyright (c) 2014 <NAME> <<EMAIL>>

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <float.h>

#include "win32_compat.h"
#include "sre.h"
#include "sre_internal.h"

// Constructor function for Scene.

sreScene::sreScene(int _max_objects, int _max_models, int _max_scene_lights) {
    nu_objects = 0;
    nu_models = 0;
    max_objects = _max_objects;
    object = new sreObject *[max_objects];
    max_models = _max_models;
    model = new sreModel *[max_models];
    nu_lights = 0;
    max_scene_lights = _max_scene_lights;
    light = new sreLight *[max_scene_lights];
    ambient_color = Color(0.1, 0.1, 0.1);
    // Scene building helpers.
    SetDiffuseReflectionColor(Color(1.0, 1.0, 1.0));
    current_flags = 0;
    current_texture = NULL;
    SetSpecularReflectionColor(Color(1.0, 1.0, 1.0));
    current_specular_exponent = 60.0;
    current_specularity_map = NULL;
    current_normal_map = NULL;
    current_emission_color = Color(0, 0, 0);
    current_emission_map = NULL;
    current_texture3d_scale = 1.0;
    current_texture3d_type = 0;
    current_mass = 1.0;
    current_roughness_values = Vector2D(0.15, 1.0);
    current_roughness_weights = Vector2D(1.0, 0);
    current_diffuse_fraction = 0.6;
    current_anisotropic = false;
    current_lod_flags = SRE_LOD_DYNAMIC;
    current_min_lod_level = 0;
    current_max_lod_level = 0;
    current_lod_threshold_scaling = 1.0;
    current_physics_lod_level = 0;
    current_UV_transformation_matrix = sre_internal_standard_UV_transformation_matrix;
    deleted_ids = new sreObjectList;
    // No rendering object arrays allocated yet.
    max_visible_objects = 0;
    max_final_pass_objects = 0; 
    max_shadow_caster_objects = 0;
    max_visible_lights = 0;
}

void sreScene::ClearOctrees() {
    // The storage for the fast octrees is freed, but they are invalid
    // until CreateOctrees() is called again.
    fast_octree_static.Destroy();
    fast_octree_dynamic.Destroy();
    fast_octree_static_infinite_distance.Destroy();
    fast_octree_dynamic_infinite_distance.Destroy();
}

// Make an already existing scene empty. Models are not affected.

void sreScene::ClearObjectsAndLights() {
    for (int i = 0; i < nu_objects; i++) {
        delete object[i];
    }
    nu_objects = 0;
    for (int i = 0; i < nu_lights; i++)
        delete light[i];
    nu_lights = 0;
    deleted_ids->MakeEmpty();
}

sreScene::~sreScene() {
    ClearModels();
    delete [] model;
    ClearObjectsAndLights();
    delete [] object;
    delete [] light;

    delete [] visible_object;
    delete [] shadow_caster_object;
    delete [] final_pass_object;
    delete [] visible_light;
}

void sreScene::PrepareForRendering(unsigned int flags) {
    if (!(flags & SRE_PREPARE_REUSE_OCTREES))
        CreateOctrees();
    if (flags & SRE_PREPARE_PREPROCESS)
        Preprocess();
    if (!(flags & SRE_PREPARE_UPLOAD_NO_MODELS)) {
        if (flags & SRE_PREPARE_UPLOAD_ALL_MODELS)
            MarkAllModelsReferenced();
        else
            RemoveUnreferencedModels();
    }

    // Temporarily allocate visual object and shadow caster object arrays
    // with full capacity for shadow volume calculation.
    nu_visible_objects = 0;
    visible_object = new int[max_objects];
    nu_shadow_caster_objects = 0;
    shadow_caster_object = new int[max_objects];
    // Use visible_object and shadow_caster_object arrays as scratch memory.<commit_msg>Fix demo program exit with not fully initialized scene<commit_after>/*

Copyright (c) 2014 <NAME> <<EMAIL>>

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <float.h>

#include "win32_compat.h"
#include "sre.h"
#include "sre_internal.h"

// Constructor function for Scene.

sreScene::sreScene(int _max_objects, int _max_models, int _max_scene_lights) {
    nu_objects = 0;
    nu_models = 0;
    max_objects = _max_objects;
    object = new sreObject *[max_objects];
    max_models = _max_models;
    model = new sreModel *[max_models];
    nu_lights = 0;
    max_scene_lights = _max_scene_lights;
    light = new sreLight *[max_scene_lights];
    ambient_color = Color(0.1, 0.1, 0.1);
    // Scene building helpers.
    SetDiffuseReflectionColor(Color(1.0, 1.0, 1.0));
    current_flags = 0;
    current_texture = NULL;
    SetSpecularReflectionColor(Color(1.0, 1.0, 1.0));
    current_specular_exponent = 60.0;
    current_specularity_map = NULL;
    current_normal_map = NULL;
    current_emission_color = Color(0, 0, 0);
    current_emission_map = NULL;
    current_texture3d_scale = 1.0;
    current_texture3d_type = 0;
    current_mass = 1.0;
    current_roughness_values = Vector2D(0.15, 1.0);
    current_roughness_weights = Vector2D(1.0, 0);
    current_diffuse_fraction = 0.6;
    current_anisotropic = false;
    current_lod_flags = SRE_LOD_DYNAMIC;
    current_min_lod_level = 0;
    current_max_lod_level = 0;
    current_lod_threshold_scaling = 1.0;
    current_physics_lod_level = 0;
    current_UV_transformation_matrix = sre_internal_standard_UV_transformation_matrix;
    deleted_ids = new sreObjectList;
    // No rendering object arrays allocated yet.
    max_visible_objects = 0;
    max_final_pass_objects = 0; 
    max_shadow_caster_objects = 0;
    max_visible_lights = 0;
}

void sreScene::ClearOctrees() {
    // The storage for the fast octrees is freed, but they are invalid
    // until CreateOctrees() is called again.
    fast_octree_static.Destroy();
    fast_octree_dynamic.Destroy();
    fast_octree_static_infinite_distance.Destroy();
    fast_octree_dynamic_infinite_distance.Destroy();
}

// Make an already existing scene empty. Models are not affected.

void sreScene::ClearObjectsAndLights() {
    for (int i = 0; i < nu_objects; i++) {
        delete object[i];
    }
    nu_objects = 0;
    for (int i = 0; i < nu_lights; i++)
        delete light[i];
    nu_lights = 0;
    deleted_ids->MakeEmpty();
}

sreScene::~sreScene() {
    ClearModels();
    delete [] model;
    ClearObjectsAndLights();
    delete [] object;
    delete [] light;

    if (max_visible_objects > 0)
        delete [] visible_object;
    if (max_shadow_caster_objects > 0)
        delete [] shadow_caster_object;
    if (max_final_pass_objects > 0)
        delete [] final_pass_object;
    if (max_visible_lights > 0)
        delete [] visible_light;
}

void sreScene::PrepareForRendering(unsigned int flags) {
    if (!(flags & SRE_PREPARE_REUSE_OCTREES))
        CreateOctrees();
    if (flags & SRE_PREPARE_PREPROCESS)
        Preprocess();
    if (!(flags & SRE_PREPARE_UPLOAD_NO_MODELS)) {
        if (flags & SRE_PREPARE_UPLOAD_ALL_MODELS)
            MarkAllModelsReferenced();
        else
            RemoveUnreferencedModels();
    }

    // Temporarily allocate visual object and shadow caster object arrays
    // with full capacity for shadow volume calculation.
    nu_visible_objects = 0;
    visible_object = new int[max_objects];
    nu_shadow_caster_objects = 0;
    shadow_caster_object = new int[max_objects];
    // Use visible_object and shadow_caster_object arrays as scratch memory.
{'fraction_non_alphanumeric': 0.0881772827474185, 'fraction_numerical': 0.016243183663998143, 'mean_word_length': 3.702673213311511, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 27, 'https://': 0, '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': '6828763', 'n_tokens_mistral': 2917, 'n_tokens_neox': 2745, 'n_words': 769}