Begin implementing simple skybox

This commit is contained in:
2024-06-05 19:02:14 +02:00
parent 794abef83a
commit ac52f99e14
15 changed files with 291 additions and 1 deletions

View File

@@ -176,6 +176,7 @@ if(BUILD_CLIENT)
src/game/world/local_player.cpp
src/game/world/physic_manager.cpp
src/game/world/player_manager.cpp
src/game/world/skybox.cpp
)
if(RENDERER_GL)
list(APPEND CLIENT_SOURCES

View File

@@ -164,7 +164,8 @@ ifeq ($(BUILD_CLIENT),1)
src/game/world/client_chunk.cpp \
src/game/world/local_player.cpp \
src/game/world/physic_manager.cpp \
src/game/world/player_manager.cpp
src/game/world/player_manager.cpp \
src/game/world/skybox.cpp
ifeq ($(RENDERER_GL),1)
SOURCES+=src/game/renderer/gl/gl_gui_renderer.cpp \

View File

@@ -0,0 +1,9 @@
#version 330 core
out vec4 out_frag_color;
in vec4 out_color;
void main() {
out_frag_color = out_color;
}

View File

@@ -0,0 +1,18 @@
#version 330 core
layout (location = 0) in vec3 in_pos;
layout (location = 3) in vec4 in_color;
out vec4 out_color;
uniform mat4 uniform_view;
uniform mat4 uniform_projection;
void main() {
mat4 view_copy = uniform_view;
view_copy[3] = vec4(0, 0, 0, 1);
view_copy[0][3] = 0;
view_copy[1][3] = 0;
view_copy[2][3] = 0;
gl_Position = uniform_projection * view_copy * vec4(in_pos, 1.0f);
out_color = in_color;
}

View File

@@ -0,0 +1,7 @@
#version 330 core
varying vec4 out_color;
void main() {
gl_FragColor = out_color;
}

View File

@@ -0,0 +1,18 @@
#version 110
attribute vec3 in_pos;
attribute vec4 in_color;
varying vec4 out_color;
uniform mat4 uniform_view;
uniform mat4 uniform_projection;
void main() {
mat4 view_copy = uniform_view;
view_copy[3] = vec4(0, 0, 0, 1);
view_copy[0][3] = 0;
view_copy[1][3] = 0;
view_copy[2][3] = 0;
gl_Position = uniform_projection * view_copy * vec4(in_pos, 1.0f);
out_color = in_color;
}

View File

@@ -251,6 +251,49 @@ void GLMeshRenderer::render_chunk_mesh(Mesh* mesh, Texture* texture, float opaci
m_transform.identity();
}
void GLMeshRenderer::render_skybox_mesh(Mesh* mesh) {
if(!m_shader) {
LOG_ERROR("Attempt to render mesh without active shader!");
return;
}
if(!mesh->has_colors()) {
LOG_ERROR("Attempt to render skybox mesh without colors!");
return;
}
GLMesh* gl_mesh = static_cast<GLMesh*>(mesh);
if(mesh!=m_prev_mesh) {
if(m_prev_mesh && !GLEW_ARB_vertex_array_object) {
glDisableVertexAttribArray(m_shader->get_attrib_location("in_pos"));
CHECK_GL;
glDisableVertexAttribArray(m_shader->get_attrib_location("in_color"));
CHECK_GL;
}
if(GLEW_ARB_vertex_array_object) {
glBindVertexArray(gl_mesh->m_vao);
CHECK_GL;
}
if(!GLEW_ARB_vertex_array_object) {
legacy_prepare_mesh_array_buffer(m_shader, gl_mesh->m_vbo, GL_FLOAT, 3, "in_pos");
legacy_prepare_mesh_array_buffer(m_shader, gl_mesh->m_cbo, GL_FLOAT, 4, "in_color");
}
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gl_mesh->m_ebo);
CHECK_GL;
m_prev_mesh = mesh;
}
m_shader->set_uniform("uniform_view", m_camera->get_view());
CHECK_GL;
m_shader->set_uniform("uniform_projection", m_camera->get_projection());
CHECK_GL;
glDrawElements(GL_TRIANGLES, gl_mesh->m_element_count, GL_UNSIGNED_INT, nullptr);
CHECK_GL;
}
void GLMeshRenderer::render_with_multiple_textures(Mesh* mesh, const std::vector<Texture*>& textures) {
// TODO
}
@@ -285,6 +328,11 @@ void GLMeshRenderer::set_3d_rendering_mode(bool enable) {
CHECK_GL;
}
void GLMeshRenderer::set_depth_buffer_mode(bool enable) {
glDepthMask(enable?GL_TRUE:GL_FALSE);
CHECK_GL;
}
void GLMeshRenderer::set_alpha_blending_mode(bool enable) {
if(enable) {
glEnable(GL_BLEND);

View File

@@ -39,10 +39,12 @@ namespace polygun::renderer {
virtual void render(Mesh* mesh, const math::RGBAColorf& color) override;
virtual void render_textured(Mesh* mesh, Texture* texture) override;
virtual void render_chunk_mesh(Mesh* mesh, Texture* texture, float opacity) override;
virtual void render_skybox_mesh(Mesh* mesh) override;
virtual void render_with_multiple_textures(Mesh* mesh, const std::vector<Texture*>& textures) override;
virtual void set_shader(Shader* shader) override;
virtual void set_camera(Camera* camera) override;
virtual void set_3d_rendering_mode(bool enable) override;
virtual void set_depth_buffer_mode(bool enable) override;
virtual void set_alpha_blending_mode(bool enable) override;
private:

View File

@@ -151,6 +151,46 @@ void LegacyGLMeshRenderer::render_textured(Mesh* mesh, Texture* texture) {
m_transform.identity();
}
void LegacyGLMeshRenderer::render_skybox_mesh(Mesh* mesh) {
if(!mesh->has_colors()) {
LOG_ERROR("Attempt to render skybox mesh without colors!");
return;
}
LegacyGLMesh* gl_mesh = static_cast<LegacyGLMesh*>(mesh);
glEnableClientState(GL_VERTEX_ARRAY);
CHECK_GL;
glVertexPointer(3, GL_FLOAT, 0, gl_mesh->m_vertices);
CHECK_GL;
glEnableClientState(GL_COLOR_ARRAY);
CHECK_GL;
glColorPointer(4, GL_FLOAT, 0, gl_mesh->m_colors);
CHECK_GL;
glMatrixMode(GL_MODELVIEW);
CHECK_GL;
glLoadIdentity();
CHECK_GL;
glMatrixMode(GL_PROJECTION);
CHECK_GL;
glLoadMatrixf(m_camera->get_projection().to_pointer());
CHECK_GL;
math::Matrix4 view = m_camera->get_view();
view.m_matrix[3][0] = 0;
view.m_matrix[3][1] = 0;
view.m_matrix[3][2] = 0;
view.m_matrix[3][3] = 1.0f;
view.m_matrix[0][3] = 0;
view.m_matrix[1][3] = 0;
view.m_matrix[2][3] = 0;
glMultMatrixf(view.to_pointer());
CHECK_GL;
glDrawElements(GL_TRIANGLES, gl_mesh->m_indices_length, GL_UNSIGNED_INT, gl_mesh->m_indices);
CHECK_GL;
glDisableClientState(GL_VERTEX_ARRAY);
CHECK_GL;
glDisableClientState(GL_COLOR_ARRAY);
CHECK_GL;
}
void LegacyGLMeshRenderer::set_3d_rendering_mode(bool enable) {
if(enable)
glEnable(GL_DEPTH_TEST);
@@ -159,6 +199,11 @@ void LegacyGLMeshRenderer::set_3d_rendering_mode(bool enable) {
CHECK_GL;
}
void LegacyGLMeshRenderer::set_depth_buffer_mode(bool enable) {
glDepthMask(enable?GL_TRUE:GL_FALSE);
CHECK_GL;
}
void LegacyGLMeshRenderer::set_alpha_blending_mode(bool enable) {
if(enable) {
glEnable(GL_BLEND);

View File

@@ -38,12 +38,14 @@ namespace polygun::renderer {
virtual void render(Mesh* mesh, const math::RGBAColorf& color) override;
virtual void render_textured(Mesh* mesh, Texture* texture) override;
virtual void render_chunk_mesh(Mesh* mesh, Texture* texture, float) override { render_textured(mesh, texture); }
virtual void render_skybox_mesh(Mesh* mesh) override;
// Unsupported
virtual void render_with_multiple_textures(Mesh* mesh, const std::vector<Texture*>& textures) override {}
// Unsupported
virtual void set_shader(Shader* shader) override {}
virtual void set_camera(Camera* camera) override { m_camera = camera; }
virtual void set_3d_rendering_mode(bool enable) override;
virtual void set_depth_buffer_mode(bool enable) override;
virtual void set_alpha_blending_mode(bool enable) override;
private:

View File

@@ -55,10 +55,12 @@ namespace polygun::renderer {
virtual void render(Mesh* mesh, const math::RGBAColorf& color) = 0;
virtual void render_textured(Mesh* mesh, Texture* texture) = 0;
virtual void render_chunk_mesh(Mesh* mesh, Texture* texture, float opacity) = 0;
virtual void render_skybox_mesh(Mesh* mesh) = 0;
virtual void render_with_multiple_textures(Mesh* mesh, const std::vector<Texture*>& textures) = 0;
virtual void set_shader(Shader* shader) = 0;
virtual void set_camera(Camera* camera) = 0;
virtual void set_3d_rendering_mode(bool enable) = 0;
virtual void set_depth_buffer_mode(bool enable) = 0;
virtual void set_alpha_blending_mode(bool enable) = 0;
protected:

View File

@@ -67,6 +67,7 @@ GameSessionScreen::GameSessionScreen(const std::string& ip, unsigned short port,
m_resource_manager(nullptr),
m_content_registry(nullptr),
m_texture_atlas(nullptr),
m_skybox(),
m_chunk_manager(),
m_player_manager(),
m_ingame_music(),
@@ -96,6 +97,7 @@ void GameSessionScreen::begin() {
m_resource_manager = new engine::ResourceManager(m_engine->get_master_renderer());
m_content_registry = new engine::ContentRegistry;
m_texture_atlas = new engine::TextureAtlas(m_engine->get_master_renderer(), *m_content_registry, *m_resource_manager);
m_skybox.reset(new world::Skybox(m_engine->get_master_renderer()));
m_chunk_manager.reset(new world::ChunkManager(m_engine, *m_texture_atlas, *m_content_registry));
m_player_manager.reset(new world::PlayerManager(m_engine, *m_chunk_manager));
m_physic_manager.reset(new world::PhysicManager(m_chunk_manager.get()));
@@ -118,6 +120,7 @@ void GameSessionScreen::render() {
renderer::MeshRenderer* mesh_renderer = m_engine->get_mesh_renderer();
mesh_renderer->set_camera(&m_player_manager->get_master_local_player());
mesh_renderer->set_3d_rendering_mode(true);
m_skybox->render(mesh_renderer);
m_chunk_manager->render();
m_player_manager->render_players();
if(!m_game_paused && m_current_hud)

View File

@@ -41,6 +41,7 @@ SOFTWARE.
#include "game/world/chunk_manager.hpp"
#include "game/world/player_manager.hpp"
#include "game/world/physic_manager.hpp"
#include "game/world/skybox.hpp"
namespace polygun::screens {
enum GameState {
@@ -74,6 +75,7 @@ namespace polygun::screens {
engine::ResourceManager* m_resource_manager;
engine::ContentRegistry* m_content_registry;
engine::TextureAtlas* m_texture_atlas;
std::unique_ptr<world::Skybox> m_skybox;
std::unique_ptr<world::ChunkManager> m_chunk_manager;
std::unique_ptr<world::PlayerManager> m_player_manager;
std::unique_ptr<world::PhysicManager> m_physic_manager;

83
src/game/world/skybox.cpp Normal file
View File

@@ -0,0 +1,83 @@
/*
PolyGun
Copyright (c) 2024 mrkubax10 <mrkubax10@onet.pl>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "game/world/skybox.hpp"
#include "game/renderer/master_renderer.hpp"
#include "game/renderer/mesh.hpp"
#include "game/renderer/shader.hpp"
using namespace polygun::world;
static const std::vector<float> cube_vertices = {
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
-0.5f, 0.5f, -0.5f
};
static const std::vector<unsigned> cube_indices = {
0, 1, 2, 2, 0, 3,
4, 5, 6, 6, 4, 7,
4, 0, 3, 3, 4, 7,
5, 1, 2, 2, 5, 6,
3, 2, 6, 6, 3, 7,
0, 1, 5, 5, 0, 4
};
static const std::vector<float> cube_colors = {
0.91f, 0.51f, 0.23f, 1.0f,
0.91f, 0.51f, 0.23f, 1.0f,
0.0f, 0.2f, 0.7f, 1.0f,
0.0f, 0.2f, 0.7f, 1.0f,
0.91f, 0.51f, 0.23f, 1.0f,
0.91f, 0.51f, 0.23f, 1.0f,
0.0f, 0.2f, 0.7f, 1.0f,
0.0f, 0.2f, 0.7f, 1.0f
};
Skybox::Skybox(renderer::MasterRenderer* master_renderer) :
m_mesh(master_renderer->create_mesh()),
m_shader(master_renderer->create_shader())
{
m_mesh->load_vertices(cube_vertices);
m_mesh->load_indices(cube_indices);
m_mesh->load_colors(cube_colors);
m_shader->load_from_file("skybox");
}
Skybox::~Skybox() {
delete m_shader;
delete m_mesh;
}
void Skybox::render(renderer::MeshRenderer* renderer) {
renderer->set_depth_buffer_mode(false);
renderer->set_alpha_blending_mode(false);
renderer->set_shader(m_shader);
renderer->render_skybox_mesh(m_mesh);
renderer->set_depth_buffer_mode(true);
}

49
src/game/world/skybox.hpp Normal file
View File

@@ -0,0 +1,49 @@
/*
PolyGun
Copyright (c) 2024 mrkubax10 <mrkubax10@onet.pl>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef POLYGUN_WORLD_SKYBOX_HPP
#define POLYGUN_WORLD_SKYBOX_HPP
namespace polygun::renderer {
class MasterRenderer;
class Mesh;
class MeshRenderer;
class Shader;
}
namespace polygun::world {
class Skybox final {
public:
Skybox(renderer::MasterRenderer* master_renderer);
~Skybox();
void render(renderer::MeshRenderer* renderer);
private:
renderer::Mesh* m_mesh;
renderer::Shader* m_shader;
};
}
#endif // POLYGUN_WORLD_SKYBOX_HPP