working texturing

This commit is contained in:
Hartmut Seichter 2021-01-30 21:23:16 +01:00
parent 0c8748befe
commit 2b71d39a9b
10 changed files with 90 additions and 76 deletions

View file

@ -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;

View file

@ -21,6 +21,7 @@ template <> struct is_automagical<pw::vector2> : 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::rectangle> : std::false_type {};
template <> struct is_automagical<pw::geometry> : std::false_type {};
}
namespace pw {
@ -113,8 +114,7 @@ void register_core_function(sol::state& lua,sol::table& ns)
,"matrix",&quaternion::to_matrix
);
ns.new_usertype<axisangle>
("axisangle",
ns.new_usertype<axisangle>("axisangle",
sol::constructors<axisangle(), axisangle(vector3,Scalar)>(),
"axis",&axisangle::axis,
"angle",&axisangle::angle,
@ -163,14 +163,16 @@ void register_core_function(sol::state& lua,sol::table& ns)
,"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);
// ,"texture_coordinates", sol::property(&geometry::ref_texture_coordinates,&geometry::ref_texture_coordinates)
,"compute_normals", &geometry::compute_normals
);
ns.new_enum<false>("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);
,"triangle_list", geometry::primitive_topology_type::triangle_list
);
ns.new_usertype<matrix_transform<real_t>>("matrixtransform"

View file

@ -6,7 +6,7 @@ namespace pw {
void register_io_function(sol::state&,sol::table& ns)
{
ns.new_usertype<image_io>("imageio"
ns.new_usertype<image_io>("image_io"
,"new", sol::no_constructor
,"get",&image_io::get
,"read",&image_io::read

View file

@ -78,6 +78,8 @@ public:
void add_texture_coordinates(vector2_array v);
const std::vector<vector2_array>& texture_coordinates() const { return _texture_coords;}
std::vector<vector2_array>& ref_texture_coordinates() { return _texture_coords; }
void transform(const matrix4x4& m);

View file

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

View file

@ -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<uint32_t>::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;

View file

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

View file

@ -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);
}
//

View file

@ -10,12 +10,19 @@ 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
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

View file

@ -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);