Add point lights
This commit is contained in:
@@ -74,8 +74,6 @@ Light *Light::Create() {
|
||||
|
||||
Name Light::GetType() const {return "Light";}
|
||||
|
||||
Name DirectionalLight::GetType() const {return "DirectionalLight";}
|
||||
|
||||
DirectionalLight::DirectionalLight() {
|
||||
this->direction = Vector3(0.0f);
|
||||
this->color = Vector3(0.0f);
|
||||
@@ -114,3 +112,46 @@ DirectionalLight *DirectionalLight::Duplicate() {
|
||||
DirectionalLight *DirectionalLight::Instance() {
|
||||
return static_cast<DirectionalLight*>(Node::Instance());
|
||||
}
|
||||
|
||||
Name DirectionalLight::GetType() const {return "DirectionalLight";}
|
||||
|
||||
PointLight::PointLight() {
|
||||
this->radius = 0.0f;
|
||||
this->color = Vector3(0.0f);
|
||||
this->ambient = 0.0f;
|
||||
this->diffuse = 0.0f;
|
||||
this->specular = 0.0f;
|
||||
}
|
||||
|
||||
PointLight::PointLight(float radius, Vector3 color, float ambient, float diffuse, float specular) {
|
||||
this->radius = radius;
|
||||
this->color = color;
|
||||
this->ambient = ambient;
|
||||
this->diffuse = diffuse;
|
||||
this->specular = specular;
|
||||
}
|
||||
|
||||
float PointLight::GetRadius() {
|
||||
return radius;
|
||||
}
|
||||
|
||||
void PointLight::SetRadius(float radius) {
|
||||
this->radius = radius;
|
||||
}
|
||||
|
||||
PointLight *PointLight::Create() {
|
||||
return new PointLight;
|
||||
}
|
||||
|
||||
PointLight *PointLight::Duplicate() {
|
||||
PointLight *pointLight = static_cast<PointLight*>(Light::Duplicate());
|
||||
pointLight->radius = radius;
|
||||
|
||||
return pointLight;
|
||||
}
|
||||
|
||||
PointLight *PointLight::Instance() {
|
||||
return static_cast<PointLight*>(Node::Instance());
|
||||
}
|
||||
|
||||
Name PointLight::GetType() const {return "PointLight";}
|
||||
|
||||
31
core/Light.h
31
core/Light.h
@@ -24,6 +24,8 @@
|
||||
#ifndef LIGHT_H
|
||||
#define LIGHT_H
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "types.h"
|
||||
#include "Spatial.h"
|
||||
|
||||
@@ -117,4 +119,33 @@ private:
|
||||
Vector3 direction;
|
||||
};
|
||||
|
||||
/**
|
||||
Point lights are omnidirectional lights that have a limited range.
|
||||
*/
|
||||
class PointLight : public Light {
|
||||
public:
|
||||
PointLight();
|
||||
PointLight(float radius, Vector3 color, float ambient, float diffuse, float specular);
|
||||
|
||||
/**
|
||||
The radius of which the light will shine.
|
||||
@returns The light radius.
|
||||
*/
|
||||
float GetRadius();
|
||||
/**
|
||||
Sets the light radius.
|
||||
@param radius The desired radius
|
||||
*/
|
||||
void SetRadius(float radius);
|
||||
|
||||
virtual Name GetType() const;
|
||||
virtual PointLight *Create();
|
||||
virtual PointLight *Duplicate();
|
||||
virtual PointLight *Instance();
|
||||
private:
|
||||
float radius;
|
||||
};
|
||||
|
||||
typedef std::vector<PointLight*> PointLightList;
|
||||
|
||||
#endif /* LIGHT_H */
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "Shader.h"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
Shader::Shader(const char* vertexCode, const char* fragmentCode) {
|
||||
Id vertex, fragment;
|
||||
int success;
|
||||
@@ -82,6 +84,16 @@ void Shader::UpdateDirectionalLight(DirectionalLight directionalLight) {
|
||||
glUniform1f(glGetUniformLocation(id, "directionalLight.specular"), directionalLight.GetSpecular());
|
||||
}
|
||||
|
||||
void Shader::UpdatePointLights(PointLightList pointLights) {
|
||||
for (int i = 0; i < pointLights.size() and i < NUM_POINT_LIGHTS; i++) {
|
||||
glUniform3fv(glGetUniformLocation(id, Util::ShaderArrayName("pointLights", i, "pos").c_str()), 1, glm::value_ptr(pointLights[i]->GetTransform().position));
|
||||
glUniform3fv(glGetUniformLocation(id, Util::ShaderArrayName("pointLights", i, "color").c_str()), 1, glm::value_ptr(pointLights[i]->GetColor()));
|
||||
|
||||
glUniform1f(glGetUniformLocation(id, Util::ShaderArrayName("pointLights", i, "radius").c_str()), pointLights[i]->GetRadius());
|
||||
glUniform1f(glGetUniformLocation(id, Util::ShaderArrayName("pointLights", i, "ambient").c_str()), pointLights[i]->GetAmbient());
|
||||
}
|
||||
}
|
||||
|
||||
Name Shader::GetName() const {
|
||||
return "Unnamed Shader";
|
||||
}
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
#include "../Material.h"
|
||||
#include "../Light.h"
|
||||
|
||||
#define NUM_POINT_LIGHTS 4
|
||||
|
||||
class Shader {
|
||||
public:
|
||||
Id id;
|
||||
@@ -21,6 +23,7 @@ public:
|
||||
void UpdateMaterial(Material material);
|
||||
|
||||
void UpdateDirectionalLight(DirectionalLight directionalLight);
|
||||
void UpdatePointLights(PointLightList pointLights);
|
||||
|
||||
virtual Name GetName() const;
|
||||
};
|
||||
|
||||
@@ -14,3 +14,9 @@ void Util::Die(std::string msg) {
|
||||
std::cerr << msg << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
std::string Util::ShaderArrayName(const char* arrName, int index, const char* memberName) {
|
||||
std::stringstream ss;
|
||||
ss << arrName << "[" << index << "]." << memberName;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
16
core/Util.h
16
core/Util.h
@@ -2,7 +2,9 @@
|
||||
#define UTIL_H
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "types.h"
|
||||
@@ -28,6 +30,20 @@ namespace Util {
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
template<class T>
|
||||
std::vector<T*> FindNodesByType(Node *&root, const Name &type) {
|
||||
std::vector<T*> nodes;
|
||||
if (root->GetType() == type) {
|
||||
nodes.push_back(dynamic_cast<T*>(root));
|
||||
}
|
||||
for (Node *child : root->GetChildren()) {
|
||||
std::vector<T*> childs = FindNodesByType<T>(child, type);
|
||||
nodes.insert(nodes.begin(), childs.begin(), childs.end());
|
||||
}
|
||||
return nodes;
|
||||
}
|
||||
|
||||
std::string ShaderArrayName(const char* arrName, int index, const char* memberName);
|
||||
}
|
||||
|
||||
#endif /* UTIL_H */
|
||||
|
||||
@@ -141,6 +141,9 @@ int main() {
|
||||
shader->UpdateDirectionalLight(DirectionalLight());
|
||||
}
|
||||
|
||||
PointLightList pointLights = Util::FindNodesByType<PointLight>(root, "PointLight");
|
||||
shader->UpdatePointLights(pointLights);
|
||||
|
||||
// Render the scene tree
|
||||
render(root, shader, Matrix(1.0f));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user