add load image from url

This commit is contained in:
2019-09-26 14:53:44 +02:00
parent 0a8c3aeaf2
commit e406f7964c
6 changed files with 152 additions and 4 deletions

View File

@@ -2,6 +2,7 @@
#include "log.h"
#include "node_image.h"
#include "shader.h"
#include "app.h"
Plane NodeImage::m_plane;
Sampler NodeImage::m_sampler;
@@ -30,6 +31,9 @@ void NodeImage::clone_copy(Node* dest) const
n->m_sz = m_sz;
n->m_path = m_path;
n->m_tex_id = m_tex_id;
n->m_url = m_url;
n->m_remote_texture = m_remote_texture;
n->m_autosize = m_autosize;
}
void NodeImage::create()
@@ -64,6 +68,9 @@ void NodeImage::parse_attributes(kAttribute ka, const tinyxml2::XMLAttribute* at
m_path = attr->Value();
m_tex_id = const_hash(attr->Value());
break;
case kAttribute::Url:
m_url = attr->Value();
break;
case kAttribute::Region:
{
glm::vec4 v;
@@ -85,9 +92,13 @@ void NodeImage::parse_attributes(kAttribute ka, const tinyxml2::XMLAttribute* at
void NodeImage::draw()
{
TextureManager::get(m_tex_id).bind();
m_remote_texture ?
m_remote_texture->bind() :
TextureManager::get(m_tex_id).bind();
auto& sampler = m_use_mipmaps ? m_sampler_mips : m_sampler;
sampler.bind(0);
glEnable(GL_BLEND);
if (m_use_atlas)
{
@@ -103,7 +114,6 @@ void NodeImage::draw()
ShaderManager::u_mat4(kShaderUniform::MVP, m_mvp * glm::scale(glm::vec3(m_scale, 1.f)));
m_plane.draw_fill();
sampler.unbind();
TextureManager::get(m_tex_id).unbind();
glDisable(GL_BLEND);
}
@@ -122,3 +132,36 @@ bool NodeImage::set_image(const std::string& path)
}
return false;
}
void NodeImage::load_url(const std::string& url)
{
m_remote_asset = std::make_shared<Asset>();
m_remote_asset->open_url_async(url,
[this] (float progress) -> bool {
return true;
},
[this] (bool success) {
if (success)
{
int w, h, c;
uint8_t* rgba = stbi_load_from_memory(m_remote_asset->m_data, m_remote_asset->m_len, &w, &h, &c, 4);
m_remote_texture = std::make_shared<Texture2D>();
m_remote_texture->create(w, h, GL_RGBA8, GL_RGBA, rgba);
m_remote_texture->auto_destroy = true;
if (m_use_mipmaps)
m_remote_texture->create_mipmaps();
delete rgba;
if (m_autosize)
SetAspectRatio(w / h);
}
m_remote_asset.reset();
}
);
}
void NodeImage::added(Node* parent)
{
Node::added(parent);
if (!m_url.empty() && root() == App::I->layout.get(App::I->main_id))
load_url(m_url);
}