Skip to content

how to adjust the transparency of a texture mesh? #180

@PengZai

Description

@PengZai

cv::Mat image = cv::imread("../figs/vis_ref_rgb_data.png");
auto texture = glk::create_texture(image);

auto front_shader_setting = guik::ShaderSetting(guik::ColorMode::TEXTURE_COLOR, transformation);
front_shader_setting.set_alpha(0.5f);

std::shared_ptrglk::Mesh sides_wire, front_tex;
make_camera_drawables(sides_wire, front_tex, 1.0f, 0.6f, 0.8f, texture);

viewer->update_drawable("front", front_tex, front_shader_setting);

void make_camera_drawables(std::shared_ptrglk::Mesh& sides_wire,
std::shared_ptrglk::Mesh& front_tex,
float w, float h, float z,
const std::shared_ptrglk::Texture& texture)
{
// ----- shared geometry (5 vertices: 4 corners + apex) -----
// std::vectorEigen::Vector3f vertices = {
// { w/2.f, h/2.f, z}, // 0
// {-w/2.f, h/2.f, z}, // 1
// {-w/2.f, -h/2.f, z}, // 2
// { w/2.f, -h/2.f, z}, // 3
// { 0.f, 0.f, 0.f} // 4 (apex)
// };

std::vector<Eigen::Vector3f> vertices = {
    {w / 2, -h / 2, z},
    { -w / 2, -h / 2, z},
    { -w / 2, h / 2, z},
    { w / 2, h / 2, z},
    {0,0,0}
};



std::vector<unsigned int> side_indices = {
    0,4,1,  1,4,2,  2,4,3,  3,4,0
};

// ----- sides: wireframe, no texcoords -----
sides_wire = std::make_shared<glk::Mesh>(
    vertices.data(),             sizeof(float)*3,   // positions
    nullptr,                     0,                 // normals (none)
    nullptr,                     0,                 // colors (none)
    nullptr,                     0,                 // texcoords (none)
    static_cast<int>(vertices.size()),
    side_indices.data(),
    static_cast<int>(side_indices.size()),
    /*wireframe=*/true
);

// ----- front: its own 4-vertex list + texcoords + texture -----
std::vector<Eigen::Vector3f> v_front = {
    vertices[0], 
    vertices[1], 
    vertices[2], 
    vertices[3]
};
std::vector<unsigned int> front_indices = { 
    0,2,1,
    0,3,2 
};

std::vector<Eigen::Vector4f> front_vertice_colors = {
  {1.0f, 1.0f, 1.0f, 1.0f},
  {1.0f, 1.0f, 1.0f, 1.0f},
  {1.0f, 1.0f, 1.0f, 1.0f},
  {1.0f, 1.0f, 1.0f, 1.0f}
};


// UVs for the front face (flip V if your image is upside-down)
std::vector<Eigen::Vector2f> uv_front = {
    {1.f, 0.f},  // for v_front[0]
    {0.f, 0.f},  // for v_front[1]
    {0.f, 1.f},  // for v_front[2]
    {1.f, 1.f}   // for v_front[3]
    // {0.0f, 0.0f},
    // {1.0f, 0.0f},
    // {1.0f, 1.0f},
    // {0.0f, 1.0f}
};

std::vector<Eigen::Vector3f> normals = {
    {0.f,0.f,1.f}
};


front_tex = std::make_shared<glk::Mesh>(
    v_front.data(),              sizeof(float)*3,
    normals.data(),    sizeof(float)*3,              // normals (optional)
    front_vertice_colors.data(),                     sizeof(float)*4,                 // colors (none)
    uv_front.data(),             sizeof(float)*2,   // texcoords
    static_cast<int>(v_front.size()),
    front_indices.data(),
    static_cast<int>(front_indices.size()),
    /*wireframe=*/false
);
front_tex->set_texture(texture);

}

I go through the source code , it looks like we can't control the color of texture. if I call set_alpha, that change the material_color...

how can I do to change the transparency of texture?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions