From 2b71d39a9b842ceb9c7e536c60ecdbca8e70e58d Mon Sep 17 00:00:00 2001 From: Hartmut Seichter Date: Sat, 30 Jan 2021 21:23:16 +0100 Subject: [PATCH] working texturing --- src/binding/src/script.cpp | 17 ----------- src/binding/src/script_core.cpp | 36 +++++++++++----------- src/binding/src/script_io.cpp | 2 +- src/core/include/pw/core/geometry.hpp | 2 ++ src/core/include/pw/core/image.hpp | 2 ++ src/core/src/image.cpp | 23 +++++++++++--- src/io/include/pw/io/image_io.hpp | 2 +- src/io/src/image_io.cpp | 14 ++++++--- src/scripts/demos/simple_003.lua | 44 ++++++++++++--------------- src/visual/src/texture.cpp | 24 ++++++++++----- 10 files changed, 90 insertions(+), 76 deletions(-) diff --git a/src/binding/src/script.cpp b/src/binding/src/script.cpp index 75cd034..085198f 100644 --- a/src/binding/src/script.cpp +++ b/src/binding/src/script.cpp @@ -15,23 +15,6 @@ PW_REGISTER_DECL_LUA(scene) PW_REGISTER_DECL_LUA(visual) -void static_example() -{ - - - - - // in SOL 2.20.6 - - - - // in SOL 3.2.2 - - - - -} - struct script::state { sol::state _state; diff --git a/src/binding/src/script_core.cpp b/src/binding/src/script_core.cpp index 5e6f4f6..06250a9 100644 --- a/src/binding/src/script_core.cpp +++ b/src/binding/src/script_core.cpp @@ -21,6 +21,7 @@ template <> struct is_automagical : std::false_type {}; template <> struct is_automagical : std::false_type {}; template <> struct is_automagical : std::false_type {}; template <> struct is_automagical : std::false_type {}; +template <> struct is_automagical : std::false_type {}; } namespace pw { @@ -91,11 +92,11 @@ void register_core_function(sol::state& lua,sol::table& ns) ns.new_usertype("vector4" ,sol::call_constructor,sol::constructors() ,"x", sol::property(sol::resolve(&vector4::x), [](vector4& v,vector4::value_type val){ v.x() = val;}) - ,"y", sol::property(sol::resolve(&vector4::y), [](vector4& v,vector4::value_type val){ v.y() = val;}) - ,"z", sol::property(sol::resolve(&vector4::z), [](vector4& v,vector4::value_type val){ v.z() = val;}) - ,"w", sol::property(sol::resolve(&vector4::w), [](vector4& v,vector4::value_type val){ v.w() = val;}) - ,"project",&vector4::project - ); + ,"y", sol::property(sol::resolve(&vector4::y), [](vector4& v,vector4::value_type val){ v.y() = val;}) + ,"z", sol::property(sol::resolve(&vector4::z), [](vector4& v,vector4::value_type val){ v.z() = val;}) + ,"w", sol::property(sol::resolve(&vector4::w), [](vector4& v,vector4::value_type val){ v.w() = val;}) + ,"project",&vector4::project + ); ns.new_usertype("quaternion" @@ -113,8 +114,7 @@ void register_core_function(sol::state& lua,sol::table& ns) ,"matrix",&quaternion::to_matrix ); - ns.new_usertype - ("axisangle", + ns.new_usertype("axisangle", sol::constructors(), "axis",&axisangle::axis, "angle",&axisangle::angle, @@ -159,18 +159,20 @@ void register_core_function(sol::state& lua,sol::table& ns) ); ns.new_usertype("geometry" - , sol::constructors() - , "primitive_topology", sol::property(&geometry::primitive_topology,&geometry::set_primitive_topology) - , "vertices", sol::property(&geometry::ref_vertices,&geometry::ref_vertices) - , "indices", sol::property(&geometry::ref_indices,&geometry::ref_indices) - , "add_texture_coordinates",&geometry::add_texture_coordinates - // , "texture_coordinates",&geometry::texture_coordinates - , "compute_normals", &geometry::compute_normals); + ,sol::constructors() + ,"primitive_topology", sol::property(&geometry::primitive_topology,&geometry::set_primitive_topology) + ,"vertices", sol::property(&geometry::ref_vertices,&geometry::ref_vertices) + ,"indices", sol::property(&geometry::ref_indices,&geometry::ref_indices) +// ,"texture_coordinates", sol::property(&geometry::ref_texture_coordinates,&geometry::ref_texture_coordinates) + ,"compute_normals", &geometry::compute_normals + ); + ns.new_enum("primitive_topology_type" - ,"point_list", geometry::primitive_topology_type::point_list - ,"line_list", geometry::primitive_topology_type::line_list - ,"triangle_list", geometry::primitive_topology_type::triangle_list); + ,"point_list", geometry::primitive_topology_type::point_list + ,"line_list", geometry::primitive_topology_type::line_list + ,"triangle_list", geometry::primitive_topology_type::triangle_list + ); ns.new_usertype>("matrixtransform" diff --git a/src/binding/src/script_io.cpp b/src/binding/src/script_io.cpp index 2cc5a23..557eeb2 100644 --- a/src/binding/src/script_io.cpp +++ b/src/binding/src/script_io.cpp @@ -6,7 +6,7 @@ namespace pw { void register_io_function(sol::state&,sol::table& ns) { - ns.new_usertype("imageio" + ns.new_usertype("image_io" ,"new", sol::no_constructor ,"get",&image_io::get ,"read",&image_io::read diff --git a/src/core/include/pw/core/geometry.hpp b/src/core/include/pw/core/geometry.hpp index 4dd420e..739a785 100644 --- a/src/core/include/pw/core/geometry.hpp +++ b/src/core/include/pw/core/geometry.hpp @@ -78,6 +78,8 @@ public: void add_texture_coordinates(vector2_array v); const std::vector& texture_coordinates() const { return _texture_coords;} + std::vector& ref_texture_coordinates() { return _texture_coords; } + void transform(const matrix4x4& m); diff --git a/src/core/include/pw/core/image.hpp b/src/core/include/pw/core/image.hpp index 002cda8..29664d2 100644 --- a/src/core/include/pw/core/image.hpp +++ b/src/core/include/pw/core/image.hpp @@ -63,6 +63,8 @@ public: static uint32_t bytes_per_pixel(pixel_layout t); + static uint32_t components(pixel_layout t); + ::pw::size size() const; void generate_noise(); diff --git a/src/core/src/image.cpp b/src/core/src/image.cpp index 43dabd8..b0706aa 100644 --- a/src/core/src/image.cpp +++ b/src/core/src/image.cpp @@ -57,13 +57,11 @@ uint32_t image::bytes_per_pixel(image::pixel_layout t) { switch (t) { case pw::image::RGB8: - return 3 * sizeof(data_t); case pw::image::RGBA8: - return 4 * sizeof(data_t); case pw::image::LUM: - return 1 * sizeof(data_t); + return components(t) * sizeof(data_t); case pw::image::DEPTH: - return 1 * sizeof(float); + return components(t) * sizeof(float); default: debug::w() << __PRETTY_FUNCTION__ << " unhandled pixel_layout"; } @@ -71,6 +69,23 @@ uint32_t image::bytes_per_pixel(image::pixel_layout t) return std::numeric_limits::max(); } +uint32_t image::components(image::pixel_layout t) +{ + switch (t) { + case pw::image::RGB8: + return 3; // * sizeof(data_t); + case pw::image::RGBA8: + return 4; // * sizeof(data_t); + case pw::image::LUM: + return 1; // * sizeof(data_t); + case pw::image::DEPTH: + return 1; // * sizeof(float); + default: + debug::w() << __PRETTY_FUNCTION__ << " unhandled pixel_layout"; + } + return 0; +} + ::pw::size image::size() const { return _size; diff --git a/src/io/include/pw/io/image_io.hpp b/src/io/include/pw/io/image_io.hpp index 53e1df4..f1646e1 100644 --- a/src/io/include/pw/io/image_io.hpp +++ b/src/io/include/pw/io/image_io.hpp @@ -35,7 +35,7 @@ public: image read(std::string const& uri,uint32_t flags = 0); - bool write(const std::string& uri,uint32_t flags = 0); + bool write(const std::string &uri, const image &img, uint32_t flags = 0); ~image_io(); diff --git a/src/io/src/image_io.cpp b/src/io/src/image_io.cpp index 00c15cf..e1052aa 100644 --- a/src/io/src/image_io.cpp +++ b/src/io/src/image_io.cpp @@ -17,7 +17,7 @@ struct image_io::impl image read_impl(const std::string& uri,uint32_t flags) { - int x,y,n; + int x{ 0 }, y{ 0 }, n{ 0 }; auto data = stbi_load(uri.c_str(), &x, &y, &n, 4); if (data) { @@ -37,10 +37,14 @@ struct image_io::impl return image(); } - bool write(const std::string& uri, uint32_t flags) + bool write(const std::string& uri, const image& img, uint32_t flags) { + auto res = stbi_write_png(uri.c_str(),img.size().width,img.size().height, + image::components(img.layout()), + img.data(), + img.size().width * image::components(img.layout())); - return false; + return 0 == res; } }; @@ -57,9 +61,9 @@ image image_io::read(const std::string &uri, uint32_t flags) return _impl->read_impl(uri,flags); } -bool image_io::write(const std::string &uri, uint32_t flags) +bool image_io::write(const std::string &uri, const image& img, uint32_t flags) { - return _impl->write(uri,flags); + return _impl->write(uri,img,flags); } // diff --git a/src/scripts/demos/simple_003.lua b/src/scripts/demos/simple_003.lua index 705b170..3851344 100644 --- a/src/scripts/demos/simple_003.lua +++ b/src/scripts/demos/simple_003.lua @@ -10,11 +10,18 @@ print("resource path:",pw.path.get().resource_path) local img = pw.image.new() if not img:create(pw.size(64,64),pw.pixel_layout.rgb8) then - print("image couldnt be created") -end --- generate some noise -img:generate_noise() + print("image couldnt be created") + +else + + -- generate some noise + img:generate_noise() + + -- for debugging + pw.image_io.get():write("test.png",img,0) + +end -- create a window (remember windows are invisible by default) local w = pw.window.new() @@ -38,7 +45,7 @@ local tc = { pw.vector2(1.0,1.0), pw.vector2(0.0,1.0), pw.vector2(0.0,0.0) } -g:add_texture_coordinates(tc) +--g:add_texture_coordinates(tc) --print(g) @@ -53,6 +60,10 @@ g.vertices:add(pw.vector3(-s,-s, z)) -- 1 g.vertices:add(pw.vector3( s,-s, z)) -- 2 g.vertices:add(pw.vector3( s, s, z)) -- 3 +-- 0 --- 3 +-- | \ | +-- 1 --- 2 + g.indices:add(0) g.indices:add(1) g.indices:add(2) @@ -62,21 +73,6 @@ g.indices:add(3) g.indices:add(0) -print(#g.vertices,g.vertices,g.vertices[1].x,g.vertices[1].y,g.vertices[1].z) ---print(g.vertices[1]) - -local v3 = pw.vector3() - -print(g.indices,#g.indices) -print(g.vertices,#g.vertices) - ---os.exit() - --- 0 --- 3 --- | \ | --- 1 --- 2 - - local mm = pw.matrix4x4.identity local mv = pw.matrix4x4() @@ -91,6 +87,7 @@ uniform mat4 view; uniform mat4 projection; in vec3 vertex_p; +in vec2 texture_coord; void main() { gl_Position = projection * view * model * vec4(vertex_p, 1.0); @@ -105,7 +102,7 @@ uniform sampler2D tex_color; out vec4 frag_color; void main() { - frag_color = color * texture(tex_color,vec2(1.0,1.0)); + frag_color = texture(tex_color,vec2(1.0,1.0)) * color; } ]]) @@ -114,7 +111,6 @@ if not s:build() then print("Error!") end - -- the renderer for a geometry local renderer = pw.renderer:new() @@ -172,7 +168,7 @@ w.on_update = function(self) s:set_uniform_mat4("projection",mp) s:set_uniform_vec4("color",cl) - s:set_uniform_texture("tex_color",tx) + s:set_uniform_int("tex_color",0) tx:bind() @@ -180,7 +176,7 @@ w.on_update = function(self) -- draw renderer:draw() - tx:unbind() +-- tx:unbind() local e = ctx:get_error() if e ~= 0 then diff --git a/src/visual/src/texture.cpp b/src/visual/src/texture.cpp index eb331ea..77d6167 100644 --- a/src/visual/src/texture.cpp +++ b/src/visual/src/texture.cpp @@ -43,22 +43,30 @@ struct texture::impl { void update(const class image& i) { +// debug::d() << __PRETTY_FUNCTION__; if (_change_count != i.change_count()) { this->bind(); glPixelStorei(GL_UNPACK_ALIGNMENT,1); - glTexSubImage2D(GL_TEXTURE_2D,0,0,0, - i.size().width,i.size().height, - GL_RGB,GL_UNSIGNED_BYTE, - i.data()); + + glTexSubImage2D(GL_TEXTURE_2D, // target + 0, // level + 0,0, // x/y offset + i.size().width,i.size().height, // width,height + GL_RGB, // format + GL_UNSIGNED_BYTE, // type + i.data() + ); + + +// this->unbind(); auto error = glGetError(); if (error != GL_NO_ERROR) { debug::e() << __PRETTY_FUNCTION__ << " " << __LINE__ << " GL error: " << error; } - this->unbind(); _change_count = i.change_count(); } @@ -87,8 +95,10 @@ struct texture::impl { glTexImage2D(GL_TEXTURE_2D,0,format, i.size().width,i.size().height, - 0,format,GL_UNSIGNED_BYTE, - i.data() + 0, + format, + GL_UNSIGNED_BYTE, + nullptr ); glGenerateMipmap(GL_TEXTURE_2D);