You will not believe the code from the textbook has a memory leak.
#include "pandaFramework.h"
#include "pandaSystem.h"
int main(int argc, char *argv[]) {
//open a new window framework
PandaFramework framework;
framework.open_framework(argc, argv);
//set the window title to My Panda3D Window
framework.set_window_title("My Panda3D Window");
//open the window
WindowFramework *window = framework.open_window();
//here is room for your own code
//do the main loop, equal to run() in python
framework.main_loop();
//close the window framework
framework.close_framework();
return (0);
}
However, if I supplement my code, it’s OK.
Example:
#include "pandaFramework.h"
#include "pandaSystem.h"
#include "geomTristrips.h"
void CubeToSphere (LVector3f&vPosition){
float x2 = vPosition[0] * vPosition[0] ;
float y2 = vPosition[1] * vPosition[1] ;
float z2 = vPosition[2] * vPosition[2];
vPosition[0] = vPosition[0] * sqrt( 1.0f - ( y2 * 0.5f ) - ( z2 * 0.5f ) + ( (y2 * z2) / 3.0f ) );
vPosition[1] = vPosition[1] * sqrt( 1.0f - ( z2 * 0.5f ) - ( x2 * 0.5f ) + ( (z2 * x2) / 3.0f ) );
vPosition[2] = vPosition[2] * sqrt( 1.0f - ( x2 * 0.5f ) - ( y2 * 0.5f ) + ( (x2 * y2) / 3.0f ) );
}
int main(int argc, char *argv[]) {
PandaFramework framework;
framework.open_framework(argc, argv);
framework.set_window_title("My Panda3D");
WindowFramework *window = framework.open_window();
window->setup_trackball();
PT(GeomVertexData) vdata;
vdata = new GeomVertexData("ProceduralMesh", GeomVertexFormat::get_v3(), Geom::UH_static);
vdata->set_num_rows(4);
GeomVertexWriter vertex; vertex = GeomVertexWriter(vdata, "vertex");
PT(GeomTristrips) prim;
prim = new GeomTristrips(Geom::UH_static);
int height = 2;
int width = 2;
float radius = 3.0;
LVector3f vMinPosition(-1.0f, -1.0f, 0.0f );
int index = 0;
for ( int y = 0; y < height; ++y){
for ( int x = 0; x < width; ++x ){
LVector3f vPosition = vMinPosition;
vPosition[0] += (float)x / (float)(width-1)* 2.0f;
vPosition[1] +=- (float)y / (float)(height-1)* 2.0f;
vertex.add_data3f(vPosition);
prim->add_vertex( index);
if ( index == 3){
prim->close_primitive();
index = 0;
}
++ index ;
}
}
PT(Geom) geom;
geom = new Geom(vdata);
geom->add_primitive(prim);
PT(GeomNode) node;
node = new GeomNode("gnode");
node->add_geom(geom);
NodePath nodePath = window->get_render().attach_new_node(node);
framework.main_loop();
framework.close_framework();
return (0);
}
No Leaks