Robię outline sześcianów z learnopengl https://learnopengl.com/Advanced-OpenGL/Stencil-testing.

Kostka, która robi zarys powinna mieć odrzucone fragmenty w miejscu gdzie znajduje się kostka z teksturą. Tak się jednak nie dzieje. Kostka, która robi zarys pokrywa cały sześcian teksturowany.

static const char* cubeVtx = R"(
#version 460 core
layout (location = 0) in vec3 a_position;
layout (location = 1) in vec2 a_uv;

out vec2 v_uv;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main()
{
    v_uv = a_uv;    
    gl_Position = projection * view * model * vec4(a_position, 1.0f);
}

)";

static const char* cubeFrag = R"(
#version 460 core

layout (location = 0) out vec4 f_color;

in vec2 v_uv;

uniform sampler2D u_texture1;

void main()
{    
    f_color = texture(u_texture1, v_uv);
}

)";

static const char* singleColorVtx = R"(
#version 460 core
layout (location = 0) in vec3 a_position;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main()
{   
    gl_Position = projection * view * model * vec4(a_position, 1.0f);
}

)";

static const char* singleColorFrag = R"(
#version 460 core

layout (location = 0) out vec4 f_color;

void main()
{    
    f_color = vec4(1.0f, 0.0f, 0.0f, 1.0f);
}

)";

void Cube::InitAPI()
{
    shader = Shader::Create();
    ShaderProgram program[2] = {
        shader->ShaderFromSource(cubeVtx, ShaderType::Vertex),
        shader->ShaderFromSource(cubeFrag, ShaderType::Fragment)
    };
    shader->End(program, 2);

    singleColorShader = Shader::Create();
    ShaderProgram singleColorProgram[2] = {
        singleColorShader->ShaderFromSource(singleColorVtx, ShaderType::Vertex),
        singleColorShader->ShaderFromSource(singleColorFrag, ShaderType::Fragment)
    };
    singleColorShader->End(singleColorProgram, 2);

    glCreateVertexArrays(1, &vao);
    glBindVertexArray(vao);
    glCreateBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glNamedBufferData(vbo, sizeof(cubeVertices), cubeVertices, GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(0, 3, GL_FLOAT, false, 5 * sizeof(float), 0);
    glVertexAttribPointer(1, 2, GL_FLOAT, false, 5 * sizeof(float), (void*)(3 * sizeof(float)));

    glCreateVertexArrays(1, &planeVao);
    glBindVertexArray(planeVao);
    glCreateBuffers(1, &planeVbo);
    glBindBuffer(GL_ARRAY_BUFFER, planeVbo);
    glNamedBufferData(planeVbo, sizeof(planeVertices), planeVertices, GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(0, 3, GL_FLOAT, false, 5 * sizeof(float), 0);
    glVertexAttribPointer(1, 2, GL_FLOAT, false, 5 * sizeof(float), (void*)(3 * sizeof(float)));

    texture = Texture::LoadPNG("Media/container2");
    planeTexture = Texture::LoadPNG("Media/matrix");

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);
    glEnable(GL_STENCIL_TEST);
    glStencilFunc(GL_NOTEQUAL, 1, 0xFF);
    glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);

    shader->UseProgram();
    shader->Uniform1i("u_texture1", 0);
}

void Cube::Render()
{
    singleColorShader->UseProgram();
    glm::mat4 model = glm::mat4(1.0f);
    glm::mat4 view = camera.GetViewMatrix();
    glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)1280.0f / (float)720.0f, 0.1f, 100.0f);
    singleColorShader->UniformMatrix4fv("view", view);
    singleColorShader->UniformMatrix4fv("projection", projection);

    shader->UseProgram();
    shader->UniformMatrix4fv("view", view);
    shader->UniformMatrix4fv("projection", projection);

    glStencilMask(0x00);
    glBindVertexArray(planeVao);
    planeTexture->Active();
    planeTexture->Bind();
    glDrawArrays(GL_TRIANGLES, 0, 6);

    glStencilFunc(GL_ALWAYS, 1, 0xFF);
    glStencilMask(0xFF);

    glBindVertexArray(vao);
    model = glm::translate(model, glm::vec3(-1.0f, 0.0f, -1.0f));
    shader->UniformMatrix4fv("model", model);
    texture->Active();
    texture->Bind();
    glDrawArrays(GL_TRIANGLES, 0, 36);

    model = glm::mat4(1.0f);
    model = glm::translate(model, glm::vec3(2.0f, 0.0f, 0.0f));
    shader->UniformMatrix4fv("model", model);
    glDrawArrays(GL_TRIANGLES, 0, 36);

    glStencilFunc(GL_NOTEQUAL, 1, 0xFF);
    glStencilMask(0x00);
    glDisable(GL_DEPTH_TEST);
    singleColorShader->UseProgram();
    float scale = 1.1;

    glBindVertexArray(vao);
    model = glm::mat4(1.0f);
    model = glm::translate(model, glm::vec3(-1.0f, 0.0f, -1.0f));
    model = glm::scale(model, glm::vec3(scale, scale, scale));
    shader->UniformMatrix4fv("model", model);
    glDrawArrays(GL_TRIANGLES, 0, 36);

    model = glm::mat4(1.0f);
    model = glm::translate(model, glm::vec3(2.0f, 0.0f, 0.0f));
    model = glm::scale(model, glm::vec3(scale, scale, scale));
    shader->UniformMatrix4fv("model", model);
    glDrawArrays(GL_TRIANGLES, 0, 36);

    glBindVertexArray(0);

    glStencilMask(0xFF);
    glEnable(GL_DEPTH_TEST);
}

InitAPI jest wykonywane raz, Render() w pętli gry. Depth, stencil i color buffer jest czyszczony przed wywołaniem Render(), w innym miejscu, nie bezpośrednio w ten metodzie.

Nadal tak średnio rozumiem ten cały depth buffer/test, a wreszcie kiedy już myślałem, że rozumiem stencil buffer/test, okazało się, że jednak tak nie do końca, bo nie działa.
Wygląda to tak:
screenshot-20190626120730.png

Powinno być tak, że czerwone to tylko obrys.

Czy mógłby ktoś mi wyjaśnić jak dla idioty, dlaczego fragmenty nie są odrzucane gdy stencil buffer, ma wartość 1?