Esempio n. 1
0
//creates a Mesh with the appropriate values and returns its pointer
//calculates the rotation
Mesh*
Surfrev::createMesh() {
    Mesh* m = new Mesh(); //empty mesh
    float degrees = 360/slices;
    float angle = 0;
    vec3 Y(0,1,0);
    //inserts all points doing the rotation around the y-axis
    for(int i=0; i<slices; i++) {
        for(int j=0; j<points; j++) {
            vec3 v = rotation3D(Y,angle)*polyline[j];
            //cout<<"X = "<<v[0]<<" Y = "<<v[1] <<" Z = "<< v[2]<<endl;
            m->addVertex(v);
        }
        angle += degrees;
    }
    for(int i=0; i<(slices*points)-1; i++) {
        //finds normal for face that is going to be inserted to the mesh
        if(i<((slices*points)-(points+1))) {
            int mod = (1+i)%points;
            if(mod != 0) {
                /*cout<<"--------\nPoint: "<<i<<endl;
                cout<<"i+1: "<<i+1<<endl;
                cout<<"i+1+points: "<<i+1+points<<endl;
                cout<<"i+points: "<<i+points<<"\n"<<endl;
                cout<<(mod)<<" = Mod\n\n";*/
            }

            vec4 vert1(m->getVertex(i)[0],m->getVertex(i)[1],m->getVertex(i)[2],1);
            vec4 vert2(m->getVertex(i+1)[0],m->getVertex(i+1)[1],m->getVertex(i+1)[2],1);
            vec4 vert3(m->getVertex(i+points+1)[0],m->getVertex(i+points+1)[1],m->getVertex(i+points+1)[2],1);
            //check for final point that has  x = 0: triangle not square
            if(((i+2)%points) == 0 && m->getVertex(i+1)[0] == 0) {
                vert3[0] = m->getVertex(i+points)[0];
                vert3[1] = m->getVertex(i+points)[1];
                vert3[2] = m->getVertex(i+points)[2];
            }


            vec4 n = getNormal(vert1,vert2,vert3);
            vec3 normal(n[0],n[1],n[2]);

            if(mod != 0) {
                /*std::cout<<"Normal x = "<<normal[0]<<std::endl;
                std::cout<<"Normal y = "<<normal[1]<<std::endl;
                std::cout<<"Normal z = "<<normal[2]<<std::endl;*/
            }
            m->addNormal(normal);
            ////creates the faces, normal index is = i, always have 4 vertices for a face
            vec2 f1((double)i,(double)i);
            vec2 f2((double)i+1,(double)i);
            vec2 f3((double)points+i+1,(double)i);
            vec2 f4((double)points+i,(double)i);
            std::vector<vec2> face;
            face.push_back(f2);
            face.push_back(f1);
            face.push_back(f4);
            face.push_back(f3);


            if(mod != 0) {
                //cout<<"add face: "<<i<<endl;
                m->addFace(face);
            }
        }
        //case: do last face
        else {
            int mod = (1+i)%points;
            int k = (points*slices);
            /*cout<<"Else Point: "<<i<<endl;
            cout<<"i+1: "<<i+1<<endl;
            cout<<"i+1+points: "<<i+1+points-k<<endl;
            cout<<"i+points: "<<i+points-k<<"\n"<<endl;
            cout<<(mod)<<" = Mod\n\n";*/

            vec4 vert1L(m->getVertex(i)[0],m->getVertex(i)[1],m->getVertex(i)[2],1);
            vec4 vert2L(m->getVertex(i+1)[0],m->getVertex(i+1)[1],m->getVertex(i+1)[2],1);
            vec4 vert3L(m->getVertex(i+points+1-k)[0],m->getVertex(i+points+1-k)[1],m->getVertex(i+points+1-k)[2],1);
            if(((i+2)%points) == 0 && m->getVertex(i+1)[0] == 0) {
                vert3L[0] = m->getVertex(i+points-k)[0];
                vert3L[1] = m->getVertex(i+points-k)[1];
                vert3L[2] = m->getVertex(i+points-k)[2];
            }
            vec4 nL = getNormal(vert1L,vert2L,vert3L);
            vec3 normalL(nL[0],nL[1],nL[2]);

            /*std::cout<<"--------\nNormal x = "<<normal[0]<<std::endl;
            std::cout<<"Normal y = "<<normal[1]<<std::endl;
            std::cout<<"Normal z = "<<normal[2]<<std::endl;*/
            m->addNormal(normalL);
            //creates the faces, normal index is = i, always have 4 vertices for a face
            vec2 f1L((double)i,(double)i);
            vec2 f2L((double)i+1,(double)i);
            vec2 f3L((double)points+i+1-k,(double)i);
            vec2 f4L((double)points+i-k,(double)i);
            std::vector<vec2> faceL;
            faceL.push_back(f2L);
            faceL.push_back(f1L);
            faceL.push_back(f4L);
            faceL.push_back(f3L);


            if(mod != 0) {
                //cout<<"add face: "<<i<<endl;
                m->addFace(faceL);
            }
        }
    }
    //check if there is an opening at the top or bottom to cap or not
    if(polyline[0][0] != 0) {	//cap the bottom
        /*cout<<"X1 = "<<m->getVertex(0)[0]<<"  Y1 = "<<m->getVertex(0)[1]<<"  Z1 = "<<m->getVertex(points-1)[2]<<endl;
        cout<<"X2 = "<<m->getVertex((points))[0]<<"  Y2 = "<<m->getVertex((points))[1]<<"  Z2 = "<<m->getVertex((points))[2]<<endl;
        cout<<"X3 = "<<m->getVertex((points*2))[0]<<"  Y3 = "<<m->getVertex((points*2))[1]<<"  Z3 = "<<m->getVertex((points*2))[2]<<"\n"<<endl;*/

        vec4 vert1(m->getVertex(0)[0],m->getVertex(0)[1],m->getVertex(0)[2],1);
        vec4 vert2(m->getVertex(points)[0],m->getVertex(points)[1],m->getVertex(points)[2],1);
        vec4 vert3(m->getVertex(points*2)[0],m->getVertex(points*2)[1],m->getVertex(points*2)[2],1);
        vec4 n = getNormal(vert1,vert2,vert3);
        vec3 normal(n[0],n[1],n[2]);

        m->addNormal(normal);
        std::vector<vec2> face;
        for(int k=0; k<slices; k++) {
            vec2 f((double)points*k,(double)m->lastNorm());
            face.push_back(f);
        }
        m->addFace(face);
    }

    if(polyline[points-1][0] != 0) {	//cap the top
        /*cout<<"Top\nX1 = "<<m->getVertex(points-1)[0]<<"  Y1 = "<<m->getVertex(points-1)[1]<<"  Z1 = "<<m->getVertex(points-1)[2]<<endl;
        cout<<"X2 = "<<m->getVertex((points*2)-1)[0]<<"  Y2 = "<<m->getVertex((points*2)-1)[1]<<"  Z2 = "<<m->getVertex((points*2)-1)[2]<<endl;
        cout<<"X3 = "<<m->getVertex((points*3)-1)[0]<<"  Y3 = "<<m->getVertex((points*3)-1)[1]<<"  Z3 = "<<m->getVertex((points*3)-1)[2]<<"\n"<<endl;*/

        vec4 vert1(m->getVertex(points-1)[0],m->getVertex(points-1)[1],m->getVertex(points-1)[2],1);
        vec4 vert2(m->getVertex((points*2)-1)[0],m->getVertex((points*2)-1)[1],m->getVertex((points*2)-1)[2],1);
        vec4 vert3(m->getVertex((points*3)-1)[0],m->getVertex((points*3)-1)[1],m->getVertex((points*3)-1)[2],1);
        vec4 n = getNormal(vert1,vert2,vert3);
        n = n * (-1);
        vec3 normal(n[0],n[1],n[2]);

        m->addNormal(normal);
        std::vector<vec2> face;
        for(int k=1; k<slices+1; k++) {
            vec2 f((double)(points*k)-1,(double)m->lastNorm());
            face.push_back(f);
        }
        m->addFace(face);
    }


    return m;

}
Esempio n. 2
0
Mesh*
Extrude::createMesh(){
	cout<<isConvex()<<" - True = "<<true<<endl;
	
	Mesh* m = new Mesh(isConvex()); //empty mesh
	vector<vec3> polygon_top(polygon);	//a copy of polygon
	
	
	//inserts polygon to mesh
	//so for polygon the index is k
	for(int j = 0; j< sides; j++){
		m->addVertex(polygon[j]);
	}
	//add y coordinate to polygon_top and inserts it to mesh
	//so for top polygon the index is k+sides
	for(int i = 0; i<sides; i++){
		polygon_top[i][1] = distance;
		m->addVertex(polygon_top[i]);
	}
	for(int k = 0; k<sides; k++){
		if(k<(sides-1)){
			//finds normal for face that is going to be inserted to the mesh
			vec4 vert1(polygon[k][0],polygon[k][1],polygon[k][2],1);
			vec4 vert2(polygon[k+1][0],polygon[k+1][1],polygon[k+1][2],1);
			vec4 vert3(polygon_top[k+1][0],polygon_top[k+1][1],polygon_top[k+1][2],1);
			vec4 n = getNormal(vert1,vert2,vert3);
			vec3 normal(n[0],n[1],n[2]);

			
			/*std::cout<<"--------\nNormal x = "<<normal[0]<<std::endl;
			std::cout<<"Normal y = "<<normal[1]<<std::endl;
			std::cout<<"Normal z = "<<normal[2]<<std::endl;*/
			m->addNormal(normal);
			//creates the faces, normal index is = k, always have 4 vertices for a face
			vec2 f1((double)k,(double)k);
			vec2 f2((double)k+1,(double)k);
			vec2 f3((double)sides+k+1,(double)k);
			vec2 f4((double)sides+k,(double)k);
			std::vector<vec2> face;
			face.push_back(f1);
			face.push_back(f2);
			face.push_back(f3);
			face.push_back(f4);
			m->addFace(face);
		}

		//case: need to do last one too
		if(k == (sides-1)){
			vec4 vert1L(polygon[k][0],polygon[k][1],polygon[k][2],1);
			vec4 vert2L(polygon[0][0],polygon[0][1],polygon[0][2],1);
			vec4 vert3L(polygon_top[0][0],polygon_top[0][1],polygon_top[0][2],1);
			vec4 nL = getNormal(vert1L,vert2L,vert3L);
			vec3 normalL(nL[0],nL[1],nL[2]);
			m->addNormal(normalL);
			//creates the faces, normal index is = k, always have 4 vertices for a face
			vec2 f1L((double)k,(double)k);
			vec2 f2L((double)0,(double)k);
			vec2 f3L((double)sides,(double)k);
			vec2 f4L((double)sides+k,(double)k);
			std::vector<vec2> faceL;
			faceL.push_back(f1L);
			faceL.push_back(f2L);
			faceL.push_back(f3L);
			faceL.push_back(f4L);
			m->addFace(faceL);
		}

	}

	//check if convex to cap or not
	if(isConvex()){
		std::vector<vec2> cap;
		std::vector<vec2> cap_top;
		//add the two normals at side and side+1
		vec4 vert1(polygon[0][0],polygon[0][1],polygon[0][2],1);
		vec4 vert2(polygon[1][0],polygon[1][1],polygon[1][2],1);
		vec4 vert3(polygon[2][0],polygon[2][1],polygon[2][2],1);
		vec4 n = getNormal(vert1,vert2,vert3);
		n = n*(-1);
		vec3 normal(n[0],n[1],n[2]);
		m->addNormal(normal);
		vec4 vert1_top(polygon_top[0][0],polygon_top[0][1],polygon_top[0][2],1);
		vec4 vert2_top(polygon_top[1][0],polygon_top[1][1],polygon_top[1][2],1);
		vec4 vert3_top(polygon_top[2][0],polygon_top[2][1],polygon_top[2][2],1);
		vec4 n_top = getNormal(vert1_top,vert2_top,vert3_top);
		vec3 normal_top(n_top[0],n_top[1],n_top[2]);
		m->addNormal(normal_top);
		//adds the faces, vertices for bottom cap = polygon(i)
		//for top cap = polygon_top(i+side)
		for(int i = 0; i<sides; i++){
			vec2 f((double)i,(double)sides);
			cap.push_back(f);
			vec2 f_top((double)i+sides,(double)sides+1);
			cap_top.push_back(f_top);
		}
		m->addFace(cap);
		m->addFace(cap_top);
	}
	
	return m;

}
Esempio n. 3
0
int main(int argc,char** argv)
{

	setCameraDistance(30.f);

#define TRISIZE 10.f
#ifdef DEBUG_MESH
	SimdVector3 vert0(-TRISIZE ,0,TRISIZE );
	SimdVector3 vert1(TRISIZE ,10,TRISIZE );
	SimdVector3 vert2(TRISIZE ,0,-TRISIZE );
	meshData.AddTriangle(vert0,vert1,vert2);
	SimdVector3 vert3(-TRISIZE ,0,TRISIZE );
	SimdVector3 vert4(TRISIZE ,0,-TRISIZE );
	SimdVector3 vert5(-TRISIZE ,0,-TRISIZE );
	meshData.AddTriangle(vert3,vert4,vert5);
#else
#ifdef ODE_MESH
	SimdVector3 Size = SimdVector3(15.f,15.f,12.5f);
	
  gVertices[0][0] = -Size[0];
  gVertices[0][1] = Size[2];
  gVertices[0][2] = -Size[1];
  
  gVertices[1][0] = Size[0];
  gVertices[1][1] = Size[2];
  gVertices[1][2] = -Size[1];
  
  gVertices[2][0] = Size[0];
  gVertices[2][1] = Size[2];
  gVertices[2][2] = Size[1];  

  gVertices[3][0] = -Size[0];
  gVertices[3][1] = Size[2];
  gVertices[3][2] = Size[1];
  
  gVertices[4][0] = 0;
  gVertices[4][1] = 0;
  gVertices[4][2] = 0;
  
  gIndices[0] = 0;
  gIndices[1] = 1;
  gIndices[2] = 4;
  
  gIndices[3] = 1;
  gIndices[4] = 2;
  gIndices[5] = 4;
  
  gIndices[6] = 2;
  gIndices[7] = 3;
  gIndices[8] = 4;
  
  gIndices[9] = 3;
  gIndices[10] = 0;
  gIndices[11] = 4;

  int vertStride = sizeof(SimdVector3);
  int indexStride = 3*sizeof(int);

	TriangleIndexVertexArray* indexVertexArrays = new TriangleIndexVertexArray(NUM_TRIANGLES,
		gIndices,
		indexStride,
		NUM_VERTICES,(float*) &gVertices[0].x(),vertStride);

	//shapePtr[4] = new TriangleMeshShape(indexVertexArrays);
	shapePtr[4] = new BvhTriangleMeshShape(indexVertexArrays);
#else

	int vertStride = sizeof(SimdVector3);
	int indexStride = 3*sizeof(int);

	const int NUM_VERTS_X = 50;
	const int NUM_VERTS_Y = 50;
	const int totalVerts = NUM_VERTS_X*NUM_VERTS_Y;
	
	const int totalTriangles = 2*(NUM_VERTS_X-1)*(NUM_VERTS_Y-1);

	SimdVector3*	gVertices = new SimdVector3[totalVerts];
	int*	gIndices = new int[totalTriangles*3];

	int i;

	for ( i=0;i<NUM_VERTS_X;i++)
	{
		for (int j=0;j<NUM_VERTS_Y;j++)
		{
			gVertices[i+j*NUM_VERTS_X].setValue((i-NUM_VERTS_X*0.5f)*10.f,2.f*sinf((float)i)*cosf((float)j),(j-NUM_VERTS_Y*0.5f)*10.f);
		}
	}

	int index=0;
	for ( i=0;i<NUM_VERTS_X-1;i++)
	{
		for (int j=0;j<NUM_VERTS_Y-1;j++)
		{
			gIndices[index++] = j*NUM_VERTS_X+i;
			gIndices[index++] = j*NUM_VERTS_X+i+1;
			gIndices[index++] = (j+1)*NUM_VERTS_X+i+1;

			gIndices[index++] = j*NUM_VERTS_X+i;
			gIndices[index++] = (j+1)*NUM_VERTS_X+i+1;
			gIndices[index++] = (j+1)*NUM_VERTS_X+i;
		}
	}
	
	TriangleIndexVertexArray* indexVertexArrays = new TriangleIndexVertexArray(totalTriangles,
		gIndices,
		indexStride,
		totalVerts,(float*) &gVertices[0].x(),vertStride);

	//shapePtr[4] = new TriangleMeshShape(indexVertexArrays);
	shapePtr[4] = new BvhTriangleMeshShape(indexVertexArrays);
#endif

	

#endif//DEBUG_MESH


//	GLDebugDrawer	debugDrawer;

	//ConstraintSolver* solver = new SimpleConstraintSolver;
	ConstraintSolver* solver = new OdeConstraintSolver;

	CollisionDispatcher* dispatcher = new	CollisionDispatcher();
		
	BroadphaseInterface* broadphase = new SimpleBroadphase();


	physicsEnvironmentPtr = new CcdPhysicsEnvironment(dispatcher,broadphase);
	

	physicsEnvironmentPtr->setGravity(-1,-10,1);
	PHY_ShapeProps shapeProps;
	
	shapeProps.m_do_anisotropic = false;
	shapeProps.m_do_fh = false;
	shapeProps.m_do_rot_fh = false;
	shapeProps.m_friction_scaling[0] = 1.;
	shapeProps.m_friction_scaling[1] = 1.;
	shapeProps.m_friction_scaling[2] = 1.;

	shapeProps.m_inertia = 1.f;
	shapeProps.m_lin_drag = 0.95999998f;
	shapeProps.m_ang_drag = 0.89999998f;
	shapeProps.m_mass = 1.0f;
	
	PHY_MaterialProps materialProps;
	materialProps.m_friction = 0.f;// 50.5f;
	materialProps.m_restitution = 0.1f;

	CcdConstructionInfo ccdObjectCi;
	ccdObjectCi.m_friction = 0.f;//50.5f;

	ccdObjectCi.m_linearDamping = shapeProps.m_lin_drag;
	ccdObjectCi.m_angularDamping = shapeProps.m_ang_drag;

	SimdTransform tr;
	tr.setIdentity();

	
	for (i=0;i<numObjects;i++)
	{
		if (i>0)
			shapeIndex[i] = 1;//2 = tetrahedron
		else
			shapeIndex[i] = 4;
	}
	for (i=0;i<numObjects;i++)
	{
		shapeProps.m_shape = shapePtr[shapeIndex[i]];

		bool isDyna = i>0;
		
		if (!i)
		{
			//SimdQuaternion orn(0,0,0.1*SIMD_HALF_PI);
			//ms[i].setWorldOrientation(orn.x(),orn.y(),orn.z(),orn[3]);
			//ms[i].setWorldPosition(0,-10,0);
		} else
		{
				ms[i].setWorldPosition(10,i*15-10,0);
		}
		
//either create a few stacks, to show several islands, or create 1 large stack, showing stability
		//ms[i].setWorldPosition((i*5) % 30,i*15-10,0);
		

		ccdObjectCi.m_MotionState = &ms[i];
		ccdObjectCi.m_gravity = SimdVector3(0,0,0);
		ccdObjectCi.m_localInertiaTensor =SimdVector3(0,0,0);
		if (!isDyna)
		{
			shapeProps.m_mass = 0.f;
			ccdObjectCi.m_mass = shapeProps.m_mass;
		}
		else
		{
			shapeProps.m_mass = 1.f;
			ccdObjectCi.m_mass = shapeProps.m_mass;
		}

		
		SimdVector3 localInertia;
		if (shapeProps.m_mass>0.f)
		{
			shapePtr[shapeIndex[i]]->CalculateLocalInertia(shapeProps.m_mass,localInertia);
		} else
		{
			localInertia.setValue(0.f,0.f,0.f);

		}
		ccdObjectCi.m_localInertiaTensor = localInertia;

		ccdObjectCi.m_collisionShape = shapePtr[shapeIndex[i]];


		physObjects[i]= new CcdPhysicsController( ccdObjectCi);
		physicsEnvironmentPtr->addCcdPhysicsController( physObjects[i]);

/*		if (i==0)
		{
			physObjects[i]->SetAngularVelocity(0,0,-2,true);
			physObjects[i]->GetRigidBody()->setDamping(0,0);
		}
*/
		//for the line that represents the AABB extents
//	physicsEnvironmentPtr->setDebugDrawer(&debugDrawer);

		
	}
	return glutmain(argc, argv,640,480,"Static Concave Mesh Demo");
}