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-04-12 00:43:53 +00:00
|
|
|
KRLight::KRLight(std::string name) : KRNode(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
|
|
|
|
|
|
|
|
|
|
void KRLight::render(KRCamera *pCamera, KRContext *pContext, KRBoundingVolume &frustrumVolume, KRMat4 &viewMatrix, KRVector3 &cameraPosition, KRVector3 &lightDirection, KRMat4 *pShadowMatrices, GLuint *shadowDepthTextures, int cShadowBuffers, KRNode::RenderPass renderPass) {
|
|
|
|
|
|
|
|
|
|
KRNode::render(pCamera, pContext, frustrumVolume, viewMatrix, cameraPosition, lightDirection, pShadowMatrices, shadowDepthTextures, cShadowBuffers, renderPass);
|
|
|
|
|
|
|
|
|
|
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-09 23:40:32 +00:00
|
|
|
KRShader *pShader = pContext->getShaderManager()->getShader("flare", pCamera, false, false, false, 0, false, false, false, false, false, false, false, false, renderPass);
|
2012-06-15 00:05:56 +00:00
|
|
|
pShader->bind(pCamera, matModelToView, mvpmatrix, cameraPosition, lightDirection, pShadowMatrices, shadowDepthTextures, 0, renderPass);
|
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_pFlareTexture->getName());
|
|
|
|
|
|
|
|
|
|
static const GLfloat squareVertices[] = {
|
|
|
|
|
0.0f, 0.0f,
|
|
|
|
|
1.0f, 0.0f,
|
|
|
|
|
0.0f, 1.0f,
|
|
|
|
|
1.0f, 1.0f,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
|
glVertexAttribPointer(KRShader::KRENGINE_ATTRIB_TEXUVA, 2, GL_FLOAT, 0, 0, squareVertices);
|
|
|
|
|
|
|
|
|
|
glUniform1f(
|
|
|
|
|
pShader->m_uniforms[KRShader::KRENGINE_UNIFORM_FLARE_SIZE],
|
|
|
|
|
m_flareSize
|
|
|
|
|
);
|
|
|
|
|
glEnableVertexAttribArray(KRShader::KRENGINE_ATTRIB_TEXUVA);
|
|
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|