Cafu Engine
1 /*
2 Cafu Engine,
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
5 */
7 /*******************************/
8 /*** Renderer Implementation ***/
9 /*******************************/
14 // This is required for cg.h to get the function calling conventions (Win32 import/export/lib) right.
15 #ifdef _WIN32
16 #undef WIN32 // VC++ 8 seems to predefine something here that is not an integer, and thus doesn't work with the "#if WIN32" expression in cgGL.h.
17 #define WIN32 1
18 #endif
20 #include <Cg/cg.h>
21 #include <Cg/cgGL.h>
22 #include "../Renderer.hpp"
23 #include "Templates/Array.hpp"
24 #include "Math3D/Matrix.hpp"
25 #include "../Common/DepRelMatrix.hpp"
26 #include "../Expression.hpp"
29 class ShaderT;
30 class TextureMap2DT;
31 class TextureMapCubeT;
35 {
36  public:
38  // RendererI implementation.
39  bool IsSupported() const;
40  bool DoesSupportCompressedSHL() const;
41  bool DoesSupportUncompressedSHL() const;
42  int GetPreferenceNr() const;
43  void Initialize();
44  void Release();
45  const char* GetDescription() const;
46  // OBSOLETE void SetBaseDir(const std::string& BaseDir_);
47  // OBSOLETE const std::string& GetBaseDir();
48  MatSys::RenderMaterialT* RegisterMaterial(const MaterialT* Material) const;
49  const MaterialT* GetMaterialFromRM(MatSys::RenderMaterialT* RenderMaterial) const;
50  unsigned long GetAmbientShaderIDFromRM(MatSys::RenderMaterialT* RenderMaterial) const;
51  unsigned long GetLightShaderIDFromRM(MatSys::RenderMaterialT* RenderMaterial) const;
52  void FreeMaterial(MatSys::RenderMaterialT* RenderMaterial);
53  void BeginFrame(double Time);
54  void EndFrame();
55  void PreCache();
56  void SetCurrentRenderAction(RenderActionT RA);
57  RenderActionT GetCurrentRenderAction() const;
59  void SetGenPurposeRenderingParam (unsigned long Index, float Value);
60  void SetGenPurposeRenderingParam (unsigned long Index, int Value);
61  float GetGenPurposeRenderingParamF(unsigned long Index) const;
62  int GetGenPurposeRenderingParamI(unsigned long Index) const;
64  void SetCurrentAmbientLightColor(float r, float g, float b);
65  float* GetCurrentAmbientLightColor(); // Can also be used for writing.
66  const float* GetCurrentAmbientLightColor() const;
68  void SetCurrentLightSourcePosition(float x, float y, float z);
69  float* GetCurrentLightSourcePosition();
70  const float* GetCurrentLightSourcePosition() const;
71  void SetCurrentLightSourceRadius(float r);
72  float& GetCurrentLightSourceRadius();
73  const float& GetCurrentLightSourceRadius() const;
74  void SetCurrentLightSourceDiffuseColor (float r, float g, float b);
75  float* GetCurrentLightSourceDiffuseColor();
76  const float* GetCurrentLightSourceDiffuseColor() const;
77  void SetCurrentLightSourceSpecularColor(float r, float g, float b);
78  float* GetCurrentLightSourceSpecularColor();
79  const float* GetCurrentLightSourceSpecularColor() const;
81  void SetCurrentEyePosition(float x, float y, float z);
82  float* GetCurrentEyePosition();
83  const float* GetCurrentEyePosition() const;
86  void PopLightingParameters();
88  void ClearColor(float r, float g, float b, float a);
89  void Flush();
91  const MatrixT& GetMatrix(MatrixNameT MN) const;
92  const MatrixT& GetMatrixInv(MatrixNameT MN) const;
93  const MatrixT& GetMatrixModelView() const;
95  void SetMatrix(MatrixNameT MN, const MatrixT& Matrix);
96  void Translate(MatrixNameT MN, float x, float y, float z);
97  void Scale (MatrixNameT MN, float scale);
98  void RotateX (MatrixNameT MN, float angle);
99  void RotateY (MatrixNameT MN, float angle);
100  void RotateZ (MatrixNameT MN, float angle);
102  void PushMatrix(MatrixNameT MN);
103  void PopMatrix (MatrixNameT MN);
105  void SetViewport(int x, int y, int width, int height);
106  void GetViewport(int viewport[4]);
108  void SetSelectionBuffer(unsigned long Size, unsigned int* Buffer);
109  unsigned long SetPickingRenderMode(PickingRenderModeT PRM);
110  void InitNameStack();
111  void LoadName(unsigned long Name);
112  void PushName(unsigned long Name);
113  void PopName();
115  void SetCurrentMaterial(MatSys::RenderMaterialT* RenderMaterial);
116  MatSys::RenderMaterialT* GetCurrentMaterial() const;
117  void LockCurrentMaterial(bool LockCM);
118  void SetCurrentLightMap(MatSys::TextureMapI* LightMap);
119  void SetCurrentLightDirMap(MatSys::TextureMapI* LightDirMap);
120  void SetCurrentSHLMaps(const ArrayT<MatSys::TextureMapI*>& SHLMaps);
121  void SetCurrentSHLLookupMap(MatSys::TextureMapI* SHLLookupMap);
122  void RenderMesh(const MatSys::MeshT& Mesh);
125  // Internal Interface.
126  MatSys::RenderMaterialT* GetCurrentRenderMaterial() const;
127  TextureMap2DT* GetCurrentLightMap() const;
128  TextureMap2DT* GetCurrentLightDirMap() const;
129  TextureMap2DT* GetCurrentSHLMap(unsigned long Index) const;
130  TextureMap2DT* GetCurrentSHLLookupMap() const;
131  TextureMapCubeT* GetNormalizationCubeMap() const;
132  TextureMap2DT* GetHelperGrayMap() const;
133  TextureMap2DT* GetHelperBlackMap() const;
134  TextureMap2DT* GetHelperLightBlue001Map() const;
135  unsigned long GetInitCounter() const;
136  CGcontext GetCgContext() const;
138  const DepRelMatrixT& GetDepRelMatrix(MatrixNameT MN) const;
139  const DepRelMatrixT& GetDepRelMatrixInv(MatrixNameT MN) const;
140  const DepRelMatrixT& GetDepRelMatrixModelView() const;
142  const ExpressionT::SymbolsT& GetExpressionSymbols() const { return ExpressionSymbols; }
144  static RendererImplT& GetInstance();
147  private:
149  struct LightingParamsT
150  {
151  float LightSourcePosition[3];
152  float LightSourceRadius;
153  float LightSourceDiffuseColor[3];
154  float LightSourceSpecularColor[3];
155  float EyePosition[3];
156  };
158  /// Based on the CurrentRenderAction and the CurrentRenderMaterial, set the CurrentShader.
159  /// Called from SetCurrentRenderAction() and SetCurrentMaterial().
160  void SetCurrentShader();
162  // std::string BaseDir;
163  RenderActionT CurrentRenderAction;
164  MatSys::RenderMaterialT* CurrentRenderMaterial;
165  bool LockCurrentRM;
166  ShaderT* CurrentShader;
167  TextureMap2DT* CurrentLightMap;
168  TextureMap2DT* CurrentLightDirMap;
169  TextureMap2DT* CurrentSHLMaps[16];
170  TextureMap2DT* CurrentSHLLookupMap;
171  LightingParamsT CurrentLightingParams;
172  ArrayT<LightingParamsT> LightingParamsStack;
173  TextureMapCubeT* NormalizationCubeMap;
174  // TextureMap2DT* HelperWhiteMap;
175  TextureMap2DT* HelperGrayMap;
176  TextureMap2DT* HelperBlackMap;
177  TextureMap2DT* HelperLightBlue001Map; ///< Represents the color-encoded (0, 0, 1) normal vector.
178  unsigned long InitCounter;
179  unsigned long ShaderChangeCounter;
181  CGcontext CgContext;
184  // The original source/parent matrices. Each matrix has an associated age, and they grow older whenever they are modified.
185  DepRelMatrixT Matrix [END_MARKER]; ///< The current MODEL_TO_WORLD, WORLD_TO_VIEW and PROJECITON matrices. All other matrices eventually depend on these.
186  ArrayT<MatrixT> MatrixStack[END_MARKER]; ///< The storage for the matrix stack.
188  // Dependent matrices. They all depend in some way or another on the elements of the Matrix array above.
189  // A dependent matrix is updated whenever its source matrix (or one of its source matrices) has grown older
190  // than it was when the dependent matrix was last created.
191  mutable InverseMatrixT MatrixInv[END_MARKER]; ///< The inverses of the above matrices. MatrixInv[MN] depends on Matrix[MN].
192  mutable ProductMatrixT MatrixModelView; ///< The model-to-view matrix. It depends on the Matrix[MODEL_TO_WORLD] and Matrix[WORLD_TO_VIEW] matrices.
194  /// This is where the symbol values for material expressions are stored.
195  ExpressionT::SymbolsT ExpressionSymbols;
198  /// Private constructor for the Singleton pattern.
199  /// The destructor is only private on accident.
200  RendererImplT();
202 #if defined(_WIN32) && defined(_MSC_VER)
203  public:
204 #endif
205  ~RendererImplT();
206 };
208 #endif
unsigned long GetAmbientShaderIDFromRM(MatSys::RenderMaterialT *RenderMaterial) const
Returns the ID of the ambient shader of the RenderMaterial.
Definition: RendererImpl.cpp:377
void BeginFrame(double Time)
Call this to begin rendering a new frame.
Definition: RendererImpl.cpp:416
const MaterialT * GetMaterialFromRM(MatSys::RenderMaterialT *RenderMaterial) const
Returns the MaterialT object that was used in RegisterMaterial() to create the RenderMaterial.
Definition: RendererImpl.cpp:369
This class represents a surface render material.
Definition: RenderMaterial.hpp:25
This class models the relationship with which a product matrix A*B depends on its components A and B ...
Definition: DepRelMatrix.hpp:76
void Initialize()
Call this after a successful call to IsSupported() in order to prepare the renderer for use...
Definition: RendererImpl.cpp:251
bool DoesSupportCompressedSHL() const
Returns true if this renderer supports SHL rendering with compressed (indexed) SHLMaps, false otherwise.
Definition: RendererImpl.cpp:125
int GetPreferenceNr() const
Returns a "global" preference number for this renderer.
Definition: RendererImpl.cpp:137
void FreeMaterial(MatSys::RenderMaterialT *RenderMaterial)
Used to free the previously registered (render-)material.
Definition: RendererImpl.cpp:401
void SetCurrentRenderAction(RenderActionT RA)
This activates a specific render action, as is required for multi-pass rendering with dynamic lights ...
Definition: RendererImpl.cpp:528
void PreCache()
This function pre-caches all textures of all materials that have been registered with RegisterMateria...
Definition: RendererImpl.cpp:454
bool IsSupported() const
Returns true if this renderer is supported on this system (e.g.
Definition: RendererImpl.cpp:85
const char * GetDescription() const
Returns a description of this renderer, e.g.
Definition: RendererImpl.cpp:314
This class represents a generic 4x4 matrix.
Definition: MapElement.hpp:30
This class represents a surface material ("A datastructural representation of a scripts material def...
Definition: Material.hpp:22
This is an interface to a texture-map.
Definition: TextureMap.hpp:23
This class models the relationship with which a inverse matrix depends on its original matrix...
Definition: DepRelMatrix.hpp:51
void PopLightingParameters()
Pops (restores) the lighting parameters that have previously been pushed onto the stack...
Definition: RendererImpl.cpp:704
void PushLightingParameters()
Pushes all lighting parameters onto a stack.
Definition: RendererImpl.cpp:698
This class represents a shader.
Definition: Shader.hpp:28
A matrix class with which dependencies among matrices can be handled.
Definition: DepRelMatrix.hpp:22
bool DoesSupportUncompressedSHL() const
Returns true if this renderer supports SHL rendering with uncompressed SHLMaps (four coefficients per...
Definition: RendererImpl.cpp:131
unsigned long GetLightShaderIDFromRM(MatSys::RenderMaterialT *RenderMaterial) const
Returns the ID of the light shader of the RenderMaterial.
Definition: RendererImpl.cpp:389
void EndFrame()
Call this to end the current frame.
Definition: RendererImpl.cpp:445
This structure contains the values for the "variables" of an expression.
Definition: Expression.hpp:69
This class represents a cube texture-map.
Definition: TextureMapImpl.hpp:92
MatSys::RenderMaterialT * RegisterMaterial(const MaterialT *Material) const
Registers the Material with the renderer, and returns a handle for future reference.
Definition: RendererImpl.cpp:332
This class represents a 2D texture-map.
Definition: TextureMapImpl.hpp:46
Definition: RendererImpl.hpp:34
This class provides an interface to a renderer.
Definition: Renderer.hpp:31
RenderActionT GetCurrentRenderAction() const
Returns the currently set render action.
Definition: RendererImpl.cpp:542
This class represents a polygonal mesh.
Definition: Mesh.hpp:45
void Release()
Call this in order to release this renderer.
Definition: RendererImpl.cpp:294