This commit is contained in:
Hartmut Seichter 2021-01-24 21:54:29 +01:00
parent 8f815a33ef
commit 42c9fac38e
9 changed files with 87 additions and 18 deletions

View file

@ -8,6 +8,7 @@
#include "pw/core/geometry.hpp" #include "pw/core/geometry.hpp"
#include "pw/core/image.hpp" #include "pw/core/image.hpp"
#include "pw/core/matrix_transform.hpp" #include "pw/core/matrix_transform.hpp"
#include "pw/core/rectangle.hpp"
#include "runtime_lua.hpp" #include "runtime_lua.hpp"
@ -18,11 +19,30 @@ template <> struct is_automagical<pw::matrix4x4> : std::false_type {};
template <> struct is_automagical<pw::vector3> : std::false_type {}; template <> struct is_automagical<pw::vector3> : std::false_type {};
template <> struct is_automagical<pw::vector4> : std::false_type {}; template <> struct is_automagical<pw::vector4> : std::false_type {};
template <> struct is_automagical<pw::quaternion> : std::false_type {}; template <> struct is_automagical<pw::quaternion> : std::false_type {};
template <> struct is_automagical<pw::rectangle> : std::false_type {};
} }
namespace pw { namespace pw {
void register_core_function(sol::state& lua,sol::table& ns)
rectangle from_lua_table(sol::table t)
{
float v[4];
for (std::size_t i {1}; i <= t.size();i++){
v[i-1] = t[i];
}
return rectangle(v);
}
vector3 vector3_from_lua_table(sol::table t)
{
return vector3();
}
void register_core_function(sol::state&,sol::table& ns)
{ {
typedef real_t Scalar; typedef real_t Scalar;
@ -93,8 +113,15 @@ void register_core_function(sol::state& lua,sol::table& ns)
, sol::constructors<size(),size(Scalar,Scalar)>() , sol::constructors<size(),size(Scalar,Scalar)>()
, "width",&size::width , "width",&size::width
, "height",&size::height , "height",&size::height
, "cast_to_float",&size::cast<float>
); );
ns.new_usertype<sizef>("sizef"
, sol::constructors<sizef(),sizef(Scalar,Scalar)>()
, "width",&sizef::width
, "height",&sizef::height
, "cast_to_int",&sizef::cast<int>
);
ns.new_usertype<point>("point", ns.new_usertype<point>("point",
sol::constructors<point(),point(Scalar,Scalar)>(), sol::constructors<point(),point(Scalar,Scalar)>(),
@ -154,6 +181,10 @@ void register_core_function(sol::state& lua,sol::table& ns)
,"gray", image::LUM); ,"gray", image::LUM);
ns.new_usertype<rectangle>("rectangle"
,sol::constructors<rectangle(),rectangle(const point_<float>&,const size_<float>&)>()
);
auto mathf_table = ns.create_named("mathf"); auto mathf_table = ns.create_named("mathf");
mathf_table.set_function("ping_pong",ping_pong<real_t>); mathf_table.set_function("ping_pong",ping_pong<real_t>);

View file

@ -45,6 +45,7 @@ void register_system_function(sol::state&, sol::table &ns)
,"new", sol::no_constructor ,"new", sol::no_constructor
,"get",&path::get ,"get",&path::get
,"separator",sol::readonly_property(&path::separator) ,"separator",sol::readonly_property(&path::separator)
,"resource_paths",sol::readonly_property(&path::resource_paths)
,"executable_path",sol::readonly_property(&path::executable_path) ,"executable_path",sol::readonly_property(&path::executable_path)
); );
} }

View file

@ -4,6 +4,7 @@
#include "pw/visual/shader.hpp" #include "pw/visual/shader.hpp"
#include "pw/visual/framebuffer.hpp" #include "pw/visual/framebuffer.hpp"
#include "pw/visual/texture.hpp" #include "pw/visual/texture.hpp"
#include "pw/visual/context.hpp"
#include "pw/core/size.hpp" #include "pw/core/size.hpp"
@ -62,6 +63,12 @@ void register_visual_function(sol::state&,sol::table &ns)
,sol::constructors<texture(),texture(texture::image_ref,texture::data_layout)>() ,sol::constructors<texture(),texture(texture::image_ref,texture::data_layout)>()
,"image",sol::property(&texture::set_image,&texture::image) ,"image",sol::property(&texture::set_image,&texture::image)
); );
ns.new_usertype<context>("context"
,sol::constructors<context()>()
,"clear",&context::clear
,"set_viewport",&context::set_viewport
);
} }
PW_REGISTER_LUA(visual) PW_REGISTER_LUA(visual)

View file

@ -20,8 +20,8 @@
* SOFTWARE. * SOFTWARE.
* *
*/ */
#ifndef PW_CORE_RECT_HPP #ifndef PW_CORE_RECTANGLE_HPP
#define PW_CORE_RECT_HPP #define PW_CORE_RECTANGLE_HPP
#include <initializer_list> #include <initializer_list>
@ -38,10 +38,16 @@ struct rectangle_ {
rectangle_() = default; rectangle_() = default;
rectangle_(const T_(&l)[4]) rectangle_(const T_ l[4])
: position(point_<T_>(l[0],l[1]))
, size(size_<T_>(l[2],l[3]))
{
}
rectangle_(const T_(&l)[4])
: position(point_<T_>(l[0],l[1]))
, size(size_<T_>(l[2],l[3]))
{ {
position.x = l[0]; position.y = l[1];
size.width = l[2]; size.height = l[3];
} }
rectangle_(point_<T_> const & p,size_<T_> const & s) : size(s), position(p) {} rectangle_(point_<T_> const & p,size_<T_> const & s) : size(s), position(p) {}
@ -53,11 +59,14 @@ struct rectangle_ {
} }
template <typename To_> template <typename To_>
rectangle_<To_> cast() const { return rectangle_<To_>(position.template cast<To_>(),size.template cast<To_>()); } rectangle_<To_> cast() const
{
return rectangle_<To_>(position.template cast<To_>(),size.template cast<To_>());
}
}; };
using rectangle = rectangle_<real_t>; using rectangle = rectangle_<real_t>;
using rectanglei = rectangle_<int>;
using rectanglef = rectangle_<float>; using rectanglef = rectangle_<float>;
using rectangled = rectangle_<double>; using rectangled = rectangle_<double>;

View file

@ -6,6 +6,8 @@
pw.script:load_all() pw.script:load_all()
print(pw.path.get().executable_path)
-- create a window (remember windows are invisible by default) -- create a window (remember windows are invisible by default)
local w = pw.window.new() local w = pw.window.new()
@ -90,16 +92,20 @@ end
local cam_pos = pw.vector3:new(0,0,0) local cam_pos = pw.vector3:new(0,0,0)
local ctx = pw.context:new()
w.on_resize = function(self) w.on_resize = function(self)
-- use client_size to resize the viewport -- use client_size to resize the viewport
print(self.client_size.width,self.client_size.height) ctx:set_viewport(pw.rectangle:new(pw.point:new(0,0),self.client_size:cast_to_float()))
end end
-- setup a lua callback function as callback -- setup a lua callback function as callback
w.on_update = function(self) w.on_update = function(self)
-- set identity on model matrix -- set identity on model matrix
mm:set_identity() mm:set_identity()
-- set view matrix with look_at -- set view matrix with look_at
mv = pw.matrixtransform.look_at(cam_pos,pw.vector3.forward(),pw.vector3.up()) mv = pw.matrixtransform.look_at(cam_pos,pw.vector3.forward(),pw.vector3.up())

View file

@ -1,5 +1,6 @@
set(hdrs set(hdrs
include/pw/visual/context.hpp
include/pw/visual/framebuffer.hpp include/pw/visual/framebuffer.hpp
include/pw/visual/shader.hpp include/pw/visual/shader.hpp
include/pw/visual/pipeline.hpp include/pw/visual/pipeline.hpp
@ -8,9 +9,9 @@ set(hdrs
) )
set(srcs set(srcs
src/context.cpp
src/framebuffer.cpp src/framebuffer.cpp
src/shader.cpp src/shader.cpp
src/context.cpp
src/pass.cpp src/pass.cpp
src/pipeline.cpp src/pipeline.cpp
src/target.cpp src/target.cpp

View file

@ -27,6 +27,7 @@
#include <pw/core/point.hpp> #include <pw/core/point.hpp>
#include <pw/core/size.hpp> #include <pw/core/size.hpp>
#include <pw/core/vector.hpp> #include <pw/core/vector.hpp>
#include <pw/core/rectangle.hpp>
namespace pw { namespace pw {
@ -40,7 +41,9 @@ public:
void set_blend(); void set_blend();
void set_depth(); void set_depth();
void set_viewport(point const &p, size const &s); void set_viewport(const rectangle& v);
rectangle viewport() const;
size viewport_size() const; size viewport_size() const;
point viewport_origin() const; point viewport_origin() const;

View file

@ -6,9 +6,17 @@
namespace pw { namespace pw {
struct context::impl { struct context::impl {
void set_viewport(const point &p, const size &s)
void set_viewport(const rectangle& v)
{ {
glViewport(p.x,p.y,s.width,s.height); glViewport(v.position.x,v.position.y,v.size.width,v.size.height);
}
const rectangle viewport()
{
float _viewport[4] = {0,0,1,1};
glGetFloatv(GL_VIEWPORT,&_viewport[0]);
return rectangle(_viewport);
} }
void clear() void clear()
@ -31,9 +39,14 @@ void context::set_blend()
} }
void context::set_viewport(const point &p, const size &s) void context::set_viewport(const rectangle& v)
{ {
_impl->set_viewport(p,s); _impl->set_viewport(v);
}
rectangle context::viewport() const
{
return _impl->viewport();
} }
void context::clear() void context::clear()

View file

@ -136,7 +136,6 @@ void texture::set_wrap(wrap_mode w)
_wrap = w; _wrap = w;
} }
uint32_t texture::native_handle() const uint32_t texture::native_handle() const
{ {
return _impl->_texture_id; return _impl->_texture_id;
@ -147,5 +146,4 @@ uint32_t texture::native_sampler_handle() const
return _impl->_texture_sampler; return _impl->_texture_sampler;
} }
} }