Compare commits

...

2 Commits

Author SHA1 Message Date
4bf4e22383 camera movement changed to minecraft-like 2023-04-08 10:41:59 +02:00
acc07bcbbb fix 2023-04-07 11:06:43 +02:00
7 changed files with 86 additions and 86 deletions

View File

@ -5,7 +5,7 @@ namespace polygun::engine {
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

@ -2,28 +2,8 @@
#include "common/logger.hpp"
namespace polygun::engine {
ChunkRenderer::ChunkRenderer(Chunk chunk_to_render) {
m_chunk = chunk_to_render;
ChunkRenderer::ChunkRenderer() {
m_chunk_shader = renderer::Shader("shaders/chunk_vertex.glsl", "shaders/chunk_fragment.glsl");
generate_mesh();
glGenVertexArrays(1, &m_vao);
glGenBuffers(1, &m_vbo);
glBindVertexArray(m_vao);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
// 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);
load_textures();
}
ChunkRenderer::~ChunkRenderer() {
@ -31,56 +11,9 @@ namespace polygun::engine {
glDeleteBuffers(1, &m_vbo);
}
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);
void ChunkRenderer::init() {
glEnable(GL_DEPTH_TEST);
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);
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/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 {
LOG_ERROR("Failed to load texture");
}
stbi_image_free(data);
m_chunk_shader.bind();
m_chunk_shader.set_uniform("texture1", 0);
}
void ChunkRenderer::generate_mesh() {
float vertices[] = {
-0.1f, -0.1f, -0.1f, 0.0f, 0.0f,
0.1f, -0.1f, -0.1f, 1.0f, 0.0f,
@ -125,6 +58,75 @@ namespace polygun::engine {
-0.1f, 0.1f, -0.1f, 0.0f, 1.0f
};
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);
}
}
}
}
}
}

View File

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

View File

@ -11,12 +11,13 @@
using namespace polygun::renderer;
int sizex = SCR_WIDTH, sizey = SCR_HEIGHT;
namespace polygun::engine {
void framebuffer_size_callback(GLFWwindow* window, int width, int height) { glViewport(0, 0, width, height); }
void framebuffer_size_callback(GLFWwindow* window, int width, int height) { glViewport(0, 0, width, height); glfwGetWindowSize(window, &sizex, &sizey);}
void Engine::init() {
// Initialize audio
polygun::audio::init();
audio::init();
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
@ -207,9 +208,6 @@ namespace polygun::engine {
// activate shader
chunk_shader.bind();
int sizex,sizey;
glfwGetWindowSize(window, &sizex, &sizey);
glm::mat4 projection = glm::perspective(glm::radians(m_camera.m_zoom), (float)sizex / (float)sizey, 0.1f, 100.0f);
chunk_shader.set_uniform("projection", projection);
@ -245,7 +243,7 @@ namespace polygun::engine {
// 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);

View File

@ -59,13 +59,13 @@ void Camera::update(bool reset) {
void Camera::process_movement(camera_movement direction, float delta_time) {
float velocity = m_movement_speed * delta_time;
if (direction == FORWARD)
m_position += m_front * velocity;
m_position += glm::vec3(m_front.x,0.0f,m_front.z) * velocity;
if (direction == BACKWARD)
m_position -= m_front * velocity;
m_position -= glm::vec3(m_front.x,0.0f,m_front.z) * velocity;
if (direction == LEFT)
m_position -= m_right * velocity;
m_position -= glm::vec3(m_right.x,0.0f,m_right.z) * velocity;
if (direction == RIGHT)
m_position += m_right * velocity;
m_position += glm::vec3(m_right.x,0.0f,m_right.z) * velocity;
if (direction == UP)
m_position += glm::vec3(0.0F, 1.0F, 0.0F) * velocity;
if (direction == DOWN)

0
src/game/game.cpp Normal file
View File

0
src/game/game.hpp Normal file
View File