Login  Register

Re: Porting a depth peeling example to modern OpenGL

Posted by elect on Oct 11, 2013; 6:47am
URL: https://forum.jogamp.org/Porting-a-depth-peeling-example-to-modern-OpenGL-tp4030224p4030229.html

Lili wrote
I am not sure I am understanding the questions but

1. Yes, gl_TexCoord[] is deprecated
2. I see that you have 5 pairs of vertex and fragment shaders. What you probably want to do is create 5 shader programs each one executing one of the shader pairs and then switch among programs to use the one you need at the time. Each shader program (vertex shader) should accept its own uniform variables and they may be different than what the other programs accept.

I hope this is somewhat helpful. Maybe I can help you further if I better understand the question.
Ok, lets make a step per time.

Starting with the first program, originally it was based on the following VSs and FSs

init_VS:

vec3 ShadeVertex();

void main(void)
{
        gl_Position = ftransform();
        gl_TexCoord[0].xyz = ShadeVertex();
}


shade_VS:

vec3 ShadeVertex()
{
        float diffuse = abs(normalize(gl_NormalMatrix * gl_Normal).z);
        return vec3(gl_Vertex.xy, diffuse);
}


init_FS:

vec4 ShadeFragment();

void main(void)
{
        vec4 color = ShadeFragment();
        gl_FragColor = vec4(color.rgb * color.a, 1.0 - color.a);
}


shade_FS:

uniform float Alpha;

#define COLOR_FREQ 30.0
#define ALPHA_FREQ 30.0

#if 1
vec4 ShadeFragment()
{
        float xWorldPos = gl_TexCoord[0].x;
        float yWorldPos = gl_TexCoord[0].y;
        float diffuse = gl_TexCoord[0].z;

        vec4 color;
        float i = floor(xWorldPos * COLOR_FREQ);
        float j = floor(yWorldPos * ALPHA_FREQ);
        color.rgb = (fmod(i, 2.0) == 0) ? vec3(.4,.85,.0) : vec3(1.0);
        //color.a = (fmod(j, 2.0) == 0) ? Alpha : 0.2;
        color.a = Alpha;

        color.rgb *= diffuse;
        return color;
}
#else
vec4 ShadeFragment()
{
        vec4 color;
        color.rgb = vec3(.4,.85,.0);
        color.a = Alpha;
        return color;
}
#endif


I partially modified them.

Here I introduced custom view and projection matrixes using UBO and declared the position attribute. I dont know what to do with the gl_TexCoord[]..

init_VS:

layout (location = 0) in vec4 position;

layout(std140) uniform mvpMatrixes
{
    mat4 projectionMatrix;
    mat4 viewMatrix;
};

vec3 ShadeVertex();

void main(void)
{
        gl_Position = projectionMatrix * viewMatrix * position;
        gl_TexCoord[0].xyz = ShadeVertex();
}


The shade_VS uses also deprecated values, should I declare my custom normalMatrix and normal attribute there or in the init_VS?

Regarding the init_FS, it should be pretty easy, just declaring the output color, substituting the deprecated gl_FragColor

vec4 ShadeFragment();

out vec4 outputColor;

void main(void)
{
        vec4 color = ShadeFragment();
        outputColor = vec4(color.rgb * color.a, 1.0 - color.a);
}


I dont know instead what to do with the shade_FS, I could modified the #if in order to execute the second branch, that appears to be easier, as follows

uniform float Alpha;

#define COLOR_FREQ 30.0
#define ALPHA_FREQ 30.0

#if 0
vec4 ShadeFragment()
{
        float xWorldPos = gl_TexCoord[0].x;
        float yWorldPos = gl_TexCoord[0].y;
        float diffuse = gl_TexCoord[0].z;

        vec4 color;
        float i = floor(xWorldPos * COLOR_FREQ);
        float j = floor(yWorldPos * ALPHA_FREQ);
        color.rgb = (fmod(i, 2.0) == 0) ? vec3(.4,.85,.0) : vec3(1.0);
        //color.a = (fmod(j, 2.0) == 0) ? Alpha : 0.2;
        color.a = Alpha;

        color.rgb *= diffuse;
        return color;
}
#else
vec4 ShadeFragment()
{
        vec4 color;
        color.rgb = vec3(.4,.85,.0);
        color.a = Alpha;
        return color;
}
#endif