2012-04-05 23:09:41 +00:00
|
|
|
//
|
|
|
|
|
// KRDirectionalLight.cpp
|
|
|
|
|
// KREngine
|
|
|
|
|
//
|
|
|
|
|
// Created by Kearwood Gilbert on 12-04-05.
|
|
|
|
|
// Copyright (c) 2012 Kearwood Software. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
|
#import "KRDirectionalLight.h"
|
2012-04-14 02:15:35 +00:00
|
|
|
#import "KRShader.h"
|
|
|
|
|
#import "KRContext.h"
|
2012-04-19 23:39:32 +00:00
|
|
|
#import "KRMat4.h"
|
2012-09-13 20:09:19 +00:00
|
|
|
#import "assert.h"
|
2012-09-19 20:26:30 +00:00
|
|
|
#import "KRStockGeometry.h"
|
2012-04-05 23:09:41 +00:00
|
|
|
|
2012-08-29 21:43:11 +00:00
|
|
|
KRDirectionalLight::KRDirectionalLight(KRScene &scene, std::string name) : KRLight(scene, name)
|
2012-04-05 23:09:41 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
KRDirectionalLight::~KRDirectionalLight()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-12 00:43:53 +00:00
|
|
|
std::string KRDirectionalLight::getElementName() {
|
|
|
|
|
return "directional_light";
|
2012-04-05 23:09:41 +00:00
|
|
|
}
|
2012-04-13 06:40:53 +00:00
|
|
|
|
2012-04-26 09:06:45 +00:00
|
|
|
KRVector3 KRDirectionalLight::getWorldLightDirection() {
|
2012-05-11 01:06:29 +00:00
|
|
|
const GLfloat PI = 3.14159265;
|
|
|
|
|
const GLfloat d2r = PI * 2 / 360;
|
|
|
|
|
|
2012-04-26 09:06:45 +00:00
|
|
|
KRVector3 world_rotation = getLocalRotation();
|
2012-05-11 01:06:29 +00:00
|
|
|
KRVector3 light_rotation = KRVector3(0.0, 0.0, -1.0);
|
2012-04-19 23:39:32 +00:00
|
|
|
KRMat4 m;
|
|
|
|
|
m.rotate(world_rotation.x, X_AXIS);
|
|
|
|
|
m.rotate(world_rotation.y, Y_AXIS);
|
2012-05-11 01:06:29 +00:00
|
|
|
m.rotate(world_rotation.z, X_AXIS);
|
|
|
|
|
m.rotate(-90.0 * d2r, Y_AXIS);
|
2012-06-10 06:38:31 +00:00
|
|
|
KRVector3 light_direction = KRMat4::Dot(m, light_rotation);
|
2012-04-19 23:39:32 +00:00
|
|
|
return light_direction;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-26 09:06:45 +00:00
|
|
|
KRVector3 KRDirectionalLight::getLocalLightDirection() {
|
|
|
|
|
return KRVector3(0.0, 0.0, 1.0);
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-13 06:40:53 +00:00
|
|
|
#if TARGET_OS_IPHONE
|
|
|
|
|
|
2012-10-26 01:17:35 +00:00
|
|
|
void KRDirectionalLight::render(KRCamera *pCamera, KRContext *pContext, const KRViewport &viewport, KRVector3 &lightDirection, KRMat4 *pShadowMatrices, GLuint *shadowDepthTextures, int cShadowBuffers, KRNode::RenderPass renderPass) {
|
2012-04-13 06:40:53 +00:00
|
|
|
|
2012-10-26 01:17:35 +00:00
|
|
|
KRLight::render(pCamera, pContext, viewport, lightDirection, pShadowMatrices, shadowDepthTextures, cShadowBuffers, renderPass);
|
2012-06-15 00:05:56 +00:00
|
|
|
|
2012-06-14 19:33:17 +00:00
|
|
|
if(renderPass == KRNode::RENDER_PASS_DEFERRED_LIGHTS) {
|
2012-04-13 06:40:53 +00:00
|
|
|
// Lights are rendered on the second pass of the deferred renderer
|
2012-04-14 02:15:35 +00:00
|
|
|
|
2012-11-03 02:57:35 +00:00
|
|
|
KRMat4 matModelViewInverseTranspose = viewport.getViewMatrix() * getModelMatrix();
|
2012-10-12 00:02:24 +00:00
|
|
|
matModelViewInverseTranspose.transpose();
|
|
|
|
|
matModelViewInverseTranspose.invert();
|
2012-04-13 06:40:53 +00:00
|
|
|
|
2012-04-26 09:06:45 +00:00
|
|
|
KRVector3 light_direction_view_space = getWorldLightDirection();
|
2012-10-12 00:02:24 +00:00
|
|
|
light_direction_view_space = KRMat4::Dot(matModelViewInverseTranspose, light_direction_view_space);
|
2012-04-26 09:06:45 +00:00
|
|
|
light_direction_view_space.normalize();
|
2012-04-13 06:40:53 +00:00
|
|
|
|
2012-10-12 00:02:24 +00:00
|
|
|
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);
|
2012-11-03 02:57:35 +00:00
|
|
|
if(pShader->bind(viewport, getModelMatrix(), lightDirection, pShadowMatrices, shadowDepthTextures, 0, renderPass)) {
|
2012-09-20 09:32:20 +00:00
|
|
|
|
2012-10-18 08:37:19 +00:00
|
|
|
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]);
|
2012-09-20 09:32:20 +00:00
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
}
|
2012-04-13 06:40:53 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|