92 lines
3.3 KiB
C++
92 lines
3.3 KiB
C++
//
|
|
// KRDirectionalLight.cpp
|
|
// KREngine
|
|
//
|
|
// Created by Kearwood Gilbert on 12-04-05.
|
|
// Copyright (c) 2012 Kearwood Software. All rights reserved.
|
|
//
|
|
|
|
#include <iostream>
|
|
|
|
#import "KRDirectionalLight.h"
|
|
#import "KRShader.h"
|
|
#import "KRContext.h"
|
|
#import "KRMat4.h"
|
|
#import "assert.h"
|
|
#import "KRStockGeometry.h"
|
|
|
|
KRDirectionalLight::KRDirectionalLight(KRScene &scene, std::string name) : KRLight(scene, name)
|
|
{
|
|
|
|
}
|
|
|
|
KRDirectionalLight::~KRDirectionalLight()
|
|
{
|
|
|
|
}
|
|
|
|
std::string KRDirectionalLight::getElementName() {
|
|
return "directional_light";
|
|
}
|
|
|
|
KRVector3 KRDirectionalLight::getWorldLightDirection() {
|
|
const GLfloat PI = 3.14159265;
|
|
const GLfloat d2r = PI * 2 / 360;
|
|
|
|
KRVector3 world_rotation = getLocalRotation();
|
|
KRVector3 light_rotation = KRVector3(0.0, 0.0, -1.0);
|
|
KRMat4 m;
|
|
m.rotate(world_rotation.x, X_AXIS);
|
|
m.rotate(world_rotation.y, Y_AXIS);
|
|
m.rotate(world_rotation.z, X_AXIS);
|
|
m.rotate(-90.0 * d2r, Y_AXIS);
|
|
KRVector3 light_direction = KRMat4::Dot(m, light_rotation);
|
|
return light_direction;
|
|
}
|
|
|
|
KRVector3 KRDirectionalLight::getLocalLightDirection() {
|
|
return KRVector3(0.0, 0.0, 1.0);
|
|
}
|
|
|
|
#if TARGET_OS_IPHONE
|
|
|
|
void KRDirectionalLight::render(KRCamera *pCamera, KRContext *pContext, const KRViewport &viewport, const KRViewport *pShadowViewports, KRVector3 &lightDirection, GLuint *shadowDepthTextures, int cShadowBuffers, KRNode::RenderPass renderPass) {
|
|
|
|
KRLight::render(pCamera, pContext, viewport, pShadowViewports, lightDirection, shadowDepthTextures, cShadowBuffers, renderPass);
|
|
|
|
if(renderPass == KRNode::RENDER_PASS_DEFERRED_LIGHTS) {
|
|
// Lights are rendered on the second pass of the deferred renderer
|
|
|
|
KRMat4 matModelViewInverseTranspose = viewport.getViewMatrix() * getModelMatrix();
|
|
matModelViewInverseTranspose.transpose();
|
|
matModelViewInverseTranspose.invert();
|
|
|
|
KRVector3 light_direction_view_space = getWorldLightDirection();
|
|
light_direction_view_space = KRMat4::Dot(matModelViewInverseTranspose, light_direction_view_space);
|
|
light_direction_view_space.normalize();
|
|
|
|
KRShader *pShader = pContext->getShaderManager()->getShader("light_directional", pCamera, false, false, false, 0, false, false, false, false, false, false, false, false, false, false, false, false, false, renderPass);
|
|
if(pShader->bind(viewport, pShadowViewports, getModelMatrix(), lightDirection, shadowDepthTextures, 0, renderPass)) {
|
|
|
|
light_direction_view_space.setUniform(pShader->m_uniforms[KRShader::KRENGINE_UNIFORM_LIGHT_DIRECTION_VIEW_SPACE]);
|
|
m_color.setUniform(pShader->m_uniforms[KRShader::KRENGINE_UNIFORM_LIGHT_COLOR]);
|
|
|
|
GLDEBUG(glUniform1f(
|
|
pShader->m_uniforms[KRShader::KRENGINE_UNIFORM_LIGHT_INTENSITY],
|
|
m_intensity / 100.0f
|
|
));
|
|
|
|
// Disable z-buffer write
|
|
GLDEBUG(glDepthMask(GL_FALSE));
|
|
|
|
// Disable z-buffer test
|
|
GLDEBUG(glDisable(GL_DEPTH_TEST));
|
|
|
|
// Render a full screen quad
|
|
m_pContext->getModelManager()->bindVBO((void *)KRENGINE_VBO_2D_SQUARE, KRENGINE_VBO_2D_SQUARE_SIZE, true, false, false, true, false);
|
|
GLDEBUG(glDrawArrays(GL_TRIANGLE_STRIP, 0, 4));
|
|
}
|
|
}
|
|
}
|
|
|
|
#endif |