#include#include #include "ximage.h"#pragma comment(lib, "libEGL.lib")#pragma comment(lib, "libGLESv1_CM.lib")#define glF(x) ((GLfixed)((x)*(1<<16)))#define GL_F GL_FIXEDtypedef GLfixed GLf;#define PI 3.141592653class COpenGLES{public: COpenGLES(void); ~COpenGLES(void); EGLConfig m_EGLXConfig; EGLContext m_EGLXContext; EGLSurface m_EGLXSurface; EGLDisplay m_EGLXDisplay; EGLint m_EGLXNumOfConfigs; EGLint max_num_config; NativeDisplayType g_dpy; HWND m_hwnd; BOOL CreateEGL(HWND hwnd); void DeleteEGL(); void glPerspectivef(GLfloat fov, GLfloat aspect, GLfloat near_val, GLfloat far_val); void SetProjectToOrtho(void); void SetProjectToFrustum(); void EGLFlush(); bool Init(HWND m_hWnd); void InitGLES(); void CloseGLES();};
cpp
COpenGLES::COpenGLES(void){}COpenGLES::~COpenGLES(void){}BOOL COpenGLES::CreateEGL(HWND hwnd){ m_hwnd = hwnd; EGLint const attrib_list[] = { EGL_RED_SIZE, 5, EGL_GREEN_SIZE, 6, EGL_BLUE_SIZE, 5, EGL_ALPHA_SIZE, 0, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT, EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_SAMPLE_BUFFERS, GL_FALSE, EGL_NONE }; EGLint ai32ContextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 1, EGL_NONE }; EGLConfig *configs = NULL; g_dpy = ::GetDC(hwnd); EGLint major, minor, num_config; m_EGLXDisplay = eglGetDisplay( g_dpy ); if ( EGL_NO_DISPLAY == m_EGLXDisplay ) { printf( "eglGetDisplay() failed (error 0x%x)\n", eglGetError() ); return 0; } if ( EGL_FALSE == eglInitialize( m_EGLXDisplay, &major, &minor ) ) { printf( "eglInitialize() failed (error 0x%x)\n", eglGetError() ); return 0; } if ( EGL_FALSE == eglGetConfigs(m_EGLXDisplay, NULL, 0, &max_num_config) ) { return 0; } if(max_num_config <= 0) { return 0; } configs = (EGLConfig *)malloc( sizeof( EGLConfig) * max_num_config ); if ( NULL == configs ) { return 0; } printf("max_num_config=%d\n",max_num_config);//23 if ( EGL_FALSE == eglChooseConfig( m_EGLXDisplay, attrib_list, configs,max_num_config, &m_EGLXNumOfConfigs ) ) { printf( "eglChooseConfig() failed (error 0x%x)\n", eglGetError() ); return 0; } printf("m_EGLXNumOfConfigs=%d\n",m_EGLXNumOfConfigs);//8 if ( 0 == m_EGLXNumOfConfigs ) { printf( "eglChooseConfig() was unable to find a suitable config\n" ); return 0; } for (int i=0; i
使用:
COpenGLES gl;gl.Init(m_hWnd);gl.SetProjectToOrtho();
在函数CreateEGL中
m_EGLXSurface = eglCreateWindowSurface( m_EGLXDisplay, m_EGLXConfig, hwnd, 0 );
第三个参数,可以传入对话框的句柄,这样,opengles与gdi使用同一个窗口。
如果传入null,GDI无法在窗口上绘图。这样如果要在屏幕上输出字符比较麻烦。(这种情况,我先将字符转为纹理,再输出)。