Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
gue-ni committed Oct 11, 2023
1 parent 7f5c5ae commit 21438df
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 24 deletions.
30 changes: 21 additions & 9 deletions OpenGL_Flightsim/src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
#include "flightmodel.h"
#include "terrain.h"

#define DRAW_HUD 0
#define DRAW_HUD 1
#define GROUND_START 1
#define PARTICLES 1
#define RENDER_LANDING_GEAR 1

const Airfoil NACA_0012(NACA_0012_data);
const Airfoil NACA_2412(NACA_2412_data);
Expand Down Expand Up @@ -201,19 +202,18 @@ void App::init_airplane()
#if PARTICLES
// afterburner

auto red_blue = gfx::Range(gfx::rgb(0xff0000), gfx::rgb(0x0000ff));
auto afterburner = gfx::Range(gfx::rgb(0xFFFFD4), gfx::rgb(0xA091A6));
auto afterburner = gfx::Range(gfx::rgb(0xFFA500), gfx::rgb(0x0000ff));
gfx::ParticleSystem::Config config = {.count = 5000U,
.emitter_radius = 0.35f,
.emitter_radius = 0.3f,
.emitter_cone = 0.01f,
.speed = gfx::Range(100.0f, 150.0f),
.size = gfx::Range(0.2f, 0.3f),
.size = gfx::Range(0.3f, 0.4f),
.lifetime = gfx::Range(0.02f, 0.03f),
.color = afterburner
};

m_particles = new gfx::ParticleSystem(config, "assets/textures/particle.png");
m_particles->set_position(glm::vec3(-5.5f, 0.0f, 0.0f));
m_particles->set_position(glm::vec3(-5.0f, 0.0f, 0.0f));
m_particles->set_rotation(glm::vec3(0.0f, 0.0f, glm::radians(90.0f)));
m_falcon->add(m_particles);
#endif
Expand Down Expand Up @@ -247,8 +247,9 @@ void App::init_airplane()

m_airplane = new Airplane(mass, inertia, wings, {Engine(thrust)}, collider);

#if 1
gfx::Object3D* landing_gear = new gfx::Object3D();
#if RENDER_LANDING_GEAR
landing_gear = new gfx::Object3D();
std::cout << "landing gear id = " << landing_gear->id << std::endl;
m_falcon->add(landing_gear);

auto wheel_texture = std::make_shared<gfx::gl::Texture>("assets/textures/container.jpg");
Expand All @@ -262,7 +263,8 @@ void App::init_airplane()
gfx::Mesh* wheel_mesh = new gfx::Mesh(wheel_geometry, wheel_material);
wheel_mesh->visible = true;
obj->add(wheel_mesh);
m_falcon->add(obj);
//m_falcon->add(obj);
landing_gear->add(obj);
}
#endif
}
Expand Down Expand Up @@ -342,6 +344,9 @@ void App::event_keydown(SDL_Keycode key)
case SDLK_p:
m_paused = !m_paused;
break;
case SDLK_g:
landing_gear->visible = !landing_gear->visible;
break;
}
}

Expand Down Expand Up @@ -527,10 +532,17 @@ void App::game_loop(float dt)
m_hud->batch_clear();

#if PARTICLES
float throttle = glm::clamp(m_airplane->throttle, 0.0f, 1.0f);
m_particles->m_config.lifetime.min_value = 0.005f + 0.02f * throttle;
m_particles->m_config.lifetime.max_value = 0.007f + 0.03f * throttle;
//m_particles->m_config.lifetime.max_value = 0.02f + 0.02f * m_airplane->throttle;
m_particles->update(dt, m_cameras[m_cameratype]->get_world_position(), m_airplane->velocity);
#endif
}

//landing_gear->visible = false;
//m_falcon->visible = false;

m_controller.update(*m_cameras[0], m_falcon->get_position(), dt);

m_renderer->render(m_cameras[m_cameratype], m_scene);
Expand Down
1 change: 1 addition & 0 deletions OpenGL_Flightsim/src/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class App

gfx::Object3D* m_camera_attachment;
gfx::Object3D* m_runway;
gfx::Object3D* landing_gear;

// physics
Airplane* m_airplane;
Expand Down
5 changes: 3 additions & 2 deletions OpenGL_Flightsim/src/gfx/object3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ void Object3D::draw(RenderContext& context)
draw_self(context);

if (wireframe) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

draw_children(context);
}
draw_children(context);
}

void Object3D::draw_self(RenderContext& context) {}
Expand Down Expand Up @@ -113,7 +114,7 @@ glm::mat4 Object3D::get_transform() const
{
// this function must only be called after updating the transform
if (m_dirty) {
//assert(!m_dirty);
// assert(!m_dirty);
}
return m_transform;
}
Expand Down
18 changes: 6 additions & 12 deletions OpenGL_Flightsim/src/gfx/particles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ int ParticleSystem::find_unused_particle()

void ParticleSystem::update(float dt, const glm::vec3& camera_position, const glm::vec3& emitter_velocity)
{
int new_particles = 100;
int new_particles = 200;

glm::vec3 world_position = get_world_position();

Expand All @@ -67,18 +67,16 @@ void ParticleSystem::update(float dt, const glm::vec3& camera_position, const gl
glm::vec4 color = glm::vec4(m_config.color.min_value, 0.0f);

auto rotation = get_world_rotation_quat();
//auto rotation = parent->get_rotation_quat();

// this needs to be actually relative to the speed that the emitter is moving
float speed = m_config.speed.value();
float speed = m_config.speed.value();

glm::vec3 direction = vector_in_hemisphere(m_config.emitter_cone);

auto velocity = rotation * (direction * speed);

particle->distance_from_camera = 1.0f;
particle->position = world_position + vector_in_sphere() * m_config.emitter_radius;
particle->velocity = emitter_velocity + velocity ; // TODO: fix this
particle->velocity = emitter_velocity + velocity; // TODO: fix this
particle->color = color;
particle->size = m_config.size.value();
particle->lifetime = m_config.lifetime.value();
Expand All @@ -95,8 +93,8 @@ void ParticleSystem::update(float dt, const glm::vec3& camera_position, const gl
if (0 < particle->lifetime) {
float t = particle->lifetime / m_config.lifetime.max_value;
particle->position += particle->velocity * dt;
particle->size *= 0.99f;
particle->color = glm::vec4(m_config.color.value(t), t);
particle->size *= 0.95f;
particle->color = glm::vec4(m_config.color.value(t), glm::mix(m_config.start_alpha, 0.0f, t));
particle->distance_from_camera = glm::length(particle->position - camera_position);
m_particle_count++;
} else {
Expand Down Expand Up @@ -134,7 +132,6 @@ void ParticleSystem::draw_self(RenderContext& context)
glm::vec3 up = {view[0][1], view[1][1], view[2][1]};
glm::vec3 right = {view[0][0], view[1][0], view[2][0]};


// get currently set blend func
GLint blendSrc, blendDst;
glGetIntegerv(GL_BLEND_SRC, &blendSrc);
Expand All @@ -149,8 +146,7 @@ void ParticleSystem::draw_self(RenderContext& context)
shader->set_uniform("u_Right", right);
shader->set_uniform("u_Up", up);

if (m_texture != nullptr)
{
if (m_texture != nullptr) {
m_texture->bind(5);
shader->set_uniform("u_Texture", 5);
}
Expand Down Expand Up @@ -186,8 +182,6 @@ void ParticleSystem::draw_self(RenderContext& context)

// reset blend func
glBlendFunc(blendSrc, blendDst);

}


} // namespace gfx
5 changes: 4 additions & 1 deletion OpenGL_Flightsim/src/gfx/particles.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,19 @@ class ParticleSystem : public Object3D
Range<float> lifetime = Range(1.0f, 2.0f);
Range<glm::vec3> color = Range(glm::vec3(1.0f), glm::vec3(0.0f));
GLenum blend_src = GL_SRC_ALPHA, blend_dest = GL_ONE;
float start_alpha = 0.5f;
};

ParticleSystem(const Config& config, const std::string& path);
void draw_self(RenderContext& context) override;
void update(float dt, const glm::vec3& camera_position, const glm::vec3& emitter_velocity = glm::vec3(0.0f));


Config m_config;

private:
int find_unused_particle();

Config m_config;

int m_last_used_particle;
int m_particle_count;
Expand Down

0 comments on commit 21438df

Please sign in to comment.