动态 2d 阴影 - 混合问题

发布于 2024-11-06 14:57:39 字数 1065 浏览 0 评论 0原文

美好的一天,我亲爱的社区。

我正在为我将要开发的游戏开发动态阴影,但正如通常发生的那样,我给你带来了一个问题,希望(我实际上确信)有人会提供帮助。

这就是我现在所在的位置:在此处输入图像描述

注意红色方块,我希望它随着光源移出视线。我确实检查多边形的点是否在圆的半径内,但这当然不能解决问题;正如我所说,如果光线太远,我希望它逐渐消失,直到完全变黑。

我脑子里只有一个想法,但我希望有更好的想法。我不会谈论它,因为它确实是最后一个选择,而且我发现它是一种“蛮力”技术。

这就是我渲染光线的方式:

    glBegin(GL_TRIANGLE_FAN);
    {
        Graphics::Instance()->SetColor(r_,g_,b_,intensity_);
        glVertex2f(posX_,posY_);

        glColor4f(0.f, 0.f, 0.f, 0.0f);

        for (angle_=0.0; angle_<=3.14159265*2; angle_+=((3.14159265*2)/64.0f) )
        {
            glVertex2f(range_*(float)cos(angle_) + posX_,
                       range_*(float)sin(angle_) + posY_);
        }

        glVertex2f(posX_+range_, posY_);
    }

这就是我混合光线的方式:

glBlendFunc(GL_SRC_ALPHA, GL_ONE);
l0->Render();

glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
l0->ProjectShadow(*mmm);
l0->ProjectShadow(*bb);

仅此而已。如果我没有说清楚或者如果我错过发布相关代码,请说出来并且不要投票。

Good day my dear community.

I'm working on dynamic shadows for a game I shall work on, but as it usually happens I bring you a problem, in hope (I'm certain actually) that someone will help.

This is where I am right now:enter image description here

Notice the red square, I want it to gradually fade away as the light source moves out of the sight. I do check if a point of a polygon is inside circle's radius, but that of course doesn't solve it; as I said I want it to fade gradually until it completely blacks out If the light is too far away.

There's one idea on my mind but I hope for a better one. I will not talk about it since it's really the last option and I find it to be a 'brute force' technique.

This is how I render my light:

    glBegin(GL_TRIANGLE_FAN);
    {
        Graphics::Instance()->SetColor(r_,g_,b_,intensity_);
        glVertex2f(posX_,posY_);

        glColor4f(0.f, 0.f, 0.f, 0.0f);

        for (angle_=0.0; angle_<=3.14159265*2; angle_+=((3.14159265*2)/64.0f) )
        {
            glVertex2f(range_*(float)cos(angle_) + posX_,
                       range_*(float)sin(angle_) + posY_);
        }

        glVertex2f(posX_+range_, posY_);
    }

And this is how I blend it:

glBlendFunc(GL_SRC_ALPHA, GL_ONE);
l0->Render();

glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
l0->ProjectShadow(*mmm);
l0->ProjectShadow(*bb);

That is all. If I didn't made myself clear or If I missed to post relevant code, please do say so and don't downvote.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天涯沦落人 2024-11-13 14:57:39

计算从光源中心到红色方块中心的范围怎么样?将该值标准化到合适的范围并调整红色方块的透明度或颜色?像这样的事情:

double Range(double x1, double y1, double x2, double y2)
{
    double xDist = x1-x2;
    double yDist = y1-y2;
    return math::sqrt(xDist*xDist+yDist*yDist);
}

double CalcIntensity(double lightX, double lightY, double lightRadius, double objectX, double objectY)
{
    double range = Range(lightX, lightY, objectX, objectY);
    double intensity;
    if( range > lightRadius )
    {
        intensity = 0.0;
    }
    else
    {
        intensity = range/lightRadius;
    }
    return intensity;
}

然后只需调用 CalcIntensity 并输入灯光和正方形的相对位置以及灯光的半径。

[编辑] ...或者,如果您没有预先检查它是否在灯光半径内,这将是一个稍微更优化的版本:

double CalcIntensity(double lightX, double lightY, double lightRadius, double objectX, double objectY)
{
    double intensity = 0.0;
    double xDist = lightX-objectX;
    if( xDist < lightRadius )
    {
        yDist = lightY-objectY;
        if( yDist < lightRadius )
        {
            double range = math::sqrt(xDist*xDist+yDist*yDist);
            intensity = range/lightRadius;
        }
    }

    return intensity;
}

How about calculating the range to the center of your red square from the light sources center? Normalise that value to a suitable range and adjust the transparency or colour of the red square? Something like this:

double Range(double x1, double y1, double x2, double y2)
{
    double xDist = x1-x2;
    double yDist = y1-y2;
    return math::sqrt(xDist*xDist+yDist*yDist);
}

double CalcIntensity(double lightX, double lightY, double lightRadius, double objectX, double objectY)
{
    double range = Range(lightX, lightY, objectX, objectY);
    double intensity;
    if( range > lightRadius )
    {
        intensity = 0.0;
    }
    else
    {
        intensity = range/lightRadius;
    }
    return intensity;
}

Then just call CalcIntensity and feed in the reletive positions of the light and the square and the radius of the light.

[Edit] ...or this would be a slightly more optomised version if you're not pre-checking it's within the lights radius:

double CalcIntensity(double lightX, double lightY, double lightRadius, double objectX, double objectY)
{
    double intensity = 0.0;
    double xDist = lightX-objectX;
    if( xDist < lightRadius )
    {
        yDist = lightY-objectY;
        if( yDist < lightRadius )
        {
            double range = math::sqrt(xDist*xDist+yDist*yDist);
            intensity = range/lightRadius;
        }
    }

    return intensity;
}
天冷不及心凉 2024-11-13 14:57:39

那么,光线在 posX,posY 处处于全亮度,在 range 处完全“耗尽”(即黑色)。之间的值是线性插值的。因此,距光源距离 d 的任何位置上的点都会被 rgb * (d/range) 照亮。

如果您现在计算红色方块的每个顶点 v_i 的距离 d_i,您可以通过乘以 c_i 将阴影应用于每个顶点颜色 c_i >(d_i/range) 到它。如果您希望整个正方形以相同的颜色显示,而不管距离较远的顶点如何,只需使用每个顶点的中心距离,即在绘制正方形之前仅设置一次颜色。

Well the light is at full brightness at posX,posY and fully "depleted" (i.e. black) at range. The values between are interpolated linearly. Therefore a point at any position with distance d from the light source is lit by rgb * (d/range).

If you now calculate the distances d_i of each vertex v_i of your red square, you can apply shadowing to each vertex color c_i by multiplying (d_i/range) to it. If you want the whole square to appear in the same color regardless of further-away vertices just use the distance of its center for every vertex, that is set the color only once before you draw the square.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文