Browse Source

Changes in the lights

Panagiotis Christopoulos Charitos 15 years ago
parent
commit
66ea24a160

File diff suppressed because it is too large
+ 395 - 392
build/debug/Makefile


+ 4 - 2
docs/doxyfile

@@ -1278,8 +1278,10 @@ INCLUDE_FILE_PATTERNS  = *.h \
 # undefined via #undef or recursively expanded use the := operator 
 # instead of the = operator.
 
-PREDEFINED             = "PROPERTY_RW(_v0_,_v1_,_v2_,_v3_):=_v0_ _v1_;" \
-                         "PROPERTY_R(_v0_,_v1_,_v2_):=_v0_ _v1_;"
+PREDEFINED             = "PROPERTY_RW(_v0_,_v1_,_v2_,_v3_):=private: _v0_ _v1_;" \
+                         "PROPERTY_R(_v0_,_v1_,_v2_):= private: _v0_ _v1_; \
+                                                       public: const _v0_& _v2_() const; /**< Property getter */ \
+                                                       private: _v0_ _v1_;"
 
 # If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then 
 # this tag can be used to specify a list of macro names that should be expanded. 

+ 2 - 0
src/Main.cpp

@@ -11,6 +11,8 @@
 #include "App.h"
 #include "Mesh.h"
 #include "Light.h"
+#include "PointLight.h"
+#include "SpotLight.h"
 #include "collision.h"
 #include "Material.h"
 #include "Resource.h"

+ 22 - 20
src/Renderer/Is.cpp

@@ -3,6 +3,8 @@
 #include "Renderer.h"
 #include "Camera.h"
 #include "Light.h"
+#include "PointLight.h"
+#include "SpotLight.h"
 #include "LightData.h"
 #include "App.h"
 #include "Scene.h"
@@ -159,7 +161,10 @@ void Is::pointLightPass(const PointLight& light)
 
 	// frustum test
 	bsphere_t sphere(light.getWorldTransform().origin, light.getRadius());
-	if(!cam.insideFrustum(sphere)) return;
+	if(!cam.insideFrustum(sphere))
+	{
+		return;
+	}
 
 	// stencil optimization
 	smo.run(light);
@@ -176,8 +181,8 @@ void Is::pointLightPass(const PointLight& light)
 	Vec3 lightPosEyeSpace = light.getWorldTransform().origin.getTransformed(cam.getViewMatrix());
 	shader.findUniVar("lightPos")->setVec3(&lightPosEyeSpace);
 	shader.findUniVar("lightRadius")->setFloat(light.getRadius());
-	shader.findUniVar("lightDiffuseCol")->setVec3(&light.lightData->getDiffuseColor());
-	shader.findUniVar("lightSpecularCol")->setVec3(&light.lightData->getSpecularColor());
+	shader.findUniVar("lightDiffuseCol")->setVec3(&light.lightData->getDiffuseCol());
+	shader.findUniVar("lightSpecularCol")->setVec3(&light.lightData->getSpecularCol());
 
 	// render quad
 	glEnableVertexAttribArray(0);
@@ -201,12 +206,15 @@ void Is::spotLightPass(const SpotLight& light)
 	const Camera& cam = r.getCamera();
 
 	// frustum test
-	if(!cam.insideFrustum(light.camera)) return;
+	if(!cam.insideFrustum(light.getCamera()))
+	{
+		return;
+	}
 
 	// shadow mapping
-	if(light.castsShadow && sm.isEnabled())
+	if(light.castsShadow() && sm.isEnabled())
 	{
-		sm.run(light.camera);
+		sm.run(light.getCamera());
 
 		// restore the IS FBO
 		fbo.bind();
@@ -222,18 +230,12 @@ void Is::spotLightPass(const SpotLight& light)
 	smo.run(light);
 
 	// set the texture
-	if(light.lightData->getTexture() == NULL)
-	{
-		ERROR("No texture is attached to the light. lightProps name: " << light.lightData->getRsrcName());
-		return;
-	}
-
-	light.lightData->getTexture()->setRepeat(false);
+	light.lightData->getTexture().setRepeat(false);
 
 	// shader prog
 	const ShaderProg* shdr;
 
-	if(light.castsShadow && sm.isEnabled())
+	if(light.castsShadow() && sm.isEnabled())
 	{
 		shdr = spotLightShadowSProg.get();
 	}
@@ -257,20 +259,20 @@ void Is::spotLightPass(const SpotLight& light)
 	Vec3 lightPosEyeSpace = light.getWorldTransform().origin.getTransformed(cam.getViewMatrix());
 	shdr->findUniVar("lightPos")->setVec3(&lightPosEyeSpace);
 	shdr->findUniVar("lightRadius")->setFloat(light.getDistance());
-	shdr->findUniVar("lightDiffuseCol")->setVec3(&light.lightData->getDiffuseColor());
-	shdr->findUniVar("lightSpecularCol")->setVec3(&light.lightData->getSpecularColor());
-	shdr->findUniVar("lightTex")->setTexture(*light.lightData->getTexture(), 4);
+	shdr->findUniVar("lightDiffuseCol")->setVec3(&light.lightData->getDiffuseCol());
+	shdr->findUniVar("lightSpecularCol")->setVec3(&light.lightData->getSpecularCol());
+	shdr->findUniVar("lightTex")->setTexture(light.lightData->getTexture(), 4);
 
 	// set texture matrix for texture & shadowmap projection
 	// Bias * P_light * V_light * inv(V_cam)
 	static Mat4 biasMat4(0.5, 0.0, 0.0, 0.5, 0.0, 0.5, 0.0, 0.5, 0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0, 1.0);
 	Mat4 texProjectionMat;
-	texProjectionMat = biasMat4 * light.camera->getProjectionMatrix() *
-	                   Mat4::combineTransformations(light.camera->getViewMatrix(), Mat4(cam.getWorldTransform()));
+	texProjectionMat = biasMat4 * light.getCamera().getProjectionMatrix() *
+	                   Mat4::combineTransformations(light.getCamera().getViewMatrix(), Mat4(cam.getWorldTransform()));
 	shdr->findUniVar("texProjectionMat")->setMat4(&texProjectionMat);
 
 	// the shadowmap
-	if(light.castsShadow && sm.isEnabled())
+	if(light.castsShadow() && sm.isEnabled())
 	{
 		shdr->findUniVar("shadowMap")->setTexture(sm.shadowMap, 5);
 	}

+ 3 - 1
src/Renderer/Smo.cpp

@@ -2,6 +2,8 @@
 #include "Renderer.h"
 #include "Light.h"
 #include "LightData.h"
+#include "PointLight.h"
+#include "SpotLight.h"
 #include "Camera.h"
 
 
@@ -78,7 +80,7 @@ void Smo::run(const SpotLight& light)
 	glDisable(GL_CULL_FACE);
 
 	// calc camera shape
-	const Camera& lcam = light.camera;
+	const Camera& lcam = light.getCamera();
 	float x = lcam.getZFar() / tan((PI-lcam.getFovX())/2);
 	float y = tan(lcam.getFovY()/2) * lcam.getZFar();
 	float z = -lcam.getZFar();

+ 13 - 0
src/Resources/LightData.cpp

@@ -33,6 +33,8 @@ bool LightData::load(const char* filename)
 
 	const Scanner::Token* token;
 
+	type = LT_POINT;
+
 	while(true)
 	{
 		token = &scanner.getNextToken();
@@ -84,6 +86,7 @@ bool LightData::load(const char* filename)
 
 			distance = (token->getDataType() == Scanner::DT_FLOAT) ? token->getValue().getFloat() :
 			                                                         float(token->getValue().getInt());
+			type = LT_SPOT;
 		}
 		// FOV_X
 		else if(token->getCode() == Scanner::TC_IDENTIFIER && !strcmp(token->getValue().getString(), "FOV_X"))
@@ -97,6 +100,7 @@ bool LightData::load(const char* filename)
 
 			fovX = (token->getDataType() == Scanner::DT_FLOAT) ? token->getValue().getFloat() :
 			                                                     float(token->getValue().getInt());
+			type = LT_SPOT;
 		}
 		// FOV_Y
 		else if(token->getCode() == Scanner::TC_IDENTIFIER && !strcmp(token->getValue().getString(), "FOV_Y"))
@@ -110,6 +114,7 @@ bool LightData::load(const char* filename)
 
 			fovY = (token->getDataType() == Scanner::DT_FLOAT) ? token->getValue().getFloat() :
 			                                                     float(token->getValue().getInt());
+			type = LT_SPOT;
 		}
 		// TEXTURE
 		else if(token->getCode() == Scanner::TC_IDENTIFIER && !strcmp(token->getValue().getString(), "TEXTURE"))
@@ -126,6 +131,7 @@ bool LightData::load(const char* filename)
 			texture->setAnisotropy(0);
 			texture->setTexParameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 			texture->setTexParameter(GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+			type = LT_SPOT;
 		}
 		// end of file
 		else if(token->getCode() == Scanner::TC_EOF)
@@ -140,5 +146,12 @@ bool LightData::load(const char* filename)
 		}
 	}
 	
+	// sanity checks
+	if(type == LT_SPOT && texture.get() == NULL)
+	{
+		ERROR("Spot light should have texture");
+		return false;
+	}
+
 	return true;
 }

+ 33 - 11
src/Resources/LightData.h

@@ -11,29 +11,51 @@
 /// Light properties Resource
 class LightData: public Resource
 {
-	PROPERTY_R(Vec3, diffuseCol, getDiffuseColor)
-	PROPERTY_R(Vec3, specularCol, getSpecularColor)
-	PROPERTY_R(float, radius, getRadius) ///< For point lights
-	PROPERTY_R(bool, castsShadow_, castsShadow) ///< For spot lights
-	PROPERTY_R(float, distance, getDistance) ///< For spot lights. A.K.A.: camera's zFar
-	PROPERTY_R(float, fovX, getFovX) ///< For spot lights
-	PROPERTY_R(float, fovY, getFovY) ///< For spot lights
+	public:
+		enum LightType
+		{
+			LT_POINT,
+			LT_SPOT
+		};
+
+	/// @name Common light properties
+	/// @{
+	PROPERTY_R(Vec3, diffuseCol, getDiffuseCol)
+	PROPERTY_R(Vec3, specularCol, getSpecularCol)
+	PROPERTY_R(bool, castsShadow_, castsShadow) ///< Currently only for spot lights
+	PROPERTY_R(LightType, type, getType)
+	/// @}
+
+	/// @name Point light properties
+	/// @{
+	PROPERTY_R(float, radius, getRadius) ///< Sphere radius
+	/// @}
+
+	/// @name Spot light properties
+	/// @{
+	PROPERTY_R(float, distance, getDistance) ///< AKA camera's zFar
+	PROPERTY_R(float, fovX, getFovX)
+	PROPERTY_R(float, fovY, getFovY)
+	/// @}
 		
 	public:
 		LightData();
 		~LightData() {}
 		bool load(const char* filename);
-		const Texture* getTexture() const;
+		const Texture& getTexture() const;
 
 	private:
-		RsrcPtr<Texture> texture; ///< For spot lights
+		/// @name Spot light properties
+		/// @{
+		RsrcPtr<Texture> texture;
+		/// @}
 };
 
 
-inline const Texture* LightData::getTexture() const
+inline const Texture& LightData::getTexture() const
 {
 	DEBUG_ERR(texture.get() == NULL);
-	return texture.get();
+	return *texture;
 }
 
 

+ 22 - 21
src/Resources/Texture.h

@@ -6,14 +6,12 @@
 #include "Resource.h"
 
 
-/**
- * Texture resource class
- *
- * It loads or creates an image and then loads it in the GPU. Its an OpenGL container. It supports compressed and
- * uncompressed TGAs and all formats of PNG (PNG loading comes through SDL_image)
- *
- * @note The last texture unit is reserved and you cannot use it
- */
+/// Texture resource class
+///
+/// It loads or creates an image and then loads it in the GPU. Its an OpenGL container. It supports compressed and
+/// uncompressed TGAs and all formats of PNG (PNG loading comes through SDL_image)
+///
+/// @note The last texture unit is reserved and you cannot use it
 class Texture: public Resource
 {
 	friend class Renderer; /// @todo Remove this when remove the SSAO load noise map crap
@@ -21,22 +19,27 @@ class Texture: public Resource
 	friend class MainRenderer;
 
 	public:
-		 Texture();
-		~Texture();
+		enum TextureFilteringType
+		{
+			TFT_NEAREST,
+			TFT_LINEAR,
+			TFT_TRILINEAR
+		};
 
+		Texture();
+		~Texture();
 		uint getGlId() const;
 
-		/**
-		 * @name Create tex funcs
-		 */
-		/**@{*/
+		/// @name Create tex funcs
+		/// @{
 		bool load(const char* filename);
 		bool createEmpty2D(float width, float height, int internalFormat, int format, uint type_);
 		bool createEmpty2DMsaa(int samplesNum, int internalFormat, int width_, int height_, bool mimapping);
-		/**@}*/
+		/// @}
 
 		void bind(uint texUnit = 0) const;
 		void setRepeat(bool repeat) const;
+		void setFiltering(TextureFilteringType filterType);
 		void setTexParameter(uint paramName, int value) const;
 		void setTexParameter(uint paramName, float value) const;
 		void setAnisotropy(uint level);
@@ -53,16 +56,14 @@ class Texture: public Resource
 		uint glId; ///< Identification for OGL
 		uint target; ///< GL_TEXTURE_2D, GL_TEXTURE_3D... etc
 
-		/**
-		 * @name Variables set by the renderer
-		 * Set upon OpenGL initialization
-		 */
-		/**@{*/
+		/// @name Variables set by the renderer
+		/// Set upon OpenGL initialization
+		/// @{
 		static int  textureUnitsNum;
 		static bool mipmappingEnabled;
 		static bool compressionEnabled;
 		static int  anisotropyLevel;
-		/**@}*/
+		/// @}
 
 		bool isLoaded() const;
 };

+ 6 - 21
src/Scene/Light.cpp

@@ -6,29 +6,15 @@
 
 
 //======================================================================================================================
-// init [PointLight]                                                                                                  =
+// init                                                                                                                =
 //======================================================================================================================
-void PointLight::init(const char* filename)
+void Light::init(const char* filename)
 {
 	lightData.loadRsrc(filename);
-	radius = lightData->getRadius();
-}
-
-
-//======================================================================================================================
-// init [SpotLight]                                                                                                    =
-//======================================================================================================================
-void SpotLight::init(const char* filename)
-{
-	lightData.loadRsrc(filename);
-	camera->setAll(lightData->getFovX(), lightData->getFovY(), 0.2, lightData->getDistance());
-	castsShadow = lightData->castsShadow();
 
-	if(lightData->getTexture() == NULL)
-	{
-		ERROR("Light properties \"" << lightData->getRsrcName() << "\" do not have a texture");
-		return;
-	}
+	diffuseCol = lightData->getDiffuseCol();
+	specularCol = lightData->getSpecularCol();
+	castsShadow_ = lightData->castsShadow();
 }
 
 
@@ -37,7 +23,6 @@ void SpotLight::init(const char* filename)
 //======================================================================================================================
 void Light::render()
 {
-	app->getMainRenderer().dbg.drawSphere(0.1, getWorldTransform(), Vec4(lightData->getDiffuseColor(), 1.0));
-	//Dbg::drawSphere(0.1, Transform::getIdentity(), Vec4(lightProps->getDiffuseColor(), 1.0));
+	app->getMainRenderer().dbg.drawSphere(0.1, getWorldTransform(), Vec4(lightData->getDiffuseCol(), 1.0));
 }
 

+ 11 - 68
src/Scene/Light.h

@@ -14,8 +14,8 @@ Specular intensity of light:    Sl
 Specular intensity of material: Sm
 */
 
-#ifndef _LIGHT_H_
-#define _LIGHT_H_
+#ifndef LIGHT_H
+#define LIGHT_H
 
 #include "Common.h"
 #include "Texture.h"
@@ -25,7 +25,7 @@ Specular intensity of material: Sm
 #include "LightData.h"
 
 
-/// Light scene node (Abstract)
+/// Light scene node. It can be spot or point
 class Light: public SceneNode
 {
 	public:
@@ -37,84 +37,27 @@ class Light: public SceneNode
 
 	PROPERTY_R(LightType, type, getType) ///< Light type
 
-	/// @name Copies of some of the resource properties
+	/// @name Copies of some of the resource properties. The others are camera properties or not changeable
 	/// @{
-	PROPERTY_RW(Vec3, diffuseCol, setDiffuseColor, getDiffuseColor)
-	PROPERTY_RW(Vec3, specularCol, setSpecularColor, getSpecularColor)
-	PROPERTY_RW(float, radius, setRadius, getRadius)
+	PROPERTY_RW(Vec3, diffuseCol, setDiffuseCo, getDiffuseCol)
+	PROPERTY_RW(Vec3, specularCol, setSpecularCol, getSpecularCol)
 	PROPERTY_RW(bool, castsShadow_, setCastsShadow, castsShadow)
-	PROPERTY_RW(float, distance, setDistance, getDistance)
-	PROPERTY_RW(float, fovX, setFovX, getFovX)
-	PROPERTY_RW(float, fovY, setFovY, getFovY)
 	/// @}
 
 	public:
 		RsrcPtr<LightData> lightData;
 	
-		Light(LightType type_);
+		Light(LightType type, SceneNode* parent = NULL);
+		~Light() {}
 		void render();
-
-	private:
-		Camera* camera;
-};
-
-
-/// PointLight scene node
-class PointLight: public Light
-{
-	PROPERTY_RW(float, radius, setRadius, getRadius)
-
-	public:
-		PointLight();
-		void init(const char*);
-};
-
-
-/// SpotLight scene node
-class SpotLight: public Light
-{
-	public:
-		Camera* camera;
-		bool castsShadow;
-
-		SpotLight();
-		float getDistance() const;
-		void setDistance(float d);
-		void init(const char*);
+		void init(const char* filename);
 };
 
 
-//======================================================================================================================
-// Inlines                                                                                                             =
-//======================================================================================================================
-
-inline Light::Light(LightType type_):
-	SceneNode(SNT_LIGHT),
+inline Light::Light(LightType type_, SceneNode* parent):
+	SceneNode(SNT_LIGHT, parent),
 	type(type_)
 {}
 
 
-inline PointLight::PointLight():
-	Light(LT_POINT)
-{}
-
-
-inline SpotLight::SpotLight():
-	Light(LT_SPOT),
-	camera(new Camera(this)),
-	castsShadow(false)
-{}
-
-
-inline float SpotLight::getDistance() const
-{
-	return camera->getZFar();
-}
-
-
-inline void SpotLight::setDistance(float d)
-{
-	camera->setZFar(d);
-}
-
 #endif

+ 31 - 0
src/Scene/PointLight.h

@@ -0,0 +1,31 @@
+#ifndef POINT_LIGHT_H
+#define POINT_LIGHT_H
+
+#include "Common.h"
+#include "Light.h"
+
+
+/// Point light. Defined by its radius
+class PointLight: public Light
+{
+	PROPERTY_RW(float, radius, setRadius, getRadius)
+
+	public:
+		PointLight(SceneNode* parent = NULL): Light(LT_POINT, parent) {}
+		void init(const char* filename);
+};
+
+
+inline void PointLight::init(const char* filename)
+{
+	Light::init(filename);
+	if(lightData->getType() != LightData::LT_POINT)
+	{
+		ERROR("Light data is wrong type");
+		return;
+	}
+	radius = lightData->getRadius();
+}
+
+
+#endif

+ 17 - 0
src/Scene/SpotLight.cpp

@@ -0,0 +1,17 @@
+#include "SpotLight.h"
+
+
+//======================================================================================================================
+// init                                                                                                                =
+//======================================================================================================================
+void SpotLight::init(const char* filename)
+{
+	Light::init(filename);
+	if(lightData->getType() != LightData::LT_SPOT)
+	{
+		ERROR("Light data is wrong type");
+		return;
+	}
+	camera = new Camera(this);
+	camera->setAll(lightData->getFovX(), lightData->getFovY(), 0.02, lightData->getDistance());
+}

+ 34 - 0
src/Scene/SpotLight.h

@@ -0,0 +1,34 @@
+#ifndef SPOT_LIGHT_H
+#define SPOT_LIGHT_H
+
+#include "Common.h"
+#include "Light.h"
+
+
+/// Spot light
+class SpotLight: public Light
+{
+	public:
+		SpotLight(SceneNode* parent = NULL): Light(LT_SPOT, parent) {}
+		~SpotLight() {}
+		void init(const char* filename);
+
+		/// @name Setters & getters
+		/// @{
+		float getDistance() const {return camera->getZFar();}
+		void setDistance(float d) {camera->setZFar(d);}
+		float getFovX() const {return camera->getFovX();}
+		void setFovX(float f) {camera->setFovX(f);}
+		float getFovY() const {return camera->getFovY();}
+		void setFovY(float f) {camera->setFovY(f);}
+		const Texture& getTexture() const {return lightData->getTexture();}
+		const Camera& getCamera() const {return *camera;}
+		Camera& getCamera() {return *camera;}
+		/// @}
+
+	private:
+		Camera* camera;
+};
+
+
+#endif

Some files were not shown because too many files changed in this diff