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
|
#ifndef __ASURA_ENGINE_SHADER_H__
#define __ASURA_ENGINE_SHADER_H__
#include <map>
#include <string>
#include <asura-utils/exceptions/exception.h>
#include <asura-utils/scripting/portable.hpp>
#include <asura-utils/io/renewable.h>
#include <asura-utils/math/vector2.hpp>
#include <asura-utils/math/vector3.hpp>
#include <asura-utils/math/vector4.h>
#include <asura-utils/math/matrix44.h>
#include <asura-utils/stringmap.hpp>
#include <asura-utils/manager.hpp>
#include "gl.h"
#include "color.h"
#include "texture.h"
#include "gpu_buffer.h"
namespace AsuraEngine
{
namespace Graphics
{
enum BuiltInUniforms
{
BUILTIN_UNIFORM_MODEL_MATRIX = 0,
BUILTIN_UNIFORM_VIEW_MATRIX,
BUILTIN_UNIFORM_PROJECTION_MATRIX,
BUILTIN_UNIFORM_COUNT
};
///
/// һshaderһڲʼ乲ijShaderuniformsͶݣֻṩ
/// uniformsuseɫķ༭ÿshaderͨshaderҵuniforms
/// ¶frameworkmaterialá
///
class Shader ASURA_FINAL
: public Scripting::Portable<Shader>
, public AEIO::Renewable
{
public:
Shader();
~Shader();
bool Load(const std::string& vert, const std::string& frag);
void OnUse();
void OnUnuse();
///
/// öԣAsuraֻ֧2ά
///
int GetAttributeLocation(const std::string& name);
void SetAttribute(int loc, void* data, uint offset = 0, uint stride = 0);
void SetAttribute(int loc, GPUBuffer* vbo, uint offset = 0, uint stride = 0);
///
/// uniform
///
bool HasUniform(const std::string& uniform);
uint GetUniformLocation(const std::string& uniform);
void SetUniformFloat(uint loc, float value);
void SetUniformVector2(uint loc, const Math::Vector2f& vec2);
void SetUniformVector3(uint loc, const Math::Vector3f& vec3);
void SetUniformVector4(uint loc, const Math::Vector4f& vec4);
void SetUniformColor(uint loc, const Color& color);
void SetUniformMatrix44(uint loc, const Math::Matrix44& mat44);
bool SetUniformTexture(uint loc, const Texture& texture);
///
/// \uniform
///
void SetBuiltInUniforms();
float GetUniformFloat(uint loc);
AEMath::Vector2f GetUniformVector2(uint loc);
AEMath::Vector3f GetUniformVector3(uint loc);
AEMath::Vector4f GetUniformVector4s(uint loc);
AEMath::Matrix44 GetUniformMatrix44(uint loc);
GLuint GetGLProgram();
static uint GetGLTextureUnitCount();
private:
//----------------------------------------------------------------------------//
LUAX_DECL_FACTORY(Shader);
LUAX_DECL_METHOD(_New);
LUAX_DECL_METHOD(_Use);
LUAX_DECL_METHOD(_Unuse);
LUAX_DECL_METHOD(_Load);
LUAX_DECL_METHOD(_Update);
LUAX_DECL_METHOD(_HasUniform);
LUAX_DECL_METHOD(_GetUniformLocation);
LUAX_DECL_METHOD(_SetBuiltInUniforms);
LUAX_DECL_METHOD(_SetUniformFloat);
LUAX_DECL_METHOD(_SetUniformTexture);
LUAX_DECL_METHOD(_SetUniformVector2);
LUAX_DECL_METHOD(_SetUniformVector3);
LUAX_DECL_METHOD(_SetUniformVector4);
LUAX_DECL_METHOD(_SetUniformColor);
//----------------------------------------------------------------------------//
std::string GetProgramWarnings();
std::string GetShaderWarnings(GLuint shader);
///
/// Asura shader ַͳһ
///
static const char* ASLSemantics[BUILTIN_UNIFORM_COUNT];
GLuint mProgram;
GLuint mVertShader;
GLuint mFragShader;
///
/// Model\View\Projection matrix uniformlocationAsuraҪshader
/// 壺
/// 1: asura_model_matrix ģ;
/// 2: asura_view_matrix ۲߾
/// 3: asura_projection_matrix ͶӰ
/// frameworkø壬ֵϷͱ༭еġ
///
GLint mMVP[3];
};
}
}
#endif
|