www.pudn.com > cmy3.rar > cmyView.cpp
// cmyView.cpp : implementation of the CCmyView class
//
#include "stdafx.h"
#include "cmy.h"
#include "MainFrm.h"
#include "Math.h"
#include "cmyDoc.h"
#include "cmyView.h"
#define ABS(x) ((x)<0 ? -(x) : (x)>0 ? (x) : 0)
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCmyView
IMPLEMENT_DYNCREATE(CCmyView, CView)
BEGIN_MESSAGE_MAP(CCmyView, CView)
//{{AFX_MSG_MAP(CCmyView)
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_BUTTONmove, OnBUTTONmove)
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()
/////////////////////////////////////////////////////////////////////////////
// CCmyView construction/destruction
CCmyView::CCmyView()
{
// TODO: add construction code here
my_pDC=NULL;
mytbar=FALSE;
mysbar=FALSE;
movekey=TRUE;
npoint=-1;
for(int i=0;i<102;i++){
for(int j=0;j<76;j++){
cpoint[i][j][0]=0.0f;
cpoint[i][j][1]=0.0f;
cpoint[i][j][2]=0.0f;
};
};
}
CCmyView::~CCmyView()
{
}
BOOL CCmyView::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);
}
/////////////////////////////////////////////////////////////////////////////
// CCmyView drawing
void CCmyView::OnDraw(CDC* pDC)
{
CCmyDoc* 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.5f,0.8f,0.9f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
mydraw();
glFinish();
SwapBuffers(wglGetCurrentDC());
bBusy = FALSE;
}
/////////////////////////////////////////////////////////////////////////////
// CCmyView printing
BOOL CCmyView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CCmyView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CCmyView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CCmyView diagnostics
#ifdef _DEBUG
void CCmyView::AssertValid() const
{
CView::AssertValid();
}
void CCmyView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CCmyDoc* CCmyView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCmyDoc)));
return (CCmyDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CCmyView message handlers
BOOL CCmyView::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;
}
void CCmyView::mydraw()
{
glTranslatef(0.0f,0.0f,-7.0f);
lm();
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT1);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LINE_STIPPLE);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_LINE_SMOOTH);
glDepthMask(GL_FALSE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glPushMatrix();
drawplat();
glPopMatrix();
glDepthMask(GL_TRUE);
glDisable(GL_BLEND) ;
glDisable(GL_LINE_SMOOTH);
glDisable(GL_POINT_SMOOTH);
glDisable(GL_LINE_STIPPLE);
}
void CCmyView::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();
}
int CCmyView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
myfirst();
return 0;
}
void CCmyView::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 CCmyView::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 CCmyView::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
return TRUE;
// return CView::OnEraseBkgnd(pDC);
}
void CCmyView::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 CCmyView::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;};
};
*/
if(npoint>=3){
npoint=0;
for(int i=0;i<102;i++){
for(int j=0;j<76;j++){
cpoint[i][j][0]=0.0f;
cpoint[i][j][1]=0.0f;
cpoint[i][j][2]=0.0f;
};
};
}else{
npoint=npoint+1;
};
lpoint[npoint][0]=float(point.x);
lpoint[npoint][1]=float(point.y);
lpoint[npoint][2]=0.0f;
lpoint[npoint][3]=1.0f;
CView::OnLButtonDown(nFlags, point);
}
void CCmyView::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 CCmyView::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 CCmyView::OnBUTTONmove()
{
// TODO: Add your command handler code here
if(movekey){
SetTimer(1, 60, NULL);
}else{
KillTimer(1);
};
movekey=!movekey;
Invalidate(FALSE);
}
void CCmyView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(nIDEvent==1){
for(int k=0;k=(3.1416f*2.0f)){lpoint[k][2]=0.0f;};
lpoint[k][3]=lpoint[k][3]+3.0f;
if((lpoint[k][3])>=1024.0f){lpoint[k][3]=1024.0f;};
};
};
Invalidate(FALSE);//ÏÔʾ¸üС£
CView::OnTimer(nIDEvent);
}
void CCmyView::drawplat()
{
glScalef(0.27f,0.27f,1.0f);
glTranslatef(-5.1f,3.8f,0.0f);
glColor4f(1.0f,1.0f,1.0f,0.1f);
glShadeModel(GL_SMOOTH);
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
for(int i=0;i<101;i++){
for(int j=0;j<75;j++){
glEnable(GL_NORMALIZE);
glBegin(GL_QUADS);
glNormal3f(
cpoint[i][j][0],
cpoint[i][j][1],
cpoint[i][j][2]
);
glVertex3f(0.1f*i,-0.1f*j,0.0f);
glVertex3f(0.1f*i,-0.1f*(j+1),0.0f);
glVertex3f(0.1f*(i+1),-0.1f*(j+1),0.0f);
glVertex3f(0.1f*(i+1),-0.1f*j,0.0f);
glEnd();
};
};
}
void CCmyView::lm()
{
GLfloat lAmb[4];
GLfloat lDif[4];
GLfloat lSpe[4];
GLfloat lPos[4];
GLfloat mAmb[4];
GLfloat mDif[4];
GLfloat mSpe[4];
GLfloat mEmi[4];
GLfloat mShininess;
lAmb[0]=0.2f; lAmb[1]=0.2f;
lAmb[2]=0.2f; lAmb[3]=1.0f;
lDif[0]=0.2f; lDif[1]=0.2f;
lDif[2]=0.1f; lDif[3]=1.0f;
lSpe[0]=0.5f; lSpe[1]=0.5f;
lSpe[2]=0.7f; lSpe[3]=1.0f;
lPos[0]=0.0f; lPos[1]=1.1f;
lPos[2]=100.1f; lPos[3]=0.0f;
mAmb[0]=0.0f; mAmb[1]=0.0f;
mAmb[2]=0.3f; mAmb[3]=0.8f;
mDif[0]=0.0f; mDif[1]=0.0f;
mDif[2]=0.0f; mDif[3]=1.0f;
mSpe[0]=0.1f; mSpe[1]=0.1f;
mSpe[2]=0.6f; mSpe[3]=0.8f;
mEmi[0]=0.3f; mEmi[1]=0.3f;
mEmi[2]=0.3f; mEmi[3]=0.0f;
mShininess=100.0f;
glLightfv(GL_LIGHT1,GL_AMBIENT,lAmb);
glLightfv(GL_LIGHT1,GL_DIFFUSE,lDif);
glLightfv(GL_LIGHT1,GL_SPECULAR,lSpe);
glLightfv(GL_LIGHT1,GL_POSITION,lPos);
glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,mAmb);
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,mDif);
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mSpe);
glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,mEmi);
glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,mShininess);
}