Skip to content

Commit

Permalink
keep manual LOD gearing
Browse files Browse the repository at this point in the history
  • Loading branch information
rt committed Apr 12, 2019
1 parent d50be94 commit 688119f
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 33 deletions.
4 changes: 2 additions & 2 deletions cont/base/springcontent/shaders/GLSL/SMFFragProg4.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ uniform mat4 viewMat;

uniform vec4 fwdDynLights[MAX_LIGHT_UNIFORM_VECS];

uniform ivec3 texSquare;
uniform ivec4 texSquare;



Expand Down Expand Up @@ -390,7 +390,7 @@ void main() {
float cosAngleSpecular = clamp(dot(normalize(halfDir), normalVec), 0.001, 1.0);
#endif

vec4 diffuseColor = texture(diffuseTex, vec3(diffTexCoords, texSquare.z));
vec4 diffuseColor = textureLod(diffuseTex, vec3(diffTexCoords, texSquare.z), texSquare.w);
vec4 specularColor = vec4(0.0, 0.0, 0.0, 1.0);
vec4 emissionColor = vec4(0.0, 0.0, 0.0, 0.0);

Expand Down
2 changes: 1 addition & 1 deletion cont/base/springcontent/shaders/GLSL/SMFVertProg4.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#define SMF_TEXSQR_SIZE 1024.0
#define SMF_DETAILTEX_RES 0.02

uniform ivec3 texSquare; // x, y, y * X + x (index into diffuseTex)
uniform ivec4 texSquare; // x, y, y * X + x (index into diffuseTex), mip
uniform vec3 cameraPos;
uniform vec4 lightDir; // mapInfo->light.sunDir
uniform vec3 fogParams; // .x := start, .y := end, .z := viewrange
Expand Down
2 changes: 2 additions & 0 deletions rts/Map/BaseGroundTextures.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class CBaseGroundTextures {

virtual void BindSquareTextureArray() const {}
virtual void UnBindSquareTextureArray() const {}

virtual unsigned int GetSquareMipLevel(unsigned int i) const { return 0; }
};

#endif
7 changes: 5 additions & 2 deletions rts/Map/SMF/SMFGroundDrawer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -528,15 +528,18 @@ void CSMFGroundDrawer::SetLuaShader(const LuaMapShaderData* luaMapShaderData)

void CSMFGroundDrawer::SetupBigSquare(const int bigSquareX, const int bigSquareY)
{
const int sqrIdx = bigSquareY * smfMap->numBigTexX + bigSquareX;
const int sqrMip = groundTextures->GetSquareMipLevel(sqrIdx);

groundTextures->DrawUpdateSquare(bigSquareX, bigSquareY);
smfRenderStates[RENDER_STATE_SEL]->SetSquareTexGen(bigSquareX, bigSquareY, smfMap->numBigTexX);
smfRenderStates[RENDER_STATE_SEL]->SetSquareTexGen(bigSquareX, bigSquareY, smfMap->numBigTexX, sqrMip);

Shader::IProgramObject& ipo = borderShader;

if (!ipo.IsBound())
return;

ipo.SetUniform("u_diffuse_tex_sqr", bigSquareX, bigSquareY, bigSquareY * smfMap->numBigTexX + bigSquareX);
ipo.SetUniform("u_diffuse_tex_sqr", bigSquareX, bigSquareY, sqrIdx);
}


Expand Down
38 changes: 15 additions & 23 deletions rts/Map/SMF/SMFGroundTextures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,24 +321,23 @@ inline bool CSMFGroundTextures::TexSquareInView(int btx, int bty) const
const int x = btx * smfMap->bigTexSize + (smfMap->bigTexSize >> 1);
const int y = bty * smfMap->bigTexSize + (smfMap->bigTexSize >> 1);
const int idx = (y >> 3) * smfMap->heightMapSizeX + (x >> 3);
const float3 bigTexSquarePos(x, hm[idx], y);

return (cam->InView(bigTexSquarePos, bigTexSquareRadius));
return (cam->InView({x * 1.0f, hm[idx], y * 1.0f}, bigTexSquareRadius));
}

void CSMFGroundTextures::DrawUpdate()
{
const CCamera* cam = CCameraHandler::GetActiveCamera();

const float3& camPos = cam->GetPos();

// screen-diagonal number of pixels
const float vsxSq = globalRendering->viewSizeX * globalRendering->viewSizeX;
const float vsySq = globalRendering->viewSizeY * globalRendering->viewSizeY;
const float vdiag = fastmath::apxsqrt(vsxSq + vsySq);

BindSquareTextureArray();

for (int y = 0; y < smfMap->numBigTexY; ++y) {
float dz = cam->GetPos().z - (y * smfMap->bigSquareSize * SQUARE_SIZE);
float dz = camPos.z - (y * smfMap->bigSquareSize * SQUARE_SIZE);
dz -= (SQUARE_SIZE << 6);
dz = std::max(0.0f, float(math::fabs(dz) - (SQUARE_SIZE << 6)));

Expand All @@ -353,19 +352,20 @@ void CSMFGroundTextures::DrawUpdate()
if ((square->GetMipLevel() < 3) && ((globalRendering->drawFrame - square->GetDrawFrame()) > 120)) {
// `unload` texture (load lowest mip-map) if
// the square wasn't visible for 120 vframes
LoadSquareTexture(x, y, 3);
square->SetMipLevel(3);
}

continue;
}

float dx = cam->GetPos().x - (x * smfMap->bigSquareSize * SQUARE_SIZE);
float dx = camPos.x - (x * smfMap->bigSquareSize * SQUARE_SIZE);
dx -= (SQUARE_SIZE << 6);
dx = std::max(0.0f, float(math::fabs(dx) - (SQUARE_SIZE << 6)));

const float hAvg =
(heightMaxima[y * smfMap->numBigTexX + x] +
heightMinima[y * smfMap->numBigTexX + x]) / 2.0f;
const float dy = std::max(cam->GetPos().y - hAvg, 0.0f);
const float avgHeight = (heightMaxima[y * smfMap->numBigTexX + x] + heightMinima[y * smfMap->numBigTexX + x]) * 0.5f;
const float difHeight = heightMaxima[y * smfMap->numBigTexX + x] - heightMinima[y * smfMap->numBigTexX + x];

const float dy = std::max(camPos.y - avgHeight, 0.0f);
const float dist = fastmath::apxsqrt(dx * dx + dy * dy + dz * dz);

// we work under the following assumptions:
Expand All @@ -377,18 +377,15 @@ void CSMFGroundTextures::DrawUpdate()
// translates to a diameter of =~ sqrt2 * bigTexSize =~ 1400 pixels
//
// half (vertical) FOV is 45 degs, for default and most other camera modes
int wantedLevel = 0;
float heightDiff =
heightMaxima[y * smfMap->numBigTexX + x] -
heightMinima[y * smfMap->numBigTexX + x];
int screenPixels = smfMap->bigTexSize;
int wantedLevel = 0;

if (dist > 0.0f) {
if (heightDiff > float(smfMap->bigTexSize)) {
if (difHeight > float(smfMap->bigTexSize)) {
// this means the heightmap chunk is taller than it is wide,
// so we use the tallness metric instead for calculating its
// on-screen size in pixels
screenPixels = (heightDiff) * (vdiag * 0.5f) / dist;
screenPixels = (difHeight) * (vdiag * 0.5f) / dist;
} else {
screenPixels = smfMap->bigTexSize * (vdiag * 0.5f) / dist;
}
Expand All @@ -408,14 +405,9 @@ void CSMFGroundTextures::DrawUpdate()
if (stretchFactors[y * smfMap->numBigTexX + x] > 16000 && wantedLevel > 0)
wantedLevel--;

if (square->GetMipLevel() == wantedLevel)
continue;

LoadSquareTexture(x, y, wantedLevel);
square->SetMipLevel(wantedLevel);
}
}

UnBindSquareTextureArray();
}


Expand Down
2 changes: 2 additions & 0 deletions rts/Map/SMF/SMFGroundTextures.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class CSMFGroundTextures: public CBaseGroundTextures
void BindSquareTextureArray() const override;
void UnBindSquareTextureArray() const override;

unsigned int GetSquareMipLevel(unsigned int i) const override { return squares[i].GetMipLevel(); }

protected:
void LoadTiles(CSMFMapFile& file);
void LoadSquareTextures(const int minLevel, const int maxLevel);
Expand Down
4 changes: 2 additions & 2 deletions rts/Map/SMF/SMFRenderState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,10 @@ void SMFRenderStateGLSL::Disable(const CSMFGroundDrawer*, const DrawPass::e&) {



void SMFRenderStateGLSL::SetSquareTexGen(const int sqx, const int sqy, const int nsx) const {
void SMFRenderStateGLSL::SetSquareTexGen(const int sqx, const int sqy, const int nsx, const int mip) const {
// needs to be set even for Lua shaders, is unknowable otherwise
// (works because SMFGroundDrawer::SetupBigSquare always calls us)
glslShaders[GLSL_SHADER_CURRENT]->SetUniform("texSquare", sqx, sqy, sqy * nsx + sqx);
glslShaders[GLSL_SHADER_CURRENT]->SetUniform("texSquare", sqx, sqy, sqy * nsx + sqx, mip);
}


Expand Down
6 changes: 3 additions & 3 deletions rts/Map/SMF/SMFRenderState.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ struct ISMFRenderState {
virtual void Enable(const CSMFGroundDrawer* smfGroundDrawer, const DrawPass::e& drawPass) = 0;
virtual void Disable(const CSMFGroundDrawer* smfGroundDrawer, const DrawPass::e& drawPass) = 0;

virtual void SetSquareTexGen(const int sqx, const int sqy, const int nsx) const = 0;
virtual void SetSquareTexGen(const int sqx, const int sqy, const int nsx, const int mip) const = 0;
virtual void SetCurrentShader(const DrawPass::e& drawPass) = 0;
virtual void SetSkyLight(const ISkyLight* skyLight) const = 0;
virtual void SetAlphaTest(const float4& params) const = 0;
Expand All @@ -70,7 +70,7 @@ struct SMFRenderStateNOP: public ISMFRenderState {
void Enable(const CSMFGroundDrawer* smfGroundDrawer, const DrawPass::e& drawPass) override {}
void Disable(const CSMFGroundDrawer* smfGroundDrawer, const DrawPass::e& drawPass) override {}

void SetSquareTexGen(const int sqx, const int sqy, const int nsx) const override {}
void SetSquareTexGen(const int sqx, const int sqy, const int nsx, const int mip) const override {}
void SetCurrentShader(const DrawPass::e& drawPass) override {}
void SetSkyLight(const ISkyLight* skyLight) const override {}
void SetAlphaTest(const float4& params) const override {}
Expand All @@ -97,7 +97,7 @@ struct SMFRenderStateGLSL: public ISMFRenderState {
void Enable(const CSMFGroundDrawer* smfGroundDrawer, const DrawPass::e& drawPass) override;
void Disable(const CSMFGroundDrawer* smfGroundDrawer, const DrawPass::e& drawPass) override;

void SetSquareTexGen(const int sqx, const int sqy, const int nsx) const override;
void SetSquareTexGen(const int sqx, const int sqy, const int nsx, const int mip) const override;
void SetCurrentShader(const DrawPass::e& drawPass) override;
void SetSkyLight(const ISkyLight* skyLight) const override;
void SetAlphaTest(const float4& params) const override;
Expand Down

0 comments on commit 688119f

Please sign in to comment.