What does the constant 0.0039215689 do in the graphic header?

Asked 2 years ago, Updated 2 years ago, 110 views

0.0039215689 is used a lot in the graphic header file What's the point?

Why do you always write 0.0039215689 without setting it to const?

If you look at the first code on Google (here )

void RDP_G_SETFOGCOLOR(void)
{
    Gfx.FogColor.R = _SHIFTR(w1, 24, 8) * 0.0039215689f;
    Gfx.FogColor.G = _SHIFTR(w1, 16, 8) * 0.0039215689f;
    Gfx.FogColor.B = _SHIFTR(w1, 8, 8) * 0.0039215689f;
    Gfx.FogColor.A = _SHIFTR(w1, 0, 8) * 0.0039215689f;
}

void RDP_G_SETBLENDCOLOR(void)
{
    Gfx.BlendColor.R = _SHIFTR(w1, 24, 8) * 0.0039215689f;
    Gfx.BlendColor.G = _SHIFTR(w1, 16, 8) * 0.0039215689f;
    Gfx.BlendColor.B = _SHIFTR(w1, 8, 8) * 0.0039215689f;
    Gfx.BlendColor.A = _SHIFTR(w1, 0, 8) * 0.0039215689f;

    if(OpenGL.Ext_FragmentProgram && (System.Options & BRDP_COMBINER)) {
        glProgramEnvParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 2, Gfx.BlendColor.R, Gfx.BlendColor.G, Gfx.BlendColor.B, Gfx.BlendColor.A);
    }
}
// and so on...

c floatig-point magic-number constant

2022-09-22 22:22

1 Answers

0.0039215689 is close to 1/255.

Graphic code such as OpenGL values performance It's faster to multiply the reciprocal than to divide by 255 That's how you write it.

Usually, this optimization is left to the compiler, but the programmer seems to have done it separately because errors such as round-off errors can occur at floating points.

For related questions, GCC does not optimize a*a*a*a*a*a*a to (a*a*a)*(a*a*a).Please see .


2022-09-22 22:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.