2013-06-23 10:25:42 -06:00
|
|
|
#ifndef RENDERER_LIGHT_INCLUDED
|
|
|
|
#define RENDERER_LIGHT_INCLUDED
|
|
|
|
#include "renderer_opengl.hpp"
|
|
|
|
#include "Types.h"
|
2013-06-23 14:56:01 -06:00
|
|
|
#include <map>
|
2013-06-24 15:59:32 -06:00
|
|
|
#include "modules/MapCache.h"
|
|
|
|
|
2013-06-23 10:25:42 -06:00
|
|
|
struct renderer_light : public renderer_wrap {
|
|
|
|
private:
|
|
|
|
void colorizeTile(int x,int y)
|
|
|
|
{
|
|
|
|
const int tile = x*(df::global::gps->dimy) + y;
|
|
|
|
old_opengl* p=reinterpret_cast<old_opengl*>(parent);
|
|
|
|
float *fg = p->fg + tile * 4 * 6;
|
|
|
|
float *bg = p->bg + tile * 4 * 6;
|
|
|
|
float *tex = p->tex + tile * 2 * 6;
|
|
|
|
lightCell light=lightGrid[tile];
|
|
|
|
for (int i = 0; i < 6; i++) {
|
|
|
|
*(fg++) *= light.r;
|
|
|
|
*(fg++) *= light.g;
|
|
|
|
*(fg++) *= light.b;
|
|
|
|
*(fg++) = 1;
|
|
|
|
|
|
|
|
*(bg++) *= light.r;
|
|
|
|
*(bg++) *= light.g;
|
|
|
|
*(bg++) *= light.b;
|
|
|
|
*(bg++) = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void reinitLightGrid(int w,int h)
|
|
|
|
{
|
|
|
|
tthread::lock_guard<tthread::fast_mutex> guard(dataMutex);
|
|
|
|
lightGrid.resize(w*h);
|
|
|
|
}
|
|
|
|
void reinitLightGrid()
|
|
|
|
{
|
|
|
|
reinitLightGrid(df::global::gps->dimy,df::global::gps->dimx);
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
tthread::fast_mutex dataMutex;
|
|
|
|
std::vector<lightCell> lightGrid;
|
|
|
|
renderer_light(renderer* parent):renderer_wrap(parent)
|
|
|
|
{
|
|
|
|
reinitLightGrid();
|
|
|
|
}
|
|
|
|
virtual void update_tile(int32_t x, int32_t y) {
|
|
|
|
renderer_wrap::update_tile(x,y);
|
|
|
|
tthread::lock_guard<tthread::fast_mutex> guard(dataMutex);
|
|
|
|
colorizeTile(x,y);
|
|
|
|
};
|
|
|
|
virtual void update_all() {
|
|
|
|
renderer_wrap::update_all();
|
|
|
|
tthread::lock_guard<tthread::fast_mutex> guard(dataMutex);
|
|
|
|
for (int x = 0; x < df::global::gps->dimx; x++)
|
|
|
|
for (int y = 0; y < df::global::gps->dimy; y++)
|
|
|
|
colorizeTile(x,y);
|
|
|
|
};
|
|
|
|
virtual void grid_resize(int32_t w, int32_t h) {
|
|
|
|
renderer_wrap::grid_resize(w,h);
|
|
|
|
reinitLightGrid(w,h);
|
|
|
|
};
|
|
|
|
virtual void resize(int32_t w, int32_t h) {
|
|
|
|
renderer_wrap::resize(w,h);
|
2013-06-23 12:29:03 -06:00
|
|
|
reinitLightGrid();
|
2013-06-23 10:25:42 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
class lightingEngine
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
lightingEngine(renderer_light* target):myRenderer(target){}
|
|
|
|
|
|
|
|
virtual void reinit()=0;
|
|
|
|
virtual void calculate()=0;
|
|
|
|
|
|
|
|
virtual void updateWindow()=0;
|
|
|
|
|
2013-06-24 09:18:57 -06:00
|
|
|
virtual void loadSettings()=0;
|
2013-06-25 11:36:53 -06:00
|
|
|
virtual void clear()=0;
|
2013-06-24 09:18:57 -06:00
|
|
|
|
2013-06-23 10:25:42 -06:00
|
|
|
protected:
|
|
|
|
renderer_light* myRenderer;
|
|
|
|
};
|
|
|
|
struct lightSource
|
|
|
|
{
|
|
|
|
lightCell power;
|
|
|
|
int radius;
|
2013-06-23 11:45:05 -06:00
|
|
|
bool flicker;
|
|
|
|
lightSource():power(0,0,0),radius(0),flicker(false)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2013-06-25 11:43:55 -06:00
|
|
|
lightSource(lightCell power,int radius);
|
2013-06-23 11:45:05 -06:00
|
|
|
float powerSquared()const
|
|
|
|
{
|
|
|
|
return power.r*power.r+power.g*power.g+power.b*power.b;
|
|
|
|
}
|
|
|
|
void combine(const lightSource& other);
|
|
|
|
|
2013-06-23 10:25:42 -06:00
|
|
|
};
|
2013-06-24 09:18:57 -06:00
|
|
|
struct matLightDef
|
|
|
|
{
|
|
|
|
int mat_index;
|
|
|
|
int mat_type;
|
|
|
|
bool isTransparent;
|
|
|
|
lightCell transparency;
|
|
|
|
bool isEmiting;
|
|
|
|
bool sizeModifiesPower;
|
|
|
|
bool sizeModifiesRange;
|
|
|
|
bool flicker;
|
|
|
|
lightCell emitColor;
|
|
|
|
int radius;
|
2013-06-25 10:34:38 -06:00
|
|
|
matLightDef():isTransparent(false),isEmiting(false),transparency(0,0,0),emitColor(0,0,0),radius(0){}
|
2013-06-24 09:18:57 -06:00
|
|
|
matLightDef(lightCell transparency,lightCell emit,int rad):isTransparent(true),isEmiting(true),
|
|
|
|
transparency(transparency),emitColor(emit),radius(rad){}
|
2013-06-24 15:59:32 -06:00
|
|
|
matLightDef(lightCell emit,int rad):isTransparent(false),isEmiting(true),emitColor(emit),radius(rad),transparency(0,0,0){}
|
2013-06-24 09:18:57 -06:00
|
|
|
matLightDef(lightCell transparency):isTransparent(true),isEmiting(false),transparency(transparency){}
|
|
|
|
lightSource makeSource(float size=1) const
|
|
|
|
{
|
|
|
|
//TODO implement sizeModifiesPower/range
|
|
|
|
return lightSource(emitColor,radius);
|
|
|
|
}
|
|
|
|
};
|
2013-06-23 10:25:42 -06:00
|
|
|
class lightingEngineViewscreen:public lightingEngine
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
lightingEngineViewscreen(renderer_light* target);
|
2013-06-26 07:42:14 -06:00
|
|
|
~lightingEngineViewscreen();
|
2013-06-23 10:25:42 -06:00
|
|
|
void reinit();
|
|
|
|
void calculate();
|
|
|
|
|
|
|
|
void updateWindow();
|
|
|
|
|
2013-06-24 09:18:57 -06:00
|
|
|
void loadSettings();
|
2013-06-25 11:36:53 -06:00
|
|
|
void clear();
|
2013-06-23 10:25:42 -06:00
|
|
|
private:
|
2013-06-24 09:18:57 -06:00
|
|
|
|
2013-06-24 15:59:32 -06:00
|
|
|
df::coord2d worldToViewportCoord(const df::coord2d& in,const DFHack::rect2d& r,const df::coord2d& window2d) ;
|
|
|
|
bool isInViewport(const df::coord2d& in,const DFHack::rect2d& r);
|
2013-06-24 09:18:57 -06:00
|
|
|
|
2013-06-24 15:59:32 -06:00
|
|
|
void doSun(const lightSource& sky,MapExtras::MapCache& map);
|
|
|
|
void doOcupancyAndLights();
|
|
|
|
lightCell propogateSun(MapExtras::Block* b, int x,int y,const lightCell& in,bool lastLevel);
|
2013-06-26 07:42:14 -06:00
|
|
|
void doRay(std::vector<lightCell> & target, lightCell power,int cx,int cy,int tx,int ty);
|
2013-06-23 10:25:42 -06:00
|
|
|
void doFovs();
|
2013-06-26 07:42:14 -06:00
|
|
|
void doLight(std::vector<lightCell> & target, int index);
|
|
|
|
bool lightUpCell(std::vector<lightCell> & target, lightCell& power,int dx,int dy,int tx,int ty);
|
2013-06-23 11:45:05 -06:00
|
|
|
bool addLight(int tileId,const lightSource& light);
|
2013-06-24 15:59:32 -06:00
|
|
|
|
|
|
|
matLightDef* getMaterial(int matType,int matIndex);
|
2013-06-24 09:18:57 -06:00
|
|
|
//apply material to cell
|
2013-06-25 05:03:01 -06:00
|
|
|
void applyMaterial(int tileId,const matLightDef& mat,float size=1, float thickness = 1);
|
2013-06-24 09:18:57 -06:00
|
|
|
//try to find and apply material, if failed return false, and if def!=null then apply def.
|
|
|
|
bool applyMaterial(int tileId,int matType,int matIndex,float size=1,const matLightDef* def=NULL);
|
2013-06-23 10:25:42 -06:00
|
|
|
size_t inline getIndex(int x,int y)
|
|
|
|
{
|
|
|
|
return x*h+y;
|
|
|
|
}
|
2013-06-26 07:42:14 -06:00
|
|
|
df::coord2d inline getCoords(int index)
|
|
|
|
{
|
|
|
|
return df::coord2d(index/h, index%h);
|
|
|
|
}
|
2013-06-24 09:18:57 -06:00
|
|
|
//maps
|
2013-06-23 10:25:42 -06:00
|
|
|
std::vector<lightCell> lightMap;
|
2013-06-26 07:42:14 -06:00
|
|
|
std::vector<lightCell> lightMap2;
|
2013-06-23 10:25:42 -06:00
|
|
|
std::vector<lightCell> ocupancy;
|
|
|
|
std::vector<lightSource> lights;
|
2013-06-26 07:42:14 -06:00
|
|
|
|
|
|
|
//Threading stuff
|
|
|
|
tthread::mutex indexMutex;
|
|
|
|
tthread::mutex writeMutex;
|
|
|
|
int nextIndex;
|
|
|
|
std::vector<tthread::thread *> threadList;
|
|
|
|
void doLightThreads();
|
|
|
|
public:
|
|
|
|
void lightWorkerThread(void * arg);
|
|
|
|
private:
|
2013-06-24 09:18:57 -06:00
|
|
|
//settings
|
2013-06-23 14:56:01 -06:00
|
|
|
|
2013-06-24 09:18:57 -06:00
|
|
|
///set up sane settings if setting file does not exist.
|
|
|
|
void defaultSettings();
|
|
|
|
|
|
|
|
static int parseMaterials(lua_State* L);
|
|
|
|
static int parseSpecial(lua_State* L);
|
|
|
|
//special stuff
|
|
|
|
matLightDef matLava;
|
|
|
|
matLightDef matIce;
|
|
|
|
matLightDef matAmbience;
|
|
|
|
matLightDef matCursor;
|
2013-06-24 15:59:32 -06:00
|
|
|
matLightDef matWall;
|
|
|
|
matLightDef matWater;
|
2013-06-25 10:34:38 -06:00
|
|
|
matLightDef matCitizen;
|
|
|
|
float levelDim;
|
2013-06-24 09:18:57 -06:00
|
|
|
//materials
|
|
|
|
std::map<std::pair<int,int>,matLightDef> matDefs;
|
2013-06-23 14:56:01 -06:00
|
|
|
|
2013-06-23 10:25:42 -06:00
|
|
|
int w,h;
|
|
|
|
DFHack::rect2d mapPort;
|
|
|
|
};
|
2013-06-26 07:42:14 -06:00
|
|
|
lightCell blend(lightCell a,lightCell b);
|
|
|
|
lightCell blendMax(lightCell a,lightCell b);
|
2013-06-24 02:50:22 -06:00
|
|
|
#endif
|