OpenGL version
iOS Systems support OpenGL ES1.0, ES2.0, and ES3.0 3 versions by default, not a simple version upgrade, or even a completely different design concept, before developing an OpenGL project, you need to choose the right version for your business needs. There are many introductions in this respect and no longer commence. When learning OpenGL code, you also need to know which version it corresponds to, and the execution of ES2 code in ES1 does not see any effect, you can specify the ES version number when initializing Eaglcontext
_eaglcontext = [[Eaglcontext alloc] initwithapi:keaglrenderingapiopengles2];
OpenGL coordinate system is different from the Uikit coordinate system, in fact, it is this, ( -1,-1) is the lower left corner of the screen, () is the upper right corner of the screen
Now we start by creating the project
We can select the game template directly when creating the project, so the system will automatically configure the environment, but there will be a lot of extra code.
To facilitate learning, we chose to create a single view Application project.
The Opengles framework is introduced after the project is created.
Apple offers a range of APIs that simplify the use of OpenGL, which is included in the glkit.framework.
In order to better understand the use of OpenGL, we do not apply Glkit-related APIs here, and there may be related introductions later.
The next step is to start using OpenGL, a new implementation of the class, inherited from UIView.
Import the corresponding version of the header file
#import <OpenGLES/ES2/gl.h> #import <OpenGLES/ES2/glext.h>
Setting properties
1234567891011121314 |
@
interface
MTGLESViewController (){
EAGLContext *_context;
CAEAGLLayer *_EALayer;
GLuint _colorBufferRender;
GLuint _frameBuffer;
GLuint _glProgram;
GLuint _positionSlot;
GLuint _textureSlot;
GLuint _textureCoordsSlot;
GLuint _textureID;
CGRect _frameCAEAGLLayer;
}
|
In an iOS application, each thread maintains a current context. When your application uses OpenGL ES calls, the context of the thread is changed by that call.
To set the current context, you can call the Eaglcontext class by calling the Setcurrentcontext: method.
[Eaglcontext Setcurrentcontext:mycontext];
Your application can also get the current context of a thread through the CurrentContext method of the Eaglcontext class.
When your application sets a new context, EAGL releases the previous context and gets the new context.
When your application creates and initializes Eaglcontext objects, it is possible to decide which version of OpenGL ES will be supported. When creating the OpenGL ES 2.0 context, your application can initialize as follows:
_eaglcontext = [[Eaglcontext alloc] initwithapi:keaglrenderingapiopengles2]; [Eaglcontext Setcurrentcontext:_eaglcontext];
After initialization, we need to do some processing on the layer and set some properties to make it possible to use OpenGL.
_eagllayer = (caeagllayer*) Self.layer;
_eagllayer.frame = Self.frame;
_eagllayer.opaque = Yes;_eagllayer.drawableproperties = [Nsdictionary dictionarywithobjectsandkeys:[nsnumber Numberwithbool:yes],keagldrawablepropertyretainedbacking,
Keaglcolorformatrgba8,keagldrawablepropertycolorformat, nil];
After initializing the layer, we need to initialize Renderbuffer and framebuffer
Glgenrenderbuffers (1, &_colorbufferrender); Glbindrenderbuffer (Gl_renderbuffer, _colorbufferrender); [_context Renderbufferstorage:gl_renderbuffer Fromdrawable:_ealayer]; Glgenframebuffers (1, &_framebuffer); Glbindframebuffer (Gl_framebuffer, _framebuffer); Glframebufferrenderbuffer (Gl_framebuffer, gl_color_attachment0, Gl_renderbuffer, _ Colorbufferrender);
Now try to see if we can use OpenGL properly.
Glclearcolor (0.5f, 0.5f, 0.5f, 1.0f);
Glclear (Gl_color_buffer_bit);
[_eaglcontext Presentrenderbuffer:gl_renderbuffer];
The background color of the screen is set to gray, proving that we have successfully completed the first key point in mastering OpenGL.
iOS-----opengl--opengl ES iOS Getting Started---> Building openGL Environment