Compare commits
2 Commits
2854da4053
...
bdf6228679
Author | SHA1 | Date | |
---|---|---|---|
bdf6228679 | |||
6e7b980096 |
@ -31,14 +31,12 @@
|
||||
static bool VertexClipTest(M3D_F4& V, sf::Vector2f& RTsize, float gb_factor);
|
||||
|
||||
Graphic3DRenderer::Graphic3DRenderer() {
|
||||
mRTSize = {1280.f, 324.f};
|
||||
|
||||
if (mMainCamera == nullptr) {
|
||||
mMainCamera = std::make_unique<Camera>();
|
||||
mMainCamera->SetPosition(0.0f, 1.5f, -8.0f);
|
||||
mMainCamera->SetFrustrum(75.0f, mRTSize.x/mRTSize.y, 1.0f, 100.f);
|
||||
mMainCamera->UpdateCamView();
|
||||
}
|
||||
SetRTSize(1280.f, 324.f);
|
||||
mMainCamera->UpdateCamView();
|
||||
|
||||
// Fill world object list to render
|
||||
mRenderList.clear();
|
||||
@ -126,10 +124,10 @@ void Graphic3DRenderer::Draw(sf::RenderTexture& context) {
|
||||
const float sgRatio = ComputeSGRatio();
|
||||
sf::RectangleShape gndRect;
|
||||
if (mMainCamera->GetPos3f().y >= 0) {
|
||||
gndRect.setSize(sf::Vector2f(1280, mRTSize.y * (1.f - sgRatio)));
|
||||
gndRect.setPosition(sf::Vector2f(0, mRTSize.y * sgRatio));
|
||||
gndRect.setSize(sf::Vector2f(mRTSize.x, mRTSize.y * sgRatio));
|
||||
gndRect.setPosition(sf::Vector2f(0, mRTSize.y * (1.f - sgRatio)));
|
||||
} else {
|
||||
gndRect.setSize(sf::Vector2f(1280, mRTSize.y * sgRatio));
|
||||
gndRect.setSize(sf::Vector2f(mRTSize.x, mRTSize.y * (1.f - sgRatio)));
|
||||
gndRect.setPosition(sf::Vector2f(0, 0));
|
||||
}
|
||||
gndRect.setFillColor(sf::Color::Green);
|
||||
@ -234,19 +232,30 @@ void Graphic3DRenderer::UpdateInternalTestObjects() {
|
||||
mRenderList[3]->SetRotation(0.f, thetaAngle, 0.f);
|
||||
}
|
||||
|
||||
// Compute the screen ratio between the ground and the sky (aka. Line of Horizon)
|
||||
inline float Graphic3DRenderer::ComputeSGRatio() {
|
||||
// FoV angle for Y axis is recovered using frustrum FoV and apply RT screen ratio to it
|
||||
const float fovYAngle = M3D_Deg2Rad(mMainCamera->GetFoV() * (mRTSize.y/mRTSize.x));
|
||||
const float fovYAngleDiv2 = M3D_Deg2Rad(mMainCamera->GetFoV()) * 0.5f;
|
||||
// Get the camera pitch angle over camera FoV ratio
|
||||
float sgRatio = M3D_ScalarASinEst(mMainCamera->GetLook3f().y) / fovYAngle;
|
||||
const float theta = M3D_ScalarASinEst(-mMainCamera->GetLook3f().y);
|
||||
// Get the camera altitude from the ground
|
||||
const float altitude = mMainCamera->GetPos3f().y;
|
||||
|
||||
// Ground/Sky screen ratio calculation using "simple" trigonometric properties of the
|
||||
// pinhole (frustrum) camera model.
|
||||
// The triangle made by the ground plane intersection with the frustum. This intersection
|
||||
// cross the far plane at some point. Instead of computing the coordinate of the point, we
|
||||
// directly use the far plane length to get the corresponding ratio for the screen.
|
||||
double sgRatio = -(altitude * M3D_ScalarCosEst(fovYAngleDiv2) - mMainCamera->GetFarZ() * M3D_ScalarSinEst(fovYAngleDiv2 + theta))
|
||||
/ (2.f * mMainCamera->GetFarZ() * M3D_ScalarSinEst(fovYAngleDiv2) * M3D_ScalarCosEst(theta));
|
||||
|
||||
// Clamp and re-scale
|
||||
if (sgRatio > M3D_PIDIV2)
|
||||
sgRatio = M3D_PIDIV2;
|
||||
else if (sgRatio < -M3D_PIDIV2)
|
||||
sgRatio = -M3D_PIDIV2;
|
||||
// Clamp
|
||||
if (sgRatio > 1.f)
|
||||
sgRatio = 1.f;
|
||||
else if (sgRatio < 0.f)
|
||||
sgRatio = 0.f;
|
||||
|
||||
return ((sgRatio - M3D_PIDIV2) / M3D_PI) + 1.f;
|
||||
return sgRatio;
|
||||
}
|
||||
|
||||
inline static bool VertexClipTest(M3D_F4& V, sf::Vector2f& RTsize, float gb_factor) {
|
||||
|
@ -8,6 +8,7 @@ Camera::Camera() {
|
||||
void Camera::SetFrustrum(float fov, float r, float zn, float zf) {
|
||||
//if (!frameDirty)
|
||||
this->fov = fov;
|
||||
this->zf = zf;
|
||||
M3D_MATRIX pMat = M3D_TransformMatrixFrustrumFovLH(M3D_Deg2Rad(fov), r, zn, zf);
|
||||
M3D_V4StoreF4x4(&mProjMat, pMat);
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ public:
|
||||
Camera& operator= (Camera const&) = delete;
|
||||
|
||||
float GetFoV() const { return fov; }
|
||||
float GetFarZ() const { return zf; }
|
||||
M3D_VECTOR GetPos() const { return M3D_V4LoadF3(&mPos); }
|
||||
M3D_F3 GetPos3f() const { return mPos; }
|
||||
M3D_VECTOR GetLook() const { return M3D_V4LoadF3(&mLook); }
|
||||
@ -39,7 +40,8 @@ public:
|
||||
void Yaw(float angle);
|
||||
|
||||
private:
|
||||
float fov;
|
||||
float fov; // It's the Y-FoV!
|
||||
float zf;
|
||||
|
||||
M3D_F4X4 mProjMat = M3D_MIdentity4x4();
|
||||
M3D_F4X4 mViewMat = M3D_MIdentity4x4();
|
||||
|
Loading…
x
Reference in New Issue
Block a user