00001 #ifndef LIGHTING_H_
00002 #define LIGHTING_H_
00003
00004 #include "Vector.h"
00005 #include "Color.h"
00006
00013 class Lighting {
00014 public:
00015 Lighting(Vector<> l = Vector<>(0,-1,-1).normalize(), const Vector<>& v = Vector<>(0,0,1), Color lightColor = Color(255,255,255,255),
00016 float kD = 0.5, float kS = 1.0, float sE = 10);
00017 ~Lighting();
00018
00019 void updateH();
00020
00021 void setV(const Vector<>& v);
00022 void setL(const Vector<>& l);
00023
00024 void setKDiffuse(float newKD);
00025 float getKDiffuse() const;
00026
00027 void setKSpecular(float newKS);
00028 float getKSpecular() const;
00029
00030 void setSExponent(float newSE);
00031 float getSExponent() const;
00032
00033 Color getColor(const Color& c, const Vector<>& n) const{
00034 int R,G,B;
00035 float cosAlphaDiff = n.dotProduct(mL);
00036 if (cosAlphaDiff < 0) cosAlphaDiff = 0;
00037
00038 float specularCoeff = n.dotProduct (mH);
00039 if (specularCoeff < 0) specularCoeff=0;
00040 else specularCoeff = pow(specularCoeff, mSE);
00041
00042
00043 R = (int) (c.R() * mKD * cosAlphaDiff + mLightColor.R() * mKS * specularCoeff);
00044 G = (int) (c.G() * mKD * cosAlphaDiff + mLightColor.G() * mKS * specularCoeff);
00045 B = (int) (c.B() * mKD * cosAlphaDiff + mLightColor.B() * mKS * specularCoeff);
00046
00047 if (R < 0) R = 0;
00048 else if (R > Color::maxChannelValue) R = Color::maxChannelValue;
00049
00050 if (G < 0) G = 0;
00051 else if (G > Color::maxChannelValue) G = Color::maxChannelValue;
00052
00053 if (B < 0) B = 0;
00054 else if (B > Color::maxChannelValue) B = Color::maxChannelValue;
00055
00056 return Color(R, G, B, c.A());
00057 }
00058
00059 void printVectors() const{
00060 printf("mL: (%f,%f,%f)\n",mL.x(),mL.y(),mL.z());
00061 printf("mV: (%f,%f,%f)\n",mV.x(),mV.y(),mV.z());
00062 printf("mH: (%f,%f,%f)\n\n",mH.x(),mH.y(),mH.z());
00063 }
00064
00065
00066 private:
00067 Vector<> mL;
00068 Vector<> mV;
00069 Vector<> mH;
00070
00071 Color mLightColor;
00072
00073
00074 float mKD;
00075 float mKS;
00076 float mSE;
00077
00078 };
00079
00080 #endif