How to convert back vertices to GLSL when creating shadow volume

I am writing a game using OpenGL and I am trying to implement shadow volumes.

I want to build the shadow volume of the model on the GPU through the vertex shader. To this end, I present a model with VBO, where:

  • The vertices are duplicated so that each triangle has its own unique three vertices
  • Each vertex has a normal to its triangle.
    • For the reasons Iโ€™m not going to enter, I did these two points anyway, so I donโ€™t worry too much about duplicating vertices
  • Degenerate triangles are added to form squares inside the edges between each pair of "regular" triangles.

Using this model format, inside the vertex shader, I can find the vertices that are part of the triangles that are facing away from the light and move them back to form a shadow volume.

What I left to figure out which transformation I should apply to the back vertices.

I can detect when the vertex is facing away from the light, but I'm not sure which transformation I should apply to it. This is what my vertex shader looks like:

uniform vec3 lightDir; // Parallel light.
                       // On the CPU this is represented in world
                       // space.  After setting the camera with
                       // gluLookAt, the light vector is multiplied by
                       // the inverse of the modelview matrix to get
                       // it into eye space (I think that what I'm
                       // working in :P ) before getting passed to
                       // this shader.

void main()
{
    vec3 eyeNormal = normalize(gl_NormalMatrix * gl_Normal);
    vec3 realLightDir = normalize(lightDir);

    float dotprod = dot(eyeNormal, realLightDir);

    if (dotprod <= 0.0)
    {
        // Facing away from the light
        // Need to translate the vertex along the light vector to
        // stretch the model into a shadow volume

        //---------------------------------//
        // This is where I'm getting stuck //
        //---------------------------------//
        // All I know is that I'll probably turn realLightDir into a
        // vec4

        gl_Position = ???;
    }
    else
    {
        gl_Position = ftransform();
    }
}

I tried just setting it gl_positionon ftransform() - (vec4(realLightDir, 1.0) * someConstant), but it caused some depth checking errors (some faces seemed to be visible behind others when I displayed the volume with color) and someConstantdidn't seem to affect how far back faces expand.

Update - January 22

, , , . , , , .

, gluLookAt. ; . , glTranslated, ().

(.. ) ( ). , . , :

  • (gluLookAt)
  • , ,

- ?

+3
1

ftransform -. , , realLightDir. , ( ), , , .

. gluLookAt, , ( , : P), .

mv, . , - ( ), , view- > model. .

4 :

  • : , gl_Vertex.
  • : , GL , . , 3D- ( ).
  • view space: , . 0,0,0 - , Z. gl_Vertex
  • : , . ftransform ( gl_ModelViewProjectionMatrix * gl_Vertex)

, ?

, : . . CPU :

vec3 vertexTemp = gl_Vertex + lightDirInModelSpace * someConst

gl_Position = gl_ModelViewProjectionMatrix * vertexTemp

, -. , , , , w.

+1

Source: https://habr.com/ru/post/1729488/


All Articles