aboutsummaryrefslogtreecommitdiffstatsci
path: root/Tests/Hello3D.cs
blob: 2730bb63bd27d01fd1cb7f86a504196912c53fb3 (plain)
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
113
114
115
116
117
118
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
144
145
146
147
148
149
150
151
152
153
154
//
// Hello3D.cs
//
// Author:
//       Jean-Philippe Bruyère <jp.bruyere@hotmail.com>
//
// Copyright (c) 2013-2017 Jean-Philippe Bruyère
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using Crow;

namespace Tests
{
	class Hello3D : CrowWindow
	{
		[STAThread]
		static void Main ()
		{
			Hello3D win = new Hello3D ();
			win.Run (30);
		}

		public Hello3D ()
			: base(800, 600,"Crow Test with OpenTK")
		{
		}

		public Matrix4 modelview, projection;
		public int[] viewport = new int[4];
		public Vector3 vEyeTarget = new Vector3(0f, 0f, 0f);
		public Vector3 vEye;
		public Vector3 vLookInit = Vector3.Normalize(new Vector3(-1.0f, -1.0f, 1.0f));
		public Vector3 vLook;  // Camera vLook Vector
		public float zNear = 0.001f, zFar = 300.0f;
		public float fovY = (float)Math.PI / 4;
		public float eyeDist = 10.2f;
		public float viewZangle, viewXangle;
		public const float MoveSpeed = 0.02f;
		public const float RotationSpeed = 0.005f;
		public const float ZoomSpeed = 0.22f;

		vaoMesh cube;
		Texture texture;
		ProjectiveIFaceControler iface3D;

		void initGL(){
			GL.Enable (EnableCap.CullFace);
			GL.Enable (EnableCap.Blend);
			GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);

			cube = vaoMesh.CreateCube ();
			texture = new Texture ("image/textest.png");
		}

		protected override void OnLoad (EventArgs e)
		{
			base.OnLoad (e);

			CrowMouseMove += HelloCube_MouseMove;
			CrowMouseWheel += Hello3D_MouseWheelChanged;

			iface3D = Add3DInterface (800, 800,
				Matrix4.CreateScale (6f) *
				Matrix4.CreateRotationX (MathHelper.PiOver2) *
				Matrix4.CreateTranslation (Vector3.UnitY * -1.1f));
			Load (@"Interfaces/Divers/0.crow").DataSource = this;
			initGL ();
			shader.Enable ();
		}

		protected override void OnResize (EventArgs e)
		{
			base.OnResize (e);
			UpdateViewMatrix ();
		}

		public override void OnRender (FrameEventArgs e)
		{			
			base.OnRender (e);

			shader.SetMVP(modelview * projection);

			GL.BindTexture (TextureTarget.Texture2D, texture);
			cube.Render (BeginMode.Triangles);
			GL.BindTexture (TextureTarget.Texture2D, 0);
		}

		public void UpdateViewMatrix()
		{
			Rectangle r = this.ClientRectangle;
			GL.Viewport( r.X, r.Y, r.Width, r.Height);
			projection = Matrix4.CreatePerspectiveFieldOfView (fovY, r.Width / (float)r.Height, zNear, zFar);
			vLook = vLookInit.Transform(
				Matrix4.CreateRotationX (viewXangle)*
				Matrix4.CreateRotationZ (viewZangle));
			vLook.Normalize();
			vEye = vEyeTarget + vLook * eyeDist;
			modelview = Matrix4.LookAt(vEye, vEyeTarget, Vector3.UnitZ);
			GL.GetInteger(GetPName.Viewport, viewport);

			iface3D.UpdateView (projection, modelview, viewport, vEye);
		}

		void HelloCube_MouseMove(object sender, OpenTK.Input.MouseMoveEventArgs otk_e)
		{
			if (otk_e.Mouse.MiddleButton == OpenTK.Input.ButtonState.Pressed) {
				viewZangle -= (float)otk_e.XDelta * RotationSpeed;
				viewXangle -= (float)otk_e.YDelta * RotationSpeed;
				UpdateViewMatrix ();
			} else if (otk_e.Mouse.LeftButton == OpenTK.Input.ButtonState.Pressed) {
				return;
			} else if (otk_e.Mouse.RightButton == OpenTK.Input.ButtonState.Pressed) {
				Vector2 v2Look = vLook.Xy.Normalized ();
				Vector2 disp = v2Look.PerpendicularLeft * otk_e.XDelta * MoveSpeed +
					v2Look * otk_e.YDelta * MoveSpeed;
				vEyeTarget += new Vector3 (disp.X, disp.Y, 0);
				UpdateViewMatrix ();
			}
		}
		void Hello3D_MouseWheelChanged (object sender, OpenTK.Input.MouseWheelEventArgs e)
		{
			float speed = ZoomSpeed;
			//if (Keyboard[OpenTK.Input.Key.ControlLeft])
			//	speed *= 20.0f;

			eyeDist -= e.Delta * speed;
			if (eyeDist < zNear)
				eyeDist = zNear;
			else if (eyeDist > zFar)
				eyeDist = zFar;
			UpdateViewMatrix ();
		}
	}
}