Using c++to add textures to an egg file

I want to know how to use C++to add texture images to a newly created EGG file and save them
The following is my code
I tried to ask ChatGPT, but there was no solution

#include <pandaFramework.h>
#include <pandaSystem.h>
#include <TexturePool.h>
#include <eggTexture.h>
#include <eggData.h>

int main(int argc, char* argv[]) {
    PandaFramework framework;
    framework.open_framework(argc, argv);
    WindowFramework* window = framework.open_window();

    PT(EggData) data = new EggData();
   
    PT(Texture) texture = TexturePool::load_texture("111.png");

    PT(EggTexture) egg_texture = new EggTexture(texture->get_filename());
    egg_texture->set_filename(texture->get_filename());
    egg_texture->set_format(texture->get_format());
    egg_texture->set_wrap_mode(EggTexture::WM_repeat);
    data->add_texture(egg_texture);

    PT(EggTextureCollection) textures = new EggTextureCollection();
    textures->add_texture(egg_texture);
    PT(EggGroupNode) root = data->get_root();
    for (int i = 0; i < root->get_num_children(); i++) {
        PT(EggNode) child = root->get_child(i);
        if (child->is_of_type(EggPolygon::get_class_type())) {
            PT(EggPolygon) poly = DCAST(EggPolygon, child);
            poly->set_texture_type(EggPolygon::TT_normal);
            poly->set_texture(ts, egg_texture);
        }
    }

    data->write_egg("cylinder.egg")

    NodePath cylinder = window->load_model(framework.get_models(), "cylinder.egg");
    cylinder.reparent_to(window->get_render());
    cylinder.set_pos(0, 0, 0);

    window->setup_trackball();

    framework.main_loop();
    framework.close_framework();
    return 0;
}

Hi, welcome to the community! :slight_smile:

I can see several potential problems:

  • The format you get from Texture can’t be passed directly into EggTexture’s set_format. It’s a different enum. However, generally you do not need to explicitly set a format on an EggTexture - the egg loader will figure it out automatically.
  • You’re just iterating through the children that are directly on the root. It could be that there are polygons nested further. You would need to create a recursive function to find all the EggPolygon objects.
  • You are assigning the texture as a “normal” map. Are you sure this is what you want? This is used for normal mapping (aka bump mapping), and is not the same as a regular diffuse color texture.

If those are not helping, could you let me know where it exactly is going wrong? Open cylinder.egg in a text editor, does it actually contain a <Texture> entry? And a <TRef> underneath the polygon? Add a print statement to the inside of your loop, is it actually finding polygons?

I would just like to warn against using ChatGPT to answer questions about Panda3D. ChatGPT is not a reliable source of knowledge, especially about niche topics. It can generate extremely plausible-sounding answers, that are nonetheless getting crucial details wrong. You’re always welcome to ask your questions on the forums or on the Discord server!

1 Like