Custom shader for selection

Hello here.
I'm trying to write a custom shader to display selected faces of drawn shapes through the drawer retrieved by myInteractiveContext->HighlightStyle(Prs3d_TypeOfHighlight_LocalSelected).

I want to draw some kind of hatch pattern on selected faces through a shader program.
I'd like to draw diagonal lines that doesn't depends on the view orientation / zoom ...
I need to be able to change the hatch pattern color at any time.

In the "selface.png" attached picture, you can see what I'd like to achieve with the black color.

What I'm actually able to do can be seen in the "selface2.png" attached picture.

Here is how I could achieve this result.

public: void SetSelectionColor(float r, float g, float b)
{
    Graphic3d_ShaderObject vertexShader = Graphic3d_ShaderObject::CreateFromFile(Graphic3d_TypeOfShaderObject::Graphic3d_TOS_VERTEX, "D:\\vshader.glsl");
    Graphic3d_ShaderObject fragmentShader = Graphic3d_ShaderObject::CreateFromFile(Graphic3d_TypeOfShaderObject::Graphic3d_TOS_FRAGMENT, "D:\\fshader.glsl");

    Handle(Graphic3d_ShaderProgram) shaderProgram = new Graphic3d_ShaderProgram();
    shaderProgram->AttachShader(vertexShader);
    shaderProgram->AttachShader(fragmentShader );	
    shaderProgram->PushVariableVec4("selectionColor", Graphic3d_Vec4(r, g, b, 1));

    Handle(Prs3d_Drawer) drawer = myInteractiveContext->HighlightStyle(Prs3d_TypeOfHighlight_LocalSelected).
    drawer->ShadingAspect()->Aspect()->SetShaderProgram(shaderProgram);
}

The vertex shader file:

varying vec4 vertexPos;

void main()
{
    vertexPos = occProjectionMatrix * occWorldViewMatrix * occModelWorldMatrix * occVertex;
    gl_Position = vertexPos;
}

The fragment shader file:

varying vec4 vertexPos;
uniform vec4 selectionColor;

void main()
{
    if (floor(mod(aPosition.x * 1000, 10)) == 1)
    {
        occFragColor = selectionColor;
    }
    else
    {
        discard;
    }
}

The problems I encounter here is that:

1 - I need to pass the vertexPos variable from the vertex shader to the fragment shader because, I don't know why, I can't access to any "occ" variables (occVertex / occProjectionMatrix ...). Is there something I'm missing ?

1 - I don't know how to retrieve screen coordinates of each vertex to draw lines correctly and regardless of the camera eye. Do anyone has a tip for me ?

2 - If I call the method, then select a face, the selection performs like in the "selface2.png", OK, but if I enter the method to change the selection color, the lines color are not updated and, whereas I unselect and reselect this face, it will always be filled using the previous color. If a select another face, NP, the color is effective in the shader program. What am I missing here ?

Thank you for those who can answer to me :)

Attachments: 
Guillaume CHAFFAROD's picture

sorry, I went to fast to post the message. in the fragment shader, it's not aPosition but vertexPos.
In fact, I'm really near of what I want to do, the only thing is the update color problem...

Guillaume CHAFFAROD's picture

nevermind, everything is now working perfectly ^^