patterncsharpMinor
Implementation of a VertexBufferObject class
Viewed 0 times
vertexbufferobjectimplementationclass
Problem
I have been working a simple 2D Game using C# language with OpenTK (OpenGL wrapper/binding for .NET). It uses a lot of sprites.
Since it uses lot of sprites, I think it's important to make everything implemented properly in order to prevent performance loss and gain more performance boost.
Also, I take care of the compatibility with old hardware too.
Can someone confirm whether this class is 'acceptable'/'good' implementation of
Notes: This class called
Rendering Implementation,
is not supported by the system.
Feel free to suggest me the better name for this class.
```
///
/// Define Rendering Implementation Processing Logic.
/// Implementation will use Vertex Bufer Object if supported and preferred, otherwise Immediate Mode will be used.
///
public class RenderImplementation : IDisposable
{
private static bool _isCompatibilityChecked = false;
private static bool _isSupported = true;
///
/// Gets whether the system support Vertex Buffer Object rendering.
/// Return true if supported, otherwise false.
///
public static bool IsVertexBufferObjectSupported
{
get
{
// I think calling this too much will lead performance issue.
// Let's check it once and return the checked value.
if (!_isCompatibilityChecked)
{
_isSupported = new Version(GL.GetString(StringName.Version).Substring(0, 3)) >= new Version(1, 5) ? true : false;
_isCompatibilityChecked = true;
}
return _isSupported;
}
}
///
/// Gets or Sets whether the Implementation should use Immediate Mode instead Vertex Buffer Object.
///
public static bool UseImmediateMode
{
get; set;
}
// Handles
// I dont think it necessary to
Since it uses lot of sprites, I think it's important to make everything implemented properly in order to prevent performance loss and gain more performance boost.
Also, I take care of the compatibility with old hardware too.
Can someone confirm whether this class is 'acceptable'/'good' implementation of
VertexBufferObject?Notes: This class called
RenderImplementation that contains 2Rendering Implementation,
VertexBufferObject and Immediate Mode,Immediate Mode is used when its preferred or.. VertexBufferObjectis not supported by the system.
Feel free to suggest me the better name for this class.
```
///
/// Define Rendering Implementation Processing Logic.
/// Implementation will use Vertex Bufer Object if supported and preferred, otherwise Immediate Mode will be used.
///
public class RenderImplementation : IDisposable
{
private static bool _isCompatibilityChecked = false;
private static bool _isSupported = true;
///
/// Gets whether the system support Vertex Buffer Object rendering.
/// Return true if supported, otherwise false.
///
public static bool IsVertexBufferObjectSupported
{
get
{
// I think calling this too much will lead performance issue.
// Let's check it once and return the checked value.
if (!_isCompatibilityChecked)
{
_isSupported = new Version(GL.GetString(StringName.Version).Substring(0, 3)) >= new Version(1, 5) ? true : false;
_isCompatibilityChecked = true;
}
return _isSupported;
}
}
///
/// Gets or Sets whether the Implementation should use Immediate Mode instead Vertex Buffer Object.
///
public static bool UseImmediateMode
{
get; set;
}
// Handles
// I dont think it necessary to
Solution
Your implementation suffers of over-engineering and is pretty rigid in terms of extensibility.
Legacy
Implementation
I would suggest to take a look at the following library I wrote a while ago, it does mimic what XNA does in the sense that you get VBOs, VAOs, Effects classes; and these are untied to any specific vertex declaration as you did.
Repository / usage example:
https://github.com/aybe/GLA
https://github.com/aybe/GLA/blob/master/GLADemo/GameDemoGLA.cs#L125
(you will see 3 examples: lines, triangles, textures)
Dynamic assignment of vertex attributes according user declaration:
https://github.com/aybe/GLA/blob/master/GLA/VertexArray.cs
https://github.com/aybe/GLA/blob/master/GLA/VertexDeclaration.cs
https://github.com/aybe/GLA/blob/master/GLA/VertexElement.cs
https://github.com/aybe/GLA/blob/master/GLA/VertexElementFormat.cs
https://github.com/aybe/GLA/blob/master/GLA/VertexElementUsage.cs
I designed that library by reverse-engineering XNA using a decompiler such as DotPeek.
Also, another great source of inspiration to you could be the MonoGame project:
https://github.com/mono/MonoGame/tree/develop/MonoGame.Framework/Graphics/Vertices
Last thing, spending a few days in using XNA or MonoGame would certainly help you get a better picture of what's a good API and how you should design yours consequently :D
EDIT:
For your sprites at different positions, either render them by each time updating the transform matrix in your shader or use instanciation.
Legacy
- are there really that many platforms still under OGL
- if you have such case, does it even support C# ?
- IMO you should have a
LegacyRenderer(immediate) and aModernRenderer, don't mix them
Implementation
- your system only supports one type of vertex declaration : position, color, UV
- nowhere you are letting the user specify its custom declaration
- nowhere you are letting the user specify a shader
- the binding of vertex attributes have fixed positions
I would suggest to take a look at the following library I wrote a while ago, it does mimic what XNA does in the sense that you get VBOs, VAOs, Effects classes; and these are untied to any specific vertex declaration as you did.
Repository / usage example:
https://github.com/aybe/GLA
https://github.com/aybe/GLA/blob/master/GLADemo/GameDemoGLA.cs#L125
(you will see 3 examples: lines, triangles, textures)
Dynamic assignment of vertex attributes according user declaration:
https://github.com/aybe/GLA/blob/master/GLA/VertexArray.cs
https://github.com/aybe/GLA/blob/master/GLA/VertexDeclaration.cs
https://github.com/aybe/GLA/blob/master/GLA/VertexElement.cs
https://github.com/aybe/GLA/blob/master/GLA/VertexElementFormat.cs
https://github.com/aybe/GLA/blob/master/GLA/VertexElementUsage.cs
I designed that library by reverse-engineering XNA using a decompiler such as DotPeek.
Also, another great source of inspiration to you could be the MonoGame project:
https://github.com/mono/MonoGame/tree/develop/MonoGame.Framework/Graphics/Vertices
Last thing, spending a few days in using XNA or MonoGame would certainly help you get a better picture of what's a good API and how you should design yours consequently :D
EDIT:
For your sprites at different positions, either render them by each time updating the transform matrix in your shader or use instanciation.
Context
StackExchange Code Review Q#98795, answer score: 3
Revisions (0)
No revisions yet.