From 8d9b24b1b84107c90a77d94c86a810cc068fe073 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 19 Feb 2020 22:14:12 +0100 Subject: Fix rendering of texture with multiple frames --- src/main.cpp | 126 ++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 81 insertions(+), 45 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index 22bfe1c..a2ea0b8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,7 +2,6 @@ #include "../include/RenderBackend/OpenGL/Shader.hpp" #include "../include/RenderBackend/OpenGL/ShaderProgram.hpp" #include "../include/RenderBackend/OpenGL/DeviceMemory.hpp" -#include "../include/RenderBackend/OpenGL/DeviceFrame.hpp" #include "../include/RenderBackend/OpenGL/Texture2D.hpp" #include "../include/Image.hpp" #include "../include/Triangle.hpp" @@ -34,24 +33,27 @@ static std::vector generate_sand() { const int columns = 50; std::vector vertices(rows * columns * 2); int i = 0; - float div = 0.3f; + float div = 1.0f / (float)rows; for(int y = 0; y < rows; ++y) { for(int x = 0; x < columns; ++x) { vertices[i++] = { - Vertex3D{x * div + 0.0f, y * div + 0.0f, 0.0f}, - Vertex3D{x * div + 1.0f * div, y * div + 0.0f, 0.0f}, - Vertex3D{x * div + 0.0f, y * div + 1.0f * div, 0.0f} + Vertex3D{x * div + 0.0f * div, y * div + 0.0f * div, 0.0f}, + Vertex3D{x * div + 1.0f * div, y * div + 0.0f * div, 0.0f}, + Vertex3D{x * div + 1.0f * div, y * div + 1.0f * div, 0.0f} }; vertices[i++] = { - Vertex3D{x * div + 1.0f * div, y * div + 0.0f, 0.0f}, Vertex3D{x * div + 1.0f * div, y * div + 1.0f * div, 0.0f}, - Vertex3D{x * div + 0.0f, y * div + 1.0f * div, 0.0f} + Vertex3D{x * div + 0.0f * div, y * div + 1.0f * div, 0.0f}, + Vertex3D{x * div + 0.0f * div, y * div + 0.0f * div, 0.0f} }; } } return vertices; } +static void create_sand(DeviceMemory *triangles, DeviceMemory *texcoords, Texture2D *texture); +static DeviceMemory* load_model(const char *filepath); + int main() { initGlfw(); @@ -60,52 +62,52 @@ int main() int window_width = 1; int window_height = 1; glfwGetWindowSize(window, &window_width, &window_height); - - DeviceFrame frame; - - std::vector cpu_sand = generate_sand(); - DeviceMemory *gpuSand = frame.alloc(); - gpuSand->copy({cpu_sand.data(), cpu_sand.size()}, DeviceMemory::StorageType::STATIC); + + glm::mat4 proj = glm::perspective(glm::radians(75.0f), (float)window_width / (float)window_height, 0.2f, 1000.0f); std::unique_ptr sand_vertex_shader = load_shader_from_file("shaders/sand_vertex.vert", Shader::Type::VERTEX); std::unique_ptr sand_pixel_shader = load_shader_from_file("shaders/sand_fragment.frag", Shader::Type::PIXEL); std::unique_ptr sand_shader_program = build_shader_program_from_shaders({ sand_vertex_shader.get(), sand_pixel_shader.get() }); + ShaderFrame sand_frame = sand_shader_program->create_frame(); + + DeviceMemory *sand_triangles = sand_frame.get_input_by_name("position"); + DeviceMemory *sand_texcoords = sand_frame.get_input_by_name("texcoord_vert"); + Texture2D sand_texture; + create_sand(sand_triangles, sand_texcoords, &sand_texture); + + Result sand_proj_uniform = sand_frame.get_uniform_by_name("proj"); + Result sand_view_uniform = sand_frame.get_uniform_by_name("view"); + Result sand_model_uniform = sand_frame.get_uniform_by_name("model"); + //Result sand_time = sand_frame.get_uniform_by_name("time"); + Result sand_heightmap_uniform = sand_frame.get_uniform_by_name("tex"); + + sand_proj_uniform->set(proj); + sand_heightmap_uniform->set(sand_texture); - std::vector triangles; - std::vector texture_coords; - Image *image; - ObjModelLoader::load_from_file("/home/dec05eba/Downloads/ELI4OS.obj", triangles, texture_coords, &image); - DeviceMemory *gpuModel = frame.alloc(); - gpuModel->copy({triangles.data(), triangles.size()}, DeviceMemory::StorageType::STATIC); - Texture2D model_texture(image); - DeviceMemory *model_gpu_texture = frame.alloc(); - model_gpu_texture->copy({texture_coords.data(), texture_coords.size()}, DeviceMemory::StorageType::STATIC); std::unique_ptr vertex_shader = load_shader_from_file("shaders/vertex.vert", Shader::Type::VERTEX); std::unique_ptr pixel_shader = load_shader_from_file("shaders/fragment.frag", Shader::Type::PIXEL); std::unique_ptr shader_program = build_shader_program_from_shaders({ vertex_shader.get(), pixel_shader.get() }); + ShaderFrame model_frame = shader_program->create_frame(); - glm::mat4 proj = glm::perspective(glm::radians(75.0f), (float)window_width / (float)window_height, 0.2f, 1000.0f); - vec3f triangle_color = { 1.0f, 0.0f, 0.0f }; - - Result sand_proj_uniform = sand_shader_program->get_uniform_by_name("proj"); - Result sand_view_uniform = sand_shader_program->get_uniform_by_name("view"); - Result sand_model_uniform = sand_shader_program->get_uniform_by_name("model"); - Result sand_time = sand_shader_program->get_uniform_by_name("time"); - Result sand_triangle_color_uniform = sand_shader_program->get_uniform_by_name("triangle_color"); + std::vector triangles; + std::vector texture_coords; + Image *image; + ObjModelLoader::load_from_file("/home/dec05eba/Downloads/ELI4OS.obj", triangles, texture_coords, &image); + DeviceMemory *gpuModel = model_frame.get_input_by_name("position"); + gpuModel->set({triangles.data(), triangles.size()}, DeviceMemory::StorageType::STATIC); - Result proj_uniform = shader_program->get_uniform_by_name("proj"); - Result view_uniform = shader_program->get_uniform_by_name("view"); - Result model_uniform = shader_program->get_uniform_by_name("model"); - Result tex_uniform = shader_program->get_uniform_by_name("tex"); + Texture2D model_texture(image); + DeviceMemory *model_gpu_texcoords = model_frame.get_input_by_name("texcoord_vert"); + model_gpu_texcoords->set({texture_coords.data(), texture_coords.size()}, DeviceMemory::StorageType::STATIC); - sand_proj_uniform->set(proj); - sand_triangle_color_uniform->set(triangle_color); + Result proj_uniform = model_frame.get_uniform_by_name("proj"); + Result view_uniform = model_frame.get_uniform_by_name("view"); + Result model_uniform = model_frame.get_uniform_by_name("model"); + Result tex_uniform = model_frame.get_uniform_by_name("tex"); proj_uniform->set(proj); - - shader_program->set_input_data("texcoord_vert", *model_gpu_texture); tex_uniform->set(model_texture); Userdata userdata; @@ -121,6 +123,9 @@ int main() }); glEnable(GL_CULL_FACE); + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LESS); + float time = 0.0f; while(!glfwWindowShouldClose(window)) { glfwPollEvents(); @@ -158,11 +163,19 @@ int main() 0.0f, glm::vec3(0.0f, 0.0f, 1.0f) ); - sand_model_uniform->set(model); model_uniform->set(model); + glm::mat4 sand_model = glm::mat4(1.0f); + sand_model = glm::rotate( + sand_model, + 0.0f, + glm::vec3(0.0f, 0.0f, 1.0f) + ); + sand_model = glm::scale(sand_model, glm::vec3(50.0f, 50.0f, 50.0f)); + sand_model_uniform->set(sand_model); + //printf("now: %f\n", time); - sand_time->set(time); + //sand_time->set(time); // Set color for clearing glClearColor(0.0f, 0.0f, 0.0f, 1.0f); @@ -170,11 +183,8 @@ int main() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); - shader_program->set_input_data("position", *gpuModel); - frame.draw(shader_program.get()); - - sand_shader_program->set_input_data("position", *gpuSand); - frame.draw(sand_shader_program.get()); + model_frame.draw(); + sand_frame.draw(); //glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glfwSwapBuffers(window); @@ -185,6 +195,32 @@ int main() return 0; } +void create_sand(DeviceMemory *triangles, DeviceMemory *texcoords, Texture2D *texture) { + std::vector cpu_sand = generate_sand(); + triangles->set({cpu_sand.data(), cpu_sand.size()}, DeviceMemory::StorageType::STATIC); + + Result height_map_image = Image::loadFromFile("heightmap.jpg"); + if(!height_map_image) { + fprintf(stderr, "Error: failed to load image: heightmap.jpg\n"); + exit(1); + } + + std::vector height_map_texcoord; + for(const Triangle3D &triangle : cpu_sand) { + height_map_texcoord.push_back(vec2f{ triangle.p1.x, triangle.p1.y }); + height_map_texcoord.push_back(vec2f{ triangle.p2.x, triangle.p2.y }); + height_map_texcoord.push_back(vec2f{ triangle.p3.x, triangle.p3.y }); + } + + Texture2D height_map_texture(height_map_image.unwrap()); + *texture = std::move(height_map_texture); + texcoords->set({height_map_texcoord.data(), height_map_texcoord.size()}, DeviceMemory::StorageType::STATIC); +} + +DeviceMemory* load_model(const char *filepath) { + return nullptr; +} + void glfwErrorHandler(int errorCode, const char *errorDescription) { printf("GLFW error code: %d, description: %s\n", errorCode, errorDescription); -- cgit v1.2.3