Skip to content

Commit

Permalink
Fix sky rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
uis246 committed Dec 24, 2020
1 parent 153a81f commit cec4498
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 59 deletions.
53 changes: 26 additions & 27 deletions cwd/assets/altcraft/shaders/frag/sky.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,40 @@ in vec3 pos;

uniform sampler2DArray textureAtlas;
uniform float DayTime;
uniform vec4 sunTexture;
uniform float sunTextureLayer;
uniform vec4 moonTexture;
uniform float moonTextureLayer;

//xy - sun
//zw - moon
uniform vec4 texturePositions;
uniform vec4 textureSizes;

uniform uint sunTextureLayer;
uniform uint moonTextureLayer;

const vec4 DaySkyColor = vec4(0.49,0.66,1, 1);

const vec3 SunPos = vec3(0,0.1,0.5);

const vec3 MoonPos = vec3(0,0.1,-0.5);

vec3 TransformTextureCoord(vec4 TextureAtlasCoords, vec2 UvCoords, float Layer) {
vec2 transformed = TextureAtlasCoords.xy + UvCoords * TextureAtlasCoords.zw;
return vec3(transformed, Layer);
}

vec4 Sun() {
vec3 sunDelta = (pos - SunPos)*3.0f;
float distanceToSun = length(sunDelta);
vec4 sunColor = texture(textureAtlas, TransformTextureCoord(sunTexture,(sunDelta.xy+0.5f), sunTextureLayer));
vec4 sun = mix(vec4(0,0,0,1),sunColor,clamp(1-distanceToSun*2.0f,0,1));
return sun;
}
void main() {
vec4 outputColor = mix(vec4(0.0f, 0.04f, 0.06f, 1.0f), DaySkyColor, DayTime);

vec4 Moon() {
vec3 moonDelta = (pos - MoonPos)*4.5f;
float distanceToMoon = length(moonDelta);
vec4 moonColor = texture(textureAtlas, TransformTextureCoord(moonTexture, (moonDelta.xy+0.5f), moonTextureLayer));
vec4 moon = mix(vec4(0,0,0,1),moonColor,clamp(1-distanceToMoon*2.0f,0,1));
return moon;
}
vec3 sunDelta = (pos - SunPos)*3.0f;
float distanceToSun = length(sunDelta);

void main() {
vec4 starColor = vec4(0.0f, 0.04f, 0.06f, 1.0f);
gl_FragColor = mix(starColor, DaySkyColor, DayTime);
gl_FragColor += Sun();
gl_FragColor += Moon();
vec3 moonDelta = (pos - MoonPos)*4.5f;
float distanceToMoon = length(moonDelta);

vec4 transformed = texturePositions + textureSizes * (vec4(sunDelta.xy, moonDelta.xy) + 0.5);

vec4 sunColor = texture(textureAtlas, vec3(transformed.xy, sunTextureLayer));
vec4 sun = mix(vec4(0,0,0,1),sunColor,clamp(1-distanceToSun*2.0f,0,1));
outputColor += sun;

vec4 moonColor = texture(textureAtlas, vec3(transformed.zw, moonTextureLayer));
vec4 moon = mix(vec4(0,0,0,1),moonColor,clamp(1-distanceToMoon*2.0f,0,1));
outputColor += moon;

gl_FragColor = outputColor;
}
4 changes: 2 additions & 2 deletions cwd/assets/altcraft/shaders/sky.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
"model",
"textureAtlas",
"DayTime",
"sunTexture",
"texturePositions",
"textureSizes",
"sunTextureLayer",
"moonTexture",
"moonTextureLayer"
]
}
41 changes: 18 additions & 23 deletions src/RendererSky.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,32 +105,27 @@ const GLfloat uv_coords[] = {
};

RendererSky::RendererSky() {
glGenBuffers(1, &VboVert);
glBindBuffer(GL_ARRAY_BUFFER, VboVert);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

glGenBuffers(1, &VboUv);
glBindBuffer(GL_ARRAY_BUFFER, VboUv);
glBufferData(GL_ARRAY_BUFFER, sizeof(uv_coords), uv_coords, GL_STATIC_DRAW);
glGenVertexArrays(1, &Vao);

glBindVertexArray(Vao);
{
glBindBuffer(GL_ARRAY_BUFFER, VboVert);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);

glBindBuffer(GL_ARRAY_BUFFER, VboUv);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(1);
}
glBindVertexArray(0);
glCheckError();
glGenVertexArrays(1, &Vao);
glGenBuffers(2, VBOs);

glBindVertexArray(Vao);
{
glBindBuffer(GL_ARRAY_BUFFER, VBOs[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);

glBindBuffer(GL_ARRAY_BUFFER, VBOs[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(uv_coords), uv_coords, GL_STATIC_DRAW);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(1);
}
glBindVertexArray(0);
glCheckError();
}

RendererSky::~RendererSky() {
glDeleteBuffers(1, &VboVert);
glDeleteBuffers(1, &VboUv);
glDeleteBuffers(2, VBOs);
glDeleteVertexArrays(1, &Vao);
//glCheckError();
}
Expand Down
2 changes: 1 addition & 1 deletion src/RendererSky.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class RenderState;

class RendererSky {
GLuint VboVert, VboUv, Vao;
GLuint VBOs[2], Vao;
public:
RendererSky();
~RendererSky();
Expand Down
11 changes: 5 additions & 6 deletions src/RendererWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ void RendererWorld::Render(RenderState & renderState) {
if (skyNode->type==AssetTreeNode::ASSET_SHADER)
skyShader = reinterpret_cast<AssetShader*>(skyNode->asset.get())->shader.get();
skyShader->Activate();
skyShader->SetUniform("projView", projection);
skyShader->SetUniform("projView", projView);
glm::mat4 model = glm::mat4(1.0);
model = glm::translate(model, player->pos.glm());
const float scale = 1000000.0f;
Expand All @@ -444,11 +444,10 @@ void RendererWorld::Render(RenderState & renderState) {
const float moonriseLength = moonriseMax - moonriseMin;

float mixLevel = 0;
float dayTime = GetGameState()->GetTimeStatus().interpolatedTimeOfDay;
long long dayTime = GetGameState()->GetTimeStatus().interpolatedTimeOfDay;
if (dayTime < 0)
dayTime *= -1;
while (dayTime > 24000)
dayTime -= 24000;
dayTime %= 24000;
if ((dayTime > 0 && dayTime < moonriseMin) || dayTime > sunriseMax) //day
mixLevel = 1.0;
if (dayTime > moonriseMax && dayTime < sunriseMin) //night
Expand Down Expand Up @@ -526,10 +525,10 @@ void RendererWorld::PrepareRender() {
sky = reinterpret_cast<AssetShader*>(skyNode->asset.get())->shader.get();
sky->Activate();
sky->SetUniform("textureAtlas", 0);
sky->SetUniform("sunTexture", glm::vec4(sunTexture.x, sunTexture.y, sunTexture.w, sunTexture.h));
sky->SetUniform("sunTextureLayer", (float)sunTexture.layer);
sky->SetUniform("moonTexture", glm::vec4(moonTexture.x, moonTexture.y, moonTexture.w, moonTexture.h));
sky->SetUniform("moonTextureLayer", (float)moonTexture.layer);
sky->SetUniform("texturePositions", glm::vec4(sunTexture.x, sunTexture.y, moonTexture.x, moonTexture.y));
sky->SetUniform("textureSizes", glm::vec4(sunTexture.w, sunTexture.h, moonTexture.w, moonTexture.h));
}

void RendererWorld::Update(double timeToUpdate) {
Expand Down

0 comments on commit cec4498

Please sign in to comment.