Ejemplo n.º 1
0
// ===================
// Stencil pass
// ===================
void World::DoStencilPass(const PointLight& l)
{
    gbuffer.StartStencilPass();
    glClear(GL_STENCIL_BUFFER_BIT);

    glEnable(GL_DEPTH_TEST);
    glDisable(GL_CULL_FACE);

    glStencilFunc(GL_ALWAYS, 0, 0);

    // Bind pass 2 shader.
    Shader* lpassShader = &emptyShader;
    Shader::Bind(lpassShader);

    // Matrices
    Matrix pmat = cam.GetProjectionMatrix();
    Matrix vmat = cam.GetViewMatrix();

    lpassShader->SetParameter("projectionMatrix", &pmat[0][0]);
    lpassShader->SetParameter("viewMatrix", &vmat[0][0]);

    // Draw point light.
    Matrix lmat = l.GetMatrix();
    lpassShader->SetParameter("modelMatrix", &lmat[0][0]);

    mdl3.Draw();
}
Ejemplo n.º 2
0
// ===================
// Point lights
// ===================
void World::DoLightingPass(const PointLight& l)
{
    gbuffer.StartLightingPass();

    // Disable depth test.
    glDisable(GL_DEPTH_TEST);

    // Setup for lighting pass.
    glStencilFunc(GL_NOTEQUAL, 0, 0xFF);

    glEnable(GL_BLEND);

    glEnable(GL_CULL_FACE);
    glCullFace(GL_FRONT);

    // Bind pass 2 shader.
    Shader* lpassShader = &pointLightShader;
    Shader::Bind(lpassShader);

    // Activate gbuffer textures.
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, gbuffer.GetColorID());
    glActiveTexture(GL_TEXTURE1);
    glBindTexture(GL_TEXTURE_2D, gbuffer.GetNormalID());
    glActiveTexture(GL_TEXTURE2);
    glBindTexture(GL_TEXTURE_2D, gbuffer.GetPositionID());

    // Matrices
    Matrix pmat = cam.GetProjectionMatrix();
    Matrix vmat = cam.GetViewMatrix();

    lpassShader->SetParameter("projectionMatrix", &pmat[0][0]);
    lpassShader->SetParameter("viewMatrix", &vmat[0][0]);
    lpassShader->SetParameter("screenSize", WINDOW_WIDTH, WINDOW_HEIGHT);

    // Set the eye position to the camera position.
    Vector3 cpos = cam.GetPosition();
    lpassShader->SetParameter("eyePosition", cpos.x, cpos.y, cpos.z);
    lpassShader->SetParameter("farClipDistance", cam.GetFarClippingPlane());

    // Texture locations
    lpassShader->SetParameter("difftex", 0);
    lpassShader->SetParameter("normtex", 1);
    lpassShader->SetParameter("postex", 2);

    // Draw point lights.
    Matrix lmat = l.GetMatrix();
    lpassShader->SetParameter("modelMatrix", &lmat[0][0]);

    const Vector3& pos = l.GetPosition();
    lpassShader->SetParameter("lightPos", pos.x, pos.y, pos.z);

    // Itensity.
    lpassShader->SetParameter("ambientIntensity", l.GetAmbientIntensity());
    lpassShader->SetParameter("diffuseIntensity", l.GetDiffuseIntensity());

    // Attenuation.
    lpassShader->SetParameter("attenuationConstant", l.GetAttenuationConstant());
    lpassShader->SetParameter("attenuationLinear", l.GetAttenuationLinear());
    lpassShader->SetParameter("attenuationExponential", l.GetAttenuationExponential());

    Color diffuseColor = l.GetColor();
    lpassShader->SetParameter("lightColor", diffuseColor.R(), diffuseColor.G(), diffuseColor.B());

    pointLightMesh.Draw();

    glCullFace(GL_BACK);
    glDisable(GL_BLEND);
}