test.cs 7.46 KB
Newer Older
pcy's avatar
initial  
pcy committed
1
2
3
4
5
6
7
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
pcy's avatar
pcy committed
8
using PoroCYon.FNAGLSL;
pcy's avatar
initial  
pcy committed
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

//using VertexType = Microsoft.Xna.Framework.Graphics.VertexPositionColorTexture;
[Serializable, StructLayout(LayoutKind.Sequential, Pack = 1)]
struct VertexType {
	Vector3 pos;
	Vector2 uv ;
	Color   col;

	public VertexType(Vector3 p, Color c, Vector2 uv) {
		pos = p; this.uv = uv; col = c;
	}

	public unsafe static VertexDeclaration Format = new VertexDeclaration(
		// what is the 4th argument even supposed to be?!!
		new VertexElement(0                                ,
			VertexElementFormat.Vector3, VertexElementUsage.Position         , 0)
	,	new VertexElement(0+sizeof(Vector3)                ,
			VertexElementFormat.Vector2, VertexElementUsage.TextureCoordinate, 0)
	,	new VertexElement(0+sizeof(Vector3)+sizeof(Vector2),
			VertexElementFormat.Color  , VertexElementUsage.Color            , 0)
	);
}

class TestGame : Game {
	GraphicsDeviceManager gdm;
	SpriteBatch sb;
	KeyboardState ks;
	BasicEffect hlsl;
pcy's avatar
pcy committed
37
	GLSLEffect glsl;
pcy's avatar
initial  
pcy committed
38
39
40
41
42

	public TestGame() : base() {
		gdm = new GraphicsDeviceManager(this);
	}

pcy's avatar
pcy committed
43
44
45
46
47
48
49
50
51
52
53
	// thanks Ralgor
	void Resize(Object s, EventArgs ea) {
		Window.ClientSizeChanged -= Resize;

		gdm.PreferredBackBufferWidth  = Window.ClientBounds.Width ;
		gdm.PreferredBackBufferHeight = Window.ClientBounds.Height;
		gdm.ApplyChanges();

		Window.ClientSizeChanged += Resize;
	}

pcy's avatar
initial  
pcy committed
54
55
56
57
58
	protected override void Initialize() {
		IsMouseVisible = true;
		Window.AllowUserResizing = true;
		Window.Title = "FNA GLSL hax";

pcy's avatar
pcy committed
59
60
61
		gdm.SynchronizeWithVerticalRetrace = true;
		gdm.ApplyChanges();

pcy's avatar
initial  
pcy committed
62
		base.Initialize();
pcy's avatar
pcy committed
63
64

		Window.ClientSizeChanged += Resize;
pcy's avatar
initial  
pcy committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
	}

	protected override void LoadContent() {
		sb = new SpriteBatch(GraphicsDevice);
		hlsl = new BasicEffect(GraphicsDevice);

		hlsl.LightingEnabled = false;
		hlsl.PreferPerPixelLighting = false;
		hlsl.FogEnabled = false;
		hlsl.TextureEnabled = false;
		hlsl.VertexColorEnabled = true;

		hlsl.World = hlsl.View = hlsl.Projection = Matrix.Identity;

pcy's avatar
pcy committed
79
80
		glsl = new GLSLEffect(GraphicsDevice, new Dictionary<GLSLPurpose, string>() {
			{ GLSLPurpose.VertexShader  ,
pcy's avatar
pcy committed
81
82
"#version 410 core\n"+
"\n"+
pcy's avatar
pcy committed
83
84
85
"in vec3 in_pos;\n"+
"in vec4 in_col;\n"+
"in vec2 in_texcoord;\n"+
pcy's avatar
pcy committed
86
"\n"+
pcy's avatar
pcy committed
87
"out vec2 out_texcoord;\n"+
pcy's avatar
pcy committed
88
"\n"+
pcy's avatar
pcy committed
89
90
91
92
93
94
95
96
97
98
99
100
"void main() {\n"+
"	gl_Position = vec4(in_pos.x, in_pos.y, in_pos.z, 1.0);\n"+
"	out_texcoord = in_texcoord;\n"+
"}\n"
/*"#version 430 core\n"+
"vec2 y = vec2(1., -1.);\n"+
"vec4 x[4] = {y.yyxx, y.xyxx, y.yxxx, y.xxxx};\n"+
"void main() {\n"+
"	gl_Position = x[gl_VertexID];\n"+
"}\n"*/
},
			{ GLSLPurpose.FragmentShader,
pcy's avatar
pcy committed
101
102
103
104
105
"#version 330 core\n"+
"\n"+
"uniform float fGlobalTime;\n"+
"uniform vec2 v2Resolution;\n"+
"\n"+
pcy's avatar
pcy committed
106
"layout(location = 0) out vec4 C;\n"+
pcy's avatar
pcy committed
107
108
109
110
"\n"+
"vec4 plas(float time, vec2 v) {\n"+
"	float c = 0.5 + sin(v.x * 10.0) + cos(sin(time + v.y) * 20.0);\n"+
"	return vec4(sin(c * 0.2 + cos(time)), c * 0.15, cos(c * 0.1 + time / .4) * .25, 1.0);\n"+
pcy's avatar
pcy committed
111
"}\n"+
pcy's avatar
pcy committed
112
"\n"+
pcy's avatar
pcy committed
113
"void main() {\n"+
pcy's avatar
pcy committed
114
115
116
/*"	vec2 v2Resolution = vec2(1920.0, 1080.0);\n"+
"	float fGlobalTime = 0.0;\n"+
"\n"+*/
pcy's avatar
pcy committed
117
"	float pi = 4*atan(1);\n"+
pcy's avatar
pcy committed
118
"	vec2 uv = vec2(gl_FragCoord.x/v2Resolution.x, gl_FragCoord.y/v2Resolution.y);\n"+
pcy's avatar
pcy committed
119
"	uv -= 0.5;\n"+
pcy's avatar
pcy committed
120
121
"	uv /= vec2(v2Resolution.y/v2Resolution.x, 1.);\n"+
"\n"+
pcy's avatar
pcy committed
122
123
124
125
"	vec2 m;\n"+
"	m.x = atan(uv.x / uv.y) / pi;\n"+
"	m.y = 1 / length(uv) * .2;\n"+
"	float d = m.y;\n"+
pcy's avatar
pcy committed
126
127
128
129
"	m.x += sin(fGlobalTime) * 0.1;\n"+
"	m.y += fGlobalTime * 0.25;\n"+
"\n"+
"	vec4 t = plas(fGlobalTime, m * pi) / d;\n"+
pcy's avatar
pcy committed
130
131
132
133
134
"	C = clamp(t, 0.0, 1.0);\n"+
"	/*gl_FragColor = vec4(0);*/\n"+
"}\n"}
		});

pcy's avatar
initial  
pcy committed
135
136
137
138
139
140
141
		base.LoadContent();
	}

	protected override void UnloadContent() {
		base.UnloadContent();
	}

142
	TimeSpan totalTime = TimeSpan.Zero;
pcy's avatar
initial  
pcy committed
143
	protected override void Update(GameTime gt) {
144
145
		totalTime += gt.ElapsedGameTime;

pcy's avatar
initial  
pcy committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
		ks = Keyboard.GetState();

		base.Update(gt);

		if (ks.IsKeyDown(Keys.Escape)) {
			Exit();
			return;
		}
	}

	protected override void Draw(GameTime gt) {
		GraphicsDevice.Clear(Color.CornflowerBlue);

		base.Draw(gt);

		/*for (int i = 0; i < hlsl.Techniques.Count; ++i)
			Console.WriteLine(String.Format("t{0}: {1}", i, hlsl.Techniques[i].Name));*/
163
		/*var tech = hlsl.CurrentTechnique = hlsl.Techniques[0];
pcy's avatar
initial  
pcy committed
164
165
166
		for (int i = 0; i < tech.Passes.Count; ++i) {
			//Console.WriteLine(String.Format("p{0}: {1}", i, tech.Passes[i].Name));
			tech.Passes[i].Apply();
167
168
169
170
171

			GraphicsDevice.DrawUserPrimitives<VertexType>(
				PrimitiveType.TriangleStrip, vertices_l, 0, 2, VertexType.Format);
		}*/

172
173
174
175
176
		/*int i = 0;
		foreach (var p in glsl.Parameters) {
			Console.WriteLine("param[" + i + "] = " + p.Name);
			++i;
		}*/
pcy's avatar
pcy committed
177
178

		var vp = GraphicsDevice.Viewport;
pcy's avatar
pcy committed
179
		glsl.Parameters["fGlobalTime" ].SetValue((float)totalTime.TotalSeconds);
pcy's avatar
pcy committed
180
		glsl.Parameters["v2Resolution"].SetValue(new Vector2(vp.Width, vp.Height));
181

182
183
		using (var act = glsl.Bind()) {
			GraphicsDevice.DrawUserPrimitives<VertexType>(
pcy's avatar
pcy committed
184
				PrimitiveType.TriangleStrip, vertices_fs, 0, 2, VertexType.Format);
pcy's avatar
initial  
pcy committed
185
186
		}
	}
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231

	readonly VertexType[] vertices_fs = new[] {
		new VertexType(new Vector3(-1, -1, 0), Color.Red   , new Vector2(0, 0))
	,	new VertexType(new Vector3(-1,  1, 0), Color.Blue  , new Vector2(0, 1))
	,	new VertexType(new Vector3( 1, -1, 0), Color.Green , new Vector2(1, 0))
	,	new VertexType(new Vector3( 1,  1, 0), Color.Yellow, new Vector2(1, 1))
	};

	readonly VertexType[] vertices_r = new[] {
		new VertexType(new Vector3( 0, -1, 0), Color.Red   , new Vector2(0, 0))
	,	new VertexType(new Vector3( 0,  1, 0), Color.Blue  , new Vector2(0, 1))
	,	new VertexType(new Vector3( 1, -1, 0), Color.Green , new Vector2(1, 0))
	,	new VertexType(new Vector3( 1,  1, 0), Color.Yellow, new Vector2(1, 1))
	};
	readonly VertexType[] vertices_l = new[] {
		new VertexType(new Vector3(-1, -1, 0), Color.Red   , new Vector2(0, 0))
	,	new VertexType(new Vector3(-1,  1, 0), Color.Blue  , new Vector2(0, 1))
	,	new VertexType(new Vector3( 0, -1, 0), Color.Green , new Vector2(1, 0))
	,	new VertexType(new Vector3( 0,  1, 0), Color.Yellow, new Vector2(1, 1))
	};

	readonly VertexType[] vertices_tr = new[] {
		new VertexType(new Vector3( 0,  0, 0), Color.Red   , new Vector2(0, 0))
	,	new VertexType(new Vector3( 0,  1, 0), Color.Blue  , new Vector2(0, 1))
	,	new VertexType(new Vector3( 1,  0, 0), Color.Green , new Vector2(1, 0))
	,	new VertexType(new Vector3( 1,  1, 0), Color.Yellow, new Vector2(1, 1))
	};
	readonly VertexType[] vertices_br = new[] {
		new VertexType(new Vector3( 0, -1, 0), Color.Red   , new Vector2(0, 0))
	,	new VertexType(new Vector3( 0,  0, 0), Color.Blue  , new Vector2(0, 1))
	,	new VertexType(new Vector3( 1, -1, 0), Color.Green , new Vector2(1, 0))
	,	new VertexType(new Vector3( 1,  0, 0), Color.Yellow, new Vector2(1, 1))
	};
	readonly VertexType[] vertices_tl = new[] {
		new VertexType(new Vector3(-1,  0, 0), Color.Red   , new Vector2(0, 0))
	,	new VertexType(new Vector3(-1,  1, 0), Color.Blue  , new Vector2(0, 1))
	,	new VertexType(new Vector3( 0,  0, 0), Color.Green , new Vector2(1, 0))
	,	new VertexType(new Vector3( 0,  1, 0), Color.Yellow, new Vector2(1, 1))
	};
	readonly VertexType[] vertices_bl = new[] {
		new VertexType(new Vector3(-1, -1, 0), Color.Red   , new Vector2(0, 0))
	,	new VertexType(new Vector3(-1,  0, 0), Color.Blue  , new Vector2(0, 1))
	,	new VertexType(new Vector3( 0, -1, 0), Color.Green , new Vector2(1, 0))
	,	new VertexType(new Vector3( 0,  0, 0), Color.Yellow, new Vector2(1, 1))
	};
pcy's avatar
initial  
pcy committed
232
233
234
235
236
237
238
239
240
241
}

static class Program {
	[STAThread]
	static void Main(string[] args) {
		using (var g = new TestGame())
			g.Run();
	}
}