android平台初始化opengles

需要用到ndk里面的app glue


首先需要包含下面这三个头文件:


#include<EGL/egl.h>
#include<GLES2/gl2.h>
#include<GLES2/gl2ext.h>




在nativeactivity的onStart事件里面 做如下初始化:


主要是用到egl的函数来初始化,关于egl函数的详细用法,请参考http://www.khronos.org/registry/egl/sdk/docs/man/xhtml/








android_app* mApplication;
int32_t mWidth, mHeight;


        EGLDisplay mDisplay;
        EGLSurface mSurface;
        EGLContext mContext;




EGLint lFormat, lNumConfigs, lErrorResult;
        EGLConfig lConfig;






//这里窗口像素格式rgb565
const EGLint lAttributes[] = {
            EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT,
            EGL_BLUE_SIZE, 5, EGL_GREEN_SIZE, 6, EGL_RED_SIZE, 5,
            EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
            EGL_NONE
        };


//先创建display
mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (mDisplay == EGL_NO_DISPLAY)
{
}


//利用display初始化opengl es
if (!eglInitialize(mDisplay, NULL, NULL))
{
}


//为display选择配置
if(!eglChooseConfig(mDisplay, lAttributes, &lConfig, 1,&lNumConfigs) || (lNumConfigs <= 0))
{
}
//获得display配置的属性
if (!eglGetConfigAttrib(mDisplay, lConfig,EGL_NATIVE_VISUAL_ID, &lFormat))
{
}


//根据display的属性,来设置本地窗口缓存的宽高和显示格式
ANativeWindow_setBuffersGeometry(mApplication->window, 0, 0,lFormat);




//根据display,display的配置,本地窗口,来创建surface
mSurface = eglCreateWindowSurface(mDisplay, lConfig,mApplication->window, NULL);
if (mSurface == EGL_NO_SURFACE)
{
}


//根据display,及其配置创建opengl es环境
mContext = eglCreateContext(mDisplay, lConfig, EGL_NO_CONTEXT,NULL);
if (mContext == EGL_NO_CONTEXT)
{
}


//绑定当前的display,surface,context
if(!eglMakeCurrent(mDisplay, mSurface, mSurface, mContext)
{
}


//得到宽高
if(!eglQuerySurface(mDisplay, mSurface, EGL_WIDTH, &mWidth)
         || !eglQuerySurface(mDisplay, mSurface, EGL_HEIGHT, &mHeight)
         || (mWidth <= 0) || (mHeight <= 0))
{
}




//到这里opengl es就初始化好了,就可以调用opengl es的函数绘制了。






//绘制完了之后交换缓存:

if (eglSwapBuffers(mDisplay, mSurface) != EGL_TRUE) 
{
}


























  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值