/
OpenGLShaderPass.h
212 lines (168 loc) · 5.4 KB
/
OpenGLShaderPass.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#pragma once
#include "math/Vector3.h"
#include "math/Matrix4.h"
#include "iglrender.h"
#include <vector>
#include <map>
/* FORWARD DECLS */
class Matrix4;
class OpenGLRenderable;
class RendererLight;
namespace render
{
class OpenGLShader;
/**
* @brief A single component pass of an OpenGL shader.
*
* Each OpenGLShader may contain multiple passes, which are rendered
* independently. Each pass retains its own OpenGLState and a list of renderable
* objects to be rendered in this pass.
*/
class OpenGLShaderPass
{
protected:
OpenGLShader& _owner;
// The state applied to this bucket
OpenGLState _glState;
/*
* Representation of a transformed-and-lit renderable object. Stores a
* single object, with its transform matrix and illuminating light source.
*/
struct TransformedRenderable
{
// The renderable object
const OpenGLRenderable* renderable;
// The modelview transform for this renderable
const Matrix4 transform;
// Constructor
TransformedRenderable(const OpenGLRenderable& r,
const Matrix4& t)
: renderable(&r),
transform(t)
{}
};
// Vector of transformed renderables using this state
std::vector<TransformedRenderable> _transformedRenderables;
protected:
void setupTextureMatrix(GLenum textureUnit, const IShaderLayer::Ptr& stage);
// Render all of the given TransformedRenderables
void drawRenderables(OpenGLState& current);
/* Helper functions to enable/disable particular GL states */
void setTexture0();
void enableTexture2D();
void disableTexture2D();
void enableTextureCubeMap();
void disableTextureCubeMap();
void enableRenderBlend();
void disableRenderBlend();
// Apply all OpenGLState textures to texture units
void applyAllTextures(OpenGLState& current, unsigned requiredState);
virtual void activateShaderProgram(OpenGLState& current);
virtual void deactivateShaderProgram(OpenGLState& current);
public:
OpenGLShaderPass(OpenGLShader& owner) :
_owner(owner)
{}
OpenGLShader& getShader()
{
return _owner;
}
// Returns true if the stage associated to this pass is active and should be rendered
bool stateIsActive();
/**
* \brief
* Add a renderable to this shader pass the given object transform matrix
* and light.
*
* \param light
* Single light illuminating this renderable. When Shader::addRenderable
* accepts a LIST of lights, this list is then broken up into individual
* lights which are submitted to the shader passes one by one (so the same
* renderable will be submitted once for each light).
*/
void addRenderable(const OpenGLRenderable& renderable,
const Matrix4& modelview);
/**
* Return the OpenGL state associated with this bucket.
*/
OpenGLState& state()
{
return _glState;
}
const OpenGLState& state() const
{
return _glState;
}
OpenGLState* statePtr()
{
return &_glState;
}
/**
* \brief
* Render the renderables attached to this shader pass.
*
* \param current
* The current OpenGL state variables.
*
* \param flagsMask
* Mask of allowed render flags.
*
* \param viewer
* Viewer location in world space.
*
*/
void submitSurfaces(OpenGLState& current,
unsigned int flagsMask,
const Vector3& viewer,
const VolumeTest& view,
std::size_t time);
/**
* \brief
* Render the renderables attached to this shader pass.
* Their geometry might not be stored in the central buffer objects.
*/
void submitRenderables(OpenGLState& current,
unsigned int flagsMask,
const Vector3& viewer,
const VolumeTest& view,
std::size_t time);
/**
* Returns true if this shaderpass doesn't have anything to render.
*/
bool empty();
// True if this shader pass has OpenGLRenderables attached
bool hasRenderables() const;
// Clear out all renderable references accumulated during this frame
void clearRenderables();
// Whether this shader pass is suitable for the give view type
bool isApplicableTo(RenderViewType renderViewType) const;
// Apply own state to the "current" state object passed in as a reference,
// in combination with the global state mask, as well as setting
// relevant GL parameters directly.
void applyState(OpenGLState& current,
unsigned int globalStateMask,
const Vector3& viewer,
std::size_t time,
const IRenderEntity* entity);
// Set up lighting calculation
static void setUpLightingCalculation(OpenGLState& current,
const RendererLight* light,
const Matrix4& worldToLight,
const Vector3& viewer,
const Matrix4& objTransform,
std::size_t time,
bool invertVertexColour);
static void SetUpNonInteractionProgram(OpenGLState& current, const Vector3& viewer, const Matrix4& objTransform);
static void setTextureState(GLint& current,
const GLint& texture,
GLenum textureUnit,
GLenum textureMode);
static void setTextureState(GLint& current,
const GLint& texture,
GLenum textureMode);
friend std::ostream& operator<<(std::ostream& st, const OpenGLShaderPass& self);
};
typedef std::shared_ptr<OpenGLShaderPass> OpenGLShaderPassPtr;
// Stream insertion operator
std::ostream& operator<<(std::ostream& st, const OpenGLShaderPass& self);
} // namespace render