subdivdlg.cpp

来自「此代码主要用于汽车车身的优化」· C++ 代码 · 共 955 行 · 第 1/2 页

CPP
955
字号
// SubdivDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Subdiv.h"
#include "SubdivDlg.h"

/*
#include <vector>
#include <set>
#include <map>
#include <functional>
#include <algorithm>
*/
#include <iostream>
#include "mesh.h"

using namespace std;
#include "Redirect_DebugWindow.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//#include "gl/glut.h"
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSubdivDlg dialog

CSubdivDlg::CSubdivDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSubdivDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSubdivDlg)
	m_renderType = 0;
	m_divBeforeLoop = TRUE;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
//	pMesh = new Mesh();
	m_blMeshReady = false;
	m_nowMeshIdx = -1;
}

CSubdivDlg::~CSubdivDlg()
{
	clearMeshHistory(0);

}
void CSubdivDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSubdivDlg)
	DDX_Radio(pDX, IDC_RADIO_RENDER_VERTEX, m_renderType);
	DDX_Check(pDX, IDC_CHECK_DIVLOOP, m_divBeforeLoop);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSubdivDlg, CDialog)
	//{{AFX_MSG_MAP(CSubdivDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_RADIO_RENDER_EDGE, OnRadioRenderEdge)
	ON_BN_CLICKED(IDC_RADIO_RENDER_FACE, OnRadioRenderFace)
	ON_BN_CLICKED(IDC_RADIO_RENDER_VERTEX, OnRadioRenderVertex)
	ON_BN_CLICKED(IDC_BUTTON_DUMP, OnButtonDump)
	ON_BN_CLICKED(IDC_BUTTON_CATMULL, OnButtonCatmull)
	ON_BN_CLICKED(IDC_BUTTON_DOOSABIN, OnButtonDoosabin)
	ON_BN_CLICKED(IDC_BUTTON_SAVEOBJ, OnButtonSaveobj)
	ON_BN_CLICKED(IDC_BUTTON_QUIT, OnButtonQuit)
	ON_BN_CLICKED(IDC_BUTTON_BACK, OnButtonBack)
	ON_BN_CLICKED(IDC_BUTTON_FORWARD, OnButtonForward)
	ON_BN_CLICKED(IDC_BUTTON_LOOP, OnButtonLoop)
	ON_WM_DESTROY()
	ON_WM_SIZE()
	ON_WM_MOUSEMOVE()
	ON_WM_CREATE()
	ON_WM_LBUTTONDBLCLK()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_RBUTTONDOWN()
	ON_WM_RBUTTONUP()
	ON_WM_MOUSEWHEEL()
	ON_BN_CLICKED(IDC_BUTTON_ZOOMIN, OnButtonZoomin)
	ON_BN_CLICKED(IDC_BUTTON_ZOOMOUT, OnButtonZoomout)
	ON_BN_CLICKED(IDC_CHECK_DIVLOOP, OnCheckDivloop)
	ON_BN_CLICKED(IDC_BUTTON_SQRT_3, OnButtonSqrt3)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSubdivDlg message handlers

BOOL CSubdivDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	CButton *bb = (CButton *)GetDlgItem(IDC_BUTTON_FORWARD);
	bb->EnableWindow(FALSE);

	bb = (CButton *)GetDlgItem(IDC_BUTTON_BACK);
	bb->EnableWindow(FALSE);

	wglMakeCurrent(m_hDC,  m_hRC);  
	OnInitOpenGL();
	wglMakeCurrent(m_hDC, NULL);  


	#ifdef _DEBUG
		RedirectIOToConsole("Redirect Debug Window");
	#endif
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CSubdivDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CSubdivDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
	
	wglMakeCurrent(m_hDC,  m_hRC);  
	OnReDrawOpengl();
//	cout << "hello redirected world";
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CSubdivDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

//#include "Edge.h"
void CSubdivDlg::OnButton1() 
{
	CFileDialog fd(TRUE,".obj",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
		"文件(*.obj)|*.obj|文件(*.3ds)|*.3ds|所有文件(*.*)|*.*||",NULL);
	if (fd.DoModal() == IDCANCEL)
		return;

	Mesh *pMesh = new Mesh();
	bool blfopen =false;
	if( fd.GetFileExt().CompareNoCase("obj") == 0 )
		blfopen = pMesh->loadObjFile(fd.GetPathName());
	else if( fd.GetFileExt().CompareNoCase("3ds") == 0 )
		blfopen = pMesh->load3dsFile(fd.GetPathName());
	if(blfopen)
	{
		m_meshs.push_back(pMesh);
		m_nowMeshIdx = m_meshs.size() - 1;
		m_blMeshReady = true;;
		Invalidate(false);
	}
	else
	{
		AfxMessageBox("Unsupported File");
	}


//	m_meshs.clear();

	/*

	CString strLine = "f 1 2 3 4 5";
	int i1, i = 2, j;
	while( i < strLine.GetLength() )
	{
		j = strLine.Find(' ',i);
		if( j == -1)	j = strLine.GetLength();
		sscanf(strLine.Mid(i,j-i), "%d", &i1);
		cout << i1 << endl;
		i = j + 1;
	}
	*/
}
	/*typedef pair<int,int> INTPAIR;
	typedef set<Edge> EDGESET;
	typedef EDGESET::iterator EDGEPOS;

	vector<int>v1;
	map<int, int> m1;
	EDGESET s1;
	
//	std::iostream f("c:\\aaa.txt", "w");
	SYSTEMTIME st1,st2;

	GetSystemTime(&st1 );
	for(int i = 0; i < 10000; i ++ )
	{
		v1.push_back(i);
	}
//ort ( v1.begin () , v1.end() );
	for( i = 0; i < 10000; i ++ )
	{
//		sort ( v1.begin () , v1.end() );

//		vector<int>::const_iterator itr = v1.begin();
		vector<int>::const_iterator itr = lower_bound(v1.begin(), v1.end(), i);
	}
	GetSystemTime(&st2 );
	for( i = 0; i < 10000; i ++ )
	{
		m1.insert(INTPAIR(i,i));
	}
	int interval1 = st2.wMilliseconds - st1.wMilliseconds;
	for( i = 0; i < 10000; i ++ )
	{
		int a, b;
		a =  (* m1.find(i)).first;
//		b =  (* m1.find(i)).second;

	}
	GetSystemTime(&st1 );
	int interval2 = st1.wMilliseconds - st2.wMilliseconds;
	for( i = 0; i < 10000; i ++ )
	{
		Edge e(i,i);
		s1.insert(e);
	}
	
	EDGEPOS e;
	for( i = 0; i < 10000; i ++ )
	{
		 e = s1.find(Edge(i,i));
		;
		//s1[i];
	}
	
	TRACE("%d,%d\n", (*e).m_bgVertexIdx, (*e).m_edVertexIdx);
	GetSystemTime(&st2 );
	int interval3 = st2.wMilliseconds - st1.wMilliseconds;
	s1.insert(Edge(200,200));
	s1.insert(Edge(200,300));
	s1.insert(Edge(5200,5200));
	s1.insert(Edge(5200,5300));
	e = s1.find(Edge(5000,5000));
	TRACE("%d,%d\n size: %d\n", (*e).m_bgVertexIdx, (*e).m_edVertexIdx, s1.size());

	CString str1, str2, str3;
	str1.Format("%d",interval1 );
	str2.Format("%d",interval2 );
	str3.Format("%d",interval3 );
	MessageBox(str1+"ms " + str2 + "ms " + str3);

//	find_if()
*/

//BEGIN_EVENTSINK_MAP(CSubdivDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CSubdivDlg)
//	ON_EVENT(CSubdivDlg, IDC_OPENGLACTIVEXCTRL1, 2 /* InitOpenGL */, OnInitOpenGL, VTS_NONE)
//	ON_EVENT(CSubdivDlg, IDC_OPENGLACTIVEXCTRL1, 3 /* LButtonUp */, OnLButtonUpOpengl, VTS_I4 VTS_I2 VTS_I2)
//	ON_EVENT(CSubdivDlg, IDC_OPENGLACTIVEXCTRL1, 4 /* ReDraw */, OnReDrawOpengl, VTS_NONE)
//	ON_EVENT(CSubdivDlg, IDC_OPENGLACTIVEXCTRL1, 5 /* ReSize */, OnReSizeOpengl, VTS_I2 VTS_I2)
//	ON_EVENT(CSubdivDlg, IDC_OPENGLACTIVEXCTRL1, 6 /* LMouseMove */, OnLMouseMoveOpengl, VTS_I2 VTS_I2)
//	ON_EVENT(CSubdivDlg, IDC_OPENGLACTIVEXCTRL1, 1 /* LButtonDown */, OnLButtonDownOpengl, VTS_I4 VTS_I2 VTS_I2)
	//}}AFX_EVENTSINK_MAP
//END_EVENTSINK_MAP()

void CSubdivDlg::OnInitOpenGL() 
{
	float red = 220;//(::GetSysColor(COLOR_WINDOW) >> 16) & 255;
	float green = 220;//::GetSysColor(COLOR_WINDOW) >> 8) & 255;
	float blue = 220;//::GetSysColor(COLOR_WINDOW) & 255;

	m_lBtnDown = false;
	m_rBtnDown = false;

	m_wid = 3;
	m_hei = 3;
	m_mouseWheeled = false;
	
	zRot = xRot = yRot = xOriginRot = yOriginRot = 0;
	xTran = yTran = 0;
	zoom = 1;
	startPoint = rstartPoint = CPoint(0,0);

	// 设置深度检测函数
	glDepthFunc(GL_LESS);
	glEnable(GL_DEPTH_TEST);

///	glClearColor(1,1,1,1);

	
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_NORMALIZE);
	glEnable(GL_COLOR_MATERIAL);

	glDisable(GL_CULL_FACE);
	glClearDepth(1.0f);
//	glFrontFace(GL_CCW);		// Counter clock-wise polygons face out
//	glEnable(GL_CULL_FACE);		// Do not calculate inside of jet

	float light0_pos[]={ -50, 50, 100, 0 }; 
		//float light1_pos[]={ -50, 50, 100, 0 }; 
	glLightfv(GL_LIGHT0, GL_POSITION, light0_pos);		// 不能放在onSize里,不要放在redraw中,不然会随着物体旋转
 //   glLightfv(GL_LIGHT0, GL_POSITION, light1_pos);
	GLfloat  ambientLight[] = { 0.0f, 0.2f, 0.0f, 1.0f };
	GLfloat  diffuseLight[] = { 0.7f, 0.7f, 0.0f, 1.0f };
	GLfloat  specular[] = { 1.0f, 1.0f, 1.0f, 1.0f};
	GLfloat  specref[] =  { 1.0f, 1.0f, 1.0f, 1.0f };

	// Setup and enable light 0
	glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);
	glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);
	glLightfv(GL_LIGHT0,GL_SPECULAR,specular);

	// Set Material properties to follow glColor values
	glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
	// All materials hereafter have full specular reflectivity
	// with a high shine
	glMaterialfv(GL_FRONT, GL_SPECULAR,specref);
	glMateriali(GL_FRONT,GL_SHININESS,128);

	glClearColor(red / 100., green / 100., blue / 255., 1 );
//	glClearColor(0.8f, 0.8f, 0.8f, 1.0f );
}


void CSubdivDlg::OnReDrawOpengl() 
{
	CRect  rect;  
	GetClientRect(&rect);  

	
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
//	gluLookAt(0,0,10, 0,0,-1,0, 1, 0);	// 摄像头上方向:y轴
	gluLookAt(0,0,-10, 0,0,0,0, 1, 0);	// 摄像头上方向:y轴
//	glColor3f(0,0,0);
//	glOrtho(-m_wid/2 * zoom,m_wid/2 * zoom,-m_hei/2 * zoom,m_hei/2 * zoom,-3,100);
	glTranslatef( -xTran * m_wid / rect.Width()*zoom*4, -yTran * m_hei / rect.Height() * 4*zoom, 0);	// 移动之后再图像旋转
	glRotatef(-xRot, 1.0f, 0.0f, 0.0f);
	glRotatef(yRot, 0.0f, 1.0f, 0.0f);
	glScalef(zoom, zoom, zoom);

	//	glLineWidth(1.0f); 

//	glutSolidSphere(2.0f, 12, 12);
	// mesh draw here
	if( m_blMeshReady )
		m_meshs[m_nowMeshIdx]->render(m_renderType);
	
	SwapBuffers(::wglGetCurrentDC());	// 与当前rc相连的dc
	wglMakeCurrent(m_hDC,NULL);

}

void CSubdivDlg::OnReSizeOpengl(short cx, short cy) 
{
	wglMakeCurrent(m_hDC,  m_hRC);  

	int m_projMode = 1;
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();	


	if (m_projMode == 0)
		glFrustum(-3,3,-3,3,4,30);
	else
		glOrtho(-3,3,-3,3,4,30);
/*
	float m_aspect = (float)cx / (cy > 1? cy : 1);
	gluPerspective(60.f, m_aspect, .1, 100);
*/

	glMatrixMode(GL_MODELVIEW);
//	glLoadIdentity();
//	int size = ((cx - 50) > cy ? cy : cx - 50);
	glViewport(15, 10,cx - 165,cy-35);
	glShadeModel(GL_FLAT);

⌨️ 快捷键说明

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