[Skybox] Sun texture added.

This commit is contained in:
Quentin Bazin 2020-07-15 01:14:05 +02:00
parent a1ababe8a6
commit 5acb1bf437
9 changed files with 85 additions and 19 deletions

View File

@ -3,6 +3,7 @@
<texture name="font" path="resources/textures/font.png" />
<texture name="menu_background" path="resources/textures/menu_background.png" repeat="true" />
<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" />
<texture name="toasts" path="resources/textures/toasts.png" />
<texture name="widgets" path="resources/textures/widgets.png" />

View File

@ -6,15 +6,15 @@ varying vec2 v_texCoord;
uniform sampler2D u_tex;
uniform vec4 u_skyColor;
void main() {
vec4 color = v_color;
if (v_texCoord.x != -1 && v_texCoord.y != -1) {
if (v_texCoord.x > -0.99 && v_texCoord.y > -0.99) {
color = texture2D(u_tex, v_texCoord);
if (color.r == color.g && color.g == color.b) {
color *= v_color;
}
color += u_skyColor;
}
if (color.a < 0.3) discard;

BIN
resources/textures/sun.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 488 B

View File

@ -26,23 +26,32 @@
*/
#include <gk/core/GameClock.hpp>
#include <gk/gl/GLCheck.hpp>
#include <gk/resource/ResourceHandler.hpp>
#include "CelestialObject.hpp"
#include "Vertex.hpp"
CelestialObject::CelestialObject() {
updateVertexBuffer();
}
void CelestialObject::setTexture(const std::string &textureName) {
m_texture = &gk::ResourceHandler::getInstance().get<gk::Texture>(textureName);
m_isUpdateNeeded = true;
}
void CelestialObject::updateVertexBuffer() const {
float width = 20.f;
float height = 20.f;
if (!m_width || !m_height) {
gkError() << "Can't update vertex buffer for celestial object of size 0";
return;
}
Vertex vertices[4] = {
// Rectangle vertices
{{0, width, 0, -1}},
{{0, 0, 0, -1}},
{{0, 0, height, -1}},
{{0, width, height, -1}},
{{0, m_width, 0, -1}},
{{0, 0, 0, -1}},
{{0, 0, m_height, -1}},
{{0, m_width, m_height, -1}},
};
for (u8 i = 0 ; i < 4 ; ++i) {
@ -52,19 +61,35 @@ void CelestialObject::updateVertexBuffer() const {
vertices[i].color[3] = m_color.a;
}
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::VertexBuffer::bind(&m_vbo);
m_vbo.setData(sizeof(vertices), vertices, GL_STATIC_DRAW);
gk::VertexBuffer::bind(nullptr);
m_isUpdateNeeded = false;
}
void CelestialObject::draw(gk::RenderTarget &target, gk::RenderStates states) const {
if (m_isUpdateNeeded)
updateVertexBuffer();
states.transform.rotate(-fmod(gk::GameClock::getInstance().getTicks() * 1.f / 1000.f, 360), {0, 1, 0});
states.transform *= getTransform();
states.vertexAttributes = VertexAttribute::All;
// glCheck(glEnable(GL_CULL_FACE));
// glCheck(glEnable(GL_DEPTH_TEST));
if (m_texture)
states.texture = m_texture;
static const GLubyte indices[] = {
0, 1, 3,

View File

@ -36,7 +36,12 @@ class CelestialObject : public gk::Drawable, public gk::Transformable {
public:
CelestialObject();
void setColor(const gk::Color &color) { m_color = color; updateVertexBuffer(); }
float width() const { return m_width; }
float height() const { return m_height; }
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);
private:
void updateVertexBuffer() const;
@ -46,6 +51,13 @@ class CelestialObject : public gk::Drawable, public gk::Transformable {
gk::VertexBuffer m_vbo;
gk::Color m_color = gk::Color::White;
float m_width = 0.f;
float m_height = 0.f;
const gk::Texture *m_texture = nullptr;
mutable bool m_isUpdateNeeded = true;
};
#endif // CELESTIALOBJECT_HPP_

View File

@ -24,22 +24,45 @@
*
* =====================================================================================
*/
#include <gk/core/GameClock.hpp>
#include "ClientWorld.hpp"
#include "Sky.hpp"
#include "Skybox.hpp"
Skybox::Skybox(gk::Camera &camera) : m_camera(camera) {
Skybox::Skybox(gk::Camera &camera, ClientWorld &world) : m_camera(camera), m_world(world) {
m_shader.createProgram();
m_shader.addShader(GL_VERTEX_SHADER, "resources/shaders/skybox.v.glsl");
m_shader.addShader(GL_FRAGMENT_SHADER, "resources/shaders/skybox.f.glsl");
m_shader.linkProgram();
m_sun.setColor(gk::Color::Yellow);
m_sun.setPosition(300, -10, -10);
m_sun.setSize(100, 100);
m_sun.setPosition(300, -m_sun.width() / 2, -m_sun.height() / 2);
m_sun.setTexture("texture-sun");
m_moon.setColor(gk::Color{240, 240, 240});
m_moon.setPosition(-300, -10, -10);
m_moon.setSize(20, 20);
m_moon.setPosition(-300, -m_moon.width() / 2, -m_moon.height() / 2);
}
void Skybox::draw(gk::RenderTarget &target, gk::RenderStates states) const {
// FIXME: Duplicated in GameState
float time = std::fmod(gk::GameClock::getInstance().getTicks() * 1.f / 1000.f, 360.f) / 360.f;
if (m_world.sky()) {
const float pi = 3.1415927f;
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);
gk::Shader::bind(&m_shader);
m_shader.setUniform("u_skyColor", skyColor);
gk::Shader::bind(nullptr);
}
states.shader = &m_shader;
// Subtract the camera position - see comment in ClientWorld::draw()

View File

@ -32,14 +32,18 @@
#include "CelestialObject.hpp"
class ClientWorld;
class Skybox : public gk::Drawable, public gk::Transformable {
public:
Skybox(gk::Camera &camera);
Skybox(gk::Camera &camera, ClientWorld &world);
private:
void draw(gk::RenderTarget &target, gk::RenderStates states) const override;
gk::Camera &m_camera;
ClientWorld &m_world;
gk::Shader m_shader;
CelestialObject m_sun;

View File

@ -201,6 +201,7 @@ void GameState::onGuiScaleChanged(const GuiScaleChangedEvent &event) {
}
void GameState::draw(gk::RenderTarget &target, gk::RenderStates states) const {
// FIXME: Duplicated in Skybox
float time = std::fmod(gk::GameClock::getInstance().getTicks() * 1.f / 1000.f, 360.f) / 360.f;
if (m_world.sky()) {
const float pi = 3.1415927f;

View File

@ -101,7 +101,7 @@ class GameState : public gk::ApplicationState {
KeyboardHandler *m_keyboardHandler;
bool m_areModKeysLoaded = false;
Skybox m_skybox{m_camera};
Skybox m_skybox{m_camera, m_world};
};
#endif // GAMESTATE_HPP_