void TweakMouseProc::ComputeNewUVW(ViewExp& vpt, IPoint2 m)
{
	if ( ! vpt.IsAlive() )
	{
		// why are we here
		DbgAssert(!_T("Invalid viewport!"));
		return;
	}

	Ray r;
	vpt.MapScreenToWorldRay ((float) m.x, (float) m.y, r);
	TimeValue t = GetCOREInterface()->GetTime();
	Matrix3 tm = mod->GetMeshTopoDataNode(mHitLDIndex)->GetObjectTM(t);
	Matrix3 itm = Inverse(tm);

	r.p = r.p * itm;
	r.dir = VectorTransform(r.dir,itm);

	//intersect our ray with that face and get our new bary coords

	Point3 n = mHitLD->GetGeomFaceNormal(mHitFace);

	Point3 p1, p2, p3;

	p1 = mHitP[0];
	p2 = mHitP[1];
	p3 = mHitP[2];

	Point3  p, bry;
	float d, rn, a; 

	// See if the ray intersects the plane (backfaced)
	rn = DotProd(r.dir,n);
	if (rn > -0.0001) return;

	// Use a point on the plane to find d
	Point3 v1 = p1;
	d = DotProd(v1,n);

	// Find the point on the ray that intersects the plane
	a = (d - DotProd(r.p,n)) / rn;

	// Must be positive...
	if (a < 0.0f) return ;



	// The point on the ray and in the plane.
	p = r.p + a*r.dir;

	// Compute barycentric coords.
	bry = BaryCoords(p1,p2,p3,p);  // DbgAssert(bry.x + bry.y+ bry.z = 1.0) 

	Point3 uvw = mHitUVW[0] * bry.x + mHitUVW[1] * bry.y + mHitUVW[2] * bry.z;
	Point3 v = uvw - mSourceUVW;
	v *= -1.0f;
	mFinalUVW = mSourceUVW + v;
	mHitLD->SetTVVert(GetCOREInterface()->GetTime(),mHitTVVert,mFinalUVW,mod);

	mod->NotifyDependents(FOREVER, PART_TEXMAP, REFMSG_CHANGE);
	mod->InvalidateView();
	if (mod->ip) 
		mod->ip->RedrawViews(mod->ip->GetTime());

}
Beispiel #2
0
BOOL BoundsTree::HitQuadTree(IPoint2 m, int &nindex, DWORD &findex, Point3 &p, Point3 &norm, Point3 &bary, float &finalZ, Matrix3 &toWorldTm)
{
//int nindex = 0;
Leaf *l = head;
BOOL hit = FALSE;
Point3 hitPoint(0.0f,0.0f,0.0f);
DWORD smgroup;

hitPoint.x = (float) m.x;
hitPoint.y = (float) m.y;
float z = 0.0f;
Point3 bry;
if (l == NULL) 
	{
	z = 0.0f;
	return FALSE;
	}
int ct = 0;
while ( (l!=NULL) && (l->IsBottom() == FALSE))
	{
	int id = l->InWhichQuad(hitPoint);
	l = l->GetQuad(id);
	ct++;
	}
if (l)
	{
	if (l->faceIndex.Count() == 0) 
		return FALSE;
	else
		{
		for (int i = 0; i < l->faceIndex.Count(); i++)
			{
			int faceIndex = l->faceIndex[i].faceIndex;
			int nodeIndex = l->faceIndex[i].nodeIndex;

			LightMesh *lmesh = meshList[nodeIndex];

			Point3 *tempVerts = lmesh->vertsViewSpace.Addr(0);
			Face *tempFaces = lmesh->faces.Addr(faceIndex);

			Box2D b = lmesh->boundingBoxList[faceIndex];
			if ( (hitPoint.x >= b.min.x) && (hitPoint.x <= b.max.x) &&
				 (hitPoint.y >= b.min.y) && (hitPoint.y <= b.max.y) )
				{
//now check bary coords
				Point3 a,b,c;
				a = tempVerts[tempFaces->v[0]];
				b = tempVerts[tempFaces->v[1]];
				c = tempVerts[tempFaces->v[2]];
				Point3 az,bz,cz,hitPointZ;
				az = a;
				bz = b;
				cz = c;
				az.z = 0.0f;
				bz.z = 0.0f;
				cz.z = 0.0f;
				hitPointZ = hitPoint;
				hitPointZ.z = 0.0f;
	
				Point3 bry;
				bry = BaryCoords(az, bz, cz, hitPointZ);
//if inside bary find the the z point			
				if (!( (bry.x<0.0f || bry.x>1.0f || bry.y<0.0f || bry.y>1.0f || bry.z<0.0f || bry.z>1.0f) ||
					(fabs(bry.x + bry.y + bry.z - 1.0f) > EPSILON) ))
					{
					float tz = a.z * bry.x + b.z * bry.y + c.z * bry.z; 
					if ( (tz > z ) || (hit == FALSE) )  
						{
						z = tz;
						findex = faceIndex;
						nindex = nodeIndex;
						bary = bry;
						finalZ = z;
						smgroup = tempFaces->getSmGroup();
						}
					hit = TRUE;
					}
				}
			}
		}	
	}
if (hit)
	{
	Point3 a,b,c;
	int ia,ib,ic;
	LightMesh *lmesh = meshList[nindex];
	Point3 *tempVerts = lmesh->vertsWorldSpace.Addr(0);
	Face *tempFaces = lmesh->faces.Addr(findex);

	ia = tempFaces->v[0];
	ib = tempFaces->v[1];
	ic = tempFaces->v[2];
	a = tempVerts[ia];
	b = tempVerts[ib];
	c = tempVerts[ic];

	ViewExp *vpt = GetCOREInterface()->GetActiveViewport();

	Ray worldRay;
//	vpt->GetAffineTM(tm);
	vpt->MapScreenToWorldRay((float) m.x, (float) m.y, worldRay);
	GetCOREInterface()->ReleaseViewport(vpt);

//intersect ray with the hit face

		// See if the ray intersects the plane (backfaced)
	norm = Normalize((b-a)^(c-b));

	float rn = DotProd(worldRay.dir,norm);
		
	// Use a point on the plane to find d
	float d = DotProd(a,norm);

	// Find the point on the ray that intersects the plane
	float ta = (d - DotProd(worldRay.p,norm)) / rn;


		// The point on the ray and in the plane.
	p = worldRay.p + ta*worldRay.dir;

		// Compute barycentric coords.
	bary = BaryCoords(a, b, c, p);

	finalZ  = ta;

//	p = a * bary.x +  b * bary.y +  c * bary.z;
	p = p * meshList[nindex]->toLocalSpace;

	norm = VectorTransform(meshList[nindex]->toLocalSpace,norm);
	toWorldTm = meshList[nindex]->toWorldSpace;

	


	}

return hit;

}
Beispiel #3
0
//This thing is ugly should really do scan conversion instead of barycentric
void  PainterTextureSample::BuildWorldSpaceData(Mesh *msh, Matrix3 tm)
{
//loop through each face
	Tab<Point3> tempWorldPointList;

	tempWorldPointList.SetCount(msh->numVerts);
	for (int i = 0; i < msh->numVerts; i++)
		{
		tempWorldPointList[i] = msh->verts[i] * tm;
		}

	worldSpaceList.ZeroCount();
	uvwList.ZeroCount();

	for (i = 0; i < msh->numFaces; i++)
		{

		TSTR name;
		name.printf("Processing Face %d/%d",i,msh->numFaces-1);
		SetWindowText(GetDlgItem(hwnd,IDC_TEXT),name);
		
//gets its world space position corners
		Point3 t[3];
		Point3 w[3];
		

		w[0] = tempWorldPointList[msh->faces[i].v[0]];
		w[1] = tempWorldPointList[msh->faces[i].v[1]];
		w[2] = tempWorldPointList[msh->faces[i].v[2]];


		t[0] = uvwPoints[uvwFaces[i].t[0]];
		t[1] = uvwPoints[uvwFaces[i].t[1]];
		t[2] = uvwPoints[uvwFaces[i].t[2]];
	
		t[0].z = 0.0f;
		t[1].z = 0.0f;
		t[2].z = 0.0f;

//scan it from bitmap space to world space
		int minX,maxX,minY,maxY;
		minX = floor(t[0].x*width);
		maxX = ceil(t[0].x*width);
		minY = floor(t[0].y*width);
		maxY = ceil(t[0].y*width);
		for (int j = 1; j < 3; j++)
			{
			int tempMinX, tempMaxX;
			int tempMinY, tempMaxY;
			tempMinX = floor(t[j].x*width);
			tempMaxX = ceil(t[j].x*width);
			tempMinY = floor(t[j].y*width);
			tempMaxY = ceil(t[j].y*width);

			if (tempMinX < minX) minX = tempMinX;
			if (tempMinY < minY) minY = tempMinY;

			if (tempMaxX > maxX) maxX = tempMaxX;
			if (tempMaxY > maxY) maxY = tempMaxY;

			}
//more ugliness below
		float baryMin = 0.0 - (20.0f/width);
		float baryMax = 1.0f+ (20.0f/width);
		minY--;
		minX--;
		maxY++;
		maxX++;

		for (j =minY; j <= maxY; j++)
			{
			for (int k =minX; k <= maxX; k++)
				{
				Point3 hitPoint;
				hitPoint.x = (float)k/(float)width;
				hitPoint.y = (float)j/(float)width;
				hitPoint.z = 0.0f;
				Point3 baryCoord = BaryCoords(t[0], t[1], t[2], hitPoint);
//check if inside a triangle

				if (baryCoord.x >= baryMin && baryCoord.x <= baryMax) 
					{
					if (baryCoord.y >= baryMin && baryCoord.y <= baryMax) 
						{
						if (baryCoord.z >= baryMin && baryCoord.z <= baryMax) 
							{
							//we are inside 
							Point3 wp = w[0] * baryCoord.x +
							 	 w[1] * baryCoord.y +
								 w[2] * baryCoord.z;


							worldSpaceList.Append(1,&wp,5000);
							uvwList.Append(1,&hitPoint,5000);
							}
						}
					}
				}
			}

		}
		TSTR name;
		name.printf("  ");
		SetWindowText(GetDlgItem(hwnd,IDC_TEXT),name);
	//FIX we can dump alot of this stuff for instance the worldSpace list
	//the uvwPoints and the UVW faces




}
Beispiel #4
0
BOOL BoundsTree::IsInsideMesh(Point3 hitPoint)
{

Leaf *l = head;
int ct = 0;
BOOL hit = FALSE;
if (l == NULL) 
	{
	return FALSE;
	}

while ( (l!=NULL) && (l->IsBottom() == FALSE))
	{
	int id = l->InWhichQuad(hitPoint);
	l = l->GetQuad(id);
	ct++;
	}

ct = 0;

if (l)
	{
	if (l->faceIndex.Count() == 0) 
		return FALSE;
	else
		{
		for (int i = 0; i < l->faceIndex.Count(); i++)
			{
			int faceIndex = l->faceIndex[i].faceIndex;
			int nodeIndex = l->faceIndex[i].nodeIndex;
			Box2D b = meshList[nodeIndex]->boundingBoxList[faceIndex];
			if ( (hitPoint.x >= b.min.x) && (hitPoint.x <= b.max.x) &&
				 (hitPoint.y >= b.min.y) && (hitPoint.y <= b.max.y) )
				{
//now check bary coords
				Point3 a,b,c;
				a = meshList[nodeIndex]->vertsViewSpace[meshList[nodeIndex]->faces[faceIndex].v[0]];
				b = meshList[nodeIndex]->vertsViewSpace[meshList[nodeIndex]->faces[faceIndex].v[1]];
				c = meshList[nodeIndex]->vertsViewSpace[meshList[nodeIndex]->faces[faceIndex].v[2]];
				Point3 az,bz,cz,hitPointZ;
				az = a;
				bz = b;
				cz = c;
				az.z = 0.0f;
				bz.z = 0.0f;
				cz.z = 0.0f;
				hitPointZ = hitPoint;
				hitPointZ.z = 0.0f;
	
				Point3 bry;
				bry = BaryCoords(az, bz, cz, hitPointZ);
//if inside bary find the the z point			
				if (!( (bry.x<0.0f || bry.x>1.0f || bry.y<0.0f || bry.y>1.0f || bry.z<0.0f || bry.z>1.0f) ||
					(fabs(bry.x + bry.y + bry.z - 1.0f) > EPSILON) ))
					{
					float tz = a.z * bry.x + b.z * bry.y + c.z * bry.z; 
					if  (tz < hitPoint.z )  ct++;
					
					}
				}
			}
		}	
	}
if ((ct%2) == 1) return TRUE;
else return FALSE;

}