New Plane and Sphere class files.

master
Andrew Copland 2014-10-09 19:21:04 +01:00
parent e952e2176a
commit e579f1cb16
7 changed files with 134 additions and 71 deletions

View File

@ -15,77 +15,11 @@
#include "graphics/Graphics.h"
#include "graphics/VertexArray.h"
#include "MathUtil.h"
#include "Sphere.h"
#include "vcacheopt/vcacheopt.h"
#include <deque>
#include <algorithm>
struct SPlane {
double a, b, c, d;
double DistanceToPoint(const vector3d &p) const {
return a*p.x + b*p.y + c*p.z + d;
}
SPlane::SPlane(const vector3d& N, const vector3d &P)
{
const vector3d NormalizedNormal = N.Normalized();
a = NormalizedNormal.x;
b = NormalizedNormal.y;
c = NormalizedNormal.z;
d = -(P.Dot(NormalizedNormal));
}
};
struct SSphere3D {
SSphere3D() : m_centre(vector3d(0.0)), m_radius(1.0) {}
vector3d m_centre;
double m_radius;
// Adapted from Ysaneya here: http://www.gamedev.net/blog/73/entry-1666972-horizon-culling/
///
/// Performs horizon culling with an object's bounding sphere, given a view point.
/// This function checks whether the object's sphere is inside the view cone formed
/// by the view point and this sphere. The view cone is capped so that the visibility
/// is false only in the 'shadow' of this sphere.
/// @param view Position of view point in world space
/// @param obj Bounding sphere of another object.
/// @return true if the object's bounding sphere is visible from the viewpoint, false if the
/// sphere is in the shadow cone AND not in front of the capping plane.
///
bool SSphere3D::HorizonCulling(const vector3d& view, const SSphere3D& obj) const
{
vector3d O1C = m_centre - view;
vector3d O2C = obj.m_centre - view;
const double D1 = O1C.Length();
const double D2 = O2C.Length();
const double R1 = m_radius;
const double R2 = obj.m_radius;
const double iD1 = 1.0f / D1;
const double iD2 = 1.0f / D2;
O1C *= iD1;
O2C *= iD2;
const double K = O1C.Dot(O2C);
const double K1 = R1 * iD1;
const double K2 = R2 * iD2;
bool status = true;
if ( K > K1 * K2 )
{
status = (-2.0f * K * K1 * K2 + K1 * K1 + K2 * K2 < 1.0f - K * K);
}
const double y = R1 * R1 * iD1;
const vector3d P = m_centre - y * O1C;
const vector3d N = -O1C;
SPlane plane(N, P);
status = status || (plane.DistanceToPoint(obj.m_centre) > obj.m_radius);
return status;
}
};
// tri edge lengths
static const double GEOPATCH_SUBDIVIDE_AT_CAMDIST = 5.0;
@ -187,10 +121,10 @@ void GeoPatch::_UpdateVBOs(Graphics::Renderer *renderer)
}
// the default sphere we do the horizon culling against
static const SSphere3D s_sph;
static const SSphere s_sph(0.9999);
void GeoPatch::Render(Graphics::Renderer *renderer, const vector3d &campos, const matrix4x4d &modelView, const Graphics::Frustum &frustum)
{
SSphere3D obj;
SSphere obj;
obj.m_centre = clipCentroid;
obj.m_radius = clipRadius;

18
src/Plane.cpp Normal file
View File

@ -0,0 +1,18 @@
// Copyright © 2008-2014 Pioneer Developers. See AUTHORS.txt for details
// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
#include "libs.h"
#include "Plane.h"
double SPlane::DistanceToPoint(const vector3d &p) const {
return a*p.x + b*p.y + c*p.z + d;
}
SPlane::SPlane(const vector3d& N, const vector3d &P)
{
const vector3d NormalizedNormal = N.Normalized();
a = NormalizedNormal.x;
b = NormalizedNormal.y;
c = NormalizedNormal.z;
d = -(P.Dot(NormalizedNormal));
}

19
src/Plane.h Normal file
View File

@ -0,0 +1,19 @@
// Copyright © 2008-2014 Pioneer Developers. See AUTHORS.txt for details
// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
#pragma once
#ifndef _PLANE_H
#define _PLANE_H
#include <SDL_stdinc.h>
#include "vector3.h"
struct SPlane {
double a, b, c, d;
double DistanceToPoint(const vector3d &p) const;
SPlane(const vector3d& N, const vector3d &P);
};
#endif /* _GEOPATCH_H */

53
src/Sphere.cpp Normal file
View File

@ -0,0 +1,53 @@
// Copyright © 2008-2014 Pioneer Developers. See AUTHORS.txt for details
// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
#include "libs.h"
#include "Sphere.h"
#include "Plane.h"
// Adapted from Ysaneya here: http://www.gamedev.net/blog/73/entry-1666972-horizon-culling/
///
/// Performs horizon culling with an object's bounding sphere, given a view point.
/// This function checks whether the object's sphere is inside the view cone formed
/// by the view point and this sphere. The view cone is capped so that the visibility
/// is false only in the 'shadow' of this sphere.
/// @param view Position of view point in world space
/// @param obj Bounding sphere of another object.
/// @return true if the object's bounding sphere is visible from the viewpoint, false if the
/// sphere is in the shadow cone AND not in front of the capping plane.
///
bool SSphere::HorizonCulling(const vector3d& view, const SSphere& obj) const
{
vector3d O1C = m_centre - view;
vector3d O2C = obj.m_centre - view;
const double D1 = O1C.Length();
const double D2 = O2C.Length();
const double R1 = m_radius;
const double R2 = obj.m_radius;
const double iD1 = 1.0f / D1;
const double iD2 = 1.0f / D2;
O1C *= iD1;
O2C *= iD2;
const double K = O1C.Dot(O2C);
if( K > 0.85 )
return true;
const double K1 = R1 * iD1;
const double K2 = R2 * iD2;
bool status = true;
if ( K > K1 * K2 )
{
status = (-2.0f * K * K1 * K2 + K1 * K1 + K2 * K2 < 1.0f - K * K);
}
const double y = R1 * R1 * iD1;
const vector3d P = m_centre - y * O1C;
const vector3d N = -O1C;
SPlane plane(N, P);
status = status || (plane.DistanceToPoint(obj.m_centre) > obj.m_radius);
return status;
}

23
src/Sphere.h Normal file
View File

@ -0,0 +1,23 @@
// Copyright © 2008-2014 Pioneer Developers. See AUTHORS.txt for details
// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
#pragma once
#ifndef _SPHERE_H
#define _SPHERE_H
#include <SDL_stdinc.h>
#include "vector3.h"
struct SSphere {
SSphere() : m_centre(vector3d(0.0)), m_radius(1.0) {}
SSphere(const double rad) : m_centre(vector3d(0.0)), m_radius(rad) {}
vector3d m_centre;
double m_radius;
// Adapted from Ysaneya here: http://www.gamedev.net/blog/73/entry-1666972-horizon-culling/
bool SSphere::HorizonCulling(const vector3d& view, const SSphere& obj) const;
};
#endif /* _SPHERE_H */

View File

@ -259,6 +259,7 @@
<ClCompile Include="..\..\src\Orbit.cpp" />
<ClCompile Include="..\..\src\perlin.cpp" />
<ClCompile Include="..\..\src\Pi.cpp" />
<ClCompile Include="..\..\src\Plane.cpp" />
<ClCompile Include="..\..\src\Planet.cpp" />
<ClCompile Include="..\..\src\Player.cpp" />
<ClCompile Include="..\..\src\PngWriter.cpp" />
@ -297,6 +298,7 @@
<ClCompile Include="..\..\src\SpaceStation.cpp" />
<ClCompile Include="..\..\src\SpaceStationType.cpp" />
<ClCompile Include="..\..\src\SpeedLines.cpp" />
<ClCompile Include="..\..\src\Sphere.cpp" />
<ClCompile Include="..\..\src\Star.cpp" />
<ClCompile Include="..\..\src\StringF.cpp" />
<ClCompile Include="..\..\src\SystemInfoView.cpp" />
@ -410,6 +412,7 @@
<ClInclude Include="..\..\src\perlin.h" />
<ClInclude Include="..\..\src\PersistSystemData.h" />
<ClInclude Include="..\..\src\Pi.h" />
<ClInclude Include="..\..\src\Plane.h" />
<ClInclude Include="..\..\src\Planet.h" />
<ClInclude Include="..\..\src\Player.h" />
<ClInclude Include="..\..\src\PngWriter.h" />
@ -440,6 +443,7 @@
<ClInclude Include="..\..\src\SpaceStation.h" />
<ClInclude Include="..\..\src\SpaceStationType.h" />
<ClInclude Include="..\..\src\SpeedLines.h" />
<ClInclude Include="..\..\src\Sphere.h" />
<ClInclude Include="..\..\src\Star.h" />
<ClInclude Include="..\..\src\StringF.h" />
<ClInclude Include="..\..\src\StringRange.h" />
@ -464,4 +468,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>

View File

@ -420,6 +420,12 @@
<ClCompile Include="..\..\src\DateTime.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\..\src\Sphere.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\..\src\Plane.cpp">
<Filter>src</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\src\Aabb.h">
@ -848,10 +854,16 @@
<ClInclude Include="..\..\src\DateTime.h">
<Filter>src</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Sphere.h">
<Filter>src</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Plane.h">
<Filter>src</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="..\..\src\win32\pioneer.rc">
<Filter>src\win32</Filter>
</ResourceCompile>
</ItemGroup>
</Project>
</Project>