Added directional lighting.

This commit is contained in:
Fabian Klemp 2014-11-17 17:51:15 +01:00
parent e3c8be4d0e
commit c87d62cae9
4 changed files with 33 additions and 5 deletions

View File

@ -14,6 +14,9 @@ uniform float specularFactor;
uniform vec3 camera; uniform vec3 camera;
uniform float shininess; uniform float shininess;
uniform int lightCount; uniform int lightCount;
uniform vec3 directionalLightVector;
uniform vec3 directionalColor;
uniform float directionalIntensity;
uniform vec3 lightSources[128]; uniform vec3 lightSources[128];
uniform vec3 lightColors[128]; uniform vec3 lightColors[128];
uniform float lightIntensities[128]; uniform float lightIntensities[128];
@ -23,6 +26,15 @@ void main()
vec3 ambientColor = ambientFactor * ambientColor; vec3 ambientColor = ambientFactor * ambientColor;
vec3 diffuseColor = vec3(0.0, 0.0, 0.0); vec3 diffuseColor = vec3(0.0, 0.0, 0.0);
vec3 specularColor = vec3(0.0, 0.0, 0.0); vec3 specularColor = vec3(0.0, 0.0, 0.0);
if(length(directionalLightVector)>0.0f) {
vec3 directionalVector = normalize(directionalLightVector);
diffuseColor += dot(normalize(vNormal), directionalVector)
*diffuseFactor*directionalIntensity*directionalColor;
vec3 cameraVector = normalize(camera - vec3(fragPosition));
specularColor += clamp(pow((dot((cameraVector+directionalVector),normalize(vNormal))/(length(cameraVector+directionalVector)*length(normalize(vNormal)))),shininess), 0.0, 1.0)
*specularFactor*directionalIntensity*directionalColor;
}
for(int i = 0; i<lightCount; i++) { for(int i = 0; i<lightCount; i++) {
float distance = distance(lightSources[i], vec3(fragPosition)); float distance = distance(lightSources[i], vec3(fragPosition));
// only take lights into account with meaningful contribution // only take lights into account with meaningful contribution

View File

@ -129,6 +129,15 @@ void Graphics::render(Level* level, ACGL::OpenGL::SharedShaderProgram shader)
glUniform1fv(shader->getUniformLocation("lightIntensities"), glUniform1fv(shader->getUniformLocation("lightIntensities"),
sizeof(lightIntensities), (GLfloat*) lightIntensities); sizeof(lightIntensities), (GLfloat*) lightIntensities);
} }
// set directional Light
if(level->getDirectionalLight()) {
shader->setUniform("directionalLightVector",
level->getDirectionalLight()->getPosition());
shader->setUniform("directionalColor",
level->getDirectionalLight()->getColour());
shader->setUniform("directionalIntensity",
level->getDirectionalLight()->getIntensity());
}
// set Material Parameters // set Material Parameters
shader->setUniform("ambientColor", level->getAmbientLight()); shader->setUniform("ambientColor", level->getAmbientLight());

View File

@ -44,25 +44,26 @@ void Level::load(ACGL::OpenGL::SharedShaderProgram shader) {
Model blockModel = Model("Block.obj", 1.0f); Model blockModel = Model("Block.obj", 1.0f);
Material blockMaterial = Material("blockTexture.png", 0.1f, 0.6, 0.4f, 2.0f); Material blockMaterial = Material("blockTexture.png", 0.1f, 0.6, 0.4f, 2.0f);
Object blockObject = Object(blockModel, blockMaterial, glm::vec3(2.0f, 6.0f, 2.0f), Object blockObject = Object(blockModel, blockMaterial, glm::vec3(2.0f, 7.0f, 2.0f),
glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f),
shader); shader);
objects.push_back(blockObject); objects.push_back(blockObject);
Model columnModel = Model("Column.obj", 1.0f); Model columnModel = Model("Column.obj", 1.0f);
Material columnMaterial = Material("columnTexture.png", 0.1f, 0.6, 0.4f, 2.0f); Material columnMaterial = Material("columnTexture.png", 0.1f, 0.6, 0.4f, 2.0f);
Object columnObject = Object(columnModel, columnMaterial, glm::vec3(-2.0f, 6.0f, -2.0f), Object columnObject = Object(columnModel, columnMaterial, glm::vec3(-2.0f, 7.0f, -2.0f),
glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f),
shader); shader);
objects.push_back(columnObject); objects.push_back(columnObject);
//set lighting parameters //set lighting parameters
ambientLight = glm::vec3(1.0f, 1.0f, 1.0f); ambientLight = glm::vec3(1.0f, 1.0f, 1.0f);
Light light = Light(glm::vec3(-3.0f, 7.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(1.0f, 1.0f, 1.0f), 10.0f); directionalLight = Light(glm::vec3(-0.5f, 0.0f, -0.5f), glm::vec3(1.0f, 1.0f, 0.0f), 0.4f);
Light light = Light(glm::vec3(-3.0f, 7.0f, 0.0f), glm::vec3(1.0f, 1.0f, 1.0f), 10.0f);
lights.push_back(light); lights.push_back(light);
Light light2 = Light(glm::vec3(3.0f, 6.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(1.0f, 1.0f, 1.0f), 10.0f); Light light2 = Light(glm::vec3(3.0f, 6.0f, 0.0f), glm::vec3(1.0f, 1.0f, 1.0f), 10.0f);
lights.push_back(light2); lights.push_back(light2);
Light light3 = Light(glm::vec3(0.0f, 5.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.5f, 0.5f, 1.0f), 4.0f); Light light3 = Light(glm::vec3(0.0f, 5.0f, 0.0f), glm::vec3(0.5f, 0.5f, 1.0f), 4.0f);
lights.push_back(light3); lights.push_back(light3);
@ -137,3 +138,7 @@ Camera* Level::getCamera() {
Object* Level::getCameraCenter() { Object* Level::getCameraCenter() {
return cameraCenter; return cameraCenter;
} }
Light* Level::getDirectionalLight() {
return &directionalLight;
}

View File

@ -19,6 +19,7 @@ class Level {
void update(float runTime, glm::vec2 mouseDelta,bool wPressed, bool aPressed,bool sPressed, bool dPressed); void update(float runTime, glm::vec2 mouseDelta,bool wPressed, bool aPressed,bool sPressed, bool dPressed);
void render(); void render();
glm::vec3 getAmbientLight(); glm::vec3 getAmbientLight();
Light* getDirectionalLight();
std::vector<Light> getLights(); std::vector<Light> getLights();
Object* getCameraCenter(); Object* getCameraCenter();
Camera* getCamera(); Camera* getCamera();
@ -27,6 +28,7 @@ class Level {
std::vector<Object> objects; std::vector<Object> objects;
std::vector<Light> lights; std::vector<Light> lights;
glm::vec3 ambientLight; glm::vec3 ambientLight;
Light directionalLight;
Object* cameraCenter; Object* cameraCenter;
Physics physics; Physics physics;
Camera camera; Camera camera;