Example #1
0
//------------------------------
bool MaterialsWriter::writeColorBlock(const COLLADAFW::Color& color, ChunkID chunkId)
{
    // write color chunk
    mBuffer.copyToBuffer(chunkId);
    mBuffer.copyToBuffer(COLOR_CHUNK_LENGTH_RGB_BYTE);

    //write color chunk rgb byte
    mBuffer.copyToBuffer(COL_TRU);
    mBuffer.copyToBuffer(CHUNK_RGB_BYTE_LENGTH);

    // we need to round
    unsigned char red = (unsigned char)(color.getRed()*255.0 + 0.5);
    unsigned char green = (unsigned char)(color.getGreen()*255.0 + 0.5);
    unsigned char blue = (unsigned char)(color.getBlue()*255.0 + 0.5);

    mBuffer.copyToBuffer(red);
    mBuffer.copyToBuffer(green);
    mBuffer.copyToBuffer(blue);
    return true;
}
	//------------------------------
	double LibraryEffectsLoader::calculateLuminance ( const COLLADAFW::Color& color )
	{
		return ( (color.getRed () * 0.212671) + (color.getGreen () * 0.715160) + (color.getBlue () * 0.072169) );
	}
Example #3
0
void DocumentImporter::write_profile_COMMON(COLLADAFW::EffectCommon *ef, Material *ma)
{
	COLLADAFW::EffectCommon::ShaderType shader = ef->getShaderType();
	
	// blinn
	if (shader == COLLADAFW::EffectCommon::SHADER_BLINN) {
		ma->spec_shader = MA_SPEC_BLINN;
		ma->spec = ef->getShininess().getFloatValue();
	}
	// phong
	else if (shader == COLLADAFW::EffectCommon::SHADER_PHONG) {
		ma->spec_shader = MA_SPEC_PHONG;
		ma->har = ef->getShininess().getFloatValue();
	}
	// lambert
	else if (shader == COLLADAFW::EffectCommon::SHADER_LAMBERT) {
		ma->diff_shader = MA_DIFF_LAMBERT;
	}
	// default - lambert
	else {
		ma->diff_shader = MA_DIFF_LAMBERT;
		fprintf(stderr, "Current shader type is not supported, default to lambert.\n");
	}
	// reflectivity
	ma->ray_mirror = ef->getReflectivity().getFloatValue();
	// index of refraction
	ma->ang = ef->getIndexOfRefraction().getFloatValue();
	
	int i = 0;
	COLLADAFW::Color col;
	MTex *mtex = NULL;
	TexIndexTextureArrayMap texindex_texarray_map;
	
	// DIFFUSE
	// color
	if (ef->getDiffuse().isColor()) {
		/* too high intensity can create artefacts (fireflies)
		   So here we take care that intensity is set to 0.8 wherever possible
		*/
		col = ef->getDiffuse().getColor();
		ma->ref = max_ffff(col.getRed(), col.getGreen(), col.getBlue(), 0.8);
		ma->r = col.getRed()   / ma->ref;
		ma->g = col.getGreen() / ma->ref;
		ma->b = col.getBlue()  / ma->ref;
	}
	// texture
	else if (ef->getDiffuse().isTexture()) {
		COLLADAFW::Texture ctex = ef->getDiffuse().getTexture(); 
		mtex = create_texture(ef, ctex, ma, i, texindex_texarray_map);
		if (mtex != NULL) {
			mtex->mapto = MAP_COL;
			ma->texact = (int)i;
			i++;
		}
	}
	// AMBIENT
	// color
	if (ef->getAmbient().isColor()) {
		col = ef->getAmbient().getColor();
		ma->ambr = col.getRed();
		ma->ambg = col.getGreen();
		ma->ambb = col.getBlue();
	}
	// texture
	else if (ef->getAmbient().isTexture()) {
		COLLADAFW::Texture ctex = ef->getAmbient().getTexture(); 
		mtex = create_texture(ef, ctex, ma, i, texindex_texarray_map);
		if (mtex != NULL) {
			mtex->mapto = MAP_AMB; 
			i++;
		}
	}
	// SPECULAR
	// color
	if (ef->getSpecular().isColor()) {
		col = ef->getSpecular().getColor();
		ma->specr = col.getRed();
		ma->specg = col.getGreen();
		ma->specb = col.getBlue();
	}
	// texture
	else if (ef->getSpecular().isTexture()) {
		COLLADAFW::Texture ctex = ef->getSpecular().getTexture(); 
		mtex = create_texture(ef, ctex, ma, i, texindex_texarray_map);
		if (mtex != NULL) {
			mtex->mapto = MAP_SPEC; 
			i++;
		}
	}
	// REFLECTIVE
	// color
	if (ef->getReflective().isColor()) {
		col = ef->getReflective().getColor();
		ma->mirr = col.getRed();
		ma->mirg = col.getGreen();
		ma->mirb = col.getBlue();
	}
	// texture
	else if (ef->getReflective().isTexture()) {
		COLLADAFW::Texture ctex = ef->getReflective().getTexture(); 
		mtex = create_texture(ef, ctex, ma, i, texindex_texarray_map);
		if (mtex != NULL) {
			mtex->mapto = MAP_REF; 
			i++;
		}
	}
	// EMISSION
	// color
	if (ef->getEmission().isColor()) {
		// XXX there is no emission color in blender
		// but I am not sure
	}
	// texture
	else if (ef->getEmission().isTexture()) {
		COLLADAFW::Texture ctex = ef->getEmission().getTexture(); 
		mtex = create_texture(ef, ctex, ma, i, texindex_texarray_map);
		if (mtex != NULL) {
			mtex->mapto = MAP_EMIT; 
			i++;
		}
	}
	
	if (ef->getOpacity().isTexture()) {
		COLLADAFW::Texture ctex = ef->getOpacity().getTexture();
		mtex = create_texture(ef, ctex, ma, i, texindex_texarray_map);
		if (mtex != NULL) {
			mtex->mapto = MAP_ALPHA;
			i++;
			ma->spectra = ma->alpha = 0;
			ma->mode |= MA_ZTRANSP | MA_TRANSP;
		}
	}
	// TRANSPARENT
	// color
#if 0
	if (ef->getOpacity().isColor()) {
		// XXX don't know what to do here
	}
	// texture
	else if (ef->getOpacity().isTexture()) {
		ctex = ef->getOpacity().getTexture();
		if (mtex != NULL) mtex->mapto &= MAP_ALPHA;
		else {
			mtex = create_texture(ef, ctex, ma, i, texindex_texarray_map);
			if (mtex != NULL) mtex->mapto = MAP_ALPHA;
		}
	}
#endif
	material_texture_mapping_map[ma] = texindex_texarray_map;
}
Example #4
0
/** When this method is called, the writer must write the light.
 * \return The writer should return true, if writing succeeded, false otherwise.*/
bool DocumentImporter::writeLight(const COLLADAFW::Light *light)
{
	if (mImportStage != General)
		return true;

	Lamp *lamp = NULL;
	std::string la_id, la_name;

	ExtraTags *et = getExtraTags(light->getUniqueId());
	/*TagsMap::iterator etit;
	ExtraTags *et = 0;
	etit = uid_tags_map.find(light->getUniqueId().toAscii());
	if (etit != uid_tags_map.end())
		et = etit->second;*/

	la_id = light->getOriginalId();
	la_name = light->getName();
	if (la_name.size()) lamp = (Lamp *)BKE_lamp_add(G.main, (char *)la_name.c_str());
	else lamp = (Lamp *)BKE_lamp_add(G.main, (char *)la_id.c_str());

	if (!lamp) {
		fprintf(stderr, "Cannot create lamp.\n");
		return true;
	}

	// if we find an ExtraTags for this, use that instead.
	if (et && et->isProfile("blender")) {
		et->setData("type", &(lamp->type));
		et->setData("flag", &(lamp->flag));
		et->setData("mode", &(lamp->mode));
		et->setData("gamma", &(lamp->k));
		et->setData("red", &(lamp->r));
		et->setData("green", &(lamp->g));
		et->setData("blue", &(lamp->b));
		et->setData("shadow_r", &(lamp->shdwr));
		et->setData("shadow_g", &(lamp->shdwg));
		et->setData("shadow_b", &(lamp->shdwb));
		et->setData("energy", &(lamp->energy));
		et->setData("dist", &(lamp->dist));
		et->setData("spotsize", &(lamp->spotsize));
		lamp->spotsize = DEG2RADF(lamp->spotsize);
		et->setData("spotblend", &(lamp->spotblend));
		et->setData("halo_intensity", &(lamp->haint));
		et->setData("att1", &(lamp->att1));
		et->setData("att2", &(lamp->att2));
		et->setData("falloff_type", &(lamp->falloff_type));
		et->setData("clipsta", &(lamp->clipsta));
		et->setData("clipend", &(lamp->clipend));
		et->setData("bias", &(lamp->bias));
		et->setData("soft", &(lamp->soft));
		et->setData("compressthresh", &(lamp->compressthresh));
		et->setData("bufsize", &(lamp->bufsize));
		et->setData("samp", &(lamp->samp));
		et->setData("buffers", &(lamp->buffers));
		et->setData("filtertype", &(lamp->filtertype));
		et->setData("bufflag", &(lamp->bufflag));
		et->setData("buftype", &(lamp->buftype));
		et->setData("ray_samp", &(lamp->ray_samp));
		et->setData("ray_sampy", &(lamp->ray_sampy));
		et->setData("ray_sampz", &(lamp->ray_sampz));
		et->setData("ray_samp_type", &(lamp->ray_samp_type));
		et->setData("area_shape", &(lamp->area_shape));
		et->setData("area_size", &(lamp->area_size));
		et->setData("area_sizey", &(lamp->area_sizey));
		et->setData("area_sizez", &(lamp->area_sizez));
		et->setData("adapt_thresh", &(lamp->adapt_thresh));
		et->setData("ray_samp_method", &(lamp->ray_samp_method));
		et->setData("shadhalostep", &(lamp->shadhalostep));
		et->setData("sun_effect_type", &(lamp->shadhalostep));
		et->setData("skyblendtype", &(lamp->skyblendtype));
		et->setData("horizon_brightness", &(lamp->horizon_brightness));
		et->setData("spread", &(lamp->spread));
		et->setData("sun_brightness", &(lamp->sun_brightness));
		et->setData("sun_size", &(lamp->sun_size));
		et->setData("backscattered_light", &(lamp->backscattered_light));
		et->setData("sun_intensity", &(lamp->sun_intensity));
		et->setData("atm_turbidity", &(lamp->atm_turbidity));
		et->setData("atm_extinction_factor", &(lamp->atm_extinction_factor));
		et->setData("atm_distance_factor", &(lamp->atm_distance_factor));
		et->setData("skyblendfac", &(lamp->skyblendfac));
		et->setData("sky_exposure", &(lamp->sky_exposure));
		et->setData("sky_colorspace", &(lamp->sky_colorspace));
	}
	else {
		float constatt = light->getConstantAttenuation().getValue();
		float linatt = light->getLinearAttenuation().getValue();
		float quadatt = light->getQuadraticAttenuation().getValue();
		float d = 25.0f;
		float att1 = 0.0f;
		float att2 = 0.0f;
		float e = 1.0f;

		if (light->getColor().isValid()) {
			COLLADAFW::Color col = light->getColor();
			lamp->r = col.getRed();
			lamp->g = col.getGreen();
			lamp->b = col.getBlue();
		}

		if (IS_EQ(linatt, 0.0f) && quadatt > 0.0f) {
			att2 = quadatt;
			d = sqrt(1.0f / quadatt);
		}
		// linear light
		else if (IS_EQ(quadatt, 0.0f) && linatt > 0.0f) {
			att1 = linatt;
			d = (1.0f / linatt);
		}
		else if (IS_EQ(constatt, 1.0f)) {
			att1 = 1.0f;
		}
		else {
			// assuming point light (const att = 1.0);
			att1 = 1.0f;
		}
		
		d *= (1.0f / unit_converter.getLinearMeter());

		lamp->energy = e;
		lamp->dist = d;

		COLLADAFW::Light::LightType type = light->getLightType();
		switch (type) {
			case COLLADAFW::Light::AMBIENT_LIGHT:
			{
				lamp->type = LA_HEMI;
			}
			break;
			case COLLADAFW::Light::SPOT_LIGHT:
			{
				lamp->type = LA_SPOT;
				lamp->att1 = att1;
				lamp->att2 = att2;
				if (IS_EQ(att1, 0.0f) && att2 > 0)
					lamp->falloff_type = LA_FALLOFF_INVSQUARE;
				if (IS_EQ(att2, 0.0f) && att1 > 0)
					lamp->falloff_type = LA_FALLOFF_INVLINEAR;
				lamp->spotsize = DEG2RADF(light->getFallOffAngle().getValue());
				lamp->spotblend = light->getFallOffExponent().getValue();
			}
			break;
			case COLLADAFW::Light::DIRECTIONAL_LIGHT:
			{
				/* our sun is very strong, so pick a smaller energy level */
				lamp->type = LA_SUN;
				lamp->mode |= LA_NO_SPEC;
			}
			break;
			case COLLADAFW::Light::POINT_LIGHT:
			{
				lamp->type = LA_LOCAL;
				lamp->att1 = att1;
				lamp->att2 = att2;
				if (IS_EQ(att1, 0.0f) && att2 > 0)
					lamp->falloff_type = LA_FALLOFF_INVSQUARE;
				if (IS_EQ(att2, 0.0f) && att1 > 0)
					lamp->falloff_type = LA_FALLOFF_INVLINEAR;
			}
			break;
			case COLLADAFW::Light::UNDEFINED:
			{
				fprintf(stderr, "Current lamp type is not supported.\n");
				lamp->type = LA_LOCAL;
			}
			break;
		}
	}

	this->uid_lamp_map[light->getUniqueId()] = lamp;
	this->FW_object_map[light->getUniqueId()] = light;
	return true;
}
/** Converts @a color to a max Point3. @a color must be valid.*/
Point3 toMaxPoint3( const COLLADAFW::Color& color )
{
    assert(color.isValid());
    return Point3(color.getRed(), color.getGreen(), color.getBlue());
}
    // --------------------------------------------
    void LightImporter::createLight ( 
        const COLLADAFW::Light* light,  
        MayaNode* mayaTransformNode )
    {
        // Check if the camera is already imported.
        const COLLADAFW::UniqueId& lightId = light->getUniqueId ();

        // Make the maya name unique and manage it in all necessary lists.
        String lightName = light->getName ();
        if ( lightName.empty () ) lightName = LIGHT_NAME;
        lightName = DocumentImporter::frameworkNameToMayaName ( lightName );
        const ExtraDataCallbackHandler& callbackHandler = getDocumentImporter ()->getMayaIdCallbackHandler ();
        String originalMayaId = getOriginalMayaId ( callbackHandler, lightId, COLLADASaxFWL15::HASH_ELEMENT_LIGHT );
        if ( !originalMayaId.empty () ) lightName = originalMayaId;
        lightName = generateUniqueDagNodeName ( lightName, mayaTransformNode );

        // Create a maya node object of the current node and push it into the map.
        MayaNode* mayaLightNode = new MayaNode ( lightId, lightName, mayaTransformNode );
        mMayaLightNodesMap [ lightId ] = mayaLightNode;

        FILE* file = getDocumentImporter ()->getFile ();

        MayaDM::Light* mayaLight = 0;
        COLLADAFW::Light::LightType lightType = light->getLightType ();
        switch ( lightType )
        {
        case COLLADAFW::Light::AMBIENT_LIGHT:
            {
                mayaLight = new MayaDM::AmbientLight ( file, lightName, mayaTransformNode->getNodePath () );
            }
            break;
        case COLLADAFW::Light::DIRECTIONAL_LIGHT:
            {
                mayaLight = new MayaDM::DirectionalLight ( file, lightName, mayaTransformNode->getNodePath () );
            }
            break;
        case COLLADAFW::Light::POINT_LIGHT:
            {
                mayaLight = new MayaDM::PointLight ( file, lightName, mayaTransformNode->getNodePath () );
                setPointLightAttributes ( light, mayaLight );
            }
            break;
        case COLLADAFW::Light::SPOT_LIGHT:
            {
                mayaLight = new MayaDM::SpotLight ( file, lightName, mayaTransformNode->getNodePath () );
                setSpotLightAttributes ( light, mayaLight );
            }
            break;
        default:
            std::cerr << "Unknown light type!" << std::endl;
            return;
        }

        // Add the original id attribute.
        String colladaId = light->getOriginalId ();
        if ( !colladaId.empty () )
        {
            MayaDM::addAttr ( file, COLLADA_ID_ATTRIBUTE_NAME, ATTRIBUTE_DATA_TYPE, ATTRIBUTE_TYPE_STRING );
            MayaDM::setAttr ( file, COLLADA_ID_ATTRIBUTE_NAME, ATTRIBUTE_TYPE, ATTRIBUTE_TYPE_STRING, colladaId );
        }
//         // TODO Add the attributes for all the extra tags.
//         setExtraData ( light->getExtraDataArray () );

        // Set the standard light color.
        COLLADAFW::Color color = light->getColor ();
        mayaLight->setColor ( MayaDM::float3 ( (float)color.getRed (), (float)color.getGreen (), (float)color.getBlue () ) );
        
        // Store the light in a map.
        appendLight ( lightId, mayaLight );
    }
	/** Converts @a color to a max color. @a color must be valid.*/
	Color toMaxColor( const COLLADAFW::Color& color )
	{
		assert(color.isValid());
		return Color(color.getRed(), color.getGreen(), color.getBlue());
	}