diff --git a/include/lol/Camera.hpp b/include/lol/Camera.hpp index 8b3e2a7..830298b 100644 --- a/include/lol/Camera.hpp +++ b/include/lol/Camera.hpp @@ -6,8 +6,13 @@ namespace lol { - // 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 { public: @@ -16,21 +21,39 @@ namespace lol void SetScale(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) { 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 { return transformation; } + /** + *@brief Get the cameras projection matrix + * + * @returns The cameras projection matrix + */ inline const glm::mat4& GetProjection() const { return projection; } + /** + * @brief Alternative rendering syntax + * + * @param drawable A Drawable that should be rendered through this camera + */ inline void Draw(const Drawable& drawable) const { drawable.Draw(*this); @@ -41,28 +64,57 @@ namespace lol }; + /** + * A camera using perspective projection + */ class Camera : public CameraBase { 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) { 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) { projection = glm::perspective(glm::radians(fov), aspect, zNear, zFar); } }; + + /** + * A camera using orthographic projection + */ class OrthogonalCamera : public CameraBase { 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) { 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) { projection = glm::ortho(left, right, bottom, top, zNear, zFar); diff --git a/include/lol/Drawable.hpp b/include/lol/Drawable.hpp index 81d6583..d9d5846 100644 --- a/include/lol/Drawable.hpp +++ b/include/lol/Drawable.hpp @@ -21,11 +21,29 @@ namespace lol TriangleFan = GL_TRIANGLE_FAN }; + /** + * A class that can be displayed on a screen. + */ class Drawable { 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 { }; + + /** + * @brief Bind the shader and draw the VAO + * + * @param camera The camera with which this object is rendered. + */ 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); protected: diff --git a/include/lol/Shader.hpp b/include/lol/Shader.hpp index e314c22..8428083 100644 --- a/include/lol/Shader.hpp +++ b/include/lol/Shader.hpp @@ -14,21 +14,60 @@ namespace lol class UniqueShader; typedef std::shared_ptr Shader; + /** + * Compiles shaders into a program and manages access to that program + */ class UniqueShader : public NonCopyable { 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(); + /** + * @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) { return std::make_shared(vertexShader, fragmentShader); } + /** + * @brief Status of the program creation + * + * @returns `true` if shader was successfully created + */ inline bool Good() { return id != 0; } + + /** + * @brief Bind this shader program + */ 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); + + /** + * 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); private: diff --git a/include/lol/Transformable.hpp b/include/lol/Transformable.hpp index 340cf9f..21a482c 100644 --- a/include/lol/Transformable.hpp +++ b/include/lol/Transformable.hpp @@ -6,26 +6,102 @@ namespace lol { + /** + * An object with a position, rotation and scale + */ class Transformable { public: + /** + * Create new object at (0, 0, 0), no rotation and default scaling (1) + */ Transformable(); + /** + * @brief Get position of the object + * + * @return The position of the object + */ 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); + + /** + * @brief Move the object + * + * @param direction Vector to move the object by + */ 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; + + /** + * @brief Get the rotation quaternion of the object + * + * @return The rotation of the object + */ 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); + + /** + * @brief Set rotation of the object via euler angles + * + * @param eulerAngles The orientation of the object + */ 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); + + /** + * @brief Get scale of the object + * + * @return Scale of the object + */ const glm::vec3& GetScale() const; + + /** + * @brief Set scale of the object + * + * @param scale New scale of the object + */ 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); private: + /** + * @brief Reconstructs the ultimate transformation matrix + */ void CalculateTransformationMatrix(); protected: diff --git a/include/lol/VertexArrayObject.hpp b/include/lol/VertexArrayObject.hpp index ca17d8c..8d1e10e 100644 --- a/include/lol/VertexArrayObject.hpp +++ b/include/lol/VertexArrayObject.hpp @@ -9,7 +9,9 @@ namespace lol { - // struct representing an OpenGL attribute pointer + /** + * Struct representing an OpenGL attribute pointer + */ struct VertexAttribute { int size; @@ -24,6 +26,7 @@ namespace lol typedef std::vector IndexArray; typedef std::vector Layout; + // OpenGL Buffer usages (I turned them into an enum so it's easier to know what options exist) enum class Usage { @@ -37,18 +40,43 @@ namespace lol // if they have the same model. typedef std::shared_ptr 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 { 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(); + /** + * @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) { return std::make_shared(vertices, indices, layout, usage); } + /** + * @brief Render the VAO + * + * @param mode The render mode (lines, triangles, ...) + */ void Render(unsigned int mode = 4); private: diff --git a/include/lol/util/NonCopyable.hpp b/include/lol/util/NonCopyable.hpp index 3c1374c..3c603ce 100644 --- a/include/lol/util/NonCopyable.hpp +++ b/include/lol/util/NonCopyable.hpp @@ -2,6 +2,10 @@ 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 { public: