]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/view.h
New Apple OS X application bundle based on work done for GtkRadiant 1.6.4. Produces...
[xonotic/netradiant.git] / radiant / view.h
1 /*
2    Copyright (C) 2001-2006, William Joseph.
3    All Rights Reserved.
4
5    This file is part of GtkRadiant.
6
7    GtkRadiant is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    GtkRadiant is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GtkRadiant; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22 #if !defined( INCLUDED_VIEW_H )
23 #define INCLUDED_VIEW_H
24
25 #include "cullable.h"
26 #include "math/frustum.h"
27
28
29 #if defined( _DEBUG )
30 #define DEBUG_CULLING
31 #endif
32
33
34 #if defined( DEBUG_CULLING )
35
36 extern int g_count_dots;
37 extern int g_count_planes;
38 extern int g_count_oriented_planes;
39 extern int g_count_bboxs;
40 extern int g_count_oriented_bboxs;
41
42 #endif
43
44 inline void debug_count_dot(){
45 #if defined( DEBUG_CULLING )
46         ++g_count_dots;
47 #endif
48 }
49
50 inline void debug_count_plane(){
51 #if defined( DEBUG_CULLING )
52         ++g_count_planes;
53 #endif
54 }
55
56 inline void debug_count_oriented_plane(){
57 #if defined( DEBUG_CULLING )
58         ++g_count_oriented_planes;
59 #endif
60 }
61
62 inline void debug_count_bbox(){
63 #if defined( DEBUG_CULLING )
64         ++g_count_bboxs;
65 #endif
66 }
67
68 inline void debug_count_oriented_bbox(){
69 #if defined( DEBUG_CULLING )
70         ++g_count_oriented_bboxs;
71 #endif
72 }
73
74
75
76
77 /// \brief View-volume culling and transformations.
78 class View : public VolumeTest
79 {
80 /// modelview matrix
81 Matrix4 m_modelview;
82 /// projection matrix
83 Matrix4 m_projection;
84 /// device-to-screen transform
85 Matrix4 m_viewport;
86
87 Matrix4 m_scissor;
88
89 /// combined modelview and projection matrix
90 Matrix4 m_viewproj;
91 /// camera position in world space
92 Vector4 m_viewer;
93 /// view frustum in world space
94 Frustum m_frustum;
95
96 bool m_fill;
97
98 void construct(){
99         m_viewproj = matrix4_multiplied_by_matrix4( matrix4_multiplied_by_matrix4( m_scissor, m_projection ), m_modelview );
100
101         m_frustum = frustum_from_viewproj( m_viewproj );
102         m_viewer = viewer_from_viewproj( m_viewproj );
103 }
104 public:
105 View( bool fill = false ) :
106         m_modelview( g_matrix4_identity ),
107         m_projection( g_matrix4_identity ),
108         m_scissor( g_matrix4_identity ),
109         m_fill( fill ){
110 }
111 void Construct( const Matrix4& projection, const Matrix4& modelview, std::size_t width, std::size_t height ){
112         // modelview
113         m_modelview = modelview;
114
115         // projection
116         m_projection = projection;
117
118         // viewport
119         m_viewport = g_matrix4_identity;
120         m_viewport[0] = float(width / 2);
121         m_viewport[5] = float(height / 2);
122         if ( fabs( m_projection[11] ) > 0.0000001 ) {
123                 m_viewport[10] = m_projection[0] * m_viewport[0];
124         }
125         else{
126                 m_viewport[10] = 1 / m_projection[10];
127         }
128
129         construct();
130 }
131 void EnableScissor( float min_x, float max_x, float min_y, float max_y ){
132         m_scissor = g_matrix4_identity;
133         m_scissor[0] = static_cast<float>( ( max_x - min_x ) * 0.5 );
134         m_scissor[5] = static_cast<float>( ( max_y - min_y ) * 0.5 );
135         m_scissor[12] = static_cast<float>( ( min_x + max_x ) * 0.5 );
136         m_scissor[13] = static_cast<float>( ( min_y + max_y ) * 0.5 );
137         matrix4_full_invert( m_scissor );
138
139         construct();
140 }
141 void DisableScissor(){
142         m_scissor = g_matrix4_identity;
143
144         construct();
145 }
146
147 bool TestPoint( const Vector3& point ) const {
148         return viewproj_test_point( m_viewproj, point );
149 }
150 bool TestLine( const Segment& segment ) const {
151         return frustum_test_line( m_frustum, segment );
152 }
153 bool TestPlane( const Plane3& plane ) const {
154         debug_count_plane();
155         return viewer_test_plane( m_viewer, plane );
156 }
157 bool TestPlane( const Plane3& plane, const Matrix4& localToWorld ) const {
158         debug_count_oriented_plane();
159         return viewer_test_transformed_plane( m_viewer, plane, localToWorld );
160 }
161 VolumeIntersectionValue TestAABB( const AABB& aabb ) const {
162         debug_count_bbox();
163         return frustum_test_aabb( m_frustum, aabb );
164 }
165 VolumeIntersectionValue TestAABB( const AABB& aabb, const Matrix4& localToWorld ) const {
166         debug_count_oriented_bbox();
167         return frustum_intersects_transformed_aabb( m_frustum, aabb, localToWorld );
168 }
169
170 const Matrix4& GetViewMatrix() const {
171         return m_viewproj;
172 }
173 const Matrix4& GetViewport() const {
174         return m_viewport;
175 };
176 const Matrix4& GetModelview() const {
177         return m_modelview;
178 }
179 const Matrix4& GetProjection() const {
180         return m_projection;
181 }
182
183 bool fill() const {
184         return m_fill;
185 }
186 const Vector3& getViewer() const {
187         return vector4_to_vector3( m_viewer );
188 }
189 };
190
191 #endif