point light shadowing problem

Started by
1 comment, last by Asem 14 years, 3 months ago
I've looked over the net and found info on this but some of it is not clear. I have my FBO set up like this. Bind a CubeMap Texture Tell it to clamp to edge for s,t,r format the cubemap with GL_DEPTH_COMPONENT32f with no pixel data since I'll be reading to it from the framebuffer. Set FBO to use a GL_DEPTH_ATTACHMENT Set a renderbuffer for the depth buffer ------------- Bind FBO set draw back buffer to none clear the depth buffer for x, -x, y, -y, z, -z { attachTexToFBO(+x + i);//It's the glFrameBufferRenderTexture something like that set view to the pointlights position... (I'm kinda confused here) --I use a lookat matrix to try to do this. --I handle all the matrix math since I'm using opengl3.2 core render objects } unbind FBO use shadowshader render objects stop shadowshader --------------

void RenderFBO()
{


    static float dt = 0.0f;

    if( runTime )
        dt += 0.1f;

glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective(90.0, 1.333, 0.1, 100.0);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();

matrix_math::Mat4X4<float> cameraDirection[6];
for(int i=0; i<6; i++)
    cameraDirection.identityClear();

///Setup Views
/*
//-Z, Z
cameraDirection[5] = matrix_math::MatrixTranslation(4.0f*cos(dt*0.5f), 4.0f*sin(dt*0.5f), movementL[2]) * matrix_math::MatrixRotationRoll(0.0f) * matrix_math::MatrixRotationYaw(0.0f) * matrix_math::MatrixRotationPitch(0.0f);
cameraDirection[4] = matrix_math::MatrixTranslation(4.0f*cos(dt*0.5f), 4.0f*sin(dt*0.5f), movementL[2]) * matrix_math::MatrixRotationRoll(0.0f) * matrix_math::MatrixRotationYaw(0.0f) * matrix_math::MatrixRotationPitch(-180.0f);

//-Y, Y
cameraDirection[3] = matrix_math::MatrixTranslation(4.0f*cos(dt*0.5f), 4.0f*sin(dt*0.5f), movementL[2]) * matrix_math::MatrixRotationRoll(180.0f) * matrix_math::MatrixRotationYaw(0.0f) * matrix_math::MatrixRotationPitch(-90.0f);
cameraDirection[2] = matrix_math::MatrixTranslation(4.0f*cos(dt*0.5f), 4.0f*sin(dt*0.5f), movementL[2]) * matrix_math::MatrixRotationRoll(0.0f) * matrix_math::MatrixRotationYaw(0.0f) * matrix_math::MatrixRotationPitch(-90.0f);

//-X, X
cameraDirection[1] = matrix_math::MatrixTranslation(4.0f*cos(dt*0.5f), 4.0f*sin(dt*0.5f), movementL[2]) * matrix_math::MatrixRotationRoll(-90.0f) * matrix_math::MatrixRotationYaw(0.0f) * matrix_math::MatrixRotationPitch(-90.0f);
cameraDirection[0] = matrix_math::MatrixTranslation(4.0f*cos(dt*0.5f), 4.0f*sin(dt*0.5f), movementL[2]) * matrix_math::MatrixRotationRoll(90.0f) * matrix_math::MatrixRotationYaw(0.0f) * matrix_math::MatrixRotationPitch(-90.0f);
*/
///Setup Projection
matrix_math::Mat4X4<float> world;
matrix_math::Mat4X4<float> viewMatrix;
projection = matrix_math::PerspectiveMatrix<float>(90.0f, float(Width)/float(Height), 0.1f, 100.0f);

/////////////
//-Z, +Z
cameraDirection[5] = matrix_math::LookAtMatrix(4.0f*cos(dt*0.5f), 4.0f*sin(dt*0.5f), movementL[2], 4.0f*cos(dt*0.5f), 4.0f*sin(dt*0.5f), movementL[2]-1.0f, -1.0f, 0.0f, 0.0f);
cameraDirection[4] = matrix_math::LookAtMatrix(4.0f*cos(dt*0.5f), 4.0f*sin(dt*0.5f), movementL[2], 4.0f*cos(dt*0.5f), 4.0f*sin(dt*0.5f), movementL[2]+0.0f,  1.0f, 0.0f, 0.0f);

//-Y, Y
cameraDirection[3] = matrix_math::LookAtMatrix(4.0f*cos(dt*0.5f), 4.0f*sin(dt*0.5f), movementL[2], 4.0f*cos(dt*0.5f), 4.0f*sin(dt*0.5f)-1.0f, movementL[2], 0.0f, 0.0f, -1.0f);//okay -
cameraDirection[2] = matrix_math::LookAtMatrix(4.0f*cos(dt*0.5f), 4.0f*sin(dt*0.5f), movementL[2], 4.0f*cos(dt*0.5f), 4.0f*sin(dt*0.5f)+1.0f, movementL[2], 0.0f, 0.0f,  1.0f);//okay +

//-X, +X
cameraDirection[1] = matrix_math::LookAtMatrix(4.0f*cos(dt*0.5f), 4.0f*sin(dt*0.5f), movementL[2], 4.0f*cos(dt*0.5f)-1.0f, 4.0f*sin(dt*0.5f), movementL[2], 0.0f, -1.0f, 0.0f);//okay -
cameraDirection[0] = matrix_math::LookAtMatrix(4.0f*cos(dt*0.5f), 4.0f*sin(dt*0.5f), movementL[2], -4.0f*cos(dt*0.5f)+1.0f, 4.0f*sin(dt*0.5f), -movementL[2], 1.0f,  0.0f, 0.0f);//okay +

///

    vector_math::Vec3<float> lightPosition = vector_math::Vec3<float>(4.0f*cos(dt*0.5f), 4.0f*sin(dt*0.5f), movementL[2]);
    glProgramUniform3fvEXT( depthShader->GetHandle(), glGetUniformLocation(depthShader->GetHandle(), "lightPosition"), 1, lightPosition.getXYZ() );



glClear(GL_DEPTH_BUFFER_BIT);	
///PROBLEM HERE I DONT THINK THE CUBEMAP IS BEING FILLED RIGHT
for(int i=0; i<6; i++)
{

    glFramebufferTexture2D( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT ,  GL_TEXTURE_CUBE_MAP_POSITIVE_X+i, fbo->GetTexture(), 0);
    
  


    ////////////////////////
    ////////////////////////
    ////////////////////////

    ///Draw Data



    matrix_math::Mat4X4<float> viewMatrix = cameraDirection;
    //---


/*
    glBindBuffer( GL_UNIFORM_BUFFER, UniformBufferTransPHandle );
        glBufferSubData( GL_UNIFORM_BUFFER, 0, sizeof(ModelViewMatrix), &ModelViewMatrix.matrixA[0]);
    glBindBuffer(GL_UNIFORM_BUFFER, 0);
*/
    depthShader->UseShader();

    world.identityClear();
    world = matrix_math::MatrixTranslation(0.0f, 0.0f, 0.0f);
    //projection
    glUniform3fv(glGetUniformLocation(depthShader->GetHandle(), "lightPosition"), 1, lightPosition.getXYZ());
    glUniformMatrix4fv(glGetUniformLocation(depthShader->GetHandle(), "projectionMatrix"), 1, GL_FALSE, &projection.matrixA[0]);
    glUniformMatrix4fv(glGetUniformLocation(depthShader->GetHandle(), "viewMatrix"), 1, GL_FALSE, &viewMatrix.matrixA[0]);
    glUniformMatrix4fv(glGetUniformLocation(depthShader->GetHandle(), "worldMatrix"), 1, GL_FALSE, &world.matrixA[0]);

        myCube2->Draw();
    depthShader->StopShader();

    glFinish();
    //---

    //matrix_math::Mat4X4<float> pMatrix = matrix_math::MatrixTranslation(0.0f, 0.0f, -1.0f) * matrix_math::MatrixScale(5.0f, 5.0f, 1.0f) * ModelViewMatrix;
    //ModelViewMatrix *= matrix_math::MatrixScale(5.0f, 5.0f, 1.0f);// * matrix_math::MatrixTranslation(0.0f, 0.0f, -2.0f);


/*
    glBindBuffer( GL_UNIFORM_BUFFER, UniformBufferTransPHandle );
        glBufferSubData( GL_UNIFORM_BUFFER, 0, sizeof(ModelViewMatrix), &pMatrix.matrixA[0]);
    glBindBuffer(GL_UNIFORM_BUFFER, 0);
*/

    depthShader->UseShader();

    world.identityClear();
    world =  matrix_math::MatrixScale(5.0f, 5.0f, 1.0f) * matrix_math::MatrixTranslation(0.0f, 0.0f, -1.0f);
    //projection


    glUniformMatrix4fv(glGetUniformLocation(depthShader->GetHandle(), "projectionMatrix"), 1, GL_FALSE, &projection.matrixA[0]);
    glUniformMatrix4fv(glGetUniformLocation(depthShader->GetHandle(), "viewMatrix"), 1, GL_FALSE, &viewMatrix.matrixA[0]);
    glUniformMatrix4fv(glGetUniformLocation(depthShader->GetHandle(), "worldMatrix"), 1, GL_FALSE, &world.matrixA[0]);

       myPlane2->Draw();
    depthShader->StopShader();

    glFinish();



    ////////////////////////
    ////////////////////////
    ////////////////////////
    ////////////////////////

}
}
////////////
////////////
/////////////
///////////////

void Render()
{
    fbo->UseBuffer();
        glCullFace( GL_FRONT );
          RenderFBO();
        glCullFace( GL_BACK );
    fbo->StopBuffer();

    glFlush();


    projection = matrix_math::PerspectiveMatrix<float>(45.0f, float(Width)/float(Height), 0.1f, 100.0f);
    static float dt = 0.0f;

    if( runTime )
        dt += 0.1f;

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		// Clear The Screen And The Depth Buffer
  glLoadIdentity();


    matrix_math::Mat4X4<float> lookAtCam = matrix_math::LookAtMatrix(movement[0], movement[1], movement[2], movement[4], 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);

    matrix_math::Mat4X4<float> ModelViewMatrix;
    matrix_math::Mat4X4<float> viewMatrix;
    matrix_math::Mat4X4<float> worldMatrix;

    ModelViewMatrix = lookAtCam;

    ///Look into how to calculate the normal matrix
    matrix_math::Mat4X4<float> NormalMatrix;
    matrix_math::Mat4X4<float> NormalMatrixTmp;


    ////////////////////////
    ////////////////////////
    ////////////////////////


    ///Set Data

    //vector_math::Vec4<float> LightPosition = matrix_math::matrixVecMultiply<float>( ModelViewMatrix , vector_math::Vec4<float>(movementL[0], movementL[1], movementL[2], 1.0f) ); //Should have beenm fine but it's off
    //vector_math::Vec4<float> LightPosition = vector_math::Vec4<float>(movementL[0], movementL[1], movementL[2], 1.0f); //OKAY!!
    vector_math::Vec3<float> LightPosition = vector_math::Vec3<float>(4.0f*cos(dt*0.5f), 4.0f*sin(dt*0.5f), movementL[2]); //OKAY!!
    vector_math::Vec3<float> eyePosition = vector_math::Vec3<float>(movement[0], movement[1], movement[2]); //OKAY!!



    ///PointLight Visual
    showLightPosition->UseShader();

        worldMatrix.identityClear();
        worldMatrix = matrix_math::MatrixTranslation(4.0f*cos(dt*0.5f), 4.0f*sin(dt*0.5f), movementL[2]);
        viewMatrix = lookAtCam;
        //projection

        glUniformMatrix4fv( glGetUniformLocation(showLightPosition->GetHandle(), "worldMatrix"), 1, GL_FALSE, &worldMatrix.matrixA[0]);
        glUniformMatrix4fv( glGetUniformLocation(showLightPosition->GetHandle(), "viewMatrix"), 1, GL_FALSE, &viewMatrix.matrixA[0]);
        glUniformMatrix4fv( glGetUniformLocation(showLightPosition->GetHandle(), "projectionMatrix"), 1, GL_FALSE, &projection.matrixA[0]);

        GLuint attribPos = glGetAttribLocation(showLightPosition->GetHandle(), "Vertex");

        glBindBuffer(GL_ARRAY_BUFFER, pointBuffer);
        glVertexAttribPointer(attribPos, 1, GL_FLOAT, GL_FALSE, sizeof(float)*4, 0);

        glEnableVertexAttribArray( attribPos );
        glDrawArrays(GL_POINTS, 0, 1);
        glBindBuffer(GL_ARRAY_BUFFER, 0);

    showLightPosition->StopShader();

glFinish();

    ///Set Data For Light Shader
    lightProp.color = vector_math::Vec4<float>(1.0f, 1.0f, 1.0f, 1.0f);
    lightProp.ambientColor = vector_math::Vec4<float>(0.0f, 0.0f, 0.0f, 1.0f);
    lightProp.specColor = vector_math::Vec4<float>(1.0f, 0.0f, 0.0f, 1.0f);
    lightProp.shine = 1.0f;

    glProgramUniform4fvEXT(pointlight_shader_15->GetHandle(), LColor, 1, lightProp.color.getXYZW());
    glProgramUniform4fvEXT(pointlight_shader_15->GetHandle(), LSpec , 1, lightProp.specColor.getXYZW());
    glProgramUniform4fvEXT(pointlight_shader_15->GetHandle(), LAmb  , 1, lightProp.ambientColor.getXYZW());
    glProgramUniform1fEXT (pointlight_shader_15->GetHandle(), LShine, lightProp.shine);

    glProgramUniformMatrix4fvEXT(pointlight_shader_15->GetHandle(), uniformProjectionMatrix, 1, GL_FALSE, &projection.matrixA[0]);
    glProgramUniform3fvEXT(pointlight_shader_15->GetHandle(), uniformLP, 1, LightPosition.getXYZ());
    glProgramUniform3fvEXT(pointlight_shader_15->GetHandle(), glGetUniformLocation(pointlight_shader_15->GetHandle(), "eyePosition"), 1, eyePosition.getXYZ());


glFinish();

/*
    glBindBuffer( GL_UNIFORM_BUFFER, UniformBufferTransPHandle );
        glBufferSubData( GL_UNIFORM_BUFFER, 0, sizeof(ModelViewMatrix), &ModelViewMatrix.matrixA[0]);
    glBindBuffer(GL_UNIFORM_BUFFER, 0);
*/
    ///Draw Data

    worldMatrix.identityClear();
    worldMatrix = matrix_math::MatrixTranslation(0.0f, 0.0f, 0.0f);
    viewMatrix = lookAtCam;
    //projection

    NormalMatrix.identityClear();
    NormalMatrixTmp.identityClear();
    NormalMatrixTmp = matrix_math::MatrixInverse( worldMatrix );
    NormalMatrix = matrix_math::MatrixTranspose( NormalMatrixTmp );

    glProgramUniformMatrix4fvEXT(pointlight_shader_15->GetHandle(), glGetUniformLocation(pointlight_shader_15->GetHandle(), "worldMatrix"), 1, GL_FALSE, &worldMatrix.matrixA[0]);
    glProgramUniformMatrix4fvEXT(pointlight_shader_15->GetHandle(), glGetUniformLocation(pointlight_shader_15->GetHandle(), "viewMatrix"), 1, GL_FALSE, &viewMatrix.matrixA[0]);

    glProgramUniformMatrix4fvEXT(pointlight_shader_15->GetHandle(), glGetUniformLocation(pointlight_shader_15->GetHandle(), "NormalMatrix"), 1, GL_FALSE, &NormalMatrix.matrixA[0]);

    pointlight_shader_15->UseShader();
        glUniform4f(glGetUniformLocation(pointlight_shader_15->GetHandle(), "matColor")  , 1.0f, 1.0f, 1.0f, 1.0f);
        glUniform1i(uniformSwitch, 0);
        myCube->Draw();
    pointlight_shader_15->StopShader();

glFinish();

    //matrix_math::Mat4X4<float> pMatrix = ModelViewMatrix * matrix_math::MatrixScale(5.0f, 5.0f, 1.0f) *  matrix_math::MatrixTranslation(0.0f, 0.0f, -1.0f);
    //ModelViewMatrix *= matrix_math::MatrixScale(5.0f, 5.0f, 1.0f);// * matrix_math::MatrixTranslation(0.0f, 0.0f, -2.0f);





/*
    glBindBuffer( GL_UNIFORM_BUFFER, UniformBufferTransPHandle );
        glBufferSubData( GL_UNIFORM_BUFFER, 0, sizeof(pMatrix), &pMatrix.matrixA[0]);
    glBindBuffer(GL_UNIFORM_BUFFER, 0);
*/
    //glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
    //glLineWidth(2.0);

    fbo->UseRenderTexture();



    pointlight_shader_15->UseShader();
        glUniform1f(glGetUniformLocation(pointlight_shader_15->GetHandle(), "time")  , dt);
        glUniform4f(glGetUniformLocation(pointlight_shader_15->GetHandle(), "matColor")  , 1.0f, 1.0f, 1.0f, 1.0f);
        glUniform1i(uniformSwitch, 2);

    worldMatrix.identityClear();
    worldMatrix = matrix_math::MatrixScale(5.0f, 5.0f, 1.0f) * matrix_math::MatrixTranslation(0.0f, 0.0f, -1.0f);
    viewMatrix = lookAtCam;
    //projection

    NormalMatrix.identityClear();
    NormalMatrixTmp.identityClear();
    NormalMatrixTmp = matrix_math::MatrixInverse( worldMatrix );
    NormalMatrix = matrix_math::MatrixTranspose( NormalMatrixTmp );

    glProgramUniformMatrix4fvEXT(pointlight_shader_15->GetHandle(), glGetUniformLocation(pointlight_shader_15->GetHandle(), "worldMatrix"), 1, GL_FALSE, &worldMatrix.matrixA[0]);
    glProgramUniformMatrix4fvEXT(pointlight_shader_15->GetHandle(), glGetUniformLocation(pointlight_shader_15->GetHandle(), "viewMatrix"), 1, GL_FALSE, &viewMatrix.matrixA[0]);

    glProgramUniformMatrix4fvEXT(pointlight_shader_15->GetHandle(), glGetUniformLocation(pointlight_shader_15->GetHandle(), "NormalMatrix"), 1, GL_FALSE, &NormalMatrix.matrixA[0]);

        myPlane->Draw();///

    pointlight_shader_15->StopShader();


    ////////////////////////
    ////////////////////////
    ////////////////////////
    ////////////////////////

    glFinish();

  // swap buffers to display, since we're double buffered.
  SDL_GL_SwapBuffers();
}

I say depthshader but I just render a reguler white shader since I was thinking the depth buffer would do it for me instead. int the frag shader i use samplerCubeShadow and feed in a normalized -lightvec.xyz and make q = 0.0. Tell me if i left out some info. I've trying to follow the directx cubmap shadow tutorial that can be found on gamedev and adapt it to opengl.
Advertisement
sorry if it seems a little weird I plan to clean up later since it's supposed to just get this working anyway.
Sorry but I really need help on this (I've been trying for some time).

Would it be better to just have a depth shader and calculate the depth from the shader rather than using the depth buffer?


If I use a depth buffer are the values from 0 to zfar value or 0 to 1 (assuming floating point)?

When I try to create the view from the point lights perspective am I supposed to feed this into gl_Position

gl_Position = Vertices * worldMatrix * viewMatrix * perspectiveMatrix;

Assuming that the objects position is at (0,0,0) and the viewMatrix is supposed to match the light's position(depending the on the cubemap ie +x, -x, etc)?

Finally, should I clear the depth buffer for ever part of the cubemap attached to the FBO or just once...

glClear(GL_DEPTH_BUFFER_BIT);
for(int i=0; i<6; i++)
{
glFrameBufferTexture(GL_CUBEMAP_POSITIVE_X + i)
RenderObjects();
}

or like this

for(int i=0; i<6; i++)
{
glClear(GL_DEPTH_BUFFER_BIT);
glFrameBufferTexture(GL_CUBEMAP_POSITIVE_X + i)
RenderObjects();
}

I've done shadows for spot lights but for some reason I can't wrap my mind around this(shadows using a cubemap for point lights) and am most likely missing some small detail that I;ve missed but I'm not sure what.

This topic is closed to new replies.

Advertisement