C++ coding exercise about Lighting Equations (GLM)

profileasbdefg
Light.cpp

/**************************************************** * 2016-2020 Eric Bachmann and Mike Zmuda * All Rights Reserved. * NOTICE: * Dissemination of this information or reproduction * of this material is prohibited unless prior written * permission is granted.. ****************************************************/ #include "Light.h" /** * @fn color ambientColor(const color &mat, const color &light) * @brief Computes the ambient color produced by a single light at a single point. * @param mat Ambient material property. * @param light Light's ambient color. * @return Ambient color. */ color ambientColor(const color &mat, const color &light) { return glm::clamp(mat*light, 0.0, 1.0); } /** * @fn color diffuseColor(const color &mat, const color &light, const dvec3 &l, const dvec3 &n) * @brief Computes diffuse color produce by a single light at a single point. * @param mat Material. * @param light The light. * @param l Light vector. * @param n Normal vector. * @return Diffuse color. */ color diffuseColor(const color &mat, const color &light, const dvec3 &l, const dvec3 &n) { double pd = glm::dot(l,n); return glm::clamp(mat*light*pd, 0.0, 1.0); } /** * @fn color specularColor(const color &mat, const color &light, double shininess, const dvec3 &r, const dvec3 &v) * @brief Computes specular color produce by a single light at a single point. * @param mat Material. * @param light The light's color. * @param shininess Material shininess. * @param r Reflection vector. * @param v Viewing vector. * @return Specular color. */ color specularColor(const color &mat, const color &light, double shininess, const dvec3 &r, const dvec3 &v) { double pd = glm::dot(r,v); double pw = glm::pow(pd,shininess); return glm::clamp(mat*light*pw, 0.0, 1.0); } /** * @fn color totalColor(const Material &mat, const LightColor &lightColor, const dvec3 &viewingDir, const dvec3 &normal, const dvec3 &lightPos, const dvec3 &intersectionPt, bool attenuationOn, const LightAttenuationParameters &ATparams) * @brief Color produced by a single light at a single point. * @param mat Material. * @param lightColor The light's color. * @param v The v vector. * @param n Normal vector. * @param lightPos Light position. * @param intersectionPt (x,y,z) of intersection point. * @param attenuationOn true if attenuation is on. * @param ATparams Attenuation parameters. * @return Color produced by a single light at a single point. */ color totalColor(const Material &mat, const LightColor &lightColor, const dvec3 &v, const dvec3 &n, const dvec3 &lightPos, const dvec3 &intersectionPt, bool attenuationOn, const LightATParams &ATparams) { /* CSE 386 - todo */ if (DEBUG_PIXEL) { cout << endl; } return mat.ambient; } /** * @fn color PositionalLight::illuminate(const dvec3 &interceptWorldCoords, const dvec3 &normal, const Material &material, const Frame &eyeFrame, bool inShadow) const * @brief Computes the color this light produces in raytracing applications. * @param interceptWorldCoords The surface properties of the intercept point. * @param normal The normal vector. * @param material The object's material properties. * @param eyeFrame The coordinate frame of the camera. * @param inShadow true if the point is in a shadow. * @return The color produced at the intercept point, given this light. */ color PositionalLight::illuminate(const dvec3 &interceptWorldCoords, const dvec3 &normal, const Material &material, const Frame &eyeFrame, bool inShadow) const { /* CSE 386 - todo */ //calltotalcolor return material.ambient; } /** * @fn color SpotLight::illuminate(const dvec3 &interceptWorldCoords, const dvec3 &normal, const Material &material, const Frame &eyeFrame, bool inShadow) const * @brief Computes the color this light produces in raytracing applications. * @param interceptWorldCoords The surface properties of the intercept point. * @param normal The normal vector. * @param material The object's material properties. * @param eyeFrame The coordinate frame of the camera. * @param inShadow true if the point is in a shadow. * @return The color produced at the intercept point, given this light. */ color SpotLight::illuminate(const dvec3 &interceptWorldCoords, const dvec3 &normal, const Material &material, const Frame &eyeFrame, bool inShadow) const { /* CSE 386 - todo */ return material.ambient; } /** * @fn ostream &operator << (ostream &os, const LightAttenuationParameters &at) * @brief Output stream for light attenuation parameters. * @param os Output stream. * @param at Attenuation parameters. * @return The output stream. */ ostream &operator << (ostream &os, const LightATParams &at) { os << dvec3(at.constant, at.linear, at.quadratic) << endl; return os; } /** * @fn ostream &operator << (ostream &os, const PositionalLight &pl) * @brief Output stream for light attenuation parameters. * @param os Output stream. * @param pl Positional light. * @return The output stream. */ ostream &operator << (ostream &os, const PositionalLight &pl) { os << (pl.isOn ? "ON" : "OFF") << endl; os << (pl.isTiedToWorld? "WORLD" : "CAMERA") << endl; os << " position " << pl.pos << endl; os << " ambient " << pl.lightColor.ambient << endl; os << " diffuse " << pl.lightColor.diffuse << endl; os << " specular " << pl.lightColor.specular << endl; os << "Attenuation: " << (pl.attenuationIsTurnedOn ? "ON" : "OFF") << " " << pl.atParams << endl; return os; } /** * @fn ostream &operator << (ostream &os, const SpotLight &sl) * @brief Output stream for light attenuation parameters. * @param os Output stream. * @param sl Spotlight. * @return The output stream. */ ostream &operator << (ostream &os, const SpotLight &sl) { PositionalLight pl = (sl); os << pl; os << " FOV " << sl.fov << endl; return os; } ostream &operator << (ostream &os, const LightColor &light) { os << "Light Color: " << light.ambient << ' ' << light.diffuse << ' ' << light.specular; return os; }