DeferredRenderer 1.0

LightingManager.cpp

Go to the documentation of this file.
00001 #include "LightingManager.h"
00002 #include "ngl/VBOPrimitives.h"
00003 
00004 LightingManager::LightingManager():m_pointLightVAO(0)
00005 {
00006 }
00007 
00008 void LightingManager::accumulateLights(TextureManager &_textureManager, const ngl::Vector &_camPos)
00009 {
00010     glActiveTexture(GL_TEXTURE1);
00011     _textureManager.bindTexture("normal");
00012 
00013     glActiveTexture(GL_TEXTURE2);
00014     _textureManager.bindTexture("albedo");
00015 
00016     glActiveTexture(GL_TEXTURE3);
00017     _textureManager.bindTexture("position");
00018 
00019     glActiveTexture(GL_TEXTURE4);
00020     _textureManager.bindTexture("shadow");
00021 
00022     glClearColor(0.f, 0.f, 0.f, 1.0f);
00023     glEnable(GL_BLEND);
00024     glEnable(GL_CULL_FACE);
00025     glCullFace(GL_BACK);
00026     glBlendFunc(GL_ONE,GL_ONE);
00027     glDepthMask(false);
00028 
00029     blendShadowSpots(_camPos);
00030     blendPointLights(_camPos);
00031     blendSpotLights(_camPos);
00032 
00033     glDisable(GL_BLEND);
00034     glDepthMask(true);
00035     glDisable(GL_CULL_FACE);
00036 }
00037 
00038 void LightingManager::blendShadowSpots(const ngl::Vector &_camPos)
00039 {
00040     for(unsigned int i = 0; i < m_shadowSpots.size(); i++)
00041     {
00042         //check for near plane clip
00043         if(m_shadowSpots[i].collidesWithSphere(_camPos, 10.0))
00044         {
00045             glCullFace(GL_FRONT);
00046             glDisable(GL_DEPTH_TEST);
00047 
00048             m_shadowSpots[i].draw("spotlight", "ShadowSpot");
00049             glEnable(GL_DEPTH_TEST);
00050             glCullFace(GL_BACK);
00051         }
00052         else
00053         {
00054             m_shadowSpots[i].draw("spotlight", "ShadowSpot");
00055         }
00056     }
00057 }
00058 
00059 void LightingManager::blendPointLights(const ngl::Vector &_camPos)
00060 {
00061     for(unsigned int i = 0; i < m_pointLights.size(); i++)
00062     {
00063         if(m_pointLights[i].collidesWithSphere(_camPos, 10.0))
00064         {
00065             glCullFace(GL_FRONT);
00066             glDisable(GL_DEPTH_TEST);
00067             m_pointLights[i].draw(m_pointLightVAO, "PointLight");
00068             glEnable(GL_DEPTH_TEST);
00069             glCullFace(GL_BACK);
00070         }
00071         else
00072         {
00073             m_pointLights[i].draw(m_pointLightVAO, "PointLight");
00074         }
00075     }
00076 }
00077 
00078 void LightingManager::blendSpotLights(const ngl::Vector &_camPos)
00079 {
00080     for(unsigned int i = 0; i < m_spotLights.size(); i++)
00081     {
00082         //check for near plane clip
00083         if(m_spotLights[i].collidesWithSphere(_camPos, 10.0))
00084         {
00085             glCullFace(GL_FRONT);
00086             glDisable(GL_DEPTH_TEST);
00087             m_spotLights[i].draw("spotlight", "SpotLight");
00088             glEnable(GL_DEPTH_TEST);
00089             glCullFace(GL_BACK);
00090         }
00091         else
00092         {
00093             m_spotLights[i].draw("spotlight", "SpotLight");
00094         }
00095     }
00096 }
00097 
00098 void LightingManager::createLightGeometry()
00099 {
00100     // vertex coords array
00101    GLfloat vertices[] = {
00102        1, 1, 1, -1, 1, 1, -1,-1, 1, 1,-1, 1, // v0-v1-v2-v3
00103        1, 1, 1, 1,-1, 1, 1,-1,-1, 1, 1,-1, // v0-v3-v4-v
00104        1, 1, 1, 1, 1,-1, -1, 1,-1, -1, 1, 1, // v0-v5-v6-v
00105        -1, 1, 1, -1, 1,-1, -1,-1,-1, -1,-1, 1, // v1-v6-v7-v
00106        -1,-1,-1, 1,-1,-1, 1,-1, 1, -1,-1, 1, // v7-v4-v3-v
00107        1,-1,-1, -1,-1,-1, -1, 1,-1, 1, 1,-1 // v4-v7-v6-v5
00108    };
00109 
00110    // first we create a vertex array Object
00111    glGenVertexArrays(1, &m_pointLightVAO);
00112 
00113      // now bind this to be the currently active one
00114    glBindVertexArray(m_pointLightVAO);
00115      // as they will be associated with the vertex array object
00116    GLuint vboID;
00117    glGenBuffers(1, &vboID);
00118    // now we will bind an array buffer to the first one and load the data for the verts
00119    glBindBuffer(GL_ARRAY_BUFFER, vboID);
00120    glBufferData(GL_ARRAY_BUFFER, 24*3*sizeof(GLfloat), vertices, GL_STATIC_DRAW);
00121    // now we bind the vertex attribute pointer for this object in this case the
00122    // vertex data
00123    ngl::ShaderManager *shader=ngl::ShaderManager::instance();
00124    (*shader)["PointLight"]->vertexAttribPointer("a_VertexPosition",3,GL_FLOAT,0,0);
00125    (*shader)["PointLight"]->enableAttribArray("a_VertexPosition");
00126 
00127    // finally switch back to the default so we don't overwrite
00128    glEnableVertexAttribArray(0);
00129    glBindVertexArray(0);
00130 
00131    ngl::VBOPrimitives *prim = ngl::VBOPrimitives::instance();
00132    prim->createVBOCone("spotlight",1.0,1.0,30.0,1.0);
00133 }
00134 
00135 void LightingManager::addNamedPoint(const std::string &_name,PointLight &_point)
00136 {
00137     m_namedPointLights.insert(std::pair<std::string, PointLight>(_name, _point));
00138 }
00139 void LightingManager::addNamedSpot(const std::string &_name,SpotLight &_spot)
00140 {
00141     m_namedSpotLights.insert(std::pair<std::string, SpotLight>(_name, _spot));
00142 }
00143 void LightingManager::addNamedShadowSpot(const std::string &_name,ShadowSpot &_spot)
00144 {
00145     m_namedShadowSpots.insert(std::pair<std::string, ShadowSpot>(_name, _spot));
00146 }
00147 
00148 void LightingManager::addNamedPoint(
00149     const std::string &_name,
00150     const ngl::Vector &_centre,
00151     const float &_radius,
00152     const ngl::Vector &_intensity)
00153 {
00154     PointLight tmp(_centre, _radius, _intensity);
00155     this->addNamedPoint(_name, tmp);
00156 }
00157 
00158 void LightingManager::addNamedSpot(
00159     const std::string &_name,
00160     const ngl::Vector &_position,
00161     const ngl::Vector &_target,
00162     const float &_distance,
00163     const float &_angle,
00164     const ngl::Vector &_col,
00165     const ngl::Vector &_up)
00166 {
00167     SpotLight tmp(_position, _target, _distance, _angle, _col, _up);
00168     this->addNamedSpot(_name, tmp);
00169 }
00170 
00171 void LightingManager::addNamedShadowSpot(
00172     const std::string &_name,
00173     const ngl::Vector &_position,
00174     const ngl::Vector &_target,
00175     const float &_distance,
00176     const float &_angle,
00177     const ngl::Vector &_col,
00178     const ngl::Vector &_up)
00179 {
00180     ShadowSpot tmp(_position, _target, _distance, _angle, _col, _up);
00181     this->addNamedSpot(_name, tmp);
00182 }
00183 
00184 void LightingManager::addPointLight(
00185     const ngl::Vector &_centre,
00186     const float &_radius,
00187     const ngl::Vector &_intensity)
00188 {
00189     PointLight tmp(_centre, _radius, _intensity);
00190     m_pointLights.push_back(tmp);
00191 }
00192 void LightingManager::addSpotLight(
00193     const ngl::Vector &_position,
00194     const ngl::Vector &_target,
00195     const float &_distance,
00196     const float &_angle,
00197     const ngl::Vector &_col,
00198     const ngl::Vector &_up)
00199 {
00200     SpotLight tmp(_position, _target, _distance, _angle, _col, _up);
00201     m_spotLights.push_back(tmp);
00202 }
00203 void LightingManager::addShadowSpot(
00204     const ngl::Vector &_position,
00205     const ngl::Vector &_target,
00206     const float &_distance,
00207     const float &_angle,
00208     const ngl::Vector &_col,
00209     const ngl::Vector &_up)
00210 {
00211     ShadowSpot tmp(_position, _target, _distance, _angle, _col, _up);
00212     m_shadowSpots.push_back(tmp);
00213 }
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Defines