www.pudn.com > 3DEDITOR.rar > RENDERVIEW.CPP


// RenderView.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "3DEditor.h" 
#include "DlgRayTrace.h" 
#include "RenderView.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// RenderView dialog 
 
int renderviewmode=0; 
extern CRITICAL_SECTION cs_rv; 
extern RenderView *renderview; 
 
RenderView::RenderView(CWnd* pParent /*=NULL*/) 
	: CDialog(RenderView::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(RenderView) 
		// NOTE: the ClassWizard will add member initialization here 
	//}}AFX_DATA_INIT 
} 
 
 
void RenderView::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(RenderView) 
		// NOTE: the ClassWizard will add DDX and DDV calls here 
	//}}AFX_DATA_MAP 
} 
 
 
BEGIN_MESSAGE_MAP(RenderView, CDialog) 
	//{{AFX_MSG_MAP(RenderView) 
	ON_WM_CLOSE() 
	ON_WM_PAINT() 
	ON_WM_SIZE() 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// RenderView message handlers 
 
void RenderView::Close() 
{ 
	OnClose(); 
} 
 
void RenderView::OnClose()  
{ 
	// TODO: Add your message handler code here and/or call default 
	EnterCriticalSection(&cs_rv); 
	renderview=0; 
	if (buf) 
		{ 
		((DlgRayTrace *)rd)->buf=buf; 
		((DlgRayTrace *)rd)->bufsx=bufsx; 
		((DlgRayTrace *)rd)->bufsy=bufsy; 
		buf=0; 
		} 
	GetWindowRect(&winpos3); 
	((DlgRayTrace *)rd)->GetDlgItem(IDC_PREVIEW)->EnableWindow(1); 
	RenderView **d=&((DlgRayTrace *)rd)->rv; 
	delete ((DlgRayTrace *)rd)->rv; 
	*d=0; 
	LeaveCriticalSection(&cs_rv); 
	CDialog::OnClose(); 
} 
 
BOOL RenderView::OnInitDialog()  
{ 
	CDialog::OnInitDialog(); 
 
	// TODO: Add extra initialization here 
	winpos3.left=-1; 
	winpos3.top=-1; 
	CMenu *menu=GetMenu(); 
	menu->CheckMenuRadioItem( ID_MODE1, ID_MODE2, ID_MODE1+renderviewmode, MF_BYCOMMAND); 
 
	if (winpos3.left!=-1) 
		SetWindowPos(0,winpos3.left,winpos3.top,0,0,SWP_NOSIZE|SWP_NOZORDER); 
 
	SetIcon(AfxGetApp()->LoadIcon(IDR_MAINFRAME), FALSE); 
	 
	return TRUE;  // return TRUE unless you set the focus to a control 
	              // EXCEPTION: OCX Property Pages should return FALSE 
} 
 
void RenderView::DrawPicture(CDC *dc,picture *p,int ssx,int ssy) 
{ 
	struct tagBITMAPINFO bmi; 
 
	bmi.bmiHeader.biSize=sizeof(BITMAPINFOHEADER); 
	bmi.bmiHeader.biCompression=BI_RGB; 
	bmi.bmiHeader.biXPelsPerMeter=6000; 
	bmi.bmiHeader.biYPelsPerMeter=6000; 
	bmi.bmiHeader.biClrUsed=0; 
	bmi.bmiHeader.biClrImportant=0; 
	bmi.bmiHeader.biSizeImage=0; 
	bmi.bmiHeader.biWidth=p?ssx:bufsx; 
	bmi.bmiHeader.biHeight=p?ssy:bufsy; 
	bmi.bmiHeader.biPlanes=1; 
	bmi.bmiHeader.biBitCount=24; 
 
	if (p) 
		{ 
		int tx=ssx*3,x,y,yp,xx; 
		byteRGB *rgb; 
		if (tx%4) tx+=(4-tx%4); 
 
		if (buf)  
			delete buf; 
		buf=new unsigned char[tx*ssy]; 
 
		for( y=0;ybuf[y*(p->sy-1)/(ssy-1)]; 
				for( x=0;xsx-1)/(ssx-1); 
						buf[yp++]=rgb[xx].b; 
						buf[yp++]=rgb[xx].g; 
						buf[yp++]=rgb[xx].r; 
					 } 
				 } 
		bufsx=ssx; 
		bufsy=ssy; 
		} 
	if (buf) 
		if (renderviewmode) 
			{ 
			RECT rect; 
			GetClientRect(&rect); 
			StretchDIBits(dc->m_hDC,0,0,rect.right,rect.bottom,0,0,bufsx,bufsy,buf,&bmi,DIB_RGB_COLORS,SRCCOPY); 
			} 
		else SetDIBitsToDevice(dc->m_hDC, 0,0, bufsx,bufsy, 0,0,  0,bufsy, buf, &bmi, DIB_RGB_COLORS);  
} 
 
 
void RenderView::DrawPicture(picture *p,int ssx,int ssy) 
{ 
	CDC *dc=GetDC(); 
	DrawPicture(dc,p,ssx,ssy); 
	ReleaseDC(dc); 
} 
 
void RenderView::OnPaint()  
{ 
	CPaintDC dc(this); // device context for painting 
	 
	// TODO: Add your message handler code here 
	DrawPicture(&dc,0,0,0); 
	// Do not call CDialog::OnPaint() for painting messages 
} 
 
BOOL RenderView::OnCommand(WPARAM wParam, LPARAM lParam)  
{ 
	// TODO: Add your specialized code here and/or call the base class 
	switch(wParam) 
	{ 
	case ID_ASPECT: 
		if (buf) 
		{ 
			CRect rect, rectw; 
			 
			GetClientRect(&rect); 
			GetWindowRect(&rectw); 
			 
			int newsx=rect.bottom*bufsx/bufsy; 
 
			SetWindowPos(0, 0, 0, rectw.right-rectw.left-rect.right+newsx, rectw.bottom-rectw.top, SWP_NOMOVE | SWP_NOZORDER); 
		} 
		break; 
	case ID_MODE1: 
		{ 
		CMenu *menu=GetMenu(); 
		renderviewmode=0; 
		menu->CheckMenuRadioItem( ID_MODE1, ID_MODE2, ID_MODE1+renderviewmode, MF_BYCOMMAND); 
		Invalidate(1); 
		menu->EnableMenuItem( ID_ASPECT, MF_GRAYED); 
		} 
		break; 
	case ID_MODE2: 
		{ 
		CMenu *menu=GetMenu(); 
		renderviewmode=1; 
		menu->CheckMenuRadioItem( ID_MODE1, ID_MODE2, ID_MODE1+renderviewmode, MF_BYCOMMAND); 
		Invalidate(0); 
		menu->EnableMenuItem( ID_ASPECT, MF_ENABLED); 
		} 
		break; 
	case ID_SAVEIMG: 
		if (buf) 
		{ 
		CString s; 
		s.LoadString(IDS_FD_TGA); 
		CFileDialog fd(FALSE,"tga",0,OFN_OVERWRITEPROMPT|OFN_HIDEREADONLY|OFN_PATHMUSTEXIST,s); 
		if(fd.DoModal()==IDOK) 
			if (SaveTGA((char *)(LPCSTR)fd.GetPathName())==0) 
				AfxMessageBox(IDS_ERRORSAVING,MB_OK|MB_ICONEXCLAMATION); 
		} 
		break; 
	case ID_COPY: 
		Copy(); 
		break; 
	} 
	return CDialog::OnCommand(wParam, lParam); 
} 
 
int RenderView::SaveTGA(char *name) 
{ 
	if (buf==0) 
		return 0; 
 
	FILE *fp=fopen(name,"wb"); 
	if (fp==0) 
		return 0; 
     
    unsigned char TGA_INI[18]; 
	memset(&TGA_INI[0],0,18); 
    TGA_INI[12]=(unsigned char)(bufsx%256); 
    TGA_INI[13]=(unsigned char)(bufsx/256); 
    TGA_INI[14]=(unsigned char)(bufsy%256); 
    TGA_INI[15]=(unsigned char)(bufsy/256); 
    TGA_INI[2]=2; 
    TGA_INI[16]=0x18; 
    fwrite((char *)&TGA_INI[0],18,1,fp); 
 
	int y,tx=3*bufsx; 
	if (tx%4) tx+=(4-tx%4); 
 
	for( y=0;ybiSize=sizeof(BITMAPINFOHEADER); 
	bi->biWidth=bufsx; 
	bi->biHeight=bufsy; 
	bi->biPlanes=1; 
	bi->biBitCount=24; 
	bi->biCompression=BI_RGB; 
	bi->biSizeImage=0; 
	bi->biXPelsPerMeter=0; 
	bi->biYPelsPerMeter=0; 
	bi->biClrUsed=0; 
	bi->biClrImportant=0; 
 
	memcpy(&b[sizeof(BITMAPINFOHEADER)],buf,tx*bufsy); 
	GlobalUnlock(hData); 
 
	SetClipboardData( CF_DIB, hData ); 
	CloseClipboard();	 
} 
 
void RenderView::OnSize(UINT nType, int cx, int cy)  
{ 
	CDialog::OnSize(nType, cx, cy); 
 
	// TODO: Add your message handler code here 
	DrawPicture(0,0,0);	 
}