一、Introduction toOpenGL ES 2.0:
(1)、What Is OpenGL ES?: OpenGL ES is an application programming interface (API)for advanced 3D graphics targeted at handheld and embedded devices such as cell phones, personal digital assistants (PDAs), consoles, appliances, vehicles, and avionics. OpenGL ES is one of a set of APIs created by the Khronos Group.
(2)、OpenGL ES 2.0: OpenGL ES 2.0 implements a graphics pipeline with programmable shading and consists of two specifications: the OpenGL ES 2.0 API specification and the OpenGL ES Shading Language Specification (OpenGL ES SL). A、 Vertex Shader; B、Primitive Assembly; C、Rasterization; D、Fragment Shader; E、Per-Fragment Operations.
(3)、OpenGL ES 2.0 and OpenGL ES 1.x Backward Compatibility: OpenGL ES 2.0 is not backward compatible with OpenGL ES 1.x. It does not support the fixed function pipeline that OpenGL ES 1.x supports.
(4)、EGL: OpenGL ES commands require a rendering context and a drawing surface. The rendering context stores the appropriate OpenGL ES state. The drawing surface is the surface to which primitives will be drawn. The drawing surface specifies the types of buffers that are required for rendering such as a color buffer,depth buffer, and stencil buffer. The drawing surface also specifies the bit depths of each of the required buffers.
The OpenGL ES API does not mention how a rendering context is created or how the rendering context gets attached to the native windowing system. EGL is one interface between the Khronos rendering APIs such as OpenGL ES and the native window system.
(5)、Programming with OpenGL ES 2.0:
A、Libraries and Include File: OpenGL ES 2.0 applications will need to link with the following libraries:the OpenGL ES 2.0 library named libGLESv2.lib and the EGL library named libEGL.lib.
OpenGL ES 2.0 applications will need to include the appropriate ES 2.0 and EGL header files. The following include files must be included by any OpenGL ES 2.0 application:
#include
#include
#include
egl.h is the EGL header file, gl2.h is the OpenGL ES 2.0 header file, and gl2ext.h is the header file that describes the list of Khronos-approved extensions for OpenGL ES 2.0.
The header file and library names are platform dependent.
B、EGL Command Syntax: All EGL commands begin with the prefix egl and use an initial capital letter for each word making up the command name.
C、OpenGL ES Command Syntax: All OpenGL ES commands begin with the prefix gl and use an initial capital letter for each word making up the command name.
D、Error Handing:
E、Flush and Finish:
F、Basic State Management:
二、Hello Triangle:An OpenGL ES 2.0 Example:
(1)、Creating a Simple Vertex and Fragment Shader: In OpenGL ES 2.0, nothing can be drawn unless a valid vertex and fragment shader have been loaded. To do any rendering at all, an OpenGL ES 2.0 program must have both a vertex and fragment shader. Every vertex shader must output a position into the gl_Position variable. The gl_FragColor is a special built-in variable that contains the final output color for the fragment shader.
(2)、Compiling and Loading the Shaders:
(3)、Creating a Program Object and Linking the Shaders: Once each shader is compiled into a shader object, they must be attached to a program object and linked together before drawing.
(4)、Setting the Viewport and Clearing the Color Buffer:
(5)、Loading the Geometry and Drawing a Primitive:
(6)、Displaying the Back Buffer: All rendering occurs to the back buffer.
三、An Introduction to EGL: EGL is available for managing drawing surfaces.
(1)、Communicating with the Windowing System: EGL provides a “glue” layer between OpenGL ES 2.0 and the native windowing system running on your computer.
(2)、Creating a Rendering Context: A rendering context is a data structure internal to OpenGL ES 2.0 that contains all of the state required for operation.
四、Shaders and Programs:
(1)、Shaders and Programs: A similar paradigm is used in OpenGL ES for representing shaders. The shader object is an object that contains a single shader. The source code is given to the shader object and then the shader object is compiled into object form (likean .objfile). After compilation, the shader object can then be attached to a program object. A program object gets multiple shader objects attached to it.In OpenGL ES, each program object will need to have one vertex shader object and one fragment shader object attached to it (no more,and no less).The program object is then linked into a final “executable.” The final program object can then be used to render.
(2)、Creating and Compiling a Shader: A、The first step to working with a shader object is to create it. This is done using glCreateShader;The return value is a handle to the new shader object. When you are finished with a shader object, you can delete it using glDeleteShader; B、Once you have a shader object created, typically the next thing you will do is provide the shader source code using glShaderSource; C、Once the shader source has been specified,the next step is to compile the shader, Not all implementations of OpenGL ES2.0 provide the ability to compile a shader (some require shaders to be compiled offline); Given an implementation of OpenGL ES supports online compilation and you have specified its source, you can then compile the shader using glCompileShader; As with any normal language compiler,the first thing you want to know after compiling is whether there were any errors. This, along with other information about the shader object, can be queried for using glGetShaderiv; The info log can then be retrieved using glGetShaderInfoLog;
(3)、Creating and Linking a Program: D、the next step is to create a program object. a program object is a container object to which you attach shaders and link a final executable program. The function calls to manipulate program objects are very similar to shader objects. A program object is created using glCreateProgram; E、Once you have a program object created, the next step is to attach shaders to it. In OpenGL ES 2.0, each program object will need to have one vertex shader and one fragment shader object attached to it. The function to attach shaders to a program is glAttachShader; Note that a shader can be attached at any point. It does not necessarily need to be compiled or even have source code before being attached to a program. The only requirement is that every program object will have to have one and only one vertex shader and fragment shader object attached to it;F、Once the shaders have been attached (and the shaders have been successfully compiled), we are finally ready to link the shaders together. Linking a program object is accomplished using glLinkProgram; After linking a program, you will need to check whether the link succeeded. The link status can be checked by using glGetProgramiv; After linking the program, we will now want to get information from the program info log (particularly if there was a link failure). Doing so is very similar to getting the info log for shader objects, glGetProgramInfoLog;G、Once we have linked the program successfully,we are just about ready to render with it. One thing we might want to check is whether the program validates. That is, there are certain aspects of execution that a successful link cannot guarantee. For example, it might be the case that the application never binds valid texture units to samplers. This will not be known at link time, but instead at draw time. To check that your program will execute with the current state, you can call glValidateProgram; You really only want to use glValidateProgram for debugging purposes. It is a slow operation and certainly not something you want to check before every render; H、There is one more thing you need to do with a program object before rendering and that is to set it as the active program using glUseProgram.
(4)、Uniforms and Attributes: Uniforms are variables that store read-only constantvalues that are passed in by the application through the OpenGL ES 2.0API to the shader. The set of uniforms is shared across a program object. Thatis, there is one set of uniforms for a program object.Ifa uniform is declared in both a vertex and fragment shader, it must have the sametype and its value will be the same in both shaders. During the link phase,the linker will assign uniform locations to each of the active uniforms in theprogram. These locations are the identifiers the application will use to loadthe uniform with a value.Attributes:In addition to querying for uniform information on the program object, you willalso need to use the program object to set up vertex attributes. The queriesfor vertex attributes are very similar to the uniform queries.
(5)、Shader Compiler and Shader Binaries: Instead of requiring that all implementationsof OpenGL ES 2.0 provide such a compiler, the specification allows an implementationto instead support only binary shaders. The idea behind binary shaders is thatthe OpenGL ES 2.0 vendor provides an offline tool that can take shader source codeand compile it to a binary format that can be consumed by the implementation. There is no standard binary format, so each vendor will have its own set of tools.
五、OpenGL ESShading Language: Every OpenGL ES 2.0 program requires both a vertex and fragment shaderto render a meaningful picture:
(1)、Variables and Variable Types: In computer graphics, there are two fundamental data types that form the basis of transformations: vectors and matrices. These two data types arecentral to the OpenGL ES Shading Language as well.Variablesin the shading language must be declared with a type; Variables can beinitialized either at declaration time or later. Initialization is done throughthe use of constructors, which are also used for doing type conversions.
(2)、Variable Constructors: The OpenGL ES Shading Language has very strict rules regarding type conversion. That is, variables can only be assigned to or operated on othervariables of the same type. To cope with type conversions, there are a number of constructors available in the language. You can use constructors for initializing variables and as a way of type-casting between variables of different types.Matrices in OpenGL ES are stored incolumn major order.
(3)、Vector and matrix Components: The individual components of a vector can be accessed in two ways:either using the “.” operator or through array subscripting.
(4)、Constants: It is possible to declare any of the basic types as being constantvariables. Constant variables are those whose values do not change within theshader. Constants are declared by adding the const qualifier to thedeclaration. Const variables must be initialized at declaration time. Just asin C or C++, a variable that is declared as const is read-only and cannot bemodified within the source.
(5)、Structures: In addition to the basic types provided in the language, it is alsopossible to aggregate variables into structures much like in C; Structures canbe initialized using constructors. After defining a new structure type, a newstructure constructor is also defined with the same name of the type. Theremust be a one-to-one correspondence between types in the structure and those inthe constructor.
(6)、Arrays: In addition to structures, the OpenGL ES Shading Language also supports arrays.The syntax is very similar to C, with the arrays being based on a 0 index.There are two important things to note about the use of arrays in the OpenGL ESShading Language. The first is that many OpenGL ES implementations willnot allow an array to be indexed with a variable with an unknownvalue at compile time. That is, OpenGL ES only mandates that arrayindexing be supported by constant integral expressions. (there is an exceptionto this, which is the indexing of uniform variables in vertex shaders). Theother note about arrays is that there is no syntax in the OpenGL ES ShadingLanguage to initialize an array at creation time. The elements of the arrayneed to be initialized one-by-one and also arrays cannot be const qualified becausethere is no syntax for initializing such an array.
(7)、Operators: Most of these operators behave just as you are used to in C, theoperators must occur between variables that have the same basic type. Thecomparison operators (==, !=, <, etc.) can only be performed on scalar values.
(8)、Functions: Functions are declared in much the same way as C. If a function is goingto be used prior to its definition, then a prototype declaration must beprovided. In general, functions work much as you are used to in C. The most significantdifference is the way in which parameters are passed to functions.The OpenGL ES Shading Language provides special qualifiers(in, inout, out)to define whether a variable argument can be modified by thefunction. One note about functions in the OpenGL ES Shading Language isthat functions cannot be recursive.
(9)、Built-In Functions: One of the most powerful features of the OpenGL ES Shading Language is thebuilt-in functions that are provided in the language.
(10)、Control Flow Statements: The syntax for control flow statements in the OpenGL ES Shading Languageis similar to C. The expression that is being tested in the conditionalstatement must evaluate to a boolean. The basic restrictions are as follows: there must be only one loop iteration variable and it mustbe incremented or decremented using a simple statement (i++, i--, i+=constant,i-=constant); the stop condition must be a comparison between the loop indexand a constant expression; and you must not change the value of the iterator inthe loop.
(11)、Uniforms: Uniform variables are variables that store read-only values that arepassed in by the application through the OpenGL ES 2.0 API to the shader. Uniformvariables are declared at the global scope and simply require the uniformqualifier. Note also that the namespace for uniform variables is shared across botha vertex and a fragment shader. That is, if a vertex and fragment shader arelinked together into a program object, they share the same set of uniform variables.Therefore, if a uniform variable is declared in the vertex shader and also inthe fragment shader, its declaration must match. When the application loads theuniform variable through the API, its value will be available in both thevertex and fragment shaders. Another note about uniforms is that uniformvariables generally are stored in hardware into what is known as the “constantstore.” This is a special space allocated in the hardware for the storage ofconstant values.
(12)、Attributes: Attribute variables are available only in thevertex shader and are used to specify the per-vertex inputs to the vertexshader.Attributes typically store data such as positions,normals, texture coordinates, and colors. The key here to understand isthat attributes are data that are specified for each vertex being drawn. It isreally up to the user of the shader to determine what data belongs in theattributes. The minimum number of attributes that an OpenGL ES 2.0implementation can support is eight.
(13)、Varyings:Varying variables are used to store the output of the vertexshader and also the input of a fragment shader. Basically, each vertex shader will outputthe data it needs to pass the fragment shader into one or more varyingvariables. These variables will then also be declared in the fragment shader(with matching types) and will be linearly interpolated across the primitiveduring rasterization. The minimum number of varying vectors that animplementation of OpenGL ES 2.0 can support is eight.
(14)、Preprocessor and Directives: The OpenGL ES Shading Language features a preprocessor that follows manyof the conventions of a standard C++ preprocessor. Note thatmacros cannot be defined with parameters. A newdirective (not in C++) that was added to the preprocessor is #version. Anotherimportant directive in the preprocessor is #extension, which is used to enableand set the behavior of extensions.
(15)、Uniform and Varying Packing: The rules for packing are based on the notion that the physical storage spaceis organized into a grid with four columns (one column for each vectorcomponent) and a row for each storage location. The packing rules seek to pack variablessuch that the complexity of the generated code will remain constant. In otherwords, the packing rules will not do reordering that will require the compilerto generate extra instructions to merge unpacked data. Rather, the packingrules seek to optimize the use of the physical address space without negativelyimpacting runtime performance.
(16)、Precision Qualifiers: One notable new feature addition to OpenGL ES that differs from desktop OpenGLis the introduction of precision qualifiers to the shading language. Precisionqualifiers enable the shader author to specify the precision with whichcomputations for a shader variable are performed. Variablescan be declared to have either low, medium, or high precision. Thesequalifiers are used as hints to the compiler to allow it to performcomputations with variables at a potentially lower range and precision. It ispossible that at lower precisions, some implementations of OpenGL ES mighteither be able to run the shaders faster or with better power efficiency.Precision qualifiers can be used to specify the precision of any float orinteger-based variable. The keywords for specifying the precision are lowp, mediump,and highp. The default precision qualifier is specifiedat the top of a vertex or fragment shader.
(17)、Invariance: The introduction of invariance gives the shader writer a way to specifythat if the same computations are used to compute an output, its value must beexactlythe same (or invariant). Once invariance is declared for an output, thecompiler guarantees that the results will be the same given the same computationsand inputs into the shader. Because the compiler needs to guarantee invariance,it might have to limit the optimizations it does. Therefore, the invariant qualifier should be used only when necessary;otherwise it might result in performance degradation. Note also that whileinvariance does imply that the calculation will have the same results on agiven GPU, it does not mean that the computation would be invariant across anyimplementation of OpenGL ES.
六、VertexAttributes, Vertex Arrays, and Buffer Objects:
(1)、Specifying Vertex Attribute Data: The attribute data can be specified foreach vertex using a vertex array or it can be a constant value that is used forall vertices of a primitive. All OpenGLES 2.0 implementations must support a minimum of eight vertex attributes.
(2)、Constant Vertex Attribute: A constant vertex attribute is the same for all vertices of a primitive,and therefore only one value needs to be specified for all the vertices of a primitive.ES 2.0 only supports the float variant.
(3)、Vertex Arrays: Vertex arrays specify attribute data per vertex and are buffers storedin the application’s address space (what OpenGL ES calls theclient space).They provide an efficient and flexible way for specifying vertex attributedata.
(4)、Selecting Between a Constant Vertex Atrribute or a Vertex Array: If the vertex attributearray is disabled for a generic attribute index, the constant vertex attributedata specified for that index will be used.
(5)、Declaring Vertex Attribute Variables in a Vertex Shader: In a vertex shader, avariable is declared as a vertex attribute by using the attribute qualifier.The attribute qualifier can only be used in a vertex shader. The attribute qualifiercan be used only with the data types float, vec2, vec3, vec4, mat2, mat3, and mat4.Attribute variables cannot be declared as arrays orstructures. Variables declared as vertex attributes in a vertex shaderareread-only variables and cannot be modified.
(6)、Binding Vertex Attributes to Attribute Variables in a Vertex Shader: Attribute names that donot exist or are not active in a vertex shader attached to the program objectare ignored.
(7)、Vertex Buffer Objects: The vertex data specified using vertex arrays is stored in clientmemory. This data has to be copied from client memory to graphics memory when acall to glDrawArrays or glDrawElements is made. Vertex buffer objects allowOpenGL ES 2.0 applications to allocate and cache vertex data in high-performancegraphics memory and render from this memory, thus avoiding resending data everytime a primitive is drawn. Not only the vertex data, but even the elementindices that describe the vertex indices of the primitive and are passed as anargument to glDrawElements can also be cached. There are two types of bufferobjects supported by OpenGL ES:array buffer objects and elementarray buffer objects. To get best performance, werecommend that OpenGL ES 2.0 applications use vertex buffer objects for vertexattribute data and element indices wherever possible.
(8)、Mappting Buffer Objects: The OES_map_buffer extension allows applications to map and unmap a vertexbuffer object’s data storage into the application’s address space. glMapBufferOES shouldonly be used if the whole buffer is being updated. Using glMapBufferOES toupdate a subregion is not recommended.
七、Primitive Assemblyand Rasterization:Rasterization is the process that converts primitivesinto a set of two-dimensional fragments, which are processed by thefragment shader. These two-dimensional fragments represent pixels that may bedrawn on the screen.
(1)、Primitives: A primitive is a geometric object that can bedrawn using the glDrawArrays and glDrawElements commands in OpenGL ES.The primitive is described by a set of vertices thatdescribe the vertex position, and other information such as color, texturecoordinates, and normals. The following aretheprimitives that can be drawn in OpenGL ES 2.0: Triangles、Lines、Point sprites.
(2)、Triangles: GL_TRIANGLES draws a series of separate triangles. A total of n/3 trianglesare drawn. GL_TRIANGLE_STRIP draws a series of connected triangles. A total of (n– 2) triangles are drawn. GL_TRIANGLE_FAN also draws a series of connectedtriangles. A total of (n – 2) triangles are drawn.
(3)、Lines: The line primitives supported by OpenGL ES are GL_LINES, GL_LINE_STRIP, andGL_LINE_LOOP. GL_LINES draws a series ofunconnected line segments. A total of n/2 segments are drawn. GL_LINE_STRIP drawsa series of connected line segments. A total of (n – 1) line segments aredrawn. GL_LINE_LOOP works similar to GL_LINE_STRIP, except that a final linesegment is drawn from Vn-1 to V0. A total of n line segments are drawn. Thewidth of a line can be specified using the glLineWidth API call.
(4)、Point Sprites: The point sprite primitive supported by OpenGL ES is GL_POINTS. A point spriteis drawn for each vertex specified. Point sprites are typically used forrendering particle effects efficiently by drawing them as points instead ofquads. A point sprite is a screen-aligned quad specified as aposition anda radius. The position describes the center of the square and the radiusis then used to calculate the four coordinates of the quad that describes thepoint sprite. By default, OpenGL ES 2.0 describes the window origin (0, 0) tobe the (left, bottom) region. However, for point sprites, the point coordinateorigin is (left, top). gl_PointSize is the built-in variable that can be usedto output the point radius (or point size) in the vertex shader. gl_PointCoord isa built-in variable available only inside a fragment shader when the primitivebeing rendered is a point sprite.
(5)、Drawing Primitives: There are two API calls in OpenGL ES that can beused to draw primitives: glDrawArrays and glDrawElements.
(6)、Primitive Assembly: Coordinate Systems, Clipping,Perspective Division, Viewport Transformation.
(7)、Rasterization: After the vertices have been transformed and primitives have been clipped,the rasterization pipelines take an individual primitive such as a triangle, aline segment, or a point sprite and generates appropriate fragments for thisprimitive. Each fragment is identified by its integer location (x, y) inscreen space. A fragment represents a pixel location given by (x, y) inscreen space and additional fragment data that will be processed by the fragmentshader to produce a fragment color. Beforetriangles are rasterized, we need to determine whether they are frontfacing (i.e.,facing the viewer) or back-facing (i.e., facing away from the viewer). Theculling operation discards triangles that face away from the viewer. Todetermine if the triangle is front-facing or back-facing we first need to knowthe orientation of the triangle. The orientation of a triangle specifies thewinding order of a path that begins at the first vertex, goes through the secondand third vertex and ends back at the first vertex. The orientation of atriangle is computed by calculating the signed area of the triangle in windowcoordinates. Culling should always be enabled to avoid the GPU wasting time rasterizingtriangles that are not visible. Enabling culling should help improve theoverall performance of the OpenGL ES application.
八、Vertex Shaders: Vertex shaders can beused to do traditional vertex-based operations such as transforming the positionby a matrix, computing the lighting equation to generate a pervertex color, andgenerating or transforming texture coordinates.
(1)、Vertex Shader Overview: The inputs to the vertex shader consist of thefollowing:A、Attributes: Per-vertex data supplied using vertex arrays; B、Uniforms:Constant dataused by the vertex shader; C、Shader program: Vertex shader program sourcecode or executable that describes the operations that will be performed on thevertex.The outputs of the vertex shader are calledvarying variables. In the primitive rasterization stage, these variablesare computed for each generated fragment and are passed in as inputs to thefragment shader.
(2)、Vertex Shader Built-In Variables: The built-in variables of a vertex shadercan be categorized into special variables that are output by the vertex shader,uniform state such as depth range, and constants that specify maximum valuessuch as the number of attributes, number of varyings, and number of uniforms.
(3)、Built-In Special Variables: OpenGL ES 2.0 has built-in special variablesthat are either output by the vertex shader that then become input to thefragment shader, or are output by the fragment shader. The built-inspecial variables available to the vertex shader are as follows: A、gl_Position: gl_Positionis used to output the vertex position in clip coordinates. The gl_Position valuesare used by the clipping and viewport stages to perform appropriate clipping ofprimitives and convert the vertex position from clip coordinates to screencoordinates. The value of gl_Position is undefined if the vertex shader doesnot write to gl_Position. gl_Position is a floating-point variable declaredusing the highp precision qualifier. B、gl_PointSize: gl_PointSize is used to writethe size of the point sprite in pixels. gl_PointSize is used when point spritesare rendered. The gl_PointSize value output by a vertex shader is then clampedto the aliased point size range supported by the OpenGL ES 2.0 implementation. gl_PointSizeis a floating-point variable declared using the mediump precision qualifier. C、gl_FrontFacing:This special variable, although not directly written by the vertex shader, isgenerated based on the position values generated by the vertex shader andprimitive type being rendered. gl_FrontFacing is a boolean variable.
(4)、Built-In Uniform State: The only built-inuniform state available inside a vertex shader is the depth range in windowcoordinates. This is given by the built-in uniform name gl_DepthRange.
(5)、Built-In Constants: The following built-in constants are also available inside the vertexshader: A、gl_MaxVertexAttribs :The minimum value supported by all ES 2.0implementations is eight; B、gl_MaxVertexUniformVectors :The minimum valuesupported by all ES 2.0 implementations is 128 vec4 entries. C、gl_MaxVaryingVectors: The minimum valuesupported by all ES 2.0 implementations is eight vec4 entries. D、gl_MaxVertexTextureImageUnits:The minimum value is 0, which implies that the implementation does not supporta vertex texture fetch; E、gl_MaxCombinedTextureImageUnits:The minimum value is eight.
(6)、Precision Qualifiers: Precision qualifiers can be used to specify the precision of any float-or integer-based variable. The keywords for specifying the precision are lowp, mediump,and highp. The default precision qualifier is specified at the top of a vertexor fragment shader. In the vertex shader, if no default precision is specified,the default precision for both int and float is highp.
(7)、ES 2.0 Vertex Shader Limitations: A、Length of Vertex Shader: If the instructioncount exceeds the maximum number of instructions allowed in a vertex shader,the vertex shader source will fail to compile. B、Temporary Variables: there is no way tospecify the minimum number of temporary variables that must be supported by allOpenGL ES 2.0 implementations. C、Flow Control: for; D、ConditionalStatements: if, if else; E、Array Indexing: Array indexing of uniforms(excluding samplers) is fully supported. The array index can be a constant,uniform, or computed value. Samplers can only be indexed using a constantintegral expression. Attribute matrices and vectors can be indexed using aconstant integral expression. Indexing attribute matrices and vectors with a non-constantintegral expression is not mandated. This, however, is a very useful feature. F、Counting Numberof Uniforms Used in a Vertex Shader: gl_MaxVertexUniformVectors describes themaximum number of uniforms that can be used in a vertex shader. The minimumvalue for gl_MaxVertexUniformVectors that must be supported by any compliant OpenGLES 2.0 implementation is 128 vec4 entries.
(8)、Vertex Shader Examples:
(9)、Generating Texture Coordinates:
(10)、OpenGL ES 1.1 Vertex Pipeline as an ES 2.0Vertex Shader:
九、Texturing:
(1)、Texturing Basics: Textures in OpenGL ES 2.0 come in twoforms: 2D textures and cube map textures. Textures are typically applied to asurface by using texture coordinates that can be thought of as indices into texturearray data.
(2)、2D Textures: 2D texture is a two-dimensional array ofimage data. When rendering with a 2D texture, a texture coordinate is used asan index into the texture image.
(3)、Cubemap Textures: a cubemap is a texture made up of six individual2D texture
faces. Each face of the cubemap represents one of the six sides of a cube.
(4)、Texture Objects and Loading Textures: The first step in theapplication of textures is to create atexture object. A texture objectis a container object that holds the texture data that is needed for renderingsuch as image data, filtering modes, and wrap modes. the next step to using a textureis to actually load the image data.
(5)、Texture Filtering and Mipmapping: filter: nearest,linear. If minification occurs and performance is a concern, using a mipmapfiltering mode is usually the best choice on most hardware.
(6)、Automatic Mipmap Generation:
(7)、Texture Coordinate Wrapping: Texture wrap modes are used to set what the behavior is when a texturecoordinate is outside of the range [0.0, 1.0].
(8)、Using Texture in the Fragment Shader:
(9)、Example of Using a Cubemap Texture:
(10)、Compressed Textures: It is up to the vendor that implements OpenGL ES 2.0 to provide optionalextension(s) that provide compressed image data types. One ratified compressedtexture extension, Ericsson Texture Compression (ETC), is likely to besupported by a number of vendors.
(11)、Texture Subimage Specification:
(12)、Copying Texture Data from the Color Buffer: This can be useful if you want to use theresults of rendering as an image in a texture. Please note that framebufferobjects provide a fast method for doing render-to-texture and are a fastermethod than copying image data. However, if performance is not a concern, theability to copy image data out of the color buffer can be a useful feature. thetexture image format cannot have more components than the color buffer.
(13)、Optional Extensions:
A、3D Textures:
B、Ericsson Texture Compression(ETC): The ETC format stores data in 4 × 4 blocksof texels that are each 64 bits in size.
C、Floating-Pont Textures: In core OpenGL ES 2.0, there is no way to store textures with 16-bit or 32-bitfloating-point precision.
D、Non-Power-of-Two Textures: In OpenGL ES 2.0, textures can have non-power-of-two (npot) dimensions. Inother words, the width and height do not need to be a power of two.
十、FragmentShaders:
(1)、Fixed Function Fragment Shaders:
(2)、FragmentShader Overview: The fragment shader provides a general-purposeprogrammable method for operating on fragments.Theinputs to the fragment shader consist of the following: A、Varyings—Interpolateddata produced by the vertex shader. B、Uniforms—State used by the fragment shader. C、Textures—Textureimages accessed through samplers. D、Code—Fragment shader source or binary thatdescribes the operations that will be performed on the fragment. The output of the fragment shader is the fragment color thatgets passed on to the per-fragment operations portion of the pipeline.
(3)、Built-In Special Variables: OpenGL ES 2.0 has built-in special variables that are output by thefragment shader or are input to the fragment shader.Thebuilt-in special variables available to the fragment shader are the following:A、gl_FragColor isused to output the fragment color from the shader. This color is then passed into the per-fragment operations in the pipeline. B、gl_FragCoord is a read-only variable that isavailable in the fragment shader. This variable holds the window relativecoordinates (x,y, z, 1/w) of the fragment. C、gl_FrontFacing is a read-only variable that is available in the fragmentshader. This variable is a boolean with a value of true if the fragment is partof a front-facing primitive and false otherwise. D、gl_PointCoord is a read-only variable thatcan be used when rendering point sprites. It holds the texture coordinate for thepoint sprite that is automatically generated in the [0, 1] range during pointrasterization.
(4)、Built-In Constants: The built-in constants describe thefollowing maximum terms: A、gl_TextureImageUnits—This is the maximumnumber of texture image units that are available. The minimum value supportedby all ES 2.0 implementations is eight. B、gl_MaxFragmentUniformVectors—This is the maximumnumber of vec4 uniform entries that can be used inside a fragment shader. The minimumvalue supported by all ES 2.0 implementations is 16 vec4 entries. C、gl_MaxDrawBuffers—This is the maximum number of draw buffers available. The minimum valuesupported by all ES 2.0 implementations is 1.
(5)、Precision Qualifiers: there is no default precision for fragment shaders. This means thatevery fragment shader must declare a default precision(or provide precision qualifiers for all variable declarations). OpenGL ES 2.0mandates that implementations support at least medium precision in the fragmentshader, but does not require support for high precision.
(6)、ES 2.0 Fragment Shader Limitations: The only difference in limitations for fragmentshaders is thatuniform arrays can only be indexed withconstant integral expressions. In the vertex shader, it is required thatall implementations support indexing of uniform arrays using computedexpressions. However, this is not the case for the fragment shader. Indexing ofuniforms using anything other than constant integral expressions is notguaranteed to be supported by an ES 2.0 implementation.
(7)、Implementing Fixed Function Techniques Using Shaders: The fixed-functionpipeline in OpenGL ES 1.x and desktop OpenGL provided APIs to performmultitexturing, fog, alpha test, and user clip planes.
A、Multitexturing:
B、Fog:
C、Alpha Test(Using Discard):
D、User Clip Planes:
十一、FragmentOperations: the output of the fragment shader is the fragment’s color and depthvalue.
(1)、Buffers: OpenGL ES supports three types of buffers, each of which storesdifferent data for every pixel in the framebuffer: Color buffer、Depth buffer、Stencil buffer. Thesize of a buffer—commonly referred to as the “depth of the buffer” (but not tobe confused with the depth buffer)—is measured by the number of bits that areavailable for storing information for a single pixel. The color buffer, for example,will have three components for storing the red, green, and blue colorcomponents, and optional storage for the alpha component. The depth of thecolor buffer is the sum of the number of bits for all of its components. Forthe depth and stencil buffers, on the other hand, a single value represents thebit depth of a pixel in those buffers. every EGL implementation must provide atleast one configuration that contains all three of the buffers, with the depthbuffer being at least 16 bits deep, and at least eight bits for the stencilbuffer.
(2)、Requesting Additional Buffers: To include a depth or stencil buffer along with your color buffer, youneed to request them when you specify the attributes for your EGLconfiguration.
(3)、Clearing Buffers:
(4)、Using Masks to Control Writing to Framebuffers:
(5)、Fragment Tests and Operations: By default, all fragment tests and operations are disabled, andfragments become pixels as they are written to the framebuffer in the order inwhich they’re received.
(6)、Using the Scissor Test:
(7)、Stencil Buffer Testing: The stencil buffer is a per-pixel mask that holds values that can beused to determine whether a pixel should be updated or not. The stencil test isenabled or disabled by the application.
(8)、Depth Buffer Testing: The depth buffer is usually used for hidden-surface removal.
(9)、Blending:
(10)、Dithering:
(11)、Multisampled Antialiasing: Multisampling divides every pixel into a set of samples, each of whichis treated like a “mini-pixel” during rasterization. That is, when a geometricprimitive is rendered, it’s like rendering into a framebuffer that has manymore pixels than the real display surface. Each sample has its own color,depth, and stencil value, and those values are preserved until the image isready for display.
(12)、Reading and Writing Pixels to the Framebuffer: If you would like to preserve your renderedimage for posterity’s sake, you can read the pixel values back from the colorbuffer, but not from the depth or stencil buffers.
十二、FramebufferObjects:
(1)、Why Framebuffer Objects?: By default, OpenGL ES uses the window system provided framebuffer asthe drawing surface. There are two techniques that applications can use to renderto a texture: A、Implement render to texture by drawing to the window system providedframebuffer and then copy the appropriate region of the framebuffer to thetexture. B、Implement render to texture by using a pbuffer that is attached to a texture.Framebuffer objects therefore provide a better and more efficient method forrendering to a texture or an off-screen surface.
(2)、Framebuffer and Renderbuffer Objects: A renderbuffer object is a 2D imagebuffer allocated by the application. The renderbuffer can be used to allocateand store color, depth, or stencil values and can be used as a color, depth, orstencil attachment in a framebuffer object. A renderbuffer is similar to an off-screenwindow system provided drawable surface, such as a pbuffer. A renderbuffer, however,cannot be directly used as a GL texture. Aframebuffer object (oftenreferred to as an FBO) is a collection of color, depth, and stencil bufferattachment points; state that describes properties such as the size and format ofthe color, depth, and stencil buffers attached to the FBO; and the names of thetexture and renderbuffer objects attached to the FBO.
(3)、Choosing a Renderbuffer Versus a Texture as a Framebuffer Attachment:
(4)、Framebuffer Objects Versus EGL Surface:
(5)、Creating Framebuffer and Renderbuffer Objects:
(6)、Using Renderbuffer Objects:
(7)、Using Framebuffer Object:
(8)、Attaching a Renderbuffer as a Framebuffer Attachment:
(9)、Attaching a 2D Texture as a Framebuffer Attachment:
(10)、Attaching an Image of a 3D Texture as a Framebuffer Attachment:
(11)、Checking for Framebuffer Completeness: A framebuffer object needs to be defined ascompletebefore it can be used as rendering target.
(12)、Deleting Framebuffer and Renderbuffer Objects:
(13)、Deleting Renderbuffer Objects That Are Used as Framebuffer Attachments:
(14)、Reading Pixels and Framebuffer Objects:
(15)、Examples:
(16)、Performance Tips and Tricks: Avoid switching between rendering to window system provided framebufferand rendering to framebuffer objects frequently. Don’t create and destroyframebuffer and renderbuffer objects (or any other large data objects for thatmatter) per frame. Try to avoid modifying textures. Set pixels argument in glTexImage2Dand glTexImage3DOES to NULL if the entire texture image will be rendered as theoriginal data will not be used anyway. Share depth and stencil renderbuffers asattachments used by framebuffer objects wherever possible to keep the memoryfootprint requirement to a minimum.
十三、AdvancedProgramming with OpenGL ES 2.0:
(1)、Per-Fragment Lighting: A、Lighting with a Normal Map: B、Lighting Shaders: C、LightingEquations:
(2)、Environment Mapping:
(3)、Particle System with Point Sprites: A、Particle System Setup: B、Particle System VertexShader: C、Particle System Fragment Shader:
(4)、Image Postprocessing: A、Render-to-Texture Setup: B、Blur Fragment Shader: C、Light Bloom:
(5)、Projective Texturing: A、Projective Texturing Basics: B、Matrices for Projective Texturing:C、ProjectiveSpotlight Shaders:
(6)、Noise Using a 3D Texture: A、Generating Noise: B、Using Noise:
(7)、Procedural Texturing: A、A Procedural Texture Example: b、Antialiasing of Procedural Textures:
十四、StateQueries:
(1)、OpenGL ES 2.0 Implementation String Queries: glGetString
(2)、Querying Implementation-Dependent Limits:
(3)、Querying OpenGL ES State:
(4)、Hints: glHint
(5)、Entity Name Queries:
(6)、Nonprogrammable Operations Control and Queries:
(7)、Shader and Program State Queries:
(8)、Vertex Attribute Queries:
(9)、Texture State Queries:
(10)、Vertex Buffer Queries:
(11)、Renderbuffer and Framebuffer State Queries:
十五、OpenGLES and EGL on Handheld Platforms:
(1)、Handheld Platforms Overview:
(2)、Online Resources: Given the wide array of handheld platforms, we thought it would beuseful to give a quick guide to where to get information to start developingfor each of the platforms.
(3)、C++ Portability: If you choose to use C++, the following is a list of features that youshould avoid to gain portability: A、Runtime type information; B、Exceptions; C、Standard TemplateLibrary; D、Multiple inheritance; E、Global data.
(4)、OpenKODE: Open-KODE provides a standard set of APIs (including OpenGL ES and EGL)to which an application can write in order to access functionality on thesystem. OpenKODE Core provides APIs for events, memory allocation, file access,input/output, math, network sockets, and more.
(5)、Platform-Specific Shader Binaries: shader binaries are inherently non-portable.generation of binary shaders does not have any defined mechanism in the APIstandard. vendor extension that defines the shader binaries might putrestrictions on how those binaries can be used. Your engine will need tosupport a preprocessing path whereby you can choose one of the device vendors’tools and store a binary shader package to go along with the app.
(6)、Targeting Extensions: If you want to guarantee portability across platforms and are unable towrite fallback paths for specific extensions, you should avoid using them.
Appendix:
1、 GL_HALF_FLOAT_OES:
2、 Built-In Functions:
3、 Shading Language Grammer:
4、 ES Framework API:
以上内容摘自《OpenGL ES 2.0 ProgrammingGuide》