DeferredRenderer 1.0

Scene.cpp

Go to the documentation of this file.
00001 #include "Scene.h"
00002 #include "string"
00003 #include "ngl/TransformStack.h"
00004 #include "ngl/ShaderManager.h"
00005 #include "ngl/Material.h"
00006 #include "ngl/Obj.h"
00007 #include "ngl/NCCABinMesh.h"
00008 #include <QImage>
00009 //#include "ngl/VBOPrimitives.h"
00010 
00011 
00012 #ifdef DARWIN
00013     #define glGenVertexArrays glGenVertexArraysAPPLE
00014     #define glBindVertexArray glBindVertexArrayAPPLE
00015 #endif
00016 
00017 Scene::Scene(int _lightNum)
00018 {   
00019     m_ySpin = 0.0;
00020     m_pointlights.resize(_lightNum);
00021     m_lightVAO = 0;
00022 }
00023 
00024 Scene::~Scene()
00025 {
00026 
00027 }
00028 
00029 void Scene::drawLights(const ngl::Vector &_camPos, unsigned int _pointLightVAO)
00030 {
00031     glPointSize(2.0);
00032     glEnable(GL_POINT_SMOOTH);
00033 
00034     for(unsigned int i = 0; i < m_pointlights.size(); i++)
00035     {
00036         if(m_pointlights[i].collidesWithSphere(_camPos, 10.0))
00037         {
00038             glCullFace(GL_FRONT);
00039             glDisable(GL_DEPTH_TEST);
00040             m_pointlights[i].draw(_pointLightVAO, "PointLight");
00041             glEnable(GL_DEPTH_TEST);
00042             glCullFace(GL_BACK);
00043         }
00044         else
00045         {
00046             m_pointlights[i].draw(_pointLightVAO, "PointLight");
00047         }
00048 
00049 
00050         // draw the points to represent the demo lights
00051         ngl::ShaderManager *shader = ngl::ShaderManager::instance();
00052         (*shader)["DemoPL"]->use();
00053 
00054         shader->setShaderParam3f("DemoPL",
00055                                  "u_Offset",
00056                                  m_pointlights[i].m_pos.m_x,
00057                                  m_pointlights[i].m_pos.m_y,
00058                                  m_pointlights[i].m_pos.m_z
00059                                  );
00060 
00061         glBindVertexArray(m_lightVAO);
00062 
00063         glDrawArrays(GL_POINTS, 0, 1);
00064 
00065         glBindVertexArray(0);
00066     }
00067 
00068     glPointSize(1.0);
00069     glDisable(GL_POINT_SMOOTH);
00070 }
00071 
00072 void Scene::moveLights(const float &_time)
00073 {
00074     float speed = 0.01;
00075     float amp = 2.7;
00076 
00077     float movement=sin(_time*speed);
00078 
00079     movement = (movement*0.5)+0.5;
00080     amp+=movement*2.4;
00081 
00082     for(unsigned int i = 0; i < m_pointlights.size(); i++)
00083     {
00084         m_pointlights[i].m_pos = ngl::Vector(sin(i+_time*speed)*amp, m_pointlights[i].m_pos.m_y, cos(i+_time*speed)*amp);//+ngl::Vector(5,0,5);
00085     }
00086 }
00087 
00088 void Scene::draw(const std::string &_shaderName, const Frustum &_f, bool _ySpin)
00089 {
00090     ngl::ShaderManager *shader = ngl::ShaderManager::instance();
00091 
00092     m_baseTrans.rotateY(m_ySpin);
00093 
00094     if(_ySpin)
00095     {
00096         m_ySpin += 0.1;
00097     }
00098 
00099     (*shader)[_shaderName]->use();
00100 
00101     ngl::Material m(ngl::GOLD);
00102     m.use();
00103 
00104 //    for(unsigned int i = 0; i < m_gameObjects.size(); i++)
00105 //    {
00106 //        ngl::Matrix model ,rot, trans, scale;
00107 //        trans.translate(m_gameObjects[i].trans.m_x, m_gameObjects[i].trans.m_y, m_gameObjects[i].trans.m_z);
00108 //        rot.rotateY(m_gameObjects[i].rot.m_y);
00109 //        scale.scale(m_gameObjects[i].scale.m_x, m_gameObjects[i].scale.m_y, m_gameObjects[i].scale.m_z);
00110 
00111 //        model = trans*rot*scale;
00112 
00113 //        shader->setShaderParamFromMatrix(_shaderName,"u_ModelMatrix",model);
00114 
00115 //        //if( _f.isInFrustum(m_gameObjects[i].trans))
00116 //        //{
00117 //            glBindTexture(GL_TEXTURE_2D, m_gameObjects[i].texID);
00118 //            m_gameObjects[i].geo.draw();
00119 //        //}
00120 
00121 //    }
00122 
00123 //  glBindTexture(GL_TEXTURE_2D, m_blankTex);
00124 
00125 
00126     //glBindTexture(GL_TEXTURE_2D, 0);
00127 }
00128 
00129 void Scene::loadTexture(const char* _texName, GLuint &_texID)
00130 {
00131       QImage *image = new QImage();
00132       bool loaded=image->load(_texName);
00133       if(loaded == true)
00134       {
00135           int width=image->width();
00136           int height=image->height();
00137           unsigned char *data = new unsigned char[width*height*3];
00138           unsigned int index=0;
00139           QRgb colour;
00140           for( int y=0; y<height; ++y)
00141           {
00142               for( int x=0; x<width; ++x)
00143               {
00144                   colour=image->pixel(x,y);
00145 
00146                   data[index++]=qRed(colour);
00147                   data[index++]=qGreen(colour);
00148                   data[index++]=qBlue(colour);
00149               }
00150           }
00151 
00152           glGenTextures(1,&_texID);
00153           glBindTexture(GL_TEXTURE_2D,_texID);
00154 
00155           glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00156           glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
00157 
00158           glTexImage2D(GL_TEXTURE_2D,0,3,width,height,0,GL_RGB,GL_UNSIGNED_BYTE,data);
00159           glGenerateMipmap(GL_TEXTURE_2D);
00160   }
00161 }
00162 
00163 void Scene::addGameObject(
00164     const std::string &_obj,
00165     const char* _tex,
00166     const ngl::Vector &_trans,
00167     const ngl::Vector &_rot,
00168     const ngl::Vector &_scale
00169     )
00170 {
00171     //GameObject tmp;
00172 
00173 
00174 //    tmp.geo = ngl::Obj(_obj);
00175 //    GLuint tmpTex;
00176 //    loadTexture(_tex, tmpTex);
00177 //    tmp.texID = tmpTex;
00178 //    tmp.scale = _scale;
00179 //    tmp.trans = _trans;
00180 //    tmp.rot = _rot;
00181 //    m_gameObjects.push_back(tmp);
00182 }
00183 
00184 void Scene::init()
00185 {
00186     loadTexture("textures/BLANK.tiff", m_blankTex);
00187 
00188     //addGameObject("models/TrollLowRes.obj", "textures/TrollColour.tiff", ngl::Vector(5,1.85,5), ngl::Vector(0,0,0), ngl::Vector(5,5,5));
00189     //addGameObject("models/ControlBox1.obj", "textures/ControlBox1.tiff", ngl::Vector(3.5,-1,-4), ngl::Vector(0,-90,0), ngl::Vector(0.1,0.1,0.1));
00190 //    addGameObject("models/ControlBox2.obj",
00191 //                  "textures/ControlBox2.tiff",
00192 //                  ngl::Vector(6.0f,-1.0f,-4.0f,1.0f),
00193 //                  ngl::Vector(0.0f,-90.0f,0.0f,1.0f),
00194 //                  ngl::Vector(0.1f,0.1f,0.1f,1.0f));
00195 //    addGameObject("models/TNT.obj",
00196 //                  "textures/TNT.tiff",
00197 //                  ngl::Vector(6.0f,0.25f,-4.0f,1.0f),
00198 //                  ngl::Vector(0.0f,-90.0f,0.0f,1.0f),
00199 //                  ngl::Vector(0.1f,0.1f,0.1f,1.0f));
00200 //    addGameObject("models/Street.obj",
00201 //                  "textures/Street.tiff",
00202 //                  ngl::Vector(0.0f,-1.0f,0.0f,1.0f),
00203 //                  ngl::Vector(0.0f,0.0f,0.0f,1.0f),
00204 //                  ngl::Vector(20.0f,20.0f,20.0f,1.0f));
00205 //    addGameObject("models/Ruins.obj",
00206 //                  "textures/Ruins.tiff",
00207 //                  ngl::Vector(-1.0f,-1.0f,5.0f,1.0f),
00208 //                  ngl::Vector(0.0f,0.0f,0.0f,1.0f),
00209 //                  ngl::Vector(0.5f,0.5f,0.5f,1.0f));
00210 //    addGameObject("models/Ruins2.obj",
00211 //                  "textures/Ruins.tiff",
00212 //                  ngl::Vector(1.0f,-1.0f,1.0f,1.0f),
00213 //                  ngl::Vector(0.0f,0.0f,0.0f,1.0f),
00214 //                  ngl::Vector(0.5f,0.5f,0.5f,1.0f));
00215 //    addGameObject("models/Minebeams.obj",
00216 //                  "textures/Minebeams.tiff",
00217 //                  ngl::Vector(5.2f,-2.0f,0.6f,1.0f),
00218 //                  ngl::Vector(0.0f,0.0f,0.0f,1.0f),
00219 //                  ngl::Vector(0.42f,0.4f,0.65f,1.0f));
00220 //    addGameObject("models/Plane.obj",
00221 //                  "textures/Sky.jpg",
00222 //                  ngl::Vector(5.2f,2.0f,-8.0f,1.0f),
00223 //                  ngl::Vector(0.0f,0.0f,0.0f,1.0f),
00224 //                  ngl::Vector(10.0f,10.0f,10.0f,1.0f));
00225 
00226 //    addGameObject("models/Killerroo.obj",
00227 //                  "textures/Sky.jpg",
00228 //                  ngl::Vector(5.2f,2.0f,-8.0f,1.0f),
00229 //                  ngl::Vector(0.0f,0.0f,0.0f,1.0f),
00230 //                  ngl::Vector(10.0f,10.0f,10.0f,1.0f));
00231 
00232 //    addGameObject("models/WaterTower.obj",
00233 //                  "textures/WaterTower.tiff",
00234 //                  ngl::Vector(9.2f,-2.0f,1.6f,1.0f),
00235 //                  ngl::Vector(0.0f,0.0f,0.0f,1.0f),
00236 //                  ngl::Vector(0.1f,0.1f,0.1f,1.0f));
00237 
00238 //    for(unsigned int i = 0; i < m_gameObjects.size(); i++)
00239 //    {
00240 //        m_gameObjects[i].geo.createVBO(GL_STATIC_DRAW);
00241 //    }
00242 
00243 
00244 
00246     setupLights();
00247     std::cout << "Scene Created" << std::endl;
00248 }
00249 
00250 void Scene::setupLights()
00251 {
00252     ngl::ShaderManager *shader=ngl::ShaderManager::instance();
00253 
00254     for(unsigned int i = 0; i < m_pointlights.size(); i++)
00255     {
00256         ngl::Vector tmp;
00257         tmp = ngl::Vector(sin(i)*2.0,(float)i*0.06,cos(i)*2.0)+0.5;
00258         m_pointlights[i] = PointLight(tmp+ngl::Vector(5,0,5),2.2,ngl::Vector(1,1,1));
00259     }
00260 
00262     //setup light points
00263     (*shader)["DemoPL"]->use();
00264 
00265     float vertices[] = {0.0,0.0,0.0};
00266 
00267     glGenVertexArrays(1, &m_lightVAO);
00268     glBindVertexArray(m_lightVAO);
00269 
00270     GLuint vboIDLight;
00271     glGenBuffers(1, &vboIDLight);
00272 
00273     glBindBuffer(GL_ARRAY_BUFFER, vboIDLight);
00274     glBufferData(GL_ARRAY_BUFFER, 3*sizeof(GLfloat), vertices, GL_STATIC_DRAW);
00275 
00276     (*shader)["DemoPL"]->vertexAttribPointer("a_VertPos",3,GL_FLOAT,0,0);
00277     (*shader)["DemoPL"]->enableAttribArray("a_VertPos");
00278 
00279     glEnableVertexAttribArray(0);
00280     glBindVertexArray(0);
00281 }
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Defines