From 3b02022fd79ad0e130b8c49e7707d221bfc97bb5 Mon Sep 17 00:00:00 2001 From: hachem Date: Tue, 12 Aug 2025 19:17:50 +0200 Subject: feat: texture and pixel buffers --- src/core/application.cpp | 95 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 83 insertions(+), 12 deletions(-) (limited to 'src/core/application.cpp') diff --git a/src/core/application.cpp b/src/core/application.cpp index b80ad84..0acd232 100644 --- a/src/core/application.cpp +++ b/src/core/application.cpp @@ -5,6 +5,7 @@ #include "rendering/vertex_buffer.h" #include "rendering/index_buffer.h" #include "rendering/shader.h" +#include "rendering/texture.h" #include #include @@ -71,6 +72,8 @@ namespace Donut else m_minimized = false; + Renderer::on_window_resize(e.get_width(), e.get_height()); + if (m_camera) m_camera->set_projection(45.0f, (float)e.get_width() / (float)e.get_height(), 0.1f, 100.0f); @@ -150,6 +153,8 @@ namespace Donut void Application::on_init() { Renderer::init(); + Renderer::on_window_resize(1280, 720); + RenderCommand::set_face_culling(false); m_camera = std::make_unique(45.0f, 1280.0f / 720.0f, 0.1f, 100.0f); m_camera->set_position({ 0.0f, 0.0f, 3.0f }); @@ -158,14 +163,14 @@ namespace Donut float vertices[] = { - -0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f, - 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, - 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 1.0f, - -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 1.0f, - -0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 1.0f, 1.0f, - 0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, - 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 1.0f, - -0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f + -0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, + 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, + 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, + -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, + -0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, + 0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, + -0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f }; uint32_t indices[] = @@ -175,7 +180,7 @@ namespace Donut 5, 4, 7, 7, 6, 5, 4, 0, 3, 3, 7, 4, 3, 2, 6, 6, 7, 3, - 4, 5, 1, 1, 0, 4 + 4, 5, 1, 1, 0, 4 }; m_vertex_array = std::shared_ptr(VertexArray::create()); @@ -184,6 +189,7 @@ namespace Donut VertexBufferLayout layout; layout.push(3); layout.push(4); + layout.push(2); m_vertex_buffer->set_layout(layout); m_vertex_array->add_vertex_buffer(m_vertex_buffer); @@ -191,7 +197,32 @@ namespace Donut m_index_buffer = std::shared_ptr(IndexBuffer::create(indices, 36)); m_vertex_array->set_index_buffer(m_index_buffer); - m_shader = std::shared_ptr(Shader::create("assets/basic.glsl")); + m_shader = std::shared_ptr(Shader::create("assets/textured.glsl")); + + m_texture = Texture2D::create(256, 256); + + uint32_t* pixel_data = new uint32_t[256 * 256]; + for (int y = 0; y < 256; y++) + { + for (int x = 0; x < 256; x++) + { + float u = (float)x / 256.0f; + float v = (float)y / 256.0f; + + float wave = sin(u * 10.0f) * cos(v * 10.0f); + wave = (wave + 1.0f) * 0.5f; + + uint8_t r = (uint8_t)(wave * 255); + uint8_t g = (uint8_t)((1.0f - wave) * 255); + uint8_t b = (uint8_t)((u + v) * 0.5f * 255); + uint8_t a = 255; + + pixel_data[y * 256 + x] = (a << 24) | (b << 16) | (g << 8) | r; + } + } + + m_texture->set_data(pixel_data, 256 * 256 * 4); + delete[] pixel_data; } void Application::on_shutdown() @@ -215,11 +246,13 @@ namespace Donut m_camera->move_left(m_delta_time); if (m_keys[GLFW_KEY_D]) m_camera->move_right(m_delta_time); - if (m_keys[GLFW_KEY_SPACE]) + if (m_keys[GLFW_KEY_SPACE]) m_camera->move_up(m_delta_time); if (m_keys[GLFW_KEY_LEFT_SHIFT]) - m_camera->move_down(m_delta_time); + m_camera->move_down(m_delta_time); } + + update_texture(); } void Application::on_render() @@ -227,6 +260,12 @@ namespace Donut Renderer::set_clear_color({ 0.0f, 0.0f, 0.0f, 1.0f }); Renderer::clear(); + if (m_texture) + { + m_texture->bind(0); + m_shader->set_int("u_Texture", 0); + } + glm::mat4 view_projection = m_camera ? m_camera->get_view_projection_matrix() : glm::mat4(1.0f); glm::vec3 center_pos(0.0f, 0.0f, 0.0f); @@ -252,4 +291,36 @@ namespace Donut transform = glm::translate(glm::mat4(1.0f), back_pos); Renderer::submit(m_shader, m_vertex_array, transform, view_projection); } + + void Application::update_texture() + { + if (!m_texture) + return; + + m_texture_time += m_delta_time; + + uint32_t* pixel_data = new uint32_t[256 * 256]; + for (int y = 0; y < 256; y++) + { + for (int x = 0; x < 256; x++) + { + float time = m_texture_time * 2.0f; + float u = (float)x / 256.0f; + float v = (float)y / 256.0f; + + float wave = sin(u * 10.0f + time) * cos(v * 10.0f + time * 0.5f); + wave = (wave + 1.0f) * 0.5f; + + uint8_t r = (uint8_t)(wave * 255); + uint8_t g = (uint8_t)((1.0f - wave) * 255); + uint8_t b = (uint8_t)((u + v) * 0.5f * 255); + uint8_t a = 255; + + pixel_data[y * 256 + x] = (a << 24) | (b << 16) | (g << 8) | r; + } + } + + m_texture->set_data(pixel_data, 256 * 256 * 4); + delete[] pixel_data; + } } -- cgit v1.3