PDA

View Full Version : D3DMaterials matching



XeviaN
14-06-2003, 08:23 PM
Hi all!
Sorry for that... but... i have a fantastic stupid question :?

So: DirectX 8.1 Clootie header (Hi Clootie! :D)

var D3DMatA, D3DMatB: D3DMaterial8;
...
if D3DMatA=D3DMatB then ...
...

Compile -> Hmm, no, "Incompatible types...". Ok:

...
if D3DMatA.Ambient=D3DMatB.Ambient then ...
...

Compile -> Hmm, no, "Incompatible types...". Ok:

...
if (D3DMatA.Ambient.r=D3DMatB.Ambient.r) and (D3DMatA.Ambient.g=D3DMatB.Ambient.g) and ...
...

It works! But, <GH!> I'm sure that's not the right method :shock:

How to do it? Tanks in advance :o

Clootie
15-06-2003, 09:47 AM
Hi!
So, we have:
[background=#FFFFFF][normal=#000000][number=#0000FF][string=#0000FF][comment=#248F24][reserved=#000000] TD3DColorValue = packed record
r: Single;
g: Single;
b: Single;
a: Single;
end {_D3DCOLORVALUE};

TD3DMaterial8 = packed record
Diffuse: TD3DColorValue; { Diffuse color RGBA }
Ambient: TD3DColorValue; { Ambient color RGB }
Specular: TD3DColorValue; { Specular 'shininess' }
Emissive: TD3DColorValue; { Emissive color RGB }
Power: Single; { Sharpness if specular highlight }
end {_D3DMATERIAL8};
So you can't compare record directly in Delphi. And compiler is right. One of possible quick solution is to use:
[background=#FFFFFF][normal=#000000][number=#0000FF][string=#0000FF][comment=#248F24][reserved=#000000]if CompareMem(@mat1, @mat2, SizeOf(mat1)) = 0 then // they are equal memory wise
But this can be correct in your case but incorrect in general as colors represented by floats and nobody should compare float by "=" operator.
Ideal solutions should be:
[background=#FFFFFF][normal=#000000][number=#0000FF][string=#0000FF][comment=#248F24][reserved=#000000]function CompareMaterials(m1, m2: TD3ddMaterial): Boolean;
const
Eps = 0.00001;
function CompareColors(c1, c2: TD3DColorValue): Boolean;
begin
Result:= (Abs(c1.r - c2.r) < Eps) and (Abs(c1.g - c2.g) < Eps) and (Abs(c1.b - c2.b) < Eps) and (Abs(c1.a - c2.a) < Eps);
end
begin
Result:= CompareColors(m1.Diffuse, m2.Diffuse) and ....
and (Abs(m1.Power - m2.Power) < Eps);
end;

XeviaN
15-06-2003, 10:03 AM
I'm glad that i was not so wrong... :wink:

Thank you! I like your solution :D