#include "pch.h"
#include "log.h"
#include "node_image_texture.h"
#include "renderer_gl/opengl_capabilities.h"
#include "shader.h"
#include "node_image.h"

Node* NodeImageTexture::clone_instantiate() const
{
    return new NodeImageTexture();
}

void NodeImageTexture::clone_copy(Node* dest) const
{
    Node::clone_copy(dest);
    NodeImageTexture* n = static_cast<NodeImageTexture*>(dest);
    n->tex = tex;
}

void NodeImageTexture::draw()
{
    tex ? tex->bind() : glBindTexture(pp::renderer::gl::texture_2d_target(), 0);
    auto& sampler = tex && tex->has_mips ? NodeImage::m_sampler_mips : NodeImage::m_sampler;
    sampler.bind(0);
    glEnable(pp::renderer::gl::blend_state());
    ShaderManager::use(kShader::Texture);
    ShaderManager::u_int(kShaderUniform::Tex, 0);
    ShaderManager::u_mat4(kShaderUniform::MVP, m_mvp);
    NodeImage::m_plane.draw_fill();
    sampler.unbind();
    glDisable(pp::renderer::gl::blend_state());
}
