mirror of
https://github.com/aap/librw.git
synced 2025-12-19 00:49:50 +00:00
implemented generic lighting callback
This commit is contained in:
@@ -1,7 +1,4 @@
|
||||
#include <cstdio>
|
||||
//#include <cstdlib>
|
||||
//#include <cstring>
|
||||
//#include <cassert>
|
||||
|
||||
#include "rwbase.h"
|
||||
#include "rwerror.h"
|
||||
|
||||
@@ -27,7 +27,7 @@ defaultRenderCB(Atomic *atomic, InstanceDataHeader *header)
|
||||
{
|
||||
RawMatrix world;
|
||||
|
||||
d3d::lightingCB(!!(atomic->geometry->flags & Geometry::NORMALS));
|
||||
d3d::lightingCB(atomic);
|
||||
|
||||
Geometry *geo = atomic->geometry;
|
||||
d3d::setRenderState(D3DRS_LIGHTING, !!(geo->flags & rw::Geometry::LIGHT));
|
||||
|
||||
@@ -66,7 +66,7 @@ defaultRenderCB(Atomic *atomic, InstanceDataHeader *header)
|
||||
|
||||
int lighting = !!(geo->flags & rw::Geometry::LIGHT);
|
||||
if(lighting)
|
||||
d3d::lightingCB(!!(atomic->geometry->flags & Geometry::NORMALS));
|
||||
d3d::lightingCB(atomic);
|
||||
|
||||
d3d::setRenderState(D3DRS_LIGHTING, lighting);
|
||||
|
||||
|
||||
@@ -20,10 +20,19 @@ IDirect3DDevice9 *d3ddevice = nil;
|
||||
#define MAX_LIGHTS 8
|
||||
|
||||
void
|
||||
lightingCB(bool32 normals)
|
||||
lightingCB(Atomic *atomic)
|
||||
{
|
||||
World *world;
|
||||
RGBAf ambLight = { 0.0, 0.0, 0.0, 1.0 };
|
||||
WorldLights lightData;
|
||||
Light *directionals[8];
|
||||
Light *locals[8];
|
||||
lightData.directionals = directionals;
|
||||
lightData.numDirectionals = 8;
|
||||
lightData.locals = locals;
|
||||
lightData.numLocals = 8;
|
||||
|
||||
((World*)engine->currentWorld)->enumerateLights(atomic, &lightData);
|
||||
|
||||
int i, n;
|
||||
RGBA amb;
|
||||
D3DLIGHT9 light;
|
||||
light.Type = D3DLIGHT_DIRECTIONAL;
|
||||
@@ -39,34 +48,83 @@ lightingCB(bool32 normals)
|
||||
light.Attenuation2 = 0.0f;
|
||||
light.Theta = 0.0f;
|
||||
light.Phi = 0.0f;
|
||||
int n = 0;
|
||||
|
||||
world = (World*)engine->currentWorld;
|
||||
// only unpositioned lights right now
|
||||
FORLIST(lnk, world->directionalLights){
|
||||
Light *l = Light::fromWorld(lnk);
|
||||
if((l->getFlags() & Light::LIGHTATOMICS) == 0)
|
||||
continue;
|
||||
if(normals &&
|
||||
l->getType() == Light::DIRECTIONAL &&
|
||||
l->getFlags() & Light::LIGHTATOMICS){
|
||||
if(n >= MAX_LIGHTS)
|
||||
continue;
|
||||
convColor(&amb, &lightData.ambient);
|
||||
d3d::setRenderState(D3DRS_AMBIENT, D3DCOLOR_RGBA(amb.red, amb.green, amb.blue, amb.alpha));
|
||||
|
||||
n = 0;
|
||||
for(i = 0; i < lightData.numDirectionals; i++){
|
||||
if(n >= MAX_LIGHTS)
|
||||
return;
|
||||
Light *l = lightData.directionals[i];
|
||||
light.Type = D3DLIGHT_DIRECTIONAL;
|
||||
light.Diffuse = *(D3DCOLORVALUE*)&l->color;
|
||||
light.Direction = *(D3DVECTOR*)&l->getFrame()->getLTM()->at;
|
||||
d3ddevice->SetLight(n, &light);
|
||||
d3ddevice->LightEnable(n, TRUE);
|
||||
n++;
|
||||
}
|
||||
|
||||
for(i = 0; i < lightData.numLocals; i++){
|
||||
if(n >= MAX_LIGHTS)
|
||||
return;
|
||||
Light *l = lightData.locals[i];
|
||||
switch(l->getType()){
|
||||
case Light::POINT:
|
||||
light.Type = D3DLIGHT_POINT;
|
||||
light.Diffuse = *(D3DCOLORVALUE*)&l->color;
|
||||
light.Direction = *(D3DVECTOR*)&l->getFrame()->getLTM()->at;
|
||||
light.Position = *(D3DVECTOR*)&l->getFrame()->getLTM()->pos;
|
||||
light.Direction.x = 0.0f;
|
||||
light.Direction.y = 0.0f;
|
||||
light.Direction.z = 0.0f;
|
||||
light.Range = l->radius;
|
||||
light.Falloff = 1.0f;
|
||||
light.Attenuation0 = 1.0f;
|
||||
light.Attenuation1 = 0.0f/l->radius;
|
||||
light.Attenuation2 = 5.0f/(l->radius*l->radius);
|
||||
d3ddevice->SetLight(n, &light);
|
||||
d3ddevice->LightEnable(n, TRUE);
|
||||
n++;
|
||||
}else if(l->getType() == Light::AMBIENT){
|
||||
ambLight.red += l->color.red;
|
||||
ambLight.green += l->color.green;
|
||||
ambLight.blue += l->color.blue;
|
||||
break;
|
||||
|
||||
case Light::SPOT:
|
||||
light.Type = D3DLIGHT_SPOT;
|
||||
light.Diffuse = *(D3DCOLORVALUE*)&l->color;
|
||||
light.Position = *(D3DVECTOR*)&l->getFrame()->getLTM()->pos;
|
||||
light.Direction = *(D3DVECTOR*)&l->getFrame()->getLTM()->at;
|
||||
light.Range = l->radius;
|
||||
light.Falloff = 1.0f;
|
||||
light.Attenuation0 = 1.0f;
|
||||
light.Attenuation1 = 0.0f/l->radius;
|
||||
light.Attenuation2 = 5.0f/(l->radius*l->radius);
|
||||
light.Theta = l->getAngle()*2.0f;
|
||||
light.Phi = light.Theta;
|
||||
d3ddevice->SetLight(n, &light);
|
||||
d3ddevice->LightEnable(n, TRUE);
|
||||
n++;
|
||||
break;
|
||||
|
||||
case Light::SOFTSPOT:
|
||||
light.Type = D3DLIGHT_SPOT;
|
||||
light.Diffuse = *(D3DCOLORVALUE*)&l->color;
|
||||
light.Position = *(D3DVECTOR*)&l->getFrame()->getLTM()->pos;
|
||||
light.Direction = *(D3DVECTOR*)&l->getFrame()->getLTM()->at;
|
||||
light.Range = l->radius;
|
||||
light.Falloff = 1.0f;
|
||||
light.Attenuation0 = 1.0f;
|
||||
light.Attenuation1 = 0.0f/l->radius;
|
||||
light.Attenuation2 = 5.0f/(l->radius*l->radius);
|
||||
light.Theta = 0.0f;
|
||||
light.Phi = l->getAngle()*2.0f;
|
||||
d3ddevice->SetLight(n, &light);
|
||||
d3ddevice->LightEnable(n, TRUE);
|
||||
n++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for(; n < MAX_LIGHTS; n++)
|
||||
d3ddevice->LightEnable(n, FALSE);
|
||||
convColor(&amb, &ambLight);
|
||||
d3d::setRenderState(D3DRS_AMBIENT, D3DCOLOR_RGBA(amb.red, amb.green, amb.blue, amb.alpha));
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -37,7 +37,7 @@ extern IDirect3DDevice9 *d3ddevice;
|
||||
void setD3dMaterial(D3DMATERIAL9 *mat9);
|
||||
#endif
|
||||
|
||||
void lightingCB(bool32 normals);
|
||||
void lightingCB(Atomic *atomic);
|
||||
|
||||
#define COLOR_ARGB(a, r, g, b) ((rw::uint32)((((a)&0xff)<<24)|(((r)&0xff)<<16)|(((g)&0xff)<<8)|((b)&0xff)))
|
||||
|
||||
|
||||
@@ -731,14 +731,24 @@ struct Clump
|
||||
void render(void);
|
||||
};
|
||||
|
||||
// used by enumerateLights for lighting callback
|
||||
struct WorldLights
|
||||
{
|
||||
RGBAf ambient; // all ambients added
|
||||
int32 numDirectionals;
|
||||
Light **directionals; // only directionals
|
||||
int32 numLocals;
|
||||
Light **locals; // points, (soft)spots
|
||||
};
|
||||
|
||||
// A bit of a stub right now
|
||||
struct World
|
||||
{
|
||||
PLUGINBASE
|
||||
enum { ID = 7 };
|
||||
Object object;
|
||||
LinkList lights; // these have positions (type >= 0x80)
|
||||
LinkList directionalLights; // these do not (type < 0x80)
|
||||
LinkList localLights; // these have positions (type >= 0x80)
|
||||
LinkList globalLights; // these do not (type < 0x80)
|
||||
LinkList clumps;
|
||||
|
||||
static int32 numAllocated;
|
||||
@@ -754,6 +764,7 @@ struct World
|
||||
void addClump(Clump *clump);
|
||||
void removeClump(Clump *clump);
|
||||
void render(void);
|
||||
void enumerateLights(Atomic *atomic, WorldLights *lightData);
|
||||
};
|
||||
|
||||
struct TexDictionary
|
||||
|
||||
@@ -28,8 +28,8 @@ World::create(void)
|
||||
}
|
||||
numAllocated++;
|
||||
world->object.init(World::ID, 0);
|
||||
world->lights.init();
|
||||
world->directionalLights.init();
|
||||
world->localLights.init();
|
||||
world->globalLights.init();
|
||||
world->clumps.init();
|
||||
s_plglist.construct(world);
|
||||
return world;
|
||||
@@ -46,11 +46,12 @@ World::destroy(void)
|
||||
void
|
||||
World::addLight(Light *light)
|
||||
{
|
||||
assert(light->world == nil);
|
||||
light->world = this;
|
||||
if(light->getType() < Light::POINT){
|
||||
this->directionalLights.append(&light->inWorld);
|
||||
this->globalLights.append(&light->inWorld);
|
||||
}else{
|
||||
this->lights.append(&light->inWorld);
|
||||
this->localLights.append(&light->inWorld);
|
||||
if(light->getFrame())
|
||||
light->getFrame()->updateObjects();
|
||||
}
|
||||
@@ -59,8 +60,9 @@ World::addLight(Light *light)
|
||||
void
|
||||
World::removeLight(Light *light)
|
||||
{
|
||||
if(light->world == this)
|
||||
light->inWorld.remove();
|
||||
assert(light->world == this);
|
||||
light->inWorld.remove();
|
||||
light->world = nil;
|
||||
}
|
||||
|
||||
void
|
||||
@@ -75,8 +77,8 @@ World::addCamera(Camera *cam)
|
||||
void
|
||||
World::removeCamera(Camera *cam)
|
||||
{
|
||||
if(cam->world == this)
|
||||
cam->world = nil;
|
||||
assert(cam->world == this);
|
||||
cam->world = nil;
|
||||
}
|
||||
|
||||
void
|
||||
@@ -136,4 +138,58 @@ World::render(void)
|
||||
Clump::fromWorld(lnk)->render();
|
||||
}
|
||||
|
||||
// Find lights that illuminate an atomic
|
||||
void
|
||||
World::enumerateLights(Atomic *atomic, WorldLights *lightData)
|
||||
{
|
||||
int32 maxDirectionals, maxLocals;
|
||||
|
||||
assert(atomic->world == this);
|
||||
|
||||
maxDirectionals = lightData->numDirectionals;
|
||||
maxLocals = lightData->numLocals;
|
||||
|
||||
lightData->numDirectionals = 0;
|
||||
lightData->numLocals = 0;
|
||||
lightData->ambient.red = 0.0f;
|
||||
lightData->ambient.green = 0.0f;
|
||||
lightData->ambient.blue = 0.0f;
|
||||
lightData->ambient.alpha = 1.0f;
|
||||
|
||||
bool32 normals = atomic->geometry->flags & Geometry::NORMALS;
|
||||
|
||||
FORLIST(lnk, this->globalLights){
|
||||
Light *l = Light::fromWorld(lnk);
|
||||
if((l->getFlags() & Light::LIGHTATOMICS) == 0)
|
||||
continue;
|
||||
if(l->getType() == Light::AMBIENT){
|
||||
lightData->ambient.red += l->color.red;
|
||||
lightData->ambient.green += l->color.green;
|
||||
lightData->ambient.blue += l->color.blue;
|
||||
}else if(normals && l->getType() == Light::DIRECTIONAL){
|
||||
if(lightData->numDirectionals < maxDirectionals)
|
||||
lightData->directionals[lightData->numDirectionals++] = l;
|
||||
}
|
||||
}
|
||||
|
||||
if(!normals)
|
||||
return;
|
||||
|
||||
// TODO: for this we would use an atomic's world sectors, but we don't have those yet
|
||||
FORLIST(lnk, this->localLights){
|
||||
if(lightData->numLocals >= maxLocals)
|
||||
return;
|
||||
|
||||
Light *l = Light::fromWorld(lnk);
|
||||
if((l->getFlags() & Light::LIGHTATOMICS) == 0)
|
||||
continue;
|
||||
|
||||
// check if spheres are intersecting
|
||||
Sphere *atomsphere = atomic->getWorldBoundingSphere();
|
||||
V3d dist = sub(l->getFrame()->getLTM()->pos, atomsphere->center);
|
||||
if(length(dist) < atomsphere->radius + l->radius)
|
||||
lightData->locals[lightData->numLocals++] = l;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user