explosions make light now.

This commit is contained in:
Andreas Ortmann 2016-10-03 10:47:02 +02:00
parent 544282dd84
commit fc2d4c5028
7 changed files with 81 additions and 13 deletions

View file

@ -10,6 +10,10 @@ uniform vec3 materialColor;
uniform int materialKind; uniform int materialKind;
uniform int materialSeed; uniform int materialSeed;
uniform int explLightsNum;
uniform vec3 explLightsPos[10];
uniform float explLightsIntensities[10];
void main() void main()
{ {
vec3 Eye = normalize(-vertex); vec3 Eye = normalize(-vertex);
@ -27,7 +31,31 @@ void main()
vec3 color = materialColor; vec3 color = materialColor;
color = max(vec3(0.0), min(vec3(1.0), color)); color = max(vec3(0.0), min(vec3(1.0), color));
vec3 IDiffuse = vec3(color) * lightColor * max(dot(normal, lightDirection), 0.0); //vec3 light = lightColor * max(dot(normal, lightDirection), 0.0);
vec3 light = vec3(0.0);
int i;
for (i=0; i<explLightsNum; i++) {
vec3 explLightColor = vec3(1.0, 0.8, 0.3);
vec3 diff = vertex - explLightsPos[i];
float l = 10.0*length(diff);
float dir = max(0.0, -dot(normal, diff));
float dp = max(0.0, 1.0-l);
float intensity = 10.0;
if (dp == 0.0) {
intensity *= dir;
} else {
intensity *= dp;
}
intensity /= 1.0 + 0.5*l*l;
light += intensity * pow(explLightsIntensities[i], 2.0) * explLightColor;
}
light = max(vec3(0.0), light);
vec3 IDiffuse = vec3(color) * light;
// TODO make instensity/exponent as parameter // TODO make instensity/exponent as parameter
//vec3 ISpecular = lightColor * 5.0 * pow(max(dot(Reflected, Eye), 0.0), 2.0); //vec3 ISpecular = lightColor * 5.0 * pow(max(dot(Reflected, Eye), 0.0), 2.0);

View file

@ -1,5 +1,4 @@
#version 120 #version 120
varying vec3 position;
varying vec2 vertex; varying vec2 vertex;
varying vec3 velocity; varying vec3 velocity;
varying float decay; varying float decay;

View file

@ -14,7 +14,6 @@ varying float decay;
varying vec3 velocity; varying vec3 velocity;
varying vec2 vertex; varying vec2 vertex;
varying float explCenterDist; varying float explCenterDist;
varying vec3 position;
uniform float aspectRatio; uniform float aspectRatio;
@ -47,7 +46,6 @@ void main()
vertex = base.xy; vertex = base.xy;
velocity = in_velocity; velocity = in_velocity;
position = in_position;
explCenterDist = length(explCenter - offset); explCenterDist = length(explCenter - offset);
} }

View file

@ -15,8 +15,13 @@ namespace endofthejedi {
: m_id(id) : m_id(id)
, m_numParticles(numParticles) , m_numParticles(numParticles)
, m_particleRadius(particleSize) , m_particleRadius(particleSize)
, m_halfAge(halfAge)
, m_age(0.0) , m_age(0.0)
, m_halfAge(halfAge)
// XXX this is used in some places in the shader.
// set it via uniform too
, m_maxNumHalfAges(5.0)
, m_maxVelocity(1.0) , m_maxVelocity(1.0)
, m_center(glm::vec3(0.0f, 0.0f, 0.0f)) , m_center(glm::vec3(0.0f, 0.0f, 0.0f))
@ -207,6 +212,6 @@ namespace endofthejedi {
bool ParticleBatch::done() const bool ParticleBatch::done() const
{ {
return m_age >= 5.0*m_halfAge; return m_age >= m_maxNumHalfAges*m_halfAge;
} }
} }

View file

@ -36,10 +36,15 @@ namespace endofthejedi {
void render(Shader *shader); void render(Shader *shader);
void tick(float dt); void tick(float dt);
float ageNormalized() const { return m_age / (m_maxNumHalfAges*m_halfAge); }
float age() const { return m_age; }
bool done() const; bool done() const;
size_t id() const { return m_id; } size_t id() const { return m_id; }
const glm::vec3 &explosionCenter() const { return m_center; }
private: private:
size_t dataSizeForIndex(size_t index); size_t dataSizeForIndex(size_t index);
void *dataSourceForIndex(size_t index); void *dataSourceForIndex(size_t index);
@ -51,8 +56,9 @@ namespace endofthejedi {
// uniforms for the shader // uniforms for the shader
size_t m_numParticles; size_t m_numParticles;
float m_particleRadius; float m_particleRadius;
const float m_halfAge;
float m_age; float m_age;
const float m_halfAge;
const float m_maxNumHalfAges;
float m_maxVelocity; float m_maxVelocity;
glm::vec3 m_center; glm::vec3 m_center;

View file

@ -29,7 +29,7 @@ namespace endofthejedi {
//addModel("../data/mesh/small_atomic_bomb.stl", &m_missileModel); //addModel("../data/mesh/small_atomic_bomb.stl", &m_missileModel);
addModel("../data/mesh/rocket.stl", &m_missileModel); addModel("../data/mesh/rocket.stl", &m_missileModel);
addModel("../data/mesh/planet_12.stl", &m_planetModel); addModel("../data/mesh/planet_128.stl", &m_planetModel);
addModel("../data/mesh/ship_ufo.stl", &m_shipModel); addModel("../data/mesh/ship_ufo.stl", &m_shipModel);
} }
@ -60,7 +60,7 @@ namespace endofthejedi {
// TODO: add ONE sun planet // TODO: add ONE sun planet
// TODO: add lights for explosions // TODO: add lights for explosions
configureLightningInShader(); configureLightningInShader(&m_shader_game_objects);
//std::cout<<"setting aspect ratio: " << m_aspectRatio << std::endl; //std::cout<<"setting aspect ratio: " << m_aspectRatio << std::endl;
glUniform1f(m_shader_game_objects.location("aspectRatio"), m_aspectRatio); glUniform1f(m_shader_game_objects.location("aspectRatio"), m_aspectRatio);
@ -452,7 +452,7 @@ namespace endofthejedi {
glPolygonMode(GL_FRONT, GL_FILL); glPolygonMode(GL_FRONT, GL_FILL);
} }
void RendererPolygon3d::configureLightningInShader() void RendererPolygon3d::configureLightningInShader(Shader *shader) const
{ {
// TODO: add a few small lights for explosions so they lit the // TODO: add a few small lights for explosions so they lit the
// surroundsings // surroundsings
@ -469,8 +469,40 @@ namespace endofthejedi {
} }
} }
glUniform3f(m_shader_game_objects.location("lightPosition"), p.x, p.y, p.z); glUniform3f(shader->location("lightPosition"), p.x, p.y, p.z);
glUniform3f(m_shader_game_objects.location("lightColor"), c.x, c.y, c.z); glUniform3f(shader->location("lightColor"), c.x, c.y, c.z);
std::vector<glm::vec3> positions;
std::vector<float> intensities;
size_t numExplLights = 0;
for (ParticleBatch *batch : m_particles) {
float age = batch->ageNormalized();
// TODO: use function with a peak for this:
//
// /\__
// _/ \--____
float intensity = 1.0-age;
glm::vec3 p = batch->explosionCenter();
intensities.push_back(intensity);
positions.push_back(p);
numExplLights++;
if (numExplLights == 10) {
break;
}
}
glUniform1i( shader->location("explLightsNum"), numExplLights);
if (numExplLights != 0) {
glUniform3fv(shader->location("explLightsPos"), numExplLights, glm::value_ptr(positions[0]));
glUniform1fv(shader->location("explLightsIntensities"), numExplLights, intensities.data());
}
} }
void RendererPolygon3d::setWindowSize(int px, int py) void RendererPolygon3d::setWindowSize(int px, int py)

View file

@ -59,7 +59,7 @@ namespace endofthejedi {
void renderTraces(); void renderTraces();
void configureLightningInShader(); void configureLightningInShader(Shader *shader) const;
private: private:
// all models are also here (for easy reloading etc.) // all models are also here (for easy reloading etc.)