working texturing
This commit is contained in:
parent
0c8748befe
commit
2b71d39a9b
10 changed files with 90 additions and 76 deletions
|
@ -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;
|
||||
|
|
|
@ -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 {
|
||||
|
@ -91,11 +92,11 @@ void register_core_function(sol::state& lua,sol::table& ns)
|
|||
ns.new_usertype<vector4>("vector4"
|
||||
,sol::call_constructor,sol::constructors<vector4(),vector4(vector4::value_type,vector4::value_type,vector4::value_type,vector4::value_type)>()
|
||||
,"x", sol::property(sol::resolve<const vector4::value_type&() const>(&vector4::x), [](vector4& v,vector4::value_type val){ v.x() = val;})
|
||||
,"y", sol::property(sol::resolve<const vector4::value_type&() const>(&vector4::y), [](vector4& v,vector4::value_type val){ v.y() = val;})
|
||||
,"z", sol::property(sol::resolve<const vector4::value_type&() const>(&vector4::z), [](vector4& v,vector4::value_type val){ v.z() = val;})
|
||||
,"w", sol::property(sol::resolve<const vector4::value_type&() const>(&vector4::w), [](vector4& v,vector4::value_type val){ v.w() = val;})
|
||||
,"project",&vector4::project
|
||||
);
|
||||
,"y", sol::property(sol::resolve<const vector4::value_type&() const>(&vector4::y), [](vector4& v,vector4::value_type val){ v.y() = val;})
|
||||
,"z", sol::property(sol::resolve<const vector4::value_type&() const>(&vector4::z), [](vector4& v,vector4::value_type val){ v.z() = val;})
|
||||
,"w", sol::property(sol::resolve<const vector4::value_type&() const>(&vector4::w), [](vector4& v,vector4::value_type val){ v.w() = val;})
|
||||
,"project",&vector4::project
|
||||
);
|
||||
|
||||
|
||||
ns.new_usertype<quaternion>("quaternion"
|
||||
|
@ -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,
|
||||
|
@ -159,18 +159,20 @@ void register_core_function(sol::state& lua,sol::table& ns)
|
|||
);
|
||||
|
||||
ns.new_usertype<geometry>("geometry"
|
||||
, sol::constructors<geometry(),geometry(geometry::primitive_topology_type,vector3_array,geometry::indices_t)>()
|
||||
, "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);
|
||||
,sol::constructors<geometry(),geometry(geometry::primitive_topology_type,vector3_array,geometry::indices_t)>()
|
||||
,"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)
|
||||
// ,"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);
|
||||
,"point_list", geometry::primitive_topology_type::point_list
|
||||
,"line_list", geometry::primitive_topology_type::line_list
|
||||
,"triangle_list", geometry::primitive_topology_type::triangle_list
|
||||
);
|
||||
|
||||
|
||||
ns.new_usertype<matrix_transform<real_t>>("matrixtransform"
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
//
|
||||
|
|
|
@ -10,11 +10,18 @@ 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
|
||||
|
||||
-- generate some noise
|
||||
img:generate_noise()
|
||||
print("image couldnt be created")
|
||||
|
||||
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
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue