refactoring and cleaning while searching for a problem in the demo code

This commit is contained in:
Hartmut Seichter 2021-01-09 21:40:23 +01:00
parent 3471196830
commit 52c077af6c
7 changed files with 47 additions and 22 deletions

View file

@ -1,8 +1,6 @@
#include "pw/io/image_io.hpp"
#include "runtime_lua.hpp"
namespace pw {

View file

@ -3,8 +3,11 @@
#include "pw/visual/pipeline.hpp"
#include "pw/visual/shader.hpp"
#include "pw/visual/framebuffer.hpp"
#include "pw/visual/texture.hpp"
#include "pw/core/size.hpp"
#include "runtime_lua.hpp"
namespace pw {
@ -17,8 +20,6 @@ void register_visual_function(sol::state&,sol::table &ns)
,"draw",&pipeline::draw
);
ns.new_usertype<shader>("shader"
,sol::constructors<shader()>()
,"ready",sol::readonly_property(&shader::ready)
@ -59,6 +60,12 @@ void register_visual_function(sol::state&,sol::table &ns)
,"bind",&framebuffer::bind
,"unbind",&framebuffer::unbind
,"blit",&framebuffer::blit);
ns.new_usertype<texture>("texture"
,sol::constructors<texture(),texture(texture::image_ref,texture::data_layout)>()
,"image",sol::property(&texture::set_image,&texture::image)
,"type",sol::property(&texture::set_type,&texture::type)
);
}
PW_REGISTER_LUA(visual)

View file

@ -108,12 +108,14 @@ w.on_update = function(self)
-- just some toying around
local color_red = pw.mathf.ping_pong(pw.time.now,1.0)
local color_green = pw.mathf.ping_pong(pw.time.now + 1,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 )
local cl = pw.vector4:new( color_red, color_green, color_blue, 1.0 )
-- bind the shader
s:use()
-- update the unifors, currently the slow path
s:set_uniform_mat4("model",mm)
s:set_uniform_mat4("view",mv)
s:set_uniform_mat4("projection",mp)

View file

@ -6,6 +6,8 @@
#include <pw/core/vector.hpp>
#include <pw/core/debug.hpp>
#include <pw/visual/texture.hpp>
#include <map>
#include <variant>
@ -32,6 +34,7 @@ public:
shader& set_uniform_at_location(int location,float v);
shader& set_uniform_at_location(int location,matrix4x4f const &v);
shader& set_uniform_at_location(int location,vector4f const &v);
shader& set_uniform_at_location(int location,texture const &t);
/**
* @brief retrieves the position of a uniform

View file

@ -33,12 +33,15 @@ public:
wrap_clamp_to_repeat
};
texture();
using image_ref = shared_ptr<::pw::image>;
texture(shared_ptr<image> i,data_layout s,data_type = data_type::color);
texture();
texture(image_ref i,data_layout s,data_type = data_type::color);
~texture();
void set_image(shared_ptr<image> i);
shared_ptr<image> get() const { return _image; }
void set_image(image_ref i);
image_ref image() const { return _image; }
void set_type(data_type t);
data_type type() const { return _type; }
@ -52,15 +55,15 @@ public:
void generate_mipmaps();
unsigned int native_handle() const;
unsigned int native_sampler_handle() const;
uint32_t native_handle() const;
uint32_t native_sampler_handle() const;
protected:
shared_ptr<image> _image;
image_ref _image;
data_type _type;
data_layout _shape;
wrap_mode _wrap;
data_type _type = data_type::color;
data_layout _shape = data_layout::shape_2d;
wrap_mode _wrap = wrap_mode::wrap_clamp_to_edge;
struct impl;
unique_ptr<impl> _impl;

View file

@ -12,6 +12,7 @@
#include "pw/visual/renderer.hpp"
#include "pw/visual/material.hpp"
#include "pw/visual/framebuffer.hpp"
#include "pw/visual/texture.hpp"
#include "glad/glad.h"
@ -53,6 +54,7 @@ struct triangle_renderer
{
texture tex;
shader shader_p;
geometry amesh;
renderer r;

View file

@ -1,6 +1,6 @@
#include "pw/visual/texture.hpp"
#include <glad/glad.h>
#include "glad/glad.h"
namespace pw {
@ -19,6 +19,11 @@ struct texture::impl {
{
}
~impl()
{
destroy();
}
GLuint gl_shape() {
switch (_host.shape()) {
case data_layout::shape_1d:
@ -76,19 +81,24 @@ struct texture::impl {
//
texture::texture()
: _impl { make_unique<texture::impl>(*this) }
{
_impl = make_unique<impl>(*this);
}
texture::texture(shared_ptr<image> i, texture::data_layout s, texture::data_type t)
texture::texture(image_ref i, texture::data_layout s, texture::data_type t)
{
texture();
set_image(i);
set_shape(s);
set_type(t);
set_type(t);
}
void texture::set_image(shared_ptr<image> i)
texture::~texture()
{
}
void texture::set_image(image_ref i)
{
_image = i;
}
@ -113,12 +123,12 @@ void texture::generate_mipmaps()
_impl->generate_mipmaps();
}
unsigned int texture::native_handle() const
uint32_t texture::native_handle() const
{
return _impl->_texture_id;
}
unsigned int texture::native_sampler_handle() const
uint32_t texture::native_sampler_handle() const
{
return _impl->_texture_sampler;
}