什么是不变0.0039215689重新present? [英] What does the constant 0.0039215689 represent?

查看:145
本文介绍了什么是不变0.0039215689重新present?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直看到这种不断弹出各种图形头文件

I keep seeing this constant pop up in various graphics header files

0.0039215689

这似乎有些事情要与颜色也许?

It seems to have something to do with color maybe?

下面是href=\"http://ozmav.google$c$c.com/svn-history/r178/trunk/badrdp/rdp.c\">打在谷歌第一

Here is the first hit on Google:

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);
    }
}

//...more like this

这是什么号码重present?为什么没人似乎将其声明为一个const?

What does this number represent? Why does no one seem to declare it as a const?

我无法在谷歌找到任何解释了。

I couldn't find anything on Google that explained it.

推荐答案

0.0039215689 约等于 1/255

看到这是OpenGL的性能可能是很重要的。因此,这可能是安全的猜测,这是为性能方面的原因做了。

Seeing that this is OpenGL, performance is probably important. So it's probably safe to guess that this was done for performance reasons.

由倒数乘以比由255分多次更快。

Multiplying by the reciprocal is faster than repeatedly dividing by 255.

侧面说明:

如果你想知道为什么这样的微型优化是不是留给编译器,这是因为它是一个不安全的浮点优化。换句话说:

If you're wondering why such a micro-optimization isn't left to the compiler, it's because it is an unsafe floating-point optimization. In other words:

x / 255  !=  x * (1. / 255)

由于浮点舍入误差。

due to floating-point round-off errors.

因此​​,虽然现代的编译器可能是足够聪明,这样做优化,他们不允许这样做,除非你明确地告诉他们要通过编译器标志。

So while modern compilers may be smart enough to do this optimization, they are not allowed to do it unless you explicitly tell them to via a compiler flag.

相关: 为什么不GCC优化A * A * A * A * A * A到(A * A * A)*(A * A * A)?

这篇关于什么是不变0.0039215689重新present?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆