2012-04-05 23:09:41 +00:00
|
|
|
//
|
|
|
|
|
// KRLight.cpp
|
|
|
|
|
// KREngine
|
|
|
|
|
//
|
|
|
|
|
// Created by Kearwood Gilbert on 12-04-05.
|
|
|
|
|
// Copyright (c) 2012 Kearwood Software. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
2012-06-15 00:05:56 +00:00
|
|
|
|
|
|
|
|
|
2012-04-05 23:09:41 +00:00
|
|
|
#import "KRLight.h"
|
|
|
|
|
|
2012-06-15 00:05:56 +00:00
|
|
|
#import "KRNode.h"
|
|
|
|
|
#import "KRMat4.h"
|
|
|
|
|
#import "KRVector3.h"
|
|
|
|
|
#import "KRCamera.h"
|
|
|
|
|
#import "KRContext.h"
|
|
|
|
|
|
|
|
|
|
#import "KRBoundingVolume.h"
|
|
|
|
|
#import "KRShaderManager.h"
|
|
|
|
|
#import "KRShader.h"
|
2012-09-19 20:26:30 +00:00
|
|
|
#import "KRStockGeometry.h"
|
2012-09-13 20:09:19 +00:00
|
|
|
#import "assert.h"
|
2012-06-15 00:05:56 +00:00
|
|
|
|
2012-08-29 21:43:11 +00:00
|
|
|
KRLight::KRLight(KRScene &scene, std::string name) : KRNode(scene, name)
|
2012-04-05 23:09:41 +00:00
|
|
|
{
|
2012-04-12 00:43:53 +00:00
|
|
|
m_intensity = 1.0f;
|
2012-06-15 00:05:56 +00:00
|
|
|
m_flareTexture = "";
|
|
|
|
|
m_pFlareTexture = NULL;
|
|
|
|
|
m_flareSize = 0.0;
|
2012-04-05 23:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
KRLight::~KRLight()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-12 00:43:53 +00:00
|
|
|
tinyxml2::XMLElement *KRLight::saveXML( tinyxml2::XMLNode *parent)
|
2012-04-05 23:09:41 +00:00
|
|
|
{
|
2012-04-12 00:43:53 +00:00
|
|
|
tinyxml2::XMLElement *e = KRNode::saveXML(parent);
|
|
|
|
|
e->SetAttribute("intensity", m_intensity);
|
|
|
|
|
e->SetAttribute("color_r", m_color.x);
|
|
|
|
|
e->SetAttribute("color_g", m_color.y);
|
|
|
|
|
e->SetAttribute("color_b", m_color.z);
|
|
|
|
|
e->SetAttribute("decay_start", m_decayStart);
|
2012-06-15 00:05:56 +00:00
|
|
|
e->SetAttribute("flare_size", m_flareSize);
|
|
|
|
|
e->SetAttribute("flare_texture", m_flareTexture.c_str());
|
2012-04-12 00:43:53 +00:00
|
|
|
return e;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-12 01:27:30 +00:00
|
|
|
void KRLight::loadXML(tinyxml2::XMLElement *e) {
|
|
|
|
|
KRNode::loadXML(e);
|
|
|
|
|
float x,y,z;
|
2012-06-15 00:05:56 +00:00
|
|
|
if(e->QueryFloatAttribute("color_r", &x) != tinyxml2::XML_SUCCESS) {
|
|
|
|
|
x = 1.0;
|
|
|
|
|
}
|
|
|
|
|
if(e->QueryFloatAttribute("color_g", &y) != tinyxml2::XML_SUCCESS) {
|
|
|
|
|
y = 1.0;
|
|
|
|
|
}
|
|
|
|
|
if(e->QueryFloatAttribute("color_b", &z) != tinyxml2::XML_SUCCESS) {
|
|
|
|
|
z = 1.0;
|
|
|
|
|
}
|
2012-04-12 01:27:30 +00:00
|
|
|
m_color = KRVector3(x,y,z);
|
|
|
|
|
|
2012-06-15 00:05:56 +00:00
|
|
|
if(e->QueryFloatAttribute("intensity", &m_intensity) != tinyxml2::XML_SUCCESS) {
|
|
|
|
|
m_intensity = 100.0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(e->QueryFloatAttribute("decay_start", &m_decayStart) != tinyxml2::XML_SUCCESS) {
|
|
|
|
|
m_decayStart = 0.0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(e->QueryFloatAttribute("flare_size", &m_flareSize) != tinyxml2::XML_SUCCESS) {
|
|
|
|
|
m_flareSize = 0.0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char *szFlareTexture = e->Attribute("flare_texture");
|
|
|
|
|
if(szFlareTexture) {
|
|
|
|
|
m_flareTexture = szFlareTexture;
|
|
|
|
|
} else {
|
|
|
|
|
m_flareTexture = "";
|
|
|
|
|
}
|
|
|
|
|
m_pFlareTexture = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void KRLight::setFlareTexture(std::string flare_texture) {
|
|
|
|
|
m_flareTexture = flare_texture;
|
|
|
|
|
m_pFlareTexture = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void KRLight::setFlareSize(float flare_size) {
|
|
|
|
|
m_flareSize = flare_size;
|
2012-04-12 01:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
2012-04-12 00:43:53 +00:00
|
|
|
void KRLight::setIntensity(float intensity) {
|
|
|
|
|
m_intensity = intensity;
|
|
|
|
|
}
|
|
|
|
|
float KRLight::getIntensity() {
|
|
|
|
|
return m_intensity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const KRVector3 &KRLight::getColor() {
|
|
|
|
|
return m_color;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void KRLight::setColor(const KRVector3 &color) {
|
|
|
|
|
m_color = color;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void KRLight::setDecayStart(float decayStart) {
|
|
|
|
|
m_decayStart = decayStart;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float KRLight::getDecayStart() {
|
|
|
|
|
return m_decayStart;
|
|
|
|
|
}
|
2012-06-15 00:05:56 +00:00
|
|
|
|
|
|
|
|
#if TARGET_OS_IPHONE
|
|
|
|
|
|
2012-09-11 03:06:35 +00:00
|
|
|
void KRLight::render(KRCamera *pCamera, KRContext *pContext, KRBoundingVolume &frustrumVolume, KRMat4 &viewMatrix, KRVector3 &cameraPosition, KRVector3 &lightDirection, KRMat4 *pShadowMatrices, GLuint *shadowDepthTextures, int cShadowBuffers, KRNode::RenderPass renderPass) {
|
2012-06-15 00:05:56 +00:00
|
|
|
|
2012-09-11 03:06:35 +00:00
|
|
|
KRNode::render(pCamera, pContext, frustrumVolume, viewMatrix, cameraPosition, lightDirection, pShadowMatrices, shadowDepthTextures, cShadowBuffers, renderPass);
|
2012-06-15 00:05:56 +00:00
|
|
|
|
|
|
|
|
if(renderPass == KRNode::RENDER_PASS_FLARES) {
|
|
|
|
|
if(m_flareTexture.size() && m_flareSize > 0.0f) {
|
|
|
|
|
if(!m_pFlareTexture && m_flareTexture.size()) {
|
|
|
|
|
m_pFlareTexture = pContext->getTextureManager()->getTexture(m_flareTexture.c_str());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(m_pFlareTexture) {
|
|
|
|
|
|
|
|
|
|
KRMat4 projectionMatrix = pCamera->getProjectionMatrix();
|
|
|
|
|
KRVector3 light_position = getLocalTranslation();
|
|
|
|
|
|
|
|
|
|
KRMat4 m_modelMatrix = KRMat4();
|
|
|
|
|
m_modelMatrix.translate(light_position.x, light_position.y, light_position.z);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
KRMat4 mvpmatrix = m_modelMatrix * viewMatrix * projectionMatrix;
|
|
|
|
|
KRMat4 matModelToView = viewMatrix * m_modelMatrix;
|
|
|
|
|
matModelToView.transpose();
|
|
|
|
|
matModelToView.invert();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Render light flare on transparency pass
|
2012-08-24 00:06:38 +00:00
|
|
|
KRShader *pShader = pContext->getShaderManager()->getShader("flare", pCamera, false, false, false, 0, false, false, false, false, false, false, false, false, false, renderPass);
|
2012-09-20 09:32:20 +00:00
|
|
|
if(pShader->bind(pCamera, matModelToView, mvpmatrix, cameraPosition, lightDirection, pShadowMatrices, shadowDepthTextures, 0, renderPass)) {
|
|
|
|
|
GLDEBUG(glUniform1f(
|
|
|
|
|
pShader->m_uniforms[KRShader::KRENGINE_UNIFORM_FLARE_SIZE],
|
|
|
|
|
m_flareSize
|
|
|
|
|
));
|
|
|
|
|
m_pContext->getTextureManager()->selectTexture(0, m_pFlareTexture);
|
|
|
|
|
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-06-15 00:05:56 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|