diff --git a/src/EnvRenderer.c b/src/EnvRenderer.c index c5e8d0b8b..2195f8f76 100644 --- a/src/EnvRenderer.c +++ b/src/EnvRenderer.c @@ -21,7 +21,7 @@ cc_bool EnvRenderer_Legacy, EnvRenderer_Minimal; #define ENV_SMALL_VERTICES 4096 -static float EnvRenderer_BlendFactor(float x) { +static float CalcBlendFactor(float x) { /* return -0.05 + 0.22 * (Math_Log(x) * 0.25f); */ double blend = -0.13 + 0.28 * (Math_Log(x) * 0.25); if (blend < 0.0) blend = 0.0; @@ -31,7 +31,7 @@ static float EnvRenderer_BlendFactor(float x) { #define EnvRenderer_AxisSize() (EnvRenderer_Legacy ? 128 : 2048) /* Returns the number of vertices needed to subdivide a quad */ -static int EnvRenderer_Vertices(int axis1Len, int axis2Len) { +static int CalcNumVertices(int axis1Len, int axis2Len) { int axisSize = EnvRenderer_AxisSize(); return Math_CeilDiv(axis1Len, axisSize) * Math_CeilDiv(axis2Len, axisSize) * 4; } @@ -40,7 +40,7 @@ static int EnvRenderer_Vertices(int axis1Len, int axis2Len) { /*########################################################################################################################* *------------------------------------------------------------Fog----------------------------------------------------------* *#########################################################################################################################*/ -static void EnvRenderer_CalcFog(float* density, PackedCol* col) { +static void CalcFog(float* density, PackedCol* col) { Vec3 pos; IVec3 coords; BlockID block; @@ -60,12 +60,12 @@ static void EnvRenderer_CalcFog(float* density, PackedCol* col) { } else { *density = 0.0f; /* Blend fog and sky together */ - blend = EnvRenderer_BlendFactor((float)Game_ViewDistance); + blend = CalcBlendFactor((float)Game_ViewDistance); *col = PackedCol_Lerp(Env.FogCol, Env.SkyCol, blend); } } -static void EnvRenderer_UpdateFogMinimal(float fogDensity) { +static void UpdateFogMinimal(float fogDensity) { int dist; /* TODO: rewrite this to avoid raising the event? want to avoid recreating vbos too many times often */ @@ -82,7 +82,7 @@ static void EnvRenderer_UpdateFogMinimal(float fogDensity) { } } -static void EnvRenderer_UpdateFogNormal(float fogDensity, PackedCol fogCol) { +static void UpdateFogNormal(float fogDensity, PackedCol fogCol) { double density; if (fogDensity != 0.0f) { @@ -113,13 +113,13 @@ void EnvRenderer_UpdateFog(void) { PackedCol fogCol; if (!World.Blocks) return; - EnvRenderer_CalcFog(&fogDensity, &fogCol); + CalcFog(&fogDensity, &fogCol); Gfx_ClearCol(fogCol); if (EnvRenderer_Minimal) { - EnvRenderer_UpdateFogMinimal(fogDensity); + UpdateFogMinimal(fogDensity); } else { - EnvRenderer_UpdateFogNormal(fogDensity, fogCol); + UpdateFogNormal(fogDensity, fogCol); } } @@ -152,7 +152,7 @@ void EnvRenderer_RenderClouds(double deltaTime) { Gfx_LoadIdentityMatrix(MATRIX_TEXTURE); } -static void EnvRenderer_DrawCloudsY(int x1, int z1, int x2, int z2, int y, VertexP3fT2fC4b* v) { +static void DrawCloudsY(int x1, int z1, int x2, int z2, int y, VertexP3fT2fC4b* v) { int endX = x2, endZ = z2, startZ = z1, axisSize = EnvRenderer_AxisSize(); float u1, u2, v1, v2; float yy = (float)y + 0.1f; @@ -179,7 +179,7 @@ static void EnvRenderer_DrawCloudsY(int x1, int z1, int x2, int z2, int y, Verte } } -static void EnvRenderer_UpdateClouds(void) { +static void UpdateClouds(void) { VertexP3fT2fC4b v[ENV_SMALL_VERTICES]; VertexP3fT2fC4b* ptr; int extent; @@ -192,14 +192,14 @@ static void EnvRenderer_UpdateClouds(void) { extent = Utils_AdjViewDist(Game_ViewDistance); x1 = -extent; x2 = World.Width + extent; z1 = -extent; z2 = World.Length + extent; - clouds_vertices = EnvRenderer_Vertices(x2 - x1, z2 - z1); + clouds_vertices = CalcNumVertices(x2 - x1, z2 - z1); ptr = v; if (clouds_vertices > ENV_SMALL_VERTICES) { ptr = (VertexP3fT2fC4b*)Mem_Alloc(clouds_vertices, sizeof(VertexP3fT2fC4b), "clouds vertices"); } - EnvRenderer_DrawCloudsY(x1, z1, x2, z2, Env.CloudsHeight, ptr); + DrawCloudsY(x1, z1, x2, z2, Env.CloudsHeight, ptr); clouds_vb = Gfx_CreateVb(ptr, VERTEX_FORMAT_P3FT2FC4B, clouds_vertices); if (clouds_vertices > ENV_SMALL_VERTICES) Mem_Free(ptr); @@ -237,7 +237,7 @@ void EnvRenderer_RenderSky(double deltaTime) { } } -static void EnvRenderer_DrawSkyY(int x1, int z1, int x2, int z2, int y, VertexP3fC4b* v) { +static void DrawSkyY(int x1, int z1, int x2, int z2, int y, VertexP3fC4b* v) { int endX = x2, endZ = z2, startZ = z1, axisSize = EnvRenderer_AxisSize(); PackedCol col = Env.SkyCol; @@ -257,7 +257,7 @@ static void EnvRenderer_DrawSkyY(int x1, int z1, int x2, int z2, int y, VertexP3 } } -static void EnvRenderer_UpdateSky(void) { +static void UpdateSky(void) { VertexP3fC4b v[ENV_SMALL_VERTICES]; VertexP3fC4b* ptr; int extent, height; @@ -270,7 +270,7 @@ static void EnvRenderer_UpdateSky(void) { extent = Utils_AdjViewDist(Game_ViewDistance); x1 = -extent; x2 = World.Width + extent; z1 = -extent; z2 = World.Length + extent; - sky_vertices = EnvRenderer_Vertices(x2 - x1, z2 - z1); + sky_vertices = CalcNumVertices(x2 - x1, z2 - z1); ptr = v; if (sky_vertices > ENV_SMALL_VERTICES) { @@ -278,7 +278,7 @@ static void EnvRenderer_UpdateSky(void) { } height = max((World.Height + 2), Env.CloudsHeight) + 6; - EnvRenderer_DrawSkyY(x1, z1, x2, z2, height, ptr); + DrawSkyY(x1, z1, x2, z2, height, ptr); sky_vb = Gfx_CreateVb(ptr, VERTEX_FORMAT_P3FC4B, sky_vertices); if (sky_vertices > ENV_SMALL_VERTICES) Mem_Free(ptr); @@ -323,7 +323,7 @@ void EnvRenderer_RenderSkybox(double deltaTime) { Gfx_SetDepthWrite(true); } -static void EnvRenderer_UpdateSkybox(void) { +static void UpdateSkybox(void) { static VertexP3fT2fC4b vertices[SKYBOX_COUNT] = { /* Front quad */ { -1, -1, -1, 0, 0.25f, 1.00f }, { 1, -1, -1, 0, 0.50f, 1.00f }, @@ -367,7 +367,7 @@ static IVec3 lastPos; #define WEATHER_VERTS_COUNT 8 * (WEATHER_EXTENT * 2 + 1) * (WEATHER_EXTENT * 2 + 1) #define Weather_Pack(x, z) ((x) * World.Length + (z)) -static void EnvRenderer_InitWeatherHeightmap(void) { +static void InitWeatherHeightmap(void) { int i; Weather_Heightmap = (cc_int16*)Mem_Alloc(World.Width * World.Length, 2, "weather heightmap"); @@ -376,7 +376,7 @@ static void EnvRenderer_InitWeatherHeightmap(void) { } } -#define EnvRenderer_RainCalcBody(get_block)\ +#define RainCalcBody(get_block)\ for (y = maxY; y >= 0; y--, i -= World.OneY) {\ draw = Blocks.Draw[get_block];\ \ @@ -386,17 +386,17 @@ for (y = maxY; y >= 0; y--, i -= World.OneY) {\ }\ } -static int EnvRenderer_CalcRainHeightAt(int x, int maxY, int z, int hIndex) { +static int CalcRainHeightAt(int x, int maxY, int z, int hIndex) { int i = World_Pack(x, maxY, z), y; cc_uint8 draw; #ifndef EXTENDED_BLOCKS - EnvRenderer_RainCalcBody(World.Blocks[i]); + RainCalcBody(World.Blocks[i]); #else if (World.IDMask <= 0xFF) { - EnvRenderer_RainCalcBody(World.Blocks[i]); + RainCalcBody(World.Blocks[i]); } else { - EnvRenderer_RainCalcBody(World.Blocks[i] | (World.Blocks2[i] << 8)); + RainCalcBody(World.Blocks[i] | (World.Blocks2[i] << 8)); } #endif @@ -404,7 +404,7 @@ static int EnvRenderer_CalcRainHeightAt(int x, int maxY, int z, int hIndex) { return -1; } -static float EnvRenderer_RainHeight(int x, int z) { +static float GetRainHeight(int x, int z) { int hIndex, height; int y; if (!World_ContainsXZ(x, z)) return (float)Env.EdgeHeight; @@ -412,7 +412,7 @@ static float EnvRenderer_RainHeight(int x, int z) { hIndex = Weather_Pack(x, z); height = Weather_Heightmap[hIndex]; - y = height == Int16_MaxValue ? EnvRenderer_CalcRainHeightAt(x, World.MaxY, z, hIndex) : height; + y = height == Int16_MaxValue ? CalcRainHeightAt(x, World.MaxY, z, hIndex) : height; return y == -1 ? 0 : y + Blocks.MaxBB[World_GetBlock(x, y, z)].Y; } @@ -435,11 +435,11 @@ void EnvRenderer_OnBlockChanged(int x, int y, int z, BlockID oldBlock, BlockID n } else { /* Part of the column is now visible to rain, we don't know how exactly how high it should be though. */ /* However, we know that if the old block was above or equal to rain height, then the new rain height must be <= old block.y */ - EnvRenderer_CalcRainHeightAt(x, y, z, hIndex); + CalcRainHeightAt(x, y, z, hIndex); } } -static float EnvRenderer_RainAlphaAt(float x) { +static float CalcRainAlphaAt(float x) { /* Wolfram Alpha: fit {0,178},{1,169},{4,147},{9,114},{16,59},{25,9} */ float falloff = 0.05f * x * x - 7 * x; return 178 + falloff * Env.WeatherFade; @@ -461,7 +461,7 @@ void EnvRenderer_RenderWeather(double deltaTime) { weather = Env.Weather; if (weather == WEATHER_SUNNY) return; - if (!Weather_Heightmap) EnvRenderer_InitWeatherHeightmap(); + if (!Weather_Heightmap) InitWeatherHeightmap(); Gfx_BindTexture(weather == WEATHER_RAINY ? rain_tex : snow_tex); IVec3_Floor(&pos, &Camera.CurrentPos); @@ -484,7 +484,7 @@ void EnvRenderer_RenderWeather(double deltaTime) { for (dz = -WEATHER_EXTENT; dz <= WEATHER_EXTENT; dz++) { x = pos.X + dx; z = pos.Z + dz; - y = EnvRenderer_RainHeight(x, z); + y = GetRainHeight(x, z); height = pos.Y - y; if (height <= 0) continue; @@ -494,7 +494,7 @@ void EnvRenderer_RenderWeather(double deltaTime) { } dist = dx * dx + dz * dz; - alpha = EnvRenderer_RainAlphaAt((float)dist); + alpha = CalcRainAlphaAt((float)dist); Math_Clamp(alpha, 0.0f, 255.0f); col = (col & PACKEDCOL_RGB_MASK) | PackedCol_A_Bits(alpha); @@ -543,7 +543,7 @@ static int sides_vertices, edges_vertices; static cc_bool sides_fullBright, edges_fullBright; static TextureLoc edges_lastTexLoc, sides_lastTexLoc; -static void EnvRenderer_RenderBorders(BlockID block, GfxResourceID vb, GfxResourceID tex, int count) { +static void RenderBorders(BlockID block, GfxResourceID vb, GfxResourceID tex, int count) { if (!vb) return; Gfx_SetTexturing(true); @@ -561,8 +561,7 @@ static void EnvRenderer_RenderBorders(BlockID block, GfxResourceID vb, GfxResour } void EnvRenderer_RenderMapSides(double delta) { - EnvRenderer_RenderBorders(Env.SidesBlock, - sides_vb, sides_tex, sides_vertices); + RenderBorders(Env.SidesBlock, sides_vb, sides_tex, sides_vertices); } void EnvRenderer_RenderMapEdges(double delta) { @@ -571,11 +570,10 @@ void EnvRenderer_RenderMapEdges(double delta) { int yVisible = min(0, Env_SidesHeight); if (Camera.CurrentPos.Y < yVisible && sides_vb) return; - EnvRenderer_RenderBorders(Env.EdgeBlock, - edges_vb, edges_tex, edges_vertices); + RenderBorders(Env.EdgeBlock, edges_vb, edges_tex, edges_vertices); } -static void EnvRenderer_MakeBorderTex(GfxResourceID* texId, BlockID block) { +static void MakeBorderTex(GfxResourceID* texId, BlockID block) { TextureLoc loc = Block_Tex(block, FACE_YMAX); if (Gfx.LostContext) return; @@ -589,7 +587,7 @@ static Rect2D EnvRenderer_Rect(int x, int y, int width, int height) { return r; } -static void EnvRenderer_CalcBorderRects(Rect2D* rects) { +static void CalcBorderRects(Rect2D* rects) { int extent = Utils_AdjViewDist(Game_ViewDistance); rects[0] = EnvRenderer_Rect(-extent, -extent, extent + World.Width + extent, extent); rects[1] = EnvRenderer_Rect(-extent, World.Length, extent + World.Width + extent, extent); @@ -598,15 +596,15 @@ static void EnvRenderer_CalcBorderRects(Rect2D* rects) { rects[3] = EnvRenderer_Rect(World.Width, 0, extent, World.Length); } -static void EnvRenderer_UpdateBorderTextures(void) { - EnvRenderer_MakeBorderTex(&edges_tex, Env.EdgeBlock); - EnvRenderer_MakeBorderTex(&sides_tex, Env.SidesBlock); +static void UpdateBorderTextures(void) { + MakeBorderTex(&edges_tex, Env.EdgeBlock); + MakeBorderTex(&sides_tex, Env.SidesBlock); } #define Borders_HorOffset(block) (Blocks.RenderMinBB[block].X - Blocks.MinBB[block].X) #define Borders_YOffset(block) (Blocks.RenderMinBB[block].Y - Blocks.MinBB[block].Y) -static void EnvRenderer_DrawBorderX(int x, int z1, int z2, int y1, int y2, PackedCol col, VertexP3fT2fC4b** vertices) { +static void DrawBorderX(int x, int z1, int z2, int y1, int y2, PackedCol col, VertexP3fT2fC4b** vertices) { int endZ = z2, endY = y2, startY = y1, axisSize = EnvRenderer_AxisSize(); float u2, v2; VertexP3fT2fC4b* v = *vertices; @@ -629,7 +627,7 @@ static void EnvRenderer_DrawBorderX(int x, int z1, int z2, int y1, int y2, Packe *vertices = v; } -static void EnvRenderer_DrawBorderZ(int z, int x1, int x2, int y1, int y2, PackedCol col, VertexP3fT2fC4b** vertices) { +static void DrawBorderZ(int z, int x1, int x2, int y1, int y2, PackedCol col, VertexP3fT2fC4b** vertices) { int endX = x2, endY = y2, startY = y1, axisSize = EnvRenderer_AxisSize(); float u2, v2; VertexP3fT2fC4b* v = *vertices; @@ -652,7 +650,7 @@ static void EnvRenderer_DrawBorderZ(int z, int x1, int x2, int y1, int y2, Packe *vertices = v; } -static void EnvRenderer_DrawBorderY(int x1, int z1, int x2, int z2, float y, PackedCol col, float offset, float yOffset, VertexP3fT2fC4b** vertices) { +static void DrawBorderY(int x1, int z1, int x2, int z2, float y, PackedCol col, float offset, float yOffset, VertexP3fT2fC4b** vertices) { int endX = x2, endZ = z2, startZ = z1, axisSize = EnvRenderer_AxisSize(); float u2, v2; VertexP3fT2fC4b* v = *vertices; @@ -676,7 +674,7 @@ static void EnvRenderer_DrawBorderY(int x1, int z1, int x2, int z2, float y, Pac *vertices = v; } -static void EnvRenderer_UpdateMapSides(void) { +static void UpdateMapSides(void) { Rect2D rects[4], r; BlockID block; PackedCol col, white = PACKEDCOL_WHITE; @@ -692,18 +690,18 @@ static void EnvRenderer_UpdateMapSides(void) { block = Env.SidesBlock; if (Blocks.Draw[block] == DRAW_GAS) return; - EnvRenderer_CalcBorderRects(rects); + CalcBorderRects(rects); sides_vertices = 0; for (i = 0; i < 4; i++) { r = rects[i]; - sides_vertices += EnvRenderer_Vertices(r.Width, r.Height); /* YQuads outside */ + sides_vertices += CalcNumVertices(r.Width, r.Height); /* YQuads outside */ } y = Env_SidesHeight; - sides_vertices += EnvRenderer_Vertices(World.Width, World.Length); /* YQuads beneath map */ - sides_vertices += 2 * EnvRenderer_Vertices(World.Width, Math_AbsI(y)); /* ZQuads */ - sides_vertices += 2 * EnvRenderer_Vertices(World.Length, Math_AbsI(y)); /* XQuads */ + sides_vertices += CalcNumVertices(World.Width, World.Length); /* YQuads beneath map */ + sides_vertices += 2 * CalcNumVertices(World.Width, Math_AbsI(y)); /* ZQuads */ + sides_vertices += 2 * CalcNumVertices(World.Length, Math_AbsI(y)); /* XQuads */ ptr = v; if (sides_vertices > ENV_SMALL_VERTICES) { @@ -717,7 +715,7 @@ static void EnvRenderer_UpdateMapSides(void) { for (i = 0; i < 4; i++) { r = rects[i]; - EnvRenderer_DrawBorderY(r.X, r.Y, r.X + r.Width, r.Y + r.Height, (float)y, col, + DrawBorderY(r.X, r.Y, r.X + r.Width, r.Y + r.Height, (float)y, col, 0, Borders_YOffset(block), &cur); } @@ -725,17 +723,17 @@ static void EnvRenderer_UpdateMapSides(void) { y1 = 0; y2 = y; if (y < 0) { y1 = y; y2 = 0; } - EnvRenderer_DrawBorderY(0, 0, World.Width, World.Length, 0, col, 0, 0, &cur); - EnvRenderer_DrawBorderZ(0, 0, World.Width, y1, y2, col, &cur); - EnvRenderer_DrawBorderZ(World.Length, 0, World.Width, y1, y2, col, &cur); - EnvRenderer_DrawBorderX(0, 0, World.Length, y1, y2, col, &cur); - EnvRenderer_DrawBorderX(World.Width, 0, World.Length, y1, y2, col, &cur); + DrawBorderY(0, 0, World.Width, World.Length, 0, col, 0, 0, &cur); + DrawBorderZ(0, 0, World.Width, y1, y2, col, &cur); + DrawBorderZ(World.Length, 0, World.Width, y1, y2, col, &cur); + DrawBorderX(0, 0, World.Length, y1, y2, col, &cur); + DrawBorderX(World.Width, 0, World.Length, y1, y2, col, &cur); sides_vb = Gfx_CreateVb(ptr, VERTEX_FORMAT_P3FT2FC4B, sides_vertices); if (sides_vertices > ENV_SMALL_VERTICES) Mem_Free(ptr); } -static void EnvRenderer_UpdateMapEdges(void) { +static void UpdateMapEdges(void) { Rect2D rects[4], r; BlockID block; PackedCol col, white = PACKEDCOL_WHITE; @@ -751,12 +749,12 @@ static void EnvRenderer_UpdateMapEdges(void) { block = Env.EdgeBlock; if (Blocks.Draw[block] == DRAW_GAS) return; - EnvRenderer_CalcBorderRects(rects); + CalcBorderRects(rects); edges_vertices = 0; for (i = 0; i < 4; i++) { r = rects[i]; - edges_vertices += EnvRenderer_Vertices(r.Width, r.Height); /* YPlanes outside */ + edges_vertices += CalcNumVertices(r.Width, r.Height); /* YPlanes outside */ } ptr = v; @@ -772,7 +770,7 @@ static void EnvRenderer_UpdateMapEdges(void) { y = (float)Env.EdgeHeight; for (i = 0; i < 4; i++) { r = rects[i]; - EnvRenderer_DrawBorderY(r.X, r.Y, r.X + r.Width, r.Y + r.Height, y, col, + DrawBorderY(r.X, r.Y, r.X + r.Width, r.Y + r.Height, y, col, Borders_HorOffset(block), Borders_YOffset(block), &cur); } @@ -801,18 +799,18 @@ static void OnContextLost(void* obj) { static void UpdateAll(void) { DeleteVbs(); - EnvRenderer_UpdateMapSides(); - EnvRenderer_UpdateMapEdges(); - EnvRenderer_UpdateClouds(); - EnvRenderer_UpdateSky(); - EnvRenderer_UpdateSkybox(); + UpdateMapSides(); + UpdateMapEdges(); + UpdateClouds(); + UpdateSky(); + UpdateSkybox(); EnvRenderer_UpdateFog(); if (Gfx.LostContext) return; /* TODO: Don't allocate unless used? */ weather_vb = Gfx_CreateDynamicVb(VERTEX_FORMAT_P3FT2FC4B, WEATHER_VERTS_COUNT); /* TODO: Don't need to do this on every new map */ - EnvRenderer_UpdateBorderTextures(); + UpdateBorderTextures(); } static void OnContextRecreated(void* obj) { @@ -852,37 +850,34 @@ static void OnTexturePackChanged(void* obj) { /* TODO: Find better way, really should delete them all here */ Gfx_DeleteTexture(&skybox_tex); } -static void OnTerrainAtlasChanged(void* obj) { - EnvRenderer_UpdateBorderTextures(); -} - +static void OnTerrainAtlasChanged(void* obj) { UpdateBorderTextures(); } static void OnViewDistanceChanged(void* obj) { UpdateAll(); } static void OnEnvVariableChanged(void* obj, int envVar) { if (envVar == ENV_VAR_EDGE_BLOCK) { - EnvRenderer_MakeBorderTex(&edges_tex, Env.EdgeBlock); - EnvRenderer_UpdateMapEdges(); + MakeBorderTex(&edges_tex, Env.EdgeBlock); + UpdateMapEdges(); } else if (envVar == ENV_VAR_SIDES_BLOCK) { - EnvRenderer_MakeBorderTex(&sides_tex, Env.SidesBlock); - EnvRenderer_UpdateMapSides(); + MakeBorderTex(&sides_tex, Env.SidesBlock); + UpdateMapSides(); } else if (envVar == ENV_VAR_EDGE_HEIGHT || envVar == ENV_VAR_SIDES_OFFSET) { - EnvRenderer_UpdateMapEdges(); - EnvRenderer_UpdateMapSides(); + UpdateMapEdges(); + UpdateMapSides(); } else if (envVar == ENV_VAR_SUN_COL) { - EnvRenderer_UpdateMapEdges(); + UpdateMapEdges(); } else if (envVar == ENV_VAR_SHADOW_COL) { - EnvRenderer_UpdateMapSides(); + UpdateMapSides(); } else if (envVar == ENV_VAR_SKY_COL) { - EnvRenderer_UpdateSky(); + UpdateSky(); } else if (envVar == ENV_VAR_FOG_COL) { EnvRenderer_UpdateFog(); } else if (envVar == ENV_VAR_CLOUDS_COL) { - EnvRenderer_UpdateClouds(); + UpdateClouds(); } else if (envVar == ENV_VAR_CLOUDS_HEIGHT) { - EnvRenderer_UpdateSky(); - EnvRenderer_UpdateClouds(); + UpdateSky(); + UpdateClouds(); } else if (envVar == ENV_VAR_SKYBOX_COL) { - EnvRenderer_UpdateSkybox(); + UpdateSkybox(); } }