src/octree.cc
branchpyrit
changeset 38 5d043eeb09d9
parent 37 5f954c0d34fc
child 42 fbdeb3e04543
equal deleted inserted replaced
37:5f954c0d34fc 38:5d043eeb09d9
    53 				getChild(i)->addShape(*sh);
    53 				getChild(i)->addShape(*sh);
    54 				shapenum++;
    54 				shapenum++;
    55 			}
    55 			}
    56 	}
    56 	}
    57 
    57 
    58 	if (shapes->size() <= 8 && shapenum > 2*shapes->size())
    58 	if ((shapes->size() <= 8 && shapenum > 2*shapes->size())
       
    59 	|| shapenum >= 6*shapes->size())
    59 	{
    60 	{
    60 		// bad subdivision, revert
    61 		// bad subdivision, revert
    61 		delete[] children;
    62 		delete[] children;
    62 		return;
    63 		return;
    63 	}
    64 	}
    76 	dbgmsg(1, "* building octree\n");
    77 	dbgmsg(1, "* building octree\n");
    77 	root = new OctreeNode();
    78 	root = new OctreeNode();
    78 	ShapeList::iterator shape;
    79 	ShapeList::iterator shape;
    79 	for (shape = shapes.begin(); shape != shapes.end(); shape++)
    80 	for (shape = shapes.begin(); shape != shapes.end(); shape++)
    80 		root->addShape(*shape);
    81 		root->addShape(*shape);
    81 
       
    82 	root->subdivide(bbox, max_depth);
    82 	root->subdivide(bbox, max_depth);
    83 	built = true;
    83 	built = true;
    84 }
    84 }
    85 
    85 
    86 
    86 
   182 
   182 
   183 	node = root;
   183 	node = root;
   184 	st_cur->next = -1;
   184 	st_cur->next = -1;
   185 
   185 
   186 	Shape *nearest_shape = NULL;
   186 	Shape *nearest_shape = NULL;
   187 	while (nearest_shape == NULL)
   187 	for (;;)
   188 	{
   188 	{
   189 		if (st_cur->next == -1)
   189 		if (st_cur->next == -1)
   190 		{
   190 		{
   191 			st_cur->next = 8;
   191 			st_cur->next = 8;
   192 
   192 
   194 			if (!(tx1 < 0.0 || ty1 < 0.0 || tz1 < 0.0))
   194 			if (!(tx1 < 0.0 || ty1 < 0.0 || tz1 < 0.0))
   195 			{
   195 			{
   196 				if (node->isLeaf())
   196 				if (node->isLeaf())
   197 				{
   197 				{
   198 					ShapeList::iterator shape;
   198 					ShapeList::iterator shape;
   199 					register Float mindist = max3(tx0,ty0,tz0);
   199 					//register Float mindist = max3(tx0,ty0,tz0);
   200 					/* correct & slow */
   200 					register Float dist = min(nearest_distance, min3(tx1,ty1,tz1));
   201 					//Float dist = min(min3(tx1,ty1,tz1),nearest_distance);
       
   202 					/* faster */
       
   203 					register Float dist = nearest_distance;
       
   204 					for (shape = node->shapes->begin(); shape != node->shapes->end(); shape++)
   201 					for (shape = node->shapes->begin(); shape != node->shapes->end(); shape++)
   205 						if (*shape != origin_shape && (*shape)->intersect(ray, dist) && dist >= mindist)
   202 						if (*shape != origin_shape && (*shape)->intersect(ray, dist))
   206 						{
   203 						{
   207 							nearest_shape = *shape;
   204 							nearest_shape = *shape;
   208 							nearest_distance = dist;
   205 							nearest_distance = dist;
   209 						}
   206 						}
       
   207 					if (nearest_shape != NULL)
       
   208 						return nearest_shape;
   210 				}
   209 				}
   211 				else
   210 				else
   212 				{
   211 				{
   213 					txm = 0.5 * (tx0+tx1);
   212 					txm = 0.5 * (tx0+tx1);
   214 					tym = 0.5 * (ty0+ty1);
   213 					tym = 0.5 * (ty0+ty1);
   325 				(st_cur-1)->next = 8;
   324 				(st_cur-1)->next = 8;
   326 				break;
   325 				break;
   327 		}
   326 		}
   328 		st_cur->next = -1;
   327 		st_cur->next = -1;
   329 	}
   328 	}
   330 	return nearest_shape;
   329 }
   331 }