How to create Egg file

I am a beginner of panda3D and want to create egg files in the program through c++ code, but there is no relevant tutorial, and I don’t know what library to import…
Here is a code I wrote to create an egg file, but the compiler reported LNK2001, what is the problem? Any help would be appreciated.

//#include "asyncTaskManager.h"
//#include <genericAsyncTask.h>
//
//#include "pandaFramework.h"
//#include "pandaSystem.h"
//
//#include <eggWriter.h>
//#include <eggPolygon.h>
//#include <eggVertexPool.h>
//#include <eggData.h>
//#include <eggVertex.h>
//#include <eggPrimitive.h>
//#include <eggNode.h>
//#include <eggGroupNode.h>
//#include <eggPolygon.h>
//#include <eggCoordinateSystem.h>
//#include <geomVertexReader.h>
//
//#include "eggWriter.h"
//#include "eggPolygon.h"
//#include "eggVertexPool.h"
//#include <eggData.h>
//#include "eggVertex.h"
//#include <eggPrimitive.h>
//#include <eggNode.h>

#include <eggGroupNode.h>
#include <eggPolygon.h>
#include <eggCoordinateSystem.h>
#include <geomVertexReader.h>

#include <fstream>
#include <iostream>
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;

void makeWedgeStart(EggData d);
EggData makeWedge();

void makeWedgeStart(EggData d) {
    EggData data = d;

    data.write_egg("20221109.egg");
}

EggData makeWedge() {

    EggCoordinateSystem z_u = EggCoordinateSystem();
    z_u.set_value(CS_zup_right);
    EggNode* z_up = &z_u;

    EggData data = EggData();
    data.add_child(z_up);

    // create vertex
    EggVertexPool v = EggVertexPool("fan");
    EggNode* vp = &v;
    data.add_child(vp);

    EggVertex v0 = EggVertex();
    LPoint3d point0 = LPoint3d(1, 1, 1);
    v0.set_pos(point0);

    EggVertex v1 = EggVertex();
    LPoint3d point1 = LPoint3d(-1, 1, 1);
    v1.set_pos(point1);

    EggVertex v2 = EggVertex();
    LPoint3d point2 = LPoint3d(-1, -1, 1);
    v2.set_pos(point2);

    EggVertex v3 = EggVertex();
    LPoint3d point3 = LPoint3d(1, -1, 1);
    v3.set_pos(point3);

    EggVertex v4 = EggVertex();
    LPoint3d point4 = LPoint3d(1, -1, -1);
    v4.set_pos(point4);

    EggVertex v5 = EggVertex();
    LPoint3d point5 = LPoint3d(-1, -1, -1);
    v5.set_pos(point5);

    EggVertex v6 = EggVertex();
    LPoint3d point6 = LPoint3d(-1, 1, -1);
    v6.set_pos(point6);

    EggVertex v7 = EggVertex();
    LPoint3d point7 = LPoint3d(1, 1, -1);
    v7.set_pos(point7);

    // create triangle
    EggPolygon pol1 = EggPolygon();
    EggNode* poly1 = &pol1;
    data.add_child(poly1);
    pol1.add_vertex(&v3);
    pol1.add_vertex(&v2);
    pol1.add_vertex(&v4);

    EggPolygon pol2 = EggPolygon();
    EggNode* poly2 = &pol2;
    data.add_child(poly2);
    pol2.add_vertex(&v4);
    pol2.add_vertex(&v2);
    pol2.add_vertex(&v5);

    EggPolygon pol3 = EggPolygon();
    EggNode* poly3 = &pol3;
    data.add_child(poly3);
    pol3.add_vertex(&v1);
    pol3.add_vertex(&v0);
    pol3.add_vertex(&v6);

    EggPolygon pol4 = EggPolygon();
    EggNode* poly4 = &pol4;
    data.add_child(poly4);
    pol4.add_vertex(&v6);
    pol4.add_vertex(&v0);
    pol4.add_vertex(&v7);

    EggPolygon pol5 = EggPolygon();
    EggNode* poly5 = &pol5;
    data.add_child(poly5);
    pol5.add_vertex(&v6);
    pol5.add_vertex(&v5);
    pol5.add_vertex(&v1);

    EggPolygon pol6 = EggPolygon();
    EggNode* poly6 = &pol6;
    data.add_child(poly6);
    pol6.add_vertex(&v1);
    pol6.add_vertex(&v5);
    pol6.add_vertex(&v2);

    EggPolygon pol7 = EggPolygon();
    EggNode* poly7 = &pol7;
    data.add_child(poly7);
    pol7.add_vertex(&v1);
    pol7.add_vertex(&v2);
    pol7.add_vertex(&v0);

    EggPolygon pol8 = EggPolygon();
    EggNode* poly8 = &pol8;
    data.add_child(poly8);
    pol8.add_vertex(&v0);
    pol8.add_vertex(&v2);
    pol8.add_vertex(&v2);

    EggPolygon pol9 = EggPolygon();
    EggNode* poly9 = &pol9;
    data.add_child(poly9);
    pol9.add_vertex(&v0);
    pol9.add_vertex(&v3);
    pol9.add_vertex(&v7);

    EggPolygon pol10 = EggPolygon();
    EggNode* poly10 = &pol10;
    data.add_child(poly10);
    pol10.add_vertex(&v7);
    pol10.add_vertex(&v3);
    pol10.add_vertex(&v4);

    EggPolygon pol11 = EggPolygon();
    EggNode* poly11 = &pol11;
    data.add_child(poly11);
    pol11.add_vertex(&v7);
    pol11.add_vertex(&v4);
    pol11.add_vertex(&v6);

    EggPolygon pol12 = EggPolygon();
    EggNode* poly12 = &pol12;
    data.add_child(poly12);
    pol12.add_vertex(&v6);
    pol12.add_vertex(&v4);
    pol12.add_vertex(&v5);

    return data;
}

int main(int argc, char* argv[]) {

    EggData data = makeWedge();
    makeWedgeStart(data);

    return 0;
}

Thanks!

Hi, welcome to the forums!

Could you be more specific about the error message? I suspect that you are missing libpandaegg.lib.

As an aside, the Egg* classes are a reference-counted class, you should allocate them with new and store them in a PT(Egg*) pointer type.

Thanks for your answer!
After putting in libpandaegg.lib and storing them in a PT(Egg*) pointer type, the program was able to proceed successfully, but an assertion appeared, which eventually resulted in only an empty egg file being generated :tired_face:
Here is a screenshot of the assertion:


Here is the improved code:

#include <eggGroupNode.h>
#include <eggPolygon.h>
#include <eggCoordinateSystem.h>
#include <geomVertexReader.h>

#include <fstream>
#include <iostream>
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;

void makeWedgeStart(EggData d);
EggData makeWedge();

void makeWedgeStart(EggData d) {
    EggData data = d;
    data.write_egg("20221109.egg");
}

EggData makeWedge() {

    PT(EggCoordinateSystem) z_u = new EggCoordinateSystem();
    //EggCoordinateSystem z_u = EggCoordinateSystem();
    z_u->set_value(CS_zup_right);
    PT(EggNode) z_up = z_u;

    PT(EggData) data = new EggData();
    data->add_child(z_up);

    // create vertex
    PT(EggVertexPool) v = new EggVertexPool("fan");
    PT(EggNode) vp = v;
    data->add_child(vp);

    PT(EggVertex) v0 = new EggVertex();
    LPoint3d point0 = LPoint3d(1, 1, 1);
    v0->set_pos(point0);

    PT(EggVertex) v1 = new EggVertex();
    LPoint3d point1 = LPoint3d(-1, 1, 1);
    v1->set_pos(point1);

    PT(EggVertex) v2 = new EggVertex();
    LPoint3d point2 = LPoint3d(-1, -1, 1);
    v2->set_pos(point2);

    PT(EggVertex) v3 = new EggVertex();
    LPoint3d point3 = LPoint3d(1, -1, 1);
    v3->set_pos(point3);

    PT(EggVertex) v4 = new EggVertex();
    LPoint3d point4 = LPoint3d(1, -1, -1);
    v4->set_pos(point4);

    PT(EggVertex) v5 = new EggVertex();
    LPoint3d point5 = LPoint3d(-1, -1, -1);
    v5->set_pos(point5);

    PT(EggVertex) v6 = new EggVertex();
    LPoint3d point6 = LPoint3d(-1, 1, -1);
    v6->set_pos(point6);

    PT(EggVertex) v7 = new EggVertex();
    LPoint3d point7 = LPoint3d(1, 1, -1);
    v7->set_pos(point7);

    // create triangle
    PT(EggPolygon) pol1 = new EggPolygon();
    PT(EggNode) poly1 = pol1;
    data->add_child(poly1);
    pol1->add_vertex(v3);
    pol1->add_vertex(v2);
    pol1->add_vertex(v4);

    PT(EggPolygon) pol2 = new EggPolygon();
    PT(EggNode) poly2 = pol2;
    data->add_child(poly2);
    pol2->add_vertex(v4);
    pol2->add_vertex(v2);
    pol2->add_vertex(v5);

    PT(EggPolygon) pol3 = new EggPolygon();
    PT(EggNode) poly3 = pol3;
    data->add_child(poly3);
    pol3->add_vertex(v1);
    pol3->add_vertex(v0);
    pol3->add_vertex(v6);

    PT(EggPolygon) pol4 = new EggPolygon();
    PT(EggNode) poly4 = pol4;
    data->add_child(poly4);
    pol4->add_vertex(v6);
    pol4->add_vertex(v0);
    pol4->add_vertex(v7);

    PT(EggPolygon) pol5 = new EggPolygon();
    PT(EggNode) poly5 = pol5;
    data->add_child(poly5);
    pol5->add_vertex(v6);
    pol5->add_vertex(v5);
    pol5->add_vertex(v1);

    PT(EggPolygon) pol6 = new EggPolygon();
    PT(EggNode) poly6 = pol6;
    data->add_child(poly6);
    pol6->add_vertex(v1);
    pol6->add_vertex(v5);
    pol6->add_vertex(v2);

    PT(EggPolygon) pol7 = new EggPolygon();
    PT(EggNode) poly7 = pol7;
    data->add_child(poly7);
    pol7->add_vertex(v1);
    pol7->add_vertex(v2);
    pol7->add_vertex(v0);

    PT(EggPolygon) pol8 = new EggPolygon();
    PT(EggNode) poly8 = pol8;
    data->add_child(poly8);
    pol8->add_vertex(v0);
    pol8->add_vertex(v2);
    pol8->add_vertex(v2);

    PT(EggPolygon) pol9 = new EggPolygon();
    PT(EggNode) poly9 = pol9;
    data->add_child(poly9);
    pol9->add_vertex(v0);
    pol9->add_vertex(v3);
    pol9->add_vertex(v7);

    PT(EggPolygon) pol10 = new EggPolygon();
    PT(EggNode) poly10 = pol10;
    data->add_child(poly10);
    pol10->add_vertex(v7);
    pol10->add_vertex(v3);
    pol10->add_vertex(v4);

    PT(EggPolygon) pol11 = new EggPolygon();
    PT(EggNode) poly11 = pol11;
    data->add_child(poly11);
    pol11->add_vertex(v7);
    pol11->add_vertex(v4);
    pol11->add_vertex(v6);

    PT(EggPolygon) pol12 = new EggPolygon();
    PT(EggNode) poly12 = pol12;
    data->add_child(poly12);
    pol12->add_vertex(v6);
    pol12->add_vertex(v4);
    pol12->add_vertex(v5);

    return *(PT(EggData))(data);
}

int main(int argc, char* argv[]) {

    PT(EggData) data = (PT(EggData))(&makeWedge());
    makeWedgeStart(*data);

    return 0;
}

You’re misusing the reference counting system. Use PT(EggData), don’t return EggData from your function.

This is also unnecessary:

    PT(EggPolygon) pol3 = new EggPolygon();
    PT(EggNode) poly3 = pol3;
    data->add_child(poly3);
    pol3->add_vertex(v1);
    pol3->add_vertex(v0);
    pol3->add_vertex(v6);

You can just do:

    PT(EggPolygon) pol3 = new EggPolygon;
    data->add_child(pol3);
    pol3->add_vertex(v1);
    pol3->add_vertex(v0);
    pol3->add_vertex(v6);
1 Like

Thank you! This really helped me a lot, after the improvement I have been able to create egg files😆
So far it looks like I still have a lot to learn.