首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

WinCE OpenGL绘制立方体和纹理贴图 01

WinCE OpenGL绘制立方体和纹理贴图 01

 最近工作之余,开始学习OpenGL,  想到WINCE也是支持OpenGL的,只不过是嵌入式的OpenGL ES.于是尝试写了一个WINCE下的OpenGL测试程序,实现了绘制立方体和纹理。效果图如下:

  需要注意的是,WINCE系统上开发OpenGL程序需具备以下条件:
  1. 处理器的支持,嵌入式处理器需支持3D加速渲染。
  2. WINCE内核的支持,定制内核时需添加OpenGL ES相关组件。
  以下是具体的参考代码:
  view plain /******************************************************************** filename:   WinceOpenGLDemo.cpp created:    2011-01-05 author:     firehood purpose:    利用OpenGL ES实现了绘制立方体和纹理效果*********************************************************************/
  // WinceOpenGLDemo.cpp : 定义应用程序的入口点。
  //
  #include "stdafx.h" #include "WinceOpenGLDemo.h" #include <windows.h> #include <commctrl.h> #include "ImgLoader.h" // OpenGL ES Includes #include <GLES/gl.h> #include <GLES/glext.h> #include <EGL/egl.h> #include <EGL/eglext.h>
  // OpenGL lib #pragma comment(lib, "OpenGlLib\\libGLESv1_CM.lib")
  #pragma comment(lib, "OpenGlLib\\libEGL.lib")
  // 全局变量:HINSTANCE   g_hInst;            // 当前实例TCHAR       szAppName[] = L"OpenGLES";        /*The application name and the window caption*/ CImgLoader  g_Image;// OpenGL variables EGLDisplay glesDisplay;  // EGL display EGLSurface glesSurface;  // EGL rendering surface EGLContext glesContext;  // EGL rendering context
  GLuint texture[6] = {0};
  // 立方体定点坐标GLshort vertices[] = { -1,-1,1,1,-1,1,1,1,1,-1,1,1,
  -1,-1,-1,-1,1,-1,1,1,-1,1,-1,-1,
  -1,1,-1,-1,1,1,1,1,1,1,1,-1,
  -1,-1,-1,1,-1,-1,1,-1,1,-1,-1,1,
  1,-1,-1,1,1,-1,1,1,1,1,-1,1,
  -1,-1,-1,-1,-1,1,-1,1,1,-1,1,-1 };
  // 各个面纹理坐标GLshort texCoords[] = { 0,0,1,0,1,1,0,1,};
  // 三角形索引数据GLbyte indices1[] = { 0,1,3,2,0,0,0,0,0,0,0,0 };GLbyte indices2[] = { 0,0,0,0,4,5,7,6,0,0,0,0,0,0,0,0 };GLbyte indices3[] = { 0,0,0,0,8,9,11,10,0,0,0,0,0,0,0,0 };GLbyte indices4[] = { 0,0,0,0,12,13,15,14,0,0,0,0,0,0,0,0 };GLbyte indices5[] = { 0,0,0,0,16,17,19,18,0,0,0,0 };GLbyte indices6[] = { 0,0,0,0,20,21,23,22 };
  // 此代码模块中包含的函数的前向声明:ATOM            MyRegisterClass(HINSTANCE, LPTSTR);BOOL            InitInstance(HINSTANCE, int);LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
  BOOL InitOGLES(HWND hWnd);void CreateSurface();BOOL LoadTexture(LPCTSTR lpFileName,GLuint *id);void Render();void Clean();
  int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR    lpCmdLine,int       nCmdShow)
  { MSG msg;
  // 执行应用程序初始化:if (!InitInstance(hInstance, nCmdShow))
  { return FALSE;}
  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();};}
  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_WINCEOPENGLDEMO));wc.hCursor       = 0;wc.hbrBackground = (HBRUSH) GetStockObject(NULL_BRUSH);wc.lpszMenuName  = 0;wc.lpszClassName = szWindowClass;
  return RegisterClass(&wc);}
  // //   函数: InitInstance(HINSTANCE, int)
  // //   目的: 保存实例句柄并创建主窗口// //   注释:// //        在此函数中,我们在全局变量中保存实例句柄并//        创建和显示主程序窗口。
  // BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  { HWND hWnd;
  g_hInst = hInstance; // 将实例句柄存储在全局变量中
  if (!MyRegisterClass(hInstance, szAppName))
  { return FALSE;}
  hWnd = CreateWindow(szAppName,WS_VISIBLE,0,::GetSystemMetrics(SM_CXSCREEN),::GetSystemMetrics(SM_CYSCREEN),NULL,hInstance,NULL);
  if (!hWnd)
  { return FALSE;}
  if(!InitOGLES(hWnd))
  { printf("InitOGLES failed\n");return FALSE;} CreateSurface();
  ShowWindow(hWnd, SW_SHOW);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)
  { PAINTSTRUCT ps;HDC hdc;
  switch (message)
  { case WM_CREATE:break;case WM_PAINT:hdc = BeginPaint(hWnd, &ps);
  // TODO: 在此添加任意绘图代码……
  EndPaint(hWnd, &ps);break;case WM_DESTROY:{ Clean();PostQuitMessage(0);} break;
  default:return DefWindowProc(hWnd, message, wParam, lParam);} return 0;}
  BOOL InitOGLES(HWND hWnd)
  { EGLint matchingConfigs;EGLint majorVersion = 0;EGLint minorVersion = 0;
  glesDisplay = eglGetDisplay(GetDC(hWnd));  //Ask for an available display if( glesDisplay == EGL_NO_DISPLAY || eglGetError() != EGL_SUCCESS )
  return FALSE;
  EGLConfig *configs_list;EGLint     num_configs;// Display initialization (we don't care about the OGLES version numbers)
  if( eglInitialize( glesDisplay, &majorVersion, &minorVersion) == EGL_FALSE)
  { printf("eglInitialize failed, eglGetError = 0x%04x\n",eglGetError());return FALSE;} // find out how many configurations are supported if ( eglGetConfigs( glesDisplay, NULL, 0, &num_configs)==EGL_FALSE || eglGetError() != EGL_SUCCESS )
  return FALSE;configs_list = (EGLConfig*) malloc(num_configs * sizeof(EGLConfig));if (configs_list == NULL)
  return FALSE;// Get Configurations if( eglGetConfigs( glesDisplay, configs_list, num_configs, &num_configs)== EGL_FALSE || eglGetError() != EGL_SUCCESS )
  return FALSE;// Obtain the first configuration with a depth buffer of 16 bits EGLint attrs[] = { EGL_RED_SIZE,      5,EGL_GREEN_SIZE,    6,EGL_BLUE_SIZE,     5,EGL_DEPTH_SIZE,   16,EGL_NONE };if (!eglChooseConfig(glesDisplay, attrs, configs_list, num_configs, &matchingConfigs))
  { return eglGetError();} // If there isn't any configuration enough good if (matchingConfigs < 1)
  return FALSE;/*eglCreateWindowSurface creates an onscreen EGLSurface and returns a handle  to it. Any EGL rendering context created with a compatible EGLConfig can be used to render into this surface.*/ glesSurface = eglCreateWindowSurface(glesDisplay, configs_list[0], hWnd, 0);if(!glesSurface)
  return FALSE;
  // Let's create our rendering context glesContext=eglCreateContext(glesDisplay, configs_list[0], 0, 0);if(!glesContext)
  return FALSE;//Now we will activate the context for rendering eglMakeCurrent(glesDisplay, glesSurface, glesSurface, glesContext);
  /*Remember: because we are programming for a mobile device, we cant use any of the OpenGL ES functions that finish in 'f', we must use the fixed point version (they finish in 'x'*/ glClearColorx(0, 0, 0, 0);glShadeModel(GL_SMOOTH);
  RECT rc;GetWindowRect(hWnd, &rc);UINT width = rc.right - rc.left;UINT height = rc.bottom - rc.top;// 设置OpenGL场景的大小glViewport(rc.left, rc.top, width, height);
  // 设置投影矩阵glMatrixMode(GL_PROJECTION);glLoadIdentity();
  // 投影变换(透视投影)
  float ratio = (float) width / height;glFrustumf(-ratio, ratio, -1, 1, 2, 10);//glOrthox(FixedFromInt(-50),FixedFromInt(50), FixedFromInt(-50), FixedFromInt(50), FixedFromInt(-50), FixedFromInt(50));
  // 选择模型观察矩阵glMatrixMode(GL_MODELVIEW);// 重置模型观察矩阵glLoadIdentity();
  return TRUE;}
  void CreateSurface()
  { glDisable(GL_DITHER);
  // 告诉系统对透视进行修正glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);// 黑色背景glClearColor(0, 0, 0, 0);
  // 启用阴影平滑glShadeModel(GL_SMOOTH);
  // 设置深度缓存glClearDepthf(1.0f);// 启用深度测试glEnable(GL_DEPTH_TEST);// 所作深度测试的类型glDepthFunc(GL_LEQUAL);


返回列表