OpenGL ES 3.0 Programming Guide 1-3

一,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 <EGL/egl.h>

                #include <GLES3/gl3.h>

 

 

 

 

第二章: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纹理贴图

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenGL® ES™ is the industry’s leading software interface and graphics library for rendering sophisticated 3D graphics on handheld and embedded devices. The newest version, OpenGL ES 3.0, makes it possible to create stunning visuals for new games and apps, without compromising device performance or battery life. In the OpenGL ES 3.0 Programming Guide, 2nd Edition, the authors cover the entire API and Shading Language. They carefully introduce OpenGL ES 3.0 features such as shadow mapping, instancing, multiple render targets, uniform buffer objects, texture compression, program binaries, and transform feedback. Through detailed, downloadable C-based code examples, you’ll learn how to set up and program every aspect of the graphics pipeline. Step by step, you’ll move from introductory techniques all the way to advanced per-pixel lighting and particle systems. Throughout, you’ll find cutting-edge tips for optimizing performance, maximizing efficiency with both the API and hardware, and fully leveraging OpenGL ES 3.0 in a wide spectrum of applications. All code has been built and tested on iOS 7, Android 4.3, Windows (OpenGL ES 3.0 Emulation), and Ubuntu Linux, and the authors demonstrate how to build OpenGL ES code for each platform. Coverage includes EGL API: communicating with the native windowing system, choosing configurations, and creating rendering contexts and surfaces Shaders: creating and attaching shader objects; compiling shaders; checking for compile errors; creating, linking, and querying program objects; and using source shaders and program binaries OpenGL ES Shading Language: variables, types, constructors, structures, arrays, attributes, uniform blocks, I/O variables, precision qualifiers, and invariance Geometry, vertices, and primitives: inputting geometry into the pipeline, and assembling it into primitives 2D/3D, Cubemap, Array texturing: creation, loading, and rendering; texture wrap modes, filtering, and formats; compressed textures, sampler objects, immutable textures, pixel unpack buffer objects, and mipmapping Fragment shaders: multitexturing, fog, alpha test, and user clip planes Fragment operations: scissor, stencil, and depth tests; multisampling, blending, and dithering Framebuffer objects: rendering to offscreen surfaces for advanced effects Advanced rendering: per-pixel lighting, environment mapping, particle systems, image post-processing, procedural textures, shadow mapping, terrain, and projective texturing Sync objects and fences: synchronizing within host application and GPU execution This edition of the book includes a color insert of the OpenGL ES 3.0 API and OpenGL ES Shading Language 3.0 Reference Cards created by Khronos. The reference cards contain a complete list of all of the functions in OpenGL ES 3.0 along with all of the types, operators, qualifiers, built-ins, and functions in the OpenGL ES Shading Language. Table of Contents Chapter 1. Introduction to OpenGL ES 3.0 Chapter 2. Hello Triangle: An OpenGL ES 3.0 Example Chapter 3. An Introduction to EGL Chapter 4. Shaders and Programs Chapter 5. OpenGL ES Shading Language Chapter 6. Vertex Attributes, Vertex Arrays, and Buffer Objects Chapter 7. Primitive Assembly and Rasterization Chapter 8. Vertex Shaders Chapter 9. Texturing Chapter 10. Fragment Shaders Chapter 11. Fragment Operations Chapter 12. Framebuffer Objects Chapter 13. Sync Objects and Fences Chapter 14. Advanced Programming with OpenGL ES 3.0 Chapter 15. State Queries Chapter 16. OpenGL ES Platforms Appendix A. GL_HALF_FLOAT Appendix B. Built-In Functions Appendix C. ES Framework API Book Details Title: OpenGL ES 3.0 Programming Guide, 2nd Edition Author: Aaftab Munshi, Budirijanto Purnomo, Dan Ginsburg, Dave Shreiner Length: 560 pages Edition: 2 Language: English Publisher: Addison-Wesley Professional Publication Date: 2014-03-10 ISBN-10: 0321933885 ISBN-13: 9780321933881

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值