Cafu Engine
Renderer3D.hpp
1 /*
2 Cafu Engine, http://www.cafu.de/
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
5 */
6 
7 #ifndef CAFU_RENDERER_3D_HPP_INCLUDED
8 #define CAFU_RENDERER_3D_HPP_INCLUDED
9 
10 #include "ChildFrameViewWin.hpp"
11 #include "OrthoBspTree.hpp"
12 #include "Math3D/Matrix3x3.hpp"
13 #include "Math3D/Plane3.hpp"
14 #include "Templates/Array.hpp"
15 
16 #include "wx/gdicmn.h"
17 
18 
19 class MapElementT;
20 class MapDocumentT;
21 class ToolT;
22 class ViewWindow3DT;
23 namespace MatSys { class RenderMaterialT; }
24 
25 
26 /// This class provides auxiliary means for rendering a 3D view.
27 /// A 3D view is essentially rendered by calling the MapElementT::Render3D() method of all relevant (visible) MapElementTs
28 /// within the document, see the ViewWindow3DT::OnPaint() method for details. The map elements can render themselves either
29 /// directly by means of the Cafu MatSys, or by calls to the auxiliary functions in this class.
31 {
32  public:
33 
34  /// A helper class that temporarily sets up the matrices in the Cafu MatSys for orthogonal rendering into the given 3D view window.
35  /// Just create an instance of this class on the stack, and within the current scope, the orthogonal mode will be active.
36  /// It works by setting the orthogonal matrices in the constructor and restoring the original matrices in the destructor.
38  {
39  public:
40 
41  /// The constructor.
42  UseOrthoMatricesT(const wxWindow& Window);
43 
44  /// The destructor.
46  };
47 
48 
49  /// The constructor.
50  Renderer3DT(ViewWindow3DT& ViewWin3D);
51 
52  /// The destructor.
53  ~Renderer3DT();
54 
55  /// Initializes the rendering of a new frame by computing and caching all relevant data.
56  void InitFrame();
57 
58  // Methods for getting the renderer state.
59  const ViewWindow3DT& GetViewWin3D() const { return m_ViewWin3D; }
60  const Plane3fT* GetViewFrustumPlanes() const { return m_FrustumPlanesCache; }
61  const ArrayT<MapElementT*>& GetVisElemsBackToFront() const { return m_VisElemsBackToFront; }
62  float GetConstShade(const Vector3T<float>& Normal) const;
63 
64  // Materials query methods.
65  MatSys::RenderMaterialT* GetRMatWireframe() const { return m_RMatWireframe; }
66  MatSys::RenderMaterialT* GetRMatWireframe_OffsetZ() const { return m_RMatWireframeOZ; }
67  MatSys::RenderMaterialT* GetRMatFlatShaded() const { return m_RMatFlatShaded; }
68  MatSys::RenderMaterialT* GetRMatFlatShaded_OffsetZ() const { return m_RMatFlatShadedOZ; }
69  MatSys::RenderMaterialT* GetRMatOverlay() const { return m_RMatOverlay; }
70  MatSys::RenderMaterialT* GetRMatOverlay_OffsetZ() const { return m_RMatOverlayOZ; }
71  MatSys::RenderMaterialT* GetRMatTerrainEditorTool() const { return m_RMatTerrainEdit; }
72  MatSys::RenderMaterialT* GetRMatTerrainEyeDropper() const { return m_RMatTerrainEyeDropper; }
73 
74  /// Renders a box from the given bounding-box in the given color, with solid faces or in wireframe.
75  void RenderBox(const BoundingBox3fT& BB, const wxColour& Color, bool Solid) const;
76 
77  /// Renders a box from the given eight vertices in the given color, with solid faces or in wireframe.
78  /// The vertices are expected in the same order as given by the BoundingBox3T<T>::GetCornerVertices() method,
79  /// and the box can be arbitrarily trans- or even deformed.
80  void RenderBox(const Vector3fT Vertices[], const wxColour& Color, bool Solid) const;
81 
82  /// Renders a line from A to B in the given color.
83  void RenderLine(const Vector3fT& A, const Vector3fT& B, const wxColour& Color) const;
84 
85  /// Renders the split planes of the BSP tree at and below the given node, up to the given depth.
86  void RenderSplitPlanes(const OrthoBspTreeT::NodeT* Node, int Depth) const;
87 
88  /// Renders the basis vectors (the "axes") of the given matrix at the given position with the given length.
89  void BasisVectors(const Vector3fT& Pos, const cf::math::Matrix3x3fT& Mat, float Length=100.0f) const;
90 
91  /// Renders a cross-hair at the given point. Assumes that orthogonal rendering mode is active.
92  void RenderCrossHair(const wxPoint& Center) const;
93 
94 
95  private:
96 
97  /// An enumeration of locations of a bounding-box in relation to the view frustum.
98  enum RelLocT
99  {
100  COMPL_OUTSIDE,
101  COMPL_INSIDE,
102  INTERSECTS
103  };
104 
105  Renderer3DT(const Renderer3DT&); ///< Use of the Copy Constructor is not allowed.
106  void operator = (const Renderer3DT&); ///< Use of the Assignment Operator is not allowed.
107 
108  RelLocT RelFrustum(const BoundingBox3fT& BB) const;
109  void GetRenderList(const OrthoBspTreeT::NodeT* Node, RelLocT ParentLoc);
110 
111  ViewWindow3DT& m_ViewWin3D; ///< The 3D view window that owns this renderer / that this renderer is assigned to.
112  const ToolT* m_ActiveToolCache; ///< Caches the active tool pointer during a call to InitFrame(). NULL at all other times.
113  Plane3fT m_FrustumPlanesCache[6]; ///< Caches the six planes that define the current view frustum for the current frame.
114  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.
115 
116  MatSys::RenderMaterialT* m_RMatWireframe; ///< The render material for wire-frame rendering.
117  MatSys::RenderMaterialT* m_RMatWireframeOZ; ///< The render material for wire-frame rendering (with polygon z-offset, e.g. for outlines).
118  MatSys::RenderMaterialT* m_RMatFlatShaded; ///< The render material for flat shaded (single solid color) rendering.
119  MatSys::RenderMaterialT* m_RMatFlatShadedOZ; ///< The render material for flat shaded (single solid color) rendering (with polygon z-offset, e.g. for decals).
120  MatSys::RenderMaterialT* m_RMatOverlay; ///< The render material for selection overlays (added in a second pass).
121  MatSys::RenderMaterialT* m_RMatOverlayOZ; ///< The render material for selection overlays (added in a second pass) (with polygon z-offset, e.g. for decals).
122  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.
123  MatSys::RenderMaterialT* m_RMatTerrainEyeDropper; ///< The Render material overlay that is used to render the eyedropper tool position on a terrain.
124 };
125 
126 #endif
This class provides auxiliary means for rendering a 3D view.
Definition: Renderer3D.hpp:30
~Renderer3DT()
The destructor.
Definition: Renderer3D.cpp:77
This class represents a surface render material.
Definition: RenderMaterial.hpp:25
This class represents a CaWE "map" document.
Definition: MapDocument.hpp:45
~UseOrthoMatricesT()
The destructor.
Definition: Renderer3D.cpp:49
UseOrthoMatricesT(const wxWindow &Window)
The constructor.
Definition: Renderer3D.cpp:35
void InitFrame()
Initializes the rendering of a new frame by computing and caching all relevant data.
Definition: Renderer3D.cpp:90
Definition: OrthoBspTree.hpp:45
void RenderCrossHair(const wxPoint &Center) const
Renders a cross-hair at the given point. Assumes that orthogonal rendering mode is active...
Definition: Renderer3D.cpp:319
void BasisVectors(const Vector3fT &Pos, const cf::math::Matrix3x3fT &Mat, float Length=100.0f) const
Renders the basis vectors (the "axes") of the given matrix at the given position with the given lengt...
Definition: Renderer3D.cpp:290
void RenderSplitPlanes(const OrthoBspTreeT::NodeT *Node, int Depth) const
Renders the split planes of the BSP tree at and below the given node, up to the given depth...
Definition: Renderer3D.cpp:228
A helper class that temporarily sets up the matrices in the Cafu MatSys for orthogonal rendering into...
Definition: Renderer3D.hpp:37
Definition: ChildFrameViewWin3D.hpp:21
void RenderLine(const Vector3fT &A, const Vector3fT &B, const wxColour &Color) const
Renders a line from A to B in the given color.
Definition: Renderer3D.cpp:211
Renderer3DT(ViewWindow3DT &ViewWin3D)
The constructor.
Definition: Renderer3D.cpp:57
This is the base class for all tools in the map editor.
Definition: Tool.hpp:34
void RenderBox(const BoundingBox3fT &BB, const wxColour &Color, bool Solid) const
Renders a box from the given bounding-box in the given color, with solid faces or in wireframe...
Definition: Renderer3D.cpp:120
This class represents a generic 3x3 matrix.
Definition: Angles.hpp:17
This class represents an axis-aligned bounding-box ("AABB") in 3-dimensional space.
Definition: BoundingBox.hpp:23
This is the base class for all elements ("objects") that can exist in a map.
Definition: MapElement.hpp:57