From 347119683009cddc0fdc4290f1f17aa2bc2c4248 Mon Sep 17 00:00:00 2001 From: Hartmut Seichter Date: Sat, 9 Jan 2021 14:17:10 +0100 Subject: [PATCH] working port from C++ to Lua with very basic shader and geometry functionality in place --- src/binding/src/script_core.cpp | 3 ++ src/binding/src/script_system.cpp | 1 + src/scripts/demos/simple_002.lua | 59 +++++++++++++++++++----- src/system/include/pw/system/window.hpp | 5 +- src/system/src/window.cpp | 8 ++-- src/visual/include/pw/visual/context.hpp | 36 +++++++++++++-- src/visual/src/context.cpp | 46 ++++++++++++++++++ src/visual/src/renderer.cpp | 16 +++++++ 8 files changed, 154 insertions(+), 20 deletions(-) diff --git a/src/binding/src/script_core.cpp b/src/binding/src/script_core.cpp index cfb4ba6..ca4dde8 100644 --- a/src/binding/src/script_core.cpp +++ b/src/binding/src/script_core.cpp @@ -154,6 +154,9 @@ void register_core_function(sol::state& lua,sol::table& ns) ,"gray", image::LUM); + auto mathf_table = ns.create_named("mathf"); + mathf_table.set_function("ping_pong",ping_pong); + } diff --git a/src/binding/src/script_system.cpp b/src/binding/src/script_system.cpp index a291142..e131459 100644 --- a/src/binding/src/script_system.cpp +++ b/src/binding/src/script_system.cpp @@ -16,6 +16,7 @@ void register_system_function(sol::state&, sol::table &ns) ns.new_usertype("window", "update",&window::update, "on_update",sol::writeonly_property(&window::set_on_update), + "on_resize",sol::writeonly_property(&window::set_on_resize), "title",sol::writeonly_property(&window::set_title), "size",sol::property(&window::size,&window::set_size), "client_size",sol::readonly_property(&window::client_size), diff --git a/src/scripts/demos/simple_002.lua b/src/scripts/demos/simple_002.lua index 99794ca..7f5de29 100644 --- a/src/scripts/demos/simple_002.lua +++ b/src/scripts/demos/simple_002.lua @@ -23,12 +23,13 @@ g.topology = pw.geometry.triangles -- meh g.vertices:clear() -z = 1.0 +z = -5.0 +s = 2 -g.vertices:add(pw.vector3:new(-1, 1, z)) -- 0 -g.vertices:add(pw.vector3:new(-1,-1, z)) -- 1 -g.vertices:add(pw.vector3:new( 1,-1, z)) -- 2 -g.vertices:add(pw.vector3:new( 1, 1, z)) -- 3 +g.vertices:add(pw.vector3:new(-s, s, z)) -- 0 +g.vertices:add(pw.vector3:new(-s,-s, z)) -- 1 +g.vertices:add(pw.vector3:new( s,-s, z)) -- 2 +g.vertices:add(pw.vector3:new( s, s, z)) -- 3 -- 0 --- 3 -- | \ | @@ -84,16 +85,33 @@ if not renderer:create(g) then print("couldn't create renderer") end +local cam_z = 0 +local cam_x = 0 + +w.on_resize = function(self) + print(self.client_size.width,self.client_size.height) +end + -- setup a lua callback function as callback w.on_update = function(self) + -- set identity on model matrix mm:set_identity() + -- set view matrix with look_at + mv = pw.matrixtransform.look_at(pw.vector3:new(cam_x,0,cam_z),pw.vector3.forward(),pw.vector3.up()) - mv = pw.matrixtransform.look_at(pw.vector3:new(0,0,0),pw.vector3.forward(),pw.vector3.up()) - mp = pw.matrixtransform.perspective_projection(1.3,1.0,0.2,100) + -- compute aspect ratio from canvas size + aspect_ratio = self.client_size.width / self.client_size.height - local cl = pw.vector4:new( 1.0, 0.0, 0.0, 1.0 ) + -- create a view frustom for a perspective projection + mp = pw.matrixtransform.perspective_projection(1.3,aspect_ratio,0.2,100) + -- just some toying around + local color_red = pw.mathf.ping_pong(pw.time.now,1.0) + local color_blue = 1.0 - pw.mathf.ping_pong(pw.time.now,1.0) + local cl = pw.vector4:new( color_red, 0.0, color_blue, 1.0 ) + + -- bind the shader s:use() s:set_uniform_mat4("model",mm) @@ -101,26 +119,45 @@ w.on_update = function(self) s:set_uniform_mat4("projection",mp) s:set_uniform_vec4("color",cl) + -- draw renderer:draw() end +-- before entering the update loop make the window visible w.visible = true +-- main update loop while w:update() do -- somehow works - if (pw.input.get().input_string == 'f') then + if pw.input.get().input_string == 'f' then w.fullscreen = not w.fullscreen end -- keycode for quit - if (pw.input.get().input_string == 'q') then + if pw.input.get().input_string == 'q' then break; end + -- just to quickly modify speed + local move_step = 0.05 + + -- camera + if pw.input.get().input_string == 'w' then + cam_z = cam_z - move_step + elseif pw.input.get().input_string == 's' then + cam_z = cam_z + move_step + print(cam_z) + elseif pw.input.get().input_string == 'a' then + cam_x = cam_x - move_step + elseif pw.input.get().input_string == 'd' then + cam_x = cam_x + move_step + print(cam_x) + end + -- just to check - if (pw.input:get().mouse_button == 1) then + if pw.input:get().mouse_button == 1 then print("elapsed",t.elapsed) t:reset() diff --git a/src/system/include/pw/system/window.hpp b/src/system/include/pw/system/window.hpp index 926ec53..cc55bb3 100644 --- a/src/system/include/pw/system/window.hpp +++ b/src/system/include/pw/system/window.hpp @@ -12,7 +12,8 @@ namespace pw { class window { public: - typedef std::function on_update_t; + using on_update_t = std::function ; + using on_resize_t = std::function ; window(); ~window(); @@ -34,6 +35,7 @@ public: void set_fullscreen(bool use_fullscreen); void set_on_update(on_update_t f) { _on_update = f; } + void set_on_resize(on_resize_t f) { _on_resize = f; } bool visible() const; void set_visible(bool is_visible); @@ -43,6 +45,7 @@ protected: std::unique_ptr _impl; on_update_t _on_update; + on_resize_t _on_resize; }; diff --git a/src/system/src/window.cpp b/src/system/src/window.cpp index 89230ac..734db9e 100644 --- a/src/system/src/window.cpp +++ b/src/system/src/window.cpp @@ -104,13 +104,14 @@ struct window::impl { static void framebuffer_size_callback(GLFWwindow* window, int width, int height) { window::impl* impl = static_cast(glfwGetWindowUserPointer(window)); + impl->_parent._on_resize(impl->_parent); - debug::d() << "window::frame_buffer_size_callback " << width << "x" << height; +// debug::d() << "window::frame_buffer_size_callback " << width << "x" << height; // impl->on_resize(width,height); // std::cout << "framebuffer " << width << "x" << height << std::endl; - // glViewport(0, 0, width, height); +// glViewport(0, 0, width, height); } void update_display_list() @@ -190,7 +191,7 @@ struct window::impl { glfwSetWindowUserPointer(_window,this); - //glfwSetFramebufferSizeCallback(_window, window::impl::framebuffer_size_callback); + glfwSetFramebufferSizeCallback(_window, window::impl::framebuffer_size_callback); glfwSetKeyCallback(_window, window::impl::key_callback); // glfwSetCharCallback(_window, character_callback); glfwSetCharModsCallback(_window, charmods_callback); @@ -314,6 +315,7 @@ struct window::impl { window::window() : _impl(std::make_unique(*this)) , _on_update([](window&){}) + , _on_resize([](window&){}) { } diff --git a/src/visual/include/pw/visual/context.hpp b/src/visual/include/pw/visual/context.hpp index c64fae0..6d5c9eb 100644 --- a/src/visual/include/pw/visual/context.hpp +++ b/src/visual/include/pw/visual/context.hpp @@ -24,21 +24,47 @@ #ifndef PW_VISUAL_CONTEXT_HPP #define PW_VISUAL_CONTEXT_HPP +#include #include +#include + namespace pw { class context { public: - virtual bool make_current() = 0; - virtual void resize() = 0; - virtual ::pw::size size() = 0; - virtual void flush() = 0; + context(); + ~context(); - virtual ~context() = default; + void set_blend(); + void set_depth(); + + void set_viewport(point const &p, size const &s); + size viewport_size() const; + point viewport_origin() const; + + void set_clearcolor(vector4f const& c); + + void clear(); + +protected: + struct impl; + std::unique_ptr _impl; }; + +//class context { +//public: + +// virtual bool make_current() = 0; +// virtual void resize() = 0; +// virtual ::pw::size size() = 0; +// virtual void flush() = 0; + +// virtual ~context() = default; +//}; + } #endif diff --git a/src/visual/src/context.cpp b/src/visual/src/context.cpp index e69de29..b816aea 100644 --- a/src/visual/src/context.cpp +++ b/src/visual/src/context.cpp @@ -0,0 +1,46 @@ +#include "pw/visual/context.hpp" + +#include "glad/glad.h" + + +namespace pw { + +struct context::impl { + void set_viewport(const point &p, const size &s) + { + glViewport(p.x,p.y,s.width,s.height); + } + + void clear() + { + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + } +}; + +context::context() + : _impl(std::make_unique()) +{ +} + +context::~context() +{ +} + +void context::set_blend() +{ + +} + +void context::set_viewport(const point &p, const size &s) +{ + _impl->set_viewport(p,s); +} + +void context::clear() +{ + _impl->clear(); +} + +} + + diff --git a/src/visual/src/renderer.cpp b/src/visual/src/renderer.cpp index 49403e8..0e7ea88 100644 --- a/src/visual/src/renderer.cpp +++ b/src/visual/src/renderer.cpp @@ -162,4 +162,20 @@ void renderer::draw() } +//class viewport { + + + +// void set(point p,size s); + + + +//protected: + +// struct _impl; +// std::unique_ptr<_impl>; + +//}; + + }