ands / lightmapper

A C/C++ single-file library for drop-in lightmap baking. Just use your existing OpenGL renderer to bounce light!
1.44k stars 134 forks source link

Small request for help - lightmapper in Qt #9

Closed ppiecuch closed 7 years ago

ppiecuch commented 7 years ago

Small request for help ... :)

I am trying to port/run lightmapper in Qt with OpenGL2. It started to work but results are broken - maybe you could point me where to start with this problem - what could cause these artifacts?

Thank you in advance Pawel

Results:

result debug_interpolation

and shaders:

// hemisphere shader (weighted downsampling of the 3x1 hemisphere layout to a 0.5x0.5 square)
{
    const char *vs =
        "#version 120\n"

        "#extension GL_EXT_gpu_shader4 : require\n"

        "const vec2 ps[4] = vec2[](vec2(1, -1), vec2(1, 1), vec2(-1, -1), vec2(-1, 1));\n"
        "void main()\n"
        "{\n"
            "gl_Position = vec4(ps[gl_VertexID], 0, 1);\n"
        "}\n";
    const char *fs =
        "#version 120\n"

        "#extension GL_EXT_gpu_shader4 : require\n"

        "uniform sampler2D hemispheres;\n"
        "uniform sampler2D weights;\n"
        "uniform ivec2 weightsTextureSize;\n"

        **"vec4 texelFetch(sampler2D tex, ivec2 size, ivec2 coord)\n"
        "{\n"
        "    vec2 fCoord = vec2((2.0*coord.x + 1.0)/(2.0*float(size.x)),(2.0*coord.y + 1.0)/(2.0*float(size.y)));\n"
        "    return texture2D(tex, fCoord);\n"
        "}\n"**

        "vec4 weightedSample(ivec2 h_uv, ivec2 w_uv, ivec2 quadrant)\n"
        "{\n"
            "vec4 sample = texelFetch(hemispheres, h_uv + quadrant, ivec2(0));\n"
            "vec2 weight = texelFetch(weights, w_uv + quadrant, ivec2(0)).rg;\n"
            "return vec4(sample.rgb * weight.r, sample.a * weight.g);\n"
        "}\n"

        "vec4 threeWeightedSamples(ivec2 h_uv, ivec2 w_uv, ivec2 offset)\n"
        "{\n" // horizontal triple sum
            "vec4 sum = weightedSample(h_uv, w_uv, offset);\n"
            "offset.x += 2;\n"
            "sum += weightedSample(h_uv, w_uv, offset);\n"
            "offset.x += 2;\n"
            "sum += weightedSample(h_uv, w_uv, offset);\n"
            "return sum;\n"
        "}\n"

        "void main()\n"
        "{\n" // this is a weighted sum downsampling pass (alpha component contains the weighted valid sample count)
            "vec2 in_uv = (gl_FragCoord.xy - vec2(0.5)) * vec2(6.0, 2.0) + vec2(0.01);\n"
            "ivec2 h_uv = ivec2(in_uv);\n"
            "ivec2 w_uv = ivec2(mod(in_uv, vec2(weightsTextureSize)));\n" // there's no integer modulo :(
            "vec4 lb = threeWeightedSamples(h_uv, w_uv, ivec2(0, 0));\n"
            "vec4 rb = threeWeightedSamples(h_uv, w_uv, ivec2(1, 0));\n"
            "vec4 lt = threeWeightedSamples(h_uv, w_uv, ivec2(0, 1));\n"
            "vec4 rt = threeWeightedSamples(h_uv, w_uv, ivec2(1, 1));\n"
            "gl_FragColor = lb + rb + lt + rt;\n"
        "}\n";
ppiecuch commented 7 years ago

Sorry for the noise, seems like I missed quite a few pieces about texelFetch implementation. Look like it works now.

ands commented 7 years ago

Hey. :) Sorry if I ask, but what is the reason for using OpenGL2? So... did you get it running/can this be closed now?

ppiecuch commented 7 years ago

Hello, yes it works now. It is just easier to integrated the code with rest of the Qt, that is OGL2 based. Pawel