view.h

Go to the documentation of this file.
00001 /*
00002  Copyright (C) 2001-2006, William Joseph.
00003  All Rights Reserved.
00004 
00005  This file is part of GtkRadiant.
00006 
00007  GtkRadiant is free software; you can redistribute it and/or modify
00008  it under the terms of the GNU General Public License as published by
00009  the Free Software Foundation; either version 2 of the License, or
00010  (at your option) any later version.
00011 
00012  GtkRadiant is distributed in the hope that it will be useful,
00013  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  GNU General Public License for more details.
00016 
00017  You should have received a copy of the GNU General Public License
00018  along with GtkRadiant; if not, write to the Free Software
00019  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020  */
00021 
00022 #if !defined(INCLUDED_VIEW_H)
00023 #define INCLUDED_VIEW_H
00024 
00025 #include "cullable.h"
00026 #include "math/frustum.h"
00027 
00028 extern int g_count_dots;
00029 extern int g_count_planes;
00030 extern int g_count_oriented_planes;
00031 extern int g_count_bboxs;
00032 extern int g_count_oriented_bboxs;
00033 
00034 static inline void debug_count_dot ()
00035 {
00036     ++g_count_dots;
00037 }
00038 
00039 static inline void debug_count_plane ()
00040 {
00041     ++g_count_planes;
00042 }
00043 
00044 static inline void debug_count_oriented_plane ()
00045 {
00046     ++g_count_oriented_planes;
00047 }
00048 
00049 static inline void debug_count_bbox ()
00050 {
00051     ++g_count_bboxs;
00052 }
00053 
00054 static inline void debug_count_oriented_bbox ()
00055 {
00056     ++g_count_oriented_bboxs;
00057 }
00058 
00060 class View : public VolumeTest {
00062     Matrix4 m_modelview;
00064     Matrix4 m_projection;
00066     Matrix4 m_viewport;
00067 
00068     Matrix4 m_scissor;
00069 
00071     Matrix4 m_viewproj;
00073     Vector4 m_viewer;
00075     Frustum m_frustum;
00076 
00077     bool m_fill;
00078 
00079     void construct() {
00080         m_viewproj = matrix4_multiplied_by_matrix4(matrix4_multiplied_by_matrix4(m_scissor, m_projection), m_modelview);
00081 
00082         m_frustum = frustum_from_viewproj(m_viewproj);
00083         m_viewer = viewer_from_viewproj(m_viewproj);
00084     }
00085     public:
00086     View(bool fill = false) :
00087     m_modelview(g_matrix4_identity),
00088     m_projection(g_matrix4_identity),
00089     m_scissor(g_matrix4_identity),
00090     m_fill(fill) {
00091     }
00092     void Construct(const Matrix4& projection, const Matrix4& modelview, std::size_t width, std::size_t height) {
00093         // modelview
00094         m_modelview = modelview;
00095 
00096         // projection
00097         m_projection = projection;
00098 
00099         // viewport
00100         m_viewport = g_matrix4_identity;
00101         m_viewport[0] = float(width / 2);
00102         m_viewport[5] = float(height / 2);
00103         if (fabs(m_projection[11])> 0.0000001)
00104         m_viewport[10] = m_projection[0] * m_viewport[0];
00105         else
00106         m_viewport[10] = 1 / m_projection[10];
00107 
00108         construct();
00109     }
00110     void EnableScissor(float min_x, float max_x, float min_y, float max_y) {
00111         m_scissor = g_matrix4_identity;
00112         m_scissor[0] = static_cast<float>((max_x - min_x) * 0.5);
00113         m_scissor[5] = static_cast<float>((max_y - min_y) * 0.5);
00114         m_scissor[12] = static_cast<float>((min_x + max_x) * 0.5);
00115         m_scissor[13] = static_cast<float>((min_y + max_y) * 0.5);
00116         matrix4_full_invert(m_scissor);
00117 
00118         construct();
00119     }
00120     void DisableScissor() {
00121         m_scissor = g_matrix4_identity;
00122 
00123         construct();
00124     }
00125 
00126     bool TestPoint(const Vector3& point) const {
00127         return viewproj_test_point(m_viewproj, point);
00128     }
00129     bool TestLine(const Segment& segment) const {
00130         return frustum_test_line(m_frustum, segment);
00131     }
00132     bool TestPlane(const Plane3& plane) const {
00133         debug_count_plane();
00134         return viewer_test_plane(m_viewer, plane);
00135     }
00136     bool TestPlane(const Plane3& plane, const Matrix4& localToWorld) const {
00137         debug_count_oriented_plane();
00138         return viewer_test_transformed_plane(m_viewer, plane, localToWorld);
00139     }
00140     VolumeIntersectionValue TestAABB(const AABB& aabb) const {
00141         debug_count_bbox();
00142         return frustum_test_aabb(m_frustum, aabb);
00143     }
00144     VolumeIntersectionValue TestAABB(const AABB& aabb, const Matrix4& localToWorld) const {
00145         debug_count_oriented_bbox();
00146         return frustum_intersects_transformed_aabb(m_frustum, aabb, localToWorld);
00147     }
00148 
00149     const Matrix4& GetViewMatrix() const {
00150         return m_viewproj;
00151     }
00152     const Matrix4& GetViewport() const {
00153         return m_viewport;
00154     }
00155     const Matrix4& GetModelview() const {
00156         return m_modelview;
00157     }
00158     const Matrix4& GetProjection() const {
00159         return m_projection;
00160     }
00161 
00162     bool fill() const {
00163         return m_fill;
00164     }
00165     const Vector3& getViewer() const {
00166         return m_viewer.getVector3();
00167     }
00168 };
00169 
00170 #endif

Generated by  doxygen 1.6.2