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

📄 plmyview.cpp

📁 此例子提供了通过显示列表绘制雨季,花伞等场景的例程.使我们更好地理解显示列表的功能.
💻 CPP
字号:
// plmyView.cpp : implementation of the CPlmyView class
//

#include "stdafx.h"
#include "plmy.h"
#include "MainFrm.h"
#include "Math.h"

#include "plmyDoc.h"
#include "plmyView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CPlmyView

IMPLEMENT_DYNCREATE(CPlmyView, CView)

BEGIN_MESSAGE_MAP(CPlmyView, CView)
	//{{AFX_MSG_MAP(CPlmyView)
	ON_WM_CREATE()
	ON_WM_DESTROY()
	ON_WM_SIZE()
	ON_WM_ERASEBKGND()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_COMMAND(ID_BUTTONfull, OnBUTTONfull)
	ON_COMMAND(ID_BUTTONumbrella, OnBUTTONumbrella)
	ON_WM_TIMER()
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CPlmyView construction/destruction

CPlmyView::CPlmyView()
{
	// TODO: add construction code here
    my_pDC=NULL;
	mytbar=FALSE;
	mysbar=FALSE;
    py[100]=0.9f;
	rainkey=TRUE;
}

CPlmyView::~CPlmyView()
{
}

BOOL CPlmyView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	cs.style=WS_CHILD|WS_VISIBLE|WS_CLIPCHILDREN|WS_CLIPSIBLINGS;

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CPlmyView drawing

void CPlmyView::OnDraw(CDC* pDC)
{

	CPlmyDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
///////////////////////////////////////
    static BOOL     bBusy = FALSE;

    if(bBusy) 	return;
    bBusy = TRUE;

	glClearColor(0.5f,0.1f,1.0f,1.0f);
 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();


	mydraw(); 

	glFinish();
		
	SwapBuffers(wglGetCurrentDC());
	bBusy = FALSE;
////////////////////////////////////////
}

/////////////////////////////////////////////////////////////////////////////
// CPlmyView printing

BOOL CPlmyView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CPlmyView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CPlmyView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CPlmyView diagnostics

#ifdef _DEBUG
void CPlmyView::AssertValid() const
{
	CView::AssertValid();
}

void CPlmyView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CPlmyDoc* CPlmyView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPlmyDoc)));
	return (CPlmyDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CPlmyView message handlers

void CPlmyView::myfirst()
{
	PIXELFORMATDESCRIPTOR pfd;
    int         n;
	HGLRC		hrc;

    my_pDC = new CClientDC(this);

    ASSERT(my_pDC != NULL);

    if (!mypixelformat())
        return;
    n =::GetPixelFormat(my_pDC->GetSafeHdc());
    ::DescribePixelFormat(my_pDC->GetSafeHdc(), n, sizeof(pfd), &pfd);


    hrc = wglCreateContext(my_pDC->GetSafeHdc());
    wglMakeCurrent(my_pDC->GetSafeHdc(), hrc);

	GetClientRect(&my_oldRect);
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

}

void CPlmyView::mydraw()
{
	glTranslatef(0.0f,0.0f,-7.0f);
glEnable(GL_POINT_SMOOTH);
glTranslatef(0.0f,py[100],0.0f);
	rrain();
	glPushMatrix();
	for(int i=0 ;i<10;i++){ 
	glCallList(1);
//	glCallList(listi);
//	glCallList(1);
//	glTranslatef(0.001f,0.001f,0.0f);
//	glScalef(1.005f,1.005f,1.0f);
	rain();
	};
	glPopMatrix();

glDisable(GL_POINT_SMOOTH);
}

BOOL CPlmyView::mypixelformat()
{
	static PIXELFORMATDESCRIPTOR pfd = 
	{
        sizeof(PIXELFORMATDESCRIPTOR), 
        1,                             
        PFD_DRAW_TO_WINDOW |           
          PFD_SUPPORT_OPENGL|		
		  PFD_DOUBLEBUFFER,         
        PFD_TYPE_RGBA,              
        24,                         
        0, 0, 0, 0, 0, 0,           
        0,                          
        0,                          
        0,                          
        0, 0, 0, 0,                 
        32,                         
        0,                          
        0,                          
        PFD_MAIN_PLANE,             
        0,                          
        0, 0, 0                     
    };
    int pixelformat;

    if ( (pixelformat = ChoosePixelFormat(my_pDC->GetSafeHdc(), &pfd)) == 0 )
    {
        MessageBox("ChoosePixelFormat failed");
        return FALSE;
    }

    if (SetPixelFormat(my_pDC->GetSafeHdc(), pixelformat, &pfd) == FALSE)
    {
        MessageBox("SetPixelFormat failed");
        return FALSE;
    }

    return TRUE;

}

int CPlmyView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	myfirst();
	mylist();
	return 0;
}

void CPlmyView::OnDestroy() 
{
	CView::OnDestroy();
	
	// TODO: Add your message handler code here
	KillTimer(1);

	HGLRC	hrc;

	hrc = ::wglGetCurrentContext();

    ::wglMakeCurrent(NULL,  NULL);
	
    if (hrc)
        ::wglDeleteContext(hrc);

    if (my_pDC)
        delete my_pDC;
	
}

void CPlmyView::OnSize(UINT nType, int cx, int cy) 
{
	CView::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here
	if(cy > 0)
    { 

        my_oldRect.right = cx;
        my_oldRect.bottom = cy;


        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
//视场调节:
		glFrustum(-1.0,1.0,-1.0*cy/cx,1.0*cy/cx,5.0,9.0);
		glViewport(0, 0, cx, cy);



    }
RedrawWindow();
	
}

BOOL CPlmyView::OnEraseBkgnd(CDC* pDC) 
{
	// TODO: Add your message handler code here and/or call default
	return TRUE;
	
//	return CView::OnEraseBkgnd(pDC);
}

void CPlmyView::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CMainFrame* pframe=(CMainFrame*)AfxGetApp()->m_pMainWnd;
    pframe->xyshow(point.x,point.y);
	  if(pframe->fullscreenkey){
	    if(point.y<5){
          if(!mytbar){mytbar=TRUE;pframe->PostMessage(WM_COMMAND,ID_VIEW_TOOLBAR);};
		}else{
          if(mytbar){mytbar=FALSE;pframe->PostMessage(WM_COMMAND,ID_VIEW_TOOLBAR);};
		};
	  };
	
	CView::OnMouseMove(nFlags, point);
}

void CPlmyView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CMainFrame* pframe=(CMainFrame*)AfxGetApp()->m_pMainWnd;
    pframe->xyshow(point.x,point.y);
	  if(pframe->fullscreenkey){
          if(!mysbar){pframe->PostMessage(WM_COMMAND,ID_VIEW_STATUS_BAR);mysbar=TRUE;};
	  };
	
	CView::OnLButtonDown(nFlags, point);
}

void CPlmyView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CMainFrame* pframe=(CMainFrame*)AfxGetApp()->m_pMainWnd;
    pframe->xyshow(point.x,point.y);
	  if(pframe->fullscreenkey){
          if(mysbar){pframe->PostMessage(WM_COMMAND,ID_VIEW_STATUS_BAR);mysbar=FALSE;};
	  };
	
	CView::OnLButtonUp(nFlags, point);
}

void CPlmyView::OnBUTTONfull() 
{
	// TODO: Add your command handler code here
	CMainFrame* pframe=(CMainFrame*)AfxGetApp()->m_pMainWnd;
	if(pframe->fullscreenkey){
        if(!mysbar){
          pframe->PostMessage(WM_COMMAND,ID_VIEW_STATUS_BAR);mysbar=TRUE;
		};
	}else{
        if(mysbar){
          pframe->PostMessage(WM_COMMAND,ID_VIEW_STATUS_BAR);mysbar=FALSE;
		};
	};
    pframe->fullscreenkey=!pframe->fullscreenkey;
	
}

void CPlmyView::mylist()
{
listi=glGenLists(1);
glListBase(listi);
glNewList(listi,GL_COMPILE); 
	glTranslatef(0.001f,0.001f,0.0f);
	glScalef(1.005f,1.005f,1.0f);
glEndList();

}

void CPlmyView::OnBUTTONumbrella() 
{
	// TODO: Add your command handler code here
    if(rainkey){
		SetTimer(1, 120, NULL);
		rainkey=!rainkey;
	}else{
		KillTimer(1);
		rainkey=!rainkey;
	};
    Invalidate(FALSE);
}

void CPlmyView::rain()
{
	for(int i=0 ;i<100;i++){ 
          
		  glPointSize(mpointsize[i]);
		  glColor3f(1.0f,1.0f,1.0f);

		  glBegin(GL_POINTS);

	      glVertex3f(px[i],py[i],pz[i]);

          glEnd();
	};
}

void CPlmyView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if(nIDEvent==1){
		if(py[100]<=0.0f){py[100]=0.0f;}else{py[100]=py[100]-0.01f;};

		Invalidate(FALSE);
	};
	
	CView::OnTimer(nIDEvent);
}

void CPlmyView::rrain()
{
	for(int i=0 ;i<100;i++){ 
	mpointsize[i]=2.0f*rand()/RAND_MAX;
    px[i]=1.0f*(rand()-rand())/RAND_MAX;//
	py[i]=1.0f*(rand()-rand())/RAND_MAX;//
	pz[i]=1.0f*(rand()-rand())/RAND_MAX;//
	};
}

⌨️ 快捷键说明

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