test.cs 2.96 KB
Newer Older
pcy's avatar
initial  
pcy committed
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
103
104
105
106
107
108
109
110
111
112
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;

//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;

	readonly VertexType[] vertices = 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))
	};

	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;

		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));*/

		var tech = hlsl.CurrentTechnique = hlsl.Techniques[0];

		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();
			GraphicsDevice.DrawUserPrimitives<VertexType>(
				PrimitiveType.TriangleStrip, vertices, 0, 2, VertexType.Format);
		}
	}
}

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