Procedural Modeling in C++

Hello,

I am trying to render a polygon in panda3D using C++. I have looked through the manuals and source code for procedural modeling. I have encountered some trouble, and I cant seem to find a solution.

I have added data for the vertices:

		vertex->add_data3f(x1,y1,z1);
		vertex->add_data3f(x2,y1,z1);
		vertex->add_data3f(x2,y2,z2);
		vertex->add_data3f(x1,y2,z2);

The problem is below:

	GeomTriangles *tri1;
	tri1 = new GeomTriangles(Geom::UH_dynamic);	
	tri1->add_vertex(0);
	tri1->add_vertex(1);
	tri1->add_vertex(3);
	tri1->close_primitive();

When running my program in debug mode, my compiler breaks at the line where i add a vertex to tri1. It says I have made an access violation, although I have allocated memory for the tri1.

Please help.

Bwyyoung

In order to use Panda’s reference-counting system in C++, you have to use the PointerTo smart-pointer, instead of an ordinary pointer. The PointerTo class automatically increments the reference count when it is assigned, and decrements it when it is reassigned.

Instead of:

GeomTriangles *tri1;

Declare:

PT(GeomTriangles) tri;

There are similar changes for your GeomVertexData, Geom, GeomNode, and so on. If you don’t do this, your reference count will be 0, and it may get inadvertently deleted the first time you do something with it.

David

Note also that not every Panda class inherits from ReferenceCount. You have to know which is which. You can look at the inheritance tree to figure this out, or look at existing examples within Panda.

In particular, GeomVertexWriter doesn’t. NodePath doesn’t. You’re not generally supposed to allocate these objects from the free store; instead, create them statically. For instance:

GeomVertexWriter vertex(vdata, "vertex");

instead of:

GeomVertexWriter *vertex = new GeomVertexWriter(vdata, "vertex");

David

Thanks for the help, but now i have another problem. I am trying to add my polygon to a GeomNode, and I have this problem.

	snode.add_geom(mySquare); 

my compiler breaks at that point. I have converted all my pointers pertaining to the ones specified to PT(). Im unclear as to the source of this problem. If you would need to know, the code for all my procedural modeling is below. Also, where can I find Panda3D’s Inheritance tree? Help is much appreciated.

	GeomVertexData vData( "Square",GeomVertexFormat::get_v3n3c4t2(),Geom::UH_dynamic);
    	GeomVertexWriter vertex(&vData,"Vertex");
	GeomVertexWriter color(&vData, "color");
   	GeomVertexWriter normal(&vData, "normal");
	GeomVertexWriter textures(&vData,"TexCoord");

	if (x1!= x2)
	{
		vertex.add_data3f(x1,y1,z1);
		vertex.add_data3f(x2,y1,z1);
		vertex.add_data3f(x2,y2,z2);
		vertex.add_data3f(x1,y2,z2);
	
		normal.add_data3f(0,0,0);
		normal.add_data3f(0,0,0);
		normal.add_data3f(0,0,0);
		normal.add_data3f(0,0,0);

	}
	else
	{
		vertex.add_data3f(x1, y1, z1);
		vertex.add_data3f(x2, y2, z1);
		vertex.add_data3f(x2, y2, z2);
		vertex.add_data3f(x1, y1, z2);
		normal.add_data3f(0,0,0);
		normal.add_data3f(0,0,0);
		normal.add_data3f(0,0,0);
		normal.add_data3f(0,0,0);//dummy values
	}

	PT(GeomTriangles) Tri1;
	Tri1 =  new GeomTriangles(Geom::UH_dynamic);
	Tri1->add_vertex(0);
	Tri1->add_vertex(1);
	Tri1->add_vertex(3);

	color.add_data4f(1.0,0.0,0.0,1.0);
	color.add_data4f(0.0,1.0,0.0,1.0);
	color.add_data4f(0.0,0.0,1.0,1.0);
	color.add_data4f(1.0,0.0,1.0,1.0);

	textures.add_data2f(0.0, 1.0);
	textures.add_data2f(0.0, 0.0);
	textures.add_data2f(1.0, 0.0);
	textures.add_data2f(1.0, 1.0);
                

PT(Geom) mySquare;
	mySquare = new Geom(&vData);
	mySquare->add_primitive((GeomPrimitive *)Tri1);
                

       
	GeomNode snode("square");
	
	snode.add_geom(mySquare);

Hmm. Didn’t you forget to close the geometry?
Try this:

   PT(GeomTriangles) Tri1;
   Tri1 =  new GeomTriangles(Geom::UH_dynamic);
   Tri1->add_vertex(0);
   Tri1->add_vertex(1);
   Tri1->add_vertex(3); 
   Tri1->close_primitive();

GeomNode also inherits from ReferenceCount. Instead of:

   GeomNode snode("square");   
   snode.add_geom(mySquare);

Do:

   PT(GeomNode) snode = new GeomNode("square");   
   snode->add_geom(mySquare);

I don’t see a problem with this code otherwise, so I can’t give you more specific advice. What is the compiler error? That would be extremely helpful.

You have to read the C++ code to see the inheritance tree. Or you can follow the API documentation on this site.

David

Dear Mr. David,

Thank you so much for your support. I have solved the problem. :smiley: