Пример #1
0
void Scene::KDInitBuild(int maxleafsize, int extradepth, bool loadpolys)
{
	if(loadpolys)
		LoadPolygons(this, &worldaabb);
	else
		ComputeWorldAABB(&worldaabb, this);

	if(numnodes > 0)
	{
		#ifdef _CELL
			_free_align(nodes);
		#else
			delete [] nodes;
		#endif
	
		#ifdef _CELL
			_free_align(leafpolys);
			_free_align(leaves);		
		#else
			delete [] leafpolys;
			delete [] leaves;
		#endif

		numnodes = 0;
	}	

	if(numpolys < maxleafsize)
		maxnumleaves = numpolys;
	else
		maxnumleaves = numpolys / maxleafsize;
	
	maxdepth = (int)(log2(maxnumleaves) + 0.5 + extradepth);
	maxnumleaves = (int)pow(2.0f, maxdepth);
	numnodes = (int)pow(2.0f, maxdepth+1) - 1;

	#ifdef _CELL
		leafpolys = (kdleafpoly_t*)_malloc_align(sizeof(kdleafpoly_t) * maxnumleaves, 4);
		leaves = (int*)_malloc_align(maxnumleaves*sizeof(int), 7);
	#else
		leafpolys = new kdleafpoly_t[maxnumleaves];
		leaves = new int[maxnumleaves];
	#endif

	for(int i=0; i < maxnumleaves; i++)
		leaves[i] = 0;
	
	numleaves = maxnumleaves;
	numleafpolys = 0;
	
	#ifdef _CELL
		nodes = (kdnode_t*)_malloc_align(numnodes*sizeof(kdnode_t), 7);
	#else
		nodes = new kdnode_t[numnodes];
	#endif
}
Пример #2
0
void KDBuildJob::ResetMinMaxBin(minmaxbin_t *mmb, int nbins, int index)
{
	if(nbins != numbins[index])
	{
		if(numbins[index] > 0)
		{
			#ifdef _CELL
				_free_align(minbins[index]);
				_free_align(maxbins[index]);
			#else
				delete [] minbins[index];
				delete [] maxbins[index];
			#endif
		}

		numbins[index] = nbins;

		#ifdef _CELL
			minbins[index] = (bin_t*)_malloc_align(numbins[index] * sizeof(bin_t), 7);
			maxbins[index] = (bin_t*)_malloc_align(numbins[index] * sizeof(bin_t), 7);			
		#else
			minbins[index] = new bin_t[numbins[index]];
			maxbins[index] = new bin_t[numbins[index]];
		#endif
	}

	int i;

	#ifdef _CELL
	vector float zero = spu_splats(0.0f);
	vector float *vminbins = (vector float*)minbins[index];
	vector float *vmaxbins = (vector float*)maxbins[index];

	for(i=0; i < numbins[index]; i++)
	{
		vminbins[i] = zero;
		vmaxbins[i] = zero;		
	}

	#else
	for(i=0; i < numbins[index]; i++)
	{
		minbins[index][i].b[0] = maxbins[index][i].b[0] = 0;
		minbins[index][i].b[1] = maxbins[index][i].b[1] = 0;
		minbins[index][i].b[2] = maxbins[index][i].b[2] = 0;
	}
	#endif

	mmb->minbins = minbins[index];
	mmb->maxbins = maxbins[index];


	mmb->numbins = numbins[index];
	mmb->bestcost = 1000000;
}
Пример #3
0
void LoadPolygons(Scene *scene, aabb_t *worldaabb)
{
	if(numpolys > 0)
	{
		#ifdef _CELL
			_free_align(polys);
		#else
			delete [] polys;
		#endif

		numpolys = 0;	
	}
	
	vector<Mesh*>::iterator it;

	numpolys = 0;

	for(it = scene->meshlist.begin(); it != scene->meshlist.end(); it++)
	{
		Mesh *m = (*it);

		numpolys += m->NumTriangles();
	}

	#ifdef _CELL
		polys = (kdpolyp_t*)_malloc_align(sizeof(kdpolyp_t) * numpolys, 7);
	#else
		polys = new kdpolyp_t[numpolys];
	#endif

	int index = 0;

	AABB waabb(worldaabb);

	waabb.Reset();

	for(it = scene->meshlist.begin(); it != scene->meshlist.end(); it++)
	{
		Mesh *m = (*it);
		triangle_t *triangles = m->GetTriangles();
		normal_t *normals = m->GetNormals();
		color_t *colors = m->GetColors();
		aabb_t *m_aabbs = m->GetAABBs();

		for(int i=0; i < m->NumTriangles(); i++)
		{
			polys[index].triangle = &triangles[i];
			polys[index].normal = &normals[i];
			polys[index].color = colors[i].c;
		
			m_aabbs[i].poly = &polys[index];

			waabb.Union(&m_aabbs[i]);

			index++;
		}		
	}
}
Пример #4
0
/* cleans up dynamically allocated memory in scene */
void free_scene(scene_t *scene)
{
	unsigned int i = 0;

	/* clean up lights */
	_free_align(scene->lights);
	
	for(; i < scene->nObjects; ++i)
	{	/* if object is polygon, free vertexes */
		if(scene->objects[i].geometryType == GEOMETRY_POLYGON)
		{
			_free_align(scene->objects[i].poly_obj.vertex);
		}
	}
	
	/* clean up objects */
	_free_align(scene->objects);
}