Skip to content

Commit

Permalink
Add Renderer::buildProjectionMatrix() to create Projection matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
375gnu committed May 15, 2023
1 parent a26cdca commit 4616f5f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
30 changes: 12 additions & 18 deletions src/celengine/render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -589,9 +589,9 @@ void Renderer::resize(int width, int height)

float Renderer::calcPixelSize(float fovY, float windowHeight)
{
if (getProjectionMode() == ProjectionMode::FisheyeMode)
return 2.0f / windowHeight;
return 2 * (float) tan(degToRad(fovY / 2.0)) / (float) windowHeight;
return (getProjectionMode() == ProjectionMode::FisheyeMode)
? 2.0f / windowHeight
: 2.0f * tan(degToRad(fovY * 0.5f)) / static_cast<float>(windowHeight);
}

void Renderer::setFieldOfView(float _fov)
Expand Down Expand Up @@ -1544,11 +1544,7 @@ void Renderer::draw(const Observer& observer,

// Set up the projection and modelview matrices.
// We'll usethem for positioning star and planet labels.
float aspectRatio = getAspectRatio();
if (getProjectionMode() == Renderer::ProjectionMode::FisheyeMode)
m_projMatrix = Ortho(-aspectRatio, aspectRatio, -1.0f, 1.0f, NEAR_DIST, FAR_DIST);
else
m_projMatrix = Perspective(fov, aspectRatio, NEAR_DIST, FAR_DIST);
buildProjectionMatrix(m_projMatrix, NEAR_DIST, FAR_DIST);
m_modelMatrix = Affine3f(getCameraOrientation()).matrix();
m_MVPMatrix = m_projMatrix * m_modelMatrix;

Expand Down Expand Up @@ -4049,7 +4045,7 @@ void Renderer::renderSkyGrids(const Observer& observer)
}

if ((renderFlags & ShowEcliptic) != 0)
renderEclipticLine();
m_eclipticLineRenderer->render();
}

void Renderer::labelConstellations(const AsterismList& asterisms,
Expand Down Expand Up @@ -5309,18 +5305,14 @@ Renderer::renderSolarSystemObjects(const Observer &observer,

// Set up a perspective projection using the current interval's near and
// far clip planes.
float aspectRatio = getAspectRatio();
Matrix4f proj;
if (getProjectionMode() == Renderer::ProjectionMode::FisheyeMode)
proj = Ortho(-aspectRatio, aspectRatio, -1.0f, 1.0f, nearPlaneDistance, farPlaneDistance);
else
proj = Perspective(fov, aspectRatio, nearPlaneDistance, farPlaneDistance);
buildProjectionMatrix(proj, nearPlaneDistance, farPlaneDistance);
Matrices m = { &proj, &m_modelMatrix };

setCurrentProjectionMatrix(proj);

Frustum intervalFrustum(degToRad(fov),
aspectRatio,
getAspectRatio(),
nearPlaneDistance,
farPlaneDistance);

Expand Down Expand Up @@ -5446,8 +5438,10 @@ Renderer::setPipelineState(const Renderer::PipelineState &ps) noexcept
}
}

void Renderer::renderEclipticLine()
void Renderer::buildProjectionMatrix(Eigen::Matrix4f &mat, float nearZ, float farZ)
{
if ((renderFlags & ShowEcliptic) != 0)
m_eclipticLineRenderer->render();
float aspectRatio = getAspectRatio();
mat = (getProjectionMode() == Renderer::ProjectionMode::FisheyeMode)
? celmath::Ortho(-aspectRatio, aspectRatio, -1.0f, 1.0f, nearZ, farZ)
: celmath::Perspective(fov, aspectRatio, nearZ, farZ);
}
3 changes: 2 additions & 1 deletion src/celengine/render.h
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ class Renderer
m_projectionPtr = &m_projMatrix;
}

void buildProjectionMatrix(Eigen::Matrix4f &mat, float nearZ, float farZ);

void setStarStyle(StarStyle);
StarStyle getStarStyle() const;
void setResolution(unsigned int resolution);
Expand Down Expand Up @@ -503,7 +505,6 @@ class Renderer

void renderAsterisms(const Universe&, float, const Matrices&);
void renderBoundaries(const Universe&, float, const Matrices&);
void renderEclipticLine();
void renderCrosshair(float size, double tsec, const Color &color, const Matrices &m);

void buildNearSystemsLists(const Universe &universe,
Expand Down

0 comments on commit 4616f5f

Please sign in to comment.