nicer particles, adding code so that particles don't penetrate through planets. added not-yet compeleted intersection code.

This commit is contained in:
Andreas Ortmann 2016-10-02 20:10:43 +02:00
parent 57e6e56217
commit 4712926be2
6 changed files with 207 additions and 17 deletions

View file

@ -25,10 +25,10 @@ namespace endofthejedi {
// TODO
// it is transformed before uploading so it looks at the camera
, m_data_geometry({
1.0f, -1.0f,
1.0f, 1.0f,
-1.0f, 1.0f,
-1.0f, -1.0f})
0.5f, -0.5f,
0.5f, 0.5f,
-0.5f, 0.5f,
-0.5f, -0.5f})
{
m_num_vertex_buffers = 4;

View file

@ -49,9 +49,9 @@ namespace endofthejedi {
// TODO: add dust particles
// TODO: add little rocks flying around
glClearColor(0.0, 0.0, 0.0, 1.0);
//float s = 0.1;
//glClearColor(s, s, s, 1.0);
//glClearColor(0.0, 0.0, 0.0, 1.0);
float s = 0.1;
glClearColor(s, s, 1.2*s, 1.0);
m_shader_game_objects.bind();
@ -94,11 +94,11 @@ namespace endofthejedi {
}
}
void RendererPolygon3d::addExplosionEffect(size_t id, const glm::vec2 &pos, size_t n, float duration)
void RendererPolygon3d::addExplosionEffect(size_t id, const glm::vec2 &explCenter, size_t n, float duration)
{
//float particleRadius = 0.005;
//float particleRadius = 0.003;
float particleRadius = 0.005;
float particleRadius = 0.02;
// TODO: use this as shader input too and make the area 2x around this
// so that it stays hot/yellow for 2/3 of the time
@ -107,7 +107,7 @@ namespace endofthejedi {
ParticleBatch *batch = new ParticleBatch(id, n, particleRadius, duration);
batch->setup(&m_shader_particles);
batch->setCenter(glm::vec3(pos, 0.0));
batch->setCenter(glm::vec3(explCenter, 0.0));
batch->setMaxVelocity(maxVelocity);
for (size_t i=0; i<n; i++) {
@ -121,11 +121,84 @@ namespace endofthejedi {
// especially in 3d this would look bad without 3d velocity vector.
//glm::vec3 v = 0.5f*glm::vec3(sin(t), cos(t), util::randf_m1_1());
glm::vec3 pos = glm::vec3(explCenter, 0.0) + glm::ballRand(explCoreSize);
glm::vec3 v = glm::ballRand(maxVelocity);
v *= util::randf_0_1() * util::randf_0_1() * util::randf_0_1();
float maxDist = 0.2;
batch->setParticle(i, glm::vec3(pos, 0.0) + glm::ballRand(explCoreSize), v, maxDist);
// find collisions with planetns and limit max distance so particles
// won't fly through planets
//float maxDist = 0.1;
//float maxDist = 0.1*util::randf_0_1();
bool isInsidePlanet = false;
float maxParticleDist = INFINITY;
const game::Planet *nearestPlanet = nullptr;
for (const game::Planet *planet : m_state->planets) {
const glm::vec3 ppos3 = glm::vec3(planet->position, 0.0f);
// TODO: check if inside planet!
float dist = glm::distance(ppos3, pos);
if (dist <= planet->radius) {
isInsidePlanet = true;
nearestPlanet = planet;
}
if (isInsidePlanet) {
// skip searching for nearer planets once we are inside some
// planet as the position/velocity will be changed to start
// at the surface of the planet we were in with
// reflected or planet-normal velocity.
continue;
}
// TODO: if inside, move position so that it looks like
// reflecting the particle from the planet
bool fliesInPlanetDirection = glm::dot(v, ppos3-pos) > 0.0f;
if (dist < maxParticleDist && fliesInPlanetDirection) {
nearestPlanet = planet;
maxParticleDist = dist;
}
}
if (isInsidePlanet) {
util::IntersectionTest intersect;
if (!intersect.raySphere(
pos, v,
glm::vec3(nearestPlanet->position, 0.0f), nearestPlanet->radius))
{
//std::cout<<"warning: intersection should be valid!" << std::endl;
// TODO: must be as they lie on a plane and the dist is < as
// the radius.
// handle if this is wrong.
} else {
// set position to the intersection point between explosion
// center and planet surface
//const glm::vec3 planetNormal = glm::vec3(glm::normalize(explCenter - nearestPlanet->position), 0.0f);
//pos = glm::vec3(nearestPlanet->position, 0.0f) + nearestPlanet->radius*planetNormal;
// build new velocity by reflecting the old velocity on the
// planet normal
// TODO: add a bit random
// TODO: add reflection
// TODO: distribute particles around main reflection angle and
// TODO: add material exhaust that is specific for the planet.
// TODO: spawn waves on water planet
// TODO: start fire on gas planet
//v = glm::length(v) * planetNormal;
//v = v - 2.0f*glm::dot(v, planetNormal) * planetNormal;
//v = glm::length(v) * planetNormal;
//glm::vec3 r = v - 2.0f*glm::dot(v, planetNormal) * planetNormal;
//glm::vec3 vn = glm::length(v) * planetNormal;
//v = (r+vn) / 2.0f;
//glm::vec3 vc = glm::vec3(nearestPlanet->position-explCenter, 0.0f);
//glm::vec3 r = vc - 2.0f*glm::dot(vc, planetNormal) * planetNormal;
//v = r;
}
}
batch->setParticle(i, pos, v, maxParticleDist);
}
batch->upload();
@ -189,6 +262,8 @@ namespace endofthejedi {
void RendererPolygon3d::renderMissiles()
{
// TODO: add fire trail for missiles near the sun
m_missileModel->bind();
for (const game::Player *player : m_state->players) {