Files
panopainter/engine/node_image.cpp

97 lines
2.1 KiB
C++

#include "pch.h"
#include "log.h"
#include "node_image.h"
#include "shader.h"
ui::Plane NodeImage::m_plane;
Sampler NodeImage::m_sampler;
void NodeImage::static_init()
{
m_plane.create<1>(1, 1);
m_sampler.create();
}
Node* NodeImage::clone_instantiate() const
{
return new NodeImage();
}
void NodeImage::clone_copy(Node* dest) const
{
Node::clone_copy(dest);
NodeImage* n = static_cast<NodeImage*>(dest);
n->m_use_atlas = m_use_atlas;
n->m_region = m_region;
n->m_off = m_off;
n->m_sz = m_sz;
n->m_path = m_path;
n->m_tex_id = m_tex_id;
}
void NodeImage::create()
{
if (!m_path.empty() && TextureManager::load(m_path.c_str(), m_use_mipmaps))
{
//LOG("load image node %s", m_path.c_str());
auto tex_sz = TextureManager::get(m_tex_id).size();
m_off = m_region.xy / tex_sz;
m_sz = (m_region.zw - m_region.xy) / tex_sz;
}
}
void NodeImage::restore_context()
{
Node::restore_context();
create();
}
void NodeImage::parse_attributes(kAttribute ka, const tinyxml2::XMLAttribute* attr)
{
Node::parse_attributes(ka, attr);
switch (ka)
{
case kAttribute::Path:
m_path = attr->Value();
m_tex_id = const_hash(attr->Value());
break;
case kAttribute::Region:
{
glm::vec4 v;
int n = sscanf(attr->Value(), "%f %f %f %f", &v.x, &v.y, &v.z, &v.w);
if (n == 4)
{
m_region = v;
m_use_atlas = true;
}
break;
}
default:
break;
}
}
void NodeImage::draw()
{
using namespace ui;
TextureManager::get(m_tex_id).bind();
m_sampler.bind(0);
glEnable(GL_BLEND);
if (m_use_atlas)
{
ui::ShaderManager::use(kShader::Atlas);
ui::ShaderManager::u_vec2(kShaderUniform::Tof, m_off);
ui::ShaderManager::u_vec2(kShaderUniform::Tsz, m_sz);
}
else
{
ui::ShaderManager::use(kShader::Texture);
}
ui::ShaderManager::u_int(kShaderUniform::Tex, 0);
ui::ShaderManager::u_mat4(kShaderUniform::MVP, m_mvp);
m_plane.draw_fill();
m_sampler.unbind();
TextureManager::get(m_tex_id).unbind();
glDisable(GL_BLEND);
}