diff options
| author | Mel <einebeere@gmail.com> | 2022-10-14 13:30:15 +0200 |
|---|---|---|
| committer | Mel <einebeere@gmail.com> | 2022-10-14 13:30:15 +0200 |
| commit | 8ec223cd4ffec8ff883e022db94714a0b814ae58 (patch) | |
| tree | 65347fb1981ce2bb76ac84e0a82ab215f92a9fe0 | |
| parent | cb3ddae385c03a8830d39dc37fcd5bf273524d5e (diff) | |
| download | meowcraft-8ec223cd4ffec8ff883e022db94714a0b814ae58.tar.zst meowcraft-8ec223cd4ffec8ff883e022db94714a0b814ae58.zip | |
Textured Cube
| -rw-r--r-- | assets/shaders/fragment.glsl | 5 | ||||
| -rw-r--r-- | src/Binder.cpp | 4 | ||||
| -rw-r--r-- | src/Mesh.hpp | 3 | ||||
| -rw-r--r-- | src/Texture.cpp | 2 | ||||
| -rw-r--r-- | src/main.cpp | 62 |
5 files changed, 49 insertions, 27 deletions
diff --git a/assets/shaders/fragment.glsl b/assets/shaders/fragment.glsl index debc145..f8e9e46 100644 --- a/assets/shaders/fragment.glsl +++ b/assets/shaders/fragment.glsl @@ -1,10 +1,11 @@ #version 330 core +uniform sampler2D tex; + in vec2 frag_tex_coord; out vec4 color; void main() { - //color = vec4(0.2f, 0.6f, 0.6f, 1.0f); // #339999 - color = vec4(frag_tex_coord.xy, 1.0f, 1.0f); + color = texture(tex, frag_tex_coord); } \ No newline at end of file diff --git a/src/Binder.cpp b/src/Binder.cpp index 9bca480..d013f08 100644 --- a/src/Binder.cpp +++ b/src/Binder.cpp @@ -6,7 +6,9 @@ namespace MC { BindableMesh Binder::load(Mesh& mesh) { auto vao = create_vao(); - store_indices(mesh.raw_indices(), mesh.indices_size()); + if (mesh.indices_size() > 0) { + store_indices(mesh.raw_indices(), mesh.indices_size()); + } store_in_attribute_list(0, 3, mesh.raw(), mesh.size() * 3); store_in_attribute_list(1, 2, mesh.raw_tex_coords(), mesh.tex_coords_size() * 2); unbind_vao(); diff --git a/src/Mesh.hpp b/src/Mesh.hpp index 50fc629..b601228 100644 --- a/src/Mesh.hpp +++ b/src/Mesh.hpp @@ -12,6 +12,9 @@ public: Mesh(std::vector<Vector<3>> positions, std::vector<Vector<2>> tex_coords, std::vector<uint32_t> indices) : m_positions(std::move(positions)), m_tex_coords(std::move(tex_coords)), m_indices(std::move(indices)) {}; + Mesh(std::vector<Vector<3>> positions, std::vector<Vector<2>> tex_coords) + : m_positions(std::move(positions)), m_tex_coords(std::move(tex_coords)), m_indices() {}; + float* raw(); size_t size(); diff --git a/src/Texture.cpp b/src/Texture.cpp index d32a75a..d2e1465 100644 --- a/src/Texture.cpp +++ b/src/Texture.cpp @@ -15,6 +15,8 @@ Texture::Texture(const Image::RawImage& image) { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.width(), image.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, image.raw()); glGenerateMipmap(GL_TEXTURE_2D); + + glBindTexture(GL_TEXTURE_2D, 0); } void Texture::bind() { diff --git a/src/main.cpp b/src/main.cpp index 1f5d3a2..58c7b8a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,6 +11,8 @@ #include "Math/MVP.hpp" #include "Shader/ShaderProgram.hpp" #include "Mouse.hpp" +#include "Texture.hpp" +#include "Image/PPMParser.hpp" #define APP_NAME "Meowcraft" @@ -21,7 +23,7 @@ #define FOV 45 void run(); -void render(MC::BindableMesh&); +void render(MC::BindableMesh&, MC::Texture&); void process_input(MC::Window&, MC::Mouse&, MC::Camera&); void setup_gl(); void fix_macos_render(MC::Window&); @@ -51,30 +53,37 @@ void run() { glViewport(0, 0, w, h); }); + auto image = MC::Image::PPMParser(MC::Assets::Images::atlas).parse(); + auto texture = MC::Texture(image); + MC::Mesh shape({ - {-0.5f, -0.5f, 0.5f}, - {0.5f, -0.5f, 0.5f}, - {0.5f, 0.5f, 0.5f}, - {-0.5f, 0.5f, 0.5f}, - - {-0.5f, -0.5f, -0.5f}, - {0.5f, -0.5f, -0.5f}, - {0.5f, 0.5f, -0.5f}, - {-0.5f, 0.5f, -0.5f} + {-1.0f, 1.0f, 1.0f}, {1.0f, 1.0f, 1.0f}, {1.0f, -1.0f, 1.0f}, {-1.0f, -1.0f, 1.0f}, + {-1.0f, 1.0f, -1.0f}, {1.0f, 1.0f, -1.0f}, {1.0f, -1.0f, -1.0f}, {-1.0f, -1.0f, -1.0f}, + + {-1.0f, 1.0f, -1.0f}, {-1.0f, 1.0f, 1.0f}, {-1.0f, -1.0f, 1.0f}, {-1.0f, -1.0f, -1.0f}, + {1.0f, 1.0f, 1.0f}, {1.0f, 1.0f, -1.0f}, {1.0f, -1.0f, -1.0f}, {1.0f, -1.0f, 1.0f}, + + {-1.0f, 1.0f, -1.0f}, {1.0f, 1.0f, -1.0f}, {1.0f, 1.0f, 1.0f}, {-1.0f, 1.0f, 1.0f}, + {-1.0f, -1.0f, 1.0f}, {1.0f, -1.0f, 1.0f}, {1.0f, -1.0f, -1.0f}, {-1.0f, -1.0f, -1.0f}, }, { - {-0.5f, -0.5f}, {0.5f, 0.5f}, - {-0.5f, 0.5f}, {0.5f, 0.5f}, - {0.5f, -0.5f}, {0.5f, 0.5f}, - {-0.5f, -0.5f}, {-0.5f, 0.5f} + {0.5f, 0.5f}, {0.0f, 0.5f}, {0.0f, 1.0f}, {0.5f, 1.0f}, + {0.5f, 0.5f}, {0.0f, 0.5f}, {0.0f, 1.0f}, {0.5f, 1.0f}, + + {0.5f, 0.5f}, {0.0f, 0.5f}, {0.0f, 1.0f}, {0.5f, 1.0f}, + {0.5f, 0.5f}, {0.0f, 0.5f}, {0.0f, 1.0f}, {0.5f, 1.0f}, + + {0.0f, 0.5f}, {0.5f, 0.5f}, {0.5f, 0.0f}, {0.0f, 0.0f}, + {0.5f, 0.5f}, {1.0f, 0.5f}, {1.0f, 0.0f}, {0.5f, 0.0f}, }, { - 0, 1, 2, 2, 3, 0, - 1, 5, 6, 6, 2, 1, - 7, 6, 5, 5, 4, 7, - 4, 0, 3, 3, 7, 4, - 4, 5, 1, 1, 0, 4, - 3, 2, 6, 6, 7, 3 + 0, 1, 3, 1, 2, 3, + 4, 5, 7, 5, 6, 7, + 8, 9, 11, 9, 10, 11, + 12, 13, 15, 13, 14, 15, + 16, 17, 19, 17, 18, 19, + 20, 21, 23, 21, 22, 23, + 24, 25, 27, 25, 26, 27, }); auto mesh = MC::Binder::load(shape); @@ -92,6 +101,9 @@ void run() { auto projection = Math::MVP::projection(ASPECT, FOV, 0.1f, 100.0f); projection_uniform.set(projection); + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LEQUAL); + uint64_t time = 0; while (!window.should_close()) { @@ -105,25 +117,27 @@ void run() { program.bind(); - auto angle = fmod(time / 10.0f, 360.0f); + auto angle = std::fmod(time / 10.0f, 360.0f); auto model = Math::MVP::model({0.0f, 0.0f, 0.0f}, {angle, angle, 0.0f}); model_uniform.set(model); auto view = Math::MVP::view(camera.position(), camera.angles()); view_uniform.set(view); - render(mesh); + render(mesh, texture); time++; } } -void render(MC::BindableMesh& mesh) { +void render(MC::BindableMesh& mesh, MC::Texture& texture) { glClearColor(0.85f, 0.85f, 0.85f, 1.0f); // #DBDBDB - glClear(GL_COLOR_BUFFER_BIT); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + texture.bind(); mesh.bind(); glDrawElements(GL_TRIANGLES, mesh.size(), GL_UNSIGNED_INT, 0); mesh.unbind(); + texture.unbind(); } void process_input(MC::Window& window, MC::Mouse& mouse, MC::Camera& camera) { |
