
Can't ref to a property that returns a Matrix4
Posted Sunday, 23 January, 2011 - 21:59 by tekord inHi! First of all, sorry for my english.
Got a problem with Matrix4. It is a value-type (struct), so there is no way to refer to it except in method argument. For example:
void GetFrustum(out Matrix4 matrix) { ... }
Necessary condition that 'out Matrix4' is a variable. But i coding a render system independed camera class which just creates a matrices (view, projection, ect.) which will be send to render system.
Camera class consist following code:
public Matrix4 ViewMatrix { get { return mViewMatrix; } }
And somewhere in program i trying to write:
GL.MatrixMode(MatrixMode.Modelview); GL.LoadMatrix(ref activeCamera.ViewMatrix);
And i failed. It is obvilious, cause trying to refer a property. Possible solution is open member field Matrix4 m_MatrixView instead of ViewMatrix property. But then we'll get violation of encapsulation cause everybody will can change this matrix. It is unacceptable.
Another solution is translate 'struct Matrix4' to 'class Matrix4' (value-type to reference-type). Matrix4 is very massive structure, we have big consumption of resources when passing parameter of Matrix4 type to methods without 'ref' modifier. In my opinion does not make sense to do Matrix4 as value-type. Do you agree?
If Matrix4 would be a class insted of structure when I would be get elegant solution of my problem. Create a class REnderSystem, put references to ViewMatrix, ProjectionMatrix, ect. into this class and pass ViewMatrix from Camera (even if it would be a property) to RenderSystem.
Do you have any ideas about this? :)


Comments
Re: Can't ref to a property that returns a Matrix4
A Matrix4 class (rather than struct) would destroy performance due to GC pressure.
The only solution is to assign the matrix to a temporary variable and pass that instead. It sucks, but I don't think this is possible to work around in C#.
Re: Can't ref to a property that returns a Matrix4
In my code I usually in this case put ViewMatrix as public member of class not as property. Then you can use it as ref/out argument.
Re: Can't ref to a property that returns a Matrix4
A Matrix4 class (rather than struct) would destroy performance due to GC pressure.
I see. Forgot about GC :(.
In my code I usually in this case put ViewMatrix as public member of class not as property. Then you can use it as ref/out argument.
I think to do your's way. But I don't like this solution anyway.
Thank you for answares! I think I got enough information.
Re: Can't ref to a property that returns a Matrix4
If somebody knows another solution please write. It is very interesting.
Re: Can't ref to a property that returns a Matrix4
Well, it is not a solution but in VB.Net i don't have this problem ;)
Re: Can't ref to a property that returns a Matrix4
Indeed, the VB.Net creates a temporary variable and passes that instead of the property, side-stepping the issue. (In C# you have to do this explicitly).
Re: Can't ref to a property that returns a Matrix4
Do you think that this behaviour could become a performance issue ?
Re: Can't ref to a property that returns a Matrix4
Possible but unlikely. This is something that could be tweaked at the very end, if the project doesn't meet its performance requirements, but I wouldn't worry about it otherwise.
Re: Can't ref to a property that returns a Matrix4
thanx for the advice
Re: Can't ref to a property that returns a Matrix4
For any data that I upload to uniforms, I created the following class:
I also created a helper class with the following function:
Since Floats is a class, it can be passed around and referenced. Meanwhile you can still do Matrix4 computations efficiently in your Camera class. Once you are done with your matrix computations, you can update your Floats instance with the new Matrix4 values. The helper Uniform() function makes it easy to upload the values to GL.