]> git.tdb.fi Git - libs/gl.git/blobdiff - source/light.h
Do not clear previous assignments on assignment to array subscript
[libs/gl.git] / source / light.h
index 6a6868412f7614a70e2241f02f2871d2d0077437..31dc3c0db709285c7f918820752b9c668d2ddcbf 100644 (file)
-/* $Id$
-
-This file is part of libmspgl
-Copyright © 2007  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
 #ifndef MSP_GL_LIGHT_H_
 #define MSP_GL_LIGHT_H_
 
+#include <vector>
+#include <msp/datafile/objectloader.h>
 #include "color.h"
+#include "placeable.h"
 
 namespace Msp {
 namespace GL {
 
-class Light
+class Matrix;
+class ProgramData;
+
+/**
+Stores properties of a single light source.  Lights can be directional, point
+lights or spotlights.  No explicit type parameter is provided; rather the
+other parameters determine what kind of light it is.  If the fourth component
+of position is zero, it's a directional light.  Otherwise, if the spot cutoff
+is not 180 degrees, it's a spotlight.  Otherwise it's an omnidirectional point
+light.
+
+Lights are usually grouped with a Lighting object, which can be used in a
+Pipeline::Pass.
+
+Lights do not cast shadows by themselves.  See ShadowMap for that.
+*/
+class Light: public Placeable
 {
+public:
+       class Loader: public DataFile::ObjectLoader<Light>
+       {
+       public:
+               Loader(Light &);
+
+       private:
+               void attenuation(float, float, float);
+               void diffuse(float, float, float);
+               void position(float, float, float, float);
+               void specular(float, float, float);
+               void spot_direction(float, float, float);
+               void spot_exponent(float);
+               void spot_cutoff(float);
+       };
+
 private:
-       Color ambient;
+       enum ParameterMask
+       {
+               DIFFUSE = 1,
+               SPECULAR = 2,
+               POSITION = 4,
+               SPOT_DIR = 8,
+               SPOT_EXP = 16,
+               SPOT_CUTOFF = 32,
+               ATTENUATION = 64
+       };
+
        Color diffuse;
        Color specular;
-       float x, y, z, w;
-       float sdx, sdy, sdz;
+       Vector4 position;
+       Vector3 spot_dir;
+       Vector3 direction;
        float spot_exp;
-       float spot_cutoff;
+       Geometry::Angle<float> spot_cutoff;
        float attenuation[3];
 
-       static unsigned current;
-
 public:
        Light();
-       void set_ambient(const Color &c);
+       ~Light();
+
+private:
+       void update_parameter(int, int = -1) const;
+       void update_matrix();
+
+public:
+       /** Sets the diffuse (direction-independent) color of the Light.  Provided
+       to shaders with the name light_sources[i].diffuse. */
        void set_diffuse(const Color &c);
+
+       /** Sets the specular (direction-dependent) color of the Light.  Provided to
+       shaders with the name light_sources[i].specular. */
        void set_specular(const Color &c);
-       void set_position(float, float, float, float);
-       void apply() const;
-       void apply_to(unsigned) const;
 
-       static void activate(unsigned);
+       const Color &get_diffuse() const { return diffuse; }
+       const Color &get_specular() const { return specular; }
+
+       /** Sets the postion and orientation of the Light from a matrix.  Negative Z
+       axis is used as the spot direction, other axes are ignored. */
+       virtual void set_matrix(const Matrix &);
+
+       /** Sets the position of the Light.  For a directional light, set the xyz
+       components to a vector pointing towards the light and the w component to 0. */
+       void set_position(const Vector4 &);
+
+       const Vector4 &get_position() const { return position; }
+
+       /** Sets the direction of a spotlight.  Has no effect if spotlight cutoff is
+       not set. */
+       void set_spot_direction(const Vector3 &);
+
+       /** Sets the angular falloff exponent of the spotlight.  Must be >= 0. */
+       void set_spot_exponent(float);
+
+       /// Deprecated.
+       void set_spot_cutoff(float);
+
+       /** Sets the cutoff angle of a spotlight.  Beyond this angle from its axis
+       the spotlight provides no illumination.  Must be between 0 and 90 degrees,
+       or exactly 180 degrees to indicate a non-spotlight. */
+       void set_spot_cutoff(const Geometry::Angle<float> &);
+
+       /** Disables spotlight, reverting to an omnidirectional point light.
+       Equivalent to setting the spot cutoff to 180 degrees. */
+       void disable_spot_cutoff();
+
+       const Vector3 &get_spot_direction() const { return spot_dir; }
+       float get_spot_exponent() const { return spot_exp; }
+       const Geometry::Angle<float> &get_spot_cutoff() const { return spot_cutoff; }
+       void set_attenuation(float, float, float);
+       const float *get_attenuation() const { return attenuation; }
+
+       /** Updates a ProgramData object with the uniforms for the Light.  A view
+       matrix and light source index must be passed in. */
+       void update_shader_data(ProgramData &, const Matrix &, unsigned) const;
+
+       void bind() const { return bind_to(0); }
+       void bind_to(unsigned) const;
+
+       static const Light *current(unsigned = 0);
+       static void unbind() { return unbind_from(0); }
+       static void unbind_from(unsigned);
 };
 
 } // namespace GL