Debian Codegraph @

include/irender.h

raw
1 #pragma once
2
3 #include "imodule.h"
4 #include "irenderview.h"
5 #include "iwindingrenderer.h"
6 #include "igeometryrenderer.h"
7 #include "isurfacerenderer.h"
8 #include <functional>
9
10 #include "math/Vector3.h"
11 #include "math/Vector4.h"
12 #include "render/Colour4.h"
13 #include "math/AABB.h"
14
15 #include "ishaderlayer.h"
16 #include <sigc++/signal.h>
17
18 /**
19 * \file
20 * Interfaces for the back-end renderer.
21 */
22
23 /**
24 * \name Global render flags
25 *
26 * These flags control which capabilities of the renderer are used throughout
27 * the render process. They have a four stage lifecycle:
28 *
29 * 1. The flags are initially SET in the Shader implementation, describing the
30 * features that the particular Shader would like to use for rendering its
31 * renderables. For example, a shader pass performing a blend will set
32 * RENDER_BLEND as one of its flags.
33 *
34 * 2. The flags are MASKED by another set of flags provided to a
35 * RenderableCollector before it traverses the scene graph, in order to control
36 * which shader-specified flags can actually be used for that render pass. For
37 * example, the XYRenderer renders in wireframe mode only, so it does not enable
38 * RENDER_FILL in its mask, while the CamRenderer does.
39 *
40 * 3. The flags may be used to set or change OpenGL state in the shader pass
41 * implementation. For example, if RENDER_BLEND is set, then glEnable(GL_BLEND)
42 * will be called before the associated shader's renderables are rendered. Some
43 * flags map directly to glEnable parameters, while others (such as
44 * RENDER_PROGRAM) specify more complex changes. Some flags do not enable any GL
45 * features at all.
46 *
47 * 4. The flags are passed as a parameter to the OpenGLRenderable::render()
48 * method, allowing individual objects to modify their behaviour accordingly.
49 * For example, objects may decide whether or not to submit colour information
50 * to OpenGL based on the value of the RENDER_VERTEX_COLOUR flag.
51 */
52 ///@{
53 const unsigned RENDER_DEFAULT = 0;
54 const unsigned RENDER_LINESTIPPLE = 1 << 0; // glEnable(GL_LINE_STIPPLE)
55 const unsigned RENDER_POLYGONSTIPPLE = 1 << 2; // glEnable(GL_POLYGON_STIPPLE)
56 const unsigned RENDER_ALPHATEST = 1 << 4; // glEnable(GL_ALPHA_TEST)
57 const unsigned RENDER_DEPTHTEST = 1 << 5; // glEnable(GL_DEPTH_TEST)
58 const unsigned RENDER_DEPTHWRITE = 1 << 6; // glDepthMask(GL_TRUE)
59
60 /// Colour buffer writing disabled with glColorMask
61 const unsigned RENDER_MASKCOLOUR = 1 << 7;
62
63 const unsigned RENDER_CULLFACE = 1 << 8; // glglEnable(GL_CULL_FACE)
64 const unsigned RENDER_SCALED = 1 << 9; // glEnable(GL_NORMALIZE)
65 const unsigned RENDER_SMOOTH = 1 << 10; // glShadeModel
66 const unsigned RENDER_LIGHTING = 1 << 11; // glEnable(GL_LIGHTING)
67 const unsigned RENDER_BLEND = 1 << 12; // glEnable(GL_BLEND)
68 const unsigned RENDER_OFFSETLINE = 1 << 13; // glEnable(GL_POLYGON_OFFSET_LINE)
69
70 /// Objects will be rendered as filled polygons (not wireframe).
71 const unsigned RENDER_FILL = 1 << 14;
72
73 /**
74 * If enabled, mesh objects (geometry that does not consist only of GL_POINTS)
75 * should submit vertex colour information. If disabled, mesh objects must not
76 * change glColor during rendering.
77 *
78 * Does not affect GL state.
79 */
80 const unsigned RENDER_VERTEX_COLOUR = 1 << 15;
81
82 /**
83 * If enabled, the renderer will apply the state's colour by calling glColor.
84 * If not enabled, a white colour will be used.
85 */
86 const unsigned RENDER_COLOUR = 1 << 16;
87
88 /// GL_TEXTURE_2D will be enabled during rendering.
89 const unsigned RENDER_TEXTURE_2D = 1 << 17;
90
91 /**
92 * Cube map rendering (in camera space) is enabled. The renderer will enable
93 * GL_TEXTURE_CUBE_MAP, and set up the texture matrix such that the viewer
94 * location is the origin. Objects should submit their vertex coordinates as
95 * texture coordinates, which will result in the correct cube map alignment.
96 */
97 const unsigned RENDER_TEXTURE_CUBEMAP = 1 << 18;
98
99 /**
100 * Normal map information will be used during rendering. If enabled, objects
101 * should submit normal/tangent/bitangent vertex attributes to enable normal
102 * mapping. Also used by shader passes to signal that they care about lighting
103 * (and need to be provided with a list of incident lights along with
104 * renderable objects).
105 */
106 const unsigned RENDER_BUMP = 1 << 19;
107
108 /// A vertex and fragment shader program will be used during rendering.
109 const unsigned RENDER_PROGRAM = 1 << 20;
110
111 const unsigned RENDER_OVERRIDE = 1 << 21;
112 typedef unsigned RenderStateFlags;
113 ///@}
114
115 class Matrix4;
116
117 template<typename Element> class BasicVector3;
118 typedef BasicVector3<double> Vector3;
119
120 class Shader;
121 typedef std::shared_ptr<Shader> ShaderPtr;
122
123 struct RenderableGeometry;
124
125 /**
126 * A RenderEntity represents a map entity as seen by the renderer.
127 * It provides up to 12 numbered parameters to the renderer:
128 * parm0, parm1 ... parm11.
129 *
130 * A few of the entity parms are hardwired to things like render colour
131 * as defined through the entity's _color keyvalue, some are set through
132 * scripting, spawmargs or gameplay code.
133 */
134 class IRenderEntity
135 {
136 public:
137 // Returns the name of this entity (mainly for debugging purposes)
138 virtual std::string getEntityName() const = 0;
139
140 /**
141 * Get the value of this entity's shader parm with the given index.
142 */
143 virtual float getShaderParm(int parmNum) const = 0;
144
145 /**
146 * Entities can specify directions, which are used for particle emission for instance.
147 */
148 virtual const Vector3& getDirection() const = 0;
149
150 /**
151 * Returns the wireframe shader for this entity - child primitives need this for rendering.
152 */
153 virtual const ShaderPtr& getWireShader() const = 0;
154
155 /**
156 * Returns the shader that is used to render coloured primitives like lines and points,
157 * using the colour of the entity as defined by its entityDef, light colour, etc.
158 * This shader will be applicable to both camera and ortho views, it can be used for
159 * visualisations such as target lines and light boxes.
160 */
161 virtual const ShaderPtr& getColourShader() const = 0;
162
163 /**
164 * Returns the colour of this entity, that is used to display its
165 * wireframe representation.
166 */
167 virtual Vector4 getEntityColour() const = 0;
168
169 /**
170 * Associates the given object with this entity and the given shader.
171 * It will be processed during the following lighting mode rendering passes.
172 */
173 virtual void addRenderable(const render::IRenderableObject::Ptr& object, Shader* shader) = 0;
174
175 /**
176 * Removes the object from this entity.
177 */
178 virtual void removeRenderable(const render::IRenderableObject::Ptr& object) = 0;
179
180 using ObjectVisitFunction = std::function<void(const render::IRenderableObject::Ptr&, Shader*)>;
181
182 /**
183 * Enumerate all entity object, unconditionally.
184 */
185 virtual void foreachRenderable(const ObjectVisitFunction& functor) = 0;
186
187 /**
188 * Enumerate all entity object (partially) intersecting with the given bounds.
189 * The bounds are specified in world coordinates.
190 */
191 virtual void foreachRenderableTouchingBounds(const AABB& bounds, const ObjectVisitFunction& functor) = 0;
192
193 // Returns true if this entity produces shadows when lit (i.e.returns false when the entity has "noshadows" set to 1)
194 virtual bool isShadowCasting() const = 0;
195 };
196 typedef std::shared_ptr<IRenderEntity> IRenderEntityPtr;
197 typedef std::weak_ptr<IRenderEntity> IRenderEntityWeakPtr;
198
199 /**
200 * \brief Interface for a light source in the renderer.
201 */
202 class RendererLight
203 {
204 public:
205 virtual ~RendererLight() {}
206
207 // Returns true when this light is visible in the scene, false otherwise
208 virtual bool isVisible() = 0;
209
210 /**
211 * \brief Return the render entity associated with this light
212 *
213 * The IRenderEntity is used to evaluate possible shader expressions in the
214 * shader returned by getShader(). The light object itself may be its own
215 * render entity (so getLightEntity() can just return *this).
216 */
217 virtual const IRenderEntity& getLightEntity() const = 0;
218
219 /// Return the shader for this light
220 virtual const ShaderPtr& getShader() const = 0;
221
222 /**
223 * \brief Return the world-space to light-texture-space transformation matrix.
224 *
225 * The light texture space is a box, with coordinates [0..1] on each
226 * dimension, representing the texture (UV) coordinates of the light falloff
227 * textures that will be applied to rendered fragments within the light
228 * volume.
229 *
230 * The matrix returned by this method transforms coordinates in world space
231 * into coordinates in light-texture space.
232 */
233 virtual Matrix4 getLightTextureTransformation() const = 0;
234
235 /**
236 * \brief Return the AABB of the illuminated volume.
237 *
238 * This AABB represents the boundaries of the volume which are illuminated
239 * by this light. Anything outside of this volume does not need to be
240 * considered for shading by this light.
241 *
242 * Note that for omni lights, dragging the light center point outside of
243 * the light volume does not expand the lightAABB() value, because the
244 * light center only affects the direction of the light rays, not the size
245 * of the illuminated volume.
246 */
247 virtual AABB lightAABB() const = 0;
248
249 /**
250 * \brief Return the light origin in world space.
251 *
252 * The light origin is the point from which the light rays are considered to
253 * be projected, i.e. the direction from which bump maps will be illuminated
254 * and shadows (if they existed) would be cast.
255 *
256 * For an omindirectional light, this origin is determined from the
257 * "light_center" keyvalue in combination with the bounding box itself,
258 * whereas for a projected light it is always equal to the tip of the
259 * pyramid (the same as worldOrigin()).
260 */
261 virtual Vector3 getLightOrigin() const = 0;
262
263 // Whether this light is allowed to cast shadows
264 virtual bool isShadowCasting() const = 0;
265
266 // Whether this light is using a blend light material
267 virtual bool isBlendLight() const = 0;
268 };
269 typedef std::shared_ptr<RendererLight> RendererLightPtr;
270
271 /// Debug stream insertion for RendererLight
272 inline std::ostream& operator<< (std::ostream& os, const RendererLight& l)
273 {
274 return os << "RendererLight(origin=" << math::pp(l.getLightOrigin())
275 << ", lightAABB=" << l.lightAABB() << ")";
276 }
277
278 class Renderable;
279 typedef std::function<void(Renderable&)> RenderableCallback;
280
281 typedef std::function<void(const RendererLight&)> RendererLightCallback;
282
283 /**
284 * \brief
285 * Interface for objects which can render themselves in OpenGL.
286 *
287 * This interface is used for highlight rendering, after renderable objects have
288 * first been submitted using the Renderable interface. The backend render()
289 * function should contain the OpenGL calls necessary to submit vertex, normal
290 * and texture-coordinate data.
291 */
292 class OpenGLRenderable
293 {
294 public:
295 virtual ~OpenGLRenderable() {}
296
297 /**
298 * \brief
299 * Submit OpenGL render calls.
300 */
301 virtual void render() const = 0;
302 };
303
304 class Matrix4;
305 class Texture;
306
307 #include "math/Vector3.h"
308
309 class Material;
310 typedef std::shared_ptr<Material> MaterialPtr;
311
312 /**
313 * A Shader represents a single material which can be rendered in OpenGL, which
314 * may correspond to an actual material (Material), a raw colour or a special
315 * GL shader.
316 *
317 * Importantly, a Shader also maintains its own list of OpenGLRenderable objects
318 * which use it -- the actual rendering is performed by traversing a list of
319 * Shaders and rendering the geometry attached to each one.
320 */
321 class Shader :
322 public render::IWindingRenderer,
323 public render::IGeometryRenderer,
324 public render::ISurfaceRenderer
325 {
326 public:
327 // Observer interface to get notified on (un-)realisation
328 class Observer
329 {
330 public:
331 virtual void onShaderRealised() = 0;
332 virtual void onShaderUnrealised() = 0;
333 };
334
335 virtual ~Shader() {}
336
337 /// Return the name used to construct this shader
338 virtual std::string getName() const = 0;
339
340 /**
341 * \brief
342 * Attach a renderable object to this Shader, which will be rendered using
343 * this Shader when the render backend is activated.
344 *
345 * \param renderable
346 * The OpenGLRenderable object to add.
347 *
348 * \param modelview
349 * The modelview transform for this object.
350 */
351 virtual void addRenderable(const OpenGLRenderable& renderable,
352 const Matrix4& modelview) = 0;
353
354 /**
355 * \brief
356 * Control the visibility of this shader.
357 *
358 * A shader that is not visible will perform no rendering and ignore any
359 * renderables submitted to it with addRenderable().
360 */
361 virtual void setVisible(bool visible) = 0;
362
363 /// Query if this shader is visible
364 virtual bool isVisible() const = 0;
365
366 virtual void incrementUsed() = 0;
367 virtual void decrementUsed() = 0;
368
369 // Attach/detach an observer to this shader object.
370 // In case the shader is already realised when attachObserver() is called,
371 // the observer's onShaderRealised() method is immediately invoked.
372 // The analogous holds for detachObserver(): if the shader is realised,
373 // the observer's onShaderUnrealised() method is invoked before unregistering it.
374 virtual void attachObserver(Observer& observer) = 0;
375 virtual void detachObserver(Observer& observer) = 0;
376
377 virtual bool isRealised() = 0;
378
379 /**
380 * \brief Retrieve the Material that was used to construct this shader (if
381 * any).
382 *
383 * \return
384 * An Material subclass with information about the shader definition
385 */
386 virtual const MaterialPtr& getMaterial() const = 0;
387
388 virtual unsigned int getFlags() const = 0;
389 };
390
391 /**
392 * Shared pointer typedef for Shader.
393 */
394 typedef std::shared_ptr<Shader> ShaderPtr;
395
396 // Defines a piece of text that should be rendered in the scene
397 class IRenderableText
398 {
399 public:
400 // The position in world space where the text needs to be rendered
401 virtual const Vector3& getWorldPosition() = 0;
402
403 // The text to be rendered. Returning an empty text will skip this renderable.
404 virtual const std::string& getText() = 0;
405
406 // The colour of the text
407 virtual const Vector4& getColour() = 0;
408 };
409
410 // An ITextRenderer is able to draw one or more IRenderableText
411 // instances to the scene
412 class ITextRenderer
413 {
414 public:
415 using Ptr = std::shared_ptr<ITextRenderer>;
416
417 using Slot = std::uint64_t;
418 static constexpr Slot InvalidSlot = std::numeric_limits<Slot>::max();
419
420 // Registers the given text instance for rendering
421 virtual Slot addText(IRenderableText& text) = 0;
422
423 // Removes the text instance in the given Slot
424 // The slot handle is invalidated by this operation and should be
425 // discarded by the client, by setting it to InvalidSlot
426 virtual void removeText(Slot slot) = 0;
427 };
428
429 const char* const MODULE_RENDERSYSTEM("ShaderCache");
430
431 // Render view type enumeration, is used to select (or leave out)
432 // Shader objects during rendering
433 enum class RenderViewType
434 {
435 Camera = 1 << 0,
436 OrthoView = 1 << 1,
437 };
438
439 enum class BuiltInShaderType
440 {
441 // A medium-sized point
442 Point,
443
444 // A bigger version of "Point"
445 BigPoint,
446
447 // Lines connecting the patch control points
448 PatchLattice,
449
450 /// Wireframe lines for manipulators
451 ManipulatorWireframe,
452
453 // Fill shader drawing above regular things
454 FlatshadeOverlay,
455
456 // Line shader used for drawing AAS area bounds
457 AasAreaBounds,
458
459 // Shader used for the boxes marking missing models
460 MissingModel,
461
462 // The polygon showing the clip plane intersecting brushes
463 BrushClipPlane,
464
465 // Line shader connecting the leak point trace
466 PointTraceLines,
467
468 // This is the shader drawing a coloured overlay
469 // over faces/polys. Its colour is configurable,
470 // and it has depth test activated (camera).
471 ColouredPolygonOverlay,
472
473 // This is the shader drawing a solid line to outline
474 // a selected item. The first pass has its depth test
475 // activated using GL_LESS, whereas the second pass
476 // draws the hidden lines in stippled appearance
477 // with its depth test using GL_GREATER (camera).
478 HighlightedPolygonOutline,
479
480 // Coloured line stipple overlay of selected items (ortho)
481 WireframeSelectionOverlay,
482
483 // Coloured line stipple overlay of selected grouped items (ortho)
484 WireframeSelectionOverlayOfGroups,
485
486 // Line shader used for drawing the three axes at entity origins
487 Pivot,
488
489 // Coloured merge action highlights (camera)
490 CameraMergeActionOverlayAdd,
491 CameraMergeActionOverlayRemove,
492 CameraMergeActionOverlayChange,
493 CameraMergeActionOverlayConflict,
494
495 // Coloured merge action highlights (ortho)
496 OrthoMergeActionOverlayAdd,
497 OrthoMergeActionOverlayRemove,
498 OrthoMergeActionOverlayChange,
499 OrthoMergeActionOverlayConflict,
500
501 // Highly transparent wire shader for inactive nodes
502 WireframeInactive,
503 };
504
505 // Available types of colour shaders. These areused
506 // to draw map items in the ortho view, or to render
507 // connection lines, light volumes in both camera and ortho.
508 enum class ColourShaderType
509 {
510 // Line shader for camera views only
511 CameraOutline,
512
513 // Fill shader, non-transparent
514 CameraSolid,
515
516 // Fill shader with transparency
517 CameraTranslucent,
518
519 // Solid lines used for ortho view rendering
520 OrthoviewSolid,
521
522 // Items drawn in both camera and ortho views
523 CameraAndOrthoview,
524
525 // Outline shader visible in camera and ortho views
526 CameraAndOrthoViewOutline,
527 };
528
529 class IRenderResult
530 {
531 public:
532 using Ptr = std::shared_ptr<IRenderResult>;
533
534 virtual ~IRenderResult() {}
535
536 virtual std::string toString() = 0;
537 };
538
539 constexpr const char* const RKEY_ENABLE_SHADOW_MAPPING = "user/ui/renderSystem/enableShadowMapping";
540
541 /**
542 * \brief
543 * The main interface for the backend renderer.
544 */
545 class RenderSystem
546 : public RegisterableModule
547 {
548 public:
549
550 /**
551 * \brief
552 * Capture the given shader, increasing its reference count and
553 * returning a pointer to the Shader object.
554 *
555 * @param name
556 * The name of the shader to capture.
557 *
558 * @returns
559 * Shader* object corresponding to the given material shader name.
560 */
561
562 virtual ShaderPtr capture(const std::string& name) = 0;
563
564 /**
565 * Retrieves an ITextRenderer instance with the given font style and size.
566 * This renderer will accept IRenderableText instances which define
567 * the actual text, colour and position.
568 */
569 virtual ITextRenderer::Ptr captureTextRenderer(IGLFont::Style style, std::size_t size) = 0;
570
571 /**
572 * Acquire one of DarkRadiant's built-in shaders, used for drawing
573 * things like connectors, lines, points and overlays.
574 */
575 virtual ShaderPtr capture(BuiltInShaderType type) = 0;
576
577 /**
578 * Capture a colour shader, there are various types available
579 * for rendering in ortho views, camera or both.
580 */
581 virtual ShaderPtr capture(ColourShaderType type, const Colour4& colour) = 0;
582
583 /**
584 * Register the given entity to be considered during rendering.
585 * If this entity is a light it will be automatically recognised
586 * and inserted into the set of lights.
587 */
588 virtual void addEntity(const IRenderEntityPtr& renderEntity) = 0;
589
590 /**
591 * Detaches this entity from this rendersystem, it won't be
592 * affected by any rendering after this call.
593 */
594 virtual void removeEntity(const IRenderEntityPtr& renderEntity) = 0;
595
596 /**
597 * Enumerates all known render entities in this system, invoking
598 * the functor for each of them.
599 */
600 virtual void foreachEntity(const std::function<void(const IRenderEntityPtr&)>& functor) = 0;
601
602 /**
603 * Enumerates all known render lights in this system, invoking
604 * the functor for each of them
605 */
606 virtual void foreachLight(const std::function<void(const RendererLightPtr&)>& functor) = 0;
607
608 /**
609 * \brief
610 * Main render method.
611 *
612 * This method traverses all of the OpenGLRenderable objects that have been
613 * submitted to Shader instances, and invokes their render() method to draw
614 * their geometry.
615 *
616 * \param globalFlagsMask
617 * The mask of render flags which are permitted during this render pass. Any
618 * render flag which is 0 in this mask will not be enabled during rendering,
619 * even if the particular shader requests it.
620 *
621 * * \param view
622 * The view used to setup the projection, modelview and lighting calculations.
623 */
624 virtual IRenderResult::Ptr renderFullBrightScene(RenderViewType renderViewType,
625 RenderStateFlags globalFlagsMask,
626 const render::IRenderView& view) = 0;
627
628 virtual void startFrame() = 0;
629 virtual void endFrame() = 0;
630
631 /**
632 * Render the scene based on the light-entity interactions.
633 * All the active lights and entities must have added themselves
634 * to this rendersystem at this point, using addEntity().
635 *
636 * \param globalFlagsMask
637 * The mask of render flags which are permitted during this render pass. Any
638 * render flag which is 0 in this mask will not be enabled during rendering,
639 * even if the particular shader requests it.
640 *
641 * \returns A result object which can be used to display a statistics summary.
642 */
643 virtual IRenderResult::Ptr renderLitScene(RenderStateFlags globalFlagsMask,
644 const render::IRenderView& view) = 0;
645
646 virtual void realise() = 0;
647 virtual void unrealise() = 0;
648
649 /**
650 * Get the current render time in milliseconds.
651 */
652 virtual std::size_t getTime() const = 0;
653
654 /**
655 * Set the render time in milliseconds.
656 */
657 virtual void setTime(std::size_t milliSeconds) = 0;
658
659 /* SHADER PROGRAMS */
660
661 /// Available GL programs used for backend rendering.
662 enum ShaderProgram
663 {
664 /// No shader program (normal GL fixed-function pipeline)
665 SHADER_PROGRAM_NONE,
666
667 /// Lighting interaction shader
668 SHADER_PROGRAM_INTERACTION
669 };
670
671 /// Get the current shader program in use.
672 virtual ShaderProgram getCurrentShaderProgram() const = 0;
673
674 /// Set the shader program to use.
675 virtual void setShaderProgram(ShaderProgram prog) = 0;
676
677 virtual void attachRenderable(Renderable& renderable) = 0;
678 virtual void detachRenderable(Renderable& renderable) = 0;
679 virtual void forEachRenderable(const RenderableCallback& callback) const = 0;
680
681 // Initialises the OpenGL extensions
682 virtual void extensionsInitialised() = 0;
683
684 // Returns true if openGL supports ARB or GLSL lighting
685 virtual bool shaderProgramsAvailable() const = 0;
686
687 // Activates or deactivates the merge render mode
688 virtual void setMergeModeEnabled(bool enabled) = 0;
689
690 // Subscription to get notified as soon as the openGL extensions have been initialised
691 virtual sigc::signal<void> signal_extensionsInitialised() = 0;
692 };
693 typedef std::shared_ptr<RenderSystem> RenderSystemPtr;
694 typedef std::weak_ptr<RenderSystem> RenderSystemWeakPtr;
695
696 /**
697 * \brief
698 * Global accessor method for the RenderSystem instance.
699 */
700 inline RenderSystem& GlobalRenderSystem()
701 {
702 static module::InstanceReference<RenderSystem> _reference(MODULE_RENDERSYSTEM);
703 return _reference;
704 }