Skip to content

Commit

Permalink
[Skybox] Stars added.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Jul 15, 2020
1 parent d85f40a commit 9b74daa
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
5 changes: 4 additions & 1 deletion resources/shaders/skybox.f.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ varying vec2 v_texCoord;
uniform sampler2D u_tex;

uniform vec4 u_skyColor;
uniform vec4 u_starColor;

void main() {
vec4 color = v_color;

if (v_texCoord.x > -0.99 && v_texCoord.y > -0.99) {
color = texture2D(u_tex, v_texCoord);

color += u_skyColor;
}
else if (color.a == 0) {
color = u_starColor;
}

if (color.a < 0.3) discard;

Expand Down
2 changes: 1 addition & 1 deletion source/client/graphics/CelestialObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void CelestialObject::draw(gk::RenderTarget &target, gk::RenderStates states) co
if (m_isUpdateNeeded)
updateVertexBuffer();

states.transform.rotate(-fmod(gk::GameClock::getInstance().getTicks() * 1.f / 1000.f, 360), {0, 1, 0});
states.transform.rotate(-fmod((gk::GameClock::getInstance().getTicks() * 1.f / 1000.f + m_rotationOffset), 360), m_rotationAxis);
states.transform *= getTransform();

states.vertexAttributes = VertexAttribute::All;
Expand Down
5 changes: 5 additions & 0 deletions source/client/graphics/CelestialObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class CelestialObject : public gk::Drawable, public gk::Transformable {
void setTexture(const std::string &textureName);
void setPhaseCount(u16 phaseCount, u16 phaseSize) { m_phaseCount = phaseCount; m_phaseSize = phaseSize; m_isUpdateNeeded = true; }
void setCurrentPhase(u16 currentPhase) { m_currentPhase = currentPhase; m_isUpdateNeeded = true; }
void setRotationOffset(u16 rotationOffset) { m_rotationOffset = rotationOffset; }
void setRotationAxis(const gk::Vector3f &rotationAxis) { m_rotationAxis = rotationAxis; }

private:
void updateVertexBuffer() const;
Expand All @@ -64,6 +66,9 @@ class CelestialObject : public gk::Drawable, public gk::Transformable {
u16 m_phaseCount = 0;
u16 m_phaseSize = 0;
u16 m_currentPhase = 0;

u16 m_rotationOffset = 0;
gk::Vector3f m_rotationAxis{0, 1, 0};
};

#endif // CELESTIALOBJECT_HPP_
23 changes: 18 additions & 5 deletions source/client/graphics/Skybox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,24 @@ Skybox::Skybox(gk::Camera &camera, ClientWorld &world) : m_camera(camera), m_wor
m_shader.addShader(GL_FRAGMENT_SHADER, "resources/shaders/skybox.f.glsl");
m_shader.linkProgram();

m_sun.setColor(gk::Color::Yellow);
m_sun.setSize(200, 200);
m_sun.setPosition(500, -m_sun.width() / 2, -m_sun.height() / 2);
m_sun.setTexture("texture-sun");

m_moon.setColor(gk::Color{240, 240, 240});
m_moon.setSize(200, 200);
m_moon.setPosition(-500, -m_moon.width() / 2, -m_moon.height() / 2);
m_moon.setTexture("texture-moon_phases");
m_moon.setPhaseCount(8, 32);
m_moon.setCurrentPhase(0);

for (int i = 0 ; i < 1000 ; ++i) {
auto &star = m_stars.emplace_back();
star.setColor(gk::Color{0, 0, 0, 0});
star.setSize(5, 5);
star.setPosition(700 * ((rand() % 2) * 2 - 1), (rand() % 500) * 2 - 500, (rand() % 500) * 2 - 500);
star.setRotationOffset(rand() % 360);
star.setRotationAxis({rand() % 100 / 100.f, rand() % 100 / 100.f, rand() % 100 / 100.f});
}
}

void Skybox::draw(gk::RenderTarget &target, gk::RenderStates states) const {
Expand All @@ -57,12 +64,13 @@ void Skybox::draw(gk::RenderTarget &target, gk::RenderStates states) const {
float sunlight = std::clamp(0.5f + std::sin(2 * pi * time) * 2.0f, 0.0f, 1.0f);

gk::Color skyColor = m_world.sky()->color();
skyColor.r = std::clamp(sunlight - (1 - skyColor.r), 0.0f, skyColor.r);
skyColor.g = std::clamp(sunlight - (1 - skyColor.g), 0.0f, skyColor.g);
skyColor.b = std::clamp(sunlight - (1 - skyColor.b), 0.0f, skyColor.b);
skyColor.r = std::clamp(sunlight - (1.f - skyColor.r), 0.0f, skyColor.r);
skyColor.g = std::clamp(sunlight - (1.f - skyColor.g), 0.0f, skyColor.g);
skyColor.b = std::clamp(sunlight - (1.f - skyColor.b), 0.0f, skyColor.b);

gk::Shader::bind(&m_shader);
m_shader.setUniform("u_skyColor", skyColor);
m_shader.setUniform("u_starColor", m_world.sky()->color());
gk::Shader::bind(nullptr);
}

Expand All @@ -74,5 +82,10 @@ void Skybox::draw(gk::RenderTarget &target, gk::RenderStates states) const {

target.draw(m_sun, states);
target.draw(m_moon, states);

glDisable(GL_CULL_FACE);

for (auto &it : m_stars)
target.draw(it, states);
}

2 changes: 2 additions & 0 deletions source/client/graphics/Skybox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class Skybox : public gk::Drawable, public gk::Transformable {

CelestialObject m_sun;
CelestialObject m_moon;

std::vector<CelestialObject> m_stars;
};

#endif // SKYBOX_HPP_

0 comments on commit 9b74daa

Please sign in to comment.