minor refactoring

This commit is contained in:
Garux
2025-01-11 12:53:41 +05:00
parent 4ea3f36f4f
commit bf9feb943a
9 changed files with 91 additions and 111 deletions
+23 -37
View File
@@ -62,21 +62,16 @@ public:
ObservedSelectable( onchanged ) }{
}
bool isSelected() const {
for ( std::size_t i = 0; i < 6; ++i )
if( m_selectables[i].isSelected() )
return true;
return false;
return std::ranges::any_of( m_selectables, std::identity{}, &ObservedSelectable::isSelected );
}
void setSelected( bool selected ){
for ( std::size_t i = 0; i < 6; ++i )
m_selectables[i].setSelected( selected );
for ( auto& se : m_selectables )
se.setSelected( selected );
}
void selectPlanes( const AABB& aabb, Selector& selector, SelectionTest& test, const PlaneCallback& selectedPlaneCallback, const Matrix4& rotation = g_matrix4_identity ){
Vector3 corners[8];
aabb_corners_oriented( aabb, rotation, corners );
const std::array<Vector3, 8> corners = aabb_corners_oriented( aabb, rotation );
Plane3 planes[6];
aabb_planes_oriented( aabb, rotation, planes );
const std::array<Plane3, 6> planes = aabb_planes_oriented( aabb, rotation );
const std::size_t indices[24] = {
2, 1, 5, 6, //+x //right
@@ -89,8 +84,7 @@ public:
const Vector3 viewdir( test.getVolume().getViewDir() );
double bestDot = 1;
ObservedSelectable* selectable = 0;
ObservedSelectable* selectable2 = 0;
int iselect[2] = { -1, -1 };
for ( std::size_t i = 0; i < 6; ++i ){
const std::size_t index = i * 4;
@@ -105,26 +99,25 @@ public:
const double diff = bestDot - dot;
if( diff > 0.03 ){
bestDot = dot;
selectable = &m_selectables[i];
selectable2 = 0;
iselect[0] = i;
iselect[1] = -1;
}
else if( fabs( diff ) <= 0.03 ){
selectable2 = &m_selectables[i];
else if( fabs( diff ) <= 0.03 && !test.getVolume().fill() ){ // select only plane in camera
iselect[1] = i;
}
}
}
if( test.getVolume().fill() ) // select only plane in camera
selectable2 = 0;
for ( std::size_t i = 0; i < 6; ++i )
if( &m_selectables[i] == selectable || &m_selectables[i] == selectable2 ){
for ( int i : iselect )
if( i >= 0 ){
Selector_add( selector, m_selectables[i] );
selectedPlaneCallback( planes[i] );
}
m_bounds = aabb;
}
void selectReversedPlanes( const AABB& aabb, Selector& selector, const SelectedPlanes& selectedPlanes, const Matrix4& rotation = g_matrix4_identity ){
Plane3 planes[6];
aabb_planes_oriented( aabb, rotation, planes );
const std::array<Plane3, 6> planes = aabb_planes_oriented( aabb, rotation );
for ( std::size_t i = 0; i < 6; ++i )
if ( selectedPlanes.contains( plane3_flipped( planes[i] ) ) )
Selector_add( selector, m_selectables[i] );
@@ -136,11 +129,9 @@ public:
if( aabb_.extents[i] < 1 )
aabb_.extents[i] = 4;
Vector3 corners[8];
aabb_corners_oriented( aabb_, rotation, corners );
const std::array<Vector3, 8> corners = aabb_corners_oriented( aabb_, rotation );
Plane3 planes[6];
aabb_planes_oriented( aabb_, rotation, planes );
const std::array<Plane3, 6> planes = aabb_planes_oriented( aabb_, rotation );
const IndexPointer::index_type indices[24] = {
2, 1, 5, 6, //+x //right
@@ -154,7 +145,7 @@ public:
for ( std::size_t i = 0; i < 6; ++i ){
const std::size_t index = i * 4;
SelectionIntersection intersection_new;
test.TestQuads( VertexPointer( reinterpret_cast<VertexPointer::pointer>( corners ), sizeof( Vector3 ) ), IndexPointer( &indices[index], 4 ), intersection_new );
test.TestQuads( VertexPointer( corners[0].data(), sizeof( Vector3 ) ), IndexPointer( &indices[index], 4 ), intersection_new );
if( SelectionIntersection_closer( intersection_new, intersection ) ){
intersection = intersection_new;
plane = planes[i];
@@ -163,11 +154,9 @@ public:
m_bounds = aabb;
}
void bestPlaneIndirect( const AABB& aabb, SelectionTest& test, Plane3& plane, Vector3& intersection, float& dist, const Matrix4& rotation = g_matrix4_identity ) const {
Vector3 corners[8];
aabb_corners_oriented( aabb, rotation, corners );
const std::array<Vector3, 8> corners = aabb_corners_oriented( aabb, rotation );
Plane3 planes[6];
aabb_planes_oriented( aabb, rotation, planes );
const std::array<Plane3, 6> planes = aabb_planes_oriented( aabb, rotation );
/*
const std::size_t indices[24] = {
2, 1, 5, 6, //+x //right
@@ -279,8 +268,7 @@ public:
m_bounds = aabb;
}
void selectByPlane( const AABB& aabb, const Plane3& plane, const Matrix4& rotation = g_matrix4_identity ){
Plane3 planes[6];
aabb_planes_oriented( aabb, rotation, planes );
const std::array<Plane3, 6> planes = aabb_planes_oriented( aabb, rotation );
for ( std::size_t i = 0; i < 6; ++i ){
if( plane3_equal( plane, planes[i] ) || plane3_equal( plane, plane3_flipped( planes[i] ) ) ){
@@ -290,11 +278,9 @@ public:
}
}
void gatherPolygonsByPlane( const AABB& aabb, const Plane3& plane, std::vector<std::vector<Vector3>>& polygons, const Matrix4& rotation = g_matrix4_identity ) const {
Vector3 corners[8];
aabb_corners_oriented( aabb, rotation, corners );
const std::array<Vector3, 8> corners = aabb_corners_oriented( aabb, rotation );
Plane3 planes[6];
aabb_planes_oriented( aabb, rotation, planes );
const std::array<Plane3, 6> planes = aabb_planes_oriented( aabb, rotation );
const std::size_t indices[24] = {
2, 1, 5, 6, //+x //right
+8 -14
View File
@@ -112,12 +112,11 @@ inline void aabb_testselect( const AABB& aabb, SelectionTest& test, SelectionInt
7, 6, 5, 4,
};
Vector3 points[8];
aabb_corners( aabb, points );
test.TestQuads( VertexPointer( reinterpret_cast<VertexPointer::pointer>( points ), sizeof( Vector3 ) ), IndexPointer( indices, 24 ), best );
const std::array<Vector3, 8> points = aabb_corners( aabb );
test.TestQuads( VertexPointer( points[0].data(), sizeof( Vector3 ) ), IndexPointer( indices, 24 ), best );
}
inline void aabb_draw_wire( const Vector3 points[8] ){
inline void aabb_draw_wire( const std::array<Vector3, 8>& points ){
unsigned int indices[26] = {
0, 1, 1, 2, 2, 3, 3, 0,
4, 5, 5, 6, 6, 7, 7, 4,
@@ -126,7 +125,7 @@ inline void aabb_draw_wire( const Vector3 points[8] ){
1, 7 // diagonal line (connect mins to maxs corner)
};
#if 1
gl().glVertexPointer( 3, GL_FLOAT, 0, points );
gl().glVertexPointer( 3, GL_FLOAT, 0, points[0].data() );
gl().glDrawElements( GL_LINES, sizeof( indices ) / sizeof( indices[0] ), GL_UNSIGNED_INT, indices );
#else
gl().glBegin( GL_LINES );
@@ -138,7 +137,7 @@ inline void aabb_draw_wire( const Vector3 points[8] ){
#endif
}
inline void aabb_draw_flatshade( const Vector3 points[8] ){
inline void aabb_draw_flatshade( const std::array<Vector3, 8>& points ){
gl().glBegin( GL_QUADS );
gl().glNormal3fv( vector3_to_array( aabb_normals[0] ) );
@@ -181,20 +180,15 @@ inline void aabb_draw_flatshade( const Vector3 points[8] ){
}
inline void aabb_draw_wire( const AABB& aabb ){
Vector3 points[8];
aabb_corners( aabb, points );
aabb_draw_wire( points );
aabb_draw_wire( aabb_corners( aabb ) );
}
inline void aabb_draw_flatshade( const AABB& aabb ){
Vector3 points[8];
aabb_corners( aabb, points );
aabb_draw_flatshade( points );
aabb_draw_flatshade( aabb_corners( aabb ) );
}
inline void aabb_draw_textured( const AABB& aabb ){
Vector3 points[8];
aabb_corners( aabb, points );
const std::array<Vector3, 8> points = aabb_corners( aabb );
gl().glBegin( GL_QUADS );
+40 -33
View File
@@ -26,6 +26,7 @@
#include "math/matrix.h"
#include "math/plane.h"
#include <array>
class AABB
{
@@ -194,32 +195,36 @@ inline unsigned int aabb_oriented_classify_plane( const AABB& aabb, const Matrix
return 0; // totally outside
}
inline void aabb_corners( const AABB& aabb, Vector3 corners[8] ){
Vector3 min( vector3_subtracted( aabb.origin, aabb.extents ) );
Vector3 max( vector3_added( aabb.origin, aabb.extents ) );
corners[0] = Vector3( min[0], max[1], max[2] );
corners[1] = Vector3( max[0], max[1], max[2] );
corners[2] = Vector3( max[0], min[1], max[2] );
corners[3] = Vector3( min[0], min[1], max[2] );
corners[4] = Vector3( min[0], max[1], min[2] );
corners[5] = Vector3( max[0], max[1], min[2] );
corners[6] = Vector3( max[0], min[1], min[2] );
corners[7] = Vector3( min[0], min[1], min[2] );
inline std::array<Vector3, 8> aabb_corners( const AABB& aabb ){
const Vector3 min( vector3_subtracted( aabb.origin, aabb.extents ) );
const Vector3 max( vector3_added( aabb.origin, aabb.extents ) );
return {
Vector3( min[0], max[1], max[2] ),
Vector3( max[0], max[1], max[2] ),
Vector3( max[0], min[1], max[2] ),
Vector3( min[0], min[1], max[2] ),
Vector3( min[0], max[1], min[2] ),
Vector3( max[0], max[1], min[2] ),
Vector3( max[0], min[1], min[2] ),
Vector3( min[0], min[1], min[2] )
};
}
inline void aabb_corners_oriented( const AABB& aabb, const Matrix4& rotation, Vector3 corners[8] ){
Vector3 x = rotation.x().vec3() * aabb.extents.x();
Vector3 y = rotation.y().vec3() * aabb.extents.y();
Vector3 z = rotation.z().vec3() * aabb.extents.z();
inline std::array<Vector3, 8> aabb_corners_oriented( const AABB& aabb, const Matrix4& rotation ){
const Vector3 x = rotation.x().vec3() * aabb.extents.x();
const Vector3 y = rotation.y().vec3() * aabb.extents.y();
const Vector3 z = rotation.z().vec3() * aabb.extents.z();
corners[0] = aabb.origin + -x + y + z;
corners[1] = aabb.origin + x + y + z;
corners[2] = aabb.origin + x + -y + z;
corners[3] = aabb.origin + -x + -y + z;
corners[4] = aabb.origin + -x + y + -z;
corners[5] = aabb.origin + x + y + -z;
corners[6] = aabb.origin + x + -y + -z;
corners[7] = aabb.origin + -x + -y + -z;
return {
aabb.origin - x + y + z,
aabb.origin + x + y + z,
aabb.origin + x - y + z,
aabb.origin - x - y + z,
aabb.origin - x + y - z,
aabb.origin + x + y - z,
aabb.origin + x - y - z,
aabb.origin - x - y - z
};
}
inline void aabb_planes( const AABB& aabb, Plane3 planes[6] ){
@@ -231,17 +236,19 @@ inline void aabb_planes( const AABB& aabb, Plane3 planes[6] ){
planes[5] = Plane3( vector3_negated( g_vector3_axes[2] ), -( aabb.origin[2] - aabb.extents[2] ) );
}
inline void aabb_planes_oriented( const AABB& aabb, const Matrix4& rotation, Plane3 planes[6] ){
double x = vector3_dot( rotation.x().vec3(), aabb.origin );
double y = vector3_dot( rotation.y().vec3(), aabb.origin );
double z = vector3_dot( rotation.z().vec3(), aabb.origin );
inline std::array<Plane3, 6> aabb_planes_oriented( const AABB& aabb, const Matrix4& rotation ){
const double x = vector3_dot( rotation.x().vec3(), aabb.origin );
const double y = vector3_dot( rotation.y().vec3(), aabb.origin );
const double z = vector3_dot( rotation.z().vec3(), aabb.origin );
planes[0] = Plane3( rotation.x().vec3(), x + aabb.extents[0] );
planes[1] = Plane3( -rotation.x().vec3(), -( x - aabb.extents[0] ) );
planes[2] = Plane3( rotation.y().vec3(), y + aabb.extents[1] );
planes[3] = Plane3( -rotation.y().vec3(), -( y - aabb.extents[1] ) );
planes[4] = Plane3( rotation.z().vec3(), z + aabb.extents[2] );
planes[5] = Plane3( -rotation.z().vec3(), -( z - aabb.extents[2] ) );
return {
Plane3( rotation.x().vec3(), x + aabb.extents[0] ),
Plane3( -rotation.x().vec3(), -( x - aabb.extents[0] ) ),
Plane3( rotation.y().vec3(), y + aabb.extents[1] ),
Plane3( -rotation.y().vec3(), -( y - aabb.extents[1] ) ),
Plane3( rotation.z().vec3(), z + aabb.extents[2] ),
Plane3( -rotation.z().vec3(), -( z - aabb.extents[2] ) )
};
}
const Vector3 aabb_normals[6] = {
+1 -2
View File
@@ -326,8 +326,7 @@ private:
void constructNull(){
AABB aabb( Vector3( 0, 0, 0 ), Vector3( 8, 8, 8 ) );
Vector3 points[8];
aabb_corners( aabb, points );
const std::array<Vector3, 8> points = aabb_corners( aabb );
m_vertices.resize( 24 );
+4 -5
View File
@@ -485,7 +485,7 @@ void light_draw_radius_wire( const Vector3& origin, const std::array<float, 3>&
#endif
void light_draw_box_lines( const Vector3& origin, const Vector3 points[8] ){
void light_draw_box_lines( const Vector3& origin, const std::array<Vector3, 8>& points ){
//draw lines from the center of the bbox to the corners
gl().glBegin( GL_LINES );
@@ -933,7 +933,7 @@ class RenderLightRadiiBox : public OpenGLRenderable
{
const Vector3& m_origin;
public:
mutable Vector3 m_points[8];
mutable std::array<Vector3, 8> m_points;
//static Shader* m_state;
RenderLightRadiiBox( const Vector3& origin ) : m_origin( origin ){
@@ -982,8 +982,7 @@ public:
}
void render( RenderStateFlags state ) const {
Matrix4 unproject( matrix4_full_inverse( m_projection ) );
Vector3 points[8];
aabb_corners( AABB( Vector3( 0.5f, 0.5f, 0.5f ), Vector3( 0.5f, 0.5f, 0.5f ) ), points );
std::array<Vector3, 8> points = aabb_corners( AABB( Vector3( 0.5f, 0.5f, 0.5f ), Vector3( 0.5f, 0.5f, 0.5f ) ) );
points[0] = vector4_projected( matrix4_transformed_vector4( unproject, Vector4( points[0], 1 ) ) );
points[1] = vector4_projected( matrix4_transformed_vector4( unproject, Vector4( points[1], 1 ) ) );
points[2] = vector4_projected( matrix4_transformed_vector4( unproject, Vector4( points[2], 1 ) ) );
@@ -1280,7 +1279,7 @@ public:
void updateLightRadiiBox() const {
const Matrix4& rotation = rotation_toMatrix( m_rotation );
aabb_corners( AABB( Vector3( 0, 0, 0 ), m_doom3Radius.m_radiusTransformed ), m_radii_box.m_points );
m_radii_box.m_points = aabb_corners( AABB( Vector3( 0, 0, 0 ), m_doom3Radius.m_radiusTransformed ) );
matrix4_transform_point( rotation, m_radii_box.m_points[0] );
vector3_add( m_radii_box.m_points[0], m_aabb_light.origin );
matrix4_transform_point( rotation, m_radii_box.m_points[1] );
+1 -2
View File
@@ -521,8 +521,7 @@ inline void Model_constructNull( Model& model ){
AABB aabb( Vector3( 0, 0, 0 ), Vector3( 8, 8, 8 ) );
Vector3 points[8];
aabb_corners( aabb, points );
const std::array<Vector3, 8> points = aabb_corners( aabb );
surface.vertices().reserve( 24 );
+1 -2
View File
@@ -264,8 +264,7 @@ private:
void constructNull(){
AABB aabb( Vector3( 0, 0, 0 ), Vector3( 8, 8, 8 ) );
Vector3 points[8];
aabb_corners( aabb, points );
const std::array<Vector3, 8> points = aabb_corners( aabb );
m_vertices.resize( 24 );
+11 -12
View File
@@ -2169,22 +2169,21 @@ Vector3 Camera_getFocusPos( camera_t& camera ){
const Vector3 viewvector( -camera.vpn );
#endif
Plane3 frustumPlanes[4];
frustumPlanes[0] = plane3_translated( view.getFrustum().left, camorigin - aabb.origin );
frustumPlanes[1] = plane3_translated( view.getFrustum().right, camorigin - aabb.origin );
frustumPlanes[2] = plane3_translated( view.getFrustum().top, camorigin - aabb.origin );
frustumPlanes[3] = plane3_translated( view.getFrustum().bottom, camorigin - aabb.origin );
const Plane3 frustumPlanes[4] = {
plane3_translated( view.getFrustum().left, camorigin - aabb.origin ),
plane3_translated( view.getFrustum().right, camorigin - aabb.origin ),
plane3_translated( view.getFrustum().top, camorigin - aabb.origin ),
plane3_translated( view.getFrustum().bottom, camorigin - aabb.origin ),
};
float offset = 64.0f;
Vector3 corners[8];
aabb_corners( aabb, corners );
const std::array<Vector3, 8> corners = aabb_corners( aabb );
for ( std::size_t i = 0; i < 4; ++i ){
for ( std::size_t j = 0; j < 8; ++j ){
for ( const Plane3& plane : frustumPlanes ){
for ( const Vector3& corner : corners ){
const Ray ray( aabb.origin, -viewvector );
//Plane3 newplane( frustumPlanes[i].normal(), vector3_dot( frustumPlanes[i].normal(), corners[j] - frustumPlanes[i].normal() * 16.0f ) );
const Plane3 newplane( frustumPlanes[i].normal(), vector3_dot( frustumPlanes[i].normal(), corners[j] ) );
//Plane3 newplane( plane.normal(), vector3_dot( plane.normal(), corner - plane.normal() * 16.0f ) );
const Plane3 newplane( plane.normal(), vector3_dot( plane.normal(), corner ) );
const float d = vector3_dot( ray.direction, newplane.normal() );
if( d != 0 ){
const float s = vector3_dot( newplane.normal() * newplane.dist() - ray.origin, newplane.normal() ) / d;
+2 -4
View File
@@ -1527,8 +1527,7 @@ void AABB_BestPoint( const Matrix4& local2view, clipcull_t cull, const AABB& aab
7, 6, 5, 4,
};
Vector3 points[8];
aabb_corners( aabb, points );
const std::array<Vector3, 8> points = aabb_corners( aabb );
const IndexPointer indices( indices_, 24 );
@@ -2843,8 +2842,7 @@ public:
if( selector.failed() ){
const Matrix4 screen2world( matrix4_full_inverse( view.GetViewMatrix() ) );
Vector3 corners[8];
aabb_corners( m_bounds_draw, corners );
const std::array<Vector3, 8> corners = aabb_corners( m_bounds_draw );
const int indices[24] = {
3, 7, 4, 0, //-x