ECS/Project/Project/GeometryGeneratorSelector/GeometryGeneratorSelector.cpp
//	Load the Geometry from the Obj.
Load the Geometry from the Obj.
Load the Geometry from the Obj .
void GeometryGeneratorSelector::loadGeometryFromObj(std::vector<std::pair<std::string, std::shared_ptr<GeometryData>>> & geometryList, std::string name, const ResourceDescription & newResourceDescription)
{
	std::string filename = "None";
	if (newResourceDescription.findProperty("Geometry Filename", filename))
	{
		tinyobj::attrib_t attrib;
		std::vector<tinyobj::shape_t> shapes;
		std::vector<tinyobj::material_t> materials;
		std::string err;
		bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, filename.c_str());
		if (!err.empty())
		{
			std::cerr << err << std::endl;
		}
		if (!ret)
		{
			exit(1);
		}
		std::cout << "# of shapes    : " << shapes.size() << std::endl;
		std::cout << "# of materials : " << materials.size() << std::endl;
		glm::vec3 maxXYZ = glm::vec3(0.0);
		glm::vec3 minXYZ = glm::vec3(FP_INFINITE);
		std::vector<std::vector<int>> currentIndicesArrays;
		std::vector<std::vector<Vertex>> currentVertexArrays;
		for (size_t s = 0; s < shapes.size(); s++)
		{
			std::vector<int> currentIndicesArray;
			std::vector<Vertex> currentVertexArray;
			std::vector<glm::vec3> offsets;
			for (size_t i = 0; i < shapes[s].mesh.indices.size(); i++)
			{
				int currentVertexIndex = shapes[s].mesh.indices[i].vertex_index;
				float vx = attrib.vertices[3 * currentVertexIndex + 0];
				float vy = attrib.vertices[3 * currentVertexIndex + 1];
				float vz = attrib.vertices[3 * currentVertexIndex + 2];
				glm::vec3 position = glm::vec3(vx, vy, vz);
				if (maxXYZ.x < position.x) maxXYZ.x = position.x;
				if (maxXYZ.y < position.y) maxXYZ.y = position.y;
				if (maxXYZ.z < position.z) maxXYZ.z = position.z;
				if (minXYZ.x > position.x) minXYZ.x = position.x;
				if (minXYZ.y > position.y) minXYZ.y = position.y;
				if (minXYZ.z > position.z) minXYZ.z = position.z;
				offsets.push_back(position);
			}
			for (size_t i = 0; i < shapes[s].mesh.indices.size(); i++)
			{
				Vertex vertexData;
				currentIndicesArray.push_back((int)i);
				int currentVertexIndex = shapes[s].mesh.indices[i].vertex_index;
				float vx = attrib.vertices[3 * currentVertexIndex + 0];
				float vy = attrib.vertices[3 * currentVertexIndex + 1];
				float vz = attrib.vertices[3 * currentVertexIndex + 2];
				glm::vec3 position = glm::vec3(vx, vy, vz);
				vertexData.position = glm::vec4(position + offsets[i], 1.0);
				Get the normal vertex.
				int currentNormalIndex = shapes[s].mesh.indices[i].normal_index;
				Create the normal vector.
				float nx = attrib.normals[3 * currentNormalIndex + 0];
				float ny = attrib.normals[3 * currentNormalIndex + 1];
				float nz = attrib.normals[3 * currentNormalIndex + 2];
				glm::vec3 normal = glm::vec3(nx, ny, nz);
				vertexData.normal = normal;
				Get the texture index.
				int currentTextureIndex = shapes[s].mesh.indices[i].texcoord_index;
				currentVertexArray.push_back(vertexData);
			}
			Add this list of indices to the list of lists of indices.
			currentIndicesArrays.push_back(currentIndicesArray);
			Add this list of vertices to the list of lists of vertices.
			currentVertexArrays.push_back(currentVertexArray);
		}
		if (currentIndicesArrays.size() == 1)
		{
			Create the geometry data of this pair of indices and vertices.
			geometryList.push_back(std::make_pair(name, std::make_shared<GeometryData>(GL_TRIANGLES, currentIndicesArrays[0], currentVertexArrays[0])));
		}
		else
		{
			Iterate over the list of lists of indices.	
			for (int i = 0; i < currentIndicesArrays.size(); i++)
			{
				Create the geometry data of this pair of indices and vertices.
				std::shared_ptr<GeometryData> newGeometryDataElement = std::make_shared<GeometryData>(GL_TRIANGLES, currentIndicesArrays[i], currentVertexArrays[i]);
				geometryList.push_back(std::make_pair(name + std::to_string(i), newGeometryDataElement));
			}
		}
	}
}
void GeometryGeneratorSelector :: loadGeometryFromObj ( std :: vector < std :: pair < std :: string , std :: shared_ptr < GeometryData > > > & geometryList , std :: string name , const ResourceDescription & newResourceDescription ) { std :: string filename = " " ; if ( newResourceDescription . findProperty ( " " , filename ) ) { tinyobj :: attrib_t attrib ; std :: vector < tinyobj :: shape_t > shapes ; std :: vector < tinyobj :: material_t > materials ; std :: string err ; bool ret = tinyobj :: LoadObj ( & attrib , & shapes , & materials , & err , filename . c_str ( ) ) ; if ( ! err . empty ( ) ) { std :: cerr << err << std :: endl ; } if ( ! ret ) { exit ( 1 ) ; } std :: cout << " " << shapes . size ( ) << std :: endl ; std :: cout << " " << materials . size ( ) << std :: endl ; glm :: vec3 maxXYZ = glm :: vec3 ( 0.0 ) ; glm :: vec3 minXYZ = glm :: vec3 ( FP_INFINITE ) ; std :: vector < std :: vector < int > > currentIndicesArrays ; std :: vector < std :: vector < Vertex > > currentVertexArrays ; for ( size_t s = 0 ; s < shapes . size ( ) ; s ++ ) { std :: vector < int > currentIndicesArray ; std :: vector < Vertex > currentVertexArray ; std :: vector < glm :: vec3 > offsets ; for ( size_t i = 0 ; i < shapes [ s ] . mesh . indices . size ( ) ; i ++ ) { int currentVertexIndex = shapes [ s ] . mesh . indices [ i ] . vertex_index ; float vx = attrib . vertices [ 3 * currentVertexIndex + 0 ] ; float vy = attrib . vertices [ 3 * currentVertexIndex + 1 ] ; float vz = attrib . vertices [ 3 * currentVertexIndex + 2 ] ; glm :: vec3 position = glm :: vec3 ( vx , vy , vz ) ; if ( maxXYZ . x < position . x ) maxXYZ . x = position . x ; if ( maxXYZ . y < position . y ) maxXYZ . y = position . y ; if ( maxXYZ . z < position . z ) maxXYZ . z = position . z ; if ( minXYZ . x > position . x ) minXYZ . x = position . x ; if ( minXYZ . y > position . y ) minXYZ . y = position . y ; if ( minXYZ . z > position . z ) minXYZ . z = position . z ; offsets . push_back ( position ) ; } for ( size_t i = 0 ; i < shapes [ s ] . mesh . indices . size ( ) ; i ++ ) { Vertex vertexData ; currentIndicesArray . push_back ( ( int ) i ) ; int currentVertexIndex = shapes [ s ] . mesh . indices [ i ] . vertex_index ; float vx = attrib . vertices [ 3 * currentVertexIndex + 0 ] ; float vy = attrib . vertices [ 3 * currentVertexIndex + 1 ] ; float vz = attrib . vertices [ 3 * currentVertexIndex + 2 ] ; glm :: vec3 position = glm :: vec3 ( vx , vy , vz ) ; vertexData . position = glm :: vec4 ( position + offsets [ i ] , 1.0 ) ; int currentNormalIndex = shapes [ s ] . mesh . indices [ i ] . normal_index ; float nx = attrib . normals [ 3 * currentNormalIndex + 0 ] ; float ny = attrib . normals [ 3 * currentNormalIndex + 1 ] ; float nz = attrib . normals [ 3 * currentNormalIndex + 2 ] ; glm :: vec3 normal = glm :: vec3 ( nx , ny , nz ) ; vertexData . normal = normal ; int currentTextureIndex = shapes [ s ] . mesh . indices [ i ] . texcoord_index ; currentVertexArray . push_back ( vertexData ) ; } currentIndicesArrays . push_back ( currentIndicesArray ) ; currentVertexArrays . push_back ( currentVertexArray ) ; } if ( currentIndicesArrays . size ( ) == 1 ) { geometryList . push_back ( std :: make_pair ( name , std :: make_shared < GeometryData > ( GL_TRIANGLES , currentIndicesArrays [ 0 ] , currentVertexArrays [ 0 ] ) ) ) ; } else { for ( int i = 0 ; i < currentIndicesArrays . size ( ) ; i ++ ) { std :: shared_ptr < GeometryData > newGeometryDataElement = std :: make_shared < GeometryData > ( GL_TRIANGLES , currentIndicesArrays [ i ] , currentVertexArrays [ i ] ) ; geometryList . push_back ( std :: make_pair ( name + std :: to_string ( i ) , newGeometryDataElement ) ) ; } } } }
Load the Geometry from the Obj.
Load the Geometry from the Obj .
//	Get the Geometry Filename. //	Use the tiny object loader. //	The attributes. //	The shapes of the object. //	The materials of the object. //	The error string. //	Load the Obj into the attribute, the shapes and the materials. //	Check if there is any error. // `err` may contain warning message. //	Quit if there was a problem with loading the object. //	Initialize to max to zero and min to infinite. //	The list of of lists of indices. //	The list of of lists of vertices. //	Iterate over the shapes. //	The list of Indices. //	The list of Vertices. //	The list of offsets. //	Create the offsets to zero the shape to the origin. //	Get the index of the current vertex. //	Get the vertex details. //	Create the position. //	Compare the extremities to the position. //	Add the offset to the list of offsets.  //	Iterate over the indices. //	Create a new VertexData. //	Add it to the list of indices.  //	Get the vertex index. //	Create the position vector. //	 //	Get the normal vertex. //	Create the normal vector. //	Get the texture index. //	Add this list of indices to the list of lists of indices. //	Add this list of vertices to the list of lists of vertices. //	Create the geometry data of this pair of indices and vertices. //	Iterate over the list of lists of indices.	 //	Create the geometry data of this pair of indices and vertices.