how to combine shadows + reflections in frag shader?

edited April 2018 in GLSL / Shaders

To put it as simply as possible, I'm modifying and recompiling PixelFlow's render.frag and render.vert files to allow me to add cubemap-based reflections to PixelFlow's skylight renderering system. i.e. I want to get a "composite" of the reflection info and the shadow info in a single vec3().

I am able to isolate each in the fragment shader and they render perfectly. You can see below:

0b 0a

But when I attempt to mix/multiply/add them together I get an "opengl error 1282 at top endDraw(): invalid operation."

render.frag:

#version 150

#extension GL_NV_shadow_samplers_cube : enable

in vec4 vertColor;
in vec4 vertPosition;
in vec3 vertNormal;
in vec3 reflectDir;

out vec4 glFragColor;

uniform mat4 projection;
uniform mat4 modelview;
uniform mat3 normalMatrix;
uniform mat4 transform;

// these uniforms are set here: https://github.com/diwi/PixelFlow/blob/master/src/com/thomasdiewald/pixelflow/java/render/skylight/DwSkyLightRenderer.java
uniform vec2 wh; 
uniform vec3 mult_sun = vec3(1.0);
uniform vec3 mult_sky = vec3(1.0);    
uniform sampler2D tex_sky; 
uniform sampler2D tex_sun;

// this uniform is set in processing sketch
uniform samplerCube cubemap;

void main(void) {
    vec3 reflectedFlipped = vec3(reflectDir.x, -reflectDir.y, reflectDir.z);
    vec4 reflectedColor = textureCube(cubemap, normalize(reflectedFlipped));

    vec2 fragCoordn = (gl_FragCoord.xy)/wh;
    vec3 shading_SKY = texture(tex_sky, fragCoordn).r * mult_sky;
    vec3 shading_SUN = texture(tex_sun, fragCoordn).r * mult_sun;

  //  vec3 outColor = reflectedColor.xyz; // <--- the reflection works by itself
  //  vec3 outColor = (shading_SUN + shading_SKY); // <--- the shadows works by itself
   vec3 outColor = reflectedColor.xyz * (shading_SUN + shading_SKY); // <--- the problem arises here, when combining the reflection and shadows. i get the 1282 error!!!

    glFragColor = vec4(outColor, 1);
}

render.vert:

#version 150

uniform mat4 projection;
uniform mat4 modelview;
uniform mat3 normalMatrix;
uniform mat4 transform;

in vec4 vertex;
in vec4 color;
in vec3 normal;

out vec4 vertColor;
out vec4 vertPosition;
out vec3 vertNormal;
out vec3 reflectDir;

void main() {
    vertNormal = normal;
    gl_Position = transform * vertex;

    vec3 ecNormal = normalize(normalMatrix * normal);
    vec3 ecVertex = vec3(modelview * vertex);
    vec3 eyeDir = ecVertex.xyz;

    reflectDir = reflect(eyeDir, ecNormal);
}

how I set up the cubemap:

PGL pgl = beginPGL();
IntBuffer envMapTextureID = IntBuffer.allocate(1);
pgl.genTextures(1, envMapTextureID);
pgl.activeTexture(PGL.TEXTURE1);
pgl.enable(PGL.TEXTURE_CUBE_MAP);
pgl.bindTexture(PGL.TEXTURE_CUBE_MAP, envMapTextureID.get(0));

String[] textureNames = { 
    "posx.jpg", "negx.jpg", "posy.jpg", "negy.jpg", "posz.jpg", "negz.jpg"
};

PImage[] textures = new PImage[textureNames.length];
for (int i=0; i<textures.length; i++) {
    textures[i] = loadImage("/data/cubemap/" + textureNames[i]);
}

for (int i=0; i<textures.length; i++) {
    int w = textures[i].width;
    int h = textures[i].height;
    textures[i].loadPixels();
    int[] pix = textures[i].pixels;
    int[] rgbaPixels = new int[pix.length];
    for (int j = 0; j< pix.length; j++) {
        int pixel = pix[j];
        rgbaPixels[j] = 0xFF000000 | ((pixel & 0xFF) << 16) | ((pixel & 0xFF0000) >> 16) | (pixel & 0x0000FF00);
    }
    pgl.texImage2D(PGL.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, PGL.RGBA, w, h, 0, PGL.RGBA, PGL.UNSIGNED_BYTE, java.nio.IntBuffer.wrap(rgbaPixels));
}
pgl.texParameteri(PGL.TEXTURE_CUBE_MAP, PGL.TEXTURE_WRAP_S, PGL.CLAMP_TO_EDGE);
pgl.texParameteri(PGL.TEXTURE_CUBE_MAP, PGL.TEXTURE_WRAP_T, PGL.CLAMP_TO_EDGE);
pgl.texParameteri(PGL.TEXTURE_CUBE_MAP, PGL.TEXTURE_WRAP_R, PGL.CLAMP_TO_EDGE);
pgl.texParameteri(PGL.TEXTURE_CUBE_MAP, PGL.TEXTURE_MIN_FILTER, PGL.LINEAR);
pgl.texParameteri(PGL.TEXTURE_CUBE_MAP, PGL.TEXTURE_MAG_FILTER, PGL.LINEAR);

endPGL();

. . .

shader.set("cubemap", 1);

Any ideas on why I get the 1282 error and how to fix it? Or recommendations on how to debug? My first thought is that it has something to do with texture units...

Answers

  • edited April 2018

    ok I figured it out. was definitely a texture unit problem. I needed to change PGL.TEXTURE1 to PGL.TEXTURE2, and shader.set("cubemap",1) to shader.set("cubemap",2)

Sign In or Register to comment.