📄 clothsimulationview.cpp
字号:
// ClothSimulationView.cpp : implementation of the CClothSimulationView class
//
#include "stdafx.h"
#include "ClothSimulation.h"
#include <mmsystem.h>
#include "ClothSimulationDoc.h"
#include "ClothSimulationView.h"
#include "LoadOBJ.h"
#include "DlgSimTiming.h"
#include "DlgNewCloth.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
///////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// CClothSimulationView
IMPLEMENT_DYNCREATE(CClothSimulationView, CView)
BEGIN_MESSAGE_MAP(CClothSimulationView, CView)
//{{AFX_MSG_MAP(CClothSimulationView)
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_SIZE()
ON_WM_LBUTTONDOWN()
ON_WM_RBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDBLCLK()
ON_WM_LBUTTONUP()
ON_COMMAND(ID_FILE_CREATECLOTHPATCH, OnFileCreateclothpatch)
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
ON_COMMAND(ID_FILE_SAVE, OnFileSave)
ON_COMMAND(ID_FILE_NEWSYSTEM, OnFileNewsystem)
ON_COMMAND(ID_VIEW_SHOWSPRINGS, OnViewShowsprings)
ON_UPDATE_COMMAND_UI(ID_VIEW_SHOWSPRINGS, OnUpdateViewShowsprings)
ON_COMMAND(ID_VIEW_SHOWSTRUCTURAL, OnViewShowstructural)
ON_UPDATE_COMMAND_UI(ID_VIEW_SHOWSTRUCTURAL, OnUpdateViewShowstructural)
ON_COMMAND(ID_VIEW_SHOWSHEAR, OnViewShowshear)
ON_UPDATE_COMMAND_UI(ID_VIEW_SHOWSHEAR, OnUpdateViewShowshear)
ON_COMMAND(ID_VIEW_SHOWBEND, OnViewShowbend)
ON_UPDATE_COMMAND_UI(ID_VIEW_SHOWBEND, OnUpdateViewShowbend)
ON_COMMAND(ID_VIEW_SHOWGEOMETRY, OnViewShowgeometry)
ON_UPDATE_COMMAND_UI(ID_VIEW_SHOWGEOMETRY, OnUpdateViewShowgeometry)
ON_COMMAND(ID_VIEW_SHOWVERTICES, OnViewShowvertices)
ON_UPDATE_COMMAND_UI(ID_VIEW_SHOWVERTICES, OnUpdateViewShowvertices)
ON_COMMAND(ID_VIEW_COLLISIONACTIVE, OnViewCollisionactive)
ON_UPDATE_COMMAND_UI(ID_VIEW_COLLISIONACTIVE, OnUpdateViewCollisionactive)
ON_COMMAND(ID_INTEULER, OnInteuler)
ON_UPDATE_COMMAND_UI(ID_INTEULER, OnUpdateInteuler)
ON_COMMAND(ID_INTMIDPOINT, OnIntmidpoint)
ON_UPDATE_COMMAND_UI(ID_INTMIDPOINT, OnUpdateIntmidpoint)
ON_COMMAND(ID_INTRK4, OnIntrk4)
ON_UPDATE_COMMAND_UI(ID_INTRK4, OnUpdateIntrk4)
ON_COMMAND(ID_SIMULATION_RUNNING, OnSimulationRunning)
ON_UPDATE_COMMAND_UI(ID_SIMULATION_RUNNING, OnUpdateSimulationRunning)
ON_COMMAND(ID_SIMULATION_RESET, OnSimulationReset)
ON_COMMAND(ID_SIMULATION_SETSIMPROPERTIES, OnSimulationSetsimproperties)
ON_COMMAND(ID_SIMULATION_SETTIMINGPROPERTIES, OnSimulationSettimingproperties)
ON_COMMAND(ID_SIMULATION_SETVERTEXPROPERTIES, OnSimulationSetvertexproperties)
ON_COMMAND(ID_SIMULATION_USEGRAVITY, OnSimulationUsegravity)
ON_UPDATE_COMMAND_UI(ID_SIMULATION_USEGRAVITY, OnUpdateSimulationUsegravity)
ON_COMMAND(ID_SIMULATION_ADDCOLLISIONSPHERE, OnSimulationAddcollisionsphere)
ON_COMMAND(ID_HELP_WHICHOPENGL, OnHelpWhichopengl)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CClothSimulationView construction/destruction
CClothSimulationView::CClothSimulationView()
{
// TODO: add construction code here
m_DrawGeometry = TRUE;
m_SimRunning = FALSE;
m_CurBone = NULL;
ResetBone(&m_Skeleton, NULL);
m_Skeleton.id = -1;
strcpy(m_Skeleton.name,"Skeleton");
m_Skeleton.b_trans.z = -100.0f;
m_Skeleton.trans.z = -100.0f;
m_TimeIterations = 10;
m_UseFixedTimeStep = TRUE;
m_MaxTimeStep = 0.01f;
m_FrameCnt = 0;
m_PickX = -1;
m_PickY = -1;
m_hDC = NULL;
}
CClothSimulationView::~CClothSimulationView()
{
DestroySkeleton(&m_Skeleton);
m_hDC = NULL;
m_SimRunning = FALSE;
}
BOOL CClothSimulationView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
////////////////////////////////////////////////////////////////
//设置窗口类型
cs.style |=WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
////////////////////////////////////////////////////////////////
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CClothSimulationView drawing
void CClothSimulationView::OnDraw(CDC* pDC)
{
CClothSimulationDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
//////////////////////////////////////////////////////////////////
RenderScene(); //渲染场景
//////////////////////////////////////////////////////////////////
}
/////////////////////////////////////////////////////////////////////////////
// CClothSimulationView printing
BOOL CClothSimulationView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CClothSimulationView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CClothSimulationView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CClothSimulationView diagnostics
#ifdef _DEBUG
void CClothSimulationView::AssertValid() const
{
CView::AssertValid();
}
void CClothSimulationView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CClothSimulationDoc* CClothSimulationView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CClothSimulationDoc)));
return (CClothSimulationDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CClothSimulationView message handlers
float CClothSimulationView::GetTime( void )
{
static DWORD StartMilliseconds;
if(!StartMilliseconds)
{
// 获得系统时间
StartMilliseconds = timeGetTime();
}
DWORD CurrentMilliseconds = timeGetTime();
return float(CurrentMilliseconds - StartMilliseconds) / 1000.0f;
}
/////////////////////////////////////////////////////////////////////////////
// CClothSimulationView message handlers
BOOL CClothSimulationView::SetupPixelFormat(HDC hdc)
{
/// Local Variables ///////////////////////////////////////////////////////////
PIXELFORMATDESCRIPTOR pfd, *ppfd;
int pixelformat;
///////////////////////////////////////////////////////////////////////////////
ppfd = &pfd;
ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
ppfd->nVersion = 1;
ppfd->dwFlags = PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
ppfd->dwLayerMask = PFD_MAIN_PLANE;
ppfd->iPixelType = PFD_TYPE_RGBA;
ppfd->cColorBits = 16;
ppfd->cDepthBits = 16;
ppfd->cAccumBits = 0;
ppfd->cStencilBits = 0;
pixelformat = ChoosePixelFormat(hdc, ppfd);
if ((pixelformat = ChoosePixelFormat(hdc, ppfd)) == 0) {
MessageBox("ChoosePixelFormat failed", "Error", MB_OK);
return FALSE;
}
if (pfd.dwFlags & PFD_NEED_PALETTE) {
MessageBox("Needs palette", "Error", MB_OK);
return FALSE;
}
if (SetPixelFormat(hdc, pixelformat, ppfd) == FALSE) {
MessageBox("SetPixelFormat failed", "Error", MB_OK);
return FALSE;
}
return TRUE;
}
int CClothSimulationView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
m_hDC = ::GetDC(m_hWnd);
if (!SetupPixelFormat(m_hDC))
PostQuitMessage (0);
m_hRC = wglCreateContext(m_hDC);
wglMakeCurrent(m_hDC, m_hRC);
RECT rect;
GetClientRect(&rect);
initializeGL(rect.right, rect.bottom);
// 创建显示列表
glNewList(OGL_AXIS_DLIST,GL_COMPILE);
glPushMatrix();
glScalef(4.0,4.0,4.0);
glBegin(GL_LINES);
glVertex3f(-0.2f, 0.0f, 0.0f);
glVertex3f( 0.2f, 0.0f, 0.0f);
glVertex3f( 0.2f, 0.0f, 0.0f);
glVertex3f( 0.15f, 0.04f, 0.0f);
glVertex3f( 0.2f, 0.0f, 0.0f);
glVertex3f( 0.15f, -0.04f, 0.0f);
glVertex3f( 0.0f, 0.2f, 0.0f);
glVertex3f( 0.0f, -0.2f, 0.0f);
glVertex3f( 0.0f, 0.2f, 0.0f);
glVertex3f( 0.04f, 0.15f, 0.0f);
glVertex3f( 0.0f, 0.2f, 0.0f);
glVertex3f( -0.04f, 0.15f, 0.0f);
glVertex3f( 0.0f, 0.0f, 0.2f);
glVertex3f( 0.0f, 0.0f, -0.2f);
glVertex3f( 0.0f, 0.0f, 0.2f);
glVertex3f( 0.0f, 0.04f, 0.15f);
glVertex3f( 0.0f, 0.0f, 0.2f);
glVertex3f( 0.0f, -0.04f, 0.15f);
glEnd();
glPopMatrix();
glEndList();
glDisable(GL_TEXTURE_2D);
RenderScene();
return 0;
}
GLvoid CClothSimulationView::initializeGL(GLsizei width, GLsizei height)
{
/// Local Variables ///////////////////////////////////////////////////////////
GLfloat aspect;
GLfloat diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
GLfloat specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat lightpos[] = { 0.30f, 0.3f, 1.0f, 0.0f }; // .5 .5 1.0
GLfloat ambient[] = { 0.8f, 0.8f, 0.8f, 1.0f };
///////////////////////////////////////////////////////////////////////////////
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
aspect = (GLfloat)width/(GLfloat)height;
gluPerspective(10.0, aspect,1, 2000);
glMatrixMode(GL_MODELVIEW);
glPolygonMode(GL_FRONT,GL_FILL);
glLineWidth(2.0f);
glPointSize(8.0f);
glDisable(GL_LINE_SMOOTH);
glDepthFunc(GL_LEQUAL);
glDisable(GL_CULL_FACE);
glDisable(GL_LIGHTING);
}
// 仿真控制主函数
void CClothSimulationView::RunSim()
{
/// 局部变量定义 ///////////////////////////////////////////////////////////
float Time;
float DeltaTime;
////////////////////////////////////////////////////////////////////////////
if (m_UseFixedTimeStep)
Time = m_LastTime + (m_MaxTimeStep * m_TimeIterations);
else
Time = GetTime() * m_TimeIterations;
if (m_SimRunning)
{
while(m_LastTime < Time)
{
DeltaTime = Time - m_LastTime;
if(DeltaTime > m_MaxTimeStep)
{
DeltaTime = m_MaxTimeStep;
}
m_PhysEnv.Simulate(DeltaTime,m_SimRunning);
m_LastTime += DeltaTime;
}
m_LastTime = Time;
}
else
{
m_PhysEnv.Simulate(DeltaTime,m_SimRunning);
}
}
// 场景绘制函数
GLvoid CClothSimulationView::RenderScene(GLvoid)
{
if (m_Skeleton.rot.y > 360.0f) m_Skeleton.rot.y -= 360.0f;
if (m_Skeleton.rot.x > 360.0f) m_Skeleton.rot.x -= 360.0f;
if (m_Skeleton.rot.z > 360.0f) m_Skeleton.rot.z -= 360.0f;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(m_Skeleton.trans.x, m_Skeleton.trans.y, m_Skeleton.trans.z);
glRotatef(m_Skeleton.rot.z, 1.0f, 0.0f, 0.0f);
glRotatef(m_Skeleton.rot.y, 0.0f, 1.0f, 0.0f);
glRotatef(m_Skeleton.rot.x, 0.0f, 0.0f, 1.0f);
glGetFloatv(GL_MODELVIEW_MATRIX,m_Skeleton.matrix.m);
if (m_PickX > -1)
m_PhysEnv.GetNearestPoint(m_PickX,m_PickY);
RunSim();
m_PhysEnv.RenderWorld();
glPopMatrix();
glFinish();
if (m_hDC)
SwapBuffers(m_hDC);
m_PickX = -1;
m_PickY = -1;
}
void CClothSimulationView::OnDestroy()
{
CView::OnDestroy();
// TODO: Add your message handler code here
/////////////////////////////////////////////////////////////////
//删除调色板和渲染上下文、定时器
if (m_hRC)
wglDeleteContext(m_hRC);
if (m_hDC)
::ReleaseDC(m_hWnd,m_hDC);
m_hRC = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -