test.cs 6.7 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

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

	protected override void Initialize() {
		IsMouseVisible = true;
		Window.AllowUserResizing = true;
		Window.Title = "FNA GLSL hax";

		base.Initialize();
	}

	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
63
64
		glsl = new GLSLEffect(GraphicsDevice, new Dictionary<GLSLPurpose, string>() {
			{ GLSLPurpose.VertexShader  ,
pcy's avatar
pcy committed
65
66
"#version 410 core\n"+
"\n"+
pcy's avatar
pcy committed
67
68
69
"in vec3 in_pos;\n"+
"in vec4 in_col;\n"+
"in vec2 in_texcoord;\n"+
pcy's avatar
pcy committed
70
"\n"+
pcy's avatar
pcy committed
71
"out vec2 out_texcoord;\n"+
pcy's avatar
pcy committed
72
"\n"+
pcy's avatar
pcy committed
73
74
75
76
77
78
79
80
81
82
83
84
"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
85
86
87
88
89
"#version 330 core\n"+
"\n"+
"uniform float fGlobalTime;\n"+
"uniform vec2 v2Resolution;\n"+
"\n"+
pcy's avatar
pcy committed
90
"layout(location = 0) out vec4 C;\n"+
pcy's avatar
pcy committed
91
92
93
94
"\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
95
"}\n"+
pcy's avatar
pcy committed
96
"\n"+
pcy's avatar
pcy committed
97
"void main() {\n"+
pcy's avatar
pcy committed
98
99
100
/*"	vec2 v2Resolution = vec2(1920.0, 1080.0);\n"+
"	float fGlobalTime = 0.0;\n"+
"\n"+*/
pcy's avatar
pcy committed
101
"	float pi = 4*atan(1);\n"+
pcy's avatar
pcy committed
102
"	vec2 uv = vec2(gl_FragCoord.x/v2Resolution.x, gl_FragCoord.y/v2Resolution.y);\n"+
pcy's avatar
pcy committed
103
"	uv -= 0.5;\n"+
pcy's avatar
pcy committed
104
105
"	uv /= vec2(v2Resolution.y/v2Resolution.x, 1.);\n"+
"\n"+
pcy's avatar
pcy committed
106
107
108
109
"	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
110
111
112
113
"	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
114
115
116
117
118
"	C = clamp(t, 0.0, 1.0);\n"+
"	/*gl_FragColor = vec4(0);*/\n"+
"}\n"}
		});

pcy's avatar
initial  
pcy committed
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
		base.LoadContent();
	}

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

	protected override void Update(GameTime gt) {
		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));*/
144
		/*var tech = hlsl.CurrentTechnique = hlsl.Techniques[0];
pcy's avatar
initial  
pcy committed
145
146
147
		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();
148
149
150
151
152
153
154
155

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

		using (var act = glsl.Bind()) {
			GraphicsDevice.DrawUserPrimitives<VertexType>(
				PrimitiveType.TriangleStrip, vertices_br, 0, 2, VertexType.Format);
pcy's avatar
initial  
pcy committed
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
191
192
193
194
195
196
197
198
199
200
201
202

	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
203
204
205
206
207
208
209
210
211
212
}

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