#include "OBJsupport.h"


void convertMaterialToVRMLCompatible(MTLmaterial &material)
{
	// Pro kompatibilitu s VRML rendererem je potreba spekularni exponent prenasobit 128.0 

	const float VRML_SHININESS_MULTIPLIER=128.0;
	material.specular_exp*=VRML_SHININESS_MULTIPLIER;
}

string getPath(const string &prefix, const string &path)
{
	// cout << "getPath(" << prefix << ", " << path << ");" << endl;
	if(path.length() >= 3 && path[1] == ':' && path[2] == '\\')
		return path; // path is absolute
	else
		return prefix + path;
}

void initMaterialAndTextures(MTLreader &reader, PhongMaterialManager &materialManager, TextureManager &textureManager, const string& textureNamePrefix,  const string texturePathPrefix )
{
	string name;
	MTLmaterial mtlMaterial;
	PhongMaterial phongMaterial;
	
	while (reader.readNextMaterial( mtlMaterial))
	{
		// do not convert from OBJ to VRML compatible material data
		//convertMaterialToVRMLCompatible(mtlMaterial);

		convertFromMtlToPhongMaterial(mtlMaterial, phongMaterial);

		materialManager.insertMaterial(mtlMaterial.name, phongMaterial);

		//load textury (zatim jen Kd)
		if (!mtlMaterial.map_Kd.empty())
		{
			Texture<unsigned char> texData;
			textureManager.readTexture( getPath(texturePathPrefix, mtlMaterial.map_Kd), texData );
			textureManager.createTexture( textureNamePrefix + mtlMaterial.name, texData );	
		}

		mtlMaterial.clear();

	}
}

bool BoundingBox::calculateBoundingBox( const vcoord_buffer &src_vcoord_buffer, BoundingBox &dstBoundingBox )
{
	if ( src_vcoord_buffer.size() < 3 )
	{
		return false;
	}

	dstBoundingBox.bound[ MAX_X ].set_data( src_vcoord_buffer[0], src_vcoord_buffer[1], src_vcoord_buffer[2] );
	dstBoundingBox.bound[ MAX_Y ].set_data( src_vcoord_buffer[0], src_vcoord_buffer[1], src_vcoord_buffer[2] );
	dstBoundingBox.bound[ MAX_Z ].set_data( src_vcoord_buffer[0], src_vcoord_buffer[1], src_vcoord_buffer[2] );
	dstBoundingBox.bound[ MIN_X ].set_data( src_vcoord_buffer[0], src_vcoord_buffer[1], src_vcoord_buffer[2] );
	dstBoundingBox.bound[ MIN_Y ].set_data( src_vcoord_buffer[0], src_vcoord_buffer[1], src_vcoord_buffer[2] );
	dstBoundingBox.bound[ MIN_Z ].set_data( src_vcoord_buffer[0], src_vcoord_buffer[1], src_vcoord_buffer[2] );

	for ( int i=0; i< src_vcoord_buffer.size(); i+=3 )
	{
		//MAX
		if ( src_vcoord_buffer[ i + 0 ] > dstBoundingBox.bound[ MAX_X ].x() )
		{
			//we found new maxX point
			dstBoundingBox.bound[ MAX_X ].set_data( src_vcoord_buffer[ i + 0 ], src_vcoord_buffer[ i + 1 ], src_vcoord_buffer[ i + 2 ] );
		}
		if ( src_vcoord_buffer[ i + 1 ] > dstBoundingBox.bound[ MAX_Y ].y() )
		{
			//we found new maxY point
			dstBoundingBox.bound[ MAX_Y ].set_data( src_vcoord_buffer[ i + 0 ], src_vcoord_buffer[ i + 1 ], src_vcoord_buffer[ i + 2 ] );
		}
		if ( src_vcoord_buffer[ i + 2 ] > dstBoundingBox.bound[ MAX_Z ].z() )
		{
			//we found new maxZ point
			dstBoundingBox.bound[ MAX_Z ].set_data( src_vcoord_buffer[ i + 0 ], src_vcoord_buffer[ i + 1 ], src_vcoord_buffer[ i + 2 ] );
		}

		//MIN
		if ( src_vcoord_buffer[ i + 0 ] < dstBoundingBox.bound[ MIN_X ].x() )
		{
			//we found new minX point
			dstBoundingBox.bound[ MIN_X ].set_data( src_vcoord_buffer[ i + 0 ], src_vcoord_buffer[ i + 1 ], src_vcoord_buffer[ i + 2 ] );
		}
		if ( src_vcoord_buffer[ i + 1 ] < dstBoundingBox.bound[ MIN_Y ].y() )
		{
			//we found new minY point
			dstBoundingBox.bound[ MIN_Y ].set_data( src_vcoord_buffer[ i + 0 ], src_vcoord_buffer[ i + 1 ], src_vcoord_buffer[ i + 2 ] );
		}
		if ( src_vcoord_buffer[ i + 2 ] < dstBoundingBox.bound[ MIN_Z ].z() )
		{
			//we found new minX point
			dstBoundingBox.bound[ MIN_Z ].set_data( src_vcoord_buffer[ i + 0 ], src_vcoord_buffer[ i + 1 ], src_vcoord_buffer[ i + 2 ] );
		}
	}

	return true;
}
