| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#if !defined( INCLUDED_VIEW_H ) |
| 23 |
#define INCLUDED_VIEW_H |
| 24 |
|
| 25 |
#include "globaldefs.h" |
| 26 |
#include "cullable.h" |
| 27 |
#include "math/frustum.h" |
| 28 |
|
| 29 |
|
| 30 |
#if GDEF_DEBUG |
| 31 |
#define DEBUG_CULLING |
| 32 |
#endif |
| 33 |
|
| 34 |
|
| 35 |
#if defined( DEBUG_CULLING ) |
| 36 |
|
| 37 |
extern int g_count_dots; |
| 38 |
extern int g_count_planes; |
| 39 |
extern int g_count_oriented_planes; |
| 40 |
extern int g_count_bboxs; |
| 41 |
extern int g_count_oriented_bboxs; |
| 42 |
|
| 43 |
#endif |
| 44 |
|
| 45 |
inline void debug_count_dot(){ |
| 46 |
#if defined( DEBUG_CULLING ) |
| 47 |
++g_count_dots; |
| 48 |
#endif |
| 49 |
} |
| 50 |
|
| 51 |
inline void debug_count_plane(){ |
| 52 |
#if defined( DEBUG_CULLING ) |
| 53 |
++g_count_planes; |
| 54 |
#endif |
| 55 |
} |
| 56 |
|
| 57 |
inline void debug_count_oriented_plane(){ |
| 58 |
#if defined( DEBUG_CULLING ) |
| 59 |
++g_count_oriented_planes; |
| 60 |
#endif |
| 61 |
} |
| 62 |
|
| 63 |
inline void debug_count_bbox(){ |
| 64 |
#if defined( DEBUG_CULLING ) |
| 65 |
++g_count_bboxs; |
| 66 |
#endif |
| 67 |
} |
| 68 |
|
| 69 |
inline void debug_count_oriented_bbox(){ |
| 70 |
#if defined( DEBUG_CULLING ) |
| 71 |
++g_count_oriented_bboxs; |
| 72 |
#endif |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
class View : public VolumeTest |
| 80 |
{ |
| 81 |
|
| 82 |
Matrix4 m_modelview; |
| 83 |
|
| 84 |
Matrix4 m_projection; |
| 85 |
|
| 86 |
Matrix4 m_viewport; |
| 87 |
|
| 88 |
Matrix4 m_scissor; |
| 89 |
|
| 90 |
|
| 91 |
Matrix4 m_viewproj; |
| 92 |
|
| 93 |
Vector4 m_viewer; |
| 94 |
|
| 95 |
Frustum m_frustum; |
| 96 |
|
| 97 |
bool m_fill; |
| 98 |
|
| 99 |
void construct(){ |
| 100 |
m_viewproj = matrix4_multiplied_by_matrix4( matrix4_multiplied_by_matrix4( m_scissor, m_projection ), m_modelview ); |
| 101 |
|
| 102 |
m_frustum = frustum_from_viewproj( m_viewproj ); |
| 103 |
m_viewer = viewer_from_viewproj( m_viewproj ); |
| 104 |
} |
| 105 |
public: |
| 106 |
View( bool fill = false ) : |
| 107 |
m_modelview( g_matrix4_identity ), |
| 108 |
m_projection( g_matrix4_identity ), |
| 109 |
m_scissor( g_matrix4_identity ), |
| 110 |
m_fill( fill ){ |
| 111 |
} |
| 112 |
void Construct( const Matrix4& projection, const Matrix4& modelview, std::size_t width, std::size_t height ){ |
| 113 |
|
| 114 |
m_modelview = modelview; |
| 115 |
|
| 116 |
|
| 117 |
m_projection = projection; |
| 118 |
|
| 119 |
|
| 120 |
m_viewport = g_matrix4_identity; |
| 121 |
m_viewport[0] = float(width / 2); |
| 122 |
m_viewport[5] = float(height / 2); |
| 123 |
if ( fabs( m_projection[11] ) > 0.0000001 ) { |
| 124 |
m_viewport[10] = m_projection[0] * m_viewport[0]; |
| 125 |
} |
| 126 |
else{ |
| 127 |
m_viewport[10] = 1 / m_projection[10]; |
| 128 |
} |
| 129 |
|
| 130 |
construct(); |
| 131 |
} |
| 132 |
void EnableScissor( float min_x, float max_x, float min_y, float max_y ){ |
| 133 |
m_scissor = g_matrix4_identity; |
| 134 |
m_scissor[0] = static_cast<float>( ( max_x - min_x ) * 0.5 ); |
| 135 |
m_scissor[5] = static_cast<float>( ( max_y - min_y ) * 0.5 ); |
| 136 |
m_scissor[12] = static_cast<float>( ( min_x + max_x ) * 0.5 ); |
| 137 |
m_scissor[13] = static_cast<float>( ( min_y + max_y ) * 0.5 ); |
| 138 |
matrix4_full_invert( m_scissor ); |
| 139 |
|
| 140 |
construct(); |
| 141 |
} |
| 142 |
void DisableScissor(){ |
| 143 |
m_scissor = g_matrix4_identity; |
| 144 |
|
| 145 |
construct(); |
| 146 |
} |
| 147 |
|
| 148 |
bool TestPoint( const Vector3& point ) const { |
| 149 |
return viewproj_test_point( m_viewproj, point ); |
| 150 |
} |
| 151 |
bool TestLine( const Segment& segment ) const { |
| 152 |
return frustum_test_line( m_frustum, segment ); |
| 153 |
} |
| 154 |
bool TestPlane( const Plane3& plane ) const { |
| 155 |
debug_count_plane(); |
| 156 |
return viewer_test_plane( m_viewer, plane ); |
| 157 |
} |
| 158 |
bool TestPlane( const Plane3& plane, const Matrix4& localToWorld ) const { |
| 159 |
debug_count_oriented_plane(); |
| 160 |
return viewer_test_transformed_plane( m_viewer, plane, localToWorld ); |
| 161 |
} |
| 162 |
VolumeIntersectionValue TestAABB( const AABB& aabb ) const { |
| 163 |
debug_count_bbox(); |
| 164 |
return frustum_test_aabb( m_frustum, aabb ); |
| 165 |
} |
| 166 |
VolumeIntersectionValue TestAABB( const AABB& aabb, const Matrix4& localToWorld ) const { |
| 167 |
debug_count_oriented_bbox(); |
| 168 |
return frustum_intersects_transformed_aabb( m_frustum, aabb, localToWorld ); |
| 169 |
} |
| 170 |
|
| 171 |
const Matrix4& GetViewMatrix() const { |
| 172 |
return m_viewproj; |
| 173 |
} |
| 174 |
const Matrix4& GetViewport() const { |
| 175 |
return m_viewport; |
| 176 |
}; |
| 177 |
const Matrix4& GetModelview() const { |
| 178 |
return m_modelview; |
| 179 |
} |
| 180 |
const Matrix4& GetProjection() const { |
| 181 |
return m_projection; |
| 182 |
} |
| 183 |
|
| 184 |
bool fill() const { |
| 185 |
return m_fill; |
| 186 |
} |
| 187 |
const Vector3& getViewer() const { |
| 188 |
return vector4_to_vector3( m_viewer ); |
| 189 |
} |
| 190 |
const Frustum& getFrustum() const { |
| 191 |
return m_frustum; |
| 192 |
} |
| 193 |
}; |
| 194 |
|
| 195 |
#endif |