From 7c190cd3f0799f385725be5115b54c0965980920 Mon Sep 17 00:00:00 2001 From: Warmist Date: Thu, 27 Jun 2013 16:11:29 +0300 Subject: [PATCH 1/2] 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 88ba41b71..7d955a76a 100644 --- a/plugins/rendermax/renderer_light.cpp +++ b/plugins/rendermax/renderer_light.cpp @@ -222,7 +222,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)); @@ -248,7 +301,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)) { @@ -271,12 +324,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]; @@ -284,20 +337,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; @@ -315,21 +368,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)); @@ -365,7 +418,6 @@ void lightingEngineViewscreen::calculate() } doOcupancyAndLights(); doFovs(); - //for each lightsource in viewscreen+x do light } void lightingEngineViewscreen::updateWindow() { @@ -405,7 +457,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) { @@ -496,7 +548,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); @@ -511,7 +563,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) { @@ -519,11 +571,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; } @@ -626,7 +678,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 { From 26d557fc68423eba7bac68afe1278ca20dee9c1e Mon Sep 17 00:00:00 2001 From: Warmist Date: Thu, 27 Jun 2013 18:11:50 +0300 Subject: [PATCH 2/2] Fixed bug with buildings and added gems to material definition. --- plugins/rendermax/renderer_light.cpp | 8 ++++++-- plugins/rendermax/renderer_light.hpp | 1 - plugins/rendermax/rendermax.lua | 12 +++++++++++- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/plugins/rendermax/renderer_light.cpp b/plugins/rendermax/renderer_light.cpp index 811a03660..4c3db88f9 100644 --- a/plugins/rendermax/renderer_light.cpp +++ b/plugins/rendermax/renderer_light.cpp @@ -882,11 +882,15 @@ void lightingEngineViewscreen::doOcupancyAndLights() df::coord2d p1(bld->x1,bld->y1); df::coord2d p2(bld->x2,bld->y2); p1=worldToViewportCoord(p1,vp,window2d); - p2=worldToViewportCoord(p1,vp,window2d); + p2=worldToViewportCoord(p2,vp,window2d); if(isInViewport(p1,vp)||isInViewport(p2,vp)) { - int tile=getIndex(p1.x,p1.y); //TODO multitile buildings. How they would work? + int tile; + if(isInViewport(p1,vp)) + tile=getIndex(p1.x,p1.y); //TODO multitile buildings. How they would work? + else + tile=getIndex(p2.x,p2.y); df::building_type type = bld->getType(); buildingLightDef* def=getBuilding(bld); if(!def) diff --git a/plugins/rendermax/renderer_light.hpp b/plugins/rendermax/renderer_light.hpp index 58676e29c..5a53242b0 100644 --- a/plugins/rendermax/renderer_light.hpp +++ b/plugins/rendermax/renderer_light.hpp @@ -114,7 +114,6 @@ struct matLightDef matLightDef(lightCell transparency):isTransparent(true),isEmiting(false),transparency(transparency){} lightSource makeSource(float size=1) const { - //TODO implement sizeModifiesPower/range if(size>0.999 && size<1.001) return lightSource(emitColor,radius); else diff --git a/plugins/rendermax/rendermax.lua b/plugins/rendermax/rendermax.lua index 74e5fc2d0..df2619864 100644 --- a/plugins/rendermax/rendermax.lua +++ b/plugins/rendermax/rendermax.lua @@ -92,6 +92,16 @@ function makeMaterialDef(transparency,emitance,radius,flags) end return {tr=transparency,em=emitance,rad=radius,flags=flg} end +function colorFrom16(col16) + return copyall(df.global.enabler.ccolor[col16]) +end +function addGems() + for k,v in pairs(df.global.world.raws.inorganics) do + if v.material.flags.IS_GEM then + addMaterial("INORGANIC:"..v.id,colorFrom16(v.material.build_color[0])) + end + end +end ------------------------------------------------------------------------ ---------------- Configuration Starts Here ------------------------- ------------------------------------------------------------------------ @@ -128,7 +138,7 @@ addMaterial("PLANT:MUSHROOM_HELMET_PLUMP",nil,{0.2,0.1,0.6},2) addMaterial("INORGANIC:ADAMANTINE",{0.1,0.3,0.3},{0.1,0.3,0.3},4) -- creature stuff addMaterial("CREATURE:DRAGON:BLOOD",nil,{0.6,0.1,0.1},4) --- TODO gems +addGems() --buildings addBuilding("Statue",{1,1,1},{0.9,0.75,0.3},8) addBuilding("Bed",{1,1,1},{0.3,0.2,0.0},2)