Jump to content

  • Log In with Google      Sign In   
  • Create Account


[OPENGL] GLSL + CPP: point light shadow mapping example?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 fread   Members   -  Reputation: 138

Like
0Likes
Like

Posted 13 February 2014 - 02:54 PM

Hello guys

im looking for point light + shadow mapping example

preferrably with cubemap.

I have already a spotlight shadow mapping, but I can't manage to implement it for point light where one have to render 6 faces of cubemap and then lookup them in a shader.

I'm asking for links to examples with full source code.

Thanks in advance :)

Fred.



Sponsor:

#2 3TATUK2   Members   -  Reputation: 634

Like
1Likes
Like

Posted 15 February 2014 - 01:43 PM

okay i'm just throwing this out there, it's my "deprecated" (i don't use shadows as I deem them too slow) shadow code....

 

It's extremely sloppy and missing stuff, ie it wont work as is, but maybe if you actually sift through it and compare it to what you have and other online tutorials is may push you in the right direction

 

 

c:

GLuint fboId;
GLint g_ShadowCubeLocation;
GLuint depthCube;

    g_ShadowCubeLocation = glGetUniformLocation( world_shader, "cubeTexture" );

    glGenFramebuffers( 1, &fboId );
    glBindFramebuffer( GL_FRAMEBUFFER , fboId );


    glDrawBuffers( 0, NULL );
    glReadBuffer( GL_NONE );

    glViewport( 0, 0, 256, 256 );

    depthCube = VBO_identity + 1;

    i = -1;
    while( i++ < numlights ){

        glBindTexture( GL_TEXTURE_CUBE_MAP, ++VBO_identity );

        glTexParameteri( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        glTexParameteri( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
        glTexParameteri( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri( GL_TEXTURE_CUBE_MAP, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE );
        glTexParameterf( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE );
        glTexParameterf( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
        glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_X + 0, 0, GL_DEPTH_COMPONENT, 256, 256, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL );
        glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_X + 1, 0, GL_DEPTH_COMPONENT, 256, 256, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL );
        glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_X + 2, 0, GL_DEPTH_COMPONENT, 256, 256, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL );
        glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_X + 3, 0, GL_DEPTH_COMPONENT, 256, 256, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL );
        glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_X + 4, 0, GL_DEPTH_COMPONENT, 256, 256, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL );
        glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_X + 5, 0, GL_DEPTH_COMPONENT, 256, 256, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL );
        glBindTexture( GL_TEXTURE_CUBE_MAP, 0 );

        cubeFrustum( 0, i );
        cubeFrustum( 1, i );
        cubeFrustum( 2, i );
        cubeFrustum( 3, i );
        cubeFrustum( 4, i );
        cubeFrustum( 5, i );
    }

    glViewport( 0, 0, config_width, config_height );
    glBindFramebuffer( GL_FRAMEBUFFER, 0 );

inline void cubeFrustum( int order, int light ){
glActiveTexture( GL_TEXTURE2 + light );
glBindTexture( GL_TEXTURE_CUBE_MAP, depthCube + light );
glFramebufferTexture2D( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_CUBE_MAP_POSITIVE_X + order, depthCube + light, 0 );
glActiveTexture( GL_TEXTURE0 );

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();

    #define zNear 1 // smaller the better (.01)
    #define zFar 3000 // closer to zNear the better

    #define fovY 90
    #define GL_height ( zNear * tanf( fovY / 2 * RADIANS_PER_DEGREE ) )
    #define GL_width  ( GL_height * 1024. / 1024. )

    glFrustum( -GL_width, GL_width, -GL_height, GL_height, zNear, zFar );

    float lightPos[3];
lightPos[0]=0;
lightPos[1]=0;
lightPos[2]=0;
    if( order == 0 )      gluLookAt( lightPos[0], lightPos[1], lightPos[2], lightPos[0] + 1, lightPos[1], lightPos[2], 0, -1, 0 );
    else if( order == 1 ) gluLookAt( lightPos[0], lightPos[1], lightPos[2], lightPos[0] - 1, lightPos[1], lightPos[2], 0, -1, 0 );
    else if( order == 2 ) gluLookAt( lightPos[0], lightPos[1], lightPos[2], lightPos[0], lightPos[1] + 1, lightPos[2], 0, 0, 1 );
    else if( order == 3 ) gluLookAt( lightPos[0], lightPos[1], lightPos[2], lightPos[0], lightPos[1] - 1, lightPos[2], 0, 0, -1 );
    else if( order == 4 ) gluLookAt( lightPos[0], lightPos[1], lightPos[2], lightPos[0], lightPos[1], lightPos[2] + 1, 0, -1, 0 );
    else if( order == 5 ) gluLookAt( lightPos[0], lightPos[1], lightPos[2], lightPos[0], lightPos[1], lightPos[2] - 1, 0, -1, 0 );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    glClear( GL_DEPTH_BUFFER_BIT );

    glGetFloatv( GL_PROJECTION_MATRIX, c_Light4x4 );


    render_objects();
    render_map();

}
glsl:

#define numlights 5
varying vec4 v_light_Pos[numlights];
varying float phong[numlights];
uniform samplerCube cubeTexture[numlights];


    for( int i = 0; i < numlights; i++ ){

        float shadow = 1;

        vec3 dir = ( f_vertex - v_light_Pos[i] ).xyz;

        vec3 abs_lightdir = abs( dir );
        if(
            ( max( max( abs_lightdir.x, abs_lightdir.y ), abs_lightdir.z ) )
             >
            ( -1 * 3000 / ( ( textureCube( cubeTexture[i], dir ).r +.0005 ) * ( 3000 - 1 ) - 3000 ) )
          )
                shadow = 0;

        float distance = length( dir );
        float attenuation = 1 / ( .1 + ( .05 * distance ) + ( .002 * distance * distance ) );
        total += vec3( colour.rgb*clamp(phong[i]*shadow*attenuation,0,1) );

    }


    for( int i = 0; i < numlights; i++ ){

        v_light_Pos[i] = vec4( light_Pos[i] , 1 );

        vec3 L = normalize( v_light_Pos[i] - f_vertex ).xyz;
        float lambert = clamp( dot( f_normal, L ), 0, 1 );
        phong[i] = lambert;
    }





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS