| 1 | /* |
|---|
| 2 | ================================================================================= |
|---|
| 3 | This file is part of Cafu, the open-source game engine and graphics engine |
|---|
| 4 | for multiplayer, cross-platform, real-time 3D action. |
|---|
| 5 | Copyright (C) 2002-2012 Carsten Fuchs Software. |
|---|
| 6 | |
|---|
| 7 | Cafu is free software: you can redistribute it and/or modify it under the terms |
|---|
| 8 | of the GNU General Public License as published by the Free Software Foundation, |
|---|
| 9 | either version 3 of the License, or (at your option) any later version. |
|---|
| 10 | |
|---|
| 11 | Cafu is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
|---|
| 12 | without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
|---|
| 13 | PURPOSE. See the GNU General Public License for more details. |
|---|
| 14 | |
|---|
| 15 | You should have received a copy of the GNU General Public License |
|---|
| 16 | along with Cafu. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 17 | |
|---|
| 18 | For support and more information about Cafu, visit us at <http://www.cafu.de>. |
|---|
| 19 | ================================================================================= |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | #ifndef CAFU_RENDERER_3D_HPP_INCLUDED |
|---|
| 23 | #define CAFU_RENDERER_3D_HPP_INCLUDED |
|---|
| 24 | |
|---|
| 25 | #include "ChildFrameViewWin.hpp" |
|---|
| 26 | #include "OrthoBspTree.hpp" |
|---|
| 27 | #include "Math3D/Matrix3x3.hpp" |
|---|
| 28 | #include "Math3D/Plane3.hpp" |
|---|
| 29 | #include "Templates/Array.hpp" |
|---|
| 30 | |
|---|
| 31 | #include "wx/gdicmn.h" |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | class MapElementT; |
|---|
| 35 | class MapDocumentT; |
|---|
| 36 | class ToolT; |
|---|
| 37 | class ViewWindow3DT; |
|---|
| 38 | namespace MatSys { class RenderMaterialT; } |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | /// This class provides auxiliary means for rendering a 3D view. |
|---|
| 42 | /// A 3D view is essentially rendered by calling the MapElementT::Render3D() method of all relevant (visible) MapElementTs |
|---|
| 43 | /// within the document, see the ViewWindow3DT::OnPaint() method for details. The map elements can render themselves either |
|---|
| 44 | /// directly by means of the Cafu MatSys, or by calls to the auxiliary functions in this class. |
|---|
| 45 | class Renderer3DT |
|---|
| 46 | { |
|---|
| 47 | public: |
|---|
| 48 | |
|---|
| 49 | /// A helper class that temporarily sets up the matrices in the Cafu MatSys for orthogonal rendering into the given 3D view window. |
|---|
| 50 | /// Just create an instance of this class on the stack, and within the current scope, the orthogonal mode will be active. |
|---|
| 51 | /// It works by setting the orthogonal matrices in the constructor and restoring the original matrices in the destructor. |
|---|
| 52 | class UseOrthoMatricesT |
|---|
| 53 | { |
|---|
| 54 | public: |
|---|
| 55 | |
|---|
| 56 | /// The constructor. |
|---|
| 57 | UseOrthoMatricesT(const wxWindow& Window); |
|---|
| 58 | |
|---|
| 59 | /// The destructor. |
|---|
| 60 | ~UseOrthoMatricesT(); |
|---|
| 61 | }; |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | /// The constructor. |
|---|
| 65 | Renderer3DT(ViewWindow3DT& ViewWin3D); |
|---|
| 66 | |
|---|
| 67 | /// The destructor. |
|---|
| 68 | ~Renderer3DT(); |
|---|
| 69 | |
|---|
| 70 | /// Initializes the rendering of a new frame by computing and caching all relevant data. |
|---|
| 71 | void InitFrame(); |
|---|
| 72 | |
|---|
| 73 | // Methods for getting the renderer state. |
|---|
| 74 | const ViewWindow3DT& GetViewWin3D() const { return m_ViewWin3D; } |
|---|
| 75 | const Plane3fT* GetViewFrustumPlanes() const { return m_FrustumPlanesCache; } |
|---|
| 76 | const ArrayT<MapElementT*>& GetVisElemsBackToFront() const { return m_VisElemsBackToFront; } |
|---|
| 77 | float GetConstShade(const Vector3T<float>& Normal) const; |
|---|
| 78 | |
|---|
| 79 | // Materials query methods. |
|---|
| 80 | MatSys::RenderMaterialT* GetRMatWireframe() const { return m_RMatWireframe; } |
|---|
| 81 | MatSys::RenderMaterialT* GetRMatWireframe_OffsetZ() const { return m_RMatWireframeOZ; } |
|---|
| 82 | MatSys::RenderMaterialT* GetRMatFlatShaded() const { return m_RMatFlatShaded; } |
|---|
| 83 | MatSys::RenderMaterialT* GetRMatFlatShaded_OffsetZ() const { return m_RMatFlatShadedOZ; } |
|---|
| 84 | MatSys::RenderMaterialT* GetRMatOverlay() const { return m_RMatOverlay; } |
|---|
| 85 | MatSys::RenderMaterialT* GetRMatOverlay_OffsetZ() const { return m_RMatOverlayOZ; } |
|---|
| 86 | MatSys::RenderMaterialT* GetRMatTerrainEditorTool() const { return m_RMatTerrainEdit; } |
|---|
| 87 | MatSys::RenderMaterialT* GetRMatTerrainEyeDropper() const { return m_RMatTerrainEyeDropper; } |
|---|
| 88 | |
|---|
| 89 | /// Renders a box from the given bounding-box in the given color, with solid faces or in wireframe. |
|---|
| 90 | void RenderBox(const BoundingBox3fT& BB, const wxColour& Color, bool Solid) const; |
|---|
| 91 | |
|---|
| 92 | /// Renders a box from the given eight vertices in the given color, with solid faces or in wireframe. |
|---|
| 93 | /// The vertices are expected in the same order as given by the BoundingBox3T<T>::GetCornerVertices() method, |
|---|
| 94 | /// and the box can be arbitrarily trans- or even deformed. |
|---|
| 95 | void RenderBox(const Vector3fT Vertices[], const wxColour& Color, bool Solid) const; |
|---|
| 96 | |
|---|
| 97 | /// Renders a line from A to B in the given color. |
|---|
| 98 | void RenderLine(const Vector3fT& A, const Vector3fT& B, const wxColour& Color) const; |
|---|
| 99 | |
|---|
| 100 | /// Renders the split planes of the BSP tree at and below the given node, up to the given depth. |
|---|
| 101 | void RenderSplitPlanes(const OrthoBspTreeT::NodeT* Node, int Depth) const; |
|---|
| 102 | |
|---|
| 103 | /// Renders the basis vectors (the "axes") of the given matrix at the given position with the given length. |
|---|
| 104 | void BasisVectors(const Vector3fT& Pos, const cf::math::Matrix3x3fT& Mat, float Length=100.0f) const; |
|---|
| 105 | |
|---|
| 106 | /// Renders a cross-hair at the given point. Assumes that orthogonal rendering mode is active. |
|---|
| 107 | void RenderCrossHair(const wxPoint& Center) const; |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | private: |
|---|
| 111 | |
|---|
| 112 | /// An enumeration of locations of a bounding-box in relation to the view frustum. |
|---|
| 113 | enum RelLocT |
|---|
| 114 | { |
|---|
| 115 | COMPL_OUTSIDE, |
|---|
| 116 | COMPL_INSIDE, |
|---|
| 117 | INTERSECTS |
|---|
| 118 | }; |
|---|
| 119 | |
|---|
| 120 | Renderer3DT(const Renderer3DT&); ///< Use of the Copy Constructor is not allowed. |
|---|
| 121 | void operator = (const Renderer3DT&); ///< Use of the Assignment Operator is not allowed. |
|---|
| 122 | |
|---|
| 123 | RelLocT RelFrustum(const BoundingBox3fT& BB) const; |
|---|
| 124 | void GetRenderList(const OrthoBspTreeT::NodeT* Node, RelLocT ParentLoc); |
|---|
| 125 | |
|---|
| 126 | ViewWindow3DT& m_ViewWin3D; ///< The 3D view window that owns this renderer / that this renderer is assigned to. |
|---|
| 127 | const ToolT* m_ActiveToolCache; ///< Caches the active tool pointer during a call to InitFrame(). NULL at all other times. |
|---|
| 128 | Plane3fT m_FrustumPlanesCache[6]; ///< Caches the six planes that define the current view frustum for the current frame. |
|---|
| 129 | ArrayT<MapElementT*> m_VisElemsBackToFront; ///< Used during rendering, the back-to-front ordered list of map elements that are in the view frustum and visible is build and kept here. |
|---|
| 130 | |
|---|
| 131 | MatSys::RenderMaterialT* m_RMatWireframe; ///< The render material for wire-frame rendering. |
|---|
| 132 | MatSys::RenderMaterialT* m_RMatWireframeOZ; ///< The render material for wire-frame rendering (with polygon z-offset, e.g. for outlines). |
|---|
| 133 | MatSys::RenderMaterialT* m_RMatFlatShaded; ///< The render material for flat shaded (single solid color) rendering. |
|---|
| 134 | MatSys::RenderMaterialT* m_RMatFlatShadedOZ; ///< The render material for flat shaded (single solid color) rendering (with polygon z-offset, e.g. for decals). |
|---|
| 135 | MatSys::RenderMaterialT* m_RMatOverlay; ///< The render material for selection overlays (added in a second pass). |
|---|
| 136 | MatSys::RenderMaterialT* m_RMatOverlayOZ; ///< The render material for selection overlays (added in a second pass) (with polygon z-offset, e.g. for decals). |
|---|
| 137 | MatSys::RenderMaterialT* m_RMatTerrainEdit; ///< The render material overlay that is used to render the tool position in a terrain if the terrain edit tool is active. |
|---|
| 138 | MatSys::RenderMaterialT* m_RMatTerrainEyeDropper; ///< The Render material overlay that is used to render the eyedropper tool position on a terrain. |
|---|
| 139 | }; |
|---|
| 140 | |
|---|
| 141 | #endif |
|---|