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

📄 z_bufferdoc.cpp

📁 基于MFC的z-buffer算法实现
💻 CPP
字号:
// Z_BUFFERDoc.cpp : implementation of the CZ_BUFFERDoc class
//

#include "stdafx.h"
#include "Z_BUFFER.h"

#include "Z_BUFFERDoc.h"

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

/////////////////////////////////////////////////////////////////////////////
// CZ_BUFFERDoc

IMPLEMENT_DYNCREATE(CZ_BUFFERDoc, CDocument)

BEGIN_MESSAGE_MAP(CZ_BUFFERDoc, CDocument)
	//{{AFX_MSG_MAP(CZ_BUFFERDoc)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CZ_BUFFERDoc construction/destruction

CZ_BUFFERDoc::CZ_BUFFERDoc()
{
	m_face=new int[1280*3];
    m_point=new double [642*3];
    m_flag=FALSE;
}

CZ_BUFFERDoc::~CZ_BUFFERDoc()
{
	delete[] m_face;
	delete[] m_point;

}

BOOL CZ_BUFFERDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CZ_BUFFERDoc serialization

void CZ_BUFFERDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CZ_BUFFERDoc diagnostics

#ifdef _DEBUG
void CZ_BUFFERDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CZ_BUFFERDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CZ_BUFFERDoc commands

BOOL CZ_BUFFERDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
	if (!CDocument::OnOpenDocument(lpszPathName))
		return FALSE;
	
	CFile file;
	CFileException fe;
	char *tx;
	tx=new char[1];
	char ph[32];
	int i,j,a,b;
	BOOL flag;
	double *m_vex;
	m_vex=new double[642*3];
	a=0;
    byte R,G,B;
	if (!file.Open(lpszPathName, CFile::modeRead | CFile::shareDenyWrite, &fe))
	{
		// 失败
		ReportSaveLoadException(lpszPathName, &fe,
			FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
		
		// 返回FALSE
		return FALSE;
	}
	
	DeleteContents();
	TRY
	{
		for(i=0;i<3;i++)
		{

			for(j=0;j<642;j++)
			{
				flag=FALSE;
				b=0;
				memset(ph,0,32);
				while (!flag)
				{
					file.Seek(a,CFile::begin);
					file.Read(tx,1);
					if ((*tx)!=' ')
					{
						if((*tx)!='.')
						{
							file.Seek(a,CFile::begin);
							file.Read((ph+b),1);
						    b+=1;
							a+=1;
                            flag=FALSE;
						}
						else
						{
							file.Seek(a,CFile::begin);
							file.Read((ph+b),7);
                            a+=7;
							flag=TRUE;

						}
 
					}
					else
					{
						a+=1;
						flag=FALSE;
					}
				}
				m_vex[i*642+j]=atof(ph);
			
          
				
			}
		}
		for (i=0;i<3;i++)
		{
			for (j=0;j<1280;j++)
			{
				flag=FALSE;
				b=0;
				memset(ph,0,32);
				while (!flag)
				{
					file.Seek(a,CFile::begin);
					file.Read(tx,1);
					if ((*tx)=='a')
					{
						flag=TRUE;
					}
					a+=1;
				}
				flag=FALSE;
				while (!flag)
				{
					file.Seek(a,CFile::begin);
					file.Read(tx,1);
                    if ((*tx)!=' ')
                    {
						file.Seek(a,CFile::begin);
						file.Read((ph+b),1);
						b+=1;
						a+=1;
                    }
					else
					{
						flag=TRUE;
					}
				}
                m_face[i*1280+j]=atoi(ph);

			}
		}

	}
		
	CATCH (CFileException, eLoad)
	{
		// 读取失败
		file.Abort();
		
		// 恢复光标形状
		EndWaitCursor();
		
		// 报告失败
		ReportSaveLoadException(lpszPathName, eLoad,
			FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
	
		// 返回FALSE
		return FALSE;
	}
	END_CATCH
    m_flag=TRUE;
    for (i=0;i<642;i++)
    {
		m_point[i*3]=m_vex[i*3]*4;
		m_point[i*3+1]=m_vex[i*3+1]*4;
		m_point[i*3+2]=m_vex[i*3+2]*4;
    }
	delete[] m_vex;
	for (i=0;i<1280;i++)
	{
		R=rand()%256;
		G=rand()%256;
		B=rand()%256;
		m_color[i]=RGB(R,G,B);
		/*if ((i%2)==0)
		{
			m_color[i]=RGB(255,0,0);
		} 
		else
		{
			m_color[i]=RGB(0,0,255);
		}*/
		
	}
	SetPathName(lpszPathName);
	SetModifiedFlag(FALSE);
	return TRUE;
}

⌨️ 快捷键说明

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