DeferredRenderer 1.0

GLWindow.cpp

Go to the documentation of this file.
00001 #include "GLWindow.h"
00002 #include <iostream>
00003 #include "ngl/Vector.h"
00004 #include "ngl/Light.h"
00005 #include "ngl/NGLInit.h"
00006 #include "ngl/VBOPrimitives.h"
00007 #include "ngl/ShaderManager.h"
00008 #include "FrameBufferObject.h"
00009 #include "ScreenQuad.h"
00010 #include "NGLMath.h"
00011 #include "Texture.h"
00012 #include "ngl/Material.h"
00013 #include "ngl/Util.h"
00014 #include <QImage>
00015 #include "LightingManager.h"
00016 #include "sstream"
00017 
00018 #define WIDTH 1024
00019 #define HEIGHT 768
00020 
00021 //----------------------------------------------------------------------------------------------------------------------
00022 GLWindow::GLWindow(
00023                    QWidget *_parent
00024                   ) :
00025                     QGLWidget(_parent),
00026                     m_viewportQuad(0.0f, 0.0f, 0.5f, 1.0f),
00027                     m_fbo(WIDTH, HEIGHT),
00028                     m_shadowBuffer(512, 512),
00029                     m_cam(ngl::Vector(0,6,10),ngl::Vector(0,0,4),ngl::Vector(0,1,0), ngl::PERSPECTIVE),
00030                     m_debugQuad1(-0.75,-0.8,0.0,0.2),
00031                     m_debugQuad2(-0.25,-0.8,0.0,0.2),
00032                     m_debugQuad3(0.25,-0.8,0.0,0.2),
00033                     m_debugQuad4(0.75,-0.8,0.0,0.2),
00034                     m_debugQuad5(-0.75,0.75,0.0,0.2)
00035 {
00036 
00037   // set this widget to have the initial keyboard focus
00038   setFocus();
00039   // re-size the widget to that of the parent (in this case the GLFrame passed in on construction)
00040   this->resize(WIDTH,HEIGHT);
00041 
00042   m_rotate=false;
00043   // mouse rotation values set to 0
00044   m_spinXFace=0;
00045   m_spinYFace=0;
00046 
00047   m_wireframe = false;
00048   m_debug = true;
00049   m_showShadowMap = false;
00050   m_currentDisplay = 0;
00051   m_Ka = 0.5;
00052   m_Kd = 0.7;
00053 
00054   m_directionalVec = ngl::Vector(1,0,0);
00055   m_ambientColour = ngl::Vector(1,1,1);
00056   m_directionalColour = ngl::Vector(1,1,1);
00057 
00058   //win32
00059   m_AAbarrierDepth = 0.5;
00060   m_AAbarrierNorm = 0.6;
00061   m_AAweightNorm = 0.15;
00062   m_AAweightDepth = 0.15;
00063   m_AAkernel = 0.4;
00064 
00065   m_time = 0.0;
00066   m_timeStep = 1.0;
00067 
00068 }
00069 
00070 void GLWindow::initializeGL()
00071 {
00072   //init ngl
00073   ngl::NGLInit *init = ngl::NGLInit::instance();
00074   #ifdef WIN32
00075     glewInit();
00076   #endif
00077     init->initGlew();
00078     init->initVBO();
00079 
00080   glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
00081   // enable depth testing for drawing
00082   glEnable(GL_DEPTH_TEST);
00083 
00084   glEnable(GL_MULTISAMPLE);
00085 
00086   for(int i = 0; i < 5; i++)
00087   {
00088     glActiveTexture(GL_TEXTURE0+i);
00089     glEnable(GL_TEXTURE_2D);
00090   }
00091 
00092   m_cam.setShape(45,float(WIDTH/HEIGHT),0.1,300,ngl::PERSPECTIVE);
00093   m_frustum.generate(m_cam.getProjection() * m_cam.getModelView());
00094 
00095   //init shaders
00096   setupShaders();
00097 
00098   //init scene
00099   //m_scene.init();
00100 
00101   m_lightingManager.createLightGeometry();
00102 
00103   //geometry for unit screen quad
00104   createScreenQuad();
00105 
00106   setupTextures();
00107 
00108   postProcessSetup();
00109 
00110   //init fbos
00111   m_fbo.create(m_textureManager.getTextureId("constant"));
00112   m_shadowBuffer.create(m_textureManager.getTextureId("shadow"));
00113 
00114   m_fbo.bindTextureSlot(1, m_textureManager.getTextureId("normal"));
00115   m_fbo.bindTextureSlot(2, m_textureManager.getTextureId("albedo"));
00116   m_fbo.bindTextureSlot(3, m_textureManager.getTextureId("position"));
00117 
00118   m_lightingManager.addPointLight(ngl::Vector(2.0,0.0,4.0),4.0, ngl::Vector(0.7,0.7,0.7));
00119   m_lightingManager.addPointLight(ngl::Vector(-2.0,0.0,-4.0),4.0, ngl::Vector(1.0,0.7,0.7));
00120   m_lightingManager.addPointLight(ngl::Vector(2.0,0.0,-4.0),4.0, ngl::Vector(0.7,0.7,1.0));
00121   m_lightingManager.addPointLight(ngl::Vector(-2.0,0.0,4.0),4.0, ngl::Vector(0.7,0.7,0.0));
00122   m_lightingManager.addPointLight(ngl::Vector(3.0,0.0,0.0),4.0, ngl::Vector(1.0,0.0,1.0));
00123   m_lightingManager.addPointLight(ngl::Vector(-3.0,0.0,0.0),4.0, ngl::Vector(0.0,1.0,1.0));
00124 
00125   m_lightingManager.addShadowSpot(ngl::Vector(0,6,10),ngl::Vector(0,0,4),30.0,45.0,ngl::Vector(0.8,0.8,0.4),ngl::Vector(0,1,0));
00126 
00127   obj = new ngl::Obj("models/Troll.obj");
00128   obj->createVBO(GL_STATIC_DRAW);
00129 
00130   //set current main diaply texturer
00131   m_currentDisplay = m_textureManager.getTextureId("constant");
00132 
00133   //start update timer for lights
00134   //m_updatetimer = startTimer(10);
00135 }
00136 
00137 
00138 void GLWindow::shadowPass()
00139 {
00140     ngl::ShaderManager *shader = ngl::ShaderManager::instance();
00141     (*shader)["ShadowBuffer"]->use();
00142 
00143     shader->setShaderParamFromMatrix("ShadowBuffer", "u_LightView", m_lightingManager.getShadowSpot(0).m_shadowView);
00144     shader->setShaderParamFromMatrix("ShadowBuffer", "u_LightProj", m_lightingManager.getShadowSpot(0).getProj());
00145 
00146     glClearColor(0.f, 0.f, 0.f, 1.0f);
00147     glEnable(GL_CULL_FACE);
00148     glCullFace(GL_BACK);
00149 
00150     //bind shadow buffer
00151     m_shadowBuffer.bind();
00152     {
00153         m_shadowBuffer.activateTarget(0);
00154 
00155     }
00156     m_shadowBuffer.unbind();
00157 
00158     glDisable(GL_CULL_FACE);
00159     glCullFace(GL_FRONT);
00160 }
00161 
00162 void GLWindow::geometryPass()
00163 {
00164     ngl::ShaderManager *shader = ngl::ShaderManager::instance();
00165 
00166     (*shader)["Gbuffer"]->use();
00167 
00168     ngl::Material m(ngl::GOLD);
00169     m.use();
00170 
00171     m_fbo.bind();
00172     {
00173       m_fbo.activateAllTargets();
00174 
00175       glClearColor(0.f, 0.f, 0.f, 1.0f);
00176       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00177 
00178       obj->draw();
00179 
00180     }
00181     m_fbo.unbind();
00182 
00183         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00184 }
00185 
00186 void GLWindow::debugPass()
00187 {
00188     glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
00189     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00190 
00191     glActiveTexture(GL_TEXTURE0);
00192 
00193     m_textureManager.bindTexture("constant");
00194     m_debugQuad1.draw("ScreenQuad", m_screenQuadVAO);
00195     m_textureManager.bindTexture("normal");
00196     m_debugQuad2.draw("ScreenQuad",m_screenQuadVAO);
00197     m_textureManager.bindTexture("albedo");
00198     m_debugQuad3.draw("ScreenQuad",m_screenQuadVAO);
00199     m_textureManager.bindTexture("position");
00200     m_debugQuad4.draw("ScreenQuad",m_screenQuadVAO);
00201 
00202     if(m_showShadowMap)
00203     {
00204         m_textureManager.bindTexture("shadow");
00205         m_debugQuad5.draw("ScreenQuad",m_screenQuadVAO);
00206     }
00207 
00208     glBindTexture(GL_TEXTURE_2D, 0);
00209 }
00210 
00211 void GLWindow::lightAccumulation()
00212 {
00213     m_fbo.bind();
00214     {
00215       m_fbo.activateTarget(0);
00216 
00217       ngl::Transformation trans;
00218       trans.setRotation(m_spinXFace,m_spinYFace,0);
00219 
00220       //get the camera pos in WS
00221       ngl::Matrix invView = Inverse(m_cam.getModelView() * trans.getMatrix());
00222       ngl::Vector camPos = invView * ngl::Vector(0,0,0,1);
00223 
00224       m_lightingManager.accumulateLights(m_textureManager, camPos);
00225     }
00226     m_fbo.unbind();
00227 
00228 }
00229 
00230 void GLWindow::paintGL()
00231 {
00232   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00233 
00234   ngl::Transformation trans;
00235   trans.setRotation(m_spinXFace,m_spinYFace,0);
00236 
00237   ngl::Matrix projView = m_cam.getProjection() * (m_cam.getModelView() * trans.getMatrix());
00238   ngl::Matrix view = m_cam.getModelView() * trans.getMatrix();
00239 
00240   m_frustum.generate(projView);
00241 
00242   if(m_wireframe)
00243   {
00244       glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
00245   }
00246   else
00247   {
00248       glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00249   }
00250 
00251   ngl::ShaderManager *shader = ngl::ShaderManager::instance();
00252 
00253   /******************
00254    Shadow pass
00255    *********************/
00256   //shadowPass();
00257   /**************************************/
00258 
00259   /******************
00260     Gpass
00261     ********************/
00262   (*shader)["Gbuffer"]->use();
00263   shader->setShaderParamFromMatrix("Gbuffer", "u_ViewMatrix", view);
00264   shader->setShaderParam3f("Gbuffer","u_AmbientLight",m_ambientColour.m_x,m_ambientColour.m_y,m_ambientColour.m_z);
00265   shader->setShaderParam3f("Gbuffer","u_DirectionalLightVec",m_directionalVec.m_x,m_directionalVec.m_y,m_directionalVec.m_z);
00266   shader->setShaderParam3f("Gbuffer","u_DirectionalLight",m_directionalColour.m_x,m_directionalColour.m_y,m_directionalColour.m_z);
00267   shader->setShaderParam3f("Gbuffer", "u_KaKdKs", m_Ka,m_Kd,0.0);
00268   geometryPass();
00269   /*************************************/
00270 
00271   /******************
00272     light accumulation
00273     *********************/
00274   (*shader)["PointLight"]->use();
00275   shader->setShaderParamFromMatrix("PointLight", "u_ViewMatrix", view);
00276 
00277   (*shader)["DemoPL"]->use();
00278   shader->setShaderParamFromMatrix("DemoPL", "u_ViewMatrix", view);
00279 
00280   (*shader)["SpotLight"]->use();
00281   shader->setShaderParamFromMatrix("SpotLight", "u_ViewMatrix", view);
00282 
00283   (*shader)["ShadowSpot"]->use();
00284   shader->setShaderParamFromMatrix("ShadowSpot", "u_ViewMatrix", view);
00285   shader->setShaderParamFromMatrix("ShadowSpot", "u_LightView", m_lightingManager.getShadowSpot(0).m_shadowView);
00286   shader->setShaderParamFromMatrix("ShadowSpot", "u_LightProj",m_lightingManager.getShadowSpot(0).getProj());
00287 
00288   lightAccumulation();
00289 
00290   /***************************************/
00291 
00292   /******************
00293     Post-process
00294     ********************/
00295   postProcessUniforms();
00296   postProcess();
00297   /***************************************/
00298 
00299 
00300   /******************
00301     debug gpass
00302     ********************/
00303   if(m_debug)
00304   {
00305       (*shader)["ScreenQuad"]->use();
00306       debugPass();
00307   }
00308   /***************************************/
00309 
00310   /******************
00311     final
00312     *********************/
00313 
00314   (*shader)["ScreenQuad"]->use();
00315   glActiveTexture(GL_TEXTURE0);
00316   glBindTexture(GL_TEXTURE_2D,  m_currentDisplay);
00317 
00318   m_viewportQuad.draw("ScreenQuad",m_screenQuadVAO);
00319   glBindTexture(GL_TEXTURE_2D, 0);
00320 
00321   /*************************************/
00322 
00323 }
00324 
00325 void GLWindow::addStandardEffect(const std::string &_effectName,
00326                                  const std::string &_outputName,
00327                                  const std::string &_shaderName,
00328                                  const std::string &_sourceName)
00329 {
00330     Effect effect(m_textureManager.getTextureId(_outputName), _shaderName);
00331     effect.addSourceTexture("u_Sampler", m_textureManager.getTextureId(_sourceName));
00332     m_postProcessor.addEffect(_effectName, effect);
00333 }
00334 
00335 void GLWindow::postProcessSetup()
00336 {
00337     m_postProcessor = PostProcessor(WIDTH, HEIGHT, m_screenQuadVAO);
00338 
00339     Effect antiAlias(m_textureManager.getTextureId("antialias"), "AntiAlias");
00340     antiAlias.addSourceTexture("u_ColorMap", m_textureManager.getTextureId("constant"));
00341     antiAlias.addSourceTexture("u_NormalMap", m_textureManager.getTextureId("normal"));
00342     antiAlias.addSourceTexture("u_PositionMap", m_textureManager.getTextureId("position"));
00343     m_postProcessor.addEffect("AntiAlias", antiAlias);
00344 
00345     Effect depthOfField(m_textureManager.getTextureId("DoF"), "DoF");
00346     depthOfField.addSourceTexture("u_ColorMap", m_textureManager.getTextureId("antialias"));
00347     depthOfField.addSourceTexture("u_BlurMap", m_textureManager.getTextureId("gaussian"));
00348     depthOfField.addSourceTexture("u_PositionMap", m_textureManager.getTextureId("position"));
00349     m_postProcessor.addEffect("DoF", depthOfField);
00350 
00351     addStandardEffect("HorizontalBlur", "horizontalBlur", "HorizontalBlur", "antialias");
00352     addStandardEffect("VerticalBlur", "gaussian", "VerticalBlur", "horizontalBlur");
00353     addStandardEffect("Grayscale", "grayscale", "Grayscale", "antialias");
00354     addStandardEffect("Sepia", "sepia", "Sepia", "antialias");
00355 
00356     addStandardEffect("BrightPass", "brightPass", "BrightPass", "antialias");
00357     addStandardEffect("BloomHBlur", "bloomHBlur", "HorizontalBlur", "brightPass");
00358     addStandardEffect("BloomGaussian", "bloomGaussian", "VerticalBlur", "bloomHBlur");
00359 
00360     Effect bloom(m_textureManager.getTextureId("bloom"), "Bloom");
00361     bloom.addSourceTexture("u_BrightMap", m_textureManager.getTextureId("bloomGaussian"));
00362     bloom.addSourceTexture("u_ColorMap", m_textureManager.getTextureId("antialias"));
00363     m_postProcessor.addEffect("Bloom", bloom);
00364 }
00365 
00366 void GLWindow::postProcessUniforms()
00367 {
00368     ngl::ShaderManager *shader = ngl::ShaderManager::instance();
00369 
00370     (*shader)["AntiAlias"]->use();
00371 
00372     shader->setShaderParam4f("AntiAlias",
00373                              "u_barrierWeights",
00374                              m_AAbarrierNorm,
00375                              m_AAbarrierDepth,
00376                              m_AAweightNorm,
00377                              m_AAweightDepth
00378                              );
00379 
00380     shader->setShaderParam1f("AntiAlias", "u_kernel", m_AAkernel);
00381 
00382     (*shader)["DoF"]->use();
00383 
00384     shader->setShaderParam1f("DoF", "u_FocalDepth", 0.5);
00385     ngl::Transformation trans;
00386     trans.setRotation(m_spinXFace,m_spinYFace,0);
00387 
00388     ngl::Matrix projView = m_cam.getProjection() * (m_cam.getModelView() * trans.getMatrix());
00389     shader->setShaderParamFromMatrix("DoF", "u_ProjView", projView);
00390 }
00391 
00392 void GLWindow::postProcess()
00393 {
00394     m_postProcessor.start();
00395     {
00396         //always do AA
00397         m_postProcessor.doEffect("AntiAlias");
00398 
00399         //grab a blur texture
00400         m_postProcessor.doEffect("HorizontalBlur");
00401         m_postProcessor.doEffect("VerticalBlur");
00402 
00403         //do depth of field
00404         m_postProcessor.doEffect("DoF");
00405 
00406         m_postProcessor.doEffect("BrightPass");
00407         m_postProcessor.doEffect("BloomHBlur");
00408         m_postProcessor.doEffect("BloomGaussian");
00409         m_postProcessor.doEffect("Bloom");
00410 
00411         //m_postProcessor.doEffect("Grayscale");
00412         //m_postProcessor.doEffect("Sepia");
00413     }
00414     m_postProcessor.finish();
00415 }
00416 
00417 void GLWindow::setMainDisplay(int _mode)
00418 {
00419     switch(_mode)
00420     {
00421     case 0:
00422         m_currentDisplay =  m_textureManager.getTextureId("constant");
00423         break;
00424     case 1:
00425         m_currentDisplay =  m_textureManager.getTextureId("normal");
00426         break;
00427     case 2:
00428         m_currentDisplay =  m_textureManager.getTextureId("albedo");
00429         break;
00430     case 3:
00431         m_currentDisplay =  m_textureManager.getTextureId("position");
00432         break;
00433     case 4:
00434         m_currentDisplay =  m_textureManager.getTextureId("antialias");
00435         break;
00436     case 5:
00437         m_currentDisplay =  m_textureManager.getTextureId("gaussian");
00438         break;
00439     case 6:
00440         m_currentDisplay =  m_postProcessor.getFinalComposite();
00441         break;
00442     default:
00443         break;
00444     }
00445 
00446     updateGL();
00447 }
00448 
00449 void GLWindow::genShader(const std::string &_name, const std::string &_VSname, const std::string &_FSname, const std::vector<std::string> &_attributes)
00450 {
00451     ngl::ShaderManager *shader = ngl::ShaderManager::instance();
00452 
00453     shader->createShaderProgram(_name);
00454     //load shader sources
00455     shader->attachShader("vertex", ngl::VERTEX);
00456     shader->attachShader("fragment", ngl::FRAGMENT);
00457     shader->loadShaderSource("vertex", _VSname);
00458     shader->loadShaderSource("fragment", _FSname);
00459     //compile
00460     shader->compileShader("vertex");
00461     shader->compileShader("fragment");
00462     //link
00463     shader->attachShaderToProgram(_name, "vertex");
00464     shader->attachShaderToProgram(_name, "fragment");
00465 
00466     for(unsigned int i = 0; i < _attributes.size(); i++)
00467     {
00468         shader->bindAttribute(_name, i, _attributes[i]);
00469     }
00470 
00471     shader->linkProgramObject(_name);
00472     //use
00473     (*shader)[_name]->use();
00474 
00475 }
00476 
00477 void GLWindow::setupTextures()
00478 {
00479     m_textureManager.addEmptyf("constant", WIDTH, HEIGHT);
00480     m_textureManager.addEmptyf("normal", WIDTH, HEIGHT);
00481     m_textureManager.addEmptyf("albedo", WIDTH, HEIGHT);
00482     m_textureManager.addEmptyf("position", WIDTH, HEIGHT);
00483 
00484     m_textureManager.addEmpty("antialias", WIDTH, HEIGHT);
00485 
00486     m_textureManager.addEmpty("grayscale", WIDTH, HEIGHT);
00487     m_textureManager.addEmpty("sepia", WIDTH, HEIGHT);
00488     m_textureManager.addEmpty("horizontalBlur", WIDTH, HEIGHT);
00489     m_textureManager.addEmpty("DoF", WIDTH, HEIGHT);
00490     m_textureManager.addEmpty("gaussian", WIDTH, HEIGHT);
00491 
00492     m_textureManager.addEmpty("bloomHBlur", WIDTH, HEIGHT);
00493     m_textureManager.addEmpty("bloom", WIDTH, HEIGHT);
00494     m_textureManager.addEmpty("bloomGaussian", WIDTH, HEIGHT);
00495     m_textureManager.addEmpty("brightPass", WIDTH, HEIGHT);
00496 
00497     Texture tmp("shadow");
00498     tmp.generateEmpty(512, 512);
00499     m_textureManager.addTexture(tmp);
00500 }
00501 
00502 void GLWindow::setupShaders()
00503 {
00504     ngl::ShaderManager *shader = ngl::ShaderManager::instance();
00505 
00506     std::vector<std::string> attrs;
00507     attrs.push_back("a_VertexPosition");
00508     attrs.push_back("a_TextureCoord");
00509 
00510     genShader("ScreenQuad", "shaders/ScreenQuad.vs", "shaders/ScreenQuad.fs", attrs);
00511 
00512     genShader("Grayscale", "shaders/ScreenQuad.vs", "shaders/postProcess/Grayscale.fs", attrs);
00513 
00514     genShader("Sepia", "shaders/ScreenQuad.vs", "shaders/postProcess/Sepia.fs", attrs);
00515 
00516     genShader("BrightPass", "shaders/ScreenQuad.vs", "shaders/postProcess/BrightPass.fs", attrs);
00517 
00518     genShader("Bloom", "shaders/ScreenQuad.vs", "shaders/postProcess/Bloom.fs", attrs);
00519 
00520     genShader("HorizontalBlur", "shaders/ScreenQuad.vs", "shaders/postProcess/HorizontalBlur.fs", attrs);
00521     shader->setShaderParam1f("HorizontalBlur", "u_pixel", 1.0/(float)WIDTH);
00522 
00523     genShader("VerticalBlur", "shaders/ScreenQuad.vs", "shaders/postProcess/VerticalBlur.fs", attrs);
00524     shader->setShaderParam1f("VerticalBlur", "u_pixel", 1.0/(float)HEIGHT);
00525 
00526     genShader("AntiAlias", "shaders/ScreenQuad.vs", "shaders/postProcess/AntiAlias.fs", attrs);
00527     shader->setShaderParam2f("AntiAlias", "u_pixel", 1.0/768.0, 1.0/(float)WIDTH);
00528 
00529     genShader("DoF", "shaders/ScreenQuad.vs", "shaders/postProcess/DoF.fs", attrs);
00530 
00531     attrs.clear();
00532 
00533     attrs.push_back("inVert");
00534     attrs.push_back("inUV");
00535     attrs.push_back("inNormal");
00536     attrs.push_back("inTangent");
00537     attrs.push_back("inBinormal");
00538 
00539     genShader("Gbuffer", "shaders/Gbuffer.vs", "shaders/Gbuffer.fs", attrs);
00540     shader->setShaderParamFromMatrix("Gbuffer", "u_ProjectionMatrix",m_cam.getProjection());
00541 
00542     attrs.clear();
00543 
00544     genShader("ShadowBuffer", "shaders/SpotShadowPass.vs", "shaders/SpotShadowPass.fs", attrs);
00545 
00546     attrs.push_back("a_VertexPosition");
00547     genShader("PointLight", "shaders/lights/PointLight.vs", "shaders/lights/PointLight.fs", attrs);
00548     shader->setShaderParamFromMatrix("PointLight", "u_ProjectionMatrix",m_cam.getProjection());
00549     shader->setShaderParam1i("PointLight","u_NormalMap",1);
00550     shader->setShaderParam1i("PointLight","u_AlbedoMap",2);
00551     shader->setShaderParam1i("PointLight","u_PositionMap",3);
00552 
00553     attrs.clear();
00554     genShader("SpotLight", "shaders/lights/SpotLight.vs", "shaders/lights/SpotLight.fs", attrs);
00555     shader->setShaderParamFromMatrix("SpotLight", "u_ProjectionMatrix",m_cam.getProjection());
00556     shader->setShaderParam1i("SpotLight","u_NormalMap",1);
00557     shader->setShaderParam1i("SpotLight","u_AlbedoMap",2);
00558     shader->setShaderParam1i("SpotLight","u_PositionMap",3);
00559 
00560     genShader("ShadowSpot", "shaders/lights/ShadowSpot.vs", "shaders/lights/ShadowSpot.fs", attrs);
00561     shader->setShaderParamFromMatrix("ShadowSpot", "u_ProjectionMatrix",m_cam.getProjection());
00562     shader->setShaderParam1i("ShadowSpot","u_NormalMap",1);
00563     shader->setShaderParam1i("ShadowSpot","u_AlbedoMap",2);
00564     shader->setShaderParam1i("ShadowSpot","u_PositionMap",3);
00565     shader->setShaderParam1i("ShadowSpot","u_ShadowMap",4);
00566 
00567     attrs.push_back("a_VertPos");
00568     genShader("DemoPL", "shaders/VisiblePLDemo.vs", "shaders/VisiblePLDemo.fs", attrs);
00569     shader->setShaderParamFromMatrix("DemoPL", "u_ProjectionMatrix",m_cam.getProjection());
00570     attrs.clear();
00571 
00572 }
00573 
00574 void GLWindow::createScreenQuad()
00575 {
00576     // vertex coords array
00577    GLfloat vertices[] = {
00578        -1.0, -1.0, 0.0,
00579        1.0, -1.0, 0.0,
00580        1.0, 1.0, 0.0,
00581        -1.0, 1.0, 0.0  // v0-v1-v2-v3
00582    };
00583 
00584    GLfloat texture[] = {
00585        0,0,
00586        1,0,
00587        1,1,
00588        0,1  // v0-v1-v2-v3
00589    };
00590 
00591      // first we create a vertex array Object
00592    glGenVertexArrays(1, &m_screenQuadVAO);
00593 
00594      // now bind this to be the currently active one
00595    glBindVertexArray(m_screenQuadVAO);
00596      // now we create two VBO's one for each of the objects these are only used here
00597      // as they will be associated with the vertex array object
00598    GLuint vboID[2];
00599    glGenBuffers(2, &vboID[0]);
00600    // now we will bind an array buffer to the first one and load the data for the verts
00601    glBindBuffer(GL_ARRAY_BUFFER, vboID[0]);
00602    glBufferData(GL_ARRAY_BUFFER, 4*3*sizeof(GLfloat), vertices, GL_STATIC_DRAW);
00603    // now we bind the vertex attribute pointer for this object in this case the
00604    // vertex data
00605    ngl::ShaderManager *shader=ngl::ShaderManager::instance();
00606    (*shader)["ScreenQuad"]->use();
00607    (*shader)["ScreenQuad"]->vertexAttribPointer("a_VertexPosition",3,GL_FLOAT,0,0);
00608    (*shader)["ScreenQuad"]->enableAttribArray("a_VertexPosition");
00609 
00610      // now we repeat for the UV data using the second VBO
00611    glBindBuffer(GL_ARRAY_BUFFER, vboID[1]);
00612    glBufferData(GL_ARRAY_BUFFER, 4*2*sizeof(GLfloat), texture, GL_STATIC_DRAW);
00613      // now bind
00614    (*shader)["ScreenQuad"]->vertexAttribPointer("a_TextureCoord",2,GL_FLOAT,0,0);
00615    (*shader)["ScreenQuad"]->enableAttribArray("a_TextureCoord");
00616      // finally switch back to the default so we don't overwrite
00617    glEnableVertexAttribArray(0);
00618    glBindVertexArray(0);
00619 }
00620 
00621 void GLWindow::mouseMoveEvent (
00622                                QMouseEvent * _event
00623                               )
00624 {
00625   if(m_rotate && _event->buttons() == Qt::LeftButton)
00626   {
00627     m_spinYFace = ( m_spinYFace + (_event->x() - m_origX) ) % 360 ;
00628     m_spinXFace = ( m_spinXFace + (_event->y() - m_origY) ) % 360 ;
00629     m_origX = _event->x();
00630     m_origY = _event->y();
00631   }
00632 
00633   // re-draw GL
00634   updateGL();
00635 }
00636 
00637 
00638 //----------------------------------------------------------------------------------------------------------------------
00639 void GLWindow::mousePressEvent (
00640                                 QMouseEvent * _event
00641                                )
00642 {
00643   if(_event->button() == Qt::LeftButton)
00644   {
00645     m_origX = _event->x();
00646     m_origY = _event->y();
00647     m_rotate =true;
00648   }
00649 
00650 }
00651 
00652 //----------------------------------------------------------------------------------------------------------------------
00653 void GLWindow::mouseReleaseEvent (
00654                                   QMouseEvent * _event
00655                                  )
00656 {
00657   if (_event->button() == Qt::LeftButton)
00658   {
00659     m_rotate=false;
00660   }
00661 }
00662 
00663 void GLWindow::processKeyPress(QKeyEvent *_event)
00664 {
00665     switch (_event->key())
00666     {
00667     default : break;
00668     }
00669 }
00670 
00671 void GLWindow::timerEvent(
00672                           QTimerEvent *_event
00673                          )
00674 {
00675   if(_event->timerId() == m_updatetimer)
00676   {
00677 
00678     m_time += m_timeStep;
00679     //m_scene.moveLights(m_time);
00680     updateGL();
00681   }
00682 }
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Defines