GlmSharp
GlmSharp copied to clipboard
Instance methods
Sometimes a simple position struct has to be updated. There is actually no need to allocate a new object and let the old one be garbage on the heap. Also, this method of using memory is much much faster than creating a new instance from , for example, two multiplied struct.
I would aprreciate instance methods like:
// Somewhere in vec3.cs
public void Load(vec3 vec){
this.x = vec.x;
this.y = vec.y;
this.z = vec.z;
}
public void Mult(vec3 vec){
this.x *= vec.x;
this.y *= vec.y;
this.z *= vec.z;
}
An example how I would use it with matrices:
// In Camera.cs
public void Update(){
viewMat.Load(dmat4.Identity);
viewMat.Translate(position);
viewMat.Mul(rotation.ToMat4);
...
}
I really dislike this for syntactical reasons but I agree that it has performance benefits.
How about this idea:
All those methods are implemented as Extension Methods inside a new namespace (e.g. GlmSharp.PerformanceExtensions).
They will not clutter the interface unless you explicitly state using GlmSharp.PerformanceExtensions;
in the beginning.
Does that work?
I`m not sure if its works.
You have to use the this
keyword in your extension methods. It will copy the struct so you are not able to edit the fields =/ Thats what I experienced.
It works with the ref
keyword. But then Its not an extension. Also you have to make sure you never assign an other struct to the
ref` parameter before the assigning values. I can explain it in detail if needed.
Right that makes sense.
Hm I have to think about it a bit more because I really don't like to clutter the interface unnecessarily.
I just have tried some approaches. But there is just no way to create an extension with this ref
or some kind of other access...