/*	==============================================================
 *	BSP - devel
 *	Drawing functiuons in v_tlist.c
 */

typedef struct	bspnode_s
{
	plane3_t			split;
	int					num_tri;
	listtri_t			*tris;
	struct bspnode_s	*left, *right;
}	bspnode_t, *bspnode_p;

/*	Drawing stuff */

vboolean	V_LOCAL
bspViewerOnRight(bspnode_t *node,vec3_t viewpoint)
{
	vec_t dist;
	dist=vec3Dot(node->split.normal,viewpoint)+node->split.dist;
	if(dist < 0)
		return false;
	return true;
}

void	V_LOCAL
bspTraverseFrontToBack_r(bspnode_t *node,vec3_t viewpoint,void (V_CALL *NodeProc)(bspnode_t *))
{
	NodeProc(node);
	if(bspViewerOnRight(node,viewpoint))
	{
		if(node->right)
			bspTraverseFrontToBack_r(node->right,viewpoint,NodeProc);
		if(node->left)
			bspTraverseFrontToBack_r(node->left,viewpoint,NodeProc);
	}
	else
	{
		if(node->left)
			bspTraverseFrontToBack_r(node->left,viewpoint,NodeProc);
		if(node->right)
			bspTraverseFrontToBack_r(node->right,viewpoint,NodeProc);
	}
}

vboolean	V_LOCAL
bspTraverseBackToFront_r(bspnode_t *node,vec3_t viewpoint,void (V_CALL *NodeProc)(bspnode_t *))
{
	NodeProc(node);
	if(bspViewerOnRight(node,viewpoint))
	{
		if(node->left)
			bspTraverseBackToFront_r(node->left,viewpoint,NodeProc);
		if(node->right)
			bspTraverseBackToFront_r(node->right,viewpoint,NodeProc);
	}
	else
	{
		if(node->right)
			bspTraverseBackToFront_r(node->right,viewpoint,NodeProc);
		if(node->left)
			bspTraverseBackToFront_r(node->left,viewpoint,NodeProc);
	}
}

void	V_LOCAL	V_CALL
DrawNodeVC(bspnode_t *node)
{
	listtri_t *ptr,*end;
	ptr=node->tris;
	end=ptr+node->num_tri;
	glBegin(GL_TRIANGLES);
	while(ptr<end)
		triDrawVC(ptr++);
	glEnd();
}

void	V_LOCAL	V_CALL
DrawNodeVCN(bspnode_t *node)
{
	listtri_t *ptr,*end;
	ptr=node->tris;
	end=ptr+node->num_tri;
	glBegin(GL_TRIANGLES);
	while(ptr<end)
		triDrawVCN(ptr++);
	glEnd();
}

void	V_LOCAL	V_CALL
DrawNodeVCT(bspnode_t *node)
{
	listtri_t *ptr,*end;
	ptr=node->tris;
	end=ptr+node->num_tri;
	glBegin(GL_TRIANGLES);
	while(ptr<end)
		triDrawVCT(ptr++);
	glEnd();
}

void	V_LOCAL	V_CALL
DrawNodeVCNT(bspnode_t *node)
{
	listtri_t *ptr,*end;
	ptr=node->tris;
	end=ptr+node->num_tri;
	glBegin(GL_TRIANGLES);
	while(ptr<end)
		triDrawVCNT(ptr++);
	glEnd();
}

void	V_GLOBAL
bspDraw(bspnode_t *root,vec3_t viewpoint,vboolean front_to_back)
{
	unsigned draw_bits=0;
	static void (V_CALL *NodeProcs[4])(bspnode_t *node)=
		{DrawNodeVC,DrawNodeVCN,DrawNodeVCT,DrawNodeVCNT};
	void (V_CALL *NodeProc)(bspnode_t *node);
	if(glIsEnabled(GL_LIGHTING))
		draw_bits|=1;
	if(glIsEnabled(GL_TEXTURE_2D))
		draw_bits|=2;
	NodeProc==NodeProcs[draw_bits];
	if(front_to_back)
		bspTraverseFrontToBack_r(root,viewpoint,NodeProc);
	else
		bspTraverseBackToFront_r(root,viewpoint,NodeProc);
}

/*	Constructing stuff */
void	V_GLOBAL
bspDestroy(bspnode_t *root)
{
	if(!root)
		return;
	bspDestroy(root->left);
	bspDestroy(root->right);
	free(root);
}

bspnode_p	V_LOCAL
bspAllocNode(stack_t *root_list)
{
	struct bspnode_t *node;
	int num=stackCount(root_list);
}

/*
bspnode_t	V_GLOBAL
tlistConvertToBSP()
*/
