kick
/Users/morten/Programmering/cpp/kick/src/kick/scene/scene_lights.cpp
00001 //
00002 //  scene_lights.cpp
00003 //  KickCPP
00004 //
00005 //  Created by morten on 8/14/13.
00006 //  Copyright (c) 2013 Morten Nobel-Joergensen. All rights reserved.
00007 //
00008 
00009 #include "kick/scene/scene_lights.h"
00010 #include <iostream>
00011 #include <algorithm>
00012 #include "kick/scene/component.h"
00013 #include "kick/scene/light.h"
00014 #include "kick/scene/game_object.h"
00015 #include "kick/scene/transform.h"
00016 
00017 #include "glm/gtx/quaternion.hpp"
00018 
00019 using namespace std;
00020 using namespace glm;
00021 
00022 namespace kick {
00023     
00024     void SceneLights::recomputeLight(mat4 viewMatrix){
00025         mat3 viewMatrixRotation = (mat3)viewMatrix;
00026         if (directionalLight){
00027             // compute light direction
00028             vec3 lightDirection{0,0,-1};
00029             auto transform = directionalLight->gameObject()->transform();
00030             mat3 rotation = mat3_cast(transform->rotation());
00031             directionalLightWorld = rotation * lightDirection;
00032 
00033             // transform to eye space
00034             vec3 directionalLightDirection = normalize(viewMatrixRotation * directionalLightWorld);
00035             directionalLightData[0] = directionalLightDirection;
00036 
00037             // compute half vector
00038             directionalLightData[2] = -normalize(vec3(0,0,-1) + directionalLightDirection);
00039 
00040             // set color intensity
00041             directionalLightData[1] = directionalLight->colorIntensity();
00042         }  else {
00043             directionalLightData = mat3(0);
00044         }
00045         int i = 0;
00046         for (;i<std::min((size_t)KICK_MAX_POINT_LIGHTS, pointLights.size());i++){
00047             auto light = pointLights[i];
00048             auto transform = light->gameObject()->transform();
00049 
00050             // save eyespace position
00051             pointLightData[i][0] = (vec3)(viewMatrix * vec4(transform->position(),1.0f));
00052             pointLightData[i][1] = light->colorIntensity();
00053             pointLightData[i][2] = light->attenuation();
00054         }
00055         // reset unused
00056         for (;i<KICK_MAX_POINT_LIGHTS;i++){
00057             pointLightData[i] = mat3(0);
00058         }
00059     }
00060 
00061     void SceneLights::clear() {
00062         ambientLight = nullptr;
00063         directionalLight = nullptr;
00064         pointLights.clear();
00065         directionalLightWorld = vec3{0};
00066     }
00067 
00068 }
 All Classes Functions Variables