[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [32468] trunk/blender/source/blender/ collada: Fix [#24201] COLLADA Exporter: Light source energy incorrect

Nathan Letwory nathan at letworyinteractive.com
Thu Oct 14 11:40:56 CEST 2010


Revision: 32468
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=32468
Author:   jesterking
Date:     2010-10-14 11:40:56 +0200 (Thu, 14 Oct 2010)

Log Message:
-----------
Fix [#24201] COLLADA Exporter: Light source energy incorrect

lamp->energy and lamp->distance are now taken in account by calculating
the constant, linear and quadratic attenuations based on this.

The import tries to do the reverse.

Note: this will work only properly for lamps that have att1 and att2 set to
1.0 or 0.0, other lamptypes won't import correctly again.

Modified Paths:
--------------
    trunk/blender/source/blender/collada/DocumentImporter.cpp
    trunk/blender/source/blender/collada/LightExporter.cpp

Modified: trunk/blender/source/blender/collada/DocumentImporter.cpp
===================================================================
--- trunk/blender/source/blender/collada/DocumentImporter.cpp	2010-10-14 09:31:14 UTC (rev 32467)
+++ trunk/blender/source/blender/collada/DocumentImporter.cpp	2010-10-14 09:40:56 UTC (rev 32468)
@@ -29,9 +29,6 @@
 #include <map>
 #include <algorithm> // sort()
 
-#include <math.h>
-#include <float.h>
-
 #include "COLLADAFWRoot.h"
 #include "COLLADAFWIWriter.h"
 #include "COLLADAFWStableHeaders.h"
@@ -67,6 +64,7 @@
 #include "BKE_image.h"
 
 #include "BLI_listbase.h"
+#include "BLI_math.h"
 #include "BLI_string.h"
 
 #include "DNA_camera_types.h"
@@ -841,6 +839,38 @@
 			lamp->g = col.getGreen();
 			lamp->b = col.getBlue();
 		}
+		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/constatt;
+		
+		/* NOTE: We assume for now that inv square is used for quadratic light
+		 * and inv linear for linear light. Exported blender lin/quad weighted
+		 * most likely will result in wrong import. */
+		/* quadratic light */
+		if(IS_EQ(linatt, 0.0f) && quadatt > 0.0f) {
+			//quadatt = att2/(d*d*(e*2));
+			float invquadatt = 1.0f/quadatt;
+			float d2 = invquadatt / (2 * e);
+			d = sqrtf(d2);
+		}
+		// linear light
+		else if(IS_EQ(quadatt, 0.0f) && linatt > 0.0f) {
+			//linatt = att1/(d*e);
+			float invlinatt = 1.0f/linatt;
+			d = invlinatt / e;
+		} else {
+			printf("no linear nor quad light, using defaults for attenuation, import will be incorrect: Lamp %s\n", lamp->id.name);
+			att2 = 1.0f;
+		}
+		
+		lamp->dist = d;
+		lamp->energy = e;
+		
 		COLLADAFW::Light::LightType type = light->getLightType();
 		switch(type) {
 		case COLLADAFW::Light::AMBIENT_LIGHT:
@@ -851,9 +881,9 @@
 		case COLLADAFW::Light::SPOT_LIGHT:
 			{
 				lamp->type = LA_SPOT;
-				lamp->falloff_type = LA_FALLOFF_SLIDERS;
-				lamp->att1 = light->getLinearAttenuation().getValue();
-				lamp->att2 = light->getQuadraticAttenuation().getValue();
+				lamp->falloff_type = LA_FALLOFF_INVSQUARE;
+				lamp->att1 = att1;
+				lamp->att2 = att2;
 				lamp->spotsize = light->getFallOffAngle().getValue();
 				lamp->spotblend = light->getFallOffExponent().getValue();
 			}
@@ -866,8 +896,9 @@
 		case COLLADAFW::Light::POINT_LIGHT:
 			{
 				lamp->type = LA_LOCAL;
-				lamp->att1 = light->getLinearAttenuation().getValue();
-				lamp->att2 = light->getQuadraticAttenuation().getValue();
+				lamp->falloff_type = LA_FALLOFF_INVSQUARE;
+				lamp->att1 = att1;
+				lamp->att2 = att2;
 			}
 			break;
 		case COLLADAFW::Light::UNDEFINED:

Modified: trunk/blender/source/blender/collada/LightExporter.cpp
===================================================================
--- trunk/blender/source/blender/collada/LightExporter.cpp	2010-10-14 09:31:14 UTC (rev 32467)
+++ trunk/blender/source/blender/collada/LightExporter.cpp	2010-10-14 09:40:56 UTC (rev 32468)
@@ -30,6 +30,8 @@
 
 #include "DNA_lamp_types.h"
 
+#include "BLI_math.h"
+
 #include "LightExporter.h"
 #include "collada_internal.h"
 
@@ -48,6 +50,7 @@
 }
 
 LightsExporter::LightsExporter(COLLADASW::StreamWriter *sw): COLLADASW::LibraryLights(sw){}
+
 void LightsExporter::exportLights(Scene *sce)
 {
 	openLibrary();
@@ -62,18 +65,45 @@
 	std::string la_id(get_light_id(ob));
 	std::string la_name(id_name(la));
 	COLLADASW::Color col(la->r, la->g, la->b);
-	float e = la->energy;
+	float att1, att2;
+	float e, d, constatt, linatt, quadatt;
+	att1 = att2 = 0.0f;
 	
+	if(la->falloff_type==LA_FALLOFF_INVLINEAR) {
+		att1 = 1.0f;
+		att2 = 0.0f;
+	}
+	else if(la->falloff_type==LA_FALLOFF_INVSQUARE) {
+		att1 = 0.0f;
+		att2 = 1.0f;
+	}
+	else if(la->falloff_type==LA_FALLOFF_SLIDERS) {
+		att1 = la->att1;
+		att2 = la->att2;
+	}
+	
+	e = la->energy;
+	d = la->dist;
+	
+	constatt = linatt = quadatt = MAXFLOAT;
+	if(e > 0.0f) {
+		constatt = 1.0f/e;
+		linatt = att1/(d*e);
+		quadatt = att2/(d*d*(e*2));
+	}
+	
 	// sun
 	if (la->type == LA_SUN) {
 		COLLADASW::DirectionalLight cla(mSW, la_id, la_name, e);
 		cla.setColor(col);
+		cla.setConstantAttenuation(constatt);
 		addLight(cla);
 	}
 	// hemi
 	else if (la->type == LA_HEMI) {
 		COLLADASW::AmbientLight cla(mSW, la_id, la_name, e);
 		cla.setColor(col);
+		cla.setConstantAttenuation(constatt);
 		addLight(cla);
 	}
 	// spot
@@ -82,16 +112,18 @@
 		cla.setColor(col);
 		cla.setFallOffAngle(la->spotsize);
 		cla.setFallOffExponent(la->spotblend);
-		cla.setLinearAttenuation(la->att1);
-		cla.setQuadraticAttenuation(la->att2);
+		cla.setConstantAttenuation(constatt);
+		cla.setLinearAttenuation(linatt);
+		cla.setQuadraticAttenuation(quadatt);
 		addLight(cla);
 	}
 	// lamp
 	else if (la->type == LA_LOCAL) {
 		COLLADASW::PointLight cla(mSW, la_id, la_name, e);
 		cla.setColor(col);
-		cla.setLinearAttenuation(la->att1);
-		cla.setQuadraticAttenuation(la->att2);
+		cla.setConstantAttenuation(constatt);
+		cla.setLinearAttenuation(linatt);
+		cla.setQuadraticAttenuation(quadatt);
 		addLight(cla);
 	}
 	// area lamp is not supported
@@ -99,8 +131,9 @@
 	else {
 		COLLADASW::PointLight cla(mSW, la_id, la_name, e);
 		cla.setColor(col);
-		cla.setLinearAttenuation(la->att1);
-		cla.setQuadraticAttenuation(la->att2);
+		cla.setConstantAttenuation(constatt);
+		cla.setLinearAttenuation(linatt);
+		cla.setQuadraticAttenuation(quadatt);
 		addLight(cla);
 	}
 }





More information about the Bf-blender-cvs mailing list