[OpenGL]If can help someone here my code to generate a cube from code

Geom* makeCube(float size = 1.0f)
{
        // https://docs.panda3d.org/1.10/python/programming/internal-structures/procedural-generation/creating-vertex-data
        // https://github.com/panda3d/panda3d-docs/blob/1.10/programming/internal-structures/procedural-generation/predefined-vertex-formats.rst
        // https://programtalk.com/python-more-examples/panda3d.core.GeomVertexFormat.getV3n3cpt2/
        // pos | normal | rgba | uv
        auto format = ::GeomVertexFormat::get_v3n3c4t2(); // direxctx style -> get_v3n3cpt2();
        PT(GeomVertexData) vdata;
        vdata = new ::GeomVertexData("box", format, ::Geom::UH_static);

        // A line corresponds to an index. Points must therefore be repeated to obtain different normals, colors
        // and texture coordinates depending on the face.
        // We therefore need 4 vertices per face, i.e. 6 x 4 = 24 lines to create.
        vdata->set_num_rows(24u);

        auto vertices = ::GeomVertexWriter(vdata, "vertex");
        auto normals = ::GeomVertexWriter(vdata, "normal");
        auto colours = ::GeomVertexWriter(vdata, "color");
        auto texcoords = ::GeomVertexWriter(vdata, "texcoord");

        // The cube must have a dimension of "size". We must therefore divide its size by two to obtain the values of the coordinates of its vertices.
        auto half_size = size * 0.5f;

        // Vertices positions
        const ::LVector3 V0(-half_size, -half_size, -half_size),
                         V1(-half_size,  half_size, -half_size),
                         V2( half_size,  half_size, -half_size),
                         V3( half_size, -half_size, -half_size),
                         V4(-half_size, -half_size,  half_size),
                         V5(-half_size,  half_size,  half_size),
                         V6( half_size,  half_size,  half_size),
                         V7( half_size, -half_size,  half_size);

        // Coordinates of normals (there is one normal for each face)
        const ::LVector3 Nxp( 1,  0,  0),
                         Nxn(-1,  0,  0),
                         Nyp( 0,  1,  0),
                         Nyn( 0, -1,  0),
                         Nzp( 0,  0,  1),
                         Nzn( 0,  0, -1);

        // Each face has a colour
        const ::LVector4 Cxp(1.0, 0.0, 0.0, 1.0),
                         Cxn(0.0, 1.0, 0.0, 1.0),
                         Cyp(0.0, 0.0, 1.0, 1.0),
                         Cyn(1.0, 0.0, 1.0, 1.0),
                         Czp(0.0, 1.0, 1.0, 1.0),
                         Czn(1.0, 1.0, 0.0, 1.0);

        //Each side is covered by the whole texture.
        const ::LVector2 T0(0.0, 1.0),
                         T1(0.0, 0.0),
                         T2(1.0, 0.0),
                         T3(1.0, 1.0);

        // Face -Z
        {
            // 0
            vertices.add_data3(V0);
            normals.add_data3(Nzn);
            colours.add_data4(Czn);
            texcoords.add_data2(T0);

            // 1
            vertices.add_data3(V1);
            normals.add_data3(Nzn);
            colours.add_data4(Czn);
            texcoords.add_data2(T1);

            // 2
            vertices.add_data3(V2);
            normals.add_data3(Nzn);
            colours.add_data4(Czn);
            texcoords.add_data2(T2);

            // 3
            vertices.add_data3(V3);
            normals.add_data3(Nzn);
            colours.add_data4(Czn);
            texcoords.add_data2(T3);
        }

        // Face +Z
        {
            // 4
            vertices.add_data3(V4);
            normals.add_data3(Nzp);
            colours.add_data4(Czp);
            texcoords.add_data2(T0);

            // 5
            vertices.add_data3(V7);
            normals.add_data3(Nzp);
            colours.add_data4(Czp);
            texcoords.add_data2(T3);

            // 6
            vertices.add_data3(V6);
            normals.add_data3(Nzp);
            colours.add_data4(Czp);
            texcoords.add_data2(T2);

            // 7
            vertices.add_data3(V5);
            normals.add_data3(Nzp);
            colours.add_data4(Czp);
            texcoords.add_data2(T1);
        }

        // Face +Y
        {
            // 8
            vertices.add_data3(V1);
            normals.add_data3(Nyp);
            colours.add_data4(Cyp);
            texcoords.add_data2(T0);

            // 9
            vertices.add_data3(V5);
            normals.add_data3(Nyp);
            colours.add_data4(Cyp);
            texcoords.add_data2(T1);

            // 10
            vertices.add_data3(V6);
            normals.add_data3(Nyp);
            colours.add_data4(Cyp);
            texcoords.add_data2(T2);

            // 11
            vertices.add_data3(V2);
            normals.add_data3(Nyp);
            colours.add_data4(Cyp);
            texcoords.add_data2(T3);
        }

        // Face -Y
        {
            // 12
            vertices.add_data3(V0);
            normals.add_data3(Nyn);
            colours.add_data4(Cyn);
            texcoords.add_data2(T0);

            // 13
            vertices.add_data3(V3);
            normals.add_data3(Nyn);
            colours.add_data4(Cyn);
            texcoords.add_data2(T1);

            // 14
            vertices.add_data3(V7);
            normals.add_data3(Nyn);
            colours.add_data4(Cyn);
            texcoords.add_data2(T2);

            // 15
            vertices.add_data3(V4);
            normals.add_data3(Nyn);
            colours.add_data4(Cyn);
            texcoords.add_data2(T3);
        }

        // Face +X
        {
            // 16
            vertices.add_data3(V3);
            normals.add_data3(Nxp);
            colours.add_data4(Cxp);
            texcoords.add_data2(T0);

            // 17
            vertices.add_data3(V2);
            normals.add_data3(Nxp);
            colours.add_data4(Cxp);
            texcoords.add_data2(T1);

            // 18
            vertices.add_data3(V6);
            normals.add_data3(Nxp);
            colours.add_data4(Cxp);
            texcoords.add_data2(T2);

            // 19
            vertices.add_data3(V7);
            normals.add_data3(Nxp);
            colours.add_data4(Cxp);
            texcoords.add_data2(T3);
        }

        // Face -X
        {
            // 20
            vertices.add_data3(V4);
            normals.add_data3(Nxn);
            colours.add_data4(Cxn);
            texcoords.add_data2(T0);

            // 21
            vertices.add_data3(V5);
            normals.add_data3(Nxn);
            colours.add_data4(Cxn);
            texcoords.add_data2(T1);

            // 22
            vertices.add_data3(V1);
            normals.add_data3(Nxn);
            colours.add_data4(Cxn);
            texcoords.add_data2(T2);

            // 23
            vertices.add_data3(V0);
            normals.add_data3(Nxn);
            colours.add_data4(Cxn);
            texcoords.add_data2(T3);
        }

        PT(GeomTriangles) tris;
        tris = new ::GeomTriangles(::GeomEnums::UsageHint::UH_static);

        auto quad = [&](int v0, int v1, int v2, int v3)
        {
            tris->add_vertices(v0, v1, v2);
            tris->add_vertices(v0, v2, v3);
            tris->close_primitive();
        };

        for(int i = 0; i < 24; i += 4)
            quad(i, i + 1, i + 2, i + 3);

        auto* square = new ::Geom(vdata);
        square->add_primitive(tris);
        return square;
    }

    // https://discourse.panda3d.org/t/draw-a-box-sphere-tube-teapot/4506/5
}

…

auto *geom = makeCube();
geometry->add_geom(geom);

1 Like