added comments
This commit is contained in:
parent
5875f1246f
commit
4c1c151422
|
@ -6,8 +6,13 @@
|
||||||
|
|
||||||
namespace lol
|
namespace lol
|
||||||
{
|
{
|
||||||
|
|
||||||
// TODO: Find better name
|
// TODO: Find better name
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A base camera object. It hides away part of the interface of a Transformable
|
||||||
|
* (because e.g. scaling doesn't make sense for a camera), and adds some other
|
||||||
|
* transformation functions that are useful for cameras.
|
||||||
|
*/
|
||||||
class CameraBase : public Transformable
|
class CameraBase : public Transformable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -16,21 +21,39 @@ namespace lol
|
||||||
void SetScale(const glm::vec3&) = delete;
|
void SetScale(const glm::vec3&) = delete;
|
||||||
void Scale(const glm::vec3&) = delete;
|
void Scale(const glm::vec3&) = delete;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Makes the camera look at the target position. Only affects rotation.
|
||||||
|
*/
|
||||||
inline void LookAt(const glm::vec3& target)
|
inline void LookAt(const glm::vec3& target)
|
||||||
{
|
{
|
||||||
transformation = glm::lookAt(position, target, glm::vec3(0.0f, 1.0f, 0.0f));
|
transformation = glm::lookAt(position, target, glm::vec3(0.0f, 1.0f, 0.0f));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Get the cameras view matrix
|
||||||
|
*
|
||||||
|
* @returns The cameras view matrix
|
||||||
|
*/
|
||||||
inline const glm::mat4& GetView() const
|
inline const glm::mat4& GetView() const
|
||||||
{
|
{
|
||||||
return transformation;
|
return transformation;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*@brief Get the cameras projection matrix
|
||||||
|
*
|
||||||
|
* @returns The cameras projection matrix
|
||||||
|
*/
|
||||||
inline const glm::mat4& GetProjection() const
|
inline const glm::mat4& GetProjection() const
|
||||||
{
|
{
|
||||||
return projection;
|
return projection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Alternative rendering syntax
|
||||||
|
*
|
||||||
|
* @param drawable A Drawable that should be rendered through this camera
|
||||||
|
*/
|
||||||
inline void Draw(const Drawable& drawable) const
|
inline void Draw(const Drawable& drawable) const
|
||||||
{
|
{
|
||||||
drawable.Draw(*this);
|
drawable.Draw(*this);
|
||||||
|
@ -41,28 +64,57 @@ namespace lol
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A camera using perspective projection
|
||||||
|
*/
|
||||||
class Camera : public CameraBase
|
class Camera : public CameraBase
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
/**
|
||||||
|
* @brief Constructs a new camera
|
||||||
|
*
|
||||||
|
* @param fov FOV of the camera
|
||||||
|
* @param aspect Aspect ratio of the screen
|
||||||
|
* @param zNear Near clipping plane
|
||||||
|
* @param zFar Far clipping plane
|
||||||
|
*/
|
||||||
Camera(float fov = 90.0f, float aspect = 1.0f, float zNear = 0.01f, float zFar = 100.0f)
|
Camera(float fov = 90.0f, float aspect = 1.0f, float zNear = 0.01f, float zFar = 100.0f)
|
||||||
{
|
{
|
||||||
projection = glm::perspective(glm::radians(fov), aspect, zNear, zFar);
|
projection = glm::perspective(glm::radians(fov), aspect, zNear, zFar);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Update the camera configuration without affecting its transformation
|
||||||
|
*
|
||||||
|
* @param fov FOV of the camera
|
||||||
|
* @param aspect Aspect ratio of the screen
|
||||||
|
* @param zNear Near clipping plane
|
||||||
|
* @param zFar Far clipping plane
|
||||||
|
*/
|
||||||
inline void Update(float fov, float aspect, float zNear, float zFar)
|
inline void Update(float fov, float aspect, float zNear, float zFar)
|
||||||
{
|
{
|
||||||
projection = glm::perspective(glm::radians(fov), aspect, zNear, zFar);
|
projection = glm::perspective(glm::radians(fov), aspect, zNear, zFar);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A camera using orthographic projection
|
||||||
|
*/
|
||||||
class OrthogonalCamera : public CameraBase
|
class OrthogonalCamera : public CameraBase
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
/**
|
||||||
|
* @brief Constructs a new camera
|
||||||
|
*/
|
||||||
OrthogonalCamera(float left = -1.0f, float right = 1.0f, float bottom = -1.0f, float top = 1.0f, float zNear = -100.0f, float zFar = 100.0f)
|
OrthogonalCamera(float left = -1.0f, float right = 1.0f, float bottom = -1.0f, float top = 1.0f, float zNear = -100.0f, float zFar = 100.0f)
|
||||||
{
|
{
|
||||||
projection = glm::ortho(left, right, bottom, top, zNear, zFar);
|
projection = glm::ortho(left, right, bottom, top, zNear, zFar);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Update the camera configuration without affecting its transformation
|
||||||
|
*/
|
||||||
inline void Update(float left, float right, float bottom, float top, float zNear, float zFar)
|
inline void Update(float left, float right, float bottom, float top, float zNear, float zFar)
|
||||||
{
|
{
|
||||||
projection = glm::ortho(left, right, bottom, top, zNear, zFar);
|
projection = glm::ortho(left, right, bottom, top, zNear, zFar);
|
||||||
|
|
|
@ -21,11 +21,29 @@ namespace lol
|
||||||
TriangleFan = GL_TRIANGLE_FAN
|
TriangleFan = GL_TRIANGLE_FAN
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A class that can be displayed on a screen.
|
||||||
|
*/
|
||||||
class Drawable
|
class Drawable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
/**
|
||||||
|
* @brief Called immediately after the shader is bound, and before the VAO is drawn.
|
||||||
|
*
|
||||||
|
* @param camera The camera with which this object is rendered.
|
||||||
|
*/
|
||||||
virtual void PreRender(const CameraBase& camera) const { };
|
virtual void PreRender(const CameraBase& camera) const { };
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Bind the shader and draw the VAO
|
||||||
|
*
|
||||||
|
* @param camera The camera with which this object is rendered.
|
||||||
|
*/
|
||||||
void Draw(const CameraBase& camera) const;
|
void Draw(const CameraBase& camera) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief The VAO can be rendered as a mesh, a set of lines, loops, strips etc
|
||||||
|
*/
|
||||||
void SetPrimitiveType(PrimitiveType type);
|
void SetPrimitiveType(PrimitiveType type);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
|
@ -14,21 +14,60 @@ namespace lol
|
||||||
class UniqueShader;
|
class UniqueShader;
|
||||||
typedef std::shared_ptr<UniqueShader> Shader;
|
typedef std::shared_ptr<UniqueShader> Shader;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compiles shaders into a program and manages access to that program
|
||||||
|
*/
|
||||||
class UniqueShader : public NonCopyable
|
class UniqueShader : public NonCopyable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
/**
|
||||||
|
* @brief Create a new shader program from source
|
||||||
|
*
|
||||||
|
* @param vertexShader Source code of the vertex shader
|
||||||
|
* @param fragmentShader Source code of the fragment shader
|
||||||
|
*/
|
||||||
UniqueShader(const std::string& vertexShader, const std::string& fragmentShader);
|
UniqueShader(const std::string& vertexShader, const std::string& fragmentShader);
|
||||||
~UniqueShader();
|
~UniqueShader();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Creates a shareable UniqueShader. Note that they're SHAREable, not COPYable
|
||||||
|
*
|
||||||
|
* @param vertexShader Source code of the vertex shader
|
||||||
|
* @param fragmentShader Source code of the fragment shader
|
||||||
|
*
|
||||||
|
* @returns Shared pointer to a UniqueShader
|
||||||
|
*/
|
||||||
inline static Shader Share(const std::string& vertexShader, const std::string& fragmentShader)
|
inline static Shader Share(const std::string& vertexShader, const std::string& fragmentShader)
|
||||||
{
|
{
|
||||||
return std::make_shared<UniqueShader>(vertexShader, fragmentShader);
|
return std::make_shared<UniqueShader>(vertexShader, fragmentShader);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Status of the program creation
|
||||||
|
*
|
||||||
|
* @returns `true` if shader was successfully created
|
||||||
|
*/
|
||||||
inline bool Good() { return id != 0; }
|
inline bool Good() { return id != 0; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Bind this shader program
|
||||||
|
*/
|
||||||
void Use();
|
void Use();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set a 4x4 matrix uniform
|
||||||
|
*
|
||||||
|
* @param name Name of the uniform
|
||||||
|
* @param value Value of the uniform
|
||||||
|
*/
|
||||||
void SetUniform(const std::string& name, const glm::mat4& value);
|
void SetUniform(const std::string& name, const glm::mat4& value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set a 4 component vector uniform
|
||||||
|
*
|
||||||
|
* @param name Name of the uniform
|
||||||
|
* @param value Value of the uniform
|
||||||
|
*/
|
||||||
void SetUniform(const std::string& name, const glm::vec4& value);
|
void SetUniform(const std::string& name, const glm::vec4& value);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -6,26 +6,102 @@
|
||||||
namespace lol
|
namespace lol
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An object with a position, rotation and scale
|
||||||
|
*/
|
||||||
class Transformable
|
class Transformable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
/**
|
||||||
|
* Create new object at (0, 0, 0), no rotation and default scaling (1)
|
||||||
|
*/
|
||||||
Transformable();
|
Transformable();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Get position of the object
|
||||||
|
*
|
||||||
|
* @return The position of the object
|
||||||
|
*/
|
||||||
const glm::vec3& GetPosition() const;
|
const glm::vec3& GetPosition() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Set position of the object
|
||||||
|
*
|
||||||
|
* @param pos The new position of the object
|
||||||
|
*/
|
||||||
void SetPosition(const glm::vec3& pos);
|
void SetPosition(const glm::vec3& pos);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Move the object
|
||||||
|
*
|
||||||
|
* @param direction Vector to move the object by
|
||||||
|
*/
|
||||||
void Move(const glm::vec3& direction);
|
void Move(const glm::vec3& direction);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Get rotation of the object in euler angles
|
||||||
|
*
|
||||||
|
* @return The rotation of the object
|
||||||
|
*/
|
||||||
const glm::vec3 GetRotation() const;
|
const glm::vec3 GetRotation() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Get the rotation quaternion of the object
|
||||||
|
*
|
||||||
|
* @return The rotation of the object
|
||||||
|
*/
|
||||||
const glm::quat& GetQuaternion() const;
|
const glm::quat& GetQuaternion() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Set rotation of the object via axis and angle
|
||||||
|
*
|
||||||
|
* @param axis Axis to rotate the object around
|
||||||
|
* @param angle Angle by which to rotate the object
|
||||||
|
*/
|
||||||
void SetRotation(const glm::vec3& axis, float angle);
|
void SetRotation(const glm::vec3& axis, float angle);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Set rotation of the object via euler angles
|
||||||
|
*
|
||||||
|
* @param eulerAngles The orientation of the object
|
||||||
|
*/
|
||||||
void SetRotation(const glm::vec3& eulerAngles);
|
void SetRotation(const glm::vec3& eulerAngles);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Rotate object around axis
|
||||||
|
*
|
||||||
|
* @param axis Axis to rotate the object around
|
||||||
|
* @param angle Angle by which to rotate the object
|
||||||
|
*/
|
||||||
void Rotate(const glm::vec3& axis, float angle);
|
void Rotate(const glm::vec3& axis, float angle);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Get scale of the object
|
||||||
|
*
|
||||||
|
* @return Scale of the object
|
||||||
|
*/
|
||||||
const glm::vec3& GetScale() const;
|
const glm::vec3& GetScale() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Set scale of the object
|
||||||
|
*
|
||||||
|
* @param scale New scale of the object
|
||||||
|
*/
|
||||||
void SetScale(const glm::vec3& scale);
|
void SetScale(const glm::vec3& scale);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Scale object by some amount
|
||||||
|
*
|
||||||
|
* @param factor By how much to scale the object in all direction
|
||||||
|
*/
|
||||||
void Scale(const glm::vec3& factor);
|
void Scale(const glm::vec3& factor);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
/**
|
||||||
|
* @brief Reconstructs the ultimate transformation matrix
|
||||||
|
*/
|
||||||
void CalculateTransformationMatrix();
|
void CalculateTransformationMatrix();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
|
@ -9,7 +9,9 @@
|
||||||
namespace lol
|
namespace lol
|
||||||
{
|
{
|
||||||
|
|
||||||
// struct representing an OpenGL attribute pointer
|
/**
|
||||||
|
* Struct representing an OpenGL attribute pointer
|
||||||
|
*/
|
||||||
struct VertexAttribute
|
struct VertexAttribute
|
||||||
{
|
{
|
||||||
int size;
|
int size;
|
||||||
|
@ -24,6 +26,7 @@ namespace lol
|
||||||
typedef std::vector<unsigned int> IndexArray;
|
typedef std::vector<unsigned int> IndexArray;
|
||||||
typedef std::vector<VertexAttribute> Layout;
|
typedef std::vector<VertexAttribute> Layout;
|
||||||
|
|
||||||
|
|
||||||
// OpenGL Buffer usages (I turned them into an enum so it's easier to know what options exist)
|
// OpenGL Buffer usages (I turned them into an enum so it's easier to know what options exist)
|
||||||
enum class Usage
|
enum class Usage
|
||||||
{
|
{
|
||||||
|
@ -37,18 +40,43 @@ namespace lol
|
||||||
// if they have the same model.
|
// if they have the same model.
|
||||||
typedef std::shared_ptr<UniqueVertexArrayObject> VertexArrayObject;
|
typedef std::shared_ptr<UniqueVertexArrayObject> VertexArrayObject;
|
||||||
|
|
||||||
// VAO structure that sets up the buffers and deletes them at the end of the lifecycle
|
/**
|
||||||
|
* VAO structure that sets up the buffers and deletes them at the end of the lifecycle
|
||||||
|
*/
|
||||||
class UniqueVertexArrayObject : public NonCopyable
|
class UniqueVertexArrayObject : public NonCopyable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
/**
|
||||||
|
* @brief Creates new VAO from a set of vertices and indices and a layout/usage description
|
||||||
|
*
|
||||||
|
* @param vertices The vertices to go into the array buffer
|
||||||
|
* @param indices The indices to go into the element array buffer
|
||||||
|
* @param layout The layout of the VAO
|
||||||
|
* @param usage Usage of the VAO (static, dynamic, stream)
|
||||||
|
*/
|
||||||
UniqueVertexArrayObject(const VertexArray& vertices, const IndexArray& indices, const Layout& layout, Usage usage = Usage::Static);
|
UniqueVertexArrayObject(const VertexArray& vertices, const IndexArray& indices, const Layout& layout, Usage usage = Usage::Static);
|
||||||
~UniqueVertexArrayObject();
|
~UniqueVertexArrayObject();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Creates a shareable UniqueVertexArrayObject. Note that they're SHAREable, not COPYable
|
||||||
|
*
|
||||||
|
* @param vertices The vertices to go into the array buffer
|
||||||
|
* @param indices The indices to go into the element array buffer
|
||||||
|
* @param layout The layout of the VAO
|
||||||
|
* @param usage Usage of the VAO (static, dynamic, stream)
|
||||||
|
*
|
||||||
|
* @returns Shared pointer to a UniqueVertexArrayObject
|
||||||
|
*/
|
||||||
inline static VertexArrayObject Share(const VertexArray& vertices, const IndexArray& indices, const Layout& layout, Usage usage = Usage::Static)
|
inline static VertexArrayObject Share(const VertexArray& vertices, const IndexArray& indices, const Layout& layout, Usage usage = Usage::Static)
|
||||||
{
|
{
|
||||||
return std::make_shared<UniqueVertexArrayObject>(vertices, indices, layout, usage);
|
return std::make_shared<UniqueVertexArrayObject>(vertices, indices, layout, usage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Render the VAO
|
||||||
|
*
|
||||||
|
* @param mode The render mode (lines, triangles, ...)
|
||||||
|
*/
|
||||||
void Render(unsigned int mode = 4);
|
void Render(unsigned int mode = 4);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -2,6 +2,10 @@
|
||||||
|
|
||||||
namespace lol
|
namespace lol
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* A object without copy constructors or assignment operators.
|
||||||
|
* Any object inheriting from this will not be able to be copied anymore
|
||||||
|
*/
|
||||||
class NonCopyable
|
class NonCopyable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
Loading…
Reference in a new issue