test.cs 6.35 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
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
		glsl = new GLSLEffect(GraphicsDevice, new Dictionary<GLSLPurpose, string>() {
			{ GLSLPurpose.VertexShader  ,
"#version 430 core\n"+
"in vec3 in_pos;\n"+
"in vec4 in_col;\n"+
"in vec2 in_texcoord;\n"+
"out vec2 out_texcoord;\n"+
"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,
"#version 430 core\n"+
"layout(location = 0) out vec4 C;\n"+
"vec4 plas(vec2 v) {\n"+
"	float c = 0.5 + sin(v.x * 10.0) + cos(sin(v.y) * 20.0);\n"+
"	return vec4(sin(c * 0.2 + 1), c * 0.15, cos(c * 0.1) * .25, 1.0);\n"+
"}\n"+
"void main() {\n"+
"	float pi = 4*atan(1);\n"+
"	vec2 uv = vec2(gl_FragCoord.x/1920.0, gl_FragCoord.y/1080);\n"+
"	uv -= 0.5;\n"+
"	uv /= vec2(float(1920)/1080.0, 1.);\n"+
"	vec2 m;\n"+
"	m.x = atan(uv.x / uv.y) / pi;\n"+
"	m.y = 1 / length(uv) * .2;\n"+
"	float d = m.y;\n"+
"	vec4 t = plas(m * pi) / d;\n"+
"	C = clamp(t, 0.0, 1.0);\n"+
"	/*gl_FragColor = vec4(0);*/\n"+
"}\n"}
		});

pcy's avatar
initial  
pcy committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
		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));*/
128
		/*var tech = hlsl.CurrentTechnique = hlsl.Techniques[0];
pcy's avatar
initial  
pcy committed
129
130
131
		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();
132
133
134
135
136
137
138
139

			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
140
141
		}
	}
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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

	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
187
188
189
190
191
192
193
194
195
196
}

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