|
From: <axl...@us...> - 2009-08-05 00:44:15
|
Revision: 454
http://hgengine.svn.sourceforge.net/hgengine/?rev=454&view=rev
Author: axlecrusher
Date: 2009-08-05 00:44:07 +0000 (Wed, 05 Aug 2009)
Log Message:
-----------
lights
Added Paths:
-----------
Mercury2/src/Light.cpp
Mercury2/src/Light.h
Mercury2/src/RenderDifferedLights.cpp
Mercury2/src/RenderDifferedLights.h
Added: Mercury2/src/Light.cpp
===================================================================
--- Mercury2/src/Light.cpp (rev 0)
+++ Mercury2/src/Light.cpp 2009-08-05 00:44:07 UTC (rev 454)
@@ -0,0 +1,159 @@
+#include <Light.h>
+#include <BoundingBox.h>
+#include <MercuryNode.h>
+
+#include <RenderGraph.h>
+#include <GLHeaders.h>
+
+#include <Shader.h>
+
+REGISTER_ASSET_TYPE(Light);
+
+Light::Light()
+ :MercuryAsset()
+{
+ m_atten[0] = m_atten[1] = m_atten[2] = 0.0f;
+ m_radius = 1.0f;
+ m_fullscreen = false;
+}
+
+Light::~Light()
+{
+}
+
+void Light::Render(const MercuryNode* node)
+{
+ m_worldPosition = glGetMatrix(GL_MODELVIEW_MATRIX);
+ m_worldPosition2 = node->FindGlobalMatrix();
+ CURRENTRENDERGRAPH->AddDifferedLight( this );
+ m_parent = node;
+}
+
+void Light::LoadFromXML(const XMLNode& node)
+{
+ if ( !node.Attribute("atten").empty() )
+ StrTo3Float(node.Attribute("atten"), m_atten);
+
+ if ( !node.Attribute("fullscreen").empty() )
+ m_fullscreen = node.Attribute("fullscreen")=="true"?true:false;
+
+ ComputeRadius();
+
+ MercuryAsset::LoadFromXML( node );
+}
+
+void Light::StrTo3Float(const MString& s, float* a)
+{
+ sscanf(s.c_str(), "%f,%f,%f", &a[0], &a[1], &a[2]);
+}
+
+Light* Light::Generate()
+{
+ return new Light();
+}
+
+void Light::ComputeRadius()
+{
+ //300 ensures that RGB of 255 reaches 0
+ //at 50, 255 is about 5. Close enough for me
+ const float maxDenom = 50;//300;
+ float a = m_atten[2]; //quadratic
+ float b = m_atten[1]; //linear
+ float c = m_atten[0]; //constant
+ float d = 0;
+
+ if ((a == 0)&&(b == 0))
+ {
+ d = 1000.0;
+ }
+ else if (a == 0)
+ {
+ d = (maxDenom - c)/b;
+ }
+ else if (b == 0)
+ {
+ d = SQRT((maxDenom - c)/a);
+ }
+ else
+ {
+ //reciprocal of the quadratic equation
+ float bottom = 2*a;
+ float s = SQRT((b*b)-(4*a*c));
+ float x1 = ((-b) - s)/bottom;
+ float x2 = ((-b) + s)/bottom;
+ d = MAX<float>(x1,x2);
+ }
+
+ m_radius = Clamp<float>(0.0f, 1000.0f, d);
+
+ SAFE_DELETE( m_boundingVolume );
+ m_boundingVolume = new BoundingBox(MercuryVertex(0,0,0), MercuryVertex(m_radius,m_radius,m_radius) );
+}
+
+void Light::DifferedRender()
+{
+ glLoadMatrix( m_worldPosition );
+ if ( !m_boundingVolume ) return;
+
+ BoundingBox* bb = (BoundingBox*)m_boundingVolume;
+// bb->Render();
+
+ MercuryVertex p(0,0,0,1);
+ p = m_worldPosition2 * p;
+
+// p.Print();
+
+ ShaderAttribute sa;
+ sa.type = ShaderAttribute::TYPE_FLOATV4;
+ sa.value.fFloatV4[0] = p.GetX();
+ sa.value.fFloatV4[1] = p.GetY();
+ sa.value.fFloatV4[2] = p.GetZ();
+ Shader::SetAttribute("HG_LightPos", sa);
+
+ sa.value.fFloatV4[0] = m_atten[0];
+ sa.value.fFloatV4[1] = m_atten[1];
+ sa.value.fFloatV4[2] = m_atten[2];
+ sa.value.fFloatV4[3] = m_radius;
+ Shader::SetAttribute("HG_LightAtten", sa);
+
+ if (m_fullscreen)
+ {
+ glCullFace(GL_BACK);
+ m_fullScreenQuad.Render( m_parent );
+ glCullFace(GL_FRONT);
+ }
+ else
+ bb->RenderFaces();
+}
+
+/****************************************************************************
+ * Copyright (C) 2009 by Joshua Allen *
+ * *
+ * *
+ * All rights reserved. *
+ * *
+ * Redistribution and use in source and binary forms, with or without *
+ * modification, are permitted provided that the following conditions *
+ * are met: *
+ * * Redistributions of source code must retain the above copyright *
+ * notice, this list of conditions and the following disclaimer. *
+ * * Redistributions in binary form must reproduce the above *
+ * copyright notice, this list of conditions and the following *
+ * disclaimer in the documentation and/or other materials provided *
+ * with the distribution. *
+ * * Neither the name of the Mercury Engine nor the names of its *
+ * contributors may be used to endorse or promote products derived *
+ * from this software without specific prior written permission. *
+ * *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR *
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT *
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, *
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY *
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE *
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
+ ***************************************************************************/
Added: Mercury2/src/Light.h
===================================================================
--- Mercury2/src/Light.h (rev 0)
+++ Mercury2/src/Light.h 2009-08-05 00:44:07 UTC (rev 454)
@@ -0,0 +1,75 @@
+#include <MercuryAsset.h>
+#include <FullscreenQuad.h>
+
+#ifndef MLIGHT_H
+#define MLIGHT_H
+
+class Light : public MercuryAsset
+{
+ public:
+ Light();
+ virtual ~Light();
+
+// virtual void Init(MercuryNode* node);
+
+ /** PreRender should be called before any real openGL render commands.
+ It is used to handles things like frustum culling, and occlusion culling.
+ Currently only occlusion culling test is run here.**/
+// virtual void PreRender(const MercuryNode* node);
+
+ virtual void Render(const MercuryNode* node);
+// virtual void PostRender(const MercuryNode* node) {};
+
+ ///Loads an asset from an XMLAsset representing itself
+ virtual void LoadFromXML(const XMLNode& node);
+
+ static Light* Generate();
+
+ void DifferedRender();
+ private:
+ void StrTo3Float(const MString& s, float* a);
+ void ComputeRadius();
+
+ float m_atten[3];
+ float m_radius;
+ MercuryMatrix m_worldPosition;
+ MercuryMatrix m_worldPosition2;
+
+ bool m_fullscreen;
+ const MercuryNode* m_parent;
+
+ FullscreenQuad m_fullScreenQuad;
+};
+
+#endif
+/****************************************************************************
+ * Copyright (C) 2009 by Joshua Allen *
+ * *
+ * *
+ * All rights reserved. *
+ * *
+ * Redistribution and use in source and binary forms, with or without *
+ * modification, are permitted provided that the following conditions *
+ * are met: *
+ * * Redistributions of source code must retain the above copyright *
+ * notice, this list of conditions and the following disclaimer. *
+ * * Redistributions in binary form must reproduce the above *
+ * copyright notice, this list of conditions and the following *
+ * disclaimer in the documentation and/or other materials provided *
+ * with the distribution. *
+ * * Neither the name of the Mercury Engine nor the names of its *
+ * contributors may be used to endorse or promote products derived *
+ * from this software without specific prior written permission. *
+ * *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR *
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT *
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, *
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY *
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE *
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
+ ***************************************************************************/
Added: Mercury2/src/RenderDifferedLights.cpp
===================================================================
--- Mercury2/src/RenderDifferedLights.cpp (rev 0)
+++ Mercury2/src/RenderDifferedLights.cpp 2009-08-05 00:44:07 UTC (rev 454)
@@ -0,0 +1,81 @@
+#include <RenderDifferedLights.h>
+#include <RenderGraph.h>
+
+#include <Texture.h>
+#include <GLHeaders.h>
+
+REGISTER_ASSET_TYPE(RenderDifferedLights);
+#define BUFFER_OFFSET(i) ((char*)NULL + (i))
+
+RenderDifferedLights::RenderDifferedLights()
+{
+}
+
+RenderDifferedLights::~RenderDifferedLights()
+{
+}
+
+void RenderDifferedLights::Render(const MercuryNode* node)
+{
+ uint8_t numTextures = Texture::NumberActiveTextures();
+ uint16_t stride = sizeof(float)*8;
+
+ //apply all the active Textures
+ for (uint8_t i = 0; i < numTextures; ++i)
+ {
+ glActiveTexture( GL_TEXTURE0+i );
+ glClientActiveTextureARB(GL_TEXTURE0+i);
+ glTexCoordPointer(2, GL_FLOAT, stride, BUFFER_OFFSET(sizeof(float)*0));
+ }
+
+ glPushAttrib( GL_CURRENT_BIT | GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_POLYGON_BIT);
+// glEnable(GL_CULL_FACE);
+ glCullFace(GL_FRONT);
+
+ glDisable(GL_DEPTH_TEST);
+ glDepthMask(false);
+ glBlendFunc(GL_ONE, GL_ONE);
+
+ CURRENTRENDERGRAPH->DoDifferedLightPass();
+
+ glPopAttrib( );
+// glCullFace(GL_BACK);
+}
+
+RenderDifferedLights* RenderDifferedLights::Generate()
+{
+ return new RenderDifferedLights();
+}
+
+
+/****************************************************************************
+ * Copyright (C) 2009 by Joshua Allen *
+ * *
+ * *
+ * All rights reserved. *
+ * *
+ * Redistribution and use in source and binary forms, with or without *
+ * modification, are permitted provided that the following conditions *
+ * are met: *
+ * * Redistributions of source code must retain the above copyright *
+ * notice, this list of conditions and the following disclaimer. *
+ * * Redistributions in binary form must reproduce the above *
+ * copyright notice, this list of conditions and the following *
+ * disclaimer in the documentation and/or other materials provided *
+ * with the distribution. *
+ * * Neither the name of the Mercury Engine nor the names of its *
+ * contributors may be used to endorse or promote products derived *
+ * from this software without specific prior written permission. *
+ * *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR *
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT *
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, *
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY *
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE *
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
+ ***************************************************************************/
Added: Mercury2/src/RenderDifferedLights.h
===================================================================
--- Mercury2/src/RenderDifferedLights.h (rev 0)
+++ Mercury2/src/RenderDifferedLights.h 2009-08-05 00:44:07 UTC (rev 454)
@@ -0,0 +1,49 @@
+#ifndef RENDERDIFFEREDLIGHTS_H
+#define RENDERDIFFEREDLIGHTS_H
+
+#include <MercuryAsset.h>
+
+class RenderDifferedLights : public MercuryAsset
+{
+ public:
+ RenderDifferedLights();
+ virtual ~RenderDifferedLights();
+
+ virtual void Render(const MercuryNode* node);
+ static RenderDifferedLights* Generate();
+
+};
+
+#endif
+
+/****************************************************************************
+ * Copyright (C) 2009 by Joshua Allen *
+ * *
+ * *
+ * All rights reserved. *
+ * *
+ * Redistribution and use in source and binary forms, with or without *
+ * modification, are permitted provided that the following conditions *
+ * are met: *
+ * * Redistributions of source code must retain the above copyright *
+ * notice, this list of conditions and the following disclaimer. *
+ * * Redistributions in binary form must reproduce the above *
+ * copyright notice, this list of conditions and the following *
+ * disclaimer in the documentation and/or other materials provided *
+ * with the distribution. *
+ * * Neither the name of the Mercury Engine nor the names of its *
+ * contributors may be used to endorse or promote products derived *
+ * from this software without specific prior written permission. *
+ * *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR *
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT *
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, *
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY *
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE *
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
+ ***************************************************************************/
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|