Compare commits

...

5 Commits

3 changed files with 264 additions and 310 deletions

View File

@ -1,7 +1,8 @@
#include "chunk_renderer.hpp"
#include "common/logger.hpp"
namespace polygun::engine {
ChunkRenderer::ChunkRenderer(Chunk *chunk_to_render) {
ChunkRenderer::ChunkRenderer(Chunk chunk_to_render) {
m_chunk = chunk_to_render;
m_chunk_shader = renderer::Shader("shaders/chunk_vertex.glsl", "shaders/chunk_fragment.glsl");
@ -14,7 +15,6 @@ namespace polygun::engine {
glBindVertexArray(m_vao);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(m_vertices), m_vertices, GL_DYNAMIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
@ -31,33 +31,32 @@ namespace polygun::engine {
glDeleteBuffers(1, &m_vbo);
}
void ChunkRenderer::load_textures() {/*
for (int i = 0; i < sizeof(nodes); i++) {
glGenTextures(1, &m_textures[i]);
glBindTexture(GL_TEXTURE_2D, m_textures[i]);
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(nodes[i++].texture.c_str(), &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);
}
void ChunkRenderer::render(glm::mat4 projection, glm::mat4 view) {
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);
m_chunk_shader.bind();
m_chunk_shader.set_uniform("texture1", 0);*/
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 (m_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);
}
}
}
}
}
void ChunkRenderer::load_textures() {
glGenTextures(1, &m_texture1);
glBindTexture(GL_TEXTURE_2D, m_texture1);
@ -67,15 +66,13 @@ namespace polygun::engine {
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(nodes[2].texture.c_str(), &width, &height, &nrChannels, 0);
if (data)
{
unsigned char *data = stbi_load("res/textures/iron.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;
else {
LOG_ERROR("Failed to load texture");
}
stbi_image_free(data);
@ -84,271 +81,50 @@ namespace polygun::engine {
}
void ChunkRenderer::generate_mesh() {
int index = 0;
for (int x = 0; x < Chunk::CHUNK_SIZE; x++) {
for (int y = 0; y < Chunk::CHUNK_SIZE; y++) {
for (int z = 0; z < Chunk::CHUNK_SIZE; z++) {
if (!m_chunk->is_air(glm::vec3(x, y, z))) {
// check if the neighbour is air and if so add the face vertices to the array
if (m_chunk->is_air(glm::vec3(x + 1, y, z))) {
// right face
m_vertices[index++] = x + 1;
m_vertices[index++] = y;
m_vertices[index++] = z;
m_vertices[index++] = 1.0f;
m_vertices[index++] = 0.0f;
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,
m_vertices[index++] = x + 1;
m_vertices[index++] = y;
m_vertices[index++] = z + 1;
m_vertices[index++] = 1.0f;
m_vertices[index++] = 1.0f;
-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,
m_vertices[index++] = x + 1;
m_vertices[index++] = y + 1;
m_vertices[index++] = z + 1;
m_vertices[index++] = 0.0f;
m_vertices[index++] = 1.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, 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,
m_vertices[index++] = x + 1;
m_vertices[index++] = y + 1;
m_vertices[index++] = z + 1;
m_vertices[index++] = 0.0f;
m_vertices[index++] = 1.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, 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,
m_vertices[index++] = x + 1;
m_vertices[index++] = y + 1;
m_vertices[index++] = z;
m_vertices[index++] = 0.0f;
m_vertices[index++] = 0.0f;
-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,
m_vertices[index++] = x + 1;
m_vertices[index++] = y;
m_vertices[index++] = z;
m_vertices[index++] = 1.0f;
m_vertices[index++] = 0.0f;
}
if (m_chunk->is_air(glm::vec3(x - 1, y, z))) {
// left face
m_vertices[index++] = x;
m_vertices[index++] = y;
m_vertices[index++] = z;
m_vertices[index++] = 0.0f;
m_vertices[index++] = 0.0f;
-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
};
m_vertices[index++] = x;
m_vertices[index++] = y + 1;
m_vertices[index++] = z;
m_vertices[index++] = 0.0f;
m_vertices[index++] = 1.0f;
m_vertices[index++] = x;
m_vertices[index++] = y + 1;
m_vertices[index++] = z + 1;
m_vertices[index++] = 1.0f;
m_vertices[index++] = 1.0f;
m_vertices[index++] = x;
m_vertices[index++] = y + 1;
m_vertices[index++] = z + 1;
m_vertices[index++] = 1.0f;
m_vertices[index++] = 1.0f;
m_vertices[index++] = x;
m_vertices[index++] = y;
m_vertices[index++] = z + 1;
m_vertices[index++] = 1.0f;
m_vertices[index++] = 0.0f;
m_vertices[index++] = x;
m_vertices[index++] = y;
m_vertices[index++] = z;
m_vertices[index++] = 0.0f;
m_vertices[index++] = 0.0f;
}
if (m_chunk->is_air(glm::vec3(x, y + 1, z))) {
// top face
m_vertices[index++] = x;
m_vertices[index++] = y + 1;
m_vertices[index++] = z;
m_vertices[index++] = 0.0f;
m_vertices[index++] = 0.0f;
m_vertices[index++] = x + 1;
m_vertices[index++] = y + 1;
m_vertices[index++] = z;
m_vertices[index++] = 1.0f;
m_vertices[index++] = 0.0f;
m_vertices[index++] = x + 1;
m_vertices[index++] = y + 1;
m_vertices[index++] = z + 1;
m_vertices[index++] = 1.0f;
m_vertices[index++] = 1.0f;
m_vertices[index++] = x + 1;
m_vertices[index++] = y + 1;
m_vertices[index++] = z + 1;
m_vertices[index++] = 1.0f;
m_vertices[index++] = 1.0f;
m_vertices[index++] = x;
m_vertices[index++] = y + 1;
m_vertices[index++] = z + 1;
m_vertices[index++] = 0.0f;
m_vertices[index++] = 1.0f;
m_vertices[index++] = x;
m_vertices[index++] = y + 1;
m_vertices[index++] = z;
m_vertices[index++] = 0.0f;
m_vertices[index++] = 0.0f;
}
if (m_chunk->is_air(glm::vec3(x, y - 1, z))) {
// bottom face
m_vertices[index++] = x;
m_vertices[index++] = y;
m_vertices[index++] = z;
m_vertices[index++] = 0.0f;
m_vertices[index++] = 0.0f;
m_vertices[index++] = x;
m_vertices[index++] = y;
m_vertices[index++] = z + 1;
m_vertices[index++] = 0.0f;
m_vertices[index++] = 1.0f;
m_vertices[index++] = x + 1;
m_vertices[index++] = y;
m_vertices[index++] = z + 1;
m_vertices[index++] = 1.0f;
m_vertices[index++] = 1.0f;
m_vertices[index++] = x + 1;
m_vertices[index++] = y;
m_vertices[index++] = z + 1;
m_vertices[index++] = 1.0f;
m_vertices[index++] = 1.0f;
m_vertices[index++] = x + 1;
m_vertices[index++] = y;
m_vertices[index++] = z;
m_vertices[index++] = 1.0f;
m_vertices[index++] = 0.0f;
m_vertices[index++] = x;
m_vertices[index++] = y;
m_vertices[index++] = z;
m_vertices[index++] = 0.0f;
m_vertices[index++] = 0.0f;
}
if (m_chunk->is_air(glm::vec3(x, y, z + 1))) {
// front face
m_vertices[index++] = x;
m_vertices[index++] = y;
m_vertices[index++] = z + 1;
m_vertices[index++] = 0.0f;
m_vertices[index++] = 0.0f;
m_vertices[index++] = x;
m_vertices[index++] = y + 1;
m_vertices[index++] = z + 1;
m_vertices[index++] = 0.0f;
m_vertices[index++] = 1.0f;
m_vertices[index++] = x + 1;
m_vertices[index++] = y + 1;
m_vertices[index++] = z + 1;
m_vertices[index++] = 1.0f;
m_vertices[index++] = 1.0f;
m_vertices[index++] = x + 1;
m_vertices[index++] = y + 1;
m_vertices[index++] = z + 1;
m_vertices[index++] = 1.0f;
m_vertices[index++] = 1.0f;
m_vertices[index++] = x + 1;
m_vertices[index++] = y;
m_vertices[index++] = z + 1;
m_vertices[index++] = 1.0f;
m_vertices[index++] = 0.0f;
m_vertices[index++] = x;
m_vertices[index++] = y;
m_vertices[index++] = z + 1;
m_vertices[index++] = 0.0f;
m_vertices[index++] = 0.0f;
}
if (m_chunk->is_air(glm::vec3(x, y, z - 1))) {
// back face
m_vertices[index++] = x;
m_vertices[index++] = y;
m_vertices[index++] = z;
m_vertices[index++] = 0.0f;
m_vertices[index++] = 0.0f;
m_vertices[index++] = x + 1;
m_vertices[index++] = y;
m_vertices[index++] = z;
m_vertices[index++] = 1.0f;
m_vertices[index++] = 0.0f;
m_vertices[index++] = x + 1;
m_vertices[index++] = y + 1;
m_vertices[index++] = z;
m_vertices[index++] = 1.0f;
m_vertices[index++] = 1.0f;
m_vertices[index++] = x + 1;
m_vertices[index++] = y + 1;
m_vertices[index++] = z;
m_vertices[index++] = 1.0f;
m_vertices[index++] = 1.0f;
m_vertices[index++] = x;
m_vertices[index++] = y + 1;
m_vertices[index++] = z;
m_vertices[index++] = 0.0f;
m_vertices[index++] = 1.0f;
m_vertices[index++] = x;
m_vertices[index++] = y;
m_vertices[index++] = z;
m_vertices[index++] = 0.0f;
m_vertices[index++] = 0.0f;
}
}
}
}
}
}
void ChunkRenderer::render(Camera camera) {
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// bind textures on corresponding texture units
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_texture1);
// activate shader
m_chunk_shader.bind();
glm::mat4 projection = glm::perspective(glm::radians(camera.m_zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
m_chunk_shader.set_uniform("projection", projection);
glm::mat4 view = camera.get_view_matrix();
m_chunk_shader.set_uniform("view", view);
glBindVertexArray(m_vao);
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(0.0f, 0.0f, 0.0f));
m_chunk_shader.set_uniform("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
}
}

View File

@ -7,21 +7,20 @@
namespace polygun::engine {
class ChunkRenderer {
public:
ChunkRenderer(Chunk *chunk_to_render);
ChunkRenderer(Chunk chunk_to_render);
~ChunkRenderer();
void update_chunk(Chunk *chunk_to_render);
void render(Camera camera);
void update_chunk(Chunk chunk_to_render);
void render(glm::mat4 projection, glm::mat4 view);
private:
void load_textures();
void generate_mesh();
renderer::Shader m_chunk_shader;
unsigned int m_vbo, m_vao;
unsigned int m_textures[sizeof(nodes)];
//unsigned int m_textures[sizeof(nodes)];
unsigned int m_texture1;
float m_vertices[32768];
Chunk *m_chunk;
Chunk m_chunk;
};
}

View File

@ -5,6 +5,9 @@
#include "chunk_renderer.hpp"
#include <GLFW/glfw3.h>
#include "vendor/imgui.h"
#include "vendor/imgui_impl_glfw.h"
#include "vendor/imgui_impl_opengl3.h"
using namespace polygun::renderer;
@ -12,9 +15,8 @@ namespace polygun::engine {
void framebuffer_size_callback(GLFWwindow* window, int width, int height) { glViewport(0, 0, width, height); }
void Engine::init() {
// Initialize audio
// Initialize audio
polygun::audio::init();
polygun::audio::play_sound("./res/example.ogg");
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
@ -37,10 +39,112 @@ namespace polygun::engine {
return;
}
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
ImGui::StyleColorsDark();
// Setup Platform/Renderer bindings
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 330");
glEnable(GL_DEPTH_TEST);
Chunk m_chunk;
m_chunk.add_node(1, glm::vec3(0, 0, 0));
ChunkRenderer m_chunk_renderer(&m_chunk);
bool cursor = false;
Shader chunk_shader("shaders/chunk_vertex.glsl", "shaders/chunk_fragment.glsl");
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,
-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,
-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,
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,
-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,
-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 chnk;
unsigned int VBO, VAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, 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);
unsigned int texture1;
glGenTextures(1, &texture1);
glBindTexture(GL_TEXTURE_2D, 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);
chunk_shader.bind();
chunk_shader.set_uniform("texture1", 0);
m_delta_time = 0;
m_last_frame = 0;
@ -55,7 +159,11 @@ namespace polygun::engine {
// -----
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
//glfwSetWindowShouldClose(window, true);
if(glfwGetInputMode(window, GLFW_CURSOR) == GLFW_CURSOR_DISABLED)
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
else
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
m_camera.process_movement(polygun::engine::FORWARD, m_delta_time);
@ -67,19 +175,90 @@ namespace polygun::engine {
m_camera.process_movement(polygun::engine::RIGHT, m_delta_time);
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
m_camera.process_movement(polygun::engine::UP, m_delta_time);
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS)
m_camera.process_movement(polygun::engine::DOWN, m_delta_time);
if(glfwGetInputMode(window, GLFW_CURSOR) == GLFW_CURSOR_DISABLED){
m_camera.update();
}
m_camera.update();
// render
// ------
m_chunk_renderer.render(m_camera);
// rendering
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// bind textures on corresponding texture units
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1);
// activate shader
chunk_shader.bind();
glm::mat4 projection = glm::perspective(glm::radians(m_camera.m_zoom), (float)SCR_HEIGHT / (float)SCR_HEIGHT, 0.1f, 100.0f);
chunk_shader.set_uniform("projection", projection);
glm::mat4 view = m_camera.get_view_matrix();
chunk_shader.set_uniform("view", view);
glBindVertexArray(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 (chnk.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));
chunk_shader.set_uniform("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
}
}
}
// imgui
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::Begin("Debug");
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
// Camera pos
ImGui::Text("Player Pos: %.3f, %.3f, %.3f", m_camera.m_position.x, m_camera.m_position.y, m_camera.m_position.z);
float x_d, y_d, z_d;
// create input for placing blocks in world with 3 float inputs and a button
int id_d;
ImGui::InputInt("ID", &id_d);
ImGui::InputFloat("X", &x_d);
ImGui::InputFloat("Y", &y_d);
ImGui::InputFloat("Z", &z_d);
if (ImGui::Button("Place Block"))
{
chnk.add_node(id_d, glm::vec3(x_d, y_d, z_d));
}
ImGui::End();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
ImGui::EndFrame();
glfwSwapBuffers(window);
glfwPollEvents();
}
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glfwTerminate();
}