Changeset View
Changeset View
Standalone View
Standalone View
source/graphics/HeightMipmap.cpp
/* Copyright (C) 2012 Wildfire Games. | /* Copyright (C) 2019 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 0 A.D. is free software: you can redistribute it and/or modify | ||||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
▲ Show 20 Lines • Show All 48 Lines • ▼ Show 20 Lines | |||||
{ | { | ||||
ENSURE(ptr != 0); | ENSURE(ptr != 0); | ||||
size_t mapSize = m_MapSize; | size_t mapSize = m_MapSize; | ||||
for (size_t i = 0; i < m_Mipmap.size(); ++i) | for (size_t i = 0; i < m_Mipmap.size(); ++i) | ||||
{ | { | ||||
// update window | // update window | ||||
left = clamp<size_t>((size_t)floorf((float)left / mapSize * m_Mipmap[i].m_MapSize), 0, m_Mipmap[i].m_MapSize - 1); | left = Clamp<size_t>(floorf(static_cast<float>(left) / mapSize * m_Mipmap[i].m_MapSize), 0, m_Mipmap[i].m_MapSize - 1); | ||||
bottom = clamp<size_t>((size_t)floorf((float)bottom / mapSize * m_Mipmap[i].m_MapSize), 0, m_Mipmap[i].m_MapSize - 1); | bottom = Clamp<size_t>(floorf(static_cast<float>(bottom) / mapSize * m_Mipmap[i].m_MapSize), 0, m_Mipmap[i].m_MapSize - 1); | ||||
Stan: static_cast | |||||
Not Done Inline ActionsSame concern as above. Stan: Same concern as above. | |||||
right = clamp<size_t>((size_t)ceilf((float)right / mapSize * m_Mipmap[i].m_MapSize), 0, m_Mipmap[i].m_MapSize); | right = Clamp<size_t>(ceilf(static_cast<float>(right) / mapSize * m_Mipmap[i].m_MapSize), 0, m_Mipmap[i].m_MapSize); | ||||
top = clamp<size_t>((size_t)ceilf((float)top / mapSize * m_Mipmap[i].m_MapSize), 0, m_Mipmap[i].m_MapSize); | top = Clamp<size_t>(ceilf(static_cast<float>(top) / mapSize * m_Mipmap[i].m_MapSize), 0, m_Mipmap[i].m_MapSize); | ||||
// TODO: should verify that the bounds calculations are actually correct | // TODO: should verify that the bounds calculations are actually correct | ||||
// update mipmap | // update mipmap | ||||
BilinearUpdate(m_Mipmap[i], mapSize, ptr, left, bottom, right, top); | BilinearUpdate(m_Mipmap[i], mapSize, ptr, left, bottom, right, top); | ||||
mapSize = m_Mipmap[i].m_MapSize; | mapSize = m_Mipmap[i].m_MapSize; | ||||
ptr = m_Mipmap[i].m_Heightmap; | ptr = m_Mipmap[i].m_Heightmap; | ||||
Show All 20 Lines | |||||
} | } | ||||
float CHeightMipmap::GetTrilinearGroundLevel(float x, float z, float radius) const | float CHeightMipmap::GetTrilinearGroundLevel(float x, float z, float radius) const | ||||
{ | { | ||||
float y; | float y; | ||||
if (radius <= 0.0f) // avoid logf of non-positive value | if (radius <= 0.0f) // avoid logf of non-positive value | ||||
y = 0.0f; | y = 0.0f; | ||||
else | else | ||||
y = clamp<float>(logf(radius * m_Mipmap[0].m_MapSize) / logf(2), 0, m_Mipmap.size()); | y = Clamp<float>(logf(radius * m_Mipmap[0].m_MapSize) / logf(2), 0, m_Mipmap.size()); | ||||
StanUnsubmitted Done Inline Actions0.0f Stan: 0.0f | |||||
const size_t iy = (size_t)clamp<ssize_t>((ssize_t)floorf(y), 0, m_Mipmap.size() - 2); | const size_t iy = static_cast<size_t>(Clamp<ssize_t>(floorf(y), 0, m_Mipmap.size() - 2)); | ||||
Not Done Inline Actionsstatic_cast Stan: static_cast | |||||
Not Done Inline ActionsStill missing :P Stan: Still missing :P | |||||
const float fy = y - iy; | const float fy = y - iy; | ||||
const float h0 = BilinearFilter(m_Mipmap[iy], x, z); | const float h0 = BilinearFilter(m_Mipmap[iy], x, z); | ||||
const float h1 = BilinearFilter(m_Mipmap[iy + 1], x, z); | const float h1 = BilinearFilter(m_Mipmap[iy + 1], x, z); | ||||
return (1 - fy) * h0 + fy * h1; | return (1 - fy) * h0 + fy * h1; | ||||
} | } | ||||
float CHeightMipmap::BilinearFilter(const SMipmap &mipmap, float x, float z) const | float CHeightMipmap::BilinearFilter(const SMipmap &mipmap, float x, float z) const | ||||
{ | { | ||||
x *= mipmap.m_MapSize; | x *= mipmap.m_MapSize; | ||||
z *= mipmap.m_MapSize; | z *= mipmap.m_MapSize; | ||||
const size_t xi = (size_t)clamp<ssize_t>((ssize_t)floor(x), 0, mipmap.m_MapSize - 2); | const size_t xi = static_cast<size_t>(Clamp<ssize_t>(floor(x), 0, mipmap.m_MapSize - 2)); | ||||
const size_t zi = (size_t)clamp<ssize_t>((ssize_t)floor(z), 0, mipmap.m_MapSize - 2); | const size_t zi = static_cast<size_t>(Clamp<ssize_t>(floor(z), 0, mipmap.m_MapSize - 2)); | ||||
Not Done Inline Actionsstatic_cast Stan: static_cast | |||||
Not Done Inline Actionsstatic_cast Stan: static_cast | |||||
Not Done Inline ActionsHere as well :) Stan: Here as well :) | |||||
const float xf = clamp<float>(x-xi, 0.0f, 1.0f); | const float xf = Clamp<float>(x-xi, 0.0f, 1.0f); | ||||
const float zf = clamp<float>(z-zi, 0.0f, 1.0f); | const float zf = Clamp<float>(z-zi, 0.0f, 1.0f); | ||||
const float h00 = mipmap.m_Heightmap[zi*mipmap.m_MapSize + xi]; | const float h00 = mipmap.m_Heightmap[zi*mipmap.m_MapSize + xi]; | ||||
const float h01 = mipmap.m_Heightmap[(zi+1)*mipmap.m_MapSize + xi]; | const float h01 = mipmap.m_Heightmap[(zi+1)*mipmap.m_MapSize + xi]; | ||||
const float h10 = mipmap.m_Heightmap[zi*mipmap.m_MapSize + (xi+1)]; | const float h10 = mipmap.m_Heightmap[zi*mipmap.m_MapSize + (xi+1)]; | ||||
const float h11 = mipmap.m_Heightmap[(zi+1)*mipmap.m_MapSize + (xi+1)]; | const float h11 = mipmap.m_Heightmap[(zi+1)*mipmap.m_MapSize + (xi+1)]; | ||||
return | return | ||||
(1.f - xf) * (1.f - zf) * h00 + | (1.f - xf) * (1.f - zf) * h00 + | ||||
▲ Show 20 Lines • Show All 56 Lines • ▼ Show 20 Lines | void CHeightMipmap::BilinearUpdate(SMipmap &out_mipmap, size_t mapSize, const u16* ptr, size_t left, size_t bottom, size_t right, size_t top) | ||||
{ | { | ||||
for (size_t dstZ = bottom; dstZ < top; ++dstZ) | for (size_t dstZ = bottom; dstZ < top; ++dstZ) | ||||
{ | { | ||||
for (size_t dstX = left; dstX < right; ++dstX) | for (size_t dstX = left; dstX < right; ++dstX) | ||||
{ | { | ||||
const float x = ((float)dstX / (float)out_mipmap.m_MapSize) * mapSize; | const float x = ((float)dstX / (float)out_mipmap.m_MapSize) * mapSize; | ||||
const float z = ((float)dstZ / (float)out_mipmap.m_MapSize) * mapSize; | const float z = ((float)dstZ / (float)out_mipmap.m_MapSize) * mapSize; | ||||
const size_t srcX = clamp<size_t>((size_t)x, 0, mapSize - 2); | const size_t srcX = Clamp<size_t>((size_t)x, 0, mapSize - 2); | ||||
const size_t srcZ = clamp<size_t>((size_t)z, 0, mapSize - 2); | const size_t srcZ = Clamp<size_t>((size_t)z, 0, mapSize - 2); | ||||
const float fx = clamp<float>(x - srcX, 0.0f, 1.0f); | const float fx = Clamp<float>(x - srcX, 0.0f, 1.0f); | ||||
const float fz = clamp<float>(z - srcZ, 0.0f, 1.0f); | const float fz = Clamp<float>(z - srcZ, 0.0f, 1.0f); | ||||
const float h00 = ptr[srcX + 0 + srcZ * mapSize]; | const float h00 = ptr[srcX + 0 + srcZ * mapSize]; | ||||
const float h10 = ptr[srcX + 1 + srcZ * mapSize]; | const float h10 = ptr[srcX + 1 + srcZ * mapSize]; | ||||
const float h01 = ptr[srcX + 0 + (srcZ + 1) * mapSize]; | const float h01 = ptr[srcX + 0 + (srcZ + 1) * mapSize]; | ||||
const float h11 = ptr[srcX + 1 + (srcZ + 1) * mapSize]; | const float h11 = ptr[srcX + 1 + (srcZ + 1) * mapSize]; | ||||
out_mipmap.m_Heightmap[dstX + dstZ * out_mipmap.m_MapSize] = (u16) | out_mipmap.m_Heightmap[dstX + dstZ * out_mipmap.m_MapSize] = (u16) | ||||
((1.f - fx) * (1.f - fz) * h00 + | ((1.f - fx) * (1.f - fz) * h00 + | ||||
▲ Show 20 Lines • Show All 46 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator
static_cast