Compare commits

...

3 Commits

Author SHA1 Message Date
b22b7411eb Merge branch greedy-implementation 2023-05-19 16:22:21 +02:00
de21aa399e Fix some oversights 2023-05-18 18:32:28 +02:00
b43b69a749 implement greedy meshing without textures 2023-05-16 15:30:29 +02:00
2 changed files with 24 additions and 102 deletions

View File

@ -38,102 +38,12 @@ SOFTWARE.
#include "server/server.hpp"
#endif
// greedy meshing debug test
//#include <iostream>
#include "../engine/greedy_meshing.hpp"
using namespace polygun::screens;
static int x_d, y_d, z_d;
static int id_d;
static const std::vector<float> vertices = {
-0.1f , -0.1f, -0.1f,
0.1f , -0.1f, -0.1f,
0.1f , 0.1f, -0.1f,
0.1f , 0.1f, -0.1f,
-0.1f , 0.1f, -0.1f,
-0.1f , -0.1f, -0.1f,
-0.1f , -0.1f, 0.1f,
0.1f , -0.1f, 0.1f,
0.1f , 0.1f, 0.1f,
0.1f , 0.1f, 0.1f,
-0.1f , 0.1f, 0.1f,
-0.1f , -0.1f, 0.1f,
-0.1f , 0.1f, 0.1f,
-0.1f , 0.1f, -0.1f,
-0.1f , -0.1f, -0.1f,
-0.1f , -0.1f, -0.1f,
-0.1f , -0.1f, 0.1f,
-0.1f , 0.1f, 0.1f,
0.1f , 0.1f, 0.1f,
0.1f , 0.1f, -0.1f,
0.1f , -0.1f, -0.1f,
0.1f , -0.1f, -0.1f,
0.1f , -0.1f, 0.1f,
0.1f , 0.1f, 0.1f,
-0.1f , -0.1f, -0.1f,
0.1f , -0.1f, -0.1f,
0.1f , -0.1f, 0.1f,
0.1f , -0.1f, 0.1f,
-0.1f , -0.1f, 0.1f,
-0.1f , -0.1f, -0.1f,
-0.1f , 0.1f, -0.1f,
0.1f , 0.1f, -0.1f,
0.1f , 0.1f, 0.1f,
0.1f , 0.1f, 0.1f,
-0.1f , 0.1f, 0.1f,
-0.1f , 0.1f, -0.1f,
};
static std::vector<unsigned> indices;
static const std::vector<float> uvs={
0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 1.0f,
1.0f, 1.0f,
0.0f, 1.0f,
0.0f, 0.0f,
0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 1.0f,
1.0f, 1.0f,
0.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 1.0f,
0.0f, 1.0f,
0.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 0.0f,
1.0f, 1.0f,
0.0f, 1.0f,
0.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f,
0.0f, 1.0f,
1.0f, 1.0f,
1.0f, 0.0f,
1.0f, 0.0f,
0.0f, 0.0f,
0.0f, 1.0f,
0.0f, 1.0f,
1.0f, 1.0f,
1.0f, 0.0f,
1.0f, 0.0f,
0.0f, 0.0f,
0.0f, 1.0f
};
static void scroll_callback(GLFWwindow* window, double xoffset, double yoffset) {
// limit from 0 to size of polygun::world::nodes array change to sizeof and use id_d as a current node id
if(yoffset > 0) {
@ -180,14 +90,7 @@ void GameSessionScreen::begin() {
m_chunk_shader->load_from_file("chunk");
m_node_mesh = m_engine->get_master_renderer().create_mesh();
for(size_t i = 0; i<vertices.size(); i+=3)
indices.push_back((int)i/3);
/*
engine::greedy_meshing greedy;
engine::vertices_indices mesh = greedy.generate_mesh(greedy.merge(m_chnk), m_chnk);
m_node_mesh->load_from_memory(mesh.vertices, mesh.indices);
*/
m_node_mesh->load_from_memory(vertices, indices, uvs);
update_mesh();
for(int i = 0; i< sizeof(world::nodes) / sizeof(world::nodes[0]); i++) {
m_texture_atlas[i] = m_engine->get_master_renderer().create_texture();
@ -242,13 +145,14 @@ void GameSessionScreen::render() {
renderer::MeshRenderer* mesh_renderer = m_engine->get_mesh_renderer();
mesh_renderer->set_3d_rendering_mode(true);
/*
for (unsigned int x = 0; x < 32; x++) {
for (unsigned int y = 0; y < 32; y++) {
for (unsigned int z = 0; z < 32; z++) {
if (m_chnk.get_node(glm::vec3(x,y,z))!= 0) {
mesh_renderer->translate(glm::vec3(x/5.0f, y/5.0f, z/5.0f));
if( m_chnk.get_node(glm::vec3(x,y,z)) >= sizeof(world::nodes) / sizeof(world::nodes[0])) {
mesh_renderer->render_textured(m_node_mesh, m_texture_atlas[0]);
}
else {
mesh_renderer->render_textured(m_node_mesh, m_texture_atlas[m_chnk.get_node(glm::vec3(x,y,z))]);
@ -257,6 +161,8 @@ void GameSessionScreen::render() {
}
}
}
*/
mesh_renderer->render(m_node_mesh, glm::vec4(0.5f, 0.5f, 0.5f, 1.0f));
m_engine->get_gui_renderer()->render_texture(glm::vec2(1,1), m_texture_atlas[id_d], glm::vec2(10,10), 0.0f, glm::vec4(1.0f,1.0f,1.0f,1.0f));
@ -286,8 +192,10 @@ void GameSessionScreen::render() {
// camera fov
ImGui::InputFloat(_("FOV"), &m_camera.m_fov);
if (ImGui::Button(_("Place Block")))
if (ImGui::Button(_("Place Block"))) {
m_chnk.add_node(id_d, glm::vec3(x_d, y_d, z_d));
update_mesh();
}
ImGui::NewLine();
@ -492,4 +400,15 @@ void GameSessionScreen::show_imgui_error_message(const std::string& title, const
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
ImGui::EndFrame();
}
}
void GameSessionScreen::update_mesh(){
std::vector<float> vertices;
engine::vertices_indices mesh = m_greedy.generate_mesh(m_greedy.merge(m_chnk), m_chnk);
for (int i = 0; i < mesh.vertices.size(); i++) {
vertices.push_back(mesh.vertices[i].x);
vertices.push_back(mesh.vertices[i].y);
vertices.push_back(mesh.vertices[i].z);
}
m_node_mesh->load_from_memory(vertices, mesh.indices);
}

View File

@ -34,6 +34,7 @@ SOFTWARE.
#include "game/engine/player_camera.hpp"
#include "game/world/chunk.hpp"
#include "game/engine/greedy_meshing.hpp"
namespace polygun::renderer {
class Shader;
@ -59,6 +60,7 @@ namespace polygun::screens {
virtual void finish() override;
private:
engine::greedy_meshing m_greedy;
std::string m_ip;
unsigned short m_port;
std::string m_player_nick;
@ -84,7 +86,8 @@ namespace polygun::screens {
void local_server_thread_func();
#endif
void show_imgui_error_message(const std::string& title, const std::string& msg);
void update_mesh();
};
}
#endif // POLYGUN_SCREENS_GAME_SESSION_SCREEN_HPP
#endif // POLYGUN_SCREENS_GAME_SESSION_SCREEN_HPP