Skip to content

Commit

Permalink
Merge pull request #322 from Monsterovich/3.1.1
Browse files Browse the repository at this point in the history
Added basic level fog preview
  • Loading branch information
sirjuddington committed Oct 1, 2015
2 parents bd7223f + 6064054 commit 22a479f
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 40 deletions.
100 changes: 70 additions & 30 deletions src/MapRenderer3D.cpp
Expand Up @@ -55,6 +55,8 @@ CVAR(Int, render_3d_things, 1, CVAR_SAVE)
CVAR(Int, render_3d_things_style, 1, CVAR_SAVE)
CVAR(Int, render_3d_hilight, 1, CVAR_SAVE)
CVAR(Float, render_3d_brightness, 1, CVAR_SAVE)
CVAR(Float, render_fog_distance, 1500, CVAR_SAVE)
CVAR(Bool, render_fog_new_formula, true, CVAR_SAVE)


/*******************************************************************
Expand Down Expand Up @@ -84,7 +86,6 @@ MapRenderer3D::MapRenderer3D(SLADEMap* map)
this->skytex1 = "SKY1";
this->quads = NULL;
this->flats = NULL;
this->last_light = 255;
this->tex_last = NULL;
this->n_quads = 0;
this->n_flats = 0;
Expand Down Expand Up @@ -450,7 +451,7 @@ void MapRenderer3D::setupView(int width, int height)
}

/* MapRenderer3D::setLight
* Sets the OpenGL colour+fog for rendering an object using [colour]
* Sets the OpenGL colour for rendering an object using [colour]
* and [light] level
*******************************************************************/
void MapRenderer3D::setLight(rgba_t& colour, uint8_t light, float alpha)
Expand All @@ -462,28 +463,45 @@ void MapRenderer3D::setLight(rgba_t& colour, uint8_t light, float alpha)
// Apply brightness
light = MathStuff::clamp(light * render_3d_brightness, 0, 255);

// Setup fog
if (fog && light != last_light)
{
if (light >= 240)
glDisable(GL_FOG);
else
{
glEnable(GL_FOG);
float lm = light/170.0f;
glFogf(GL_FOG_END, (lm * lm * 3000.0f));
}

last_light = light;
}

// If we have a non-coloured light, darken it a bit to
// closer resemble the software renderer light level
float mult = (float)light / 255.0f;
mult *= (mult * 1.3f);
glColor4f(colour.fr()*mult, colour.fg()*mult, colour.fb()*mult, colour.fa()*alpha);
}

/* MapRenderer3D::setFog
* Sets the OpenGL fog for rendering an object using [fogcol]
*******************************************************************/
void MapRenderer3D::setFog(rgba_t &fogcol, uint8_t light)
{
if (!fog)
return;

GLfloat fogColor[3]= {fogcol.fr(), fogcol.fg(), fogcol.fb()};

glEnable(GL_FOG);

glFogfv(GL_FOG_COLOR, fogColor);
glFogi(GL_FOG_MODE, GL_LINEAR);
glFogf(GL_FOG_DENSITY, 1.0f);
glFogf(GL_FOG_START, 0.0f);

// check if fog color is default
if ((fogColor[0] == 0 && fogColor[1] == 0 && fogColor[2] == 0) || !render_fog_new_formula)
{
float lm = light/170.0f;
glFogf(GL_FOG_END, (lm * lm * 3000.0f));
}
else
glFogf(GL_FOG_END, render_fog_distance);

if (render_fog_quality)
glHint(GL_FOG_HINT, GL_NICEST);
else
glHint(GL_FOG_HINT, GL_FASTEST);
}

/* MapRenderer3D::renderMap
* Renders the map in 3d
*******************************************************************/
Expand All @@ -497,20 +515,7 @@ void MapRenderer3D::renderMap()
glDepthMask(GL_TRUE);
glAlphaFunc(GL_GREATER, 0.0f);

// Setup fog
GLfloat fogColor[4]= {0.0f, 0.0f, 0.0f, 0.6f};
glFogi(GL_FOG_MODE, GL_LINEAR);
glFogfv(GL_FOG_COLOR, fogColor);
glFogf(GL_FOG_DENSITY, 2.0f);
glFogf(GL_FOG_START, 0.0f);
glFogf(GL_FOG_END, 3000.0f);
if (render_fog_quality)
glHint(GL_FOG_HINT, GL_NICEST);
else
glHint(GL_FOG_HINT, GL_FASTEST);

// Init
last_light = -1;
tex_last = NULL;

// Init VBO stuff
Expand Down Expand Up @@ -818,6 +823,7 @@ void MapRenderer3D::updateSector(unsigned index)
floors[index].sector = sector;
floors[index].texture = theMapEditor->textureManager().getFlat(sector->getFloorTex(), theGameConfiguration->mixTexFlats());
floors[index].colour = sector->getColour(1, true);
floors[index].fogcolour = sector->getFogColour();
floors[index].light = sector->getLight(1);
floors[index].flags = 0;
floors[index].plane = sector->getFloorPlane();
Expand All @@ -838,6 +844,7 @@ void MapRenderer3D::updateSector(unsigned index)
ceilings[index].sector = sector;
ceilings[index].texture = theMapEditor->textureManager().getFlat(sector->getCeilingTex(), theGameConfiguration->mixTexFlats());
ceilings[index].colour = sector->getColour(2, true);
ceilings[index].fogcolour = sector->getFogColour();
ceilings[index].light = sector->getLight(2);
ceilings[index].flags = CEIL;
ceilings[index].plane = sector->getCeilingPlane();
Expand Down Expand Up @@ -884,6 +891,9 @@ void MapRenderer3D::renderFlat(flat_3d_t* flat)
// Setup colour/light
setLight(flat->colour, flat->light, alpha);

// Setup fog colour
setFog(flat->fogcolour, flat->light);

// Render flat
if (OpenGL::vboSupport() && flats_use_vbo)
{
Expand Down Expand Up @@ -1167,6 +1177,7 @@ void MapRenderer3D::updateLine(unsigned index)
plane_t fp1 = line->frontSector()->getFloorPlane();
plane_t cp1 = line->frontSector()->getCeilingPlane();
rgba_t colour1 = line->frontSector()->getColour(0, true);
rgba_t fogcolour1 = line->frontSector()->getFogColour();
int light1 = line->frontSector()->getLight();
int xoff1 = line->s1()->getOffsetX();
int yoff1 = line->s1()->getOffsetY();
Expand Down Expand Up @@ -1203,6 +1214,7 @@ void MapRenderer3D::updateLine(unsigned index)
// Create quad
setupQuad(&quad, line->x1(), line->y1(), line->x2(), line->y2(), cp1, fp1);
quad.colour = colour1;
quad.fogcolour = fogcolour1;
quad.light = light1;
quad.texture = theMapEditor->textureManager().getTexture(line->s1()->getTexMiddle(), mixed);
setupQuadTexCoords(&quad, length, xoff, yoff, ceiling1, floor1, lpeg, sx, sy);
Expand All @@ -1221,6 +1233,7 @@ void MapRenderer3D::updateLine(unsigned index)
plane_t fp2 = line->backSector()->getFloorPlane();
plane_t cp2 = line->backSector()->getCeilingPlane();
rgba_t colour2 = line->backSector()->getColour(0, true);
rgba_t fogcolour2 = line->backSector()->getFogColour();
int light2 = line->backSector()->getLight();
int xoff2 = line->s2()->getOffsetX();
int yoff2 = line->s2()->getOffsetY();
Expand Down Expand Up @@ -1273,6 +1286,7 @@ void MapRenderer3D::updateLine(unsigned index)
// Create quad
setupQuad(&quad, line->x1(), line->y1(), line->x2(), line->y2(), fp2, fp1);
quad.colour = colour1;
quad.fogcolour = fogcolour1;
quad.light = light1;
quad.texture = theMapEditor->textureManager().getTexture(line->s1()->getTexLower(), mixed);
setupQuadTexCoords(&quad, length, xoff, yoff, floor2, floor1, false, sx, sy);
Expand Down Expand Up @@ -1351,6 +1365,7 @@ void MapRenderer3D::updateLine(unsigned index)
// Create quad
setupQuad(&quad, line->x1(), line->y1(), line->x2(), line->y2(), top, bottom);
quad.colour = colour1.ampf(1.0f, 1.0f, 1.0f, alpha);
quad.fogcolour = fogcolour1;
quad.light = light1;
setupQuadTexCoords(&quad, length, xoff, ytex, top, bottom, false, sx, sy);
quad.flags |= MIDTEX;
Expand Down Expand Up @@ -1393,6 +1408,7 @@ void MapRenderer3D::updateLine(unsigned index)
// Create quad
setupQuad(&quad, line->x1(), line->y1(), line->x2(), line->y2(), cp1, cp2);
quad.colour = colour1;
quad.fogcolour = fogcolour1;
quad.light = light1;
quad.texture = theMapEditor->textureManager().getTexture(line->s1()->getTexUpper(), mixed);
setupQuadTexCoords(&quad, length, xoff, yoff, ceiling1, ceiling2, !upeg, sx, sy);
Expand Down Expand Up @@ -1438,6 +1454,7 @@ void MapRenderer3D::updateLine(unsigned index)
// Create quad
setupQuad(&quad, line->x2(), line->y2(), line->x1(), line->y1(), fp1, fp2);
quad.colour = colour2;
quad.fogcolour = fogcolour2;
quad.light = light2;
quad.texture = theMapEditor->textureManager().getTexture(line->s2()->getTexLower(), mixed);
setupQuadTexCoords(&quad, length, xoff, yoff, floor1, floor2, false, sx, sy);
Expand Down Expand Up @@ -1516,6 +1533,7 @@ void MapRenderer3D::updateLine(unsigned index)
// Create quad
setupQuad(&quad, line->x2(), line->y2(), line->x1(), line->y1(), top, bottom);
quad.colour = colour2.ampf(1.0f, 1.0f, 1.0f, alpha);
quad.fogcolour = fogcolour2;
quad.light = light2;
setupQuadTexCoords(&quad, length, xoff, ytex, top, bottom, false, sx, sy);
quad.flags |= BACK;
Expand Down Expand Up @@ -1559,6 +1577,7 @@ void MapRenderer3D::updateLine(unsigned index)
// Create quad
setupQuad(&quad, line->x2(), line->y2(), line->x1(), line->y1(), cp2, cp1);
quad.colour = colour2;
quad.fogcolour = fogcolour2;
quad.light = light2;
quad.texture = theMapEditor->textureManager().getTexture(line->s2()->getTexUpper(), mixed);
setupQuadTexCoords(&quad, length, xoff, yoff, ceiling2, ceiling1, !upeg, sx, sy);
Expand Down Expand Up @@ -1601,6 +1620,9 @@ void MapRenderer3D::renderQuad(MapRenderer3D::quad_3d_t* quad, float alpha)
// Setup colour/light
setLight(quad->colour, quad->light, alpha);

// Setup fog
setFog(quad->fogcolour, quad->light);

// Draw quad
glBegin(GL_QUADS);
glTexCoord2f(quad->points[0].tx, quad->points[0].ty); glVertex3f(quad->points[0].x, quad->points[0].y, quad->points[0].z);
Expand Down Expand Up @@ -1962,6 +1984,11 @@ void MapRenderer3D::renderThings()
col.set(things[a].sector->getColour(0, true));
}
setLight(col, light, calcDistFade(dist, mdist));
rgba_t fogcol = rgba_t(0, 0, 0, 0);
if (things[a].sector)
fogcol = things[a].sector->getFogColour();

setFog(fogcol, light);

// Draw thing
glBegin(GL_QUADS);
Expand Down Expand Up @@ -2001,6 +2028,14 @@ void MapRenderer3D::renderThings()

// Fill
glColor4f(col.fr(), col.fg(), col.fb(), 0.21f);
uint8_t light2 = 255;
rgba_t fogcol2 = rgba_t(0, 0, 0, 0);
if (things[a].sector)
{
light2 = things[a].sector->getLight();
fogcol2 = things[a].sector->getFogColour();
}
setFog(fogcol2, light2);
glBegin(GL_QUADS);
// Bottom
glVertex3f(thing->xPos() - radius, thing->yPos() - radius, bottom);
Expand Down Expand Up @@ -2453,8 +2488,13 @@ void MapRenderer3D::checkVisibleFlats()
// Update sector info if needed
if (floors[a].updated_time < sector->modifiedTime() ||
floors[a].updated_time < sector->geometryUpdatedTime())
{
updateSector(a);

// update sector lines
sector->setModified();
}

// Set distance fade alpha
if (render_max_dist > 0)
alpha = calcDistFade(dist_sectors[a], render_max_dist);
Expand Down
4 changes: 3 additions & 1 deletion src/MapRenderer3D.h
Expand Up @@ -42,6 +42,7 @@ class MapRenderer3D : public Listener
{
gl_vertex_t points[4];
rgba_t colour;
rgba_t fogcolour;
uint8_t light;
GLTexture* texture;
uint8_t flags;
Expand Down Expand Up @@ -88,6 +89,7 @@ class MapRenderer3D : public Listener
uint8_t flags;
uint8_t light;
rgba_t colour;
rgba_t fogcolour;
GLTexture* texture;
plane_t plane;
float alpha;
Expand Down Expand Up @@ -141,6 +143,7 @@ class MapRenderer3D : public Listener
// -- Rendering --
void setupView(int width, int height);
void setLight(rgba_t& colour, uint8_t light, float alpha = 1.0f);
void setFog(rgba_t &fogcol, uint8_t light);
void renderMap();
void renderSkySlice(float top, float bottom, float atop, float abottom, float size, float tx = 0.125f, float ty = 2.0f);
void renderSky();
Expand Down Expand Up @@ -189,7 +192,6 @@ class MapRenderer3D : public Listener
bool udmf_zdoom;
bool fullbright;
bool fog;
int last_light;
GLTexture* tex_last;
unsigned n_quads;
unsigned n_flats;
Expand Down
25 changes: 25 additions & 0 deletions src/MapSector.cpp
Expand Up @@ -656,6 +656,31 @@ rgba_t MapSector::getColour(int where, bool fullbright)
}
}

/* MapSector::getColour
* Returns the fog colour of the sector
*******************************************************************/
rgba_t MapSector::getFogColour()
{
rgba_t color(0, 0, 0, 0);

// map specials/scripts
if (parent_map->mapSpecials()->tagFadeColoursSet())
{
if (parent_map->mapSpecials()->getTagFadeColour(tag, &color))
return color;
}

// udmf
if (parent_map->currentFormat() == MAP_UDMF && S_CMPNOCASE(parent_map->udmfNamespace(), "zdoom"))
{
int intcol = MapObject::intProperty("fadecolor");

wxColour wxcol(intcol);
color = rgba_t(wxcol.Blue(), wxcol.Green(), wxcol.Red(), 0);
}
return color;
}

/* MapSector::connectSide
* Adds [side] to the list of 'connected sides' (sides that are part
* of this sector)
Expand Down
1 change: 1 addition & 0 deletions src/MapSector.h
Expand Up @@ -122,6 +122,7 @@ class MapSector : public MapObject
uint8_t getLight(int where = 0);
void changeLight(int amount, int where = 0);
rgba_t getColour(int where = 0, bool fullbright = false);
rgba_t getFogColour();
long geometryUpdatedTime() { return geometry_updated; }

void connectSide(MapSide* side);
Expand Down

0 comments on commit 22a479f

Please sign in to comment.