Skip to content

Commit 4616f5f

Browse files
committed
Add Renderer::buildProjectionMatrix() to create Projection matrix
1 parent a26cdca commit 4616f5f

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

src/celengine/render.cpp

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -589,9 +589,9 @@ void Renderer::resize(int width, int height)
589589

590590
float Renderer::calcPixelSize(float fovY, float windowHeight)
591591
{
592-
if (getProjectionMode() == ProjectionMode::FisheyeMode)
593-
return 2.0f / windowHeight;
594-
return 2 * (float) tan(degToRad(fovY / 2.0)) / (float) windowHeight;
592+
return (getProjectionMode() == ProjectionMode::FisheyeMode)
593+
? 2.0f / windowHeight
594+
: 2.0f * tan(degToRad(fovY * 0.5f)) / static_cast<float>(windowHeight);
595595
}
596596

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

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

@@ -4049,7 +4045,7 @@ void Renderer::renderSkyGrids(const Observer& observer)
40494045
}
40504046

40514047
if ((renderFlags & ShowEcliptic) != 0)
4052-
renderEclipticLine();
4048+
m_eclipticLineRenderer->render();
40534049
}
40544050

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

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

53205312
setCurrentProjectionMatrix(proj);
53215313

53225314
Frustum intervalFrustum(degToRad(fov),
5323-
aspectRatio,
5315+
getAspectRatio(),
53245316
nearPlaneDistance,
53255317
farPlaneDistance);
53265318

@@ -5446,8 +5438,10 @@ Renderer::setPipelineState(const Renderer::PipelineState &ps) noexcept
54465438
}
54475439
}
54485440

5449-
void Renderer::renderEclipticLine()
5441+
void Renderer::buildProjectionMatrix(Eigen::Matrix4f &mat, float nearZ, float farZ)
54505442
{
5451-
if ((renderFlags & ShowEcliptic) != 0)
5452-
m_eclipticLineRenderer->render();
5443+
float aspectRatio = getAspectRatio();
5444+
mat = (getProjectionMode() == Renderer::ProjectionMode::FisheyeMode)
5445+
? celmath::Ortho(-aspectRatio, aspectRatio, -1.0f, 1.0f, nearZ, farZ)
5446+
: celmath::Perspective(fov, aspectRatio, nearZ, farZ);
54535447
}

src/celengine/render.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,8 @@ class Renderer
361361
m_projectionPtr = &m_projMatrix;
362362
}
363363

364+
void buildProjectionMatrix(Eigen::Matrix4f &mat, float nearZ, float farZ);
365+
364366
void setStarStyle(StarStyle);
365367
StarStyle getStarStyle() const;
366368
void setResolution(unsigned int resolution);
@@ -503,7 +505,6 @@ class Renderer
503505

504506
void renderAsterisms(const Universe&, float, const Matrices&);
505507
void renderBoundaries(const Universe&, float, const Matrices&);
506-
void renderEclipticLine();
507508
void renderCrosshair(float size, double tsec, const Color &color, const Matrices &m);
508509

509510
void buildNearSystemsLists(const Universe &universe,

0 commit comments

Comments
 (0)