1. Decide which version of OpenGL ES you want to support. Currently, OpenGL ES contains 1.1 and 2.0 two versions, with IPhone 3g+ and ipad starting to support OpenGL ES2.0. And the difference between the two versions is very large, not only in the programming mind, the gap between the API is also very large. Therefore, if you want to develop 3D programs or games using OpenGL ES, you will first decide which version to use, or two versions to support. OpenGL ES defines macros that represent different versions:
enum
{
keaglrenderingapiopengles1 = 1, // 1.1 Edition
keaglrenderingapiopengles2 = 2 //2.0 version
}typedef Nsuinteger Eaglrenderingapi;
As an example of the iphone code, you can determine the OpenGL ES version supported by the user device in the following ways, if 2.0 is supported, 2.0 is used for rendering, and if only 1.1is supported, it is rendered using version 1.1 :
Eaglrenderingapi API = kEAGLRenderingAPIOpenGLES2; Default priority to use version 2.0
M_context = [[Eaglcontext alloc] initwithapi:api]; Initializing Eaglcontext with version 2.0
if (!m_context) {//Initialize with version 2.0 Eaglcontext failed
API = KEAGLRenderingAPIOpenGLES1; Set the API to1.1Version
M_context = [[Eaglcontext alloc] initwithapi:api];//use1.1Version of Initialization Eaglcontext
}
if (!m_context | |![ Eaglcontext Setcurrentcontext:m_context]) {//1.1Version initialization fails, the memory is freed
[Self release];
return nil;
}
if (API = = kEAGLRenderingAPIOpenGLES1) {
Use1.1Version to start rendering
}
else {
Start rendering with version 2.0
}
2. Do you want your OpenGL ES program to run across the mobile platform? Android, IPhone, Windows Phone system is the most mainstream mobile phone system, how can we write out the program can be run across platforms? The good news is that all three platforms support OpenGL ES and support the C + + language. Perhaps you understand that the method is to encapsulate OpenGL ES through C + +, minimizing the coupling code for a particular platform. This allows us to use the encapsulated OpenGL ES engine on different mobile platforms, and only need to modify the code of the specific platform to connect OpenGL es.
3. Getting Started common code parsing
3.1 Creating a render buffer
Gluint M_renderbuffer; //Create a render buffer object
Glgenrenderbuffers (1, &m_renderbuffer);//Create a render buffer object
Glbindrenderbuffer (Gl_renderbuffer, m_renderbuffer);//Bind the render buffer object to the pipeline
3.2 Creating a Frame buffer
Gluint M_framebuffer; //Create a Frame buffer object
Glgenframebuffers (1, &m_framebuffer);//Create a frame-dyed buffer object
Glbindframebuffer (Gl_framebuffer, m_framebuffer);//Bind the frame dye buffer object to the pipeline
Glframebufferrenderbuffer (Gl_framebuffer, Gl_color_attachment0, Gl_renderbuffer, m_renderbuffer);// Binds the created render buffer to the frame buffer and fills it with color
3.3 Setting the Viewport
Glviewport (0, 0, width, height);//define the viewport size, which is plainly the OpenGL ES window size
3.4 Creating shaders
3.4.1 Creating an empty shader
Gluint Shaderhandle = Glcreateshader (Shadertype);//shadertype represents the type of shader, which can be gl_vertex_shader (vertex shader) or Gl_fragment_ SHADER (Element Shader)
3.4.2 Specifying shader source code
Glshadersource (Shaderhandle, 1, &source, 0);//source represents an array of source code strings to execute, 1 indicates that the number of strings in the source string array is one, and 0 indicates that the source code string length array is 0
3.4.3 Compiling shaders
Glcompileshader (Shaderhandle);//Compiler shader
3.4.4 Check whether the compilation was successful
Glint compilesuccess;
Glgetshaderiv (Shaderhandle, Gl_compile_status, &compilesuccess);//To see if the compiler shader is successful, optional query status is L_delete_status,gl_ Compile_status, Gl_info_log_length, Gl_shader_source_length
Log an error message if a compilation error occurs
if (compilesuccess = = Gl_false) {
Glchar messages[256];
Glgetshaderinfolog (Shaderhandle, sizeof (messages), 0, &messages[0]);
Std::cout << messages;
Exit (1);
}
3.5 Creating a Render source program
3.5.1 Creating an empty source program
Gluint Programhandle = Glcreateprogram ();//Create a renderer
3.5.2 Adding shaders to the source program
Glattachshader (Programhandle, shaderhandle);//Add shaders to the program
3.5.3 Link Source program
Gllinkprogram (Programhandle);//You may have added multiple shaders, linking programs
3.5.4 Check if the linker is successful
Glint linksuccess;
GLGETPROGRAMIV (Programhandle, Gl_link_status, &linksuccess);//To see if the connection was successful
Link Failure record failure information
if (linksuccess = = Gl_false) {
Glchar message[256];
Glgetprograminfolog (programhandle, sizeof (message), 0, &message[0]);
Std::cout << message;
Exit (1);
}
3.6 Vertex Structural Body
This defines a structure that represents a vertex, contains the position information of a vertex consisting of two points (x, y), and a color information represented by a four value (R, G, B, a)
struct vertex{
float position[2];
float color[4];
};
Create a vertex array with 6 vertex information
Const Vertex vertices[] = {
{{-0.5,-0.866}, {0.5, 1, 0.5, 1}},
{0.5,-0.866}, {0.2, 0.6, 0.5, 1}},
{0, 1}, {0.6, 0.1, 0.8, 1}},
{0.5,-0.866}, {0.5, 0.5, 0.5, 1}},
{1.5,-0.866}, {0.5, 0.5, 0.5, 1}},
{{1, 0.4}, {0.5, 0.5, 0.5, 1}}
};
3.7 Shaders
3.7.1 Vertex Shader
#define STRINGIFY (A) #A
const char *simplevertexshader = stringify (
Attribute vec4 position;//Position, VEC4 description has 4 points, attribute represents attributes, input values provided by the program
Attribute vec4 sourcecolor;//source color, RGBA
Varying VEC4 destinationcolor;//target color, output value, used to pass to the slice shader, vary represents a variable outgoing variable
Uniform mat4 projection;//projection matrix, mat4 represents a 4*4 matrix, uniform represents a uniform, unchanging, each vertex is a fixed value
Uniform mat4 modelview;//Model matrix
void Main (void) {
Destinationcolor = sourcecolor;//simply assigns the source color to the target color
gl_position = Projection * Modelview * position;//The target position is obtained by matrix multiplication, gl_position is the value of OpenGL ES default and must be specified
}
);
3.7.2 Chip Shader
const char *simplefragmentshader = stringify (
Varying LOWP VEC4 destinationcolor;//is passed in by the vertex shader, LOWP indicates low precision
void Main (void) {
Gl_fragcolor = destinationcolor;//color, Gl_fragcolor is also OpenGL es default, you must specify
}
);
3.8 Start rendering
3.8.1 Fill (Clean) screen
Glclearcolor (0.1f, 0.9f, 0.5f, 1);//Specifies the RGBA value of the fill screen
Glclear (gl_color_buffer_bit);//Specifies which buffers to clear, gl_color_buffer_bit represents a color buffer, gl_depth_buffer_bit represents a depth buffer, Gl_stencil_ Buffer_bit represents a template buffer
3.8.2 getting property information from shader code
Gluint M_simpleprogram = Programhandle;
Gluint Positionslot = glgetattriblocation (M_simpleprogram, "Position");//Get Position attribute from vertex shader in shader source program
Gluint Colorslot = glgetattriblocation (M_simpleprogram, "Sourcecolor");//Get Sourcecolor attribute from vertex shader in shader source program
3.8.3 opening a vertex attribute array
Glenablevertexattribarray (Positionslot);
Glenablevertexattribarray (Colorslot);
3.8.4 Assigning values to shader properties
Glsizei stride = sizeof (VERTEX);//Data length for individual vertices
const glvoid *pcoords = &vertices[0] . position[0];//the position array in the vertex array the first address
const glvoid *pcolors = &vertices[0]. color[0];/the first address of the array of colors in the vertex array
glvertexattribpointer (Positionslot, 2, Gl_float, Gl_false, Stride, Pcoords);//assigns a value to the vertex shader location information, Positionslot represents the vertex shader position attribute (that is, Position); 2 means that each vertex information consists of several values that must be bit-by-box or 4;gl_float to represent the data type of the vertex information Gl_false indicates that the data type is not standardized (that is, fixed-point); Stride represents the length of each element in the array; Pcoords represents the first address of the array
Glvertexattribpointer (Colorslot, 4, Gl_float, Gl_false, Stride, pcolors);//Ibid.
3.8.5 Render vertices
Glsizei vertexcount = sizeof (vertices)/sizeof (Vertex);//Vertex count
Gldrawarrays (gl_triangles, 0, Vertexcount);//The vertex array is rendered using a triangle, gl_triangles represents a triangle, 0 represents the position of the first value of the array, and Vertexcount represents the array length
3.8.6 rendering complete, close the array of vertex attributes
Gldisablevertexattribarray (Positionslot);
Gldisablevertexattribarray (Colorslot);
The difference between 4.OpenGL 1.1 and 2.0 in programming
4.1 Differences in function naming
1.1 API functions and the end of the macro are usually added OES (that is, opengles abbreviation), the 2.0 version of the suffix is basically removed the name, such as:
1.1 API functions and Macros: GlbindrenderbufferOES(gl_renderbuffer_OES, M_renderbuffer);
2.0API functions and Macros: Glbindrenderbuffer (Gl_renderbuffer, M_renderbuffer);
4.2 Rendering in different ways
Version 1.1 is based on a non-programmable pipeline, version 2.0 is based on a programmable pipeline, the obvious difference is that 1.1 does not support shaders and 2.0 supports shaders, as follows:
Rendering vertices to the screen in version 1.1 is generally written like this:
Glmatrixmode (gl_projection);
const float MaxX = 2;
Const float Maxy = 3;
Glorthof (-maxx, MaxX,-maxy, Maxy,-1, 1);
Glmatrixmode (Gl_modelview);
Version 2.0 supports shaders and no longer renders vertices like this. Place vertex properties and transformations all in the vertex shader and the slice shader, and then render through the shader in the program
4.3 Writing the same program API is different
For example, the same is the activation and closing of the vertex array, which is glenableclientstate (Gl_vertex_array) in 1.1 , and Gldisableclientstate (Gl_vertex_array);
But in 2.0, it became: Glenablevertexattribarray (*); and Gldisablevertexattribarray (*);
The source of the difference is that 1.1 does not use shaders, and 2.0 uses shaders.
4.4 The degree of difficulty in programming is different.
1.1 is based on a non-programmable pipeline, so each component of the pipeline is well written, and we just need to call it. While 2.0 is based on a programmable pipeline, the flexibility is greatly increased, but the difficulty and complexity of writing is increased, as all functions have to be written by themselves.
5. Shader Usage Flow
just started to learn OpenGL ES2.0, the shader is very cold, what is a shader, how to use the shader, what does the shader contain?
A shader is a string of source code that contains coloring information. Usually the shader is divided into vertex shader (Vertex Shader) and the element shader (Fragment Shader), two shaders are written in different files, the file does not have a fixed suffix, can be written according to your own hobby, But it's best to distinguish between a vertex shader or a slice shader in a file, or you don't know what the file is written about in a long time. If you can give your vertex shader suffix name named: vert, ver, V, vsh, etc., give you the element shader suffix name named: Frag, FRA, F, FSH, etc.
The shader source code and the OpenGL source code are not compiled together, so make a special emphasis on what I just said " shader is a source code string containing coloring information." Therefore, the OpenGL source code must be compiled with the project, but the shader source code is compiled at run time. You might ask, the source code of the shader is a string how to compile it? So OpenGL ES provides a run-time dynamic compilation process:
(1) Create shader: Glcreateshader
(2) Specify shader source code string: Glshadersource
(3) Compiler shader: Glcompileshader
(4) Create shader executable program: Glcompileshader
(5) Adding a shader to an executable program: Glattachshader
(6) Link executable program: Gllinkprogram
OpenGL ES Introductory Explanation