Skip to content

Commit 3deba64

Browse files
authored
Update Viewport on resize in all of the GL tutorials. (#1965)
* Update Viewport on resize in all of the GL tutorials. * Update camera uniforms when updating viewport. * Hook resize in Tutorial 3.2 (missed). * Fix Tutorial 4.1 model file missing exception.
1 parent f698417 commit 3deba64

File tree

14 files changed

+104
-35
lines changed

14 files changed

+104
-35
lines changed

examples/CSharp/OpenGL Tutorials/Tutorial 1.1 - Hello Window/Program.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ private static void Main(string[] args)
2121
window.Load += OnLoad;
2222
window.Update += OnUpdate;
2323
window.Render += OnRender;
24+
window.FramebufferResize += OnFramebufferResize;
2425

2526
//Run the window.
2627
window.Run();
@@ -52,6 +53,11 @@ private static void OnUpdate(double obj)
5253
//Here all updates to the program should be done.
5354
}
5455

56+
private static void OnFramebufferResize(Vector2D<int> newSize)
57+
{
58+
//Update aspect ratios, clipping regions, viewports, etc.
59+
}
60+
5561
private static void KeyDown(IKeyboard arg1, Key arg2, int arg3)
5662
{
5763
//Check to close the window on escape.

examples/CSharp/OpenGL Tutorials/Tutorial 1.2 - Hello quad/Program.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ private static void Main(string[] args)
6666
window.Load += OnLoad;
6767
window.Render += OnRender;
6868
window.Update += OnUpdate;
69+
window.FramebufferResize += OnFramebufferResize;
6970
window.Closing += OnClose;
7071

7172
window.Run();
@@ -171,6 +172,11 @@ private static void OnUpdate(double obj)
171172

172173
}
173174

175+
private static void OnFramebufferResize(Vector2D<int> newSize)
176+
{
177+
Gl.Viewport(newSize);
178+
}
179+
174180
private static void OnClose()
175181
{
176182
//Remember to delete the buffers.

examples/CSharp/OpenGL Tutorials/Tutorial 1.3 - Textures/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static void Main(string[] args)
3535
_window.Load += OnLoad;
3636
_window.Update += OnUpdate;
3737
_window.Render += OnRender;
38-
_window.FramebufferResize += OnResize;
38+
_window.FramebufferResize += OnFramebufferResize;
3939
_window.Run();
4040

4141
_window.Dispose();
@@ -305,9 +305,9 @@ private static unsafe void OnRender(double dt)
305305
_gl.DrawElements(PrimitiveType.Triangles, 6, DrawElementsType.UnsignedInt, (void*) 0);
306306
}
307307

308-
private static void OnResize(Vector2D<int> size)
308+
private static void OnFramebufferResize(Vector2D<int> newSize)
309309
{
310-
_gl.Viewport(0, 0, (uint) size.X, (uint) size.Y);
310+
_gl.Viewport(newSize);
311311
}
312312
}
313313
}

examples/CSharp/OpenGL Tutorials/Tutorial 1.4 - Abstractions/Program.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ private static void Main(string[] args)
4444

4545
window.Load += OnLoad;
4646
window.Render += OnRender;
47+
window.FramebufferResize += OnFramebufferResize;
4748
window.Closing += OnClose;
4849

4950
window.Run();
@@ -92,6 +93,11 @@ private static unsafe void OnRender(double obj)
9293
Gl.DrawElements(PrimitiveType.Triangles, (uint) Indices.Length, DrawElementsType.UnsignedInt, null);
9394
}
9495

96+
private static void OnFramebufferResize(Vector2D<int> newSize)
97+
{
98+
Gl.Viewport(newSize);
99+
}
100+
95101
private static void OnClose()
96102
{
97103
//Remember to dispose all the instances.

examples/CSharp/OpenGL Tutorials/Tutorial 1.5 - Transformations/Program.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ private static void Main(string[] args)
4444

4545
window.Load += OnLoad;
4646
window.Render += OnRender;
47+
window.FramebufferResize += OnFramebufferResize;
4748
window.Closing += OnClose;
4849

4950
window.Run();
5051

51-
window.Dispose();
52+
window.Dispose();
5253
}
5354

5455

@@ -108,6 +109,11 @@ private static unsafe void OnRender(double obj)
108109
}
109110
}
110111

112+
private static void OnFramebufferResize(Vector2D<int> newSize)
113+
{
114+
Gl.Viewport(newSize);
115+
}
116+
111117
private static void OnClose()
112118
{
113119
Vbo.Dispose();

examples/CSharp/OpenGL Tutorials/Tutorial 2.1 - Co-ordinate Systems/Program.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ class Program
1111
private static IWindow window;
1212
private static GL Gl;
1313

14-
private const int Width = 800;
15-
private const int Height = 700;
16-
1714
private static BufferObject<float> Vbo;
1815
private static BufferObject<uint> Ebo;
1916
private static VertexArrayObject<float, uint> Vao;
@@ -88,6 +85,7 @@ private static void Main(string[] args)
8885

8986
window.Load += OnLoad;
9087
window.Render += OnRender;
88+
window.FramebufferResize += OnFramebufferResize;
9189
window.Closing += OnClose;
9290

9391
window.Run();
@@ -130,9 +128,11 @@ private static unsafe void OnRender(double obj)
130128
//Use elapsed time to convert to radians to allow our cube to rotate over time
131129
var difference = (float) (window.Time * 100);
132130

131+
var size = window.FramebufferSize;
132+
133133
var model = Matrix4x4.CreateRotationY(MathHelper.DegreesToRadians(difference)) * Matrix4x4.CreateRotationX(MathHelper.DegreesToRadians(difference));
134134
var view = Matrix4x4.CreateLookAt(CameraPosition, CameraTarget, CameraUp);
135-
var projection = Matrix4x4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(45.0f), Width / Height, 0.1f, 100.0f);
135+
var projection = Matrix4x4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(45.0f), (float)size.X / size.Y, 0.1f, 100.0f);
136136

137137
Shader.SetUniform("uModel", model);
138138
Shader.SetUniform("uView", view);
@@ -142,6 +142,11 @@ private static unsafe void OnRender(double obj)
142142
Gl.DrawArrays(PrimitiveType.Triangles, 0, 36);
143143
}
144144

145+
private static void OnFramebufferResize(Vector2D<int> newSize)
146+
{
147+
Gl.Viewport(newSize);
148+
}
149+
145150
private static void OnClose()
146151
{
147152
Vbo.Dispose();

examples/CSharp/OpenGL Tutorials/Tutorial 2.2 - Camera/Program.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ class Program
1414
private static GL Gl;
1515
private static IKeyboard primaryKeyboard;
1616

17-
private const int Width = 800;
18-
private const int Height = 700;
19-
2017
private static BufferObject<float> Vbo;
2118
private static BufferObject<uint> Ebo;
2219
private static VertexArrayObject<float, uint> Vao;
@@ -97,6 +94,7 @@ private static void Main(string[] args)
9794
window.Load += OnLoad;
9895
window.Update += OnUpdate;
9996
window.Render += OnRender;
97+
window.FramebufferResize += OnFramebufferResize;
10098
window.Closing += OnClose;
10199

102100
window.Run();
@@ -172,9 +170,11 @@ private static unsafe void OnRender(double deltaTime)
172170
//Use elapsed time to convert to radians to allow our cube to rotate over time
173171
var difference = (float) (window.Time * 100);
174172

173+
var size = window.FramebufferSize;
174+
175175
var model = Matrix4x4.CreateRotationY(MathHelper.DegreesToRadians(difference)) * Matrix4x4.CreateRotationX(MathHelper.DegreesToRadians(difference));
176176
var view = Matrix4x4.CreateLookAt(CameraPosition, CameraPosition + CameraFront, CameraUp);
177-
var projection = Matrix4x4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(CameraZoom), Width / Height, 0.1f, 100.0f);
177+
var projection = Matrix4x4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(CameraZoom), (float)size.X / size.Y, 0.1f, 100.0f);
178178

179179
Shader.SetUniform("uModel", model);
180180
Shader.SetUniform("uView", view);
@@ -184,6 +184,11 @@ private static unsafe void OnRender(double deltaTime)
184184
Gl.DrawArrays(PrimitiveType.Triangles, 0, 36);
185185
}
186186

187+
private static void OnFramebufferResize(Vector2D<int> newSize)
188+
{
189+
Gl.Viewport(newSize);
190+
}
191+
187192
private static unsafe void OnMouseMove(IMouse mouse, Vector2 position)
188193
{
189194
var lookSensitivity = 0.1f;

examples/CSharp/OpenGL Tutorials/Tutorial 3.1 - Ambient Lighting/Program.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ class Program
1313
private static GL Gl;
1414
private static IKeyboard primaryKeyboard;
1515

16-
private const int Width = 800;
17-
private const int Height = 700;
18-
1916
private static BufferObject<float> Vbo;
2017
private static BufferObject<uint> Ebo;
2118
private static VertexArrayObject<float, uint> VaoCube;
@@ -89,6 +86,7 @@ private static void Main(string[] args)
8986
window.Load += OnLoad;
9087
window.Update += OnUpdate;
9188
window.Render += OnRender;
89+
window.FramebufferResize += OnFramebufferResize;
9290
window.Closing += OnClose;
9391

9492
window.Run();
@@ -125,7 +123,8 @@ private static void OnLoad()
125123
LampShader = new Shader(Gl, "shader.vert", "shader.frag");
126124

127125
//Start a camera at position 3 on the Z axis, looking at position -1 on the Z axis
128-
Camera = new Camera(Vector3.UnitZ * 6, Vector3.UnitZ * -1, Vector3.UnitY, Width / Height);
126+
var size = window.FramebufferSize;
127+
Camera = new Camera(Vector3.UnitZ * 6, Vector3.UnitZ * -1, Vector3.UnitY, (float)size.X / size.Y);
129128
}
130129

131130
private static unsafe void OnUpdate(double deltaTime)
@@ -186,6 +185,12 @@ private static unsafe void OnRender(double deltaTime)
186185
Gl.DrawArrays(PrimitiveType.Triangles, 0, 36);
187186
}
188187

188+
private static void OnFramebufferResize(Vector2D<int> newSize)
189+
{
190+
Gl.Viewport(newSize);
191+
Camera.AspectRatio = (float)newSize.X / newSize.Y;
192+
}
193+
189194
private static unsafe void OnMouseMove(IMouse mouse, Vector2 position)
190195
{
191196
var lookSensitivity = 0.1f;

examples/CSharp/OpenGL Tutorials/Tutorial 3.2 - Diffuse Lighting/Program.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ class Program
1313
private static GL Gl;
1414
private static IKeyboard primaryKeyboard;
1515

16-
private const int Width = 800;
17-
private const int Height = 700;
18-
1916
private static BufferObject<float> Vbo;
2017
private static BufferObject<uint> Ebo;
2118
private static VertexArrayObject<float, uint> VaoCube;
@@ -90,6 +87,7 @@ private static void Main(string[] args)
9087
window.Load += OnLoad;
9188
window.Update += OnUpdate;
9289
window.Render += OnRender;
90+
window.FramebufferResize += OnFramebufferResize;
9391
window.Closing += OnClose;
9492

9593
window.Run();
@@ -127,7 +125,8 @@ private static void OnLoad()
127125
LampShader = new Shader(Gl, "shader.vert", "shader.frag");
128126

129127
//Start a camera at position 3 on the Z axis, looking at position -1 on the Z axis
130-
Camera = new Camera(Vector3.UnitZ * 6, Vector3.UnitZ * -1, Vector3.UnitY, Width / Height);
128+
var size = window.FramebufferSize;
129+
Camera = new Camera(Vector3.UnitZ * 6, Vector3.UnitZ * -1, Vector3.UnitY, (float)size.X / size.Y);
131130
}
132131

133132
private static unsafe void OnUpdate(double deltaTime)
@@ -170,6 +169,12 @@ private static unsafe void OnRender(double deltaTime)
170169
RenderLampCube();
171170
}
172171

172+
private static void OnFramebufferResize(Vector2D<int> newSize)
173+
{
174+
Gl.Viewport(newSize);
175+
Camera.AspectRatio = (float)newSize.X / newSize.Y;
176+
}
177+
173178
private static unsafe void RenderLitCube()
174179
{
175180
//Use the 'lighting shader' that is capable of modifying the cubes colours based on ambient lighting and diffuse lighting

examples/CSharp/OpenGL Tutorials/Tutorial 3.3 - Specular Lighting/Program.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ class Program
1313
private static GL Gl;
1414
private static IKeyboard primaryKeyboard;
1515

16-
private const int Width = 800;
17-
private const int Height = 700;
18-
1916
private static BufferObject<float> Vbo;
2017
private static BufferObject<uint> Ebo;
2118
private static VertexArrayObject<float, uint> VaoCube;
@@ -90,6 +87,7 @@ private static void Main(string[] args)
9087
window.Load += OnLoad;
9188
window.Update += OnUpdate;
9289
window.Render += OnRender;
90+
window.FramebufferResize += OnFramebufferResize;
9391
window.Closing += OnClose;
9492

9593
window.Run();
@@ -127,7 +125,8 @@ private static void OnLoad()
127125
LampShader = new Shader(Gl, "shader.vert", "shader.frag");
128126

129127
//Start a camera at position 3 on the Z axis, looking at position -1 on the Z axis
130-
Camera = new Camera(Vector3.UnitZ * 6, Vector3.UnitZ * -1, Vector3.UnitY, Width / Height);
128+
var size = window.FramebufferSize;
129+
Camera = new Camera(Vector3.UnitZ * 6, Vector3.UnitZ * -1, Vector3.UnitY, (float)size.X / size.Y);
131130
}
132131

133132
private static unsafe void OnUpdate(double deltaTime)
@@ -190,6 +189,12 @@ private static unsafe void OnRender(double deltaTime)
190189
Gl.DrawArrays(PrimitiveType.Triangles, 0, 36);
191190
}
192191

192+
private static void OnFramebufferResize(Vector2D<int> newSize)
193+
{
194+
Gl.Viewport(newSize);
195+
Camera.AspectRatio = (float)newSize.X / newSize.Y;
196+
}
197+
193198
private static unsafe void OnMouseMove(IMouse mouse, Vector2 position)
194199
{
195200
var lookSensitivity = 0.1f;

0 commit comments

Comments
 (0)