working port from C++ to Lua with very basic shader and geometry functionality in place

This commit is contained in:
Hartmut Seichter 2021-01-09 14:17:10 +01:00
parent 0d2138c9f2
commit 3471196830
8 changed files with 154 additions and 20 deletions

View file

@ -154,6 +154,9 @@ void register_core_function(sol::state& lua,sol::table& ns)
,"gray", image::LUM); ,"gray", image::LUM);
auto mathf_table = ns.create_named("mathf");
mathf_table.set_function("ping_pong",ping_pong<real_t>);
} }

View file

@ -16,6 +16,7 @@ void register_system_function(sol::state&, sol::table &ns)
ns.new_usertype<window>("window", ns.new_usertype<window>("window",
"update",&window::update, "update",&window::update,
"on_update",sol::writeonly_property(&window::set_on_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), "title",sol::writeonly_property(&window::set_title),
"size",sol::property(&window::size,&window::set_size), "size",sol::property(&window::size,&window::set_size),
"client_size",sol::readonly_property(&window::client_size), "client_size",sol::readonly_property(&window::client_size),

View file

@ -23,12 +23,13 @@ g.topology = pw.geometry.triangles -- meh
g.vertices:clear() 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(-s, s, z)) -- 0
g.vertices:add(pw.vector3:new(-1,-1, z)) -- 1 g.vertices:add(pw.vector3:new(-s,-s, z)) -- 1
g.vertices:add(pw.vector3:new( 1,-1, z)) -- 2 g.vertices:add(pw.vector3:new( s,-s, z)) -- 2
g.vertices:add(pw.vector3:new( 1, 1, z)) -- 3 g.vertices:add(pw.vector3:new( s, s, z)) -- 3
-- 0 --- 3 -- 0 --- 3
-- | \ | -- | \ |
@ -84,16 +85,33 @@ if not renderer:create(g) then
print("couldn't create renderer") print("couldn't create renderer")
end 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 -- setup a lua callback function as callback
w.on_update = function(self) w.on_update = function(self)
-- set identity on model matrix
mm:set_identity() 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()) -- compute aspect ratio from canvas size
mp = pw.matrixtransform.perspective_projection(1.3,1.0,0.2,100) 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:use()
s:set_uniform_mat4("model",mm) s:set_uniform_mat4("model",mm)
@ -101,26 +119,45 @@ w.on_update = function(self)
s:set_uniform_mat4("projection",mp) s:set_uniform_mat4("projection",mp)
s:set_uniform_vec4("color",cl) s:set_uniform_vec4("color",cl)
-- draw
renderer:draw() renderer:draw()
end end
-- before entering the update loop make the window visible
w.visible = true w.visible = true
-- main update loop
while w:update() do while w:update() do
-- somehow works -- somehow works
if (pw.input.get().input_string == 'f') then if pw.input.get().input_string == 'f' then
w.fullscreen = not w.fullscreen w.fullscreen = not w.fullscreen
end end
-- keycode for quit -- keycode for quit
if (pw.input.get().input_string == 'q') then if pw.input.get().input_string == 'q' then
break; break;
end 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 -- just to check
if (pw.input:get().mouse_button == 1) then if pw.input:get().mouse_button == 1 then
print("elapsed",t.elapsed) print("elapsed",t.elapsed)
t:reset() t:reset()

View file

@ -12,7 +12,8 @@ namespace pw {
class window { class window {
public: public:
typedef std::function<void(window&)> on_update_t; using on_update_t = std::function<void(window&)> ;
using on_resize_t = std::function<void(window&)> ;
window(); window();
~window(); ~window();
@ -34,6 +35,7 @@ public:
void set_fullscreen(bool use_fullscreen); void set_fullscreen(bool use_fullscreen);
void set_on_update(on_update_t f) { _on_update = f; } 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; bool visible() const;
void set_visible(bool is_visible); void set_visible(bool is_visible);
@ -43,6 +45,7 @@ protected:
std::unique_ptr<impl> _impl; std::unique_ptr<impl> _impl;
on_update_t _on_update; on_update_t _on_update;
on_resize_t _on_resize;
}; };

View file

@ -104,13 +104,14 @@ struct window::impl {
static void framebuffer_size_callback(GLFWwindow* window, int width, int height) static void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{ {
window::impl* impl = static_cast<window::impl*>(glfwGetWindowUserPointer(window)); window::impl* impl = static_cast<window::impl*>(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); // impl->on_resize(width,height);
// std::cout << "framebuffer " << width << "x" << height << std::endl; // std::cout << "framebuffer " << width << "x" << height << std::endl;
// glViewport(0, 0, width, height); // glViewport(0, 0, width, height);
} }
void update_display_list() void update_display_list()
@ -190,7 +191,7 @@ struct window::impl {
glfwSetWindowUserPointer(_window,this); glfwSetWindowUserPointer(_window,this);
//glfwSetFramebufferSizeCallback(_window, window::impl::framebuffer_size_callback); glfwSetFramebufferSizeCallback(_window, window::impl::framebuffer_size_callback);
glfwSetKeyCallback(_window, window::impl::key_callback); glfwSetKeyCallback(_window, window::impl::key_callback);
// glfwSetCharCallback(_window, character_callback); // glfwSetCharCallback(_window, character_callback);
glfwSetCharModsCallback(_window, charmods_callback); glfwSetCharModsCallback(_window, charmods_callback);
@ -314,6 +315,7 @@ struct window::impl {
window::window() window::window()
: _impl(std::make_unique<window::impl>(*this)) : _impl(std::make_unique<window::impl>(*this))
, _on_update([](window&){}) , _on_update([](window&){})
, _on_resize([](window&){})
{ {
} }

View file

@ -24,21 +24,47 @@
#ifndef PW_VISUAL_CONTEXT_HPP #ifndef PW_VISUAL_CONTEXT_HPP
#define PW_VISUAL_CONTEXT_HPP #define PW_VISUAL_CONTEXT_HPP
#include <pw/core/point.hpp>
#include <pw/core/size.hpp> #include <pw/core/size.hpp>
#include <pw/core/vector.hpp>
namespace pw { namespace pw {
class context { class context {
public: public:
virtual bool make_current() = 0; context();
virtual void resize() = 0; ~context();
virtual ::pw::size size() = 0;
virtual void flush() = 0;
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> _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 #endif

View file

@ -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<impl>())
{
}
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();
}
}

View file

@ -162,4 +162,20 @@ void renderer::draw()
} }
//class viewport {
// void set(point p,size s);
//protected:
// struct _impl;
// std::unique_ptr<_impl>;
//};
} }