码迷,mamicode.com
首页 > 其他好文 > 详细

GLES1.x改动到GLES2.x要注意的点(一)

时间:2015-08-17 13:36:45      阅读:1016      评论:0      收藏:0      [点我收藏+]

标签:

GLES1.下支持的是固定管线,GLES2.x使用的是可编程管线,因此有一点点区别。

包含的头文件不同 .

从初始化过程一路讲下去。createDisplay->chooseConfigure->createSurface->setupContent

调用eglChooseConfig时,要在传入的参数中显示的指明RenderType为openGL ES的版本比如:

    const EGLint configurationAttributes[] =
    {
        EGL_SURFACE_TYPE,        EGL_WINDOW_BIT,
        EGL_RENDERABLE_TYPE,    EGL_OPENGL_ES_BIT,
        EGL_NONE
    };

或者

    const EGLint configurationAttributes[] =
    {
        EGL_SURFACE_TYPE,        EGL_WINDOW_BIT,
        EGL_RENDERABLE_TYPE,    EGL_OPENGL_ES2_BIT,
        EGL_NONE
    };

同时在SetupContext的时候,调用eglCreateContext时要声明EGL_CONTEXT_CLIENT_VERSION 为2,GLES1.x可以直接传NULL即可。

    EGLint contextAttributes[] = 
    {
        EGL_CONTEXT_CLIENT_VERSION, 2, 
        EGL_NONE
    };

    // Create the context with the context attributes supplied
    eglContext = eglCreateContext(eglDisplay, eglConfig, NULL, contextAttributes);

调用了之后 调用eglBindAPI(EGL_OPENGL_ES_API);

对比固定管线,可编程管线当然会多一个shader 的create,compile,然后link到program上。(也就是说program包括vertex,fragment两个shader)。

在渲染调用中,比固定管线要多的地方是

调用glUserProgram激活shader program。

然后传递矩阵值等。

这里需要注意的时

GLES2.x使用的glEnableVertexAttribArray 指明user-defined vertex array,然后调用glVertexAttribPointer,绑定数据到顶点的分量上。注意这些分量都是自定义的,可以传递自己想要的数据,确定好顺序即可。

注意必须要有的一步:glBindAttribLocation(m_uiProgramObject, VERTEX_ARRAY, "myVertex");用来分量与shader中的变量名字进行关联。以上的操作才能正确。

整体来说要用

glBindAttribLocation //声明Program中的attribute对应的序号
glEnableVertexAttribArray//启用这个序号
glVertexAttribPointer//将序号与CPU中的内存地址绑定,或者与VBO的下标偏移绑定

 

GLES1.x使用的glEnableClientState,激活vertex array,使用glVertexPointer来指定顶点分量。注意这些分量都是预定义的,不要绑错了就行了。

当然在推出的时候,要注意shader与program的析构。

 

如果对应的是ios的版本话要替换成ios的EAGL的初始化函数。

不同的是,相同函数,GLES1.x大体上都带_OES后缀,而GLES2.x 不带。

///////////////////////////////////////////////////////////////////////////////////////////////////

基础的光照与转换

/////////////////////////////////////////////////////////////////////////////////////////////////

由于使用了可编程管线之后,光照,材质,变化的关系由shader自己去处理,因此这里就需要自己处理这些关系了。因此也有了简化的可能性。

在固定管线过程中:

如果定义光照需要:

    // Enables lighting and light 0
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);

    /*
        Specifies the light direction.
        If the 4th component is 0, it‘s a parallel light (the case here).
        If the 4th component is not 0, it‘s a point light.
    */
    float aLightPosition[] = {0.0f,0.0f,1.0f,0.0f};

    /*
        Assigns the light direction to the light number 0.
        This function allows you to set also the ambiant, diffuse,
        specular, emission colors of the light as well as attenuation parameters.
        We keep the other parameters to their default value in this demo.
    */
    glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, aLightPosition);

启用了light0,并设置了light0的光照方向

对应的GLES2.x的shader可以是

//In vertex
mediump vec3 transNormal = myModelViewIT * myNormal;
varDot = max( dot(transNormal, myLightDirection), 0.0 );
//In fragment
gl_FragColor.rgb = texture2D(sampler2d,varCoord).rgb * varDot

/////////////////////////////////////////////////////////////////////////////////////

颜色混合等操作

/////////////////////////////////////////////////////////////////////////////////////

在介绍这个内容的时候 要引用下面这个网站,基本上用可视的方式表明了固定管线支持的所有的颜色混合内容。

http://www.andersriggelsen.dk/glblendfunc.php

在可编程管线中基本上没什么区别,都是调用

glEnable(GL_Blend)

glBlendFunc(...,...)来设置前后贴图的颜色混合关系。

需要注意的是,如果glBlendFunc用的是GL_ONE,GL_ZERO,效果等同于glDisable(GL_Blend),但是没有后者性能好。

大面积的alpha blend通常会导致像素填充的问题,这个要注意一下。有很多工具用来优化这个。

///////////////////////////////////////////////////////////////////////////////////////////

alpha Test

/////////////////////////////////////////////////////////////////////////////////////////

 

GLES1.x改动到GLES2.x要注意的点(一)

标签:

原文地址:http://www.cnblogs.com/peterfenng/p/4736149.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!