-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTutorial.cs
More file actions
147 lines (122 loc) · 5.55 KB
/
Tutorial.cs
File metadata and controls
147 lines (122 loc) · 5.55 KB
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
using System.Linq;
using Fusee.Base.Common;
using Fusee.Base.Core;
using Fusee.Engine.Common;
using Fusee.Engine.Core;
using Fusee.Math.Core;
using Fusee.Serialization;
using Fusee.Xene;
using static Fusee.Engine.Core.Input;
namespace Fusee.Tutorial.Core
{
[FuseeApplication(Name = "Tutorial Example", Description = "The official FUSEE Tutorial.")]
public class Tutorial : RenderCanvas
{
private Mesh _mesh;
private const string _vertexShader = @"
attribute vec3 fuVertex;
attribute vec3 fuNormal;
uniform mat4 xform;
varying vec3 modelpos;
varying vec3 normal;
void main()
{
modelpos = fuVertex;
normal = fuNormal;
gl_Position = xform * vec4(fuVertex, 1.0);
}";
private const string _pixelShader = @"
#ifdef GL_ES
precision highp float;
#endif
varying vec3 modelpos;
varying vec3 normal;
void main()
{
gl_FragColor = vec4(normal*0.5 + 0.5, 1);
}";
private IShaderParam _xformParam;
private float4x4 _xform;
private float _alpha;
private float _beta;
private float _yawCube1;
private float _pitchCube1;
private float _yawCube2;
private float _pitchCube2;
// Init is called on startup.
public override void Init()
{
// Load the scene file "Cube.fus"
SceneContainer sc = AssetStorage.Get<SceneContainer>("Cube.fus");
// Extract the 'First' object of type 'MeshComponent' found in 'sc'`s list of 'Children' without
// further specifying any search criterion ('c => true' means: any found MeshComponent will do).
MeshComponent mc = sc.Children.FindComponents<MeshComponent>(c => true).First();
// Generate a mesh from the MeshComponent's vertices, normals and triangles.
_mesh = new Mesh
{
Vertices = mc.Vertices,
Normals = mc.Normals,
Triangles = mc.Triangles
};
var shader = RC.CreateShader(_vertexShader, _pixelShader);
RC.SetShader(shader);
_xformParam = RC.GetShaderParam(shader, "xform");
_xform = float4x4.Identity;
// Set the clear color for the backbuffer
RC.ClearColor = new float4(1, 1, 1, 1);
}
static float4x4 ModelXForm(float3 pos, float3 rot, float3 pivot)
{
return float4x4.CreateTranslation(pos + pivot)
*float4x4.CreateRotationY(rot.y)
*float4x4.CreateRotationX(rot.x)
*float4x4.CreateRotationZ(rot.z)
*float4x4.CreateTranslation(-pivot);
}
// RenderAFrame is called once a frame
public override void RenderAFrame()
{
// Clear the backbuffer
RC.Clear(ClearFlags.Color | ClearFlags.Depth);
float2 speed = Mouse.Velocity + Touch.GetVelocity(TouchPoints.Touchpoint_0);
if (Mouse.LeftButton || Touch.GetTouchActive(TouchPoints.Touchpoint_0))
{
_alpha -= speed.x*0.0001f;
_beta -= speed.y*0.0001f;
}
_yawCube1 += Keyboard.ADAxis * 0.1f;
_pitchCube1 += Keyboard.WSAxis * 0.1f;
_yawCube2 += Keyboard.LeftRightAxis * 0.1f;
_pitchCube2 += Keyboard.UpDownAxis * 0.1f;
// Setup matrices
var aspectRatio = Width / (float)Height;
var projection = float4x4.CreatePerspectiveFieldOfView(3.141592f * 0.25f, aspectRatio, 0.01f, 20);
var view = float4x4.CreateTranslation(0, 0, 3)*float4x4.CreateRotationY(_alpha)*float4x4.CreateRotationX(_beta);
// First cube
var cube1Model = ModelXForm(new float3(-0.5f, 0, 0), new float3(_pitchCube1, _yawCube1, 0), new float3(0, 0, 0));
_xform = projection * view * cube1Model * float4x4.CreateScale(0.5f, 0.1f, 0.1f);
RC.SetShaderParam(_xformParam, _xform);
RC.Render(_mesh);
// Second cube
var cube2Model = ModelXForm(new float3(1, 0, 0), new float3(_pitchCube2, _yawCube2, 0), new float3(-0.5f, 0, 0));
_xform = projection * view * cube1Model * cube2Model * float4x4.CreateScale(0.5f, 0.1f, 0.1f);
RC.SetShaderParam(_xformParam, _xform);
RC.Render(_mesh);
// Swap buffers: Show the contents of the backbuffer (containing the currently rendered farame) on the front buffer.
Present();
}
// Is called when the window was resized
public override void Resize()
{
// Set the new rendering area to the entire new windows size
RC.Viewport(0, 0, Width, Height);
// Create a new projection matrix generating undistorted images on the new aspect ratio.
var aspectRatio = Width/(float) Height;
// 0.25*PI Rad -> 45° Opening angle along the vertical direction. Horizontal opening angle is calculated based on the aspect ratio
// Front clipping happens at 1 (Objects nearer than 1 world unit get clipped)
// Back clipping happens at 2000 (Anything further away from the camera than 2000 world units gets clipped, polygons will be cut)
var projection = float4x4.CreatePerspectiveFieldOfView(3.141592f * 0.25f, aspectRatio, 1, 20000);
RC.Projection = projection;
}
}
}