求wince 下用opengl es 画一个三角形的代码

RT!或者es1.1编程指南... RT!
或者es 1.1编程指南
展开
 我来答
火驹
2010-01-26 · TA获得超过1447个赞
知道小有建树答主
回答量:6294
采纳率:0%
帮助的人:1722万
展开全部
  下面是绘制的函数
  void Render()
  {
  static int rotation=0;
  GLshort vertexArray[9]={-25,-25,0, 25,-25,0, 0,25,0 };
  GLubyte colorArray[12]={255,0,0,0, 0,255,0,0, 0,0,255,0 };

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glLoadIdentity();

  glTranslatex(0,0,FixedFromInt(-10));
  glRotatex(FixedFromInt(rotation++),0,ONE,0);

  glEnableClientState(GL_VERTEX_ARRAY);
  glVertexPointer(3,GL_SHORT,0,vertexArray);

  glEnableClientState(GL_COLOR_ARRAY);
  glColorPointer(4,GL_UNSIGNED_BYTE,0,colorArray);

  glDrawArrays(GL_TRIANGLES,0,3);

  glDisableClientState(GL_VERTEX_ARRAY);
  glDisableClientState(GL_COLOR_ARRAY);

  eglSwapBuffers(glesDisplay,glesSurface);
  }

  其中vertexArray指定3个顶点的坐标值,
  glVertexPointer(3,GL_SHORT,0,vertexArray);完成三角形的绘制

  另外完整源代码如下:

  // hello.cpp : 定义应用程序的入口点。
  //

  #include "stdafx.h"
  #include "hello.h"
  #include <windows.h>
  #include <commctrl.h>
  #include <GLES/gl.h>
  #include <GLES/egl.h>

  #define MAX_LOADSTRING 100
  #define PRECISION 16
  #define ONE (1<<PRECISION)
  #define ZERO 0

  inline GLfixed FixedFromInt(int value){ return value<<PRECISION; }
  bool InitOGLES();
  void Render();
  void Clean();

  // 全局变量:
  HINSTANCE g_hInst; // 当前实例
  HWND g_hWndMenuBar; // 菜单栏句柄

  HWND hWnd;
  HDC hDc;

  EGLDisplay glesDisplay;
  EGLSurface glesSurface;
  EGLContext glesContext;

  TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
  TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名

  // 此代码模块中包含的函数的前向声明:
  ATOM MyRegisterClass(HINSTANCE, LPTSTR);
  BOOL InitInstance(HINSTANCE, int);
  LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);

  int WINAPI WinMain(HINSTANCE hInstance,
  HINSTANCE hPrevInstance,
  LPTSTR lpCmdLine,
  int nCmdShow)
  {
  MSG msg;

  // 执行应用程序初始化:
  if (!InitInstance(hInstance, nCmdShow))
  {
  return FALSE;
  }

  HACCEL hAccelTable;
  hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_HELLO));

  /* 主消息循环:
  while (GetMessage(&msg, NULL, 0, 0))
  {
  if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
  }
  }*/

  bool done=false;

  while(!done)
  {
  if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  {
  if(msg.message==WM_QUIT)
  {
  done=true;
  }
  else
  {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
  }
  }
  else
  {
  Render();
  }
  }
  Clean();

  return (int) msg.wParam;
  }

  //
  // 函数: MyRegisterClass()
  //
  // 目的: 注册窗口类。
  //
  // 注释:
  //
  ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
  {
  WNDCLASS wc;

  wc.style = CS_HREDRAW | CS_VREDRAW;
  wc.lpfnWndProc = WndProc;
  wc.cbClsExtra = 0;
  wc.cbWndExtra = 0;
  wc.hInstance = hInstance;
  wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_HELLO));
  wc.hCursor = 0;
  wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
  wc.lpszMenuName = 0;
  wc.lpszClassName = szWindowClass;

  return RegisterClass(&wc);
  }

  //
  // 函数: InitInstance(HINSTANCE, int)
  //
  // 目的: 保存实例句柄并创建主窗口
  //
  // 注释:
  //
  // 在此函数中,我们在全局变量中保存实例句柄并
  // 创建和显示主程序窗口。
  //
  BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  {
  g_hInst = hInstance; // 将实例句柄存储在全局变量中

  // 在应用程序初始化期间,应调用一次 SHInitExtraControls 以初始化
  // 所有设备特定控件,例如,CAPEDIT 和 SIPPREF。
  SHInitExtraControls();

  LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  LoadString(hInstance, IDC_HELLO, szWindowClass, MAX_LOADSTRING);

  //如果它已经在运行,则将焦点置于窗口上,然后退出
  hWnd = FindWindow(szWindowClass, szTitle);
  if (hWnd)
  {
  // 将焦点置于最前面的子窗口
  // “| 0x00000001”用于将所有附属窗口置于前台并
  // 激活这些窗口。
  SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001));
  return 0;
  }

  if (!MyRegisterClass(hInstance, szWindowClass))
  {
  return FALSE;
  }

  hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
  CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

  if (!hWnd)
  {
  return FALSE;
  }

  if(!InitOGLES())
  return false;

  // 使用 CW_USEDEFAULT 创建主窗口时,将不会考虑菜单栏的高度(如果创建了一个
  // 菜单栏)。因此,我们要在创建窗口后调整其大小
  // 如果菜单栏存在
  if (g_hWndMenuBar)
  {
  RECT rc;
  RECT rcMenuBar;

  GetWindowRect(hWnd, &rc);
  GetWindowRect(g_hWndMenuBar, &rcMenuBar);
  rc.bottom -= (rcMenuBar.bottom - rcMenuBar.top);

  MoveWindow(hWnd, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, FALSE);
  }

  ShowWindow(hWnd, nCmdShow);
  UpdateWindow(hWnd);

  return TRUE;
  }

  //
  // 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
  //
  // 目的: 处理主窗口的消息。
  //
  // WM_COMMAND - 处理应用程序菜单
  // WM_PAINT - 绘制主窗口
  // WM_DESTROY - 发送退出消息并返回
  //
  //
  LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  {
  int wmId, wmEvent;
  PAINTSTRUCT ps;
  HDC hdc;

  static SHACTIVATEINFO s_sai;

  switch (message)
  {
  case WM_COMMAND:
  wmId = LOWORD(wParam);
  wmEvent = HIWORD(wParam);
  // 分析菜单选择:
  switch (wmId)
  {
  case IDM_HELP_ABOUT:
  DialogBox(g_hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, About);
  break;
  case IDM_OK:
  SendMessage (hWnd, WM_CLOSE, 0, 0);
  break;
  default:
  return DefWindowProc(hWnd, message, wParam, lParam);
  }
  break;
  case WM_CREATE:
  SHMENUBARINFO mbi;

  memset(&mbi, 0, sizeof(SHMENUBARINFO));
  mbi.cbSize = sizeof(SHMENUBARINFO);
  mbi.hwndParent = hWnd;
  mbi.nToolBarId = IDR_MENU;
  mbi.hInstRes = g_hInst;

  if (!SHCreateMenuBar(&mbi))
  {
  g_hWndMenuBar = NULL;
  }
  else
  {
  g_hWndMenuBar = mbi.hwndMB;
  }

  // 初始化外壳程序激活信息结构
  memset(&s_sai, 0, sizeof (s_sai));
  s_sai.cbSize = sizeof (s_sai);
  break;
  case WM_PAINT:
  ValidateRect(hWnd,NULL);
  break;
  case WM_DESTROY:
  CommandBar_Destroy(g_hWndMenuBar);
  PostQuitMessage(0);
  break;
  case WM_ACTIVATE:
  // 向外壳程序通知我们的激活消息
  SHHandleWMActivate(hWnd, wParam, lParam, &s_sai, FALSE);
  break;
  case WM_SETTINGCHANGE:
  SHHandleWMSettingChange(hWnd, wParam, lParam, &s_sai);
  break;

  default:
  return DefWindowProc(hWnd, message, wParam, lParam);
  }
  return 0;
  }

  // “关于”框的消息处理程序。
  INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  {
  switch (message)
  {
  case WM_INITDIALOG:
  {
  // 创建一个“完成”按钮并调整其大小。
  SHINITDLGINFO shidi;
  shidi.dwMask = SHIDIM_FLAGS;
  shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN | SHIDIF_EMPTYMENU;
  shidi.hDlg = hDlg;
  SHInitDialog(&shidi);
  }
  return (INT_PTR)TRUE;

  case WM_COMMAND:
  if (LOWORD(wParam) == IDOK)
  {
  EndDialog(hDlg, LOWORD(wParam));
  return TRUE;
  }
  break;

  case WM_CLOSE:
  EndDialog(hDlg, message);
  return TRUE;

  }
  return (INT_PTR)FALSE;
  }

  bool InitOGLES()
  {
  EGLConfig configs[10];
  EGLint matchingConfigs;

  const EGLint configAttribs[]={
  EGL_RED_SIZE, 8,
  EGL_GREEN_SIZE, 8,
  EGL_BLUE_SIZE, 8,
  EGL_ALPHA_SIZE, EGL_DONT_CARE,
  EGL_DEPTH_SIZE, 16,
  EGL_STENCIL_SIZE,EGL_DONT_CARE,
  EGL_SURFACE_TYPE,EGL_WINDOW_BIT,
  EGL_NONE, EGL_NONE
  };

  hDc=GetWindowDC(hWnd);
  glesDisplay=eglGetDisplay(hDc);

  if(!eglInitialize(glesDisplay,NULL,NULL))
  return false;

  if(!eglChooseConfig(glesDisplay,configAttribs,&configs[0],10,&matchingConfigs))
  return false;

  if(matchingConfigs<1)
  return false;

  glesSurface=eglCreateWindowSurface(glesDisplay,configs[0],hWnd,configAttribs);
  if(!glesSurface)
  return false;

  glesContext=eglCreateContext(glesDisplay,configs[0],0,configAttribs);
  if(!glesContext)
  return false;

  eglMakeCurrent(glesDisplay,glesSurface,glesSurface,glesContext);

  glClearColorx(0,0,0,0);
  glShadeModel(GL_SMOOTH);

  RECT r;
  GetWindowRect(hWnd,&r);
  glViewport(r.left,r.top,r.right-r.left,r.bottom-r.top);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrthox(FixedFromInt(-50),FixedFromInt(50),
  FixedFromInt(-50),FixedFromInt(50),
  FixedFromInt(-50),FixedFromInt(50));
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

  return true;
  }

  void Render()
  {
  static int rotation=0;
  GLshort vertexArray[9]={-25,-25,0, 25,-25,0, 0,25,0 };
  GLubyte colorArray[12]={255,0,0,0, 0,255,0,0, 0,0,255,0 };

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glLoadIdentity();

  glTranslatex(0,0,FixedFromInt(-10));
  glRotatex(FixedFromInt(rotation++),0,ONE,0);

  glEnableClientState(GL_VERTEX_ARRAY);
  glVertexPointer(3,GL_SHORT,0,vertexArray);

  glEnableClientState(GL_COLOR_ARRAY);
  glColorPointer(4,GL_UNSIGNED_BYTE,0,colorArray);

  glDrawArrays(GL_TRIANGLES,0,3);

  glDisableClientState(GL_VERTEX_ARRAY);
  glDisableClientState(GL_COLOR_ARRAY);

  eglSwapBuffers(glesDisplay,glesSurface);
  }

  void Clean()
  {
  if(glesDisplay)
  {
  eglMakeCurrent(glesDisplay,NULL,NULL,NULL);
  if(glesContext)
  eglDestroyContext(glesDisplay,glesContext);
  if(glesSurface)
  eglDestroySurface(glesDisplay,glesSurface);
  eglTerminate(glesDisplay);
  }

  DestroyWindow(hWnd);
  UnregisterClass(szWindowClass,g_hInst);
  }
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式