Compare commits

...

5 Commits

11 changed files with 131 additions and 164 deletions

View File

@ -3,9 +3,9 @@ out vec4 FragColor;
in vec2 TexCoord;
uniform sampler2D texture1;
uniform sampler2D texture_atlas;
void main()
{
FragColor = texture(texture1, TexCoord);
FragColor = texture(texture_atlas, TexCoord);
}

View File

@ -10,6 +10,7 @@
#include "vendor/imgui_impl_opengl3.h"
using namespace polygun::renderer;
using namespace polygun::world;
int sizex = SCR_WIDTH, sizey = SCR_HEIGHT;
@ -118,10 +119,10 @@ namespace polygun::engine {
glEnableVertexAttribArray(1);
unsigned int texture1;
unsigned int texture_atlas;
glGenTextures(1, &texture1);
glBindTexture(GL_TEXTURE_2D, texture1);
glGenTextures(1, &texture_atlas);
glBindTexture(GL_TEXTURE_2D, texture_atlas);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
@ -143,7 +144,7 @@ namespace polygun::engine {
chunk_shader.bind();
chunk_shader.set_uniform("texture1", 0);
chunk_shader.set_uniform("texture_atlas", 0);
m_delta_time = 0;
m_last_frame = 0;
@ -204,7 +205,7 @@ namespace polygun::engine {
// bind textures on corresponding texture units
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1);
glBindTexture(GL_TEXTURE_2D, texture_atlas);
// activate shader
chunk_shader.bind();

View File

@ -0,0 +1,9 @@
#include "greedy_merging.hpp"
namespace polygun::engine {
cuboid_list greedy_merging::merge(world::Chunk& chunk) {
// code here
//cuboid_list list; EXAMPLE
//return list;
}
}

View File

@ -0,0 +1,28 @@
#ifndef POLYGUN_ENGINE_GREEDY_MERGING_HPP
#define POLYGUN_ENGINE_GREEDY_MERGING_HPP
#include "game/world/chunk.hpp"
namespace polygun::engine {
struct cuboid {
int x1, y1, z1, x2, y2, z2, material;
};
struct cuboid_list {
cuboid* cuboids;
int count;
};
class greedy_merging {
public:
greedy_merging() = default;
~greedy_merging() = default;
cuboid_list merge(world::Chunk& chunk);
private:
// greedy merging variables
};
}
#endif // POLYGUN_ENGINE_GREEDY_MERGING_HPP

View File

@ -0,0 +1,22 @@
#ifndef POLYGUN_RENDERER_VERTEX_HPP
#define POLYGUN_RENDERER_VERTEX_HPP
#include <glm/glm.hpp>
namespace polygun::renderer {
struct Vertex {
Vertex() = default;
Vertex(float x, float y, float z) : m_position(glm::vec3(x, y, z)) {}
Vertex(glm::vec3 position) : m_position(position), m_normal(0), m_tex_coords(0) {}
Vertex(glm::vec3 position, glm::vec3 normal) : m_position(position), m_normal(normal), m_tex_coords(0) {}
Vertex(glm::vec3 position, glm::vec3 normal, glm::vec2 tex_coord) : m_position(position), m_normal(normal), m_tex_coords(tex_coord) {}
// position
glm::vec3 m_position = glm::vec3(0.0f);
// normal
glm::vec3 m_normal = glm::vec3(0.0f);
// coords
glm::vec2 m_tex_coords = glm::vec2(0.0f);
};
}
#endif // POLYGUN_RENDERER_VERTEX_HPP

View File

@ -2,6 +2,7 @@
#define POLYGUN_RENDERER_VERTEX_ARRAY_HPP
#include "../core.hpp"
#include "vertex.hpp"
namespace polygun::renderer {
class VertexArray {
@ -27,10 +28,10 @@ namespace polygun::renderer {
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)0);
// Normals
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, Normal));
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, m_normal));
// Texcoords
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, TexCoords));
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, m_tex_coords));
glBindVertexArray(0);
m_num_of_vertices /= sizeof(Vertex);
@ -48,23 +49,23 @@ namespace polygun::renderer {
}
void bind() {
GLCALL(glBindVertexArray(m_vao));
glBindVertexArray(m_vao);
}
void unbind() {
GLCALL(glBindVertexArray(0));
glBindVertexArray(0);
}
void draw_elements(GLenum mode = GL_TRIANGLE_STRIP) {
GLCALL(glBindVertexArray(m_vao));
GLCALL(glDrawElements(mode, m_num_of_indices, GL_UNSIGNED_INT, 0));
GLCALL(glBindVertexArray(0));
glBindVertexArray(m_vao);
glDrawElements(mode, m_num_of_indices, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
void draw_arrays(GLenum mode = GL_TRIANGLES) {
GLCALL(glBindVertexArray(m_vao));
GLCALL(glDrawArrays(mode, 0, m_num_of_vertices));
GLCALL(glBindVertexArray(0));
glBindVertexArray(m_vao);
glDrawArrays(mode, 0, m_num_of_vertices);
glBindVertexArray(0);
}
private:

View File

@ -1,11 +1,11 @@
#include "chunk.hpp"
namespace polygun::engine {
namespace polygun::world {
Chunk::Chunk() {
for (int x = 0; x < CHUNK_SIZE; x++) {
for (int y = 0; y < CHUNK_SIZE; y++) {
for (int z = 0; z < CHUNK_SIZE; z++) {
chunk_data[x][y][z] = 0;
chunk_data[x][y][z] = 1;
}
}
}

View File

@ -1,9 +1,9 @@
#ifndef POLYGUN_ENGINE_CHUNK_HPP
#define POLYGUN_ENGINE_CHUNK_HPP
#ifndef POLYGUN_WORLD_CHUNK_HPP
#define POLYGUN_WORLD_CHUNK_HPP
#include "nodes.hpp"
namespace polygun::engine {
namespace polygun::world {
class Chunk {
public:
Chunk();
@ -23,4 +23,4 @@ namespace polygun::engine {
};
}
#endif // POLYGUN_ENGINE_CHUNK_HPP
#endif // POLYGUN_WORLD_CHUNK_HPP

View File

@ -1,132 +1,38 @@
#include "chunk_renderer.hpp"
#include "common/logger.hpp"
namespace polygun::engine {
ChunkRenderer::ChunkRenderer() {
m_chunk_shader = renderer::Shader("shaders/chunk_vertex.glsl", "shaders/chunk_fragment.glsl");
}
namespace polygun::world {
ChunkRenderer::ChunkRenderer() : chunk_shader("shaders/chunk_vertex.glsl", "shaders/chunk_fragment.glsl") {}
ChunkRenderer::~ChunkRenderer() {
glDeleteVertexArrays(1, &m_vao);
glDeleteBuffers(1, &m_vbo);
}
ChunkRenderer::~ChunkRenderer() {}
void ChunkRenderer::init() {
glEnable(GL_DEPTH_TEST);
void ChunkRenderer::init() {/*
float vertices[] = {
// Add your vertex data here
};
GLsizei v_size = sizeof(vertices);
GLuint indices[] = {
// Add your indices data here
};
GLsizei i_size = sizeof(indices);
float vertices[] = {
-0.1f, -0.1f, -0.1f, 0.0f, 0.0f,
0.1f, -0.1f, -0.1f, 1.0f, 0.0f,
0.1f, 0.1f, -0.1f, 1.0f, 1.0f,
0.1f, 0.1f, -0.1f, 1.0f, 1.0f,
-0.1f, 0.1f, -0.1f, 0.0f, 1.0f,
-0.1f, -0.1f, -0.1f, 0.0f, 0.0f,
vertex_array = polygun::renderer::VertexArray(vertices, v_size, indices, i_size);
-0.1f, -0.1f, 0.1f, 0.0f, 0.0f,
0.1f, -0.1f, 0.1f, 1.0f, 0.0f,
0.1f, 0.1f, 0.1f, 1.0f, 1.0f,
0.1f, 0.1f, 0.1f, 1.0f, 1.0f,
-0.1f, 0.1f, 0.1f, 0.0f, 1.0f,
-0.1f, -0.1f, 0.1f, 0.0f, 0.0f,
// Load and configure textures here
-0.1f, 0.1f, 0.1f, 1.0f, 0.0f,
-0.1f, 0.1f, -0.1f, 1.0f, 1.0f,
-0.1f, -0.1f, -0.1f, 0.0f, 1.0f,
-0.1f, -0.1f, -0.1f, 0.0f, 1.0f,
-0.1f, -0.1f, 0.1f, 0.0f, 0.0f,
-0.1f, 0.1f, 0.1f, 1.0f, 0.0f,
chunk_shader.bind();
chunk_shader.set_uniform("texture", 0);*/
}
0.1f, 0.1f, 0.1f, 1.0f, 0.0f,
0.1f, 0.1f, -0.1f, 1.0f, 1.0f,
0.1f, -0.1f, -0.1f, 0.0f, 1.0f,
0.1f, -0.1f, -0.1f, 0.0f, 1.0f,
0.1f, -0.1f, 0.1f, 0.0f, 0.0f,
0.1f, 0.1f, 0.1f, 1.0f, 0.0f,
void ChunkRenderer::render(const glm::mat4& view, const glm::mat4& projection) {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
-0.1f, -0.1f, -0.1f, 0.0f, 1.0f,
0.1f, -0.1f, -0.1f, 1.0f, 1.0f,
0.1f, -0.1f, 0.1f, 1.0f, 0.0f,
0.1f, -0.1f, 0.1f, 1.0f, 0.0f,
-0.1f, -0.1f, 0.1f, 0.0f, 0.0f,
-0.1f, -0.1f, -0.1f, 0.0f, 1.0f,
chunk_shader.bind();
-0.1f, 0.1f, -0.1f, 0.0f, 1.0f,
0.1f, 0.1f, -0.1f, 1.0f, 1.0f,
0.1f, 0.1f, 0.1f, 1.0f, 0.0f,
0.1f, 0.1f, 0.1f, 1.0f, 0.0f,
-0.1f, 0.1f, 0.1f, 0.0f, 0.0f,
-0.1f, 0.1f, -0.1f, 0.0f, 1.0f
};
chunk_shader.set_uniform("projection", projection);
chunk_shader.set_uniform("view", view);
Chunk chnk;
unsigned int m_vbo, m_vao;
glGenVertexArrays(1, &m_vao);
glGenBuffers(1, &m_vbo);
glBindVertexArray(m_vao);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// texture coord attribute
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glGenTextures(1, &m_texture1);
glBindTexture(GL_TEXTURE_2D, m_texture1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
int width, height, nrChannels;
stbi_set_flip_vertically_on_load(true);
unsigned char *data = stbi_load("res/textures/gold.png", &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
m_chunk_shader.bind();
m_chunk_shader.set_uniform("texture1", 0);
}
void ChunkRenderer::render(glm::mat4 projection, glm::mat4 view, Chunk chunk) {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_texture1);
m_chunk_shader.bind();
m_chunk_shader.set_uniform("projection", projection);
m_chunk_shader.set_uniform("view", view);
glBindVertexArray(m_vao);
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 (chunk.get_node(glm::vec3(x,y,z))!= 0) {
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(x/5.0f, y/5.0f, z/5.0f));
m_chunk_shader.set_uniform("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
}
}
}
}
vertex_array.draw_elements();
}
}

View File

@ -1,27 +1,27 @@
#ifndef POLYGUN_ENGINE_CHUNK_RENDERER_HPP
#define POLYGUN_ENGINE_CHUNK_RENDERER_HPP
#include <glm/glm.hpp>
#include "chunk.hpp"
#include "game/engine/engine.hpp"
#include "../renderer/shader.hpp"
#include "../renderer/vertex_array.hpp"
namespace polygun::engine {
class ChunkRenderer {
public:
ChunkRenderer();
~ChunkRenderer();
void init();
void render(glm::mat4 projection, glm::mat4 view, Chunk chunk_to_render);
private:
void load_textures();
void generate_mesh();
namespace polygun::world {
class ChunkRenderer {
public:
ChunkRenderer();
~ChunkRenderer();
void init();
void render(const glm::mat4& view, const glm::mat4& projection);
private:
unsigned int texture;
renderer::Shader chunk_shader;
renderer::VertexArray vertex_array;
};
renderer::Shader m_chunk_shader;
unsigned int m_vbo, m_vao;
Chunk m_chunk;
//unsigned int m_textures[sizeof(nodes)];
unsigned int m_texture1;
};
}
#endif //POLYGUN_ENGINE_CHUNK_RENDERER_HPP

View File

@ -1,9 +1,9 @@
#ifndef POLYGUN_ENGINE_NODES_HPP
#define POLYGUN_ENGINE_NODES_HPP
#ifndef POLYGUN_WORLD_NODES_HPP
#define POLYGUN_WORLD_NODES_HPP
#include "../core.hpp"
namespace polygun::engine {
namespace polygun::world {
struct NodeType {
std::string name;
std::string texture;
@ -18,4 +18,4 @@ namespace polygun::engine {
};
}
#endif // POLYGUN_ENGINE_NODES_HPP
#endif // POLYGUN_WORLD_NODES_HPP