-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
17-surface-shader.inl
71 lines (63 loc) · 2.19 KB
/
17-surface-shader.inl
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
#define SCENE_GAME 1
struct sSceneGame : public ZL_Scene
{
ZL_Shader shader_both, shader_fragment_only, shader_vertex_only;
ZL_Surface srfLogo;
sSceneGame() : ZL_Scene(SCENE_GAME) { }
void InitGlobal()
{
const char shader_vertex_shader_src[] = ZL_SHADER_SOURCE_HEADER(ZL_GLES_PRECISION_LOW)
"uniform mat4 u_mvpMatrix;"
"attribute vec4 a_position;"
"attribute vec4 a_color;"
"attribute vec2 a_texcoord;"
"varying vec4 v_color;"
"varying vec2 v_texcoord;"
"uniform float shear;"
"void main()"
"{"
"v_color = a_color;"
"v_texcoord = a_texcoord;"
"gl_Position = u_mvpMatrix * (a_position + vec4(a_texcoord.y*shear,0.0,0.0,0.0));"
"}";
const char shader_fragment_shader_src[] = ZL_SHADER_SOURCE_HEADER(ZL_GLES_PRECISION_LOW)
"uniform sampler2D u_texture;"
"varying vec4 v_color;"
"varying vec2 v_texcoord;"
"uniform float brightness;"
"void main()"
"{"
"gl_FragColor = texture2D(u_texture, v_texcoord);"
"gl_FragColor.rgb = brightness*vec3((gl_FragColor.r+gl_FragColor.g+gl_FragColor.b)/3.0);"
"}";
shader_both = ZL_Shader(shader_fragment_shader_src, shader_vertex_shader_src, "shear", "brightness");
shader_fragment_only = ZL_Shader(shader_fragment_shader_src, NULL, "brightness");
shader_vertex_only = ZL_Shader(NULL, shader_vertex_shader_src, "shear");
srfLogo = ZL_Surface("Data/ZILLALIB.png");
}
void Draw()
{
float t = ZLSINCESECONDS(0);
ZL_Display::ClearFill(ZL_Color::Black);
shader_both.Activate();
shader_both.SetUniform(50.0f+50.0f*scos(t), 0.5f+0.5f*ssin(t));
srfLogo.Draw( 50, 250);
shader_both.Deactivate();
shader_fragment_only.Activate();
shader_fragment_only.SetUniform(0.5f+0.5f*ssin(t));
srfLogo.Draw(450, 250);
shader_fragment_only.Deactivate();
shader_vertex_only.Activate();
shader_vertex_only.SetUniform(50.0f+50.0f*scos(t));
srfLogo.Draw(850, 250);
shader_vertex_only.Deactivate();
}
} SceneGame;
struct sMain : public ZL_Application
{
void Load(int argc, char *argv[])
{
ZL_Display::Init("Surface Shader", 1280, 720);
ZL_SceneManager::Init(SCENE_GAME);
}
} Main;