From 753f51453f1b87615218fa0ee3dc5aead56e52e7 Mon Sep 17 00:00:00 2001 From: Hartmut Seichter Date: Sat, 23 Jan 2021 14:21:08 +0100 Subject: [PATCH] refactoring of texture code into a 2D version first --- src/binding/src/script_system.cpp | 1 + src/scripts/demos/simple_002.lua | 49 +++++++++++++----------- src/system/include/pw/system/input.hpp | 1 + src/visual/include/pw/visual/texture.hpp | 3 -- src/visual/src/shader.cpp | 19 ++++++--- src/visual/src/texture.cpp | 39 ++++++++++--------- 6 files changed, 63 insertions(+), 49 deletions(-) diff --git a/src/binding/src/script_system.cpp b/src/binding/src/script_system.cpp index e131459..411d68c 100644 --- a/src/binding/src/script_system.cpp +++ b/src/binding/src/script_system.cpp @@ -32,6 +32,7 @@ void register_system_function(sol::state&, sol::table &ns) "mouse_position",sol::readonly_property(&input::mouse_position), "mouse_button",sol::readonly_property(&input::mouse_button), "mouse_pressed",sol::readonly_property(&input::mouse_pressed), + "has_input",sol::readonly_property(&input::has_input), "input_string",sol::readonly_property(&input::input_string) ); diff --git a/src/scripts/demos/simple_002.lua b/src/scripts/demos/simple_002.lua index dcfff71..69542d6 100644 --- a/src/scripts/demos/simple_002.lua +++ b/src/scripts/demos/simple_002.lua @@ -1,5 +1,5 @@ -- --- demonstrator for Lua binding on pixwerx +-- pixwerx - bare rendering engine binding usage -- pw.script:load_all() @@ -8,7 +8,7 @@ local w = pw.window.new() w.visible = false -- set title -w.title = "My Cool 3D Content" +w.title = "pixwerx - bare rendering" -- set size w.size = pw.size.new(640,480) @@ -132,30 +132,33 @@ w.visible = true -- main update loop while w:update() do - -- somehow works - if pw.input.get().input_string == 'f' then - w.fullscreen = not w.fullscreen - end + -- only check if get a new input + if pw.input.get().has_input then - -- keycode for quit - if pw.input.get().input_string == 'q' then - break; - end + -- somehow works + if pw.input.get().input_string == 'f' then + w.fullscreen = not w.fullscreen + end - -- just to quickly modify speed - local move_step = 0.05 + -- keycode for quit + if pw.input.get().input_string == 'q' then + break + end - -- 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) + -- 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 + end end -- just to check diff --git a/src/system/include/pw/system/input.hpp b/src/system/include/pw/system/input.hpp index 9c00e6a..5e39f56 100644 --- a/src/system/include/pw/system/input.hpp +++ b/src/system/include/pw/system/input.hpp @@ -17,6 +17,7 @@ public: int mouse_button() const { return _mouse_button; } + bool has_input() const { return !_input_string.empty();} std::string input_string() const { return _input_string; } ~input() = default; diff --git a/src/visual/include/pw/visual/texture.hpp b/src/visual/include/pw/visual/texture.hpp index 622637b..f045c66 100644 --- a/src/visual/include/pw/visual/texture.hpp +++ b/src/visual/include/pw/visual/texture.hpp @@ -49,9 +49,6 @@ public: void set_wrap(wrap_mode w); wrap_mode wrap() const { return _wrap; } - void generate_mipmaps(); - - uint32_t native_handle() const; uint32_t native_sampler_handle() const; diff --git a/src/visual/src/shader.cpp b/src/visual/src/shader.cpp index a4d05df..ded2adc 100644 --- a/src/visual/src/shader.cpp +++ b/src/visual/src/shader.cpp @@ -20,7 +20,7 @@ struct shader::impl ~impl() { - clear(); + destroy(); } bool is_valid() @@ -132,16 +132,21 @@ struct shader::impl glUseProgram(_shader_program); } - void clear() + void destroy() { // potentially the GL driver hasn't been loaded if (is_valid()) { - glDeleteProgram(_shader_program); + + // deleting and detaching should happen much earlier for (auto s : _shader_stages) { glDeleteShader(s); - } + } + + // only program needs to be deleted + + glDeleteProgram(_shader_program); } } @@ -169,8 +174,12 @@ struct shader::impl { glUniform1f(location,v); } -}; + void bind(int location,const texture& v) + { + glUniform1d(location,v.native_sampler_handle()); + } +}; shader::shader() diff --git a/src/visual/src/texture.cpp b/src/visual/src/texture.cpp index 6546257..34a12e2 100644 --- a/src/visual/src/texture.cpp +++ b/src/visual/src/texture.cpp @@ -64,32 +64,35 @@ struct texture::impl { unbind(); } - void create(const class image& i) + void create() { - glGenTextures(1, &_texture_id); + + auto i = _host._image; + + glGenTextures(1, &_texture_id); + glBindTexture(gl_shape(),_texture_id); + + GLuint format = GL_RGBA; + + glTexImage2D(gl_shape(),0,format,i->size().width,i->size().height,0,format,GL_UNSIGNED_BYTE,i->data()); + + glGenerateMipmap(gl_shape()); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); -// glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); -// glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); - // glTexImage1D(gl_shape(), 0, GL_R8, cosAngleResolution, 0, - // GL_RED, GL_UNSIGNED_BYTE, &textureData[0]); - // glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_BASE_LEVEL, 0); - // glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAX_LEVEL, 0); - // glBindTexture(GL_TEXTURE_1D, 0); } void destroy() { - + glDeleteTextures(1,&_texture_id); } - void generate_mipmaps() - { - glGenerateMipmap(gl_shape()); - } }; @@ -108,6 +111,8 @@ texture::texture(image_ref i, texture::data_layout s) texture(); set_image(i); set_shape(s); + + _impl->create(); } texture::~texture() @@ -117,6 +122,8 @@ texture::~texture() void texture::set_image(image_ref i) { _image = i; + + _impl->create(); } void texture::set_shape(data_layout s) @@ -129,10 +136,6 @@ void texture::set_wrap(wrap_mode w) _wrap = w; } -void texture::generate_mipmaps() -{ - _impl->generate_mipmaps(); -} uint32_t texture::native_handle() const {