opengles 开发l流程

来源:互联网 发布:任子行网络审计 编辑:程序博客网 时间:2024/06/04 00:42
[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. {  
  2.     1:获取 Display  
  3.     2:初始化 egl  
  4.     3:选择 Config  
  5.     4:创建 Surface  
  6.     5:创建上下文对象  
  7.     6:查看 Display Surface Context 是否创建成功  
  8.     7:绘制  
  9.     8:销毁 OpenGLES  
  10. }  


[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <EGL/egl.h>  
  2. #include <gles2/gl2.h>  
  3. #include "freeImage/FreeImage.h"  
  4.   
  5. class WinApp {  
  6. public:  
  7.     WinApp(HINSTANCE hInstance);  
  8.     virtual~WinApp(void);  
  9.     virtual LRESULT onEvent(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);  
  10.     static LRESULT CALLBACK wndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);  
  11.     int main(int wWidth,int wHegith);  
  12.   
  13.   
  14.     bool configurationInit();  
  15.     void draw();//画  
  16.     void destroy();//销毁  
  17. private:  
  18.     HINSTANCE   _hInstance; //实例句柄  
  19.     HWND _hWnd;//窗口句柄  
  20. protected;  
  21.   
  22.     int _width; // 视口宽度  
  23.     int _height;// 视口高度  
  24.   
  25.     EGLDisplay _display;  
  26.     EGLContext _context;  
  27.     EGLSurface _surface;  
  28.     EGLConfig  _config;  
  29. };  



1.下载AMD的OpenGL ES2.0的模拟器,地址: http://www.opengles-book.com/ESEmulator.2009-04-28-v1.4.APRIL_2009_RELEASE.msi

2.下载OpenGL ES2.0实例代码,用《OpenGL ES2.0 Programming guide》里的例子代码,地址: http://www.opengles-book.com/OpenGL_ES_Programming_Guide_v1.0.2.zip.

3.下载Visual Studio, 会写点程序的都懂。

教程:

1. 解压上面下载的例子代码。

2.安装OpenGL ES2.0模拟器, 将安装目录下的AMD\OpenGL ES 2.0 Emulator v1.1\bin 以及AMD\OpenGL ES 2.0 Emulator v1.1\lib 里的 libEGL.dll 、 libGLESv2.dll 和 libEGL.lib 、libGLESv2.lib分别拷贝至VS的bin和lib文件夹下。

3.接着打开例子代码里的工程就可以了。

之后就可以通过修改例子来实现在windows上练习实践opengles2.0了。这样的好处是用c/c++实现编程,之后方便移植至Android、iOS等系统,再者还可以利用起强大的VS。


[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include "WinApp.h"    
  2.     
  3. bool WinApp::configurationInit(){    
  4.     //1:获取 Display    
  5.     _display = eglGetDisplay(EGL_DEFAULT_DISPLAY);    
  6.         
  7.     //2:初始化 egl    
  8.         EGLint  major;//返回主版本号    
  9.         EGLint  minor;//返回次版本号    
  10.         eglInitialize(_display, &major, &minor);    
  11.         
  12.     
  13.     //3:选择 Config    
  14.     const EGLint attribs[] ={    
  15.       //属性是一对一的初始化(类似与key->value)    
  16.       EGL_SURFACE_TYPE, EGL_WINDOW_BIT,//把数据画到窗口上    
  17.       EGL_BLUE_SIZE, 8,//R 占8个比特位    
  18.       EGL_GREEN_SIZE, 8,//G 占8个比特位    
  19.       EGL_RED_SIZE, 8, //B 占8个比特位    
  20.       EGL_DEPTH_SIZE,24,//深度值 占24个比特位  画图的深度值 (前后顺序的层)    
  21.       EGL_NONE  //这个机构结束了    
  22.     };    
  23.     EGLint  format(0);    
  24.     EGLint  numConfigs(0);    
  25.         
  26.     //让EGL为你选择一个配置    
  27.     eglChooseConfig(_display, attribs, &_config, 1, &numConfigs);    
  28.     //可以查询某个配置的某个属性    
  29.     eglGetConfigAttrib(_display, _config, EGL_NATIVE_VISUAL_ID, &format);    
  30.     
  31.     
  32.     4:创建 Surface    
  33.     _surface = eglCreateWindowSurface(_display, _config, _hWnd, NULL);    
  34.     
  35.     //5:创建上下文对象    
  36.     EGLint attr[] ={ EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE, EGL_NONE };    
  37.     //创建上下文对象    
  38.     _context = eglCreateContext(_display, _config, EGL_NO_CONTEXT, attr);    
  39.     
  40.     //6:查看 Display Surface Context 是否创建成功    
  41.     if (eglMakeCurrent(_display, _surface, _surface, _context) == EGL_FALSE){    
  42.         return false;    
  43.     }    
  44.     
  45.     //查询Suface的属性 获取Suface中的宽高    
  46.     eglQuerySurface(_display, _surface, EGL_WIDTH,  &_width);    
  47.     eglQuerySurface(_display, _surface, EGL_HEIGHT, &_height);    
  48.         
  49.     return true;    
  50. }    
  51.     
  52. void WinApp::draw(){    
  53.     
  54.     //glDrawArrays(GL_TRIANGLE_STRIP,0,0);    
  55.     glClearColor(0,0,1,1);//刷屏    
  56.     
  57.     //清除颜色GL_COLOR_BUFFER_BIT 和 清除深度值GL_DEPTH_BUFFER_BIT    
  58.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    
  59.     glViewport(0,0,_width,_height);//绘制区域    
  60.     
  61.     //绘制    
  62.     eglSwapBuffers(_display,_surface);    
  63. }    
  64.     
  65. void WinApp::destroy(){    
  66.     if (_display != EGL_NO_DISPLAY){//当前的Display 不等于null    
  67.             //清楚绑定的 Surface Context     
  68.             eglMakeCurrent(_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);    
  69.             if (_context != EGL_NO_CONTEXT){//不等于空Context    
  70.                //销毁上下文    
  71.                 eglDestroyContext(_display, _context);    
  72.             }    
  73.     
  74.             if (_surface != EGL_NO_SURFACE){//不等于空Surface    
  75.                 //销毁Surface    
  76.                 eglDestroySurface(_display, _surface);    
  77.             }    
  78.             //终止Dispay    
  79.             eglTerminate(_display);    
  80.         }    
  81.         //把 Display Context Surface 设置成初始化    
  82.         _display    =   EGL_NO_DISPLAY;    
  83.         _context    =   EGL_NO_CONTEXT;    
  84.         _surface    =   EGL_NO_SURFACE;    
  85. }    
  86.     
  87. //---------------------窗口----------------------    
  88.     
  89. WinApp::WinApp(HINSTANCE hInstance):_hInstance(hInstance){    
  90.     WNDCLASSEX  winClass;    
  91.     winClass.lpszClassName  =   _T("CELLWinApp");//指向类名称的指针(窗口名字)    
  92.     winClass.cbSize         =   sizeof(winClass);//WNDCLASSEX 的大小    
  93.     winClass.style          =   CS_HREDRAW | CS_VREDRAW | CS_OWNDC;//窗口风格    
  94.     winClass.lpfnWndProc    =   wndProc;//窗口处理函数的指针    
  95.     winClass.hInstance      =   hInstance;//本模块的实例句柄    
  96.     winClass.hIcon          =   0;//图标    
  97.     winClass.hIconSm        =   0;//和窗口类关联的小图标,如果该值为NULL,则把hIcon中的图标转换成大小合适的小图标    
  98.     winClass.hCursor        =   LoadCursor(hInstance, IDC_ARROW);//鼠标句柄    
  99.     winClass.hbrBackground  =   (HBRUSH)GetStockObject(BLACK_BRUSH);//背景画刷的句柄。    
  100.     winClass.lpszMenuName   =   NULL;//指向菜单的指针    
  101.     winClass.cbClsExtra     =   0;    
  102.     winClass.cbWndExtra     =   0;    
  103.     //1:注册窗口    
  104.     //随后在调用Createwindow函数和CreatewindowEx函数中使用的窗口注册一个窗口类。    
  105.     RegisterClassEx(&winClass);    
  106. }    
  107.     
  108. LRESULT CALLBACK WinApp::wndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam){    
  109.     //获取窗口属性    
  110.     WinApp*  pThis   =   (WinApp*)GetWindowLong(hWnd,GWL_USERDATA);    
  111.     if (pThis) {    
  112.         return  pThis->onEvent(hWnd,msg,wParam,lParam);    
  113.     }    
  114.     if (WM_CREATE == msg){ //窗口创建完成时发来的消息    
  115.         
  116.         CREATESTRUCT*   pCreate =   (CREATESTRUCT*)lParam;    
  117.         //获取当前对象(CreateWindowEx函数中最后一个参数)    
  118.         DWORD_PTR   pCreateParams = (DWORD_PTR)pCreate->lpCreateParams;    
  119.     
  120.         /**设置窗口属性  
  121.         * @param HWND hWnd 窗口句柄  
  122.         * @param int nlndex  
  123.         * @param LONG dwNewLong  
  124.         */    
  125.         SetWindowLong(hWnd,GWL_USERDATA,pCreateParams);    
  126.     }    
  127.     return  DefWindowProc( hWnd, msg, wParam, lParam );    
  128. }    
  129.     
  130. //事件函数    
  131. LRESULT WinApp::onEvent(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam){    
  132.     switch (msg){ //处理得到的消息    
  133.     case WM_CLOSE://处理窗口关闭时的消息    
  134.     case WM_DESTROY://销毁    
  135.         {    
  136.             ::PostQuitMessage(0);    
  137.         }    
  138.         break;    
  139.     case WM_LBUTTONDOWN: //鼠标左键被按下的消息    
  140.     
  141.         break;    
  142.     case WM_RBUTTONDOWN://鼠标右键被按下的消息    
  143.     
  144.         break;    
  145.     case WM_MOUSEMOVE://鼠标移动    
  146.         break;    
  147.     case WM_KEYDOWN://监听键盘按键    
  148.         switch (wParam){    
  149.         case VK_LEFT:    
  150.             break;    
  151.         default:    
  152.             break;    
  153.         }    
  154.         break;    
  155.     default:    
  156.         return  DefWindowProc( hWnd, msg, wParam, lParam );     
  157.     }    
  158.     
  159.     return  S_OK;    
  160. }    
  161.     
  162. WinApp::~WinApp(void){    
  163.     //注销一个窗口类,类释放所需的内存    
  164.     UnregisterClass(_T("CELLWinApp"),_hInstance);    
  165.     destroy();    
  166. }    
  167.     
  168. int WinApp::main(int wWidth,int wHegith){    
  169.     //2:创建窗口    
  170.     //创建一个具有扩展风格的层叠式窗口、弹出式窗口或子窗口,其他与CreateWindow函数相同。关于创建窗口和其他参数的内容,请参看CreateWindow。    
  171.     
  172.     
  173.     /**创建一个具有扩展风格的层叠式窗口、弹出式窗口或子窗口  
  174.     * @param    DWORD dwExStyle 窗口的扩展风格  
  175.     * @param    LPCWSTR lpClassName 指向注册类名的指针  
  176.     * @param    LPCWSTR lpWindowName 指向窗口名称的指针  
  177.     * @param    DWORD dwStyle 窗口风格  
  178.     * @param    int X 窗口的水平位置  
  179.     * @param    int Y 窗口的垂直位置  
  180.     * @param    int nWidth 窗口的宽度  
  181.     * @param    int nHeight 窗口的高度  
  182.     * @param    HWND hWndParent 父窗口的句柄  
  183.     * @param    HMENU hMenu 菜单的句柄或是子窗口的标识符  
  184.     * @param    HINSTANCE hInstance 应用程序实例的句柄  
  185.     * @param    LPVOID lpParam 指向窗口的创建数据  
  186.     */    
  187.     _hWnd   =   CreateWindowEx( NULL, _T("CELLWinApp"), _T("CELLWinApp"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, wWidth, wHegith, NULL,  NULL, _hInstance,this );    
  188.     if (_hWnd == 0) {    
  189.         return  -1;    
  190.     }    
  191.     //3:更新显示窗口    
  192.     ShowWindow(_hWnd,SW_SHOW);//SW_MAXIMIZE 扩大窗口,SW_SHOW    
  193.     
  194.     configurationInit();    
  195.     
  196.     //4:消息循环    
  197.     MSG msg =   {0};    
  198.     while(msg.message != WM_QUIT) { //消息不等于退出    
  199.         //毁坏,结束    
  200.         if (msg.message == WM_DESTROY ||  msg.message == WM_CLOSE){    
  201.             break;    
  202.         }    
  203.     
  204.         //有消息返回真,无消息返回假    
  205.         if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ){//有消息:处理消息    
  206.             TranslateMessage( &msg );//消息处理    
  207.             DispatchMessage( &msg );//分发给那个窗口(程序中有好多窗口,那个点击了,就分发给那个)    
  208.         }else{//无消息:则进行渲染绘制或睡眠    
  209.             // Sleep(1);    
  210.             draw();    
  211.         }    
  212.     }    
  213.         
  214.     return 0;    
  215. }  


画出一个蓝色的界面就可以了




0 0