BGE code cleanup: Removing OpenGL and bf_gpu code from KX_LightObject
authorMitchell Stokes <mogurijin@gmail.com>
Fri, 28 Mar 2014 05:32:06 +0000 (22:32 -0700)
committerMitchell Stokes <mogurijin@gmail.com>
Fri, 28 Mar 2014 05:32:06 +0000 (22:32 -0700)
The ultimate goal is to only allow the rasterizer to handle OpenGL and bf_gpu
calls. This commit creates a RAS_ILightObject interface and a RAS_OpenGLLight
implementation.

16 files changed:
source/gameengine/Converter/BL_BlenderDataConversion.cpp
source/gameengine/Ketsji/KX_KetsjiEngine.cpp
source/gameengine/Ketsji/KX_Light.cpp
source/gameengine/Ketsji/KX_Light.h
source/gameengine/Ketsji/KX_LightIpoSGController.cpp
source/gameengine/Ketsji/KX_LightIpoSGController.h
source/gameengine/Ketsji/KX_Scene.cpp
source/gameengine/Rasterizer/CMakeLists.txt
source/gameengine/Rasterizer/RAS_ILightObject.h [moved from source/gameengine/Rasterizer/RAS_LightObject.h with 71% similarity]
source/gameengine/Rasterizer/RAS_IRasterizer.h
source/gameengine/Rasterizer/RAS_OpenGLRasterizer/CMakeLists.txt
source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_OpenGLLight.cpp [new file with mode: 0644]
source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_OpenGLLight.h [new file with mode: 0644]
source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_OpenGLRasterizer.cpp
source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_OpenGLRasterizer.h
source/gameengine/VideoTexture/Texture.cpp

index 8779cdd3249fb07b73b58fa13ef33c117df0dd17..e604b7bf81f8f4738a3e74caf8f2b9886332607d 100644 (file)
@@ -68,6 +68,8 @@
 #include "KX_GameObject.h"
 #include "RAS_FramingManager.h"
 #include "RAS_MeshObject.h"
+#include "RAS_IRasterizer.h"
+#include "RAS_ILightObject.h"
 
 #include "KX_ConvertActuators.h"
 #include "KX_ConvertControllers.h"
@@ -1767,22 +1769,22 @@ static void BL_CreatePhysicsObjectNew(KX_GameObject* gameobj,
 
 static KX_LightObject *gamelight_from_blamp(Object *ob, Lamp *la, unsigned int layerflag, KX_Scene *kxscene, RAS_IRasterizer *rasterizer, KX_BlenderSceneConverter *converter)
 {
-       RAS_LightObject lightobj;
+       RAS_ILightObject *lightobj = rasterizer->CreateLight();
        KX_LightObject *gamelight;
        
-       lightobj.m_att1 = la->att1;
-       lightobj.m_att2 = (la->mode & LA_QUAD) ? la->att2 : 0.0f;
-       lightobj.m_red = la->r;
-       lightobj.m_green = la->g;
-       lightobj.m_blue = la->b;
-       lightobj.m_distance = la->dist;
-       lightobj.m_energy = la->energy;
-       lightobj.m_layer = layerflag;
-       lightobj.m_spotblend = la->spotblend;
-       lightobj.m_spotsize = la->spotsize;
+       lightobj->m_att1 = la->att1;
+       lightobj->m_att2 = (la->mode & LA_QUAD) ? la->att2 : 0.0f;
+       lightobj->m_color[0] = la->r;
+       lightobj->m_color[1] = la->g;
+       lightobj->m_color[2] = la->b;
+       lightobj->m_distance = la->dist;
+       lightobj->m_energy = la->energy;
+       lightobj->m_layer = layerflag;
+       lightobj->m_spotblend = la->spotblend;
+       lightobj->m_spotsize = la->spotsize;
        
-       lightobj.m_nodiffuse = (la->mode & LA_NO_DIFF) != 0;
-       lightobj.m_nospecular = (la->mode & LA_NO_SPEC) != 0;
+       lightobj->m_nodiffuse = (la->mode & LA_NO_DIFF) != 0;
+       lightobj->m_nospecular = (la->mode & LA_NO_SPEC) != 0;
        
        bool glslmat = converter->GetGLSLMaterials();
 
@@ -1790,18 +1792,18 @@ static KX_LightObject *gamelight_from_blamp(Object *ob, Lamp *la, unsigned int l
        if (glslmat==0) {
                if (la->mode & LA_NEG)
                {
-                       lightobj.m_red = -lightobj.m_red;
-                       lightobj.m_green = -lightobj.m_green;
-                       lightobj.m_blue = -lightobj.m_blue;
+                       lightobj->m_color[0] = -lightobj->m_color[0];
+                       lightobj->m_color[1] = -lightobj->m_color[1];
+                       lightobj->m_color[2] = -lightobj->m_color[2];
                }
        }
                
        if (la->type==LA_SUN) {
-               lightobj.m_type = RAS_LightObject::LIGHT_SUN;
+               lightobj->m_type = RAS_ILightObject::LIGHT_SUN;
        } else if (la->type==LA_SPOT) {
-               lightobj.m_type = RAS_LightObject::LIGHT_SPOT;
+               lightobj->m_type = RAS_ILightObject::LIGHT_SPOT;
        } else {
-               lightobj.m_type = RAS_LightObject::LIGHT_NORMAL;
+               lightobj->m_type = RAS_ILightObject::LIGHT_NORMAL;
        }
 
        gamelight = new KX_LightObject(kxscene, KX_Scene::m_callbacks, rasterizer,
index 1bcfc4c974bf3d6027bd01ab18f6c9cafec0bbad..231590cde830289632fd11e3d1933f44a95dc30b 100644 (file)
@@ -49,6 +49,7 @@
 #include "RAS_Rect.h"
 #include "RAS_IRasterizer.h"
 #include "RAS_ICanvas.h"
+#include "RAS_ILightObject.h"
 #include "MT_Vector3.h"
 #include "MT_Transform.h"
 #include "SCA_IInputDevice.h"
@@ -1156,10 +1157,11 @@ void KX_KetsjiEngine::RenderShadowBuffers(KX_Scene *scene)
                KX_GameObject *gameobj = (KX_GameObject*)lightlist->GetValue(i);
 
                KX_LightObject *light = (KX_LightObject*)gameobj;
+               RAS_ILightObject *raslight = light->GetLightData();
 
-               light->Update();
+               raslight->Update();
 
-               if (m_rasterizer->GetDrawingMode() == RAS_IRasterizer::KX_TEXTURED && light->HasShadowBuffer()) {
+               if (m_rasterizer->GetDrawingMode() == RAS_IRasterizer::KX_TEXTURED && raslight->HasShadowBuffer()) {
                        /* make temporary camera */
                        RAS_CameraData camdata = RAS_CameraData();
                        KX_Camera *cam = new KX_Camera(scene, scene->m_callbacks, camdata, true, true);
@@ -1172,10 +1174,10 @@ void KX_KetsjiEngine::RenderShadowBuffers(KX_Scene *scene)
                        m_rasterizer->SetDrawingMode(RAS_IRasterizer::KX_SHADOW);
 
                        /* binds framebuffer object, sets up camera .. */
-                       light->BindShadowBuffer(m_rasterizer, m_canvas, cam, camtrans);
+                       raslight->BindShadowBuffer(m_canvas, cam, camtrans);
 
                        /* update scene */
-                       scene->CalculateVisibleMeshes(m_rasterizer, cam, light->GetShadowLayer());
+                       scene->CalculateVisibleMeshes(m_rasterizer, cam, raslight->GetShadowLayer());
 
                        /* render */
                        m_rasterizer->ClearDepthBuffer();
@@ -1183,7 +1185,7 @@ void KX_KetsjiEngine::RenderShadowBuffers(KX_Scene *scene)
                        scene->RenderBuckets(camtrans, m_rasterizer);
 
                        /* unbind framebuffer object, restore drawmode, free camera */
-                       light->UnbindShadowBuffer(m_rasterizer);
+                       raslight->UnbindShadowBuffer();
                        m_rasterizer->SetDrawingMode(drawmode);
                        cam->Release();
                }
index 20db6d2dd37f5b583989c96becd37c268c7c2f96..88138318479c1c328f54eb03fc90aef7568ba284 100644 (file)
 
 #include <stdio.h>
 
-#include "GL/glew.h"
-
 #include "KX_Light.h"
 #include "KX_Camera.h"
 #include "RAS_IRasterizer.h"
 #include "RAS_ICanvas.h"
+#include "RAS_ILightObject.h"
 
 #include "KX_PyMath.h"
 
 
 KX_LightObject::KX_LightObject(void* sgReplicationInfo,SG_Callbacks callbacks,
                                RAS_IRasterizer* rasterizer,
-                               const RAS_LightObject&  lightobj,
+                               RAS_ILightObject* lightobj,
                                bool glsl)
        : KX_GameObject(sgReplicationInfo,callbacks),
          m_rasterizer(rasterizer)
 {
        m_lightobj = lightobj;
-       m_lightobj.m_scene = sgReplicationInfo;
-       m_lightobj.m_light = this;
-       m_rasterizer->AddLight(&m_lightobj);
-       m_glsl = glsl;
+       m_lightobj->m_scene = sgReplicationInfo;
+       m_lightobj->m_light = this;
+       m_rasterizer->AddLight(m_lightobj);
+       m_lightobj->m_glsl = glsl;
        m_blenderscene = ((KX_Scene*)sgReplicationInfo)->GetBlenderScene();
        m_base = NULL;
 };
@@ -73,18 +72,11 @@ KX_LightObject::KX_LightObject(void* sgReplicationInfo,SG_Callbacks callbacks,
 
 KX_LightObject::~KX_LightObject()
 {
-       GPULamp *lamp;
-       Lamp *la = (Lamp*)GetBlenderObject()->data;
-
-       if ((lamp = GetGPULamp())) {
-               float obmat[4][4] = {{0}};
-               GPU_lamp_update(lamp, 0, 0, obmat);
-               GPU_lamp_update_distance(lamp, la->dist, la->att1, la->att2);
-               GPU_lamp_update_spot(lamp, la->spotsize, la->spotblend);
+       if (m_lightobj) {
+               m_rasterizer->RemoveLight(m_lightobj);
+               delete(m_lightobj);
        }
 
-       m_rasterizer->RemoveLight(&m_lightobj);
-
        if (m_base) {
                BKE_scene_base_unlink(m_blenderscene, m_base);
                MEM_freeN(m_base);
@@ -99,225 +91,25 @@ CValue*            KX_LightObject::GetReplica()
 
        replica->ProcessReplica();
        
-       replica->m_lightobj.m_light = replica;
-       m_rasterizer->AddLight(&replica->m_lightobj);
+       replica->m_lightobj = m_lightobj->Clone();
+       replica->m_lightobj->m_light = replica;
+       m_rasterizer->AddLight(replica->m_lightobj);
        if (m_base)
                m_base = NULL;
 
        return replica;
 }
 
-bool KX_LightObject::ApplyLight(KX_Scene *kxscene, int oblayer, int slot)
-{
-       KX_Scene* lightscene = (KX_Scene*)m_lightobj.m_scene;
-       float vec[4];
-       int scenelayer = ~0;
-
-       if (kxscene && kxscene->GetBlenderScene())
-               scenelayer = kxscene->GetBlenderScene()->lay;
-       
-       /* only use lights in the same layer as the object */
-       if (!(m_lightobj.m_layer & oblayer))
-               return false;
-       /* only use lights in the same scene, and in a visible layer */
-       if (kxscene != lightscene || !(m_lightobj.m_layer & scenelayer))
-               return false;
-
-       // lights don't get their openGL matrix updated, do it now
-       if (GetSGNode()->IsDirty())
-               GetOpenGLMatrix();
-
-       MT_CmMatrix4x4& worldmatrix= *GetOpenGLMatrixPtr();
-
-       vec[0] = worldmatrix(0,3);
-       vec[1] = worldmatrix(1,3);
-       vec[2] = worldmatrix(2,3);
-       vec[3] = 1.0f;
-
-       if (m_lightobj.m_type==RAS_LightObject::LIGHT_SUN) {
-               
-               vec[0] = worldmatrix(0,2);
-               vec[1] = worldmatrix(1,2);
-               vec[2] = worldmatrix(2,2);
-               //vec[0] = base->object->obmat[2][0];
-               //vec[1] = base->object->obmat[2][1];
-               //vec[2] = base->object->obmat[2][2];
-               vec[3] = 0.0;
-               glLightfv((GLenum)(GL_LIGHT0+slot), GL_POSITION, vec); 
-       }
-       else {
-               //vec[3] = 1.0;
-               glLightfv((GLenum)(GL_LIGHT0+slot), GL_POSITION, vec); 
-               glLightf((GLenum)(GL_LIGHT0+slot), GL_CONSTANT_ATTENUATION, 1.0);
-               glLightf((GLenum)(GL_LIGHT0+slot), GL_LINEAR_ATTENUATION, m_lightobj.m_att1/m_lightobj.m_distance);
-               // without this next line it looks backward compatible.
-               //attennuation still is acceptable 
-               glLightf((GLenum)(GL_LIGHT0+slot), GL_QUADRATIC_ATTENUATION, m_lightobj.m_att2/(m_lightobj.m_distance*m_lightobj.m_distance)); 
-               
-               if (m_lightobj.m_type==RAS_LightObject::LIGHT_SPOT) {
-                       vec[0] = -worldmatrix(0,2);
-                       vec[1] = -worldmatrix(1,2);
-                       vec[2] = -worldmatrix(2,2);
-                       //vec[0] = -base->object->obmat[2][0];
-                       //vec[1] = -base->object->obmat[2][1];
-                       //vec[2] = -base->object->obmat[2][2];
-                       glLightfv((GLenum)(GL_LIGHT0+slot), GL_SPOT_DIRECTION, vec);
-                       glLightf((GLenum)(GL_LIGHT0+slot), GL_SPOT_CUTOFF, RAD2DEGF(m_lightobj.m_spotsize * 0.5f));
-                       glLightf((GLenum)(GL_LIGHT0+slot), GL_SPOT_EXPONENT, 128.0f * m_lightobj.m_spotblend);
-               }
-               else {
-                       glLightf((GLenum)(GL_LIGHT0+slot), GL_SPOT_CUTOFF, 180.0);
-               }
-       }
-       
-       if (m_lightobj.m_nodiffuse) {
-               vec[0] = vec[1] = vec[2] = vec[3] = 0.0;
-       }
-       else {
-               vec[0] = m_lightobj.m_energy*m_lightobj.m_red;
-               vec[1] = m_lightobj.m_energy*m_lightobj.m_green;
-               vec[2] = m_lightobj.m_energy*m_lightobj.m_blue;
-               vec[3] = 1.0;
-       }
-
-       glLightfv((GLenum)(GL_LIGHT0+slot), GL_DIFFUSE, vec);
-       if (m_lightobj.m_nospecular)
-       {
-               vec[0] = vec[1] = vec[2] = vec[3] = 0.0;
-       }
-       else if (m_lightobj.m_nodiffuse) {
-               vec[0] = m_lightobj.m_energy*m_lightobj.m_red;
-               vec[1] = m_lightobj.m_energy*m_lightobj.m_green;
-               vec[2] = m_lightobj.m_energy*m_lightobj.m_blue;
-               vec[3] = 1.0;
-       }
-
-       glLightfv((GLenum)(GL_LIGHT0+slot), GL_SPECULAR, vec);
-       glEnable((GLenum)(GL_LIGHT0+slot));
-
-       return true;
-}
-
-GPULamp *KX_LightObject::GetGPULamp()
-{
-       if (m_glsl)
-               return GPU_lamp_from_blender(m_blenderscene, GetBlenderObject(), GetBlenderGroupObject());
-       else
-               return NULL;
-}
-
-void KX_LightObject::Update()
-{
-       GPULamp *lamp;
-
-       if ((lamp = GetGPULamp()) != NULL && GetSGNode()) {
-               float obmat[4][4];
-               // lights don't get their openGL matrix updated, do it now
-               if (GetSGNode()->IsDirty())
-                       GetOpenGLMatrix();
-               double *dobmat = GetOpenGLMatrixPtr()->getPointer();
-
-               for (int i=0; i<4; i++)
-                       for (int j=0; j<4; j++, dobmat++)
-                               obmat[i][j] = (float)*dobmat;
-
-               GPU_lamp_update(lamp, m_lightobj.m_layer, 0, obmat);
-               GPU_lamp_update_colors(lamp, m_lightobj.m_red, m_lightobj.m_green, 
-                       m_lightobj.m_blue, m_lightobj.m_energy);
-               GPU_lamp_update_distance(lamp, m_lightobj.m_distance, m_lightobj.m_att1, m_lightobj.m_att2);
-               GPU_lamp_update_spot(lamp, m_lightobj.m_spotsize, m_lightobj.m_spotblend);
-       }
-}
-
 void KX_LightObject::UpdateScene(KX_Scene *kxscene)
 {
-       m_lightobj.m_scene = (void*)kxscene;
+       m_lightobj->m_scene = (void*)kxscene;
        m_blenderscene = kxscene->GetBlenderScene();
        m_base = BKE_scene_base_add(m_blenderscene, GetBlenderObject());
 }
 
-bool KX_LightObject::HasShadowBuffer()
-{
-       GPULamp *lamp;
-
-       if ((lamp = GetGPULamp()))
-               return GPU_lamp_has_shadow_buffer(lamp);
-       else
-               return false;
-}
-
-int KX_LightObject::GetShadowLayer()
-{
-       GPULamp *lamp;
-
-       if ((lamp = GetGPULamp()))
-               return GPU_lamp_shadow_layer(lamp);
-       else
-               return 0;
-}
-
-void KX_LightObject::BindShadowBuffer(RAS_IRasterizer *ras, RAS_ICanvas *canvas, KX_Camera *cam, MT_Transform& camtrans)
-{
-       GPULamp *lamp;
-       float viewmat[4][4], winmat[4][4];
-       int winsize;
-
-       /* bind framebuffer */
-       lamp = GetGPULamp();
-       GPU_lamp_shadow_buffer_bind(lamp, viewmat, &winsize, winmat);
-
-       if (GPU_lamp_shadow_buffer_type(lamp) == LA_SHADMAP_VARIANCE)
-               ras->SetUsingOverrideShader(true);
-
-       /* GPU_lamp_shadow_buffer_bind() changes the viewport, so update the canvas */
-       canvas->UpdateViewPort(0, 0, winsize, winsize);
-
-       /* setup camera transformation */
-       MT_Matrix4x4 modelviewmat((float*)viewmat);
-       MT_Matrix4x4 projectionmat((float*)winmat);
-
-       MT_Transform trans = MT_Transform((float*)viewmat);
-       camtrans.invert(trans);
-
-       cam->SetModelviewMatrix(modelviewmat);
-       cam->SetProjectionMatrix(projectionmat);
-       
-       cam->NodeSetLocalPosition(camtrans.getOrigin());
-       cam->NodeSetLocalOrientation(camtrans.getBasis());
-       cam->NodeUpdateGS(0);
-
-       /* setup rasterizer transformations */
-       /* SetViewMatrix may use stereomode which we temporarily disable here */
-       RAS_IRasterizer::StereoMode stereomode = ras->GetStereoMode();
-       ras->SetStereoMode(RAS_IRasterizer::RAS_STEREO_NOSTEREO);
-       ras->SetProjectionMatrix(projectionmat);
-       ras->SetViewMatrix(modelviewmat, cam->NodeGetWorldOrientation(), cam->NodeGetWorldPosition(), cam->GetCameraData()->m_perspective);
-       ras->SetStereoMode(stereomode);
-}
-
-void KX_LightObject::UnbindShadowBuffer(RAS_IRasterizer *ras)
+void KX_LightObject::SetLayer(int layer)
 {
-       GPULamp *lamp = GetGPULamp();
-       GPU_lamp_shadow_buffer_unbind(lamp);
-
-       if (GPU_lamp_shadow_buffer_type(lamp) == LA_SHADMAP_VARIANCE)
-               ras->SetUsingOverrideShader(false);
-}
-
-struct Image *KX_LightObject::GetTextureImage(short texslot)
-{
-       Lamp *la = (Lamp*)GetBlenderObject()->data;
-
-       if (texslot >= MAX_MTEX || texslot < 0)
-       {
-               printf("KX_LightObject::GetTextureImage(): texslot exceeds slot bounds (0-%d)\n", MAX_MTEX-1);
-               return NULL;
-       }
-       
-       if (la->mtex[texslot])
-               return la->mtex[texslot]->tex->ima;
-
-       return NULL;
+       m_lightobj->m_layer = layer;
 }
 
 #ifdef WITH_PYTHON
@@ -358,14 +150,14 @@ PyMethodDef KX_LightObject::Methods[] = {
 };
 
 PyAttributeDef KX_LightObject::Attributes[] = {
-       KX_PYATTRIBUTE_INT_RW("layer", 1, 20, true, KX_LightObject, m_lightobj.m_layer),
-       KX_PYATTRIBUTE_FLOAT_RW("energy", 0, 10, KX_LightObject, m_lightobj.m_energy),
-       KX_PYATTRIBUTE_FLOAT_RW("distance", 0.01, 5000, KX_LightObject, m_lightobj.m_distance),
+       KX_PYATTRIBUTE_RW_FUNCTION("layer", KX_LightObject, pyattr_get_layer, pyattr_set_layer),
+       KX_PYATTRIBUTE_RW_FUNCTION("energy", KX_LightObject, pyattr_get_energy, pyattr_set_energy),
+       KX_PYATTRIBUTE_RW_FUNCTION("distance", KX_LightObject, pyattr_get_distance, pyattr_set_distance),
        KX_PYATTRIBUTE_RW_FUNCTION("color", KX_LightObject, pyattr_get_color, pyattr_set_color),
-       KX_PYATTRIBUTE_FLOAT_RW("lin_attenuation", 0, 1, KX_LightObject, m_lightobj.m_att1),
-       KX_PYATTRIBUTE_FLOAT_RW("quad_attenuation", 0, 1, KX_LightObject, m_lightobj.m_att2),
+       KX_PYATTRIBUTE_RW_FUNCTION("lin_attenuation", KX_LightObject, pyattr_get_lin_attenuation, pyattr_set_lin_attenuation),
+       KX_PYATTRIBUTE_RW_FUNCTION("quad_attenuation", KX_LightObject, pyattr_get_quad_attenuation, pyattr_set_quad_attenuation),
        KX_PYATTRIBUTE_RW_FUNCTION("spotsize", KX_LightObject, pyattr_get_spotsize, pyattr_set_spotsize),
-       KX_PYATTRIBUTE_FLOAT_RW("spotblend", 0, 1, KX_LightObject, m_lightobj.m_spotblend),
+       KX_PYATTRIBUTE_RW_FUNCTION("spotblend", KX_LightObject, pyattr_get_spotblend, pyattr_set_spotblend),
        KX_PYATTRIBUTE_RO_FUNCTION("SPOT", KX_LightObject, pyattr_get_typeconst),
        KX_PYATTRIBUTE_RO_FUNCTION("SUN", KX_LightObject, pyattr_get_typeconst),
        KX_PYATTRIBUTE_RO_FUNCTION("NORMAL", KX_LightObject, pyattr_get_typeconst),
@@ -373,10 +165,85 @@ PyAttributeDef KX_LightObject::Attributes[] = {
        { NULL }        //Sentinel
 };
 
+PyObject *KX_LightObject::pyattr_get_layer(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
+{
+       KX_LightObject* self = static_cast<KX_LightObject*>(self_v);
+       return PyLong_FromLong(self->m_lightobj->m_layer);
+}
+
+int KX_LightObject::pyattr_set_layer(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
+{
+       KX_LightObject* self = static_cast<KX_LightObject*>(self_v);
+
+       if (PyLong_Check(value)) {
+               int val = PyLong_AsLong(value);
+               if (val < 1)
+                       val = 1;
+               else if (val > 20)
+                       val = 20;
+
+               self->m_lightobj->m_layer = val;
+               return PY_SET_ATTR_SUCCESS;
+       }
+
+       PyErr_Format(PyExc_TypeError, "expected an integer for attribute \"%s\"", attrdef->m_name);
+       return PY_SET_ATTR_FAIL;
+}
+
+PyObject *KX_LightObject::pyattr_get_energy(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
+{
+       KX_LightObject* self = static_cast<KX_LightObject*>(self_v);
+       return PyFloat_FromDouble(self->m_lightobj->m_energy);
+}
+
+int KX_LightObject::pyattr_set_energy(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
+{
+       KX_LightObject* self = static_cast<KX_LightObject*>(self_v);
+
+       if (PyFloat_Check(value)) {
+               float val = PyFloat_AsDouble(value);
+               if (val < 0)
+                       val = 0;
+               else if (val > 10)
+                       val = 10;
+
+               self->m_lightobj->m_energy = val;
+               return PY_SET_ATTR_SUCCESS;
+       }
+
+       PyErr_Format(PyExc_TypeError, "expected float value for attribute \"%s\"", attrdef->m_name);
+       return PY_SET_ATTR_FAIL;
+}
+
+PyObject *KX_LightObject::pyattr_get_distance(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
+{
+       KX_LightObject* self = static_cast<KX_LightObject*>(self_v);
+       return PyFloat_FromDouble(self->m_lightobj->m_distance);
+}
+
+int KX_LightObject::pyattr_set_distance(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
+{
+       KX_LightObject* self = static_cast<KX_LightObject*>(self_v);
+
+       if (PyFloat_Check(value)) {
+               float val = PyFloat_AsDouble(value);
+               if (val < 0.01)
+                       val = 0.01;
+               else if (val > 5000.f)
+                       val = 5000.f;
+
+               self->m_lightobj->m_energy = val;
+               return PY_SET_ATTR_SUCCESS;
+       }
+
+       PyErr_Format(PyExc_TypeError, "expected float value for attribute \"%s\"", attrdef->m_name);
+       return PY_SET_ATTR_FAIL;
+}
+
 PyObject *KX_LightObject::pyattr_get_color(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 {
        KX_LightObject* self = static_cast<KX_LightObject*>(self_v);
-       return Py_BuildValue("[fff]", self->m_lightobj.m_red, self->m_lightobj.m_green, self->m_lightobj.m_blue);
+       return Py_BuildValue("[fff]", self->m_lightobj->m_color[0], self->m_lightobj->m_color[1], self->m_lightobj->m_color[1]);
 }
 
 int KX_LightObject::pyattr_set_color(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
@@ -386,34 +253,111 @@ int KX_LightObject::pyattr_set_color(void *self_v, const KX_PYATTRIBUTE_DEF *att
        MT_Vector3 color;
        if (PyVecTo(value, color))
        {
-               self->m_lightobj.m_red = color[0];
-               self->m_lightobj.m_green = color[1];
-               self->m_lightobj.m_blue = color[2];
+               self->m_lightobj->m_color[0] = color[0];
+               self->m_lightobj->m_color[1] = color[1];
+               self->m_lightobj->m_color[2] = color[2];
                return PY_SET_ATTR_SUCCESS;
        }
        return PY_SET_ATTR_FAIL;
 }
 
+PyObject *KX_LightObject::pyattr_get_lin_attenuation(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
+{
+       KX_LightObject* self = static_cast<KX_LightObject*>(self_v);
+       return PyFloat_FromDouble(self->m_lightobj->m_att1);
+}
+
+int KX_LightObject::pyattr_set_lin_attenuation(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
+{
+       KX_LightObject* self = static_cast<KX_LightObject*>(self_v);
+
+       if (PyFloat_Check(value)) {
+               float val = PyFloat_AsDouble(value);
+               if (val < 0.f)
+                       val = 0.f;
+               else if (val > 1.f)
+                       val = 1.f;
+
+               self->m_lightobj->m_att1 = val;
+               return PY_SET_ATTR_SUCCESS;
+       }
+
+       PyErr_Format(PyExc_TypeError, "expected float value for attribute \"%s\"", attrdef->m_name);
+       return PY_SET_ATTR_FAIL;
+}
+
+PyObject *KX_LightObject::pyattr_get_quad_attenuation(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
+{
+       KX_LightObject* self = static_cast<KX_LightObject*>(self_v);
+       return PyFloat_FromDouble(self->m_lightobj->m_att2);
+}
+
+int KX_LightObject::pyattr_set_quad_attenuation(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
+{
+       KX_LightObject* self = static_cast<KX_LightObject*>(self_v);
+
+       if (PyFloat_Check(value)) {
+               float val = PyFloat_AsDouble(value);
+               if (val < 0.f)
+                       val = 0.f;
+               else if (val > 1.f)
+                       val = 1.f;
+
+               self->m_lightobj->m_att2 = val;
+               return PY_SET_ATTR_SUCCESS;
+       }
+
+       PyErr_Format(PyExc_TypeError, "expected float value for attribute \"%s\"", attrdef->m_name);
+       return PY_SET_ATTR_FAIL;
+}
+
 PyObject *KX_LightObject::pyattr_get_spotsize(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 {
        KX_LightObject* self = static_cast<KX_LightObject*>(self_v);
-       return Py_BuildValue("f", RAD2DEGF(self->m_lightobj.m_spotsize));
+       return PyFloat_FromDouble(RAD2DEG(self->m_lightobj->m_spotsize));
 }
 
 int KX_LightObject::pyattr_set_spotsize(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
 {
        KX_LightObject* self = static_cast<KX_LightObject*>(self_v);
 
-       float spotsize = (float)PyFloat_AsDouble(value);
-       if (PyErr_Occurred())
-               return PY_SET_ATTR_FAIL;
+       if (PyFloat_Check(value)) {
+               float val = PyFloat_AsDouble(value);
+               if (val < 0.f)
+                       val = 0.f;
+               else if (val > 180.f)
+                       val = 180.f;
 
-       if (spotsize < 1.0f)
-               spotsize = 1.0f;
-       else if (spotsize > 180.0f)
-               spotsize = 180.0f;
-       self->m_lightobj.m_spotsize = DEG2RADF(spotsize);
-       return PY_SET_ATTR_SUCCESS;
+               self->m_lightobj->m_spotsize = DEG2RAD(val);
+               return PY_SET_ATTR_SUCCESS;
+       }
+
+       PyErr_Format(PyExc_TypeError, "expected float value for attribute \"%s\"", attrdef->m_name);
+       return PY_SET_ATTR_FAIL;
+}
+PyObject *KX_LightObject::pyattr_get_spotblend(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
+{
+       KX_LightObject* self = static_cast<KX_LightObject*>(self_v);
+       return PyFloat_FromDouble(self->m_lightobj->m_spotblend);
+}
+
+int KX_LightObject::pyattr_set_spotblend(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
+{
+       KX_LightObject* self = static_cast<KX_LightObject*>(self_v);
+
+       if (PyFloat_Check(value)) {
+               float val = PyFloat_AsDouble(value);
+               if (val < 0.f)
+                       val = 0.f;
+               else if (val > 1.f)
+                       val = 1.f;
+
+               self->m_lightobj->m_spotblend = val;
+               return PY_SET_ATTR_SUCCESS;
+       }
+
+       PyErr_Format(PyExc_TypeError, "expected float value for attribute \"%s\"", attrdef->m_name);
+       return PY_SET_ATTR_FAIL;
 }
 
 PyObject *KX_LightObject::pyattr_get_typeconst(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
@@ -423,11 +367,11 @@ PyObject *KX_LightObject::pyattr_get_typeconst(void *self_v, const KX_PYATTRIBUT
        const char* type = attrdef->m_name;
 
        if (!strcmp(type, "SPOT")) {
-               retvalue = PyLong_FromLong(RAS_LightObject::LIGHT_SPOT);
+               retvalue = PyLong_FromLong(RAS_ILightObject::LIGHT_SPOT);
        } else if (!strcmp(type, "SUN")) {
-               retvalue = PyLong_FromLong(RAS_LightObject::LIGHT_SUN);
+               retvalue = PyLong_FromLong(RAS_ILightObject::LIGHT_SUN);
        } else if (!strcmp(type, "NORMAL")) {
-               retvalue = PyLong_FromLong(RAS_LightObject::LIGHT_NORMAL);
+               retvalue = PyLong_FromLong(RAS_ILightObject::LIGHT_NORMAL);
        }
        else {
                /* should never happen */
@@ -441,7 +385,7 @@ PyObject *KX_LightObject::pyattr_get_typeconst(void *self_v, const KX_PYATTRIBUT
 PyObject *KX_LightObject::pyattr_get_type(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 {
        KX_LightObject* self = static_cast<KX_LightObject*>(self_v);
-       return PyLong_FromLong(self->m_lightobj.m_type);
+       return PyLong_FromLong(self->m_lightobj->m_type);
 }
 
 int KX_LightObject::pyattr_set_type(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
@@ -455,13 +399,13 @@ int KX_LightObject::pyattr_set_type(void* self_v, const KX_PYATTRIBUTE_DEF *attr
        
        switch (val) {
                case 0:
-                       self->m_lightobj.m_type = self->m_lightobj.LIGHT_SPOT;
+                       self->m_lightobj->m_type = self->m_lightobj->LIGHT_SPOT;
                        break;
                case 1:
-                       self->m_lightobj.m_type = self->m_lightobj.LIGHT_SUN;
+                       self->m_lightobj->m_type = self->m_lightobj->LIGHT_SUN;
                        break;
                case 2:
-                       self->m_lightobj.m_type = self->m_lightobj.LIGHT_NORMAL;
+                       self->m_lightobj->m_type = self->m_lightobj->LIGHT_NORMAL;
                        break;
        }
 
index d689287504246e5af587427c13f7149e7d9032a2..503ed7411e956afdb7137e0c97b828f487a9dc04 100644 (file)
@@ -32,7 +32,6 @@
 #ifndef __KX_LIGHT_H__
 #define __KX_LIGHT_H__
 
-#include "RAS_LightObject.h"
 #include "KX_GameObject.h"
 
 struct GPULamp;
@@ -40,46 +39,47 @@ struct Scene;
 struct Base;
 class KX_Camera;
 class RAS_IRasterizer;
+class RAS_ILightObject;
 class MT_Transform;
 
 class KX_LightObject : public KX_GameObject
 {
        Py_Header
 protected:
-       RAS_LightObject         m_lightobj;
+       RAS_ILightObject*               m_lightobj;
        class RAS_IRasterizer*  m_rasterizer;   //needed for registering and replication of lightobj
-       bool                            m_glsl;
        Scene*                          m_blenderscene;
        Base*                           m_base;
 
 public:
-       KX_LightObject(void* sgReplicationInfo,SG_Callbacks callbacks,RAS_IRasterizer* rasterizer,const RAS_LightObject&        lightobj, bool glsl);
+       KX_LightObject(void* sgReplicationInfo,SG_Callbacks callbacks,RAS_IRasterizer* rasterizer,RAS_ILightObject*     lightobj, bool glsl);
        virtual ~KX_LightObject();
        virtual CValue*         GetReplica();
-       RAS_LightObject*        GetLightData() { return &m_lightobj;}
-
-       /* OpenGL Light */
-       bool ApplyLight(KX_Scene *kxscene, int oblayer, int slot);
-
-       /* GLSL Light */
-       struct GPULamp *GetGPULamp();
-       bool HasShadowBuffer();
-       int GetShadowLayer();
-       void BindShadowBuffer(RAS_IRasterizer *ras, class RAS_ICanvas *canvas, class KX_Camera *cam, class MT_Transform& camtrans);
-       void UnbindShadowBuffer(RAS_IRasterizer *ras);
-       struct Image *GetTextureImage(short texslot);
-       void Update();
+       RAS_ILightObject*       GetLightData() { return m_lightobj;}
        
        void UpdateScene(class KX_Scene *kxscene);
+       void SetLayer(int layer);
 
        virtual int GetGameObjectType() { return OBJ_LIGHT; }
 
 #ifdef WITH_PYTHON
        /* attributes */
+       static PyObject*        pyattr_get_layer(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
+       static int                      pyattr_set_layer(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
+       static PyObject*        pyattr_get_energy(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
+       static int                      pyattr_set_energy(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
+       static PyObject*        pyattr_get_distance(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
+       static int                      pyattr_set_distance(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
        static PyObject*        pyattr_get_color(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
        static int                      pyattr_set_color(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
+       static PyObject*        pyattr_get_lin_attenuation(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
+       static int                      pyattr_set_lin_attenuation(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
+       static PyObject*        pyattr_get_quad_attenuation(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
+       static int                      pyattr_set_quad_attenuation(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
        static PyObject*        pyattr_get_spotsize(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
        static int                      pyattr_set_spotsize(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
+       static PyObject*        pyattr_get_spotblend(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
+       static int                      pyattr_set_spotblend(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
        static PyObject*        pyattr_get_typeconst(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
        static PyObject*        pyattr_get_type(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
        static int                      pyattr_set_type(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
index 1e65c6a6fc5f7aeea5d7c1b2d8150af436bb3595..6b641f7a63f78c9b0dd991280889aa6743ca85a8 100644 (file)
@@ -33,7 +33,7 @@
 #include "KX_LightIpoSGController.h"
 #include "KX_ScalarInterpolator.h"
 #include "KX_Light.h"
-#include "RAS_LightObject.h"
+#include "RAS_ILightObject.h"
 
 #if defined(_WIN64)
 typedef unsigned __int64 uint_ptr;
@@ -50,7 +50,7 @@ bool KX_LightIpoSGController::Update(double currentTime)
                        (*i)->Execute(m_ipotime);//currentTime);
                }
                
-               RAS_LightObject *lightobj;
+               RAS_ILightObject *lightobj;
 
                SG_Spatial* ob = (SG_Spatial*)m_pObject;
                KX_LightObject* kxlight = (KX_LightObject*) ob->GetSGClientObject();
@@ -62,9 +62,9 @@ bool KX_LightIpoSGController::Update(double currentTime)
                }
 
                if (m_modify_color) {
-                       lightobj->m_red   = m_col_rgb[0];
-                       lightobj->m_green = m_col_rgb[1];
-                       lightobj->m_blue  = m_col_rgb[2];
+                       lightobj->m_color[0]   = m_col_rgb[0];
+                       lightobj->m_color[1] = m_col_rgb[1];
+                       lightobj->m_color[2]  = m_col_rgb[2];
                }
 
                if (m_modify_dist) {
index c82fe7bcad5ae3eba0014da295b8395cd6b79ed9..8f5e8ba7187acfdd35ae2477df719ac12aa95b81 100644 (file)
@@ -37,7 +37,7 @@
 
 #include "KX_IInterpolator.h"
 
-struct RAS_LightObject;
+struct RAS_ILightObject;
 
 class KX_LightIpoSGController : public SG_Controller
 {
index b66201f4b47eaa592570ae741742faa400de7311..a9ac13c755f3faebf526a980abb3abfd347b9b1d 100644 (file)
@@ -837,7 +837,7 @@ void KX_Scene::DupliGroupRecurse(CValue* obj, int level)
                if ((*git)->GetGameObjectType()==SCA_IObject::OBJ_LIGHT)
                {
                        KX_LightObject* lightobj = static_cast<KX_LightObject*>(*git);
-                       lightobj->GetLightData()->m_layer = groupobj->GetLayer();
+                       lightobj->SetLayer(groupobj->GetLayer());
                }
        }
 
@@ -947,7 +947,7 @@ SCA_IObject* KX_Scene::AddReplicaObject(class CValue* originalobject,
                if ((*git)->GetGameObjectType()==SCA_IObject::OBJ_LIGHT)
                {
                        KX_LightObject* lightobj = static_cast<KX_LightObject*>(*git);
-                       lightobj->GetLightData()->m_layer = parentobj->GetLayer();
+                       lightobj->SetLayer(parentobj->GetLayer());
                }
        }
 
index 879115add477b9430e7c45db86de25c74af1edc4..e254bf9b1c5c22e25343b16862ce6089d1041e26 100644 (file)
@@ -61,7 +61,7 @@ set(SRC
        RAS_ICanvas.h
        RAS_IPolygonMaterial.h
        RAS_IRasterizer.h
-       RAS_LightObject.h
+       RAS_ILightObject.h
        RAS_MaterialBucket.h
        RAS_MeshObject.h
        RAS_ObjectColor.h
similarity index 71%
rename from source/gameengine/Rasterizer/RAS_LightObject.h
rename to source/gameengine/Rasterizer/RAS_ILightObject.h
index 79818def5f02c8bc1fd27fb55bcca38166c0ac5a..28c5d9aedd7d23a510a625291aaea2783bd0eb2e 100644 (file)
@@ -20,7 +20,7 @@
  *
  * The Original Code is: all of this file.
  *
- * Contributor(s): none yet.
+ * Contributor(s): Mitchell Stokes
  *
  * ***** END GPL LICENSE BLOCK *****
  */
 #ifndef __RAS_LIGHTOBJECT_H__
 #define __RAS_LIGHTOBJECT_H__
 
-#include "MT_CmMatrix4x4.h"
+class RAS_ICanvas;
 
-struct RAS_LightObject
+class KX_Camera;
+class KX_Scene;
+
+class MT_Transform;
+
+struct Image;
+
+class RAS_ILightObject
 {
+public:
        enum LightType {
                LIGHT_SPOT,
                LIGHT_SUN,
@@ -48,10 +56,8 @@ struct RAS_LightObject
        
        float   m_energy;
        float   m_distance;
-       
-       float   m_red;
-       float   m_green;
-       float   m_blue;
+
+       float   m_color[3];
 
        float   m_att1;
        float   m_att2;
@@ -62,6 +68,17 @@ struct RAS_LightObject
        
        bool    m_nodiffuse;
        bool    m_nospecular;
+       bool    m_glsl;
+
+       virtual ~RAS_ILightObject() {}
+       virtual RAS_ILightObject* Clone() = 0;
+
+       virtual bool HasShadowBuffer() = 0;
+       virtual int GetShadowLayer() = 0;
+       virtual void BindShadowBuffer(RAS_ICanvas *canvas, KX_Camera *cam, MT_Transform& camtrans) = 0;
+       virtual void UnbindShadowBuffer() = 0;
+       virtual Image *GetTextureImage(short texslot) = 0;
+       virtual void Update() = 0;
 };
 
 #endif  /* __RAS_LIGHTOBJECT_H__ */
index 887ebc476b082de46c9dba6ef8b0e87e4e6b4dc5..d7b5221319167910b5c052fe5060b53fb825b3d7 100644 (file)
@@ -53,6 +53,7 @@ using namespace std;
 class RAS_ICanvas;
 class RAS_IPolyMaterial;
 class RAS_MeshSlot;
+class RAS_ILightObject;
 
 typedef vector<unsigned short> KX_IndexArray;
 typedef vector<RAS_TexVert> KX_VertexArray;
@@ -470,9 +471,11 @@ public:
 
        virtual void PopMatrix() = 0;
 
-       virtual void AddLight(struct RAS_LightObject *lightobject) = 0;
+       virtual RAS_ILightObject *CreateLight() = 0;
 
-       virtual void RemoveLight(struct RAS_LightObject *lightobject) = 0;
+       virtual void AddLight(RAS_ILightObject *lightobject) = 0;
+
+       virtual void RemoveLight(RAS_ILightObject *lightobject) = 0;
 
        virtual void MotionBlur() = 0;
 
index 76ac6316e3834476bb810c2c003d1e8a8f8e857d..f0410ba891d7a96a9c34afcf937c79aadd29fad8 100644 (file)
@@ -50,6 +50,7 @@ set(INC_SYS
 set(SRC
        RAS_GLExtensionManager.cpp
        RAS_ListRasterizer.cpp
+       RAS_OpenGLLight.cpp
        RAS_OpenGLRasterizer.cpp
        RAS_StorageIM.cpp
        RAS_StorageVA.cpp
@@ -58,6 +59,7 @@ set(SRC
        RAS_GLExtensionManager.h
        RAS_IStorage.h
        RAS_ListRasterizer.h
+       RAS_OpenGLLight.h
        RAS_OpenGLRasterizer.h
        RAS_StorageIM.h
        RAS_StorageVA.h
diff --git a/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_OpenGLLight.cpp b/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_OpenGLLight.cpp
new file mode 100644 (file)
index 0000000..91ad283
--- /dev/null
@@ -0,0 +1,276 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+ * All rights reserved.
+ *
+ * The Original Code is: all of this file.
+ *
+ * Contributor(s): Mitchell Stokes
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+#include "GL/glew.h"
+
+#include "RAS_OpenGLLight.h"
+#include "RAS_OpenGLRasterizer.h"
+#include "RAS_ICanvas.h"
+
+#include "MT_CmMatrix4x4.h"
+
+#include "KX_Camera.h"
+#include "KX_Light.h"
+#include "KX_Scene.h"
+
+#include "DNA_lamp_types.h"
+#include "DNA_scene_types.h"
+
+#include "GPU_material.h"
+
+RAS_OpenGLLight::RAS_OpenGLLight(RAS_OpenGLRasterizer *ras)
+    :m_rasterizer(ras)
+{
+}
+
+RAS_OpenGLLight::~RAS_OpenGLLight()
+{
+       GPULamp *lamp;
+       KX_LightObject* kxlight = (KX_LightObject*)m_light;
+       Lamp *la = (Lamp*)kxlight->GetBlenderObject()->data;
+
+       if ((lamp = GetGPULamp())) {
+               float obmat[4][4] = {{0}};
+               GPU_lamp_update(lamp, 0, 0, obmat);
+               GPU_lamp_update_distance(lamp, la->dist, la->att1, la->att2);
+               GPU_lamp_update_spot(lamp, la->spotsize, la->spotblend);
+       }
+}
+
+bool RAS_OpenGLLight::ApplyFixedFunctionLighting(KX_Scene *kxscene, int oblayer, int slot)
+{
+       KX_Scene* lightscene = (KX_Scene*)m_scene;
+       KX_LightObject* kxlight = (KX_LightObject*)m_light;
+       float vec[4];
+       int scenelayer = ~0;
+
+       if (kxscene && kxscene->GetBlenderScene())
+               scenelayer = kxscene->GetBlenderScene()->lay;
+
+       /* only use lights in the same layer as the object */
+       if (!(m_layer & oblayer))
+               return false;
+       /* only use lights in the same scene, and in a visible layer */
+       if (kxscene != lightscene || !(m_layer & scenelayer))
+               return false;
+
+       // lights don't get their openGL matrix updated, do it now
+       if (kxlight->GetSGNode()->IsDirty())
+               kxlight->GetOpenGLMatrix();
+
+       MT_CmMatrix4x4& worldmatrix= *kxlight->GetOpenGLMatrixPtr();
+
+       vec[0] = worldmatrix(0,3);
+       vec[1] = worldmatrix(1,3);
+       vec[2] = worldmatrix(2,3);
+       vec[3] = 1.0f;
+
+       if (m_type==RAS_ILightObject::LIGHT_SUN) {
+
+               vec[0] = worldmatrix(0,2);
+               vec[1] = worldmatrix(1,2);
+               vec[2] = worldmatrix(2,2);
+               //vec[0] = base->object->obmat[2][0];
+               //vec[1] = base->object->obmat[2][1];
+               //vec[2] = base->object->obmat[2][2];
+               vec[3] = 0.0;
+               glLightfv((GLenum)(GL_LIGHT0+slot), GL_POSITION, vec);
+       }
+       else {
+               //vec[3] = 1.0;
+               glLightfv((GLenum)(GL_LIGHT0+slot), GL_POSITION, vec);
+               glLightf((GLenum)(GL_LIGHT0+slot), GL_CONSTANT_ATTENUATION, 1.0);
+               glLightf((GLenum)(GL_LIGHT0+slot), GL_LINEAR_ATTENUATION, m_att1/m_distance);
+               // without this next line it looks backward compatible.
+               //attennuation still is acceptable
+               glLightf((GLenum)(GL_LIGHT0+slot), GL_QUADRATIC_ATTENUATION, m_att2/(m_distance*m_distance));
+
+               if (m_type==RAS_ILightObject::LIGHT_SPOT) {
+                       vec[0] = -worldmatrix(0,2);
+                       vec[1] = -worldmatrix(1,2);
+                       vec[2] = -worldmatrix(2,2);
+                       //vec[0] = -base->object->obmat[2][0];
+                       //vec[1] = -base->object->obmat[2][1];
+                       //vec[2] = -base->object->obmat[2][2];
+                       glLightfv((GLenum)(GL_LIGHT0+slot), GL_SPOT_DIRECTION, vec);
+                       glLightf((GLenum)(GL_LIGHT0+slot), GL_SPOT_CUTOFF, m_spotsize / 2.0f);
+                       glLightf((GLenum)(GL_LIGHT0+slot), GL_SPOT_EXPONENT, 128.0f * m_spotblend);
+               }
+               else {
+                       glLightf((GLenum)(GL_LIGHT0+slot), GL_SPOT_CUTOFF, 180.0);
+               }
+       }
+
+       if (m_nodiffuse) {
+               vec[0] = vec[1] = vec[2] = vec[3] = 0.0;
+       }
+       else {
+               vec[0] = m_energy*m_color[0];
+               vec[1] = m_energy*m_color[1];
+               vec[2] = m_energy*m_color[2];
+               vec[3] = 1.0;
+       }
+
+       glLightfv((GLenum)(GL_LIGHT0+slot), GL_DIFFUSE, vec);
+       if (m_nospecular)
+       {
+               vec[0] = vec[1] = vec[2] = vec[3] = 0.0;
+       }
+       else if (m_nodiffuse) {
+               vec[0] = m_energy*m_color[0];
+               vec[1] = m_energy*m_color[1];
+               vec[2] = m_energy*m_color[2];
+               vec[3] = 1.0;
+       }
+
+       glLightfv((GLenum)(GL_LIGHT0+slot), GL_SPECULAR, vec);
+       glEnable((GLenum)(GL_LIGHT0+slot));
+
+       return true;
+}
+
+GPULamp *RAS_OpenGLLight::GetGPULamp()
+{
+       KX_LightObject* kxlight = (KX_LightObject*)m_light;
+
+       if (m_glsl)
+               return GPU_lamp_from_blender(kxlight->GetScene()->GetBlenderScene(), kxlight->GetBlenderObject(), kxlight->GetBlenderGroupObject());
+       else
+               return NULL;
+}
+
+
+bool RAS_OpenGLLight::HasShadowBuffer()
+{
+       GPULamp *lamp;
+
+       if ((lamp = GetGPULamp()))
+               return GPU_lamp_has_shadow_buffer(lamp);
+       else
+               return false;
+}
+
+int RAS_OpenGLLight::GetShadowLayer()
+{
+       GPULamp *lamp;
+
+       if ((lamp = GetGPULamp()))
+               return GPU_lamp_shadow_layer(lamp);
+       else
+               return 0;
+}
+
+void RAS_OpenGLLight::BindShadowBuffer(RAS_ICanvas *canvas, KX_Camera *cam, MT_Transform& camtrans)
+{
+       GPULamp *lamp;
+       float viewmat[4][4], winmat[4][4];
+       int winsize;
+
+       /* bind framebuffer */
+       lamp = GetGPULamp();
+       GPU_lamp_shadow_buffer_bind(lamp, viewmat, &winsize, winmat);
+
+       if (GPU_lamp_shadow_buffer_type(lamp) == LA_SHADMAP_VARIANCE)
+               m_rasterizer->SetUsingOverrideShader(true);
+
+       /* GPU_lamp_shadow_buffer_bind() changes the viewport, so update the canvas */
+       canvas->UpdateViewPort(0, 0, winsize, winsize);
+
+       /* setup camera transformation */
+       MT_Matrix4x4 modelviewmat((float*)viewmat);
+       MT_Matrix4x4 projectionmat((float*)winmat);
+
+       MT_Transform trans = MT_Transform((float*)viewmat);
+       camtrans.invert(trans);
+
+       cam->SetModelviewMatrix(modelviewmat);
+       cam->SetProjectionMatrix(projectionmat);
+
+       cam->NodeSetLocalPosition(camtrans.getOrigin());
+       cam->NodeSetLocalOrientation(camtrans.getBasis());
+       cam->NodeUpdateGS(0);
+
+       /* setup rasterizer transformations */
+       /* SetViewMatrix may use stereomode which we temporarily disable here */
+       RAS_IRasterizer::StereoMode stereomode = m_rasterizer->GetStereoMode();
+       m_rasterizer->SetStereoMode(RAS_IRasterizer::RAS_STEREO_NOSTEREO);
+       m_rasterizer->SetProjectionMatrix(projectionmat);
+       m_rasterizer->SetViewMatrix(modelviewmat, cam->NodeGetWorldOrientation(), cam->NodeGetWorldPosition(), cam->GetCameraData()->m_perspective);
+       m_rasterizer->SetStereoMode(stereomode);
+}
+
+void RAS_OpenGLLight::UnbindShadowBuffer()
+{
+       GPULamp *lamp = GetGPULamp();
+       GPU_lamp_shadow_buffer_unbind(lamp);
+
+       if (GPU_lamp_shadow_buffer_type(lamp) == LA_SHADMAP_VARIANCE)
+               m_rasterizer->SetUsingOverrideShader(false);
+}
+
+Image *RAS_OpenGLLight::GetTextureImage(short texslot)
+{
+       KX_LightObject* kxlight = (KX_LightObject*)m_light;
+       Lamp *la = (Lamp*)kxlight->GetBlenderObject()->data;
+
+       if (texslot >= MAX_MTEX || texslot < 0)
+       {
+               printf("KX_LightObject::GetTextureImage(): texslot exceeds slot bounds (0-%d)\n", MAX_MTEX-1);
+               return NULL;
+       }
+
+       if (la->mtex[texslot])
+               return la->mtex[texslot]->tex->ima;
+
+       return NULL;
+}
+
+void RAS_OpenGLLight::Update()
+{
+       GPULamp *lamp;
+       KX_LightObject* kxlight = (KX_LightObject*)m_light;
+
+       if ((lamp = GetGPULamp()) != NULL && kxlight->GetSGNode()) {
+               float obmat[4][4];
+               // lights don't get their openGL matrix updated, do it now
+               if (kxlight->GetSGNode()->IsDirty())
+                       kxlight->GetOpenGLMatrix();
+               double *dobmat = kxlight->GetOpenGLMatrixPtr()->getPointer();
+
+               for (int i=0; i<4; i++)
+                       for (int j=0; j<4; j++, dobmat++)
+                               obmat[i][j] = (float)*dobmat;
+
+               GPU_lamp_update(lamp, m_layer, 0, obmat);
+               GPU_lamp_update_colors(lamp, m_color[0], m_color[1],
+                       m_color[2], m_energy);
+               GPU_lamp_update_distance(lamp, m_distance, m_att1, m_att2);
+               GPU_lamp_update_spot(lamp, m_spotsize, m_spotblend);
+       }
+}
+
+
diff --git a/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_OpenGLLight.h b/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_OpenGLLight.h
new file mode 100644 (file)
index 0000000..0c4e5bf
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+ * All rights reserved.
+ *
+ * The Original Code is: all of this file.
+ *
+ * Contributor(s): Mitchell Stokes
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+#include "RAS_ILightObject.h"
+
+class RAS_OpenGLRasterizer;
+struct GPULamp;
+struct Image;
+
+class RAS_OpenGLLight : public RAS_ILightObject
+{
+
+       RAS_OpenGLRasterizer *m_rasterizer;
+
+       GPULamp *GetGPULamp();
+public:
+       RAS_OpenGLLight(RAS_OpenGLRasterizer *ras);
+       ~RAS_OpenGLLight();
+
+       bool ApplyFixedFunctionLighting(KX_Scene *kxscene, int oblayer, int slot);
+
+       RAS_OpenGLLight* Clone() { return new RAS_OpenGLLight(*this); }
+
+       bool HasShadowBuffer();
+       int GetShadowLayer();
+       void BindShadowBuffer(RAS_ICanvas *canvas, KX_Camera *cam, MT_Transform& camtrans);
+       void UnbindShadowBuffer();
+       Image *GetTextureImage(short texslot);
+       void Update();
+};
index 63355696082200dd53312a900e89e1d184d3270f..1227fe892a376a8b72b15747ca8631fb949b19db 100644 (file)
 #include "RAS_TexVert.h"
 #include "RAS_MeshObject.h"
 #include "RAS_Polygon.h"
-#include "RAS_LightObject.h"
+#include "RAS_ILightObject.h"
 #include "MT_CmMatrix4x4.h"
 
+#include "RAS_OpenGLLight.h"
+
 #include "RAS_StorageIM.h"
 #include "RAS_StorageVA.h"
 #include "RAS_StorageVBO.h"
@@ -59,7 +61,6 @@ extern "C"{
 
 // XXX Clean these up <<<
 #include "Value.h"
-#include "KX_Light.h"
 #include "KX_Scene.h"
 #include "KX_RayCast.h"
 #include "KX_GameObject.h"
@@ -1187,7 +1188,7 @@ void RAS_OpenGLRasterizer::ProcessLighting(bool uselights, const MT_Transform& v
                KX_Scene* kxscene = (KX_Scene*)m_auxilaryClientInfo;
                float glviewmat[16];
                unsigned int count;
-               std::vector<struct      RAS_LightObject*>::iterator lit = m_lights.begin();
+               std::vector<RAS_OpenGLLight*>::iterator lit = m_lights.begin();
 
                for (count=0; count<m_numgllights; count++)
                        glDisable((GLenum)(GL_LIGHT0+count));
@@ -1198,10 +1199,9 @@ void RAS_OpenGLRasterizer::ProcessLighting(bool uselights, const MT_Transform& v
                glLoadMatrixf(glviewmat);
                for (lit = m_lights.begin(), count = 0; !(lit==m_lights.end()) && count < m_numgllights; ++lit)
                {
-                       RAS_LightObject* lightdata = (*lit);
-                       KX_LightObject *kxlight = (KX_LightObject*)lightdata->m_light;
+                       RAS_OpenGLLight* light = (*lit);
 
-                       if (kxlight->ApplyLight(kxscene, layer, count))
+                       if (light->ApplyFixedFunctionLighting(kxscene, layer, count))
                                count++;
                }
                glPopMatrix();
@@ -1243,15 +1243,25 @@ void RAS_OpenGLRasterizer::DisableOpenGLLights()
        m_lastlighting = false;
 }
 
-void RAS_OpenGLRasterizer::AddLight(struct RAS_LightObject* lightobject)
+RAS_ILightObject *RAS_OpenGLRasterizer::CreateLight()
 {
-       m_lights.push_back(lightobject);
+       return new RAS_OpenGLLight(this);
 }
 
-void RAS_OpenGLRasterizer::RemoveLight(struct RAS_LightObject* lightobject)
+void RAS_OpenGLRasterizer::AddLight(RAS_ILightObject* lightobject)
 {
-       std::vector<struct      RAS_LightObject*>::iterator lit =
-               std::find(m_lights.begin(),m_lights.end(),lightobject);
+       RAS_OpenGLLight* gllight = dynamic_cast<RAS_OpenGLLight*>(lightobject);
+       assert(gllight);
+       m_lights.push_back(gllight);
+}
+
+void RAS_OpenGLRasterizer::RemoveLight(RAS_ILightObject* lightobject)
+{
+       RAS_OpenGLLight* gllight = dynamic_cast<RAS_OpenGLLight*>(lightobject);
+       assert(gllight);
+
+       std::vector<RAS_OpenGLLight*>::iterator lit =
+               std::find(m_lights.begin(),m_lights.end(),gllight);
 
        if (!(lit==m_lights.end()))
                m_lights.erase(lit);
index ae9e89cd8cc8196be6d167c353daef894072b39b..1334ddb2a26cf0e4d311eacb7d73a23715709c99 100644 (file)
@@ -46,6 +46,7 @@ using namespace std;
 
 class RAS_IStorage;
 class RAS_ICanvas;
+class RAS_OpenGLLight;
 
 #define RAS_MAX_TEXCO  8     /* match in BL_Material */
 #define RAS_MAX_ATTRIB 16    /* match in BL_BlenderShader */
@@ -117,7 +118,7 @@ class RAS_OpenGLRasterizer : public RAS_IRasterizer
        /* Render tools */
        void *m_clientobject;
        void *m_auxilaryClientInfo;
-       std::vector<struct RAS_LightObject *> m_lights;
+       std::vector<RAS_OpenGLLight *> m_lights;
        int m_lastlightlayer;
        bool m_lastlighting;
        void *m_lastauxinfo;
@@ -315,9 +316,11 @@ public:
        bool RayHit(struct KX_ClientObjectInfo *client, class KX_RayCast *result, void * const data);
        bool NeedRayCast(struct KX_ClientObjectInfo *) { return true; }
 
-       void AddLight(struct RAS_LightObject *lightobject);
-       void RemoveLight(struct RAS_LightObject *lightobject);
-       int ApplyLights(int objectlayer, const MT_Transform &viewmat);
+       RAS_ILightObject* CreateLight();
+       void AddLight(RAS_ILightObject* lightobject);
+
+       void RemoveLight(RAS_ILightObject* lightobject);
+       int ApplyLights(int objectlayer, const MT_Transform& viewmat);
 
        void MotionBlur();
 
index c187f7c41c1102030ae6393e75af156eac08994b..35a73193a246b9a3e9f6edf97b93e76f5061a7e6 100644 (file)
@@ -36,6 +36,7 @@
 #include "KX_GameObject.h"
 #include "KX_Light.h"
 #include "RAS_MeshObject.h"
+#include "RAS_ILightObject.h"
 #include "DNA_mesh_types.h"
 #include "DNA_meshdata_types.h"
 #include "DNA_image_types.h"
@@ -237,7 +238,7 @@ static int Texture_init(Texture *self, PyObject *args, PyObject *kwds)
                        }
                        else if (lamp != NULL)
                        {
-                               self->m_imgTexture = lamp->GetTextureImage(texID);
+                               self->m_imgTexture = lamp->GetLightData()->GetTextureImage(texID);
                                self->m_useMatTexture = false;
                        }