Quellcode durchsuchen

Added an importance property to lights. You can use it to make distant lights override closer ones.

Ivan Safrin vor 14 Jahren
Ursprung
Commit
6cb1164fc4

+ 10 - 2
Core/Contents/Include/PolyRenderer.h

@@ -54,13 +54,20 @@ namespace Polycode {
 			bool shadowsEnabled;
 			Matrix4 textureMatrix;
 			Texture* shadowMapTexture;
+			int lightImportance;
 	};
 
 	class _PolyExport LightSorter {
 		public:
 			Vector3 basePosition;
 			Matrix4 cameraMatrix;
-			bool operator() (LightInfo i,LightInfo j) { return ((cameraMatrix*i.position).distance(basePosition)<(cameraMatrix*j.position).distance(basePosition));}
+			bool operator() (LightInfo i,LightInfo j) {
+				if(i.lightImportance > j.lightImportance)
+					return true;
+				if(i.lightImportance == j.lightImportance)
+					return ((cameraMatrix*i.position).distance(basePosition)<(cameraMatrix*j.position).distance(basePosition));
+				return false; 
+			}
 	};
 
 	/**
@@ -177,7 +184,7 @@ namespace Polycode {
 		virtual void cullFrontFaces(bool val) = 0;
 		
 		void clearLights();
-		void addLight(Vector3 position, Vector3 direction, int type, Color color, Color specularColor, Number constantAttenuation, Number linearAttenuation, Number quadraticAttenuation, Number intensity, Number spotlightCutoff, Number spotlightExponent, bool shadowsEnabled, Matrix4 *textureMatrix, Texture *shadowMapTexture);
+		void addLight(int lightImportance, Vector3 position, Vector3 direction, int type, Color color, Color specularColor, Number constantAttenuation, Number linearAttenuation, Number quadraticAttenuation, Number intensity, Number spotlightCutoff, Number spotlightExponent, bool shadowsEnabled, Matrix4 *textureMatrix, Texture *shadowMapTexture);
 		
 		void setExposureLevel(Number level);
 		
@@ -231,6 +238,7 @@ namespace Polycode {
 		vector<LightInfo> getSpotLights() { return spotLights;	}
 		
 	protected:
+		LightSorter sorter;	
 	
 		bool cullingFrontFaces;
 				

+ 5 - 0
Core/Contents/Include/PolySceneLight.h

@@ -183,6 +183,9 @@ namespace Polycode {
 			* If set to true, draws a wireframe primitive visualizing the light.
 			*/
 			void enableDebugDraw(bool val);
+			
+			void setLightImportance(int newImportance);
+			int getLightImportance();
 		
 			SceneEntity *lightShape;
 			
@@ -191,6 +194,8 @@ namespace Polycode {
 			Number spotlightExponent;
 			Number spotlightCutoff;
 		
+			int lightImportance;
+		
 			Number constantAttenuation;
 			Number linearAttenuation;
 			Number quadraticAttenuation;

+ 8 - 0
Core/Contents/Source/PolyGLSLShaderModule.cpp

@@ -394,6 +394,14 @@ bool GLSLShaderModule::applyShaderMaterial(Renderer *renderer, Material *materia
 	int lightIndex = 0;
 	
 	vector<LightInfo> areaLights = renderer->getAreaLights();
+	
+//	printf("Applying {\n");
+//	for(int z=0;z < areaLights.size(); z++) {
+//		LightInfo light = areaLights[z];		
+//		printf("Light: %f %f %f\n", light.position.x, light.position.y, light.position.z);
+//	}
+//	printf("}\n");
+		
 	GLfloat ambientVal[] = {1, 1, 1, 1.0};				
 	for(int i=0; i < glslShader->numAreaLights; i++) {
 		LightInfo light;

+ 2 - 2
Core/Contents/Source/PolyRenderer.cpp

@@ -125,14 +125,13 @@ void Renderer::addShaderModule(PolycodeShaderModule *module) {
 
 void Renderer::sortLights(){
 
-	LightSorter sorter;
 	sorter.basePosition = (getModelviewMatrix()).getPosition();
 	sorter.cameraMatrix = getCameraMatrix().inverse();	
 	sort (areaLights.begin(), areaLights.end(), sorter);
 	sort (spotLights.begin(), spotLights.end(), sorter);	
 }
 
-void Renderer::addLight(Vector3 position, Vector3 direction, int type, Color color, Color specularColor, Number constantAttenuation, Number linearAttenuation, Number quadraticAttenuation, Number intensity, Number spotlightCutoff, Number spotlightExponent, bool shadowsEnabled, Matrix4 *textureMatrix,Texture *shadowMapTexture) {
+void Renderer::addLight(int lightImportance, Vector3 position, Vector3 direction, int type, Color color, Color specularColor, Number constantAttenuation, Number linearAttenuation, Number quadraticAttenuation, Number intensity, Number spotlightCutoff, Number spotlightExponent, bool shadowsEnabled, Matrix4 *textureMatrix,Texture *shadowMapTexture) {
 
 	numLights++;
 	
@@ -141,6 +140,7 @@ void Renderer::addLight(Vector3 position, Vector3 direction, int type, Color col
 		info.textureMatrix = *textureMatrix;
 	}
 	
+	info.lightImportance = lightImportance;
 	info.shadowMapTexture = shadowMapTexture;
 	info.shadowsEnabled = shadowsEnabled;
 	info.spotlightCutoff = spotlightCutoff;

+ 4 - 3
Core/Contents/Source/PolyScene.cpp

@@ -160,9 +160,10 @@ void Scene::Render(Camera *targetCamera) {
 	CoreServices::getInstance()->getRenderer()->clearLights();
 	
 	for(int i=0; i < lights.size(); i++) {
-		
 		SceneLight *light = lights[i];
-		
+		if(!light->enabled)
+			continue;
+			
 		Vector3 direction;
 		Vector3 position;
 		matrixPtr = NULL;				
@@ -197,7 +198,7 @@ void Scene::Render(Camera *targetCamera) {
 		if(light->getParentEntity() != NULL) {
 			position = light->getParentEntity()->getConcatenatedMatrix() * position;			
 		}
-		CoreServices::getInstance()->getRenderer()->addLight(position, direction, light->getLightType(), light->lightColor, light->specularLightColor, light->getConstantAttenuation(), light->getLinearAttenuation(), light->getQuadraticAttenuation(), light->getIntensity(), light->getSpotlightCutoff(), light->getSpotlightExponent(), light->areShadowsEnabled(), matrixPtr, shadowMapTexture);
+		CoreServices::getInstance()->getRenderer()->addLight(light->getLightImportance(), position, direction, light->getLightType(), light->lightColor, light->specularLightColor, light->getConstantAttenuation(), light->getLinearAttenuation(), light->getQuadraticAttenuation(), light->getIntensity(), light->getSpotlightCutoff(), light->getSpotlightExponent(), light->areShadowsEnabled(), matrixPtr, shadowMapTexture);
 	}	
 	
 	targetCamera->doCameraTransform();

+ 11 - 0
Core/Contents/Source/PolySceneLight.cpp

@@ -66,8 +66,19 @@ SceneLight::SceneLight(int type, Scene *parentScene, Number intensity, Number co
 	*/
 	
 	lightShape = NULL;
+	
+	lightImportance = 0;
+}
+
+void SceneLight::setLightImportance(int newImportance) {
+	lightImportance = newImportance;
 }
 
+int SceneLight::getLightImportance() {
+	return lightImportance;
+}
+
+
 void SceneLight::enableDebugDraw(bool val) {
 	if(lightShape) {
 		lightShape->visible = val;