OpenGL.cpp
上传用户:helili
上传日期:2015-01-05
资源大小:34k
文件大小:5k
源码类别:

OpenGL

开发平台:

Visual C++

  1. // OpenGL.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "$$root$$.h"
  5. #include "OpenGL.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // COpenGL
  13. COpenGL::COpenGL()
  14. {
  15. //给成员变量赋初值
  16. step=0.0;
  17. s=0.1;
  18. }
  19. COpenGL::~COpenGL()
  20. {
  21. wglMakeCurrent(NULL, NULL) ; 
  22. wglDeleteContext(hglrc); //删除渲染描述表
  23. ::ReleaseDC (m_hWnd, hdc) ; //释放设备描述表
  24. }
  25. BEGIN_MESSAGE_MAP(COpenGL, CWnd)
  26. //{{AFX_MSG_MAP(COpenGL)
  27. ON_WM_CREATE()
  28. ON_WM_PAINT()
  29. //}}AFX_MSG_MAP
  30. END_MESSAGE_MAP()
  31. ////////////////////////////////////////////////////////////////////////////
  32. // 设置像素格式函数
  33. int COpenGL::MySetPixelFormat(HDC hdc)
  34. {
  35. PIXELFORMATDESCRIPTOR pfd = { 
  36.     sizeof(PIXELFORMATDESCRIPTOR),    // pfd结构的大小 
  37.     1,                                // 版本号 
  38.     PFD_DRAW_TO_WINDOW |              // 支持在窗口中绘图 
  39.     PFD_SUPPORT_OPENGL |              // 支持 OpenGL 
  40.     PFD_DOUBLEBUFFER,                 // 双缓存模式 
  41.     PFD_TYPE_RGBA,                    // RGBA 颜色模式 
  42.     24,                               // 24 位颜色深度 
  43.     0, 0, 0, 0, 0, 0,                 // 忽略颜色位 
  44.     0,                                // 没有非透明度缓存 
  45.     0,                                // 忽略移位位 
  46.     0,                                // 无累加缓存 
  47.     0, 0, 0, 0,                       // 忽略累加位 
  48.     32,                               // 32 位深度缓存     
  49.     0,                                // 无模板缓存 
  50.     0,                                // 无辅助缓存 
  51.     PFD_MAIN_PLANE,                   // 主层 
  52.     0,                                // 保留 
  53.     0, 0, 0                           // 忽略层,可见性和损毁掩模 
  54. }; 
  55. int  iPixelFormat; 
  56.  
  57. // 为设备描述表得到最匹配的像素格式 
  58. if((iPixelFormat = ChoosePixelFormat(hdc, &pfd)) == 0)
  59. {
  60. MessageBox("ChoosePixelFormat Failed", NULL, MB_OK);
  61. return 0;
  62. }
  63.  
  64. // 设置最匹配的像素格式为当前的像素格式 
  65. if(SetPixelFormat(hdc, iPixelFormat, &pfd) == FALSE)
  66. {
  67. MessageBox("SetPixelFormat Failed", NULL, MB_OK);
  68. return 0;
  69. }
  70. return 1;
  71. }
  72. /////////////////////////////////////////////////////////////////////////////
  73. // COpenGL message handlers
  74. int COpenGL::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  75. {
  76. if (CWnd::OnCreate(lpCreateStruct) == -1)
  77. return -1;
  78. // TODO: Add your specialized creation code here
  79. // 设置当前的绘图像素格式
  80. MySetPixelFormat(::GetDC(m_hWnd));
  81. // 创建绘图描述表
  82. hdc = ::GetDC(m_hWnd);
  83. // 创建渲染描述表
  84. hglrc = wglCreateContext(hdc);
  85. // 使绘图描述表为当前调用现程的当前绘图描述表 
  86. wglMakeCurrent(hdc, hglrc);
  87. return 0;
  88. }
  89. void COpenGL::OnPaint() 
  90. {
  91. //CPaintDC dc(this); // device context for painting
  92. // TODO: Add your message handler code here
  93. //调用OpenGL绘图函数进行图形绘制
  94. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //清除颜色缓存和深度缓存
  95.     s+=0.005;
  96.     if(s>1.0)
  97. s=0.1;
  98. step = step + 1.0;
  99.     if (step > 360.0)
  100. step = step - 360.0;
  101.     glPushMatrix();
  102. glScalef(s,s,s);
  103.     glRotatef(step,0.0,1.0,0.0);
  104.     glRotatef(step,0.0,0.0,1.0);
  105.    glRotatef(step,1.0,0.0,0.0);
  106.     DrawColorBox();
  107.     glPopMatrix();
  108.     glFlush();
  109. SwapBuffers(hdc);
  110. // Do not call CWnd::OnPaint() for painting messages
  111. }
  112. void COpenGL::DrawColorBox(void)
  113. {
  114. GLfloat  p1[]={0.5,-0.5,-0.5}, p2[]={0.5,0.5,-0.5},
  115.                 p3[]={0.5,0.5,0.5},   p4[]={0.5,-0.5,0.5},
  116.                 p5[]={-0.5,-0.5,0.5}, p6[]={-0.5,0.5,0.5},
  117.                 p7[]={-0.5,0.5,-0.5}, p8[]={-0.5,-0.5,-0.5};
  118. GLfloat  m1[]={1.0,0.0,0.0}, m2[]={-1.0,0.0,0.0},
  119. m3[]={0.0,1.0,0.0}, m4[]={0.0,-1.0,0.0},
  120. m5[]={0.0,0.0,1.0}, m6[]={0.0,0.0,-1.0};
  121.    
  122. GLfloat  c1[]={0.0,0.0,1.0},  c2[]={0.0,1.0,1.0},
  123.                 c3[]={1.0,1.0,1.0},  c4[]={1.0,0.0,1.0},
  124.                 c5[]={1.0,0.0,0.0},  c6[]={1.0,1.0,0.0},
  125.                 c7[]={0.0,1.0,0.0},  c8[]={1.0,1.0,1.0};
  126.               
  127.  
  128.     glBegin (GL_QUADS); //绘制多个四边形
  129.         
  130.   glColor3fv(c1);
  131.          glNormal3fv(m1);
  132.         glVertex3fv(p1);
  133.   glColor3fv(c2);
  134.         glVertex3fv(p2);
  135.   glColor3fv(c3);
  136.         glVertex3fv(p3);
  137.   glColor3fv(c4);
  138.         glVertex3fv(p4);
  139.   glColor3fv(c5);
  140.          glNormal3fv(m5);
  141.         glVertex3fv(p5);
  142.   glColor3fv(c6);
  143.         glVertex3fv(p6);
  144.   glColor3fv(c7);
  145.         glVertex3fv(p7);
  146.   glColor3fv(c8);
  147.         glVertex3fv(p8);
  148.   glColor3fv(c5);
  149.          glNormal3fv(m3);
  150.         glVertex3fv(p5);
  151.   glColor3fv(c6);
  152.         glVertex3fv(p6);
  153.   glColor3fv(c3);
  154.         glVertex3fv(p3);
  155.   glColor3fv(c4);
  156.         glVertex3fv(p4);
  157.   glColor3fv(c1);
  158.          glNormal3fv(m4);
  159.         glVertex3fv(p1);
  160.   glColor3fv(c2);
  161.         glVertex3fv(p2);
  162.   glColor3fv(c7);
  163.         glVertex3fv(p7);
  164.   glColor3fv(c8);
  165.         glVertex3fv(p8);
  166.   glColor3fv(c2);
  167.          glNormal3fv(m5);
  168.         glVertex3fv(p2);
  169.   glColor3fv(c3);
  170.         glVertex3fv(p3);
  171.   glColor3fv(c6);
  172.         glVertex3fv(p6);
  173.   glColor3fv(c7);
  174.         glVertex3fv(p7);
  175.   glColor3fv(c1);
  176.          glNormal3fv(m6);
  177.         glVertex3fv(p1);
  178.   glColor3fv(c4);
  179.         glVertex3fv(p4);
  180.   glColor3fv(c5);
  181.         glVertex3fv(p5);
  182.   glColor3fv(c8);
  183.         glVertex3fv(p8);
  184.   glEnd();
  185. }