diff options
Diffstat (limited to 'src/main.cpp')
-rw-r--r-- | src/main.cpp | 111 |
1 files changed, 79 insertions, 32 deletions
diff --git a/src/main.cpp b/src/main.cpp index 6f825b1..5845332 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,6 +3,8 @@ #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" #include "../include/model_loader/ObjModelLoader.hpp" @@ -27,6 +29,29 @@ struct Userdata { float cam_dist; }; +static std::vector<Triangle3D> generate_sand() { + const int rows = 50; + const int columns = 50; + std::vector<Triangle3D> vertices(rows * columns * 2); + int i = 0; + float div = 0.3f; + 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} + }; + 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} + }; + } + } + return vertices; +} + int main() { initGlfw(); @@ -38,40 +63,50 @@ int main() DeviceFrame frame; - std::array<Triangle3D, 2> cpuTriangle = { - Triangle3D( - Vertex3D(0.0f, 0.0f, 0.0f), - Vertex3D(1.0f, 0.0f, 0.0f), - Vertex3D(0.0f, 1.0f, 0.0f) - ), - Triangle3D( - Vertex3D(1.0f, 0.0f, 0.0f), - Vertex3D(1.0f, 1.0f, 0.0f), - Vertex3D(0.0f, 1.0f, 0.0f) - ), - }; - //DataView<Triangle3D> cpuTriangles(cpuTriangle.data(), cpuTriangle.size()); + std::vector<Triangle3D> cpu_sand = generate_sand(); + DeviceMemory *gpuSand = frame.alloc(); + gpuSand->copy({cpu_sand.data(), cpu_sand.size()}, DeviceMemory::StorageType::STATIC); + + std::unique_ptr<Shader> sand_vertex_shader = load_shader_from_file("shaders/sand_vertex.vert", Shader::Type::VERTEX); + std::unique_ptr<Shader> sand_pixel_shader = load_shader_from_file("shaders/sand_fragment.frag", Shader::Type::PIXEL); + std::unique_ptr<ShaderProgram> sand_shader_program = build_shader_program_from_shaders({ sand_vertex_shader.get(), sand_pixel_shader.get() }); + std::vector<Triangle3D> triangles; - ObjModelLoader::load_from_file("/home/dec05eba/Downloads/FinalBaseMesh.obj", triangles); - DataView<Triangle3D> cpuTriangles(triangles.data(), triangles.size()); - DeviceMemory *gpuTriangle = frame.alloc(); - gpuTriangle->copy(cpuTriangles, DeviceMemory::StorageType::STATIC); + std::vector<vec2f> 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<Shader> vertex_shader = load_shader_from_file("shaders/vertex.vert", Shader::Type::VERTEX); std::unique_ptr<Shader> pixel_shader = load_shader_from_file("shaders/fragment.frag", Shader::Type::PIXEL); std::unique_ptr<ShaderProgram> shader_program = build_shader_program_from_shaders({ vertex_shader.get(), pixel_shader.get() }); - shader_program->set_vertex_input("position", *gpuTriangle); - - Result<Uniform> proj_uniform = shader_program->get_uniform_by_name("proj"); glm::mat4 proj = glm::perspective(glm::radians(75.0f), (float)window_width / (float)window_height, 0.2f, 1000.0f); - proj_uniform->set(proj); + vec3f triangle_color = { 1.0f, 0.0f, 0.0f }; - Result<Uniform> triangle_color_uniform = shader_program->get_uniform_by_name("triangle_color"); - triangle_color_uniform->set(vec3f{ 1.0f, 0.0f, 0.0f }); + Result<Uniform> sand_proj_uniform = sand_shader_program->get_uniform_by_name("proj"); + Result<Uniform> sand_view_uniform = sand_shader_program->get_uniform_by_name("view"); + Result<Uniform> sand_model_uniform = sand_shader_program->get_uniform_by_name("model"); + Result<Uniform> sand_time = sand_shader_program->get_uniform_by_name("time"); + Result<Uniform> sand_triangle_color_uniform = sand_shader_program->get_uniform_by_name("triangle_color"); + Result<Uniform> proj_uniform = shader_program->get_uniform_by_name("proj"); Result<Uniform> view_uniform = shader_program->get_uniform_by_name("view"); Result<Uniform> model_uniform = shader_program->get_uniform_by_name("model"); + Result<Uniform> tex_uniform = shader_program->get_uniform_by_name("tex"); + + sand_proj_uniform->set(proj); + sand_triangle_color_uniform->set(triangle_color); + + proj_uniform->set(proj); + + shader_program->set_input_data("texcoord_vert", *model_gpu_texture); + tex_uniform->set(model_texture); Userdata userdata; userdata.cam_dist = 3.0f; @@ -85,7 +120,8 @@ int main() userdata->cam_dist += yoffset; }); - //glEnable(GL_CULL_FACE); + glEnable(GL_CULL_FACE); + float time = 0.0f; while(!glfwWindowShouldClose(window)) { glfwPollEvents(); if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) @@ -94,6 +130,7 @@ int main() auto t_now = std::chrono::high_resolution_clock::now(); float time_delta = std::chrono::duration_cast<std::chrono::duration<float>>(t_now - t_start).count(); t_start = t_now; + time += time_delta; float move_speed = time_delta * 3.0f; if(glfwGetKey(window, GLFW_KEY_W)) @@ -112,6 +149,7 @@ int main() character_pos - glm::vec3(0.0f, 3.0f, 3.0f), glm::vec3(0.0f, 0.0f, 1.0f) ); + sand_view_uniform->set(view); view_uniform->set(view); glm::mat4 model = glm::mat4(1.0f); @@ -120,18 +158,32 @@ int main() 0.0f, glm::vec3(0.0f, 0.0f, 1.0f) ); + sand_model_uniform->set(model); model_uniform->set(model); + //printf("now: %f\n", time); + sand_time->set(time); + // Set color for clearing glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Do the actual screen clearing, using the color set using glClearColor glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + + shader_program->set_input_data("position", *gpuModel); frame.draw(shader_program.get()); - glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + + sand_shader_program->set_input_data("position", *gpuSand); + frame.draw(sand_shader_program.get()); + + //glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glfwSwapBuffers(window); } + delete model_gpu_texture; + delete image; + delete gpuModel; + delete gpuSand; glfwTerminate(); return 0; } @@ -166,13 +218,8 @@ void initGlew() { } } -static void error_callback(int error, const char *description) { - fprintf(stderr, "Opengl error: %s (%d)\n", description, error); -} - GLFWwindow* createWindow() { - glfwSetErrorCallback(error_callback); - GLFWwindow *window = glfwCreateWindow(1280, 720, "Amalgine", nullptr, nullptr); + GLFWwindow *window = glfwCreateWindow(1920, 1080, "Amalgine", nullptr, nullptr); if(!window) { fprintf(stderr, "Failed to open GLFW window\n"); |