Skip to content

Commit

Permalink
Merge pull request #2080 from paulhoux/camera-ortho-calc-ray
Browse files Browse the repository at this point in the history
Added a `calcRay` implementation for `CameraOrtho`.
  • Loading branch information
paulhoux committed Feb 14, 2019
2 parents 642dbbd + fa4d3c6 commit ccf4251
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
11 changes: 7 additions & 4 deletions include/cinder/Camera.h
Expand Up @@ -138,17 +138,19 @@ class CI_API Camera {

protected:
Camera()
: mModelViewCached( false ), mProjectionCached( false ), mInverseModelViewCached( false ), mWorldUp( vec3( 0, 1, 0 ) ),
mPivotDistance( 0 )
{}
: mWorldUp( vec3( 0, 1, 0 ) ), mFov( 35 ), mAspectRatio( 1 ), mNearClip( 0.1f ), mFarClip( 1000 )
, mPivotDistance( 1 ), mProjectionCached( false ), mModelViewCached( false ), mInverseModelViewCached( false )
, mFrustumLeft( -1 ), mFrustumRight( 1 ), mFrustumTop( 1 ), mFrustumBottom( -1 )
{
}

void calcMatrices() const;

virtual void calcViewMatrix() const;
virtual void calcInverseView() const;
virtual void calcProjection() const = 0;

virtual Ray calcRay( float u, float v, float imagePlaneAspectRatio ) const;
virtual Ray calcRay( float u, float v, float imagePlaneAspectRatio ) const = 0;

vec3 mEyePoint;
vec3 mViewDirection;
Expand Down Expand Up @@ -239,6 +241,7 @@ class CI_API CameraOrtho : public Camera {

protected:
void calcProjection() const override;
Ray calcRay( float u, float v, float imagePlaneAspectRatio ) const override;
};

//! A Camera used for stereoscopic displays.
Expand Down
35 changes: 15 additions & 20 deletions src/cinder/Camera.cpp
Expand Up @@ -240,28 +240,16 @@ void Camera::calcInverseView() const
mInverseModelViewCached = true;
}

Ray Camera::calcRay( float uPos, float vPos, float imagePlaneApectRatio ) const
{
calcMatrices();

float s = ( uPos - 0.5f ) * imagePlaneApectRatio;
float t = ( vPos - 0.5f );
float viewDistance = imagePlaneApectRatio / math<float>::abs( mFrustumRight - mFrustumLeft ) * mNearClip;
return Ray( mEyePoint, normalize( mU * s + mV * t - ( mW * viewDistance ) ) );
}

////////////////////////////////////////////////////////////////////////////////////////
// CameraPersp
// Creates a default camera resembling Maya Persp
CameraPersp::CameraPersp()
: Camera()
{
lookAt( vec3( 28, 21, 28 ), vec3(), vec3( 0, 1, 0 ) );
setPerspective( 35, 1.3333f, 0.1f, 1000 );
}

CameraPersp::CameraPersp( int pixelWidth, int pixelHeight, float fovDegrees )
: Camera()
{
float eyeX = pixelWidth / 2.0f;
float eyeY = pixelHeight / 2.0f;
Expand All @@ -277,7 +265,6 @@ CameraPersp::CameraPersp( int pixelWidth, int pixelHeight, float fovDegrees )
}

CameraPersp::CameraPersp( int pixelWidth, int pixelHeight, float fovDegrees, float nearPlane, float farPlane )
: Camera()
{
float halfFov, theTan, aspect;

Expand All @@ -302,13 +289,13 @@ void CameraPersp::setPerspective( float verticalFovDegrees, float aspectRatio, f
mProjectionCached = false;
}

Ray CameraPersp::calcRay( float uPos, float vPos, float imagePlaneApectRatio ) const
Ray CameraPersp::calcRay( float uPos, float vPos, float imagePlaneAspectRatio ) const
{
calcMatrices();

float s = ( uPos - 0.5f + 0.5f * mLensShift.x ) * imagePlaneApectRatio;
float s = ( uPos - 0.5f + 0.5f * mLensShift.x ) * imagePlaneAspectRatio;
float t = ( vPos - 0.5f + 0.5f * mLensShift.y );
float viewDistance = imagePlaneApectRatio / math<float>::abs( mFrustumRight - mFrustumLeft ) * mNearClip;
float viewDistance = imagePlaneAspectRatio / math<float>::abs( mFrustumRight - mFrustumLeft ) * mNearClip;
return Ray( mEyePoint, normalize( mU * s + mV * t - ( mW * viewDistance ) ) );
}

Expand Down Expand Up @@ -396,14 +383,12 @@ CameraPersp CameraPersp::calcFraming( const Sphere &worldSpaceSphere ) const
////////////////////////////////////////////////////////////////////////////////////////
// CameraOrtho
CameraOrtho::CameraOrtho()
: Camera()
{
lookAt( vec3( 0, 0, 0.1f ), vec3(), vec3( 0, 1, 0 ) );
setFov( 35 );
}

CameraOrtho::CameraOrtho( float left, float right, float bottom, float top, float nearPlane, float farPlane )
: Camera()
{
mFrustumLeft = left;
mFrustumRight = right;
Expand Down Expand Up @@ -432,10 +417,10 @@ void CameraOrtho::setOrtho( float left, float right, float bottom, float top, fl
void CameraOrtho::calcProjection() const
{
mat4 &p = mProjectionMatrix;
p[0][0] = 2 / (mFrustumRight - mFrustumLeft);
p[0][0] = 2 / ( (mFrustumRight - mFrustumLeft) * mAspectRatio );
p[1][0] = 0;
p[2][0] = 0;
p[3][0] = -(mFrustumRight + mFrustumLeft) / (mFrustumRight - mFrustumLeft);
p[3][0] = -(mFrustumRight + mFrustumLeft) / (mFrustumRight - mFrustumLeft) * mAspectRatio;

p[0][1] = 0;
p[1][1] = 2 / (mFrustumTop - mFrustumBottom);
Expand Down Expand Up @@ -476,6 +461,16 @@ void CameraOrtho::calcProjection() const
mProjectionCached = true;
}

Ray CameraOrtho::calcRay( float uPos, float vPos, float imagePlaneAspectRatio ) const
{
calcMatrices();

float s = ( uPos - 0.5f ) * imagePlaneAspectRatio;
float t = ( vPos - 0.5f );
vec3 eyePoint = mEyePoint + mU * s * ( mFrustumRight - mFrustumLeft ) + mV * t * ( mFrustumTop - mFrustumBottom );
return Ray( eyePoint, -mW );
}

////////////////////////////////////////////////////////////////////////////////////////
// CameraStereo
vec3 CameraStereo::getEyePointShifted() const
Expand Down

0 comments on commit ccf4251

Please sign in to comment.