0
\$\begingroup\$

I implemented lightmap creation for a tilemap, but I cannot figure out how do I interpolate light intensity values. For now, my lighted tiles create an obvious grid-like structure. It was pretty easy to interpolate via GLSL fragment shader because I could control every fragment of a mesh and therefore calculate the intensity using distance between fragment and light source. Downside is that shader solution was limited because of inability to process changing quantity of light sources.

Since a tile layer has one mesh for all tiles, I am not able to control fragment set of exact tile. I create the lightmap using positions of each tile, I then calculate the intensity using the distance between the tile and the light source. Therefore the whole tile is lighted with a single light intensity value.

I also create a single mesh for a whole lightmap. I need some kind of control over color of tiles' fragments, but I cannot create a separate mesh for each tile. Maybe is there any technique to mix/interpolate colors of adjacent fragments?

Current fragment shader code for lightmap:

in vec4 vColor;
out vec4 fColor;

uniform float alpha;

void main(void)
{
    fColor = vColor;
    fColor.a = alpha;
}

How do I interpolate light intensity of my light map?

EDIT:

I managed to render Lightmap to a texture using the Framebuffer. (I basically used another render target while drawing the Lightmap as a set of colored tiles) I then set the texture's parameters like this:

setParameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR) 
setParameter(GL_TEXTURE_MAG_FILTER, GL_LINEAR)

Shader is pretty simple:

in vec2 fragmentUV;
out vec4 fColor;
uniform sampler2D textureSample;

void main(void)
{
    fColor = texture(textureSample, fragmentUV).rgba;

    if (fColor.a <= 0){
        discard;
    }
}
\$\endgroup\$
7
  • \$\begingroup\$ Normally the term "lightmap" is used to describe a texture. Storing your lighting in a texture would let you get bilinear interpolation basically for free, and you could write custom sampling code if you want a different filter. \$\endgroup\$
    – DMGregory
    Commented Mar 31 at 21:05
  • \$\begingroup\$ @DMGregory Isn't it inefficient if light sources are moving during runtime? \$\endgroup\$
    – Steyrix
    Commented Apr 1 at 7:57
  • \$\begingroup\$ What would be less efficient about storing this data in a texture? Games repaint textures every frame all the time: not just the game view you see, but all kinds of off-screen render targets like shadow maps, masks, reflection cube maps, etc. \$\endgroup\$
    – DMGregory
    Commented Apr 1 at 10:50
  • \$\begingroup\$ @DMGregory I managed to render Lightmap to a texture, however I still don't know how do I get it interpolated. It pretty much looks the same \$\endgroup\$
    – Steyrix
    Commented Apr 2 at 9:37
  • \$\begingroup\$ For the lightmap texture, you want to render it to a scale where each tile is only one or two pixels across, so you're rendering as little as possible, and so it gets interpolated as it's upscaled to tile size in the lighting sampling. \$\endgroup\$
    – DMGregory
    Commented Apr 2 at 10:07

1 Answer 1

1
\$\begingroup\$

I managed to get it working as I wanted. Thanks to DMGregory's advices. The key point was to render the lightmap to a texture, which should be significantly smaller than the visible scene's size. The texture's fragments get interpolated as it upscales.

For my case I used OpenGL framebuffer object to which I bound the generated texture reference. I then draw the downscaled mesh of the lightmap to FBO. Therefore the resulting texture was significantly small. I then upscaled it to the scene's size and drew it over the scene - that did the trick.

The original Lightmap mesh was generated as a set of tiles with different shades of gray as their colors, brightnesses of which were depending on the distance between them and the light source.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Great work solving it! \$\endgroup\$
    – DMGregory
    Commented Apr 2 at 11:12
  • \$\begingroup\$ @DMGregory can I chat with you about light intensity calculation or should I post another question for that? I feel like I posted a plenty recently \$\endgroup\$
    – Steyrix
    Commented Apr 4 at 10:21
  • \$\begingroup\$ You're welcome to post in Game Development Chat if you don't want to jump straight to a new question post. \$\endgroup\$
    – DMGregory
    Commented Apr 4 at 11:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .