DeferredRenderer 1.0

SpotLight.cpp

Go to the documentation of this file.
00001 #include "SpotLight.h"
00002 #include "ngl/VBOPrimitives.h"
00003 #include "ngl/Vector.h"
00004 #include "ngl/Matrix.h"
00005 #include "NGLMath.h"
00006 #include "ngl/ShaderManager.h"
00007 #include "ngl/Camera.h"
00008 
00009 #include "math.h"
00010 
00011 #include <cstring>
00012 
00013 SpotLight::SpotLight(const ngl::Vector &_position, const ngl::Vector &_target, const float &_distance, const float &_angle, const ngl::Vector &_col, const ngl::Vector &_up)
00014 {
00015     this->set(_position,_target,_distance,_angle,_col,_up);
00016 }
00017 
00018 SpotLight::~SpotLight(){}
00019 
00020 bool SpotLight::collidesWithSphere(const ngl::Vector &_pos, const float &_rad)
00021 {
00022     float radius = m_directionDistance.m_w * 0.5;
00023 
00024     ngl::Vector centre = ngl::Vector(m_directionDistance.m_x, m_directionDistance.m_y, m_directionDistance.m_z) * (m_directionDistance.m_w * 0.5);
00025 
00026     ngl::Vector length = centre-_pos;
00027     float lengthSq = length.lengthSquared();
00028 
00029     float radiiSq = (radius*radius)*1.5 /*scale up to avoid nasty culling */ + (_rad*_rad);
00030 
00031     return lengthSq < radiiSq;
00032 }
00033 
00034 void SpotLight::draw(const std::string &_vaoName, const std::string &_shader)
00035 {
00036     ngl::ShaderManager *shader = ngl::ShaderManager::instance();
00037 
00038     (*shader)[_shader]->use();
00039 
00040     shader->setShaderParamFromMatrix(_shader, "u_ModelMatrix", m_world);
00041     shader->setShaderParamFromVector(_shader, "a_PositionAngle", m_positionAngle);
00042     shader->setShaderParamFromVector(_shader, "a_DirectionDistance", m_directionDistance);
00043     shader->setShaderParamFromVector(_shader, "a_Colour", m_colour);
00044 
00045     ngl::VBOPrimitives *prim = ngl::VBOPrimitives::instance();
00046     prim->draw(_vaoName);
00047 }
00048 
00049 void SpotLight::set(
00050         const ngl::Vector &_position,
00051         const ngl::Vector &_target,
00052         const float &_distance,
00053         const float &_angle,
00054         const ngl::Vector &_col,
00055         const ngl::Vector &_up
00056         )
00057 {
00058     const float xyscale = _distance*sinf( ( _angle*.5)*ngl::PI/180 ) * 1.1f; // Scaling up to avoid hard edges eating the light
00059 
00060     ngl::Matrix scale, invView, trans;
00061 
00062     scale.scale(xyscale,xyscale,_distance);
00063 
00064     m_matrixCam = ngl::Camera(_position, _target, _up, ngl::PERSPECTIVE);
00065 
00066     m_lightView = Lookat(_position,_target,_up);
00067     invView = Inverse(m_lightView);
00068     
00069     //magic number to get ngl cone into position
00070     trans.translate(0,0,-1);
00071 
00072     m_world =  invView * scale * trans;
00073 
00074     ngl::Vector direction = _target-_position;
00075     direction.normalize();
00076 
00077     m_positionAngle = _position;
00078     m_positionAngle.m_w=cos( ( _angle*.5)*ngl::PI/180 );
00079     m_directionDistance = direction;
00080     m_directionDistance.m_w=_distance;
00081     m_colour = _col;
00082 }
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Defines