-
Notifications
You must be signed in to change notification settings - Fork 0
/
Laser.cpp
102 lines (91 loc) · 2.5 KB
/
Laser.cpp
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
#include "Laser.h"
StaticAsset<VertexBuffer> Laser::myVertexBuffer(AssetManager::CreateStaticFromMemory<VertexBuffer>(
"A2 position float2 false 16 0 texcoord float2 false 16 8 1 24"
"0.0 -0.5 0.0 0.0 "
"1.0 -0.5 1.0 0.0 "
"1.0 0.5 1.0 1.0 "
"0.0 -0.5 0.0 0.0 "
"1.0 0.5 1.0 1.0 "
"0.0 0.5 0.0 1.0 "));
StaticAsset<Program> Laser::LaserProgram(AssetManager::CreateStaticFromMemory<Program>(
"##s\n"
"uniform ivec2 FrameCount; \n"
"uniform ivec2 Frame; \n"
"uniform mat4 ViewProjection; \n"
"uniform mat4 World; \n"
"uniform float Length; \n"
"attribute vec2 position; \n"
"attribute vec2 texcoord; \n"
"varying vec2 vtexcoord; \n"
"void main(void) \n"
"{ \n"
" vec2 pos = position; \n"
" vec2 tex = texcoord; \n"
" if(pos.x < 0.1) \n"
" { \n"
" tex.x = 1.0 - (Length / float(FrameCount.x)); \n"
" } \n"
" else if(pos.x > 0.9) \n"
" { \n"
" pos.x = Length; \n"
" } \n"
" vec2 frameSize = vec2(1.0,1.0/float(FrameCount.y));\n"
" vtexcoord = vec2((tex.x+float(Frame.x)) * frameSize.x,(tex.y+float(Frame.y)) * frameSize.y); \n"
" gl_Position = ViewProjection * (World * vec4(pos.x, pos.y, 0.0, 1.0)); \n"
"} \n"
"##s\n"
"uniform sampler2D Texture; \n"
"varying vec2 vtexcoord; \n"
"void main(void) \n"
"{ \n"
" gl_FragColor = texture2D(Texture, vtexcoord); \n"
"} \n"
"##\n"
"Global:ViewProjection;"
"World:World;"
"Default:FrameCount=1 1;"
"Default:Frame=0 0;"
"Default:Length=0;"));
PropertyInfo Laser::LengthProperty("Length", typeid(Laser));
void Laser::OnPropertyChanged(const PropertyInfo *info, bool implicit)
{
if(info == &LengthProperty)
{
if(HasProgram())
{
ProgramState().SetUniform("Length", Length.Get());
}
}
FixedGeometry::OnPropertyChanged(info, implicit);
}
void Laser::OnCreate()
{
if(!myTextureRef.Empty())
myTexture = myTextureRef;
//Debug("Set my program");
FixedGeometry::OnCreate();
if(!HasProgram())
SetProgram(LaserProgram.Get(Scene.Get()->GetAssetManager()));
}
void Laser::OnDestroy()
{
FixedGeometry::OnDestroy();
myTexture.Release();
}
void Laser::OnNewProgram()
{
if(!myTexture.Empty())
ProgramState().SetUniform("Texture", myTexture);
ProgramState().SetUniform("FrameCount", VectorI2(8,16));
ProgramState().SetUniform("Length", Length.Get());
FixedGeometry::OnNewProgram();
}
void Laser::OnDraw(FrameTime elapsed)
{
//glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
glDrawArrays(GL_TRIANGLES, 0, 6);
}
void LaserAnimationData::Update(real t)
{
Animated->Length.Set(t*Length);
}