Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
gue-ni committed Oct 12, 2023
1 parent 2ff515a commit cfda14a
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 21 deletions.
15 changes: 9 additions & 6 deletions OpenGL_Flightsim/src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "terrain.h"

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

Expand Down Expand Up @@ -101,7 +101,7 @@ void App::init()
m_scene->add(m_cameras[0]);

// cockpit camera
m_cameras[1] = new gfx::Camera(glm::radians(75.0f), aspect_ratio, 2.0f, far);
m_cameras[1] = new gfx::Camera(glm::radians(70.0f), aspect_ratio, 0.1f, far);

// following camera
m_cameras[2] = new gfx::Camera(glm::radians(45.0f), aspect_ratio, near, far);
Expand Down Expand Up @@ -146,7 +146,7 @@ void App::init()
m_airplane_model->add(m_camera_attachment);

// cockpit camera
m_cameras[1]->set_position({6.5f, 0.8f, 0.0f});
m_cameras[1]->set_position({5.8f, 0.85f, 0.0f});
m_cameras[1]->set_rotation(look_forward);
m_airplane_model->add(m_cameras[1]);

Expand Down Expand Up @@ -209,7 +209,8 @@ void App::init_airplane()
.speed = gfx::Range(100.0f, 150.0f),
.size = gfx::Range(0.3f, 0.4f),
.lifetime = gfx::Range(0.02f, 0.03f),
.color = afterburner};
.color = afterburner,
.particles_per_second = 50000};

m_particles = new gfx::ParticleSystem(config, "assets/textures/particle.png");
m_particles->set_position(glm::vec3(-5.0f, 0.0f, 0.0f));
Expand All @@ -218,7 +219,7 @@ void App::init_airplane()
#endif

const float mass = 10000.0f;
const float thrust = 75000.0f;
const float thrust = 100000.0f;

const float wing_offset = -1.0f;
const float tail_offset = -6.6f;
Expand Down Expand Up @@ -510,7 +511,7 @@ void App::game_loop(float dt)
#endif

// smooth following camera
const float speed = 15.0f * dt;
const float speed = 35.0f * dt;
m_cameras[2]->set_transform(
glm::mix(m_cameras[2]->get_world_position(), m_camera_attachment->get_world_position(), speed),
glm::mix(m_cameras[2]->get_world_rotation_quat(), m_camera_attachment->get_world_rotation_quat(), speed));
Expand All @@ -522,6 +523,8 @@ void App::game_loop(float dt)
#endif

m_screen->visible = (m_selected_camera != 0);
m_airplane_model->children[3]->visible = (m_selected_camera != 1);

m_hud->batch_clear();

#if PARTICLES
Expand Down
3 changes: 1 addition & 2 deletions OpenGL_Flightsim/src/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class App

int m_selected_camera = 0;

unsigned int m_frames = 0;
float m_seconds = 0.0f;
unsigned int m_frames = 0;

uint64_t last = 0, now = 0;

Expand Down Expand Up @@ -70,7 +70,6 @@ class App
void destroy();

void draw_imgui(float dt);

void draw_hud();

void game_loop(float dt);
Expand Down
12 changes: 7 additions & 5 deletions OpenGL_Flightsim/src/gfx/particles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ namespace gfx

ParticleSystem::ParticleSystem(const Config& config, const std::string& path)
: m_config(config),
m_max_particle_count(config.count),
m_max_particle_count(static_cast<int>(config.particles_per_second * config.lifetime.max_value)),
m_particles(config.count),
m_position_buffer(config.count),
m_color_buffer(config.count),
m_last_used_particle(0),
m_particle_count(0),
m_texture(gl::Texture::load(path))

{
const std::vector<float> vertex_buffer_data = {
-0.5f, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f, -0.5f, 0.5f, 0.0f, 0.5f, 0.5f, 0.0f,
Expand Down Expand Up @@ -54,7 +53,10 @@ int ParticleSystem::find_unused_particle()

void ParticleSystem::update(float dt, const glm::vec3& camera_position, const glm::vec3& emitter_velocity)
{
int new_particles = 200;
int new_particles = static_cast<int>(dt * m_config.particles_per_second);
new_particles = glm::max(new_particles, 1);

// std::cout << "new_particles " << new_particles << std::endl;

glm::vec3 world_position = get_world_position();

Expand All @@ -70,12 +72,12 @@ void ParticleSystem::update(float dt, const glm::vec3& camera_position, const gl

float speed = m_config.speed.value();

glm::vec3 direction = vector_in_hemisphere(m_config.emitter_cone);
glm::vec3 direction = point_on_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->position = world_position + point_on_sphere() * m_config.emitter_radius;
particle->velocity = emitter_velocity + velocity; // TODO: fix this
particle->color = color;
particle->size = m_config.size.value();
Expand Down
3 changes: 1 addition & 2 deletions OpenGL_Flightsim/src/gfx/particles.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,18 @@ class ParticleSystem : public Object3D
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;
int particles_per_second = 10000;
};

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();


int m_last_used_particle;
int m_particle_count;
const int m_max_particle_count;
Expand Down
5 changes: 2 additions & 3 deletions OpenGL_Flightsim/src/gfx/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ std::string load_text_file(const std::string& path)
return buffer.str();
}


float random_01() { return static_cast<float>(rand()) / static_cast<float>(RAND_MAX); }

glm::vec3 vector_in_sphere()
glm::vec3 point_on_sphere()
{
float r1 = random_01();
float r2 = random_01();
Expand All @@ -27,7 +26,7 @@ glm::vec3 vector_in_sphere()
return glm::vec3(cos(phi) * sin(theta), cos(theta), sin(phi) * sin(theta));
}

glm::vec3 vector_in_hemisphere(float factor)
glm::vec3 point_on_hemisphere(float factor)
{
float r1 = random_01();
float r2 = random_01();
Expand Down
6 changes: 3 additions & 3 deletions OpenGL_Flightsim/src/gfx/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ constexpr glm::vec3 rgb(uint32_t hex)

float random_01();

glm::vec3 vector_in_sphere();

glm::vec3 vector_in_hemisphere(float factor = 1.0f);
glm::vec3 point_on_sphere();
glm::vec3 point_on_ellipsoid();
glm::vec3 point_on_hemisphere(float factor = 1.0f);

class Image
{
Expand Down

0 comments on commit cfda14a

Please sign in to comment.