<commit_before>#include "ModuleLoader.h"
#include "glew\include\GL\glew.h"
#include "Application.h"
#include "Console.h"
#include "Assimp/include/cimport.h"
#include "Assimp/include/scene.h"
#include "Assimp/include/postprocess.h"
#include "Assimp/include/cfileio.h"
#include "GameObject.h"
#include "ModuleSceneIntro.h"
#include "ModuleImGui.h"
#include "ModuleCamera3D.h"
#include "Mesh.h"
#include "Transform.h"
#include "Material.h"
#include <queue>

#pragma comment (lib, "Assimp/libx86/assimp.lib")


ModuleLoader::ModuleLoader(bool start_enabled) : Module(start_enabled)
{
	SetName("Loader");
}

ModuleLoader::~ModuleLoader()
{
}

bool ModuleLoader::Init()
{

	struct aiLogStream stream;
	stream = aiGetPredefinedLogStream(aiDefaultLogStream_DEBUGGER, nullptr);
	aiAttachLogStream(&stream);

	return true;
}

update_status ModuleLoader::Update(float dt)
{
	
	/*for (list<Mesh*>::iterator it = meshes.begin(); it != meshes.end(); ++it) {
		(*it)->Render(texture);
	}*/

	
	return update_status::UPDATE_CONTINUE;
}

bool ModuleLoader::CleanUp()
{
	aiDetachAllLogStreams();
	if (!meshes.empty()) {
		for (list<Mesh*>::iterator it = meshes.begin(); it != meshes.end(); ++it) {
		//	UnloadMesh(*it);
			delete (*it);
		}
		meshes.clear();
	}
	LOG("All meshes deleted");
	return true;
}

void ModuleLoader::LoadFBX(char* path)
{
	const aiScene* scene = aiImportFile(path, aiProcessPreset_TargetRealtime_MaxQuality);
	LOG("FBX Load: Loading %s", path);
	if (scene->mRootNode != nullptr) {
		std::queue<pair<aiNode*, GameObject*>> nodes;
		nodes.push({ scene->mRootNode, nullptr });
		while (!nodes.empty())
		{
			pair<aiNode*, GameObject*> cnode = nodes.front();
			LOG("FBX Load: Loading node with %d meshes", cnode.first->mNumMeshes);
			//Creating new gameobject
			GameObject* new_obj = new GameObject(/*cnode.first->mName.C_Str()*/"", cnode.second);
			if (cnode.first == scene->mRootNode) {
				App->scene_intro->AddObject(new_obj);
				App->scene_intro->SetobjSelected(new_obj);
			}

			//Adding childs to queue
			LOG("FBX Load: Queueing %d child nodes", cnode.first->mNumChildren);
			for (int i = 0; i < cnode.first->mNumChildren; ++i)
				nodes.push({ cnode.first->mChildren[i], new_obj });

			GameObject* parent = new_obj;
			// Use scene->mNumMeshes to iterate on scene->mMeshes array
			for (int i = 0; i < cnode.first->mNumMeshes; i++, new_obj = new GameObject(/*scene->mMeshes[i]->mName.C_Str()*/"", parent)) {

				//Transform
				float3 pos(0, 0, 0);
				float3 scale(1, 1, 1);
				Quat rot({ 1,0,0 }, 0);

				if (new_obj == parent)
				{
					LOG("Loading transform");
					aiVector3D translation;
					aiVector3D scaling;
					aiQuaternion rotation;

					cnode.first->mTransformation.Decompose(scaling, rotation, translation);
					pos = { translation.x, translation.y, translation.z };
					scale = { scaling.x, scaling.y, scaling.z };
					rot = Quat( rotation.x, rotation.y, rotation.z, rotation.w );
				}

				Transform* trans = new Transform(scale, rot, pos, new_obj);
				new_obj->AddComponent(trans);

				//Vertices
				App->imgui->curr_obj = new_obj;
				aiMesh* m = scene->mMeshes[i];
				Mesh* new_mesh = new Mesh(new_obj);<commit_msg>Fixed bug when loading multiple nodes with meshes<commit_after>#include "ModuleLoader.h"
#include "glew\include\GL\glew.h"
#include "Application.h"
#include "Console.h"
#include "Assimp/include/cimport.h"
#include "Assimp/include/scene.h"
#include "Assimp/include/postprocess.h"
#include "Assimp/include/cfileio.h"
#include "GameObject.h"
#include "ModuleSceneIntro.h"
#include "ModuleImGui.h"
#include "ModuleCamera3D.h"
#include "Mesh.h"
#include "Transform.h"
#include "Material.h"
#include <queue>

#pragma comment (lib, "Assimp/libx86/assimp.lib")


ModuleLoader::ModuleLoader(bool start_enabled) : Module(start_enabled)
{
	SetName("Loader");
}

ModuleLoader::~ModuleLoader()
{
}

bool ModuleLoader::Init()
{

	struct aiLogStream stream;
	stream = aiGetPredefinedLogStream(aiDefaultLogStream_DEBUGGER, nullptr);
	aiAttachLogStream(&stream);

	return true;
}

update_status ModuleLoader::Update(float dt)
{
	
	/*for (list<Mesh*>::iterator it = meshes.begin(); it != meshes.end(); ++it) {
		(*it)->Render(texture);
	}*/

	
	return update_status::UPDATE_CONTINUE;
}

bool ModuleLoader::CleanUp()
{
	aiDetachAllLogStreams();
	if (!meshes.empty()) {
		for (list<Mesh*>::iterator it = meshes.begin(); it != meshes.end(); ++it) {
		//	UnloadMesh(*it);
			delete (*it);
		}
		meshes.clear();
	}
	LOG("All meshes deleted");
	return true;
}

void ModuleLoader::LoadFBX(char* path)
{
	const aiScene* scene = aiImportFile(path, aiProcessPreset_TargetRealtime_MaxQuality);
	LOG("FBX Load: Loading %s", path);
	if (scene->mRootNode != nullptr) {
		std::queue<pair<aiNode*, GameObject*>> nodes;
		nodes.push({ scene->mRootNode, nullptr });
		while (!nodes.empty())
		{
			pair<aiNode*, GameObject*> cnode = nodes.front();
			LOG("FBX Load: Loading node with %d meshes", cnode.first->mNumMeshes);
			//Creating new gameobject
			GameObject* new_obj = new GameObject(/*cnode.first->mName.C_Str()*/"", cnode.second);
			if (cnode.first == scene->mRootNode) {
				App->scene_intro->AddObject(new_obj);
				App->scene_intro->SetobjSelected(new_obj);
			}

			//Adding childs to queue
			LOG("FBX Load: Queueing %d child nodes", cnode.first->mNumChildren);
			for (int i = 0; i < cnode.first->mNumChildren; ++i)
				nodes.push({ cnode.first->mChildren[i], new_obj });

			GameObject* parent = new_obj;
			// Use scene->mNumMeshes to iterate on scene->mMeshes array
			for (int i = 0; i < cnode.first->mNumMeshes; i++, new_obj = new GameObject(/*scene->mMeshes[i]->mName.C_Str()*/"", parent)) {

				//Transform
				float3 pos(0, 0, 0);
				float3 scale(1, 1, 1);
				Quat rot({ 1,0,0 }, 0);

				if (new_obj == parent)
				{
					LOG("Loading transform");
					aiVector3D translation;
					aiVector3D scaling;
					aiQuaternion rotation;

					cnode.first->mTransformation.Decompose(scaling, rotation, translation);
					pos = { translation.x, translation.y, translation.z };
					scale = { scaling.x, scaling.y, scaling.z };
					rot = Quat( rotation.x, rotation.y, rotation.z, rotation.w );
				}

				Transform* trans = new Transform(scale, rot, pos, new_obj);
				new_obj->AddComponent(trans);

				//Vertices
				App->imgui->curr_obj = new_obj;
				aiMesh* m = scene->mMeshes[cnode.first->mMeshes[i]];
				Mesh* new_mesh = new Mesh(new_obj);
{'fraction_non_alphanumeric': 0.16510465479537645, 'fraction_numerical': 0.005935645110902843, 'mean_word_length': 4.335833333333333, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 62, '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': '2831143', 'n_tokens_mistral': 2578, 'n_tokens_neox': 2277, 'n_words': 478}