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

📄 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;
	ange=5.0f;
	mykey=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.0f,0.6f,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_LINE_STIPPLE);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_LINE_SMOOTH);
	glPushMatrix();
glTranslatef(0.0f,-0.6f,0.0f);
glRotated(ange,0.0,1.0,0.0);
	glCallList(1);
	glPopMatrix();
	glPushMatrix();
	rrain();
	for(int i=0 ;i<10;i++){ 
	glCallList(2);
	rain();
	};
	glPopMatrix();

glDisable(GL_LINE_SMOOTH);
glDisable(GL_POINT_SMOOTH);
glDisable(GL_LINE_STIPPLE);

}

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(2);
glListBase(listi);
glNewList(listi,GL_COMPILE); 
umbrella();
glEndList();

glNewList(listi+1,GL_COMPILE); 
	glScalef(1.001f,1.001f,1.0f);
	glTranslatef(-0.002f,-0.005f,0.0f);
glEndList();
}

void CPlmyView::umbrella()
{
float rx=1.0f;
float ange1=3.1416f/3.0f;
float ange2=0.0f;
float k=-14.5;
	glPushMatrix();
for(float i=0;i<360;i++){
	ange2=3.1416f/180.0f*i;
    glShadeModel(GL_SMOOTH);
    glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
 glBegin(GL_QUAD_STRIP);
   for(float j=0;j<35;j++){
	  ange1=3.1416f/3.0f-3.1416f/900.0f*((k*k)/14-14)+3.1416f/180.0f*j;
	  rx=1.0f+0.003f*((k*k)/14-14)*(1-j/35);
	  if(ange1>=1.5708f){}else{
	glColor3f(rx*rx*sinf(ange1),rx*cosf(ange1)*cosf(ange2),rx*cosf(ange1)*sinf(ange2));
	glVertex3f(rx*cosf(ange1)*cosf(ange2),
	           rx*sinf(ange1),
			   -rx*cosf(ange1)*sinf(ange2));
		  ange1=3.1416f/3.0f-3.1416f/900.0f*((k+1.0f)*(k+1.0f)/14-14)+3.1416f/180.0f*j;
	  rx=1.0f+0.003f*((k+1.0f)*(k+1.0f)/14-14)*(1-j/35);
	glVertex3f(rx*cosf(ange1)*cosf(ange2+3.1416f/180.0f),
	           rx*sinf(ange1),
			   -rx*cosf(ange1)*sinf(ange2+3.1416f/180.0f));
	  };
  };
glEnd();
k=k+1;
if(k>=15.5){
 glBegin(GL_QUAD_STRIP);
 for(float j=0;j<35;j++){
	  ange1=3.1416f/3.0f-3.1416f/900.0f*((k*k)/14-14)+3.1416f/180.0f*j;
	  rx=1.0f+0.003f*((k*k)/14-14)*(1-j/35);
	  if(ange1>=1.5708f){}else{
	glColor3f(rx*rx*sinf(ange1),rx*cosf(ange1)*cosf(ange2),rx*cosf(ange1)*sinf(ange2));
	glVertex3f(rx*cosf(ange1)*cosf(ange2),
	           rx*sinf(ange1),
			   -rx*cosf(ange1)*sinf(ange2));
		  ange1=3.1416f/3.0f-3.1416f/900.0f*((k-1.0f)*(k-1.0f)/14-14)+3.1416f/180.0f*j;
	      rx=1.0f+0.003f*((k-1.0f)*(k-1.0f)/14-14)*(1-j/35);
	glVertex3f(rx*cosf(ange1)*cosf(ange2+3.1416f/180.0f),
	           rx*sinf(ange1),
			   -rx*cosf(ange1)*sinf(ange2+3.1416f/180.0f));
	  };
  };
 glEnd();
 k=-14.5f;
};

};
         glLineStipple(1,0xFEFE);
	     glColor3f(1.0f,0.6f,0.0f);
         glLineWidth (2.5f);
glBegin(GL_LINES);
	glVertex3f(0.0f,0.50f,0.0f);
	glVertex3f(0.0f,0.60f,0.0f);
glEnd();
         glLineStipple(1,0xFFFF);
         glLineWidth (2.5f);
	     glColor3f(1.0f,0.8f,0.0f);
glBegin(GL_LINES);
	glVertex3f(0.0f,1.01f,0.0f);
	glVertex3f(0.0f,0.50f,0.0f);
glEnd();
         glLineStipple(1,0xFFFF);
         glLineWidth (0.5f);

	glPopMatrix();
}

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

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

		  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(ange==360.0f){ange=5.0f;}else{ange=ange+5.0f;};
		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 + -