一,introduction to OGLES 3.0
OGLES 3.0 Graphics Pipeline:
VertexBuffer/ArrayObj => VertexShader(texture)===(transform feedback)===> primitives assembler => Rasterizer Stage => fragment/Pixel shader stage(texture) => per fragment operations => framebuffer
1,Vertex Shader
Input:
Shader program : vertex shader program source code or executable, that describes operations that will be performed on vertex.
Vertex shader inputs (or attributes):per-vertex data supplied using vertex arrays.
Uniforms:constant data, used by vertex or fragment shader
Samplers:specific特殊 types of uniforms that represent代表 textures used by vertex shader,
Output:
Output(varying)0/1/2/…
Gl_Position
Gl_PointSize
In primitive restertization stage, vertex shader output values are calculated , for each generated fragment , and are passed in as input to fragment shader.
The mechanism , used to generate a value for each fragment from the vertex shader outputs, that is assigned to each vertex of the primitive is called interpolation插值,
Vertex shaders can be used for
traditional vertex-absed operations :
such as transforming the position by a matrix,
computing the lighting equation公式 to generate a per-vertex color
generating or transforming texture coordinates,
Alternatively,because vertex shader is specified by app,so can be used to perform custom math that enables:
New transforms, lingting, or vertex-based effects not allowed in more traditional fixed-function pipeline,
Example:
vertex shader in it takes取得 a postion and its associated color data as input attributes,
transforms the position using a 4x4 matrix, and outputs the transformed position and color.
#version 300 es
uniform mat4 u_mvpMatrix; //matrix to convert a_position from model space to normalized device space
//attributes input to vertex shader
in vec4 a_position //position value
in vec4 a_color //input vertex color
//output of the vertex shader – input to fragment
out vec4 v_color; //output vertex color
void main()
{
v_color = a_color;
gl_Position = u_mvpMatrix * a_position;
}
Line1, provides version of shading language,
Line 2,describes a uniform variable u_mvpMatrix that stores the combined model view and projection matrix投影矩阵,
Line 12, declare the output v_color to store the ouput of the vertex shader that describes the per-vertex color.
The built-in variable called gl_Position is declared automatically, shader must write the transformed position to this variable.
Vertex or fragment shader has a single entry point called the main function.
1.1.2 Primitive Assembly图元装配
After vertex shader, next stage in pipeine is primitive assembnly. Primitive is a geometric obj such as triangle ,line, or point sprite.
Each vertex of a primitive is sent to a diff copy of vertex shader. During primitive assembly, these vertices are grouped back into the primitive.
For each primitive, must be determined whether the primitive lies within the view frustums视锥体,(the region of 3D space that is visible on the screen)
If not, it need to be clipped裁剪 to the view frustum.
If primitive is completely outside this region it is discarded,
After clipping, the vertex position is converted to screen coordinates.
A culling淘汰 operation can also be performed that discards primitives based on whether they face forward or backwared.
After clipping and culling, the primitive is ready to be passed to next stage of pipeline : rasterization stage
1.1.3 Rasterization
Rasterization phase, where the appropriate primitive is drawn.
Rasterization is the process that converts primitives into a set of two-dimensional fragments,which are then processed by fragment shader.
Output:
For each fragment : scrren(x,y) coordinate, attributes such as color, texture coordinates. Etc.
1.1.4 Fragment Shader
Implements a general purpose programmabnle method for operating on fragments.
This shader is executed for each generated fragment by rasterization stage and tasked follow inputs :
Shader program
Inpuit variables : outputs of vertex shader , that are generated by rasterization unit for each fragment using interpolation插值。
Uniforms
Samplers
The color, depth ,stencil and scrren coordinate location, generated by rasterization stage become inputs to the per-fragment operations stage of pipeline.
Examples:
The input to fragment shader are linearly interpolated across the primitive before being passed into the fragment shader.
1.1.5 Per-Fragment Operations
Fragment produced by rasterization with (x,y) scrren coordinates can only modify the pixel at location(x,y ) in framebuffer.
Per-Freagment Operations:
Fragment data => Pixel Ownership Test => Scissor Test => Stencil test => Depth Test => Blending => Dithering抖动 => to Frambuffer
Pixel ownership test:
Stencil and depth tests:
These test are performed on the stencil and depth value of incoming fragment to determine whether the fragment should be rejected.
Blending :
Blending combines the newly generated freagment color value with color values stored in framebuffer at location(x,y)
1.4 EGL
GLES commands require rendering context and drawing surface.
Renmdering contrext stores appropriate GLES state.
Drawing surface is surface to whch primitives whill be drawn.
Drawing surrace specifies types of buffer that are required or rendering, such as color buffer, depth buffer, stencil buffer.
Drawing surface aloso specified bit depths of each of required buffers.
Perform follow tasks using EGL before any rendering :
Query displays and init
Create rendering surface. Can be categorized as on-screeen surfces or off-screen surface.
On-screen surface attached to native window system, whereas off-screen surface are pixel buffers that do not get displayed but can be used as rendering surface.
Create Rendering Context.context need to be attached to appropriate surface before rendering can actuially begin.
1.4.2 libraries and include files
GLES3.0 lib named libGLESv2.lib
EGL lib named libGL.lib
Head file:
#include
#include
第二章:Hello Triangle
Hello_Triangle.c
Typedef struct
{
//Handle to a grogram object
GLuint programObject;
}UserData;
//Create a shader object, load shader source , compile shader
GLuint LoadShader(GLenum type, const cahr* shaderSRc)
{
GLuint shader;
Glint compiled;
//create shader obj
Shader = glCreateShader(type);
If(shaer == 0) return 0;
//load shader source
glShaderSource(shader, 1, &shaderSrc, NULL);
//Compile shader
glCompileShader(shader);
//check compile status
glGetShaderiv*shdaer, GL_COMILE_STATUS, &compiled);
if(!compiled)
….
Return shader;
}
//init shader and program obj
Int init(ESContext *esContxt)
{
UserData *userData = esContext->userData;
Char vShaderStr[] =
“#version 300 es \n”
“layout(location = 0 ) in vec4 vPosition; \n”
“void main() \n”
“{ gl_Position = vPosition }\n ”
Char fShaderStr[] =
“#verfssion 300 es \n”
“precision medium float ;\n”
“out vec4 fragColor;\n”
“void main() \n”
“{ fragColor = vec4(1.0, 0.0, 0.0, 1.0) }”
GLuint vertexShader;
GLuint fragmentShader;
GLuint programobjk;
Glint linked
//Load vertex/fragment shader
vertexShader = loadShader(GL_VERTEX_SHADER, vShaderStr);
fragmentShader = LoadShader(GL_FRAGMENT_SHADER, fShaderStr);
//Create program obj
programOBject = glCreateProgram();
glAttachShader(programOBj, verttexShader);
glAttachShader(programObj, gragmentShader);
//link program
glLinkProgram(ProgramObj);
//store program obnk
userData->programobnjk = programObject;
glClearColor(0.0f, 0.0f, 0.0f ,0.0f);
return TRUE;
}
//Draw a triangle using the shader pair created in INIt()
Void Draw(ESContext * escontext)
{
UserData *userData = esContext->userData;
GLfloat vVertices[] = {
0.0f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f };
//Set viewport
glViewport(0,0,esContext->width,esContext0>height);
//Clear color buffer
glClear(GL_COLOR_BUFFER_BIT);
//Use program object
glUseProgram(userData->programObj);
//Load vertex data
glVertexAttribPointer(0,3,GL_FLOAT, GL_FLSE, 0, vVertices);
glEnableVertexAttribArtrray(0);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
2.5 Create Simple Vertex and fragment shader
In GLES 3.0, no geometry can be drawn unless a valid vetex and fragment shader have been loaded.
To do any rendering at all, OpenGLES3 program must have at least on vetex shader and one fragment shader.
Vertex shader that is given in program is simple:
Vettex shader declares one input attribute array – a four-compnent 4分量 vector向量 named vPosition. Later on , the draw function will send-in positions for each vertex顶点 that will be places in this variable.
Layout(location = 0) qualifier signifies that location of this variable is vertex attribute 0.
Every vertex shader must output a position into gl_Position variable.this variable defines position that is passed through to next stage in pipeline.
Fragment shader:
Fragment shader declears a single output variable fragColor, which is a vector of four components 4分量的向量.
Value written to this variable is what will be writeen out into the color buffer.
In this case, the sahder output a red color(1.0, 0.0, 0.0, 1.0) for all fragments. …
2.6 Compiling and loading shaders
2.7 Creating program obj and link shaders
Once app has created shader obj for vertex and fragment shader. It need to crteate program object.
Conceptually, program obj can be thought of as final linked program.
Once various shaders are compiled into shader object, they must be attached to program object and linked together before drawing.
Program obj and link is fully described in chapter 4.
To use program obj for rendering , we bind it using glUseProgram.
After calling glUseProgram with the program object handle, all subsequent rendering will occur suing the vertex and fragment shader attached to program obj.
2.8 set Viewport and clearing color buffer
First command that we execute in draw is glViewport, informs OpenGLES of origin,width,height, of 2D rendering surface that will be drawn to.
More detail in chapter 7 “Primitive Assembly and Rasterization” when we discuss coordinate sytstems and clipping.
After setting viewport, next step is to clear screen.,in ES, multiple types of buffers are involved in drawing : color, depth, stencil. Chapter 11讲。
In this example, only the color buffer is dran to . At beginning of each frame, we clear color buffer using glClear()
2.9 Loading Geometry and drawing primitive
Now that we have the color buffer cleared , viewport set, program object loaded, we need to specify geometry ofr tringle.
Vertices顶点 for the tringle are specified with three(x,y,z) coordinates in the vVertices array.
The vertex positions need to be loaded to GL and connected to vPosition attribute decleared in vertex shader.
We bound the vPosition variable to input attribute location 0.
Each attribute in vertex sahder has a location that is uniquely identified by an unsinged integer value.
To load the data into vertex attribute 0, we call the glVertexAttribPointer function, 6章讲。
We use func glDrawAttays for draws a primitive such as a triangle, line, strip. 7章讲。
EglSwapBuffer(display, surface)下章讲。
Chapter 3 , introduction to EGL
EGL provides mechanisms for following:
Communicating with native windowing system of your device
Querying available types and configurations of drawing surface
Create drawing surface
Managing rendering resoures such as texture maps纹理贴图