refactoring of texture code into a 2D version first

This commit is contained in:
Hartmut Seichter 2021-01-23 14:21:08 +01:00
parent 2c4cc29f97
commit 753f51453f
6 changed files with 63 additions and 49 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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