Exemple #1
0
void ParticleGroup::MakeCloud(int nParticle,const YsVec3 &min,const YsVec3 &max)
{
	particle.resize(nParticle);
	for(auto &p : particle)
	{
		double s=(double)rand()/(double)RAND_MAX;
		double t=(double)rand()/(double)RAND_MAX;
		double u=(double)rand()/(double)RAND_MAX;

		double x=min.x()*(1.0-s)+max.x()*s;
		double y=min.y()*(1.0-t)+max.y()*t;
		double z=min.z()*(1.0-u)+max.z()*u;

		p.pos.Set(x,y,z);

		p.t=0.0;
		p.tRemain=1.0;

		// Assume 4x4 texture atlas
		s=0.25*(double)(rand()%4);
		t=0.75;
		p.texCoordRange[0]=(float)s;
		p.texCoordRange[1]=(float)t;
		p.texCoordRange[2]=(float)s+0.25;
		p.texCoordRange[3]=(float)t+0.25;
	}
}
Exemple #2
0
void YsClassSample::Display(void)
{
	BIPOSATT biEyePos;
	BIPOINT cursor;

	eyePos.Set(0.0,0.0,-eyeDistance);
	eyePos=eyeAtt.GetMatrix()*eyePos;
	eyePos=eyeLookAt+eyePos;

	BiSetPoint(&biEyePos.p,eyePos.x(),eyePos.y(),eyePos.z());
	BiSetAngleDeg(&biEyePos.a,
	              YsRadToDeg(eyeAtt.h()),
	              YsRadToDeg(eyeAtt.p()),
	              YsRadToDeg(eyeAtt.b()));

	BiClearScreen();
	BiStartBuffer(&biEyePos);

	BiSetPoint(&cursor,
	           pointOfInterest.x(),
	           pointOfInterest.y(),
	           pointOfInterest.z());
	BiInsMarker(&cursor,&BiWhite,BIMK_CROSS);
	DrawSlashedPolygon();
	DrawPolygon(4,q,blu,YSFALSE);
	DrawPolygon(4,r,blu,YSFALSE);
	DrawAxis(5.0);

	BiFlushBuffer();
	BiSwapBuffers();
}
Exemple #3
0
YSRESULT YsZClip(YsVec3 &clipPos,const YsVec3 &p1,const YsVec3 &p2,const double &clipz)
{
	const double denom=(p2.z()-p1.z());
	if(YsTolerance<=fabs(denom))
	{
		const YsVec3 v=p2-p1;
		const double t=(clipz-p1.z())/denom;
		clipPos.Set(p1[0]+t*v[0],p1[1]+t*v[1],clipz);
		return YSOK;
	}
	return YSERR;
}
Exemple #4
0
YSRESULT YsClipLineWithNearZ(
	YsVec3 &newP1,YsVec3 &newP2,const YsVec3 &p1,const YsVec3 &p2,const double &nearz,YSBOOL positivez)
{
	class TestIsFrontSide
	{
	public:
		inline YSBOOL IsFrontSide(const double tstz,const double nearz,YSBOOL positivez)
		{
			if(YSTRUE==positivez && nearz<=tstz)
			{
				return YSTRUE;
			}
			else if(YSTRUE!=positivez && tstz<=nearz)
			{
				return YSTRUE;
			}
			return YSFALSE;
		}
	};

	TestIsFrontSide tester;

	if(YSTRUE==tester.IsFrontSide(p1.z(),nearz,positivez) &&
	   YSTRUE==tester.IsFrontSide(p2.z(),nearz,positivez))
	{
		newP1=p1;
		newP2=p2;
		return YSOK;
	}
	else if(YSTRUE==tester.IsFrontSide(p1.z(),nearz,positivez) ||
	        YSTRUE==tester.IsFrontSide(p2.z(),nearz,positivez))
	{
		YsVec3 clipPos;
		if(YSOK==YsZClip(clipPos,p1,p2,nearz))
		{
			if(YSTRUE==tester.IsFrontSide(p2.z(),nearz,positivez))
			{
				newP1=clipPos;
				newP2=p2;
				return YSOK;
			}
			else
			{
				newP1=p1;
				newP2=clipPos;
				return YSOK;
			}
		}
	}
	return YSERR;
}
Exemple #5
0
void YsClassSample::DrawNearestPointInsideOrOutside(void)
{
	YsPlane pln;
	YsVec3 nearestPoint;
	YSSIDE sd;
	BIPOINT cursor;

	pln.MakeBestFitPlane(4,p);
	pln.GetNearestPoint(nearestPoint,pointOfInterest);

	sd=YsCheckInsidePolygon3(nearestPoint,4,p);
	BiSetPoint(&cursor,nearestPoint.x(),nearestPoint.y(),nearestPoint.z());
	switch(sd)
	{
	case YSINSIDE:
		BiInsMarker(&cursor,&BiGreen,BIMK_CROSS);
		break;
	case YSOUTSIDE:
		BiInsMarker(&cursor,&BiBlue,BIMK_CROSS);
		break;
	case YSBOUNDARY:
		BiInsMarker(&cursor,&BiYellow,BIMK_CROSS);
		break;
	case YSUNKNOWNSIDE:
		BiInsMarker(&cursor,&BiRed,BIMK_CROSS);
		break;
	}
}
Exemple #6
0
void YsBoundingBoxMaker3::Add(const YsVec3 &vec)
{
	if(first!=YSTRUE)
	{
		min.Set
		   (YsSmaller(min.x(),vec.x()),
		    YsSmaller(min.y(),vec.y()),
		    YsSmaller(min.z(),vec.z()));
		max.Set
		   (YsGreater(max.x(),vec.x()),
		    YsGreater(max.y(),vec.y()),
		    YsGreater(max.z(),vec.z()));
	}
	else
	{
		Begin(vec);
	}
}
Exemple #7
0
void Display(void)
{
	double matBuf[4*4];
	YsVec3 eyePos;
	YsMatrix4x4 eyeTfm;
	YsVec3 p;

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	eyePos.Set(0.0,0.0,eyeDistance);
	eyePos=eyeAtt.GetMatrix()*eyePos;

	eyeTfm.Initialize();
	eyeTfm.Translate(eyePos);
	eyeTfm.Rotate(eyeAtt);
	eyeTfm.Invert();
	eyeTfm.Translate(-eyeLookAt);

	eyeTfm.GetArray(matBuf);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glMultMatrixd(matBuf);
	DrawAxis(5.0);

	glDisable(GL_LIGHTING);
	glColor3d(1.0,1.0,1.0);
	glBegin(GL_LINES);
	glVertex3d(0.0,0.0,0.0);
	glVertex3d(axisOfOrbit.x()*5.0,axisOfOrbit.y()*5.0,axisOfOrbit.z()*5.0);
	glEnd();

	rot.RotatePositive(p,orbiter);
	glPushMatrix();
	glTranslatef(p.x()*5.0,p.y()*5.0,p.z()*5.0);
	DrawCursor();
	glPopMatrix();

	glFlush();
	glutSwapBuffers();
}
void YsTransform3DCoordToScreenCoord(YsVec3 &scrn,const YsVec3 &from,const int viewport[4],const YsMatrix4x4 &projViewModelTfm)
{
	double pos4d[4]={from.x(),from.y(),from.z(),1.0};
	projViewModelTfm.Mul(pos4d,pos4d);

	pos4d[0]/=pos4d[3];
	pos4d[1]/=pos4d[3];
	pos4d[2]/=pos4d[3];

	pos4d[0]=(double)viewport[0]+(double)viewport[2]*(pos4d[0]+1.0)/2.0;
	pos4d[1]=(double)viewport[1]+(double)viewport[3]*(pos4d[1]+1.0)/2.0;

	scrn.Set(pos4d[0],pos4d[1],pos4d[2]);
}
YSRESULT YsShell::SaveMs3(int &nIgnored,const char fn[]) const
{
	FILE *fp;
	YsShellVertexHandle vtHd;
	YsShellPolygonHandle plHd;

	Encache();
	nIgnored=0;

	fp=fopen(fn,"w");
	if(fp!=NULL)
	{
		vtHd=NULL;
		while((vtHd=FindNextVertex(vtHd))!=NULL)
		{
			YsVec3 pos;
			GetVertexPosition(pos,vtHd);
			fprintf(fp,"V %lf %lf %lf\n",pos.x(),pos.y(),pos.z());
		}

		plHd=NULL;
		while((plHd=FindNextPolygon(plHd))!=NULL)
		{
			int n;
			const YsShellVertexHandle *plVtHd;
			n=GetNumVertexOfPolygon(plHd);
			plVtHd=GetVertexListOfPolygon(plHd);

			if(n==3)
			{
				fprintf(fp,"D %d %d %d\n",   // D of Delta
				    GetVertexIdFromHandle(plVtHd[0]),
				    GetVertexIdFromHandle(plVtHd[1]),
				    GetVertexIdFromHandle(plVtHd[2]));
			}
			else if(n==4)
			{
				fprintf(fp,"Q %d %d %d %d\n",
				    GetVertexIdFromHandle(plVtHd[0]),
				    GetVertexIdFromHandle(plVtHd[1]),
				    GetVertexIdFromHandle(plVtHd[2]),
				    GetVertexIdFromHandle(plVtHd[3]));
			}
		}

		fclose(fp);
		return YSOK;
	}
	return YSERR;
}
void FsGui3DInterface::DrawRectMarker(const YsVec3 &o,const double lng) const
{
	YsVec3 scrnO;
	YsTransform3DCoordToScreenCoord(scrnO,o,viewport,projViewModelTfm);

	YsVec3 quad[4]=
	{
		YsVec3(scrnO.x()-markerSize,scrnO.y()-markerSize,scrnO.z()),
		YsVec3(scrnO.x()+markerSize,scrnO.y()-markerSize,scrnO.z()),
		YsVec3(scrnO.x()+markerSize,scrnO.y()+markerSize,scrnO.z()),
		YsVec3(scrnO.x()-markerSize,scrnO.y()+markerSize,scrnO.z())
	};
	YsTransformScreenCoordTo3DCoord(quad[0],quad[0],viewport,projViewModelTfm);
	YsTransformScreenCoordTo3DCoord(quad[1],quad[1],viewport,projViewModelTfm);
	YsTransformScreenCoordTo3DCoord(quad[2],quad[2],viewport,projViewModelTfm);
	YsTransformScreenCoordTo3DCoord(quad[3],quad[3],viewport,projViewModelTfm);

	glBegin(GL_LINE_LOOP);
	glVertex3dv(quad[0]);
	glVertex3dv(quad[1]);
	glVertex3dv(quad[2]);
	glVertex3dv(quad[3]);
	glEnd();
}
Exemple #11
0
void YsTransformScreenCoordTo3DCoord(YsVec3 &threeD,const YsVec3 &scrn,const int viewport[4],const YsMatrix4x4 &projViewModelTfm)
{
	double pos4d[4]=
	{
		(scrn.x()-(double)viewport[0])*2.0/(double)viewport[2]-1.0,
		(scrn.y()-(double)viewport[1])*2.0/(double)viewport[3]-1.0,
		scrn.z(),
		1.0
	};

	projViewModelTfm.MulInverse(pos4d,pos4d);

	pos4d[0]/=pos4d[3];
	pos4d[1]/=pos4d[3];
	pos4d[2]/=pos4d[3];
	threeD.Set(pos4d[0],pos4d[1],pos4d[2]);
}
Exemple #12
0
void DrawShell(YsShell &shell,YsColor &col,YSBOOL inPolygon)
{
	int i,j,k;
	int nVtx,nPlg;
	YsVec3 vtx[256];

	if(inPolygon==YSTRUE)
	{
		glEnable(GL_LIGHTING);
	}
	else
	{
		glDisable(GL_LIGHTING);
	}

	nPlg=shell.GetNumPolygon();
	for(i=0; i<nPlg; i++)
	{
		nVtx=shell.GetNumVertexOfPolygon(i);
		if(nVtx>0)
		{
			double r,g,b;
			YsVec3 nom;

			shell.GetVertexListOfPolygon(vtx,256,i);
			nom=(vtx[1]-vtx[0])^(vtx[2]-vtx[1]);
			nom.Normalize();

			col.GetDoubleRGB(r,g,b);

			glColor3d(r,g,b);

			if(inPolygon==YSFALSE || YsCheckConvex3(nVtx,vtx)==YSTRUE)
			{
				switch(inPolygon)
				{
				case YSFALSE:
					glBegin(GL_LINE_LOOP);
					break;
				case YSTRUE:
					glBegin(GL_POLYGON);
					break;
				}
				glNormal3d(nom.x(),nom.y(),nom.z());
				for(j=0; j<nVtx; j++)
				{
					glVertex3d(vtx[j].x(),vtx[j].y(),vtx[j].z());
				}
				glEnd();
			}
			else
			{
				YsSword swd;
				swd.SetInitialPolygon(nVtx,vtx);
				swd.Convexnize();
				for(j=0; j<swd.GetNumPolygon(); j++)
				{
					nVtx=swd.GetNumVertexOfPolygon(j);
					swd.GetVertexListOfPolygon(vtx,256,j);
					glBegin(GL_POLYGON);
					for(k=0; k<nVtx; k++)
					{
						glVertex3dv(vtx[k].GetValue());
					}
					glEnd();
				}
			}
		}
	}
}
Exemple #13
0
double  Cylinder::side_fun(YsVec3 r)const
{
    return pow((r.x()-origin.x()),2.0)+pow((r.z()-origin.z()), 2.0)-pow(radius, 2.0);
}
YSRESULT YsShell::SaveMsh(int &nIgnored,const char fn[]) const
{
	FILE *fp;
	YsShellVertexHandle vtHd;
	YsShellPolygonHandle plHd;

	Encache();
	nIgnored=0;

	fp=fopen(fn,"w");
	if(fp!=NULL)
	{
		int id,nofel2;

		fprintf(fp,"nofnod %d\n",GetNumVertex());

		id=1;
		vtHd=NULL;
		while((vtHd=FindNextVertex(vtHd))!=NULL)
		{
			YsVec3 pos;
			GetVertexPosition(pos,vtHd);
			fprintf(fp,"nod %d %lf %lf %lf\n",id,pos.x(),pos.y(),pos.z());
			id++;
		}

		nofel2=0;
		plHd=NULL;
		while((plHd=FindNextPolygon(plHd))!=NULL)
		{
			if(GetNumVertexOfPolygon(plHd)==3 || GetNumVertexOfPolygon(plHd)==4)
			{
				nofel2++;
			}
			else
			{
				nIgnored++;
			}
		}
		fprintf(fp,"nofel2 %d\n",nofel2);

		id=1;
		plHd=NULL;
		while((plHd=FindNextPolygon(plHd))!=NULL)
		{
			int i,n;
			const YsShellVertexHandle *plVtHd;
			n=GetNumVertexOfPolygon(plHd);
			plVtHd=GetVertexListOfPolygon(plHd);

			if(n==3 || n==4)
			{
				fprintf(fp,"el2 %d %d",id,n);
				for(i=0; i<n; i++)
				{
					fprintf(fp," %d",GetVertexIdFromHandle(plVtHd[i])+1);
				}
				fprintf(fp,"\n");
				id++;
			}
		}

		fclose(fp);
		return YSOK;
	}
	return YSERR;
}
YSRESULT YsShell::SaveSrf(
    class YsTextOutputStream &textOut,
    YSSIZE_T nNoShadingPolygon,const YsShellPolygonHandle noShadingPolygon[],
    YSSIZE_T nRoundVtx,const YsShellVertexHandle roundVtx[]) const
{
	YsShellVertexHandle vtHd,tstVtHd;
	YsShellPolygonHandle plHd,tstPlHd;

	YsHashTable <YsShellVertexHandle> roundVtxTable(nRoundVtx+1);
	YsHashTable <YsShellPolygonHandle> noShadingPolygonTable(nNoShadingPolygon+1);

	int i;
	if(noShadingPolygon!=NULL)
	{
		for(i=0; i<nNoShadingPolygon; i++)
		{
			noShadingPolygonTable.AddElement(GetSearchKey(noShadingPolygon[i]),noShadingPolygon[i]);
		}
	}
	if(roundVtx!=NULL)
	{
		for(i=0; i<nRoundVtx; i++)
		{
			roundVtxTable.AddElement(GetSearchKey(roundVtx[i]),roundVtx[i]);
		}
	}


	Encache();


	textOut.Printf("SURF\n");

	vtHd=NULL;
	while((vtHd=FindNextVertex(vtHd))!=NULL)
	{
		YsVec3 pos;
		GetVertexPosition(pos,vtHd);

		if(roundVtxTable.FindElement(tstVtHd,GetSearchKey(vtHd))!=YSOK)
		{
			textOut.Printf("V %lf %lf %lf\n",pos.x(),pos.y(),pos.z());
		}
		else
		{
			textOut.Printf("V %lf %lf %lf R\n",pos.x(),pos.y(),pos.z());
		}
	}

	plHd=NULL;
	while((plHd=FindNextPolygon(plHd))!=NULL)
	{
		int i,n;
		YsVec3 cen,nom,pos;
		const YsShellVertexHandle *plVtHd;
		n=GetNumVertexOfPolygon(plHd);
		plVtHd=GetVertexListOfPolygon(plHd);

		textOut.Printf("F\n");

		if(noShadingPolygonTable.FindElement(tstPlHd,GetSearchKey(plHd))==YSOK)
		{
			textOut.Printf("B\n");
		}

		cen.Set(0.0,0.0,0.0);
		for(i=0; i<n; i++)
		{
			if(i%8==0)
			{
				textOut.Printf("V");
			}

			textOut.Printf(" %d",GetVertexIdFromHandle(plVtHd[i]));

			if(i%8==7 || i==n-1)
			{
				textOut.Printf("\n");
			}

			GetVertexPosition(pos,plVtHd[i]);
			cen+=pos;
		}

		cen/=double(n);
		GetNormalOfPolygon(nom,plHd);


		YsColor col;
//		unsigned int r,g,b;
		GetColorOfPolygon(col,plHd);
//		r=YsBound(int(col.Rd()*31.0),0,31);
//		g=YsBound(int(col.Gd()*31.0),0,31);
//		b=YsBound(int(col.Bd()*31.0),0,31);
//		textOut.Printf("C %d\n",g*1024+r*32+b);
		textOut.Printf("C %d\n",col.Get15BitColor());

		textOut.Printf("N %lf %lf %lf %lf %lf %lf\n",cen.x(),cen.y(),cen.z(),nom.x(),nom.y(),nom.z());
		textOut.Printf("E\n");
	}

	textOut.Printf("E\n");
	return YSOK;
}