📄 clothsimulationview.cpp
字号:
m_hDC = 0;
/////////////////////////////////////////////////////////////////
}
void CClothSimulationView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
m_ScreenWidth = cx;
m_ScreenHeight = cy;
GLfloat aspect;
glViewport(0, 0, cx, cy);
aspect = (GLfloat)cx/(GLfloat)cy;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(10.0, aspect, 1.0, 2000.0);
glMatrixMode(GL_MODELVIEW);
}
BOOL CClothSimulationView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
// TODO: Add your specialized code here and/or call the base class
/// 局部变量定义 ///////////////////////////////////////////////////////////
t_Visual *visual = NULL;
///////////////////////////////////////////////////////////////////////////////
return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}
void CClothSimulationView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_mousepos = point;
m_Base_Rot_X = m_Skeleton.rot.x;
m_Base_Rot_Y = m_Skeleton.rot.y;
m_Base_Rot_Z = m_Skeleton.rot.z;
if ((nFlags & MK_SHIFT) == 0)
{
m_PickX = point.x;
m_PickY = m_ScreenHeight - point.y;
RenderScene();
}
SetCapture( );
CView::OnLButtonDown(nFlags, point);
}
void CClothSimulationView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_mousepos = point;
m_Base_Rot_X = m_Skeleton.rot.x;
m_Base_Rot_Y = m_Skeleton.rot.y;
m_Base_Rot_Z = m_Skeleton.rot.z;
CView::OnRButtonDown(nFlags, point);
}
void CClothSimulationView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
tVector localX,localY;
if (nFlags & MK_LBUTTON > 0)
{
// 如果按下Ctr键
if ((nFlags & MK_CONTROL) > 0 && m_CurBone != NULL)
{
}
else if ((nFlags & MK_SHIFT) > 0)
{
if ((point.x - m_mousepos.x) != 0)
{
m_Skeleton.rot.y = m_Base_Rot_Y + ((float)ROTATE_SPEED * (point.x - m_mousepos.x));
RenderScene();
}
if ((point.y - m_mousepos.y) != 0)
{
m_Skeleton.rot.z = m_Base_Rot_Z + ((float)ROTATE_SPEED * (point.y - m_mousepos.y));
RenderScene();
}
}
else if (m_SimRunning)
{
localY.x = m_Skeleton.matrix.m[1];
localY.y = m_Skeleton.matrix.m[5];
localY.z = m_Skeleton.matrix.m[9];
localX.x = m_Skeleton.matrix.m[0];
localX.y = m_Skeleton.matrix.m[4];
localX.z = m_Skeleton.matrix.m[8];
m_PhysEnv.SetMouseForce(point.x - m_mousepos.x,point.y - m_mousepos.y,&localX,&localY);
m_PhysEnv.m_MouseForceActive = TRUE;
}
}
else if ((nFlags & MK_RBUTTON) == MK_RBUTTON)
{
if ((nFlags & MK_CONTROL) > 0 && m_CurBone != NULL)
{
}
else if ((nFlags & MK_SHIFT) > 0)
{
if ((point.x - m_mousepos.x) != 0)
{
m_Skeleton.rot.x = m_Base_Rot_X + ((float)ROTATE_SPEED * (point.x - m_mousepos.x));
RenderScene();
}
}
else
{
if ((point.x - m_mousepos.x) != 0)
{
m_Skeleton.rot.y = m_Base_Rot_Y + ((float)ROTATE_SPEED * (point.x - m_mousepos.x));
RenderScene();
}
if ((point.y - m_mousepos.y) != 0)
{
m_Skeleton.rot.z = m_Base_Rot_Z + ((float)ROTATE_SPEED * (point.y - m_mousepos.y));
RenderScene();
}
}
}
CView::OnMouseMove(nFlags, point);
}
void CClothSimulationView::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CView::OnLButtonDblClk(nFlags, point);
}
void CClothSimulationView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_PhysEnv.m_MouseForceActive = FALSE;
ReleaseCapture();
CView::OnLButtonUp(nFlags, point);
}
void CClothSimulationView::HandleKeyUp(UINT nChar)
{
tVector userforce;
switch (nChar)
{
case 13:
m_PhysEnv.AddSpring();
break;
case 'G':
m_PhysEnv.m_UseGravity = !m_PhysEnv.m_UseGravity;
break;
case '1': m_curVisual = 0;
break;
case '2': m_curVisual = 1;
break;
case 'O':
glPolygonMode(GL_FRONT,GL_LINE);
break;
case 'F':
glPolygonMode(GL_FRONT,GL_FILL);
break;
case 'R':
m_SimRunning = !m_SimRunning;
if (m_SimRunning)
m_LastTime = GetTime() * m_TimeIterations;
m_StartTime = timeGetTime();
m_FrameCnt = 0;
break;
case 'T':
m_PhysEnv.ResetWorld();
break;
case VK_HOME:
userforce.x = m_Skeleton.matrix.m[1];
userforce.y = m_Skeleton.matrix.m[5];
userforce.z = m_Skeleton.matrix.m[9];
m_PhysEnv.ApplyUserForce(&userforce);
break;
case VK_END:
userforce.x = -m_Skeleton.matrix.m[1];
userforce.y = -m_Skeleton.matrix.m[5];
userforce.z = -m_Skeleton.matrix.m[9];
m_PhysEnv.ApplyUserForce(&userforce);
break;
case VK_RIGHT:
userforce.x = m_Skeleton.matrix.m[0];
userforce.y = m_Skeleton.matrix.m[4];
userforce.z = m_Skeleton.matrix.m[8];
m_PhysEnv.ApplyUserForce(&userforce);
break;
case VK_LEFT:
userforce.x = -m_Skeleton.matrix.m[0];
userforce.y = -m_Skeleton.matrix.m[4];
userforce.z = -m_Skeleton.matrix.m[8];
m_PhysEnv.ApplyUserForce(&userforce);
break;
case VK_UP:
userforce.x = -m_Skeleton.matrix.m[2];
userforce.y = -m_Skeleton.matrix.m[6];
userforce.z = -m_Skeleton.matrix.m[10];
m_PhysEnv.ApplyUserForce(&userforce);
break;
case VK_DOWN:
userforce.x = m_Skeleton.matrix.m[2];
userforce.y = m_Skeleton.matrix.m[6];
userforce.z = m_Skeleton.matrix.m[10];
m_PhysEnv.ApplyUserForce(&userforce);
break;
}
MSG msg;
if (m_SimRunning)
{
while (m_SimRunning == TRUE)
{
while (::PeekMessage(&msg,0,0,0,PM_REMOVE))
{
if (msg.message == WM_QUIT)
{
m_SimRunning = FALSE;
m_hDC = NULL;
::PostQuitMessage(0);
break;
}
if (msg.message == WM_CLOSE)
{
m_hDC = NULL;
m_SimRunning = FALSE;
}
if (!AfxGetApp()->PreTranslateMessage(&msg))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
AfxGetApp()->OnIdle(0);
AfxGetApp()->OnIdle(1);
}
if (m_SimRunning) RenderScene();
}
}
else
Invalidate(TRUE);
}
// 清除仿真系统中的内容
void CClothSimulationView::NewSystem()
{
m_PhysEnv.FreeSystem();
m_SimRunning = FALSE;
if (m_Skeleton.childCnt > 0)
{
if (m_Skeleton.children->visuals->vertexData)
free(m_Skeleton.children->visuals->vertexData);
if (m_Skeleton.children->visuals->faceIndex)
free(m_Skeleton.children->visuals->faceIndex);
free(m_Skeleton.children->visuals);
free(m_Skeleton.children);
m_Skeleton.childCnt = 0;
}
RenderScene();
}
// 读入文件函数
void CClothSimulationView::LoadFile(CString file1,CString baseName,CString ext)
{
/// 局部变量定义 ///////////////////////////////////////////////////////////
t_Bone *children;
t_Visual *visual;
FILE *fp;
///////////////////////////////////////////////////////////////////////////////
ext.MakeUpper();
if (ext == "OBJ") // 读入OBJ文件
{
visual = (t_Visual *)malloc(sizeof(t_Visual));
NewSystem(); // 清除系统
if (file1.GetLength() > 0 && LoadOBJ((char *)(LPCTSTR)file1 ,visual,
LOADOBJ_VERTEXONLY | LOADOBJ_REUSEVERTICES))
{
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,(LPCTSTR)baseName);
m_CurBone->visuals = visual;
m_CurBone->visualCnt = 1;
m_Skeleton.childCnt = 1;
m_Skeleton.children = children;
}
else
{
MessageBox("Must Be A Valid OBJ File","Error",MB_OK);
free(visual);
}
}
else // 读入仿真文件
{
if (file1.GetLength())
{
fp = fopen(file1,"rb");
if (fp != NULL)
{
NewSystem(); // 清除文件
fread(&m_Skeleton,sizeof(t_Bone),1,fp);
if (m_Skeleton.childCnt > 0)
{
m_Skeleton.children = (t_Bone *)malloc(sizeof(t_Bone));
fread(m_Skeleton.children,sizeof(t_Bone),1,fp);
if (m_Skeleton.children->visualCnt > 0)
{
m_Skeleton.children->visuals = (t_Visual *)malloc(sizeof(t_Visual));
visual = m_Skeleton.children->visuals;
fread(visual,sizeof(t_Visual),1,fp);
if (visual->reuseVertices)
{
visual->vertexData = (float *)malloc(sizeof(float) * visual->vSize * visual->vertexCnt);
visual->faceIndex = (unsigned short *)malloc(sizeof(unsigned short) * visual->faceCnt * visual->vPerFace);
fread(visual->vertexData,sizeof(float),visual->vSize * visual->vertexCnt,fp);
fread(visual->faceIndex,sizeof(unsigned short),visual->faceCnt * visual->vPerFace,fp);
}
m_PhysEnv.LoadData(fp);
}
}
fclose(fp);
}
}
}
}
// 保存仿真文件
void CClothSimulationView::SaveFile(CString file1,CString baseName)
{
/// 局部变量定义 ///////////////////////////////////////////////////////////
t_Visual *visual;
FILE *fp;
///////////////////////////////////////////////////////////////////////////////
if (file1.GetLength() > 0)
{
fp = fopen(file1,"wb");
if (fp != NULL)
{
fwrite(&m_Skeleton,sizeof(t_Bone),1,fp);
if (m_Skeleton.childCnt > 0)
{
fwrite(&m_Skeleton.children,sizeof(t_Bone),1,fp);
if (m_Skeleton.children->visualCnt > 0)
{
visual = m_Skeleton.children->visuals;
fwrite(visual,sizeof(t_Visual),1,fp);
if (visual->reuseVertices)
{
fwrite(visual->vertexData,sizeof(float),visual->vSize * visual->vertexCnt,fp);
fwrite(visual->faceIndex,sizeof(unsigned short),visual->faceCnt * visual->vPerFace,fp);
}
m_PhysEnv.SaveData(fp);
}
}
fclose(fp);
}
}
}
// 创建新的织物
void CClothSimulationView::CreateClothPatch()
{
/// 局部变量定义 ///////////////////////////////////////////////////////////
t_Bone *children;
t_Visual *visual;
int fPos,vPos,l1,l2;
tTexturedVertex *vertex;
float sx,sy,stepx,stepy;
BOOL orientHoriz = TRUE;
int u = 9, v = 9;
float w = 8.0f, h = 8.0,tsu,tsv,tdu,tdv;
float SstK = 4.0f,SstD = 0.6f;
float SshK = 4.0f,SshD = 0.6f;
float SflK = 2.4f,SflD = 0.8f;
CDlgNewCloth dialog;
///////////////////////////////////////////////////////////////////////////////
dialog.m_StructCoef = SstK;
dialog.m_StructDamp = SstD;
dialog.m_ShearCoef = SshK;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -