这样的博客数不胜数了,我也是这样学习过来的,记录一下自己的学习经历
第一步: 在Xcode创建项目并创建一个简单的类为OpenGLView导入opengels/es2/gl.h , 接下来创建CAEAGLLayer的对象。

image.png

image2.png
第二步:EAGLContext的对象,是opengles渲染必不可少的。EAGLContext管理所有通过OpenGL进行draw的信息。

image3.png
第三步:设置帧缓冲区和渲染缓冲区,FrameBuffer and RenderBuffer。apple不允许OpenGL直接渲染在屏幕上,我们需要把它放进输出的颜色缓冲,然后询问EAGL 去把缓冲对象展现到屏幕上。

image4.png

image5.png
第四步:创建program,这里就用到了之前提到的GLSL语言,后续会更新关于它的语法等。这里会写到顶点着色器和片元着色器,就是glsl语言,代码见下图。

shader文本
关于program,第一步就是加载着色器,编译,然后通过shader去创建program,固定着色器,链接着色器并检查program状态,接下来就是使用program啦,获取着色器程序中,指定为attribute类型变量的id。

image6.png

image7.png

image8.png
加载编译shader代码:

loadShader.png
第五步:Render
在render里设置清屏颜色,然后清屏。设置视口viewport,然后调用glVertexAttribPointer传入顶点数据,因为默认情况下所有顶点着色器的属性(Attribute)变量都是关闭的,所以这里调用glEnableVertexAttribArray启用指定属性。然后通过 glDrawArrays渲染三角形。最后调用EAGLContext的presentRenderbuffer将渲染结果提交给上下文中。

render.png
记住opengl的流程,记住opengl的状态!
本人正在自学,本文相当于记录,如有错误,请指出,谢谢!