MFC reads obj format file 2

The previous article read the quantity information related to the vertices in the obj format file. The following reads and displays the coordinate information related to the vertices in the obj format file; use the previous cube obj file;

void CObjtest2View::OnDraw(CDC* pDC)
{
	CObjtest2Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here

	CStdioFile file;
	CString strline;
	CString str1;
	int row=0,col=0;
 
	if(NULL != file.Open("cube1.obj", CFile::modeRead))
	{
		while (file.ReadString(strline))
		{
			if (strline[0] == 'v') {
				if (strline[1] == 'n') {//vn
					pDC->TextOut(75, 230, "法线:");
					for (int i=1; i<5; i++)
					{
						 AfxExtractSubString(str1, strline, i, ' ');
						 pDC->TextOut(50+(col+1)*100, 90 + row*20, str1);
						 col = col + 1;
					}
					row = row + 1;
					col = 0;
				}
				else if (strline[1] == 't') {//vt
					pDC->TextOut(75, 390, "UV坐标:");
					for (int i=1; i<5; i++)
					{
						 AfxExtractSubString(str1, strline, i, ' ');
						 pDC->TextOut(50+(col+1)*100, 130 + row*20, str1);
						 col = col + 1;
					}
					row = row + 1;
					col = 0;
				}
				else {//v
					pDC->TextOut(75, 30, "位置:");
					for (int i=1; i<5; i++)
					{
						 AfxExtractSubString(str1, strline, i, ' ');
						 pDC->TextOut(50+col*100, 50 + row*20, str1);
						 col = col + 1;
					}
					row = row + 1;
					col = 0;
				}
			}
 
			if (strline[0] == 'f') {
				//fNum++;
			}
		}
		file.Close();
	}
	else
	{
		AfxMessageBox("文件打开失败!");
	}
}

Still use the CStdioFile class to open the file; loop to read one line at a time; process them separately according to the starting character of a line is v/vt/vn; use
AfxExtractSubString(str1, strline, i, ' ') to extract a single string from a line; The first parameter is the string fetched this time, the second parameter is the string to be split, the third parameter is the position, and the fourth parameter is the character used for splitting. Here, spaces are used to split;

Run as follows;

Guess you like

Origin blog.csdn.net/bcbobo21cn/article/details/132929793