不知你写这些函数是做什么用的?我没有理会
intersect_triangle函数定义时是8个参数,而调用时传了5个参数。
main函数中数组定义时用{}不是()。
intersect_triangle(orig,dir,v0,v1,v2)直接使用参数名作参数来传递就可以了。
修改后是这样的:(有错误请指出)
#include "stdafx.h"
#include
#include
#include
#include
#include
#include
using namespace std;
#define EPSILON 0.000001
double CROSS(double v1[3],double v2[3],double dest[3])
{
dest[0]=v1[1]*v2[2]-v1[2]*v2[1];
dest[1]=v1[2]*v2[0]-v1[0]*v2[2];
dest[2]=v1[0]*v2[1]-v1[1]*v2[0];
return 0;
}
double DOT(double dot,double v1[3],double v2[3])
{
dot=v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2];
return dot;
}
double SUB(double dest[3],double v1[3],double v2[3])
{
dest[0]=v1[0]-v2[0];
dest[1]=v1[1]-v2[1];
dest[2]=v1[2]-v2[2];
return 0;
}
int
intersect_triangle(double orig[3], double dir[3],double vert0[3], double vert1[3], double vert2[3])
{
double *t = NULL;
double *u = NULL;
double *v = NULL;
double edge1[3], edge2[3], tvec[3], pvec[3], qvec[3];
double det,inv_det,dot=0.0;
/* find vectors for two edges sharing vert0 */
SUB(edge1, vert1, vert0);
SUB(edge2, vert2, vert0);
/* begin calculating determinant - also used to calculate U parameter */
CROSS(pvec, dir, edge2);
/* if determinant is near zero, ray lies in plane of triangle */
det = DOT(dot,edge1,pvec);
#ifdef TEST_CULL /* define TEST_CULL if culling is desired */
if (det < EPSILON)
return 0;
/* calculate distance from vert0 to ray origin */
SUB(tvec, orig, vert0);
/* calculate U parameter and test bounds */
*u = DOT(tvec, pvec);
if (*u < 0.0 || *u > det)
return 0;
/* prepare to test V parameter */
CROSS(qvec, tvec, edge1);
/* calculate V parameter and test bounds */
*v = DOT(dir, qvec);
if (*v < 0.0 || *u + *v > det)
return 0;
/* calculate t, scale parameters, ray intersects triangle */
*t = DOT(edge2, qvec);
inv_det = 1.0 / det;
*t *= inv_det;
*u *= inv_det;
*v *= inv_det;
#else /* the non-culling branch */
if (det > -EPSILON && det < EPSILON)
return 0;
inv_det = 1.0 / det;
/* calculate distance from vert0 to ray origin */
SUB(tvec, orig, vert0);
/* calculate U parameter and test bounds */
*u = DOT(dot,tvec, pvec) * inv_det;
if (*u < 0.0 || *u > 1.0)
return 0;
/* prepare to test V parameter */
CROSS(qvec, tvec, edge1);
/* calculate V parameter and test bounds */
*v = DOT(dot,dir, qvec) * inv_det;
if (*v < 0.0 || *u + *v > 1.0)
return 0;
/* calculate t, ray intersects triangle */
*t = DOT(dot,edge2, qvec) * inv_det;
#endif
return 1;
}
void main()
{
double orig[3]={0.0,0.0,0.0};
double dir[3]={1.0,1.0,1.0};
double v0[3]={2.0,5.0,7.0};
double v1[3]={1.0,4.0,-2.0};
double v2[3]={-2.0,-3.0,4.0};
int result;
result=intersect_triangle(orig,dir,v0,v1,v2);
}