-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgshader.h
190 lines (158 loc) · 4.73 KB
/
gshader.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
#ifndef GSHADER_H
#define GSHADER_H
#include "gmath.h"
#include "gbuffer.h"
#include <tuple>
#include "tgaimage.h"
#include "ggraphiclibdefine.h"
enum GShaderAppDataType
{
kSADTDefault,
};
// POSITION vertex position
// TANGENT vertex tangent
// NORMAL vertex normal
// TEXCOORD0 uv
// COLOR vertex color
enum GSlotType
{
kSlotTPosition,
kSlotTTangent,
kSlotTNormal,
kSlotTColor,
kSlotTUV0,
kSlotTUV1,
};
struct S_SlotInfo
{
int slot;
int datumCount;
GSlotType slotType;
};
struct S_appdata;
struct S_abs_appdata
{
S_abs_appdata(GShaderAppDataType appdataType):appdataType(appdataType){}
virtual ~S_abs_appdata(){}
static S_abs_appdata* CreateAppData(GShaderAppDataType appdataType);
static const std::vector<S_SlotInfo>& GetSlotInfoArr(GShaderAppDataType appdataType);
static const S_SlotInfo& GetSlotInfo(GShaderAppDataType appdataType, int slot);
const std::vector<S_SlotInfo>& GetSlotInfoArr()
{
return GetSlotInfoArr(appdataType);
}
const S_SlotInfo& GetSlotInfo(int slot)
{
return GetSlotInfo(appdataType, slot);
}
virtual void SetSlotDatum(int slot, int datumIdx, double value) = 0;
GShaderAppDataType appdataType;
};
struct S_appdata : public S_abs_appdata
{
S_appdata():S_abs_appdata(GShaderAppDataType::kSADTDefault){}
// slot info arr
static const std::vector<S_SlotInfo> _slotInfoArr;
virtual void SetSlotDatum(int slot, int datumIdx, double value);
GMath::vec3 vert;
GMath::vec2 uv;
GMath::vec3 normal;
};
struct S_abs_v2f
{
GMath::vec4 gl_position;
GMath::vec4 ndc()
{
return gl_position / gl_position[3];
}
};
struct S_v2f : public S_abs_v2f
{
virtual ~S_v2f(){}
GMath::vec3 wPos;
GMath::vec3 wNormal;
GMath::vec3 wTangent;
GMath::vec2 uv;
GMath::vec3 normal; // tangent space
GMath::vec3 tangent; // tangent space
};
struct S_fout
{
std::vector<GMath::vec4> colors;
};
class GGraphicLibAPI;
struct IShader
{
IShader(GShaderAppDataType appdataType):appdataType(appdataType)
{}
virtual ~IShader(){}
virtual GMath::vec4 vertex(GGraphicLibAPI* GLAPI, S_abs_appdata* vert_in, int vertIdx) = 0;
virtual int homogenous_clipping(GGraphicLibAPI* GLAPI) = 0;
virtual void calc_tangent(GGraphicLibAPI *GLAPI) = 0;
virtual void fragment(S_abs_v2f& frag_in, S_fout& frag_out, int fragIdx) = 0;
virtual S_abs_v2f* GetV2f(int idx) = 0;
virtual S_abs_v2f* interpolation(GGraphicLibAPI* GLAPI, GMath::vec3 lerpFactor, int fragIdx) = 0;
S_abs_appdata* CreateAppData()
{
return S_abs_appdata::CreateAppData(appdataType);
}
const std::vector<S_SlotInfo>& GetSlotInfoArr()
{
return S_abs_appdata::GetSlotInfoArr(appdataType);
}
GShaderAppDataType appdataType;
// status
GCullFaceType cullFaceType;
bool useEarlyPerFragementTest;
bool useAlphaBlend;
};
enum GShaderType
{
kSTDefault,
kSTPBRSpecular,
kSTPBRMetallic
};
struct GLightInfo;
struct GShader : public IShader
{
GShader():IShader(GShaderAppDataType::kSADTDefault)
{
useEarlyPerFragementTest = true;
cullFaceType = GCullFaceType::kFTBack;
}
// uniform attribute
GMath::vec3f wCamPos;
GMath::mat4f obj2World;
GMath::mat4f world2Obj;
GMath::mat4f world2View;
GMath::mat4f projMat;
GMath::mat4f invertProjMat;
std::vector<GLightInfo*> lights;
GColor diffuseColor{GColor::white};
std::vector<TGAImage>* diffusemaps_;
GMipmapType diff_mipmaptype{GMipmapType::kMipmapOff};
std::vector<TGAImage>* normalmaps_;
GMipmapType norm_mipmaptype{GMipmapType::kMipmapOff};
std::vector<TGAImage>* specularmaps_;
GMipmapType spec_mipmaptype{GMipmapType::kMipmapOff};
S_abs_v2f* GetV2f(int idx)
{
return &(v2f_data_arr[idx]);
}
std::vector<S_v2f> v2f_data_arr_tmp;
std::vector<S_v2f> v2f_data_arr;
S_v2f v2f_interpolated[4];
virtual GMath::vec4 vertex(GGraphicLibAPI* GLAPI, S_abs_appdata* vert_in, int vertIdx);
virtual int homogenous_clipping(GGraphicLibAPI* GLAPI);
int clip_vertex(int primitiveCount);
int clip_vertex(GFrustumPlaneType planeType, std::vector<S_v2f>& vertsIn, std::vector<S_v2f>& vertsOut);
virtual void calc_tangent(GGraphicLibAPI *GLAPI);
virtual S_abs_v2f* interpolation(GGraphicLibAPI* GLAPI, GMath::vec3 lerpFactor, int fragIdx);
virtual void fragment(S_abs_v2f& frag_in, S_fout& frag_out, int fragIdx);
GColor SampleTex(std::vector<TGAImage>* mipmaps, GMipmapType mipmapType, GMath::vec2f uv, int fragIdx, GColor defaultColor=GColor::black);
GMath::vec2f GetPixelCountPerTexel(const TGAImage* mipmap0Tex, int fragIdx);
};
struct GPBRShader : public GShader
{
};
#endif // GSHADER_H