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
|
#ifndef __JE_SHADER_H__
#define __JE_SHADER_H__
#include "../../core/je_configuration.h"
#if defined(jin_graphics) && (jin_graphics & jin_graphics_shader)
#include <string>
#include <map>
#include "GLee/GLee.h"
#include "../je_color.h"
#include "../je_texture.h"
#include "../je_canvas.h"
#include "je_base.shader.h"
namespace JinEngine
{
namespace Graphics
{
namespace Shaders
{
///
/// Built in shader program.
///
/// Built in shader program written with custom shading language called JSL (jin shading language). A JSL
/// program is compiled into glsl, so most glsl built in functions and structs are available in JSL.
///
class Shader
{
public:
///
/// Create shader program from source code.
///
/// @param source The shader source code.
///
static Shader* createShader(const std::string& source);
///
/// Get current shader.
///
/// @return Current used shader program.
///
static inline Shader* getCurrentShader() { return CurrentShader; }
///
/// Unuse current shader.
///
static void unuse();
///
/// Destructor of shader.
///
virtual ~Shader();
///
/// Use specific shader.
///
void use();
///
/// Send float value to shader.
///
/// @param name Name of the uniform variable to be assigned.
/// @param number Value of uniform variable to be sent.
///
void sendFloat(const char* name, float number);
///
/// Send texture to shader.
///
/// @param name Name of the uniform variable to be assigned.
/// @param texture Texture to be sent.
///
void sendTexture(const char* name, const Texture* texture);
///
/// Send integer value to shader
///
/// @param name Name of the uniform variable to be assigned.
/// @param value Value to be sent.
///
void sendInt(const char* name, int value);
///
/// Send 2D vector to shader.
///
/// @param name Name of the uniform variable to be assigned.
/// @param x X value of the vector to be sent.
/// @param y Y value of the vector to be sent.
///
void sendVec2(const char* name, float x, float y);
///
/// Send 3D vector to shader.
///
/// @param name Name of the uniform variable to be assigned.
/// @param x X value of the vector to be sent.
/// @param y Y value of the vector to be sent.
/// @param z Z value of the vector to be sent.
///
void sendVec3(const char* name, float x, float y, float z);
///
/// Send 4D vector to shader.
///
/// @param name Name of the uniform variable to be assigned.
/// @param x X value of the vector to be sent.
/// @param y Y value of the vector to be sent.
/// @param z Z value of the vector to be sent.
/// @param w W value of the vector to be sent.
///
void sendVec4(const char* name, float x, float y, float z, float w);
///
/// Send canvas to shader.
///
/// @param name Name of the uniform variable to be assigned.
/// @param canvas Canvas to be sent.
///
void sendCanvas(const char* name, const Canvas* canvas);
///
/// Send color to shader.
///
/// @param name Name of the uniform variable to be assigned.
/// @param color Color to be sent.
///
void sendColor(const char* name, const Color* color);
///
/// Send 4 by 4 matrix to shader.
///
/// @param name Name of the uniform variable to be assigned.
/// @param mat4 Matrix to be sent.
///
void sendMatrix4(const char* name, const Math::Matrix* mat4);
///
/// Set vertices value.
///
/// @param n Number of vertices.
/// @param type Data type of each component in the array.
/// @param stride Byte offset between consecutive generic vertex attributes.
/// @param pointers Pointer to the first component of the first generic vertex attribute in the array.
///
void bindVertexPointer(int n, GLenum type, GLsizei stride, const GLvoid * pointers);
///
/// Set texture UV coordinates.
///
/// @param n Number of vertices.
/// @param type Data type of each component in the array.
/// @param stride Byte offset between consecutive generic vertex attributes.
/// @param pointers Pointer to the first component of the first generic vertex attribute in the array.
///
void bindUVPointer(int n, GLenum type, GLsizei stride, const GLvoid * pointers);
protected:
///
/// Reference of current used shader.
///
static Shader* CurrentShader;
///
/// Get texture unit of the uniform texture. If not, assign one.
///
/// @param name Name of the texture uniform variable.
/// @return Texture unit which texture variable be assigned.
///
GLint claimTextureUnit(const std::string& name);
///
/// Shader constructor.
///
Shader(const std::string& program);
///
/// Compile JSL program into GLSL source.
///
/// @param program JSL source code.
/// @return Return true if compile successed, otherwise return false.
///
bool compile(const std::string& program);
GLuint mPID;
GLint mCurrentTextureUnit;
std::map<std::string, GLint> mTextureUnits;
};
} // namespace Shaders
} // namespace Graphics
} // namespace JinEngine
#endif // (jin_graphics) && (jin_graphics & jin_graphics_shader)
#endif // __JE_SHADER_H__
|