fix memory issues
This commit is contained in:
parent
b0ebc29a29
commit
a7ced6a773
|
@ -4,7 +4,7 @@
|
|||
|
||||
#include "camera.hpp"
|
||||
#include "input/keyboard.hpp"
|
||||
#include "../math.hpp"
|
||||
#include "../util/math.hpp"
|
||||
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
|
@ -75,7 +75,11 @@ void camera::update(const system& sys, double dt)
|
|||
velocity.z += 3.5;
|
||||
}
|
||||
|
||||
glm::vec<3, double> velocity2 = sys.scene.check_intersect(pos, velocity * dt) / dt;
|
||||
glm::vec<3, double> normal_last(0);
|
||||
glm::vec<3, double> velocity2;
|
||||
|
||||
velocity2 = sys.scene.calc_intersect(pos, velocity * dt, normal_last);
|
||||
velocity2 = sys.scene.calc_intersect(pos + glm::vec<3, double>(0, 0, -1.5), velocity2, normal_last) / dt;
|
||||
|
||||
pos += velocity2 * dt;
|
||||
on_ground = ((velocity * dt / dt).z != velocity2.z);
|
||||
|
|
|
@ -89,6 +89,8 @@ void font::init()
|
|||
|
||||
chars[i] = c;
|
||||
}
|
||||
|
||||
FT_Done_FreeType(ft);
|
||||
}
|
||||
|
||||
void mesh::load_text(const char* text, double size)
|
||||
|
|
|
@ -73,6 +73,11 @@ void glmesh::set(const mesh& m, int mode)
|
|||
|
||||
void glmesh::render()
|
||||
{
|
||||
glDrawElements(GL_TRIANGLES, size, GL_UNSIGNED_INT, 0);
|
||||
render(GL_TRIANGLES);
|
||||
}
|
||||
|
||||
void glmesh::render(int type)
|
||||
{
|
||||
glDrawElements(type, size, GL_UNSIGNED_INT, 0);
|
||||
}
|
||||
|
||||
|
|
|
@ -27,6 +27,7 @@ struct glmesh
|
|||
void bind();
|
||||
void uniform();
|
||||
void set(const mesh& m, int mode);
|
||||
void render(int type);
|
||||
void render();
|
||||
};
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
#include "arrays.hpp"
|
||||
#include "../shader.hpp"
|
||||
#include "../camera.hpp"
|
||||
#include "../../math.hpp"
|
||||
#include "../../util/math.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
|
@ -95,19 +95,18 @@ bool ray_intersects_triangle(vec3 ray_origin,
|
|||
return false;
|
||||
}
|
||||
|
||||
vec3 mesh::check_intersect(vec3 pos, vec3 path) const
|
||||
bool mesh::check_intersect(vec3 pos, vec3 path) const
|
||||
{
|
||||
double l = glm::length(path);
|
||||
|
||||
if(l == 0)
|
||||
{
|
||||
return path;
|
||||
return false;
|
||||
}
|
||||
|
||||
vec3 path_n = path / l;
|
||||
bool intersects = false;
|
||||
|
||||
for(unsigned int i = 0; i < indices.size(); i += 3)
|
||||
|
||||
for(unsigned int i = 0; i < indices.size(); i++)
|
||||
{
|
||||
vec3 v[3] = {
|
||||
vec3(this->vertices[indices[i]].pos),
|
||||
|
@ -117,7 +116,7 @@ vec3 mesh::check_intersect(vec3 pos, vec3 path) const
|
|||
|
||||
vec3 ipoint;
|
||||
vec3 normal = glm::normalize(glm::cross(v[1] - v[0], v[2] - v[0]));
|
||||
double d = glm::dot(normal, path);
|
||||
double d = glm::dot(normal, path_n);
|
||||
|
||||
if(d >= 0)
|
||||
continue;
|
||||
|
@ -126,12 +125,107 @@ vec3 mesh::check_intersect(vec3 pos, vec3 path) const
|
|||
if(l < glm::length(ipoint - pos))
|
||||
continue;
|
||||
|
||||
intersects = true;
|
||||
path -= normal * d;
|
||||
l = glm::length(path);
|
||||
path_n = path / l;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
vec3 mesh::calc_intersect(vec3 pos, vec3 path) const
|
||||
{
|
||||
vec3 normal_last(0);
|
||||
return calc_intersect(pos, path, normal_last);
|
||||
}
|
||||
|
||||
static bool calc_intercept_vert(vec3 v[3], vec3 pos, vec3& path, vec3& path_n, vec3& normal_last, double& l)
|
||||
{
|
||||
vec3 ipoint;
|
||||
vec3 normal = glm::normalize(glm::cross(v[1] - v[0], v[2] - v[0]));
|
||||
double d = glm::dot(normal, path);
|
||||
|
||||
if(d >= 0)
|
||||
return false;
|
||||
if(!ray_intersects_triangle(pos, path_n, v, ipoint))
|
||||
return false;
|
||||
if(l < glm::length(ipoint - pos))
|
||||
return false;
|
||||
|
||||
if(normal_last != vec3(0))
|
||||
{
|
||||
vec3 n = glm::cross(normal_last, normal);
|
||||
|
||||
if(glm::length(n) > 0)
|
||||
{
|
||||
normal = glm::normalize(glm::cross(glm::cross(normal_last, normal), normal_last));
|
||||
d = glm::dot(normal, path);
|
||||
}
|
||||
}
|
||||
|
||||
path -= normal * d;
|
||||
normal_last = normal;
|
||||
l = glm::length(path);
|
||||
path_n = path / l;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
vec3 mesh::calc_intersect(vec3 pos, vec3 path, vec3& normal_last) const
|
||||
{
|
||||
double l = glm::length(path);
|
||||
|
||||
if(l == 0)
|
||||
{
|
||||
return path;
|
||||
}
|
||||
|
||||
vec3 path_n = path / l;
|
||||
unsigned int i_found = 0;
|
||||
|
||||
for(unsigned int i = 0; i < indices.size(); i += 3)
|
||||
{
|
||||
vec3 v[3] = {
|
||||
vec3(this->vertices[indices[i]].pos),
|
||||
vec3(this->vertices[indices[i + 1]].pos),
|
||||
vec3(this->vertices[indices[i + 2]].pos)
|
||||
};
|
||||
|
||||
if(calc_intercept_vert(v, pos, path, path_n, normal_last, l))
|
||||
{
|
||||
i_found = i;
|
||||
}
|
||||
}
|
||||
/*
|
||||
for(unsigned int i = i_found - 1; i >= 0; i--)
|
||||
{
|
||||
vec3 v[3] = {
|
||||
vec3(this->vertices[indices[i]].pos),
|
||||
vec3(this->vertices[indices[i + 1]].pos),
|
||||
vec3(this->vertices[indices[i + 2]].pos)
|
||||
};
|
||||
|
||||
calc_intercept_vert(v, pos, path, path_n, normal_last, l);
|
||||
}
|
||||
*/
|
||||
return path;
|
||||
}
|
||||
|
||||
mesh mesh::to_lines() const
|
||||
{
|
||||
mesh m;
|
||||
m.vertices = vertices;
|
||||
|
||||
for(int i = 0; i < indices.size(); i += 3)
|
||||
{
|
||||
m.indices.push_back(indices[i]);
|
||||
m.indices.push_back(indices[i + 1]);
|
||||
m.indices.push_back(indices[i + 1]);
|
||||
m.indices.push_back(indices[i + 2]);
|
||||
m.indices.push_back(indices[i]);
|
||||
m.indices.push_back(indices[i + 2]);
|
||||
}
|
||||
|
||||
std::cout << "indices.size() = " << m.indices.size() << ", vertices.size() = " << m.vertices.size() << "\n";
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
|
|
|
@ -27,7 +27,10 @@ struct mesh
|
|||
void load_text(const char* text, double size);
|
||||
void add(const mesh& o, glm::mat4 mat);
|
||||
|
||||
glm::vec<3, double> check_intersect(glm::vec<3, double> pos, glm::vec<3, double> path) const;
|
||||
mesh to_lines() const;
|
||||
bool check_intersect(glm::vec<3, double> pos, glm::vec<3, double> path) const;
|
||||
glm::vec<3, double> calc_intersect(glm::vec<3, double> pos, glm::vec<3, double> path) const;
|
||||
glm::vec<3, double> calc_intersect(glm::vec<3, double> pos, glm::vec<3, double> path, glm::vec<3, double>& normal_last) const;
|
||||
|
||||
template <class T>
|
||||
void load_text(const char* header, T& item, double size)
|
||||
|
|
|
@ -28,7 +28,7 @@ using namespace sim::graphics;
|
|||
static GLFWwindow* win;
|
||||
static bool win_should_close = false;
|
||||
|
||||
static glmesh MeshScene;
|
||||
static glmesh MeshScene, MeshCollisionScene;
|
||||
static monitor::vessel MonitorVessel;
|
||||
static monitor::core MonitorCore;
|
||||
|
||||
|
@ -40,7 +40,7 @@ void GLAPIENTRY cb_debug_message(GLenum source, GLenum type, GLuint id, GLenum s
|
|||
}
|
||||
}
|
||||
|
||||
void window::create()
|
||||
void window::create(system& sys)
|
||||
{
|
||||
glfwInit();
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
||||
|
@ -96,11 +96,14 @@ void window::create()
|
|||
font::init();
|
||||
|
||||
shader::init_program();
|
||||
mesh rmesh;
|
||||
|
||||
rmesh.load_model("../assets", "scene-baked.glb");
|
||||
sys.scene.load_model("../assets", "scene-baked.glb");
|
||||
MeshScene.bind();
|
||||
MeshScene.set(rmesh, GL_STATIC_DRAW);
|
||||
MeshScene.set(sys.scene, GL_STATIC_DRAW);
|
||||
|
||||
sys.scene.load_model("../assets/model", "scene_collisions.stl");
|
||||
MeshCollisionScene.bind();
|
||||
MeshCollisionScene.set(sys.scene.to_lines(), GL_STATIC_DRAW);
|
||||
|
||||
MonitorCore.init();
|
||||
MonitorVessel.init();
|
||||
|
@ -123,6 +126,10 @@ void window::loop(sim::system& sys)
|
|||
MeshScene.uniform();
|
||||
MeshScene.render();
|
||||
|
||||
MeshCollisionScene.bind();
|
||||
MeshCollisionScene.uniform();
|
||||
MeshCollisionScene.render(GL_LINES);
|
||||
|
||||
MonitorCore.render(sys);
|
||||
MonitorVessel.render();
|
||||
|
||||
|
|
|
@ -8,8 +8,8 @@
|
|||
namespace sim::graphics::window
|
||||
{
|
||||
|
||||
void create();
|
||||
bool should_close();
|
||||
void create(sim::system& sys);
|
||||
void loop(sim::system& sys);
|
||||
void destroy();
|
||||
void close();
|
||||
|
|
|
@ -28,8 +28,8 @@ unsigned long get_now()
|
|||
|
||||
int main()
|
||||
{
|
||||
graphics::window::create();
|
||||
sim::system sys;
|
||||
graphics::window::create(sys);
|
||||
|
||||
long clock = get_now();
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
#include <cstdlib>
|
||||
|
||||
using namespace sim::reactor;
|
||||
|
||||
|
@ -14,31 +15,31 @@ sim::reactor::reactor sim::reactor::builder(const int W, const int H, const doub
|
|||
for(int x = 0; x < W; x++)
|
||||
{
|
||||
char c = lines[y][x];
|
||||
rod* r;
|
||||
std::unique_ptr<rod> r;
|
||||
|
||||
switch(c)
|
||||
{
|
||||
case 'F':
|
||||
r = new fuel::fuel_rod(fr);
|
||||
r = fr.clone();
|
||||
break;
|
||||
case 'C':
|
||||
r = new control::boron_rod(br);
|
||||
r = br.clone();
|
||||
break;
|
||||
case 'G':
|
||||
r = new control::graphite_rod();
|
||||
r = std::make_unique<control::graphite_rod>();
|
||||
break;
|
||||
case 'H':
|
||||
r = new coolant::heater();
|
||||
r = std::make_unique<coolant::heater>();
|
||||
break;
|
||||
case 'P':
|
||||
r = new coolant::pipe(v);
|
||||
r = std::make_unique<coolant::pipe>(v);
|
||||
break;
|
||||
case ' ':
|
||||
r = new rod();
|
||||
r = std::make_unique<rod>();
|
||||
break;
|
||||
}
|
||||
|
||||
arr[y * W + x] = std::unique_ptr<rod>(std::move(r));
|
||||
arr[y * W + x] = std::move(r);
|
||||
}
|
||||
|
||||
return reactor(&arr[0], W, H, CW, CH);
|
||||
|
|
|
@ -15,7 +15,6 @@ class boron_rod : public sim::reactor::coolant::pipe
|
|||
|
||||
virtual void display(std::ostream& o) const;
|
||||
virtual const char* get_name() const { return "Boron Control Rod"; }
|
||||
virtual rod* clone() const { return new boron_rod(*this); }
|
||||
virtual glm::vec4 get_colour() const;
|
||||
virtual int get_id() const { return 5; }
|
||||
|
||||
|
@ -30,6 +29,7 @@ public:
|
|||
virtual bool should_display() const { return true; }
|
||||
virtual bool should_select() const { return true; }
|
||||
virtual void update_selected(double a);
|
||||
virtual std::unique_ptr<rod> clone() const { return std::make_unique<boron_rod>(*this); }
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -12,7 +12,6 @@ class graphite_rod : public sim::reactor::rod
|
|||
|
||||
virtual void display(std::ostream& o) const;
|
||||
virtual const char* get_name() const { return "Graphite Rod"; }
|
||||
virtual rod* clone() const { return new graphite_rod(*this); };
|
||||
virtual double get_k(val_t type) const;
|
||||
virtual glm::vec4 get_colour() const;
|
||||
virtual int get_id() const { return 4; }
|
||||
|
@ -27,6 +26,7 @@ public:
|
|||
virtual bool should_display() const { return true; }
|
||||
virtual bool should_select() const { return true; }
|
||||
virtual void update_selected(double a);
|
||||
virtual std::unique_ptr<rod> clone() const { return std::make_unique<graphite_rod>(*this); }
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -15,7 +15,6 @@ class heater : public sim::reactor::rod
|
|||
virtual bool has_sensors(val_t t) const { return true; }
|
||||
virtual const char* get_name() const { return "Heater"; }
|
||||
virtual double get_k(val_t type) const { return 0.5; }
|
||||
virtual rod* clone() const { return new heater(*this); };
|
||||
virtual int get_id() const { return 3; }
|
||||
|
||||
public:
|
||||
|
@ -24,6 +23,7 @@ public:
|
|||
virtual bool should_display() const { return true; }
|
||||
virtual bool should_select() const { return true; }
|
||||
virtual void update_selected(double a);
|
||||
virtual std::unique_ptr<rod> clone() const { return std::make_unique<heater>(*this); }
|
||||
};
|
||||
|
||||
};
|
||||
|
|
|
@ -16,7 +16,6 @@ protected:
|
|||
|
||||
virtual double get_k(sim::reactor::rod::val_t type) const;
|
||||
virtual const char* get_name() const { return "Coolant"; }
|
||||
virtual rod* clone() const { return new pipe(*this); };
|
||||
virtual int get_id() const { return 2; }
|
||||
|
||||
void update_pipe(double secs);
|
||||
|
@ -25,6 +24,7 @@ public:
|
|||
|
||||
pipe(coolant::vessel& v);
|
||||
|
||||
virtual std::unique_ptr<rod> clone() const { return std::make_unique<pipe>(*this); }
|
||||
virtual bool should_display() const { return true; }
|
||||
virtual void update(double secs);
|
||||
|
||||
|
|
|
@ -16,7 +16,6 @@ class fuel_rod : public sim::reactor::rod
|
|||
|
||||
virtual bool has_sensors(val_t t) const { return true; }
|
||||
virtual const char* get_name() const { return "Fuel"; }
|
||||
virtual rod* clone() const { return new fuel_rod(*this); };
|
||||
virtual int get_id() const { return 1; }
|
||||
virtual glm::vec4 get_colour() const;
|
||||
|
||||
|
@ -24,6 +23,7 @@ public:
|
|||
|
||||
fuel_rod(double fuel);
|
||||
|
||||
virtual std::unique_ptr<rod> clone() const { return std::make_unique<fuel_rod>(*this); }
|
||||
virtual bool should_display() const { return true; }
|
||||
virtual void update(double secs);
|
||||
};
|
||||
|
|
|
@ -8,7 +8,7 @@ using namespace sim::reactor;
|
|||
|
||||
reactor::reactor(std::unique_ptr<rod>* rods, int w, int h, double cw, double ch) : cell_width(cw), cell_height(ch), width(w), height(h), size(w * h)
|
||||
{
|
||||
this->rods = new std::unique_ptr<rod>[width * height];
|
||||
this->rods = std::make_unique<std::unique_ptr<rod>[]>(width * height);
|
||||
|
||||
for(int i = 0; i < size; i++)
|
||||
{
|
||||
|
@ -19,14 +19,13 @@ reactor::reactor(std::unique_ptr<rod>* rods, int w, int h, double cw, double ch)
|
|||
|
||||
reactor::reactor(reactor&& o) : cell_width(o.cell_width), cell_height(o.cell_height), width(o.width), height(o.height), size(o.size)
|
||||
{
|
||||
rods = o.rods;
|
||||
rods = std::move(o.rods);
|
||||
cursor = o.cursor;
|
||||
o.rods = nullptr;
|
||||
}
|
||||
|
||||
reactor::reactor(const reactor& o) : cell_width(o.cell_width), cell_height(o.cell_height), width(o.width), height(o.height), size(o.size)
|
||||
{
|
||||
rods = new std::unique_ptr<rod>[width * height];
|
||||
this->rods = std::make_unique<std::unique_ptr<rod>[]>(width * height);
|
||||
|
||||
for(int i = 0; i < size; i++)
|
||||
{
|
||||
|
@ -34,14 +33,6 @@ reactor::reactor(const reactor& o) : cell_width(o.cell_width), cell_height(o.cel
|
|||
}
|
||||
}
|
||||
|
||||
reactor::~reactor()
|
||||
{
|
||||
if(rods != nullptr)
|
||||
{
|
||||
delete[] rods;
|
||||
}
|
||||
}
|
||||
|
||||
void reactor::update(double secs)
|
||||
{
|
||||
int rods_lookup[size];
|
||||
|
|
|
@ -18,13 +18,12 @@ struct reactor
|
|||
const int height;
|
||||
const int size;
|
||||
|
||||
std::unique_ptr<rod>* rods;
|
||||
std::unique_ptr<std::unique_ptr<rod>[]> rods;
|
||||
int cursor = 0;
|
||||
|
||||
reactor(std::unique_ptr<rod>* rods, int width, int height, double cell_width, double cell_height);
|
||||
reactor(const reactor& r);
|
||||
reactor(reactor&& r);
|
||||
~reactor();
|
||||
|
||||
void update(double secs);
|
||||
void update_selected(int v);
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
#include <ostream>
|
||||
#include <glm/vec4.hpp>
|
||||
#include <memory>
|
||||
|
||||
namespace sim::reactor
|
||||
{
|
||||
|
@ -21,12 +22,13 @@ public:
|
|||
N_FAST = 2
|
||||
};
|
||||
|
||||
virtual ~rod() {};
|
||||
virtual void interact(rod* o, double secs);
|
||||
virtual void update(double secs) { }
|
||||
virtual void add(val_t type, double v);
|
||||
virtual double extract(val_t type, double s, double k, double o);
|
||||
virtual double get(val_t type) const;
|
||||
virtual rod* clone() const { return new rod(*this); }
|
||||
virtual std::unique_ptr<rod> clone() const { return std::make_unique<rod>(*this); }
|
||||
virtual glm::vec4 get_colour() const { return {0, 0, 0, 0}; }
|
||||
virtual int get_id() const { return 0; }
|
||||
|
||||
|
|
|
@ -33,37 +33,22 @@ system::system()
|
|||
" C C C C "
|
||||
};
|
||||
|
||||
vessel = new reactor::coolant::vessel(8, 10, 300, sim::coolant::WATER);
|
||||
reactor = new sim::reactor::reactor(sim::reactor::builder(19, 19, 0.25, 8,
|
||||
vessel = std::make_unique<reactor::coolant::vessel>(8, 10, 300, sim::coolant::WATER);
|
||||
reactor = std::make_unique<sim::reactor::reactor>(sim::reactor::builder(19, 19, 0.25, 8,
|
||||
reactor::fuel::fuel_rod(0.5),
|
||||
reactor::control::boron_rod(*vessel, 1),
|
||||
*vessel, layout));
|
||||
reactor::control::boron_rod(*vessel.get(), 1),
|
||||
*vessel.get(), layout));
|
||||
|
||||
valve = new coolant::valve<reactor::coolant::vessel>(*vessel, 1, 500);
|
||||
pump = new coolant::pump<reactor::coolant::vessel>(*vessel, 1e4, 15);
|
||||
|
||||
scene.load_model("../assets/model", "scene_collisions.stl");
|
||||
valve = std::make_unique<coolant::valve<reactor::coolant::vessel>>(*vessel.get(), 1, 500);
|
||||
pump = std::make_unique<coolant::pump<reactor::coolant::vessel>>(*vessel.get(), 1e4, 15);
|
||||
}
|
||||
|
||||
system::system(system&& o)
|
||||
{
|
||||
vessel = o.vessel;
|
||||
reactor = o.reactor;
|
||||
valve = o.valve;
|
||||
pump = o.pump;
|
||||
|
||||
o.vessel = nullptr;
|
||||
o.reactor = nullptr;
|
||||
o.valve = nullptr;
|
||||
o.pump = nullptr;
|
||||
}
|
||||
|
||||
system::~system()
|
||||
{
|
||||
if(vessel != nullptr) delete vessel;
|
||||
if(reactor != nullptr) delete reactor;
|
||||
if(valve != nullptr) delete valve;
|
||||
if(pump != nullptr) delete pump;
|
||||
vessel = std::move(o.vessel);
|
||||
reactor = std::move(o.reactor);
|
||||
valve = std::move(o.valve);
|
||||
pump = std::move(o.pump);
|
||||
}
|
||||
|
||||
void system::update(double dt)
|
||||
|
|
|
@ -14,16 +14,15 @@ namespace sim
|
|||
|
||||
struct system
|
||||
{
|
||||
sim::reactor::reactor* reactor;
|
||||
sim::reactor::coolant::vessel* vessel;
|
||||
sim::coolant::valve<sim::reactor::coolant::vessel>* valve;
|
||||
sim::coolant::pump<sim::reactor::coolant::vessel>* pump;
|
||||
std::unique_ptr<sim::reactor::reactor> reactor;
|
||||
std::unique_ptr<sim::reactor::coolant::vessel> vessel;
|
||||
std::unique_ptr<sim::coolant::valve<sim::reactor::coolant::vessel>> valve;
|
||||
std::unique_ptr<sim::coolant::pump<sim::reactor::coolant::vessel>> pump;
|
||||
sim::graphics::mesh scene;
|
||||
|
||||
system();
|
||||
system(system&& o);
|
||||
system(const system& o) = delete;
|
||||
~system();
|
||||
|
||||
void update(double dt);
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue