1 | #ifndef __RENDERQUEUE_H
|
---|
2 | #define __RENDERQUEUE_H
|
---|
3 |
|
---|
4 | #include "common.h"
|
---|
5 |
|
---|
6 |
|
---|
7 | namespace CHCDemoEngine
|
---|
8 | {
|
---|
9 |
|
---|
10 | class RenderState;
|
---|
11 | class Camera;
|
---|
12 |
|
---|
13 | typedef std::pair<Shape *, Transform3 *> ShapePair;
|
---|
14 | typedef std::vector<ShapePair> ShapePairArray;
|
---|
15 |
|
---|
16 | /** Defines a bucket for scene entities that have the same properties.
|
---|
17 | */
|
---|
18 | struct RenderQueueBucket
|
---|
19 | {
|
---|
20 | inline bool IsEmpty() const { return mShapes.empty();}
|
---|
21 |
|
---|
22 | ////////
|
---|
23 | //-- the attributes used to sort the materials
|
---|
24 |
|
---|
25 | // texture related parameters
|
---|
26 | int mTexHeight;
|
---|
27 | int mTexWidth;
|
---|
28 | int mTexFormat;
|
---|
29 |
|
---|
30 | bool mHasTexture;
|
---|
31 |
|
---|
32 | bool mColorWriteEnabled;
|
---|
33 | bool mDepthWriteEnabled;
|
---|
34 | bool mLightingEnabled;
|
---|
35 |
|
---|
36 | bool mAlphaTestEnabled;
|
---|
37 | bool mCullFaceEnabled;
|
---|
38 |
|
---|
39 | //bool mHasVertexProgram;
|
---|
40 | //bool mHasFragmentProgram;
|
---|
41 | ShaderProgram *mVertexProgram;
|
---|
42 | ShaderProgram *mFragmentProgram;
|
---|
43 |
|
---|
44 | /// minimal distance to the camera
|
---|
45 | //float mMinDistance;
|
---|
46 |
|
---|
47 | /// the shapes that belong to a bucket
|
---|
48 | ShapePairArray mShapes;
|
---|
49 | };
|
---|
50 |
|
---|
51 |
|
---|
52 | /** This class implements a render queue that sorts renderable geometry in order
|
---|
53 | to minimize state changes while approximately keeping front-to-back order
|
---|
54 | The implementation roughly follows the suggested implementation in
|
---|
55 | Tom Forsyth's article on render state changes. It assumes that the incoming
|
---|
56 | objecs are roughly front to back level and therefore does not require any sorting
|
---|
57 | and therefore has very low overhead.
|
---|
58 | */
|
---|
59 | class RenderQueue
|
---|
60 | {
|
---|
61 | public:
|
---|
62 |
|
---|
63 | /** Default constructor
|
---|
64 | */
|
---|
65 | RenderQueue();
|
---|
66 | /** Constructor passing the current render state
|
---|
67 | */
|
---|
68 | RenderQueue(RenderState *state);
|
---|
69 |
|
---|
70 | ~RenderQueue();
|
---|
71 | /** Enqueues an entity.
|
---|
72 | */
|
---|
73 | void Enqueue(SceneEntity *entity);
|
---|
74 | /** Enqueues a single shape.
|
---|
75 | */
|
---|
76 | void Enqueue(Shape *shape, Transform3 *trafo);
|
---|
77 | /** Sets the current render state
|
---|
78 | */
|
---|
79 | void SetRenderState(RenderState *state);
|
---|
80 | /** Returns the number of entries currently in the queue.
|
---|
81 | */
|
---|
82 | inline int GetSize() const { return (int)mNumEntries; }
|
---|
83 | /** Renders and clears the queue.
|
---|
84 | */
|
---|
85 | void Apply();
|
---|
86 | /** Clears the render queue
|
---|
87 | */
|
---|
88 | void Clear();
|
---|
89 |
|
---|
90 |
|
---|
91 | protected:
|
---|
92 |
|
---|
93 | //void Sort();
|
---|
94 |
|
---|
95 | inline bool FitsInBucket(Shape *shape, size_t idx) const;
|
---|
96 | /** Renders the contents of the render queue.
|
---|
97 | */
|
---|
98 | void Render();
|
---|
99 |
|
---|
100 |
|
---|
101 | ///////////
|
---|
102 | //-- members
|
---|
103 |
|
---|
104 | // the current render state
|
---|
105 | RenderState *mState;
|
---|
106 | /// each bucket contains objects with similar materials that don't cause a hard state change
|
---|
107 | std::vector<RenderQueueBucket *> mBuckets;
|
---|
108 | /// this are the buckets that where touched until the last clear. They are sorted by distance
|
---|
109 | std::vector<RenderQueueBucket *> mActiveBuckets;
|
---|
110 |
|
---|
111 | int mNumEntries;
|
---|
112 | };
|
---|
113 |
|
---|
114 |
|
---|
115 | }
|
---|
116 |
|
---|
117 | #endif
|
---|