test.cs 7.96 KB
Newer Older
pcy's avatar
initial  
pcy committed
1
2
using System;
using System.Collections.Generic;
pcy's avatar
pcy committed
3
using System.IO;
pcy's avatar
initial  
pcy committed
4
5
6
7
8
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
9
using PoroCYon.FNAGLSL;
pcy's avatar
initial  
pcy committed
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

//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
38
	GLSLEffect glsl;
pcy's avatar
pcy committed
39
	Texture2D thetex;
pcy's avatar
initial  
pcy committed
40
41
42
43
44

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

pcy's avatar
pcy committed
45
46
47
48
49
50
51
52
53
54
55
	// 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
56
57
58
59
60
	protected override void Initialize() {
		IsMouseVisible = true;
		Window.AllowUserResizing = true;
		Window.Title = "FNA GLSL hax";

pcy's avatar
pcy committed
61
62
63
		gdm.SynchronizeWithVerticalRetrace = true;
		gdm.ApplyChanges();

pcy's avatar
initial  
pcy committed
64
		base.Initialize();
pcy's avatar
pcy committed
65
66

		Window.ClientSizeChanged += Resize;
pcy's avatar
initial  
pcy committed
67
68
69
70
	}

	protected override void LoadContent() {
		sb = new SpriteBatch(GraphicsDevice);
pcy's avatar
pcy committed
71
72
73
74
75

		using (var fs = File.OpenRead("checker.png")) {
			thetex = Texture2D.FromStream(GraphicsDevice, fs);
		}

pcy's avatar
initial  
pcy committed
76
77
78
79
80
81
82
83
84
85
		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
86
87
		glsl = new GLSLEffect(GraphicsDevice, new Dictionary<GLSLPurpose, string>() {
			{ GLSLPurpose.VertexShader  ,
pcy's avatar
pcy committed
88
89
"#version 410 core\n"+
"\n"+
pcy's avatar
pcy committed
90
91
92
"in vec3 in_pos;\n"+
"in vec4 in_col;\n"+
"in vec2 in_texcoord;\n"+
pcy's avatar
pcy committed
93
"\n"+
pcy's avatar
pcy committed
94
"out vec2 out_texcoord;\n"+
pcy's avatar
pcy committed
95
"\n"+
pcy's avatar
pcy committed
96
97
98
99
100
101
102
103
104
105
106
107
"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
108
109
110
111
"#version 330 core\n"+
"\n"+
"uniform float fGlobalTime;\n"+
"uniform vec2 v2Resolution;\n"+
pcy's avatar
pcy committed
112
"uniform sampler2D mytex;\n"+
pcy's avatar
pcy committed
113
"\n"+
pcy's avatar
pcy committed
114
"layout(location = 0) out vec4 C;\n"+
pcy's avatar
pcy committed
115
116
117
118
"\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
119
"}\n"+
pcy's avatar
pcy committed
120
"\n"+
pcy's avatar
pcy committed
121
"void main() {\n"+
pcy's avatar
pcy committed
122
123
124
/*"	vec2 v2Resolution = vec2(1920.0, 1080.0);\n"+
"	float fGlobalTime = 0.0;\n"+
"\n"+*/
pcy's avatar
pcy committed
125
"	float pi = 4*atan(1);\n"+
pcy's avatar
pcy committed
126
"	vec2 uv = vec2(gl_FragCoord.x/v2Resolution.x, gl_FragCoord.y/v2Resolution.y);\n"+
pcy's avatar
pcy committed
127
"	uv -= 0.5;\n"+
pcy's avatar
pcy committed
128
"	uv /= vec2(v2Resolution.y/v2Resolution.x, 1.);\n"+
pcy's avatar
pcy committed
129
"	vec2 ouv = uv;\n"+
pcy's avatar
pcy committed
130
"\n"+
pcy's avatar
pcy committed
131
132
133
134
"	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
135
136
137
138
"	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
139
"	C = clamp(t, 0.0, 1.0)*texture2D(mytex, ouv);\n"+
pcy's avatar
pcy committed
140
141
142
143
"	/*gl_FragColor = vec4(0);*/\n"+
"}\n"}
		});

pcy's avatar
initial  
pcy committed
144
145
146
147
148
		base.LoadContent();
	}

	protected override void UnloadContent() {
		base.UnloadContent();
pcy's avatar
pcy committed
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165

		if (thetex != null) {
			thetex.Dispose();
			thetex = null;
		}
		if (hlsl != null) {
			hlsl.Dispose();
			hlsl = null;
		}
		if (glsl != null) {
			glsl.Dispose();
			glsl = null;
		}
		if (sb != null) {
			sb.Dispose();
			sb = null;
		}
pcy's avatar
initial  
pcy committed
166
167
	}

168
	TimeSpan totalTime = TimeSpan.Zero;
pcy's avatar
initial  
pcy committed
169
	protected override void Update(GameTime gt) {
170
171
		totalTime += gt.ElapsedGameTime;

pcy's avatar
initial  
pcy committed
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
		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));*/
189
		/*var tech = hlsl.CurrentTechnique = hlsl.Techniques[0];
pcy's avatar
initial  
pcy committed
190
191
192
		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();
193
194
195
196
197

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

198
199
200
201
202
		/*int i = 0;
		foreach (var p in glsl.Parameters) {
			Console.WriteLine("param[" + i + "] = " + p.Name);
			++i;
		}*/
pcy's avatar
pcy committed
203
204

		var vp = GraphicsDevice.Viewport;
pcy's avatar
pcy committed
205
		glsl.Parameters["fGlobalTime" ].SetValue((float)totalTime.TotalSeconds);
pcy's avatar
pcy committed
206
		glsl.Parameters["v2Resolution"].SetValue(new Vector2(vp.Width, vp.Height));
pcy's avatar
pcy committed
207
		glsl.Parameters["mytex"       ].SetValue(thetex);
208

209
210
		using (var act = glsl.Bind()) {
			GraphicsDevice.DrawUserPrimitives<VertexType>(
pcy's avatar
pcy committed
211
				PrimitiveType.TriangleStrip, vertices_fs, 0, 2, VertexType.Format);
pcy's avatar
initial  
pcy committed
212
213
		}
	}
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258

	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
259
260
261
262
263
264
265
266
267
268
}

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