our_dick/include/engine/object.hpp

143 lines
5.4 KiB
C++

/**
This file is a part of our_dick
Copyright (C) 2020 rexy712
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OUR_DICK_ENGINE_OBJECT_HPP
#define OUR_DICK_ENGINE_OBJECT_HPP
#include "graphics/gl_include.hpp" //GLfloat
#include "math/math.hpp"
#include "base_types.hpp"
namespace egn{
class object_base
{
protected:
enum update{
NO_UPDATE,
SCALE_UPDATE = 1,
TRANSLATION_UPDATE = 2,
ROTATION_UPDATE = 4,
BOUNDING_VOLUME_UPDATE = 8,
};
protected:
math::vec3<GLfloat> m_position; //track current positon in world space
math::quaternion<GLfloat> m_orientation; //track current model space rotation
math::vec3<GLfloat> m_scale{1.0f, 1.0f, 1.0f}; //track model space scale
private:
mutable math::mat4<GLfloat> m_model_matrix; //compile all the above info into a matrix
protected:
mutable int m_update_flag = NO_UPDATE; //whether or not to update the matrix upon access
//make the update flag protected so subclasses can share it
public:
object_base() = default;
explicit object_base(const math::vec3<GLfloat>& position);
object_base(const math::vec3<GLfloat>& position, const math::quaternion<GLfloat>& orientation);
object_base(const object_base&) = default;
object_base(object_base&&) = default;
virtual ~object_base() = default;
object_base& operator=(const object_base&) = default;
object_base& operator=(object_base&&) = default;
void translate(const math::vec3<GLfloat>& distance);
void rotate(const math::quaternion<GLfloat>& distance);
void scale(const math::vec3<GLfloat>& distance);
void look_at(const math::vec3<GLfloat>& targ, const math::vec3<GLfloat>& up);
virtual void set_position(const math::vec3<GLfloat>& pos);
virtual void set_orientation(const math::quaternion<GLfloat>& orient);
virtual void set_scale(const math::vec3<GLfloat>& scale);
const math::mat4<GLfloat>& model_matrix()const;
const math::vec3<GLfloat>& position()const;
const math::vec3<GLfloat>& scale()const;
const math::quaternion<GLfloat>& orientation()const;
protected:
void recalc_model_matrix()const;
};
class aabb_object : virtual public object_base
{
protected:
aabb m_model_bounding_box; //model coordinate bounding volume
private:
mutable aabb m_world_bounding_box; //world coordinate bounding volume
public:
using object_base::object_base;
using object_base::operator=;
aabb_object(const math::vec3<GLfloat>& position,
const math::vec3<GLfloat>& aabb_p1, const math::vec3<GLfloat>& aabb_p2);
aabb_object(const math::vec3<GLfloat>& position, const math::quaternion<GLfloat>& orientation,
const math::vec3<GLfloat>& aabb_p1, const math::vec3<GLfloat>& aabb_p2);
aabb_object() = default;
aabb_object(const aabb_object&) = default;
aabb_object(aabb_object&&) = default;
~aabb_object() = default;
aabb_object& operator=(const aabb_object&) = default;
aabb_object& operator=(aabb_object&&) = default;
const aabb& get_model_bounding_box()const;
const aabb& get_bounding_box()const;
};
class aabs_object : virtual public object_base
{
protected:
aabs m_model_bounding_sphere; //model coordinate bounding volume
private:
mutable aabs m_world_bounding_sphere; //world coordinate bounding volume
public:
using object_base::object_base;
using object_base::operator=;
aabs_object(const math::vec3<GLfloat>& position, float aabs_r);
aabs_object(const math::vec3<GLfloat>& position, const math::quaternion<GLfloat>& orientation,
float aabs_r);
aabs_object() = default;
aabs_object(const aabs_object&) = default;
aabs_object(aabs_object&&) = default;
~aabs_object() = default;
aabs_object& operator=(const aabs_object&) = default;
aabs_object& operator=(aabs_object&&) = default;
const aabs& get_model_bounding_sphere()const;
const aabs& get_bounding_sphere()const;
};
bool check_collision(const aabb_object& l, const aabb_object& r);
bool check_collision(const aabb_object& l, const aabs_object& r);
bool check_collision(const aabs_object& l, const aabb_object& r);
bool check_collision(const aabs_object& l, const aabs_object& r);
bool check_collision(const aabb_object& o, const math::vec3<float>& point);
bool check_collision(const aabs_object& o, const math::vec3<float>& point);
bool check_collision(const aabb_object& o, const line& l);
bool check_collision(const aabs_object& o, const line& l);
bool check_collision(const aabb& l, const aabb& r);
bool check_collision(const aabb& l, const aabs& r);
bool check_collision(const aabs& l, const aabb& r);
bool check_collision(const aabs& l, const aabs& r);
bool check_collision(const aabb& l, const math::vec3<float>& point);
bool check_collision(const aabs& l, const math::vec3<float>& point);
bool check_collision(const aabb& o, const line& l);
bool check_collision(const aabs& o, const line& l);
}
#endif