void BulletPhysics::LoadXml()
{
	// load physics config file
	TiXmlElement* pRoot = XmlResourceLoader::LoadAndReturnRootXmlElement("config\\Physics.xml");
	CB_ASSERT(pRoot);

	// load materials
	TiXmlElement* pParentNode = pRoot->FirstChildElement("PhysicsMaterials");
	CB_ASSERT(pParentNode);
	for (TiXmlElement* pNode = pParentNode->FirstChildElement(); pNode; pNode = pNode->NextSiblingElement())
	{
		double restitution = 0.0;
		double friction = 0.0;
		pNode->Attribute("restitution", &restitution);
		pNode->Attribute("friction", &friction);
		m_MaterialTable.insert(std::make_pair(pNode->Value(), MaterialData((float)restitution, (float)friction)));
	}

	// load densities
	pParentNode = pRoot->FirstChildElement("DensityTable");
	CB_ASSERT(pParentNode);
	for (TiXmlElement* pNode = pParentNode->FirstChildElement(); pNode; pNode = pNode->NextSiblingElement())
	{
		m_DensityTable.insert(std::make_pair(pNode->Value(), (float)atof(pNode->FirstChild()->Value())));
	}
}
Example #2
0
    MaterialData MaterialManager::createAveragedMaterial( const MaterialVec& materials ) {
      
      std::stringstream sstr ;
      
      double sum_l = 0 ;
      double sum_rho_l = 0 ;
      double sum_rho_l_over_A = 0 ;
      double sum_rho_l_Z_over_A = 0 ;
      //double sum_rho_l_over_x = 0 ;
      double sum_l_over_x = 0 ;
      //double sum_rho_l_over_lambda = 0 ;
      double sum_l_over_lambda = 0 ;

     for(unsigned i=0,n=materials.size(); i<n ; ++i){

	Material mat = materials[i].first ;
	double   l   = materials[i].second ;

	if( i != 0 ) sstr << "_" ; 
	sstr << mat.name() << "_" << l ;

	double rho      = mat.density() ;
	double  A       = mat.A() ;
	double  Z       = mat.Z() ;
	double  x       = mat.radLength() ;
	double  lambda  = mat.intLength() ;
	
	sum_l                 +=   l ;
	sum_rho_l             +=   rho * l  ;
	sum_rho_l_over_A      +=   rho * l / A ;
	sum_rho_l_Z_over_A    +=   rho * l * Z / A ;
	sum_l_over_x          +=   l / x ;
	sum_l_over_lambda     +=   l / lambda ;
	// sum_rho_l_over_x      +=   rho * l / x ;
	// sum_rho_l_over_lambda +=   rho * l / lambda ;
     }

     double rho      =  sum_rho_l / sum_l ;

     double  A       =  sum_rho_l / sum_rho_l_over_A ;
     double  Z       =  sum_rho_l_Z_over_A / sum_rho_l_over_A ;

     // radiation and interaction lengths already given in cm - average by length
     
     // double  x       =  sum_rho_l / sum_rho_l_over_x ;
     double  x       =  sum_l / sum_l_over_x ;

     //     double  lambda  =  sum_rho_l / sum_rho_l_over_lambda ;
     double  lambda  =  sum_l / sum_l_over_lambda ;

     
     return MaterialData( sstr.str() , Z, A, rho, x, lambda ) ;

    }
MaterialData BulletPhysics::LookupMaterialData(const std::string& materialStr)
{
	// return material data
	auto it = m_MaterialTable.find(materialStr);
	if (it != m_MaterialTable.end())
	{
		return it->second;
	}
	else
	{
		return MaterialData(0.0f, 0.0f);
	}
}
Light::Light()
{
	//Default Settings
    constantAttenuation = 0.5;
    linearAttenuation = 0.5;
    quadraticAttenuation = 1.0;

	pointLightPosition = vec3(0, 0, 0);
	spotLightPosition = vec3(0, 0, 0);
	directionLightPosition = vec3(0, 10.0, 0);

	spotLightDirection = vec3(0.0, 0.0, -1.0);
	spot_cutoff = 30.0;
	exponentP = 4.0;

	materialData = MaterialData(); //set default properties
}
Example #5
0
DefaultResources::DefaultResources(DevicePtr dptr) :
		_spirv(std::make_unique<SpirVData[]>(spirv_count)),
		_computes(std::make_unique<ComputeProgram[]>(compute_count)),
		_materials(std::make_unique<AssetPtr<Material>[]>(material_count)) {

	for(usize i = 0; i != spirv_count; ++i) {
		_spirv[i] = load_spirv(spirv_names[i]);
	}

	for(usize i = 0; i != compute_count; ++i) {
		_computes[i] = ComputeProgram(ComputeShader(dptr, _spirv[usize(compute_spirvs[i])]));
	}

	for(usize i = 0; i != material_count; ++i) {
		const auto& data = material_datas[i];
		_materials[i] = make_asset<Material>(dptr, MaterialData()
				.set_frag_data(_spirv[data.frag])
				.set_vert_data(_spirv[data.vert])
				.set_depth_tested(data.depth_tested)
				.set_culled(data.culled)
				.set_blended(data.blended)
			);
	}
}
Example #6
0
MaterialData ModelLoader::loadMaterial(unsigned int index, const aiMaterial* material)
{
    Q_ASSERT(material != nullptr);

    MaterialData data = MaterialData();
    data.name = "material_" + to_string(index);

    aiColor3D ambientColor(0.1f, 0.1f, 0.1f);
    aiColor3D diffuseColor(0.8f, 0.8f, 0.8f);
    aiColor3D specularColor(0.0f, 0.0f, 0.0f);
    aiColor3D emissiveColor(0.0f, 0.0f, 0.0f);

    int blendMode;
    int twoSided = 1;

    float opacity = 1.0f;
    float shininess = 0.0f;
    float shininessStrength = 1.0f;

    if(material->Get(AI_MATKEY_COLOR_AMBIENT, ambientColor) == AI_SUCCESS)
    {
        data.ambientColor.setX(ambientColor.r);
        data.ambientColor.setY(ambientColor.g);
        data.ambientColor.setZ(ambientColor.b);
    }

    if(material->Get(AI_MATKEY_COLOR_DIFFUSE, diffuseColor) == AI_SUCCESS)
    {
        data.diffuseColor.setX(diffuseColor.r);
        data.diffuseColor.setY(diffuseColor.g);
        data.diffuseColor.setZ(diffuseColor.b);
    }

    if(material->Get(AI_MATKEY_COLOR_SPECULAR, specularColor) == AI_SUCCESS)
    {
        data.specularColor.setX(specularColor.r);
        data.specularColor.setY(specularColor.g);
        data.specularColor.setZ(specularColor.b);
    }

    if(material->Get(AI_MATKEY_COLOR_EMISSIVE, emissiveColor) == AI_SUCCESS)
    {
        data.emissiveColor.setX(emissiveColor.r);
        data.emissiveColor.setY(emissiveColor.g);
        data.emissiveColor.setZ(emissiveColor.b);
    }

    if(material->Get(AI_MATKEY_TWOSIDED, twoSided) == AI_SUCCESS)
    {
        data.twoSided = twoSided;
    }

    if(material->Get(AI_MATKEY_OPACITY, opacity) == AI_SUCCESS)
    {
        data.ambientColor.setW(opacity);
        data.diffuseColor.setW(opacity);
        data.specularColor.setW(opacity);
        data.emissiveColor.setW(opacity);

        if(opacity < 1.0f)
        {
            data.alphaBlending = true;

            // Activate backface culling allows to avoid
            // cull artifacts when alpha blending is activated
            data.twoSided = 1;

            if(material->Get(AI_MATKEY_BLEND_FUNC, blendMode) == AI_SUCCESS)
            {
                if(blendMode == aiBlendMode_Additive)
                    data.blendMode = aiBlendMode_Additive;
                else
                    data.blendMode = aiBlendMode_Default;
            }
        }
        else
        {
            data.alphaBlending = false;
            data.blendMode = -1;
        }
    }

    if(material->Get(AI_MATKEY_SHININESS, shininess) == AI_SUCCESS)
    {
        data.shininess = shininess;
    }

    if(material->Get(AI_MATKEY_SHININESS_STRENGTH, shininessStrength) == AI_SUCCESS)
    {
        data.shininessStrength = shininessStrength;
    }

    return data;
}
Example #7
0
ObjData* ObjLoader::LoadObjFile(string filepath)
{



	string mtlfile = "";
	ObjData* returndata = new ObjData();
	//this->trianglulate(filename);								////////// Not quite working

	string folders = "";
	string filename = filepath;
	size_t slashpos = filename.find("/");
	while(slashpos != string::npos)
	{
		slashpos = filename.find("/");
		folders += filename.substr(0, slashpos + 1);
		filename = filename.substr(slashpos + 1);
	}

	
	// Binary file
	ifstream binfile;
	binfile.open(folders + "Cache/" + filename.substr(0, filename.size() - 4) + ".MalEng", ios::binary);
	if(binfile)
	{
		this->ReadFromBinaryFile(returndata, binfile);
		binfile.close();
		return returndata;			// Return and skip the Obj-file.
	}
	


	// Obj file
	ifstream file;
	file.open(filepath);
	if(!file)
	{
		MaloW::Debug("Couldnt open file in ObjLoader: " + filepath);
	}

	string currentMaterial = "";

	while(!file.eof())
	{
		string line = "";
		getline(file, line);
		if(line.size() > 0 && line.substr(0, 2) == "v ")
		{
			D3DXVECTOR3 vertpos = this->GetVertPosData(line);
			vertpos.z *= -1;	// For D3DX
			returndata->vertspos->add(vertpos);
		}
		else if(line.size() > 0 && line.substr(0, 2) == "vt")
		{
			returndata->textcoords->add(this->GetTextCoordsData(line));
		}
		else if(line.size() > 0 && line.substr(0, 2) == "vn")
		{
			D3DXVECTOR3 norm = this->GetVertsNormsData(line);
			norm.z *= -1;	// For D3DX
			returndata->vertsnorms->add(norm);
		}
		else if(line.size() > 0 && line.at(0) == 'f')
		{
			FaceData data = this->GetFaceData(line);
			data.material = currentMaterial;
			returndata->faces->add(data);
		}
		else if(line.size() > 0 && line.substr(0, 6) == "mtllib")
			mtlfile = line.substr(7);

		else if(line.size() > 0 && line.substr(0, 6) == "usemtl")
			currentMaterial = line.substr(7);
	}
	file.close();


	
	// mtl file
	if(mtlfile == "")
		return returndata;
	
	mtlfile = folders + mtlfile;
	

	file.open(mtlfile);
	if(!file)
	{
		MaloW::Debug("Couldnt open file in ObjLoader: " + mtlfile);
	}

	bool firstMat = true;
	MaterialData md;
	while(!file.eof())
	{
		string line = "";
		getline(file, line);

		if(line.size() > 0 && line.substr(0, 6) == "newmtl")
		{
			if(!firstMat)
			{
				returndata->mats->add(md);
				md = MaterialData();
			}
			md.name = line.substr(7);
			firstMat = false;
		}
		else if(line.size() > 0 && line.substr(0, 5) == "illum")
		{
			md.illum = atoi(line.substr(6).c_str());
		}
		else if(line.size() > 0 && line.substr(0, 2) == "Kd")
		{
			D3DXVECTOR3 Kd = this->GetVertPosData(line);
			md.kd = Kd;
		}
		else if(line.size() > 0 && line.substr(0, 2) == "Ka")
		{
			D3DXVECTOR3 Ka = this->GetVertPosData(line);
			md.ka = Ka;
		}
		else if(line.size() > 0 && line.substr(0, 2) == "Tf")
		{
			D3DXVECTOR3 Tf = this->GetVertPosData(line);
			md.tf = Tf;
		}
		else if(line.size() > 0 && line.substr(0, 6) == "map_Kd")
		{
			md.texture = folders;
			md.texture += line.substr(7);
		}
		else if(line.size() > 0 && line.substr(0, 2) == "Ks")
		{
			D3DXVECTOR3 Ks = this->GetVertPosData(line);
			md.ks = Ks;
		}
		else if(line.size() > 0 && line.substr(0, 2) == "Ni")
		{
			float Ni = (float)atof(line.substr(3).c_str());
			md.ni = Ni;
		}

	}
	if(!firstMat)
		returndata->mats->add(md);
	
	file.close();

	this->CreateBinaryFile(folders + "Cache/" + filename, folders + "Cache/", returndata);

	return returndata;
}