Commit 328a4b02 authored by Philip Trettner's avatar Philip Trettner

Initial commit with readme and ported cameras

parents
cmake_minimum_required(VERSION 3.0)
project(glow-extras)
add_library(glow-extras ${GLOW_LINK_TYPE} glow-extras.cc)
# Camera
add_subdirectory(camera)
target_link_libraries(glow-extras PUBLIC glow-extras-camera)
# Glow Extras
Companion library for glow with convenience and helper functions.
## Usage
Add git submodule
```
git submodule add https://www.graphics.rwth-aachen.de:9000/Glow/glow-extras.git
```
Add to cmake (be sure to do that _after_ glow)
```
add_subdirectory(path/to/glow-extras)
```
Choose which libraries you need
```
# all
target_link_libraries(YourLib PUBLIC glow-extras)
# .. or single ones
target_link_libraries(YourLib PUBLIC glow-extras-camera)
target_link_libraries(YourLib PUBLIC glow-extras-mesh)
target_link_libraries(YourLib PUBLIC glow-extras-postprocess)
```
## Sub-libs
Currently supported sub-libs with their intent.
### glow-extras-camera
Camera classes:
* BaseCamera
* StaticCamera
* GenericCamera
cmake_minimum_required(VERSION 3.0)
file(GLOB_RECURSE SOURCE_FILES "*.cc")
file(GLOB_RECURSE HEADER_FILES "*.hh")
add_library(glow-extras-camera ${GLOW_LINK_TYPE} ${SOURCE_FILES} ${HEADER_FILES})
target_include_directories(glow-extras-camera PUBLIC ./)
target_compile_options(glow-extras-camera PRIVATE -Wall -Werror -std=c++11)
target_link_libraries(glow-extras-camera PUBLIC glow)
#include "CameraBase.hh"
using namespace glow::extras;
CameraBase::CameraBase()
{
}
CameraBase::~CameraBase()
{
}
#pragma once
#include <glm/fwd.hpp>
namespace glow
{
namespace extras
{
/**
* @brief Common interface for cameras
*
* This interface only contains getter on purpose.
* All logic that wants to modify a camera should know the actual structure of the camera and therefore use the
* specific subclass.
*/
class CameraBase
{
protected:
CameraBase();
public:
virtual ~CameraBase();
/**
* @brief gets the Position of the camera
* @return a 3-dimensional position in the global coordinate system
*/
virtual glm::vec3 getPosition() const = 0;
/**
* @brief gets the ViewMatrix of the camera
* @return a 4x4 matrix containing projection independent camera transforms
*/
virtual glm::mat4 getViewMatrix() const = 0;
/**
* @brief gets the ProjectionMatrix of the camera
* @return a 4x4 matrix containing the projection into normalized device coordinates
*/
virtual glm::mat4 getProjectionMatrix() const = 0;
/**
* @brief gets the ViewportSize of the current viewport of this camera
* @return the 2-dimensional size of the viewport
*/
virtual glm::uvec2 getViewportSize() const = 0;
/**
* @brief Gets the near clipping plane as a distance from the camera.
* @return the near clipping plane
*/
virtual float getNearClippingPlane() const = 0;
/**
* @brief Gets the far clipping plane as a distance from the camera. Note that it could be inf!
* Not all projection matrices have a real far plane but some are unlimited!
* @return the near clipping plane
*/
virtual float getFarClippingPlane() const = 0;
};
}
}
#include "FixedCamera.hh"
#include <glm/glm.hpp>
using namespace glow::extras;
FixedCamera::FixedCamera()
{
}
FixedCamera::FixedCamera(const glm::vec3 &_pos, const glm::mat4 &_view, const glm::mat4 &_proj, const glm::uvec2 &_viewport)
: mPosition(_pos), mViewMatrix(_view), mViewportSize(_viewport)
{
setProjectionMatrix(_proj);
}
void FixedCamera::setProjectionMatrix(glm::mat4 const &_val)
{
mProjectionMatrix = _val;
//
// Calculate near and far plane from the matrix
// As the matrix does not have to be a standard OpenGL
// projection matrix, we can't derrive the values from
// the matrix entries directly but we have to reproject
// points from the NDC cube... ...but as some matrices
// project to -1..1 and others to 0..1 we have to figure
// this out first... ...keeping in mind, that some also
// invert the range inside of the Z buffer, so Z=1 might
// be the near plane while Z=0 is the far plane...
// ...oh, and the far plane might be at infinity...
//
//
// First step, figure out which values project to
// -1, 0 and 1:
//
glm::mat4 invProj = glm::inverse(mProjectionMatrix);
glm::vec4 tmp = invProj * glm::vec4(0.0f, 0.0f, -1.0f, 1.0f);
float reproj_minusOne = tmp.z / tmp.w;
tmp = invProj * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
float reproj_Zero = tmp.z / tmp.w;
tmp = invProj * glm::vec4(0.0f, 0.0f, 1.0f, 1.0f);
float reproj_One = tmp.z / tmp.w;
//
// Next step, figure out which value is the near plane and which is the far plane.
//
// right handed coordinate system -> the camera was looking along the negative Z axis
// if it was an OpenGL matrix, the projections will result in -near, -something, -far
// if it was an OpenGL matrix with infinit far plane, the projections will result in -near, -something, -inf
// if it was a reverse DX style matrix it will result in +near, -inf, -near
if (reproj_minusOne > 0.0f)
{
// near,far mapped to 1,0 or 0,1 as -1 was mapped to a positive value which is behind the camera
// if we assume we are looking along the negative Z axis!
if (-reproj_Zero > -reproj_One)
{
// the far plane is mapped to 0 -> inverse DX style
mFarPlane = -reproj_Zero;
mNearPlane = -reproj_One;
}
else
{
// the near plane is mapped to zero -> DX style
mFarPlane = -reproj_One;
mNearPlane = -reproj_Zero;
}
}
else
{
// OpenGL style, mapping to -1..1 or 1..-1
if (-reproj_minusOne > -reproj_One)
{
// the far plane is mapped to -1 -> inverse GL style
mFarPlane = -reproj_minusOne;
mNearPlane = -reproj_One;
}
else
{
// the near plane is mapped to -1 -> GL style
mFarPlane = -reproj_One;
mNearPlane = -reproj_minusOne;
}
}
}
#pragma once
#include "CameraBase.hh"
#include <glm/vec3.hpp>
#include <glm/mat4x4.hpp>
#include <glm/vec2.hpp>
namespace glow
{
namespace extras
{
/**
* @brief A fixed camera where all attributes are set explicitly except for the near/far plane
* which are derrived from the projection matrix.
*/
class FixedCamera : public CameraBase
{
private:
glm::vec3 mPosition;
glm::mat4 mViewMatrix;
glm::mat4 mProjectionMatrix;
glm::uvec2 mViewportSize;
// will get calculated based on the projection matrix
// so there are no explicit setters for this!
float mNearPlane;
float mFarPlane;
public:
/// CAUTION: default ctor with zero values
FixedCamera();
FixedCamera(const glm::vec3& _pos, const glm::mat4& _view, const glm::mat4& _proj, const glm::uvec2& _viewport);
// Getter, Setter for Camera Position
virtual glm::vec3 getPosition() const override { return mPosition; }
virtual void setPosition(glm::vec3 const& _val) { mPosition = _val; }
// Getter, Setter for Camera ViewMatrix
virtual glm::mat4 getViewMatrix() const override { return mViewMatrix; }
virtual void setViewMatrix(glm::mat4 const& _val) { mViewMatrix = _val; }
// Getter, Setter for Camera ProjectionMatrix
virtual glm::mat4 getProjectionMatrix() const override { return mProjectionMatrix; }
virtual void setProjectionMatrix(glm::mat4 const& _val);
// Getter, Setter for Camera ViewportSize
virtual glm::uvec2 getViewportSize() const override { return mViewportSize; }
virtual void setViewportSize(glm::uvec2 const& _val) { mViewportSize = _val; }
// getters for near/far plane (far can be inf!)
virtual float getNearClippingPlane() const override { return mNearPlane; }
virtual float getFarClippingPlane() const override { return mFarPlane; }
};
}
}
This diff is collapsed.
This diff is collapsed.
void dummy_glow_extras_func() { }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment