Chinaunix首页 | 论坛 | 博客
  • 博客访问: 90566
  • 博文数量: 50
  • 博客积分: 1086
  • 博客等级: 少尉
  • 技术积分: 420
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-25 16:16
文章分类
文章存档

2011年(50)

我的朋友

分类: C/C++

2011-11-15 18:53:15

  1. // WindowsOpengl33.cpp : 定义应用程序的入口点。
  2. //
  3. #define WIN32_LEAN_AND_MEAN
  4. #include "stdafx.h"
  5. #include "WindowsOpengl33.h"
  6. #include <Windows.h>
  7. #include <gl/gl.h>
  8. #include <gl/glu.h>
  9. #include <gl/glaux.h>

  10. float angle = 0.0f;
  11. HDC g_HDC;
  12. bool fullScreen = false;
  13. //为设备环境设置像素格式的函数
  14. void SetupPixelFormat(HDC hDC)
  15. {
  16.  int nPixelFormat;
  17.  static PIXELFORMATDESCRIPTOR pfd = {
  18.   sizeof(PIXELFORMATDESCRIPTOR),
  19.   1,
  20.   PFD_DRAW_TO_WINDOW |
  21.   PFD_SUPPORT_OPENGL |
  22.   PFD_DOUBLEBUFFER,
  23.   PFD_TYPE_RGBA,
  24.   32,
  25.   0, 0, 0, 0, 0, 0,
  26.   0,
  27.   0,
  28.   0,
  29.   0, 0, 0, 0,
  30.   16,
  31.   0,
  32.   0,
  33.   PFD_MAIN_PLANE,
  34.   0,
  35.   0, 0, 0
  36.  };
  37.  nPixelFormat = ChoosePixelFormat(hDC, &pfd);
  38.  SetPixelFormat(hDC, nPixelFormat, &pfd);
  39. }
  40. // 此代码模块中包含的函数的前向声明:
  41. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  42. int WINAPI WinMain(HINSTANCE hInstance,
  43.                      HINSTANCE hPrevInstance,
  44.                      LPTSTR lpCmdLine,
  45.                      int nShowCmd)
  46. {
  47.  WNDCLAS*** windowClass;
  48.  HWND hwnd;
  49.  MSG msg;
  50.  bool done;
  51.  DWORD dwExStyle;
  52.  DWORD dwStyle;
  53.  RECT windowRect;
  54.  int width = 800;
  55.  int height = 600;
  56.  int bits = 32;
  57.  windowRect.left = (long)0;
  58.  windowRect.right = (long)width;
  59.  windowRect.top = (long)0;
  60.  windowRect.bottom = (long)height;

  61.  windowClass.cbSize = sizeof(WNDCLAS***);
  62.  windowClass.style = CS_HREDRAW | CS_VREDRAW;
  63.  windowClass.lpfnWndProc = WndProc;
  64.  windowClass.cbCl***tra = 0;
  65.  windowClass.cbWndExtra = 0;
  66.  windowClass.hInstance = hInstance;
  67.  windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  68.  windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
  69.  windowClass.hbrBackground = NULL;
  70.  windowClass.lpszMenuName = NULL;
  71.  windowClass.lpszClassName = "MyClass";
  72.  windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
  73.  if(!RegisterClas***(&windowClass))
  74.   return 0;
  75.  if(fullScreen)
  76.  {
  77.   DEVMODE dmScreenSettings;
  78.   memset(&dmScreenSettings, 0, sizeof(dmScreenSettings));
  79.   dmScreenSettings.dmSize = sizeof(dmScreenSettings);
  80.   dmScreenSettings.dmPelsWidth = width;
  81.   dmScreenSettings.dmPelsHeight = height;
  82.   dmScreenSettings.dmBitsPerPel = bits;
  83.   dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  84.   if(ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) !=
  85.                DISP_CHANGE_SUCCESSFUL)
  86.   {
  87.    MessageBox(NULL, "Display mode failed", NULL, MB_OK);
  88.    fullScreen = false;
  89.   }
  90.  }
  91.  if(fullScreen)
  92.  {
  93.   dwExStyle = WS_EX_APPWINDOW;
  94.   dwStyle = WS_POPUP;
  95.   ShowCursor(false);
  96.  }
  97.  else
  98.  {
  99.   dwExStyle = NULL;
  100.   dwStyle = WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_SYSMENU | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
  101.  }
  102.  AdjustWindowRectEx(&windowRect, dwStyle, FALSE, dwExStyle);
  103.  hwnd = CreateWindowEx(NULL,
  104.       "MyClass",
  105.       "The OpenGl Window Application",
  106.       WS_OVERLAPPEDWINDOW | WS_VISIBLE |
  107.       WS_SYSMENU | WS_CLIPCHILDREN |
  108.       WS_CLIPSIBLINGS,
  109.       0, 0,
  110.       windowRect.right - windowRect.left,
  111.       windowRect.bottom - windowRect.top,
  112.       NULL,
  113.       NULL,
  114.       hInstance,
  115.       NULL);
  116.  if(!hwnd)
  117.   return 0;
  118.  ShowWindow(hwnd, SW_SHOW);
  119.  UpdateWindow(hwnd);
  120.  done = false;
  121.  while(!done)
  122.  {
  123.   PeekMessage(&msg, hwnd, NULL, NULL, PM_REMOVE);
  124.   if(msg.message == WM_QUIT)
  125.   {
  126.    done = true;
  127.   }
  128.   else //.....Draw......//
  129.        //1.创建RC、关联RC与DC.....................................WM_CREATE 中进行设置
  130.        //2.做具体的绘图工作 myDrawScene().........................以下将进行
  131.        //3.SwapBuffers(m_pDC->m_hDC) 把RC绘制的传到当前的DC上......以下将进行
  132.        //4.释放RC,以便其他的DC使用................................WM_CLOSE 中进行设置
  133.   {
  134.    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  135.    glLoadIdentity();
  136.    angle += 0.1f;
  137.    if(angle >= 360.0f)
  138.     angle = 0.0f;
  139.    glTranslatef(0.0f, 0.0f, -5.0f);
  140.    glRotatef(angle, 0.0f, 0.0f, 1.0f);
  141.    glColor3f(1.0f, 1.0f, 0.0f);
  142.    glBegin(GL_TRIANGLES);
  143.     glVertex3f(0.0f, 0.0f, 0.0f);
  144.     glVertex3f(1.0f, 0.0f, 0.0f);
  145.     glVertex3f(1.0f, 1.0f, 0.0f);
  146.    glEnd();
  147.    SwapBuffers(g_HDC);
  148.    TranslateMessage(&msg);
  149.    DispatchMessage(&msg);
  150.   }
  151.  }
  152.  if(fullScreen)
  153.  {
  154.   ChangeDisplaySettings(NULL, 0);
  155.   ShowCursor(true);
  156.  }
  157.  return msg.wParam;
  158. }

  159. // 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
  160. //
  161. // 目的: 处理主窗口的消息。
  162. //
  163. // WM_COMMAND - 处理应用程序菜单
  164. // WM_PAINT - 绘制主窗口
  165. // WM_DESTROY - 发送退出消息并返回
  166. //
  167. //
  168. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  169. {
  170.  static HGLRC hRC;
  171.  static HDC hDC;
  172.  char string[] = "hello world!";
  173.  int width, height;
  174.  switch (message)
  175.  {
  176.  case WM_CREATE:
  177.   hDC = GetDC(hWnd);
  178.   g_HDC = hDC;
  179.   SetupPixelFormat(hDC);
  180.   hRC = wglCreateContext(hDC); //创建RC
  181.   wglMakeCurrent(hDC, hRC); //关联RC
  182.   return 0;
  183.   break;
  184.  case WM_CLOSE:
  185.   wglMakeCurrent(hDC, NULL); //释放与hDC对应的RC
  186.   wglDeleteContext(hRC); //删除RC
  187.   PostQuitMessage(0);
  188.   return 0;
  189.   break;
  190.  case WM_SIZE:
  191.   height = HIWORD(lParam);
  192.   width = LOWORD(lParam);
  193.   if(height == 0)
  194.    height = 1;
  195.   glViewport(0, 0, width, height);
  196.   glMatrixMode(GL_PROJECTION);
  197.   glLoadIdentity();
  198.   //计算窗口尺寸比例
  199.   gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 1.0f ,1000.0f);
  200.   glMatrixMode(GL_MODELVIEW);
  201.   glLoadIdentity();
  202.   return 0;
  203.   break;
  204.  default:
  205.   break;
  206.  }
  207.  return(DefWindowProc(hWnd, message, wParam, lParam));
  208. }
阅读(603) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~