|  | 
 
 
 楼主|
发表于 2007-7-22 16:50:14
|
显示全部楼层 
回复: OpenGL sample
| 2.SDOpenGLView.cpp // SDOpenGLView.cpp : implementation of the CSDOpenGLView class
 //
 #include "stdafx.h"
 #include "Gps.h"
 #include "SDOpenGLDoc.h"
 #include "SDOpenGLView.h"
 #ifdef _DEBUG
 #define new DEBUG_NEW
 #undef THIS_FILE
 static char THIS_FILE[] = __FILE__;
 #endif
 /////////////////////////////////////////////////////////////////////////////
 // CSDOpenGLView
 IMPLEMENT_DYNCREATE(CSDOpenGLView, CView)
 BEGIN_MESSAGE_MAP(CSDOpenGLView, CView)
 //{{AFX_MSG_MAP(CSDOpenGLView)
 ON_WM_CREATE()
 ON_WM_DESTROY()
 ON_WM_SIZE()
 ON_WM_TIMER()
 ON_WM_LBUTTONDOWN()
 ON_WM_CHAR()
 ON_WM_MOUSEWHEEL()
 ON_COMMAND(ID_VIEW_LIGHT, OnViewLight)
 //}}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()
 /////////////////////////////////////////////////////////////////////////////
 // CSDOpenGLView construction/destruction
 CSDOpenGLView::CSDOpenGLView()
 {
 // TODO: add construction code here
 pApp=(CGpsApp*)AfxGetApp();
 ReSetView(1,1);
 ReSetView(2,2);
 ReSetView(3,3);
 ReSetView(4,4);
 
 }
 CSDOpenGLView::~CSDOpenGLView()
 {
 }
 BOOL CSDOpenGLView:
  reCreateWindow(CREATESTRUCT& cs) {
 // TODO: Modify the Window class or styles here by modifying
 //  the CREATESTRUCT cs
 ////////////////////////////////////////////////////////////////
 //设置窗口类型
 cs.style |=WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
 ////////////////////////////////////////////////////////////////
 
 return CView:
  reCreateWindow(cs); }
 /////////////////////////////////////////////////////////////////////////////
 // CSDOpenGLView drawing
 void CSDOpenGLView::OnDraw(CDC* pDC)
 {
 CSDOpenGLDoc* pDoc = GetDocument();
 ASSERT_VALID(pDoc);
 // TODO: add draw code for native data here
 CRect reWindow;
 GetWindowRect(reWindow);
 //SX,SY分别对应屏幕的1/2宽和1/2高
 SX=reWindow.Width()/2;
 SY=reWindow.Height()/2;
 /*
 VRect[1].left=0;VRect[1].right=SX/2;
 VRect[1].top=0;VRect[1].bottom=SY+SY/2;
 VRect[2].left=SX/2;VRect[2].right=SX*2;
 VRect[2].top=0;VRect[2].bottom=SY+SY/2;
 VRect[3].left=0;VRect[3].right=SY;
 VRect[3].top=SY;VRect[3].bottom=SY*2;
 VRect[4].left=SY;VRect[4].right=SY*2;
 VRect[4].top=SY;VRect[4].bottom=SY*2;
 */
 
 //////////////////////////////////////////////////////////////////
 RenderScene(); //渲染场景
 //////////////////////////////////////////////////////////////////
 
 
 }
 /////////////////////////////////////////////////////////////////////////////
 // CSDOpenGLView printing
 BOOL CSDOpenGLView::OnPreparePrinting(CPrintInfo* pInfo)
 {
 // default preparation
 return DoPreparePrinting(pInfo);
 }
 void CSDOpenGLView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
 {
 // TODO: add extra initialization before printing
 }
 void CSDOpenGLView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
 {
 // TODO: add cleanup after printing
 }
 /////////////////////////////////////////////////////////////////////////////
 // CSDOpenGLView diagnostics
 #ifdef _DEBUG
 void CSDOpenGLView::AssertValid() const
 {
 CView::AssertValid();
 }
 void CSDOpenGLView:
  ump(CDumpContext& dc) const {
 CView:
  ump(dc); }
 CSDOpenGLDoc* CSDOpenGLView::GetDocument() // non-debug version is inline
 {
 ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSDOpenGLDoc)));
 return (CSDOpenGLDoc*)m_pDocument;
 }
 #endif //_DEBUG
 /////////////////////////////////////////////////////////////////////////////
 // CSDOpenGLView message handlers
 int CSDOpenGLView::OnCreate(LPCREATESTRUCT lpCreateStruct)
 {
 if (CView::OnCreate(lpCreateStruct) == -1)
 return -1;
 
 // TODO: Add your specialized creation code here
 //////////////////////////////////////////////////////////////////
 //初始化OpenGL和设置定时器
 m_pDC = new CClientDC(this);
 SetTimer(1, 20, NULL);
 InitializeOpenGL(m_pDC);
 //////////////////////////////////////////////////////////////////
 return 0;
 }
 void CSDOpenGLView::OnDestroy()
 {
 CView::OnDestroy();
 
 // TODO: Add your message handler code here
 /////////////////////////////////////////////////////////////////
 //删除调色板和渲染上下文、定时器
 ::wglMakeCurrent(0,0);
 ::wglDeleteContext( m_hRC);
 if (m_hPalette)
 DeleteObject(m_hPalette);
 if ( m_pDC )
 {
 delete m_pDC;
 }
 KillTimer(1);
 /////////////////////////////////////////////////////////////////
 
 }
 void CSDOpenGLView::OnSize(UINT nType, int cx, int cy)
 {
 CView::OnSize(nType, cx, cy);
 
 // TODO: Add your message handler code here
 /////////////////////////////////////////////////////////////////
 //添加窗口缩放时的图形变换函数
 // glViewport(0,0,cx,cy);
 /////////////////////////////////////////////////////////////////
 
 }
 void CSDOpenGLView::OnTimer(UINT nIDEvent)
 {
 // TODO: Add your message handler code here and/or call default
 /////////////////////////////////////////////////////////////////
 //添加定时器响应函数和场景更新函数
 Invalidate(FALSE);
 /////////////////////////////////////////////////////////////////
 
 CView::OnTimer(nIDEvent);
 }
 /////////////////////////////////////////////////////////////////////
 //                   设置逻辑调色板
 //////////////////////////////////////////////////////////////////////
 void CSDOpenGLView::SetLogicalPalette(void)
 {
 struct
 {
 WORD Version;
 WORD NumberOfEntries;
 PALETTEENTRY aEntries[256];
 } logicalPalette = { 0x300, 256 };
 BYTE reds[] = {0, 36, 72, 109, 145, 182, 218, 255};
 BYTE greens[] = {0, 36, 72, 109, 145, 182, 218, 255};
 BYTE blues[] = {0, 85, 170, 255};
 for (int colorNum=0; colorNum<256; ++colorNum)
 {
 logicalPalette.aEntries[colorNum].peRed =
 reds[colorNum & 0x07];
 logicalPalette.aEntries[colorNum].peGreen =
 greens[(colorNum >> 0x03) & 0x07];
 logicalPalette.aEntries[colorNum].peBlue =
 blues[(colorNum >> 0x06) & 0x03];
 logicalPalette.aEntries[colorNum].peFlags = 0;
 }
 m_hPalette = CreatePalette ((LOGPALETTE*)&logicalPalette);
 }
 
 //////////////////////////////////////////////////////////
 //      初始化openGL场景
 //////////////////////////////////////////////////////////
 BOOL CSDOpenGLView::InitializeOpenGL(CDC* pDC)
 {
 
 m_pDC = pDC;
 SetupPixelFormat();
 //生成绘制描述表
 m_hRC = ::wglCreateContext(m_pDC->GetSafeHdc());
 //置当前绘制描述表
 ::wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC);
 
 return TRUE;
 }
 //////////////////////////////////////////////////////////
 //      设置像素格式
 //////////////////////////////////////////////////////////
 BOOL CSDOpenGLView::SetupPixelFormat()
 {
 PIXELFORMATDESCRIPTOR pfd = {
 sizeof(PIXELFORMATDESCRIPTOR),    // pfd结构的大小
 1,                                // 版本号
 PFD_DRAW_TO_WINDOW |              // 支持在窗口中绘图
 PFD_SUPPORT_OPENGL |              // 支持 OpenGL
 PFD_DOUBLEBUFFER,                 // 双缓存模式
 PFD_TYPE_RGBA,                    // RGBA 颜色模式
 24,                               // 24 位颜色深度
 0, 0, 0, 0, 0, 0,                 // 忽略颜色位
 0,                                // 没有非透明度缓存
 0,                                // 忽略移位位
 0,                                // 无累加缓存
 0, 0, 0, 0,                       // 忽略累加位
 32,                               // 32 位深度缓存
 0,                                // 无模板缓存
 0,                                // 无辅助缓存
 PFD_MAIN_PLANE,                   // 主层
 0,                                // 保留
 0, 0, 0                           // 忽略层,可见性和损毁掩模
 };
 int pixelformat;
 pixelformat = ::ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd);//选择像素格式
 ::SetPixelFormat(m_pDC->GetSafeHdc(), pixelformat, &pfd); //设置像素格式
 if(pfd.dwFlags & PFD_NEED_PALETTE)
 SetLogicalPalette(); //设置逻辑调色板
 return TRUE;
 }
 
 //////////////////////////////////////////////////////////
 //      场景绘制与渲染
 //////////////////////////////////////////////////////////
 BOOL CSDOpenGLView::RenderScene()
 {
 //设置背景色
 SetBkColor();
 glClear(GL_COLOR_BUFFER_BIT);
 for(int i=1;i<5;i++)
 {
 if((pApp->WFlag!=1)&&(pApp->ActScean!=i))
 DrawView(i);
 }
 DrawView(pApp->ActScean);
 ::SwapBuffers(m_pDC->GetSafeHdc());
 //m_pDC->TextOut(10,10,"顶视图");
 return TRUE;
 }
 //设置背景色
 void CSDOpenGLView::SetBkColor()
 {
 glClearColor(pApp->BkR, pApp->BkG, pApp->BkB, 0.0f);
 }
 
 void CSDOpenGLView:
  rawGrid() {
 float gs,gxmax,gx;
 gs=pApp->GridS;
 gxmax=(pApp->GridN-1)*gs/2;
 gx=-gxmax;
 //指定颜色
 glColor3f(0.263, 0.4, 0.333);//墨绿色
 //glColor3f(1.0f, 1.0f, 1.0f);//白色
 
 for(int i=0;i<pApp->GridN;i++,gx+=gs)
 {
 glBegin(GL_LINES);
 glVertex2f(-gxmax,gx);
 glVertex2f(gxmax,gx);
 glVertex2f(gx,-gxmax);
 glVertex2f(gx,gxmax);
 glEnd();
 }
 }
 void CSDOpenGLView::OnLButtonDown(UINT nFlags, CPoint point)
 {
 // TODO: Add your message handler code here and/or call default
 
 for(int i=1;i<5;i++)
 if((VRect.PtInRect(point))&&(pApp->WFlag!=1))
 {
 pApp->ActScean=i;
 }
 CView::OnLButtonDown(nFlags, point);
 }
 void CSDOpenGLView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
 {
 // TODO: Add your message handler code here and/or call default
 if(nChar=='w')pApp->WFlag=1;
 if(nChar=='W')pApp->WFlag=0;
 if(nChar=='t')ReSetView(pApp->ActScean,1);
 if(nChar=='l')ReSetView(pApp->ActScean,3);
 if(nChar=='f')ReSetView(pApp->ActScean,2);
 if(nChar=='p')ReSetView(pApp->ActScean,4);
 if(nChar=='a'){
 pApp->SceanX[pApp->ActScean]-=0.1*sin(45);
 pApp->SceanZ[pApp->ActScean]-=0.1*cos(45);
 }
 if(nChar=='d'){
 pApp->SceanX[pApp->ActScean]+=0.1*sin(45);
 pApp->SceanZ[pApp->ActScean]+=0.1*cos(45);
 }
 
 CView::OnChar(nChar, nRepCnt, nFlags);
 }
 void CSDOpenGLView:
  rawView(int n) {
 if(n==1) glViewport(0,0,SX*2,SY*2);
 //if(n==1)glViewport(0,SY,SY,SY);
 //if(n==2)glViewport(SY,SY,SY,SY);
 //if(n==3)glViewport(0,0,SY,SY);
 //if(n==4)glViewport(SY,0,SY,SY);
 //if(pApp->WFlag==1)glViewport(0,0,SY*2,SY*2);
 
 glLoadIdentity ();
 glRotatef(pApp->SceanAy[n],0,1,0);
 glRotatef(pApp->SceanAx[n],1,0,0);
 glRotatef(pApp->SceanAz[n],0,0,1);
 glTranslatef(pApp->SceanX[n],pApp->SceanY[n],pApp->SceanZ[n]);
 glScalef(pApp->SceanSx[n],pApp->SceanSy[n],pApp->SceanSz[n]);
 //画栅格
 DrawGrid();
 //设置图形的颜色
 glColor3f(0.3f, 1.0f, 1.0f);
 auxSolidBox(0.3,0.2,0.3);
 glColor3f(0.8,0.8,0.8);
 if(pApp->ActScean==n)
 {
 glColor3f(0.9,0.8,0.3);
 }
 glLoadIdentity ();
 glLineWidth(3);
 glBegin(GL_LINE_STRIP);
 glVertex2f(-1,-1);
 glVertex2f(1,-1);
 glVertex2f(1,1);
 glVertex2f(-1,1);
 glVertex2f(-1,-1);
 glEnd();
 glLineWidth(1);
 }
 BOOL CSDOpenGLView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
 {
 // TODO: Add your message handler code here and/or call default
 if(zDelta>0){
 pApp->SceanSx[pApp->ActScean]+=0.1;
 pApp->SceanSy[pApp->ActScean]+=0.1;
 pApp->SceanSz[pApp->ActScean]+=0.1;
 }
 if(zDelta<0){
 pApp->SceanSx[pApp->ActScean]-=0.1;
 pApp->SceanSy[pApp->ActScean]-=0.1;
 pApp->SceanSz[pApp->ActScean]-=0.1;
 }
 return CView::OnMouseWheel(nFlags, zDelta, pt);
 }
 void CSDOpenGLView::ReSetView(int N, int Kind)
 {
 pApp->SceanSx[N]=1;
 pApp->SceanSy[N]=1;
 pApp->SceanSz[N]=1;
 pApp->SceanX[N]=0;
 pApp->SceanY[N]=0;
 pApp->SceanZ[N]=0;
 if(Kind==1){
 pApp->SceanAx[N]=0;
 pApp->SceanAy[N]=0;
 pApp->SceanAz[N]=0;
 }
 if(Kind==2){
 pApp->SceanAx[N]=90;
 pApp->SceanAy[N]=0;
 pApp->SceanAz[N]=0;
 }
 if(Kind==3){
 pApp->SceanAx[N]=90;
 pApp->SceanAy[N]=0;
 pApp->SceanAz[N]=0;
 }
 if(Kind==4){
 pApp->SceanAx[N]=45;
 pApp->SceanAy[N]=0;
 pApp->SceanAz[N]=45;
 }
 }
 //不同视图方向——主视图,左视图,俯视图等
 void CSDOpenGLView::CadViewDirection(int direction)
 {
 // if(direction==1)
 // AfxMessageBox("i");
 switch(direction)
 {
 case 0 :
 OnFrontView();
 break;
 case 1:
 OnLeftView();
 break;
 case 2:
 OnTopView();
 break;
 default:
 OnFrontView();
 break;
 }
 
 }
 //主视图
 void CSDOpenGLView::OnFrontView()
 {
 // TODO: Add your command handler code here
 pApp->SceanSx[pApp->ActScean]=1;
 pApp->SceanSy[pApp->ActScean]=1;
 pApp->SceanSz[pApp->ActScean]=1;
 pApp->SceanAx[pApp->ActScean]=90;
 pApp->SceanAy[pApp->ActScean]=0;
 pApp->SceanAz[pApp->ActScean]=0;
 }
 //左视图
 void CSDOpenGLView::OnLeftView()
 {
 // TODO: Add your command handler code here
 pApp->SceanSx[pApp->ActScean]=1;
 pApp->SceanSy[pApp->ActScean]=1;
 pApp->SceanSz[pApp->ActScean]=1;
 pApp->SceanAx[pApp->ActScean]=90;
 pApp->SceanAy[pApp->ActScean]=0;
 pApp->SceanAz[pApp->ActScean]=0;
 
 }
 //俯视图
 void CSDOpenGLView::OnTopView()
 {
 // TODO: Add your command handler code here
 
 pApp->SceanSx[pApp->ActScean]=1;
 pApp->SceanSy[pApp->ActScean]=1;
 pApp->SceanSz[pApp->ActScean]=1;
 pApp->SceanAx[pApp->ActScean]=0;
 pApp->SceanAy[pApp->ActScean]=0;
 pApp->SceanAz[pApp->ActScean]=0;
 }
 void CSDOpenGLView::OnViewLight()
 {
 // TODO: Add your command handler code here
 // theApp.pDocTemplateCatalog->OpenDocumentFile(NULL);//窗口大小???
 }
 | 
 |