From d8b0b82122f842b74e341c78cbf13ec0e4c4ee95 Mon Sep 17 00:00:00 2001 From: Warmist Date: Thu, 27 Jun 2013 16:11:29 +0300 Subject: [PATCH] Added antialiased lights. They kind of suck. So I disabled them. --- plugins/rendermax/renderer_light.cpp | 114 +++++++++++++++++++------- plugins/rendermax/renderer_light.hpp | 2 +- plugins/rendermax/renderer_opengl.hpp | 28 +++++-- 3 files changed, 105 insertions(+), 39 deletions(-) diff --git a/plugins/rendermax/renderer_light.cpp b/plugins/rendermax/renderer_light.cpp index 8fccf81e6..7b723756b 100644 --- a/plugins/rendermax/renderer_light.cpp +++ b/plugins/rendermax/renderer_light.cpp @@ -221,7 +221,7 @@ void plotSquare(int xm, int ym, int r,std::function setPixel) setPixel(xm-x, ym+r); /* IV.2 Quadrant */ } } -void plotLine(int x0, int y0, int x1, int y1,std::function setPixel) +void plotLine(int x0, int y0, int x1, int y1,lightCell power,std::function setPixel) { int dx = abs(x1-x0), sx = x0 setPixelAA) +{ + int dx = abs(x1-x0), sx = x0= -dx) { /* x step */ + if (x0 == x1) break; + + if (e2+dy < ed) + { + str=1-(e2+dy)/(float)ed; + sumPower+=setPixelAA(power*str,lrdx,lrdy,x0,y0+sy); + strsum+=str; + } + err -= dy; x0 += sx; rdx=sx; + } + if (2*e2 <= dy) { /* y step */ + if (y0 == y1) break; + + if (dx-e2 < ed) + { + str=1-(dx-e2)/(float)ed; + sumPower+=setPixelAA(power*str,lrdx,lrdy,x2+sx,y0); + strsum+=str; + } + err += dx; y0 += sy; rdy=sy; + } + if(strsum<0.001f) + return; + sumPower=sumPower/strsum; + if(sumPower.dot(sumPower)<0.00001f) + return; + power=sumPower; + } +} lightCell blendMax(lightCell a,lightCell b) { return lightCell(std::max(a.r,b.r),std::max(a.g,b.g),std::max(a.b,b.b)); @@ -247,7 +300,7 @@ lightCell blend(lightCell a,lightCell b) { return blendMax(a,b); } -bool lightingEngineViewscreen::lightUpCell(std::vector & target, lightCell& power,int dx,int dy,int tx,int ty) +lightCell lightingEngineViewscreen::lightUpCell(std::vector & target,lightCell power,int dx,int dy,int tx,int ty) { if(isInViewport(coord2d(tx,ty),mapPort)) { @@ -270,12 +323,12 @@ bool lightingEngineViewscreen::lightUpCell(std::vector & target, ligh if (dsq>0 && !wallhack) { - power*=v.power(dt); + power*=v.pow(dt); } if(ls.radius>0 && dsq>0) { if(power<=ls.power) - return false; + return lightCell(); } //float dt=sqrt(dsq); lightCell oldCol=target[tile]; @@ -283,20 +336,20 @@ bool lightingEngineViewscreen::lightUpCell(std::vector & target, ligh target[tile]=ncol; if(wallhack) - return false; - float pwsq=power.r*power.r+power.g*power.g+power.b*power.b; - return pwsq>levelDim*levelDim; + return lightCell(); + + + return power; } else - return false; + return lightCell(); } void lightingEngineViewscreen::doRay(std::vector & target, lightCell power,int cx,int cy,int tx,int ty) { using namespace std::placeholders; - lightCell curPower=power; - plotLine(cx,cy,tx,ty,std::bind(&lightingEngineViewscreen::lightUpCell,this,std::ref(target),std::ref(curPower),_1,_2,_3,_4)); + + plotLine(cx,cy,tx,ty,power,std::bind(&lightingEngineViewscreen::lightUpCell,this,std::ref(target),_1,_2,_3,_4,_5)); } - void lightingEngineViewscreen::doLight(std::vector & target, int index) { using namespace std::placeholders; @@ -314,21 +367,21 @@ void lightingEngineViewscreen::doLight(std::vector & target, int inde radius*=flicker; power=power*flicker; } - int surrounds = 0; - lightCell curPower; + lightCell surrounds; + - lightUpCell(target, curPower = power, 0, 0,i+0, j+0); + lightUpCell(target,power, 0, 0,i+0, j+0); { - surrounds += lightUpCell(target, curPower = power, 0, 1,i+0, j+1); - surrounds += lightUpCell(target, curPower = power, 1, 1,i+1, j+1); - surrounds += lightUpCell(target, curPower = power, 1, 0,i+1, j+0); - surrounds += lightUpCell(target, curPower = power, 1,-1,i+1, j-1); - surrounds += lightUpCell(target, curPower = power, 0,-1,i+0, j-1); - surrounds += lightUpCell(target, curPower = power,-1,-1,i-1, j-1); - surrounds += lightUpCell(target, curPower = power,-1, 0,i-1, j+0); - surrounds += lightUpCell(target, curPower = power,-1, 1,i-1, j+1); + surrounds += lightUpCell(target, power, 0, 1,i+0, j+1); + surrounds += lightUpCell(target, power, 1, 1,i+1, j+1); + surrounds += lightUpCell(target, power, 1, 0,i+1, j+0); + surrounds += lightUpCell(target, power, 1,-1,i+1, j-1); + surrounds += lightUpCell(target, power, 0,-1,i+0, j-1); + surrounds += lightUpCell(target, power,-1,-1,i-1, j-1); + surrounds += lightUpCell(target, power,-1, 0,i-1, j+0); + surrounds += lightUpCell(target, power,-1, 1,i-1, j+1); } - if(surrounds) + if(surrounds.dot(surrounds)>0.00001f) { plotSquare(i,j,radius, std::bind(&lightingEngineViewscreen::doRay,this,std::ref(target),power,i,j,_1,_2)); @@ -364,7 +417,6 @@ void lightingEngineViewscreen::calculate() } doOcupancyAndLights(); doFovs(); - //for each lightsource in viewscreen+x do light } void lightingEngineViewscreen::updateWindow() { @@ -407,7 +459,7 @@ void lightingEngineViewscreen::addOclusion(int tileId,const lightCell& c,float t if(thickness > 0.999 && thickness < 1.001) ocupancy[tileId]*=c; else - ocupancy[tileId]*=(c.power(thickness)); + ocupancy[tileId]*=(c.pow(thickness)); } lightCell getStandartColor(int colorId) { @@ -498,7 +550,7 @@ lightCell lightingEngineViewscreen::propogateSun(MapExtras::Block* b, int x,int ret*=matIce.transparency; else if(basicShapeIce==df::tiletype_shape_basic::Floor || basicShapeIce==df::tiletype_shape_basic::Ramp || shapeIce==df::tiletype_shape::STAIR_UP) if(!lastLevel) - ret*=matIce.transparency.power(1.0f/7.0f); + ret*=matIce.transparency.pow(1.0f/7.0f); } lightDef=getMaterial(mat.mat_type,mat.mat_index); @@ -513,7 +565,7 @@ lightCell lightingEngineViewscreen::propogateSun(MapExtras::Block* b, int x,int { if(!lastLevel) - ret*=lightDef->transparency.power(1.0f/7.0f); + ret*=lightDef->transparency.pow(1.0f/7.0f); } else if(shape==df::tiletype_shape::STAIR_DOWN || shape==df::tiletype_shape::STAIR_UPDOWN) { @@ -521,11 +573,11 @@ lightCell lightingEngineViewscreen::propogateSun(MapExtras::Block* b, int x,int } if(d.bits.liquid_type == df::enums::tile_liquid::Water && d.bits.flow_size > 0) { - ret *=matWater.transparency.power((float)d.bits.flow_size/7.0f); + ret *=matWater.transparency.pow((float)d.bits.flow_size/7.0f); } else if(d.bits.liquid_type == df::enums::tile_liquid::Magma && d.bits.flow_size > 0) { - ret *=matLava.transparency.power((float)d.bits.flow_size/7.0f); + ret *=matLava.transparency.pow((float)d.bits.flow_size/7.0f); } return ret; } @@ -628,7 +680,7 @@ void lightingEngineViewscreen::doOcupancyAndLights() daycol= fmod(dayHour,24.0f)/24.0f; //1->12h 0->24h lightCell sky_col=getSkyColor(daycol); - lightSource sky(sky_col, 15);//auto calculate best size + lightSource sky(sky_col, -1);//auto calculate best size lightSource candle(lightCell(0.96f,0.84f,0.03f),5); lightSource torch(lightCell(0.9f,0.75f,0.3f),8); diff --git a/plugins/rendermax/renderer_light.hpp b/plugins/rendermax/renderer_light.hpp index e5a9db787..58676e29c 100644 --- a/plugins/rendermax/renderer_light.hpp +++ b/plugins/rendermax/renderer_light.hpp @@ -152,7 +152,7 @@ private: void doRay(std::vector & target, lightCell power,int cx,int cy,int tx,int ty); void doFovs(); void doLight(std::vector & target, int index); - bool lightUpCell(std::vector & target, lightCell& power,int dx,int dy,int tx,int ty); + lightCell lightUpCell(std::vector & target, lightCell power,int dx,int dy,int tx,int ty); bool addLight(int tileId,const lightSource& light); void addOclusion(int tileId,const lightCell& c,float thickness); diff --git a/plugins/rendermax/renderer_opengl.hpp b/plugins/rendermax/renderer_opengl.hpp index 8ba35f5ea..151ff94f0 100644 --- a/plugins/rendermax/renderer_opengl.hpp +++ b/plugins/rendermax/renderer_opengl.hpp @@ -196,7 +196,7 @@ struct lightCell { } - lightCell operator-(lightCell cell) const + lightCell operator-(const lightCell& cell) const { return lightCell(r-cell.r,g-cell.g,b-cell.b); } @@ -208,17 +208,31 @@ struct lightCell { return lightCell(r/val,g/val,b/val); } - lightCell operator*(lightCell cell) const + lightCell operator*(const lightCell& cell) const { return lightCell(r*cell.r,g*cell.g,b*cell.b); } - lightCell operator*=(lightCell cell) + lightCell operator*=(float val) + { + r*=val; + g*=val; + b*=val; + return *this; + } + lightCell operator*=(const lightCell& cell) { r*=cell.r; g*=cell.g; b*=cell.b; return *this; } + lightCell operator+=(const lightCell& cell) + { + r+=cell.r; + g+=cell.g; + b+=cell.b; + return *this; + } lightCell operator+(const lightCell& other) const { return lightCell(r+other.r,g+other.g,b+other.b); @@ -231,13 +245,13 @@ struct lightCell { return r*other.r+g*other.g+b*other.b; } - lightCell power(const float exp) const + lightCell pow(const float exp) const { - return lightCell(pow(r, exp), pow(g, exp), pow(b, exp)); + return lightCell(std::pow(r, exp), std::pow(g, exp), std::pow(b, exp)); } - lightCell power(const int exp) const + lightCell pow(const int exp) const { - return lightCell(pow(r, exp), pow(g, exp), pow(b, exp)); + return lightCell(std::pow(r, exp), std::pow(g, exp), std::pow(b, exp)); } }; struct renderer_test : public renderer_wrap {