-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTutorial.cs
More file actions
108 lines (89 loc) · 3.53 KB
/
Tutorial.cs
File metadata and controls
108 lines (89 loc) · 3.53 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
using Fusee.Engine.Common;
using Fusee.Engine.Core;
using Fusee.Math.Core;
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;
uniform float alpha;
varying vec3 modelpos;
void main()
{
modelpos = fuVertex;
float s = sin(alpha);
float c = cos(alpha);
gl_Position = vec4( fuVertex.x * c - fuVertex.z * s,
fuVertex.y,
fuVertex.x * s + fuVertex.z * c,
1.0);
}";
private const string _pixelShader = @"
#ifdef GL_ES
precision highp float;
#endif
varying vec3 modelpos;
void main()
{
gl_FragColor = vec4(modelpos*0.5 + 0.5, 1);
}";
private IShaderParam _alphaParam;
private float _alpha;
// Init is called on startup.
public override void Init()
{
_mesh = new Mesh
{
Vertices = new[]
{
new float3(-0.8165f, -0.3333f, -0.4714f),
new float3(0.8165f, -0.3333f, -0.4714f),
new float3(0, -0.3333f, 0.9428f),
new float3(0, 1, 0),
},
Triangles = new ushort[]
{
0, 2, 1,
0, 1, 3,
1, 2, 3,
2, 0, 3,
},
};
var shader = RC.CreateShader(_vertexShader, _pixelShader);
RC.SetShader(shader);
_alphaParam = RC.GetShaderParam(shader, "alpha");
_alpha = 0;
// Set the clear color for the backbuffer
RC.ClearColor = new float4(0.1f, 0.3f, 0.2f, 1);
}
// RenderAFrame is called once a frame
public override void RenderAFrame()
{
// Clear the backbuffer
RC.Clear(ClearFlags.Color | ClearFlags.Depth);
float2 speed = Mouse.Velocity;
_alpha += speed.x * 0.0001f;
RC.SetShaderParam(_alphaParam, _alpha);
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;
}
}
}