Skip to content

Commit

Permalink
[Skybox] Moon texture added.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Jul 14, 2020
1 parent 0c6e56d commit 2ea3d16
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
1 change: 1 addition & 0 deletions resources/config/textures.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<texture name="block_destroy" path="resources/textures/block_destroy.png" />
<texture name="font" path="resources/textures/font.png" />
<texture name="menu_background" path="resources/textures/menu_background.png" repeat="true" />
<texture name="moon_phases" path="resources/textures/moon_phases.png" />
<texture name="player" path="resources/textures/player.png" />
<texture name="sun" path="resources/textures/sun.png" />
<texture name="title_screen" path="resources/textures/title_screen.png" />
Expand Down
28 changes: 20 additions & 8 deletions source/client/graphics/CelestialObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*/
#include <gk/core/GameClock.hpp>
#include <gk/gl/GLCheck.hpp>
#include <gk/gl/Texture.hpp>
#include <gk/resource/ResourceHandler.hpp>

#include "CelestialObject.hpp"
Expand Down Expand Up @@ -62,14 +63,25 @@ void CelestialObject::updateVertexBuffer() const {
}

if (m_texture) {
vertices[0].texCoord[0] = 1.f;
vertices[0].texCoord[1] = 0.f;
vertices[1].texCoord[0] = 0.f;
vertices[1].texCoord[1] = 0.f;
vertices[2].texCoord[0] = 0.f;
vertices[2].texCoord[1] = 1.f;
vertices[3].texCoord[0] = 1.f;
vertices[3].texCoord[1] = 1.f;
gk::FloatRect texRect{0, 0, 1, 1};

if (m_phaseCount && m_phaseSize && m_currentPhase < m_phaseCount) {
u16 currentPhaseX = m_currentPhase % (m_texture->getSize().x / m_phaseSize);
u16 currentPhaseY = m_currentPhase / (m_texture->getSize().x / m_phaseSize);
texRect.x = currentPhaseX / float(m_texture->getSize().x);
texRect.y = currentPhaseY / float(m_texture->getSize().y);
texRect.sizeX = m_phaseSize / float(m_texture->getSize().x);
texRect.sizeY = m_phaseSize / float(m_texture->getSize().y);
}

vertices[0].texCoord[0] = texRect.x + texRect.sizeX;
vertices[0].texCoord[1] = texRect.y;
vertices[1].texCoord[0] = texRect.x;
vertices[1].texCoord[1] = texRect.y;
vertices[2].texCoord[0] = texRect.x;
vertices[2].texCoord[1] = texRect.y + texRect.sizeY;
vertices[3].texCoord[0] = texRect.x + texRect.sizeX;
vertices[3].texCoord[1] = texRect.y + texRect.sizeY;
}

gk::VertexBuffer::bind(&m_vbo);
Expand Down
6 changes: 6 additions & 0 deletions source/client/graphics/CelestialObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class CelestialObject : public gk::Drawable, public gk::Transformable {
void setColor(const gk::Color &color) { m_color = color; m_isUpdateNeeded = true; }
void setSize(float width, float height) { m_width = width; m_height = height; m_isUpdateNeeded = true; }
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; }

private:
void updateVertexBuffer() const;
Expand All @@ -58,6 +60,10 @@ class CelestialObject : public gk::Drawable, public gk::Transformable {
const gk::Texture *m_texture = nullptr;

mutable bool m_isUpdateNeeded = true;

u16 m_phaseCount = 0;
u16 m_phaseSize = 0;
u16 m_currentPhase = 0;
};

#endif // CELESTIALOBJECT_HPP_
11 changes: 7 additions & 4 deletions source/client/graphics/Skybox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@ Skybox::Skybox(gk::Camera &camera, ClientWorld &world) : m_camera(camera), m_wor
m_shader.linkProgram();

m_sun.setColor(gk::Color::Yellow);
m_sun.setSize(100, 100);
m_sun.setPosition(300, -m_sun.width() / 2, -m_sun.height() / 2);
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(20, 20);
m_moon.setPosition(-300, -m_moon.width() / 2, -m_moon.height() / 2);
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);
}

void Skybox::draw(gk::RenderTarget &target, gk::RenderStates states) const {
Expand Down

0 comments on commit 2ea3d16

Please sign in to comment.