⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clothsimulationview.cpp

📁 本程序描述的是一个三维织物动感模拟系统。计算机生成真实感服装被列入计算机图形学亟需解决的三大问题之一。
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	dialog.m_ShearDamp = SshD;
	dialog.m_BendCoef = SflK;
	dialog.m_BendDamp = SflD;
	dialog.m_USize = u;
	dialog.m_VSize = v;
	dialog.m_Vertical = !orientHoriz;
	dialog.m_UseStruct = TRUE;
	dialog.m_UseShear = TRUE;
	dialog.m_UseBend = TRUE;
	if (dialog.DoModal())
	{
		NewSystem();	// 清除系统
		SstK = dialog.m_StructCoef;
		SstD = dialog.m_StructDamp;
		SshK = dialog.m_ShearCoef;
		SshK = dialog.m_ShearDamp;
		SflK = dialog.m_BendCoef;
		SflD = dialog.m_BendDamp;
		u = dialog.m_USize;
		v = dialog.m_VSize;
		orientHoriz = !dialog.m_Vertical;

		sx = -(w / 2.0f);
		sy = (h / 2.0f);
		stepx = w / (float)u;
		stepy = -(h / (float)v);

		tsu = 0.0f;
		tsv = 0.0f;
		tdu = 1.0f / (float)u;
		tdv = 1.0f / (float)v;

		fPos = (u - 1) * (v - 1) * 2;
		vPos = u * v;
		visual = (t_Visual *)malloc(sizeof(t_Visual));

		visual->reuseVertices = TRUE;
		visual->dataFormat = GL_T2F_V3F;
		visual->vPerFace = 3;
		visual->vSize = 5;	
		visual->vertexData = (float *)malloc(sizeof(float) * visual->vSize * vPos);
		visual->vertexCnt = vPos;
		visual->faceIndex = (unsigned short *)malloc(sizeof(unsigned short) * fPos * visual->vPerFace);
		visual->faceCnt = fPos;
		
		// 设置节点
		vertex = (tTexturedVertex *)visual->vertexData;
		for (l1 = 0; l1 < v; l1++,tsv+=tdv)
			for (l2 = 0; l2 < u; l2++,tsu+=tdu)
			{
				vertex->u = tsu;
				vertex->v = tsv;

				vertex->x = sx + (stepx * l2);
				if (orientHoriz)
				{
					vertex->z = sy + (stepy * l1);
					vertex->y = 0.0f;
				}
				else
				{
					vertex->y = sy + (stepy * l1);
					vertex->z = 0.0f;
				}
				vertex++;
			}

		m_PhysEnv.SetWorldParticles((tTexturedVertex *)visual->vertexData,visual->vertexCnt);

		if (m_Skeleton.childCnt > 0)
		{
			if (m_Skeleton.children->visuals->faceIndex != NULL)
				free(m_Skeleton.children->visuals->faceIndex);
			free(m_Skeleton.children->visuals);
			free(m_Skeleton.children->visuals->vertexData);
			free(m_Skeleton.children);
			m_Skeleton.childCnt = 0;
		}
		children = (t_Bone *)malloc(sizeof(t_Bone));
		m_CurBone = &children[m_Skeleton.childCnt];
		ResetBone(m_CurBone,&m_Skeleton);
		strcpy(m_CurBone->name,"Cloth");
		m_CurBone->visuals = visual;
		m_CurBone->visualCnt = 1;
		m_Skeleton.childCnt = 1;
		m_Skeleton.children = children;

		if (dialog.m_UseStruct)
		{
			// 水平
			for (l1 = 0; l1 < v; l1++)	// v
				for (l2 = 0; l2 < (u - 1); l2++)
				{
					m_PhysEnv.AddSpring((l1 * u) + l2,(l1 * u) + l2 + 1,SstK,SstD,STRUCTURAL_SPRING);
				}

			// 垂直
			for (l1 = 0; l1 < (u); l1++)	
				for (l2 = 0; l2 < (v - 1); l2++)
				{
					m_PhysEnv.AddSpring((l2 * u) + l1,((l2 + 1) * u) + l1,SstK,SstD,STRUCTURAL_SPRING);
				}
		}

		if (dialog.m_UseShear)
		{
			// 剪切弹簧
			for (l1 = 0; l1 < (v - 1); l1++)	
				for (l2 = 0; l2 < (u - 1); l2++)
				{
					m_PhysEnv.AddSpring((l1 * u) + l2,((l1 + 1) * u) + l2 + 1,SshK,SshD,SHEAR_SPRING);
					m_PhysEnv.AddSpring(((l1 + 1) * u) + l2,(l1 * u) + l2 + 1,SshK,SshD,SHEAR_SPRING);
				}
		}

		if (dialog.m_UseBend)
		{
			// 弯曲弹簧
			for (l1 = 0; l1 < (v); l1++)	
			{
				for (l2 = 0; l2 < (u - 2); l2++)
				{
					m_PhysEnv.AddSpring((l1 * u) + l2,(l1 * u) + l2 + 2,SflK,SflD,BEND_SPRING);
				}
				m_PhysEnv.AddSpring((l1 * u) + (u - 3),(l1 * u) + (u - 1),SflK,SflD,BEND_SPRING);
			}
			for (l1 = 0; l1 < (u); l1++)	
			{
				for (l2 = 0; l2 < (v - 2); l2++)
				{
					m_PhysEnv.AddSpring((l2 * u) + l1,((l2 + 2) * u) + l1,SflK,SflD,BEND_SPRING);
				}
				m_PhysEnv.AddSpring(((v - 3) * u) + l1,((v - 1) * u) + l1,SflK,SflD,BEND_SPRING);
			}
		}
	}

}


void CClothSimulationView::OnFileCreateclothpatch() 
{
	// TODO: Add your command handler code here
	CreateClothPatch();
	Invalidate(TRUE);	
}

void CClothSimulationView::OnFileOpen() 
{
	// TODO: Add your command handler code here
	char szFilter[] = "DPS files (*.dps)|*.dps|OBJ files (*.obj)|*.obj||";  // WILL INCLUDE Biovision Hierarchy BVH (*.bvh)|*.bvh|
	CFileDialog	dialog( TRUE, ".obj", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter, this);
	CString name;		
	if (dialog.DoModal())
	{
		LoadFile(dialog.GetFileName( ),dialog.GetFileTitle( ),dialog.GetFileExt()  );
		Invalidate(TRUE);
	}	
}

void CClothSimulationView::OnFileSave() 
{
	// TODO: Add your command handler code here
	char szFilter[] = "DPS files (*.dps)|*.dps||";  // WILL INCLUDE Biovision Hierarchy BVH (*.bvh)|*.bvh|
	CFileDialog	dialog( FALSE, ".dps", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter, this);
	CString name;		
	if (dialog.DoModal())
	{
		SaveFile(dialog.GetFileName( ),dialog.GetFileTitle( ));
		Invalidate(TRUE);
	}	
}

void CClothSimulationView::OnFileNewsystem() 
{
	// TODO: Add your command handler code here
	NewSystem();	
}

void CClothSimulationView::OnViewShowsprings() 
{
	// TODO: Add your command handler code here
	m_PhysEnv.m_DrawSprings = !m_PhysEnv.m_DrawSprings;
	Invalidate(TRUE);	
}

void CClothSimulationView::OnUpdateViewShowsprings(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck( m_PhysEnv.m_DrawSprings );	
}

void CClothSimulationView::OnViewShowstructural() 
{
	// TODO: Add your command handler code here
	m_PhysEnv.m_DrawStructural = !m_PhysEnv.m_DrawStructural;
	Invalidate(TRUE);	
}

void CClothSimulationView::OnUpdateViewShowstructural(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck( m_PhysEnv.m_DrawStructural );	
}

void CClothSimulationView::OnViewShowshear() 
{
	// TODO: Add your command handler code here
	m_PhysEnv.m_DrawShear = !m_PhysEnv.m_DrawShear;
	Invalidate(TRUE);	
}

void CClothSimulationView::OnUpdateViewShowshear(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck( m_PhysEnv.m_DrawShear );	
}

void CClothSimulationView::OnViewShowbend() 
{
	// TODO: Add your command handler code here
	m_PhysEnv.m_DrawBend = !m_PhysEnv.m_DrawBend;
	Invalidate(TRUE);	
}

void CClothSimulationView::OnUpdateViewShowbend(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck( m_PhysEnv.m_DrawBend );	
}

void CClothSimulationView::OnViewShowgeometry() 
{
	// TODO: Add your command handler code here
	m_DrawGeometry = !m_DrawGeometry;
	Invalidate(TRUE);	
}

void CClothSimulationView::OnUpdateViewShowgeometry(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck( m_DrawGeometry );	
}

void CClothSimulationView::OnViewShowvertices() 
{
	// TODO: Add your command handler code here
	m_PhysEnv.m_DrawVertices = !m_PhysEnv.m_DrawVertices;
	Invalidate(TRUE);	
}

void CClothSimulationView::OnUpdateViewShowvertices(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck( m_PhysEnv.m_DrawVertices );	
}

void CClothSimulationView::OnViewCollisionactive() 
{
	// TODO: Add your command handler code here
	m_PhysEnv.m_CollisionActive = !m_PhysEnv.m_CollisionActive;
	Invalidate(TRUE);	
}

void CClothSimulationView::OnUpdateViewCollisionactive(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck( m_PhysEnv.m_CollisionActive );	
}

void CClothSimulationView::OnInteuler() 
{
	// TODO: Add your command handler code here
	m_PhysEnv.m_IntegratorType = EULER_INTEGRATOR;
	Invalidate(TRUE);	
}

void CClothSimulationView::OnUpdateInteuler(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck( m_PhysEnv.m_IntegratorType == EULER_INTEGRATOR );	
}

void CClothSimulationView::OnIntmidpoint() 
{
	// TODO: Add your command handler code here
	m_PhysEnv.m_IntegratorType = MIDPOINT_INTEGRATOR;
	Invalidate(TRUE);	
}

void CClothSimulationView::OnUpdateIntmidpoint(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck( m_PhysEnv.m_IntegratorType == MIDPOINT_INTEGRATOR );	
}

void CClothSimulationView::OnIntrk4() 
{
	// TODO: Add your command handler code here
	m_PhysEnv.m_IntegratorType = RK4_INTEGRATOR;
	Invalidate(TRUE);	
}

void CClothSimulationView::OnUpdateIntrk4(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck( m_PhysEnv.m_IntegratorType == RK4_INTEGRATOR );	
}

void CClothSimulationView::OnSimulationRunning() 
{
	// TODO: Add your command handler code here
	HandleKeyUp('R');	
}

void CClothSimulationView::OnUpdateSimulationRunning(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck( m_SimRunning );	
}

void CClothSimulationView::OnSimulationReset() 
{
	// TODO: Add your command handler code here
	m_PhysEnv.ResetWorld();
	Invalidate(TRUE);	
}

void CClothSimulationView::OnSimulationSetsimproperties() 
{
	// TODO: Add your command handler code here
	m_PhysEnv.SetWorldProperties();	
}

void CClothSimulationView::OnSimulationSettimingproperties() 
{
	// TODO: Add your command handler code here
	CDlgSimTiming dialog;
	dialog.m_Iterations = m_TimeIterations;
	dialog.m_FixedTimeSteps = m_UseFixedTimeStep;
	dialog.m_MaxTimeStep = m_MaxTimeStep;
	if (dialog.DoModal())
	{
		m_TimeIterations = dialog.m_Iterations;
		m_UseFixedTimeStep = dialog.m_FixedTimeSteps;
		m_MaxTimeStep = dialog.m_MaxTimeStep;
	}		
}

void CClothSimulationView::OnSimulationSetvertexproperties() 
{
	// TODO: Add your command handler code here
	m_PhysEnv.SetVertexProperties();	
}

void CClothSimulationView::OnSimulationUsegravity() 
{
	// TODO: Add your command handler code here
	m_PhysEnv.m_UseGravity = !m_PhysEnv.m_UseGravity;
	Invalidate(TRUE);	
}

void CClothSimulationView::OnUpdateSimulationUsegravity(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck( m_PhysEnv.m_UseGravity );	
}

void CClothSimulationView::OnSimulationAddcollisionsphere() 
{
	// TODO: Add your command handler code here
	m_PhysEnv.AddCollisionSphere();	
	Invalidate(TRUE);	
}

void CClothSimulationView::OnHelpWhichopengl() 
{
	// TODO: Add your command handler code here
//// 局部变量定义 ////////////////////////////////////////////////////////////////
	char *who, *which, *ver, *ext, *message;
	int len;
//////////////////////////////////////////////////////////////////////////////////
	who = (char *)::glGetString( GL_VENDOR );
	which = (char *)::glGetString( GL_RENDERER );
	ver = (char *)::glGetString( GL_VERSION );
	ext = (char *)::glGetString( GL_EXTENSIONS );

	len = 200 + strlen(who) + strlen(which) + strlen(ver) + strlen(ext);

	message = (char *)malloc(len);
	sprintf(message,"Who:\t%s\nWhich:\t%s\nVersion:\t%s\nExtensions:\t%s",
		who, which, ver, ext);

	::MessageBox(NULL,message,"GL Info",MB_OK);

	free(message);	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -