1313
1414namespace RenderEngine
1515{
16+ /// <summary>
17+ /// 3D Renderer that supports drawing solid colored triangles, textured triangles, and sprites (billboards).
18+ /// It uses OpenGL 4.5 and OpenTK for rendering. The renderer manages its own shaders and buffers, and provides a simple API for drawing primitives and flushing batches.
19+ /// </summary>
20+ /// <seealso cref="System.IDisposable" />
1621 public sealed class Simple3DRenderer : IDisposable
1722 {
23+ /// <summary>
24+ /// Gets the width.
25+ /// </summary>
26+ /// <value>
27+ /// The width.
28+ /// </value>
1829 public int Width { get ; private set ; }
30+
31+ /// <summary>
32+ /// Gets the height.
33+ /// </summary>
34+ /// <value>
35+ /// The height.
36+ /// </value>
1937 public int Height { get ; private set ; }
38+
39+ /// <summary>
40+ /// The resource Manager, used to load shaders and textures. The renderer does not own the resources, it only uses them.
41+ /// </summary>
2042 private readonly GlResourceManager _resources ;
2143
2244 private int _vaoSolid , _vboSolid , _vaoTex , _vboTex ;
45+
2346 private int _shaderSolid , _shaderTex ;
47+
48+ /// <summary>
49+ /// The initialized
50+ /// </summary>
2451 private bool _initialized ;
52+
2553 private int _vboSolidCapacity = 16384 , _vboTexCapacity = 16384 ;
2654
55+ /// <summary>
56+ /// The projection
57+ /// </summary>
2758 private TK . Matrix4 _projection ;
59+
60+ /// <summary>
61+ /// The view
62+ /// </summary>
2863 private TK . Matrix4 _view ;
2964
65+ /// <summary>
66+ /// Gets the view matrix.
67+ /// </summary>
68+ /// <value>
69+ /// The view matrix.
70+ /// </value>
3071 public TK . Matrix4 ViewMatrix => _view ;
3172
73+ /// <summary>
74+ /// Initializes a new instance of the <see cref="Simple3DRenderer"/> class.
75+ /// </summary>
76+ /// <param name="width">The width.</param>
77+ /// <param name="height">The height.</param>
78+ /// <param name="resources">The resources.</param>
3279 public Simple3DRenderer ( int width , int height , GlResourceManager resources )
3380 {
3481 _resources = resources ;
3582 UpdateProjection ( width , height ) ;
3683 SetCamera ( new Vector3 ( 8 , 15 , 25 ) , new Vector3 ( 8 , 0 , 8 ) , Vector3 . UnitY ) ;
3784 }
3885
86+ /// <summary>
87+ /// Updates the projection.
88+ /// </summary>
89+ /// <param name="width">The width.</param>
90+ /// <param name="height">The height.</param>
3991 public void UpdateProjection ( int width , int height )
4092 {
4193 if ( width <= 0 ) width = 1 ;
@@ -45,6 +97,12 @@ public void UpdateProjection(int width, int height)
4597 _projection = TK . Matrix4 . CreatePerspectiveFieldOfView ( TK . MathHelper . DegreesToRadians ( 45f ) , aspect , 0.1f , 1000f ) ;
4698 }
4799
100+ /// <summary>
101+ /// Sets the camera.
102+ /// </summary>
103+ /// <param name="position">The position.</param>
104+ /// <param name="target">The target.</param>
105+ /// <param name="up">Up.</param>
48106 public void SetCamera ( Vector3 position , Vector3 target , Vector3 up )
49107 {
50108 // Convert System.Numerics to OpenTK here using the extension or manual copy
@@ -54,6 +112,9 @@ public void SetCamera(Vector3 position, Vector3 target, Vector3 up)
54112 new TK . Vector3 ( up . X , up . Y , up . Z ) ) ;
55113 }
56114
115+ /// <summary>
116+ /// Ensures the initialized.
117+ /// </summary>
57118 private void EnsureInitialized ( )
58119 {
59120 if ( _initialized ) return ;
@@ -81,7 +142,14 @@ private void EnsureInitialized()
81142 _initialized = true ;
82143 }
83144
84- // --- FIXED SIGNATURES ---
145+ // --- SIGNATURES ---
146+ /// <summary>
147+ /// Draws the triangle.
148+ /// </summary>
149+ /// <param name="v0">The v0.</param>
150+ /// <param name="v1">The v1.</param>
151+ /// <param name="v2">The v2.</param>
152+ /// <param name="color">The color.</param>
85153 public void DrawTriangle ( Vector3 v0 , Vector3 v1 , Vector3 v2 , ( int r , int g , int b , int a ) color )
86154 {
87155 EnsureInitialized ( ) ;
@@ -95,6 +163,16 @@ public void DrawTriangle(Vector3 v0, Vector3 v1, Vector3 v2, (int r, int g, int
95163 GL . DrawArrays ( PrimitiveType . Triangles , 0 , 3 ) ;
96164 }
97165
166+ /// <summary>
167+ /// Draws the textured triangle.
168+ /// </summary>
169+ /// <param name="v0">The v0.</param>
170+ /// <param name="uv0">The uv0.</param>
171+ /// <param name="v1">The v1.</param>
172+ /// <param name="uv1">The uv1.</param>
173+ /// <param name="v2">The v2.</param>
174+ /// <param name="uv2">The uv2.</param>
175+ /// <param name="textureId">The texture identifier.</param>
98176 public void DrawTexturedTriangle ( Vector3 v0 , Vector2 uv0 , Vector3 v1 , Vector2 uv1 , Vector3 v2 , Vector2 uv2 , int textureId )
99177 {
100178 EnsureInitialized ( ) ;
@@ -108,6 +186,12 @@ public void DrawTexturedTriangle(Vector3 v0, Vector2 uv0, Vector3 v1, Vector2 uv
108186 GL . DrawArrays ( PrimitiveType . Triangles , 0 , 3 ) ;
109187 }
110188
189+ /// <summary>
190+ /// Draws the sprite.
191+ /// </summary>
192+ /// <param name="position">The position.</param>
193+ /// <param name="radius">The radius.</param>
194+ /// <param name="textureId">The texture identifier.</param>
111195 public void DrawSprite ( Vector3 position , float radius , int textureId )
112196 {
113197 EnsureInitialized ( ) ;
@@ -130,6 +214,10 @@ public void DrawSprite(Vector3 position, float radius, int textureId)
130214 GL . DrawArrays ( PrimitiveType . Triangles , 0 , 6 ) ;
131215 }
132216
217+ /// <summary>
218+ /// Flushes the specified batch.
219+ /// </summary>
220+ /// <param name="batch">The batch.</param>
133221 public unsafe void Flush ( RenderBatch batch )
134222 {
135223 EnsureInitialized ( ) ;
@@ -160,14 +248,29 @@ public unsafe void Flush(RenderBatch batch)
160248 GL . BindVertexArray ( 0 ) ;
161249 }
162250
251+ /// <summary>
252+ /// Uploads the matrices.
253+ /// </summary>
254+ /// <param name="shader">The shader.</param>
255+ /// <param name="model">The model.</param>
163256 private void UploadMatrices ( int shader , ref TK . Matrix4 model )
164257 {
165258 GL . UniformMatrix4 ( GL . GetUniformLocation ( shader , "model" ) , false , ref model ) ;
166259 GL . UniformMatrix4 ( GL . GetUniformLocation ( shader , "view" ) , false , ref _view ) ;
167260 GL . UniformMatrix4 ( GL . GetUniformLocation ( shader , "projection" ) , false , ref _projection ) ;
168261 }
169262
263+ /// <summary>
264+ /// Ensures the buffer capacity.
265+ /// </summary>
266+ /// <param name="vbo">The vbo.</param>
267+ /// <param name="cap">The cap.</param>
268+ /// <param name="req">The req.</param>
170269 private void EnsureBufferCapacity ( int vbo , ref int cap , int req ) { if ( req <= cap ) return ; while ( cap < req ) cap *= 2 ; GL . BindBuffer ( BufferTarget . ArrayBuffer , vbo ) ; GL . BufferData ( BufferTarget . ArrayBuffer , cap * sizeof ( float ) , IntPtr . Zero , BufferUsageHint . DynamicDraw ) ; }
270+
271+ /// <summary>
272+ /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
273+ /// </summary>
171274 public void Dispose ( ) { if ( ! _initialized ) return ; GL . DeleteBuffer ( _vboSolid ) ; GL . DeleteVertexArray ( _vaoSolid ) ; GL . DeleteBuffer ( _vboTex ) ; GL . DeleteVertexArray ( _vaoTex ) ; }
172275 }
173276}
0 commit comments