动态 2d 阴影 - 混合问题
美好的一天,我亲爱的社区。
我正在为我将要开发的游戏开发动态阴影,但正如通常发生的那样,我给你带来了一个问题,希望(我实际上确信)有人会提供帮助。
这就是我现在所在的位置:
注意红色方块,我希望它随着光源移出视线。我确实检查多边形的点是否在圆的半径内,但这当然不能解决问题;正如我所说,如果光线太远,我希望它逐渐消失,直到完全变黑。
我脑子里只有一个想法,但我希望有更好的想法。我不会谈论它,因为它确实是最后一个选择,而且我发现它是一种“蛮力”技术。
这就是我渲染光线的方式:
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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
计算从光源中心到红色方块中心的范围怎么样?将该值标准化到合适的范围并调整红色方块的透明度或颜色?像这样的事情:
然后只需调用 CalcIntensity 并输入灯光和正方形的相对位置以及灯光的半径。
[编辑] ...或者,如果您没有预先检查它是否在灯光半径内,这将是一个稍微更优化的版本:
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:
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:
那么,光线在
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) atrange
. The values between are interpolated linearly. Therefore a point at any position with distanced
from the light source is lit byrgb * (d/range)
.If you now calculate the distances
d_i
of each vertexv_i
of your red square, you can apply shadowing to each vertex colorc_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.