OpenGL ES variables, structures, statements, functions, precision

Here Insert Picture Description

variable

void

Denotes empty, without a function return value. Vertex shader, such as main functions:

void main() {
    gl_Position = vPosition;
}

float、int、bool

Represent floating point, integer, boolean. Various types of variables defined as follows:

float f = 1.0;
int i =0;
bool b = true;

vec2、vec3、vec4

2,3,4 float type vectors comprise respectively. Define the variable as follows:

vec2 v2 = vec2(1.0, 0.0);
vec3 v3 = vec3(1.0, 0.0, 0.5);
vec4 v4 = vec4(1.0, 0.0, 0.5,0.5);

If only one parameter, the other vector values ​​will use this value, for example to a value of 1.0 vec4:

vec4 v5 = vec4(1.0);

Equivalent to:

vec4 v5 = vec4(1.0,1.0,1.0,1.0);

Note that the parameter vector is provided to only one or a corresponding number of vectors, such as vec4 not provide the type of two parameters:

vec4 v6 = vec4(1.0,1.0);

Vec4 above to provide two parameters wording is wrong.
And built-in variables gl_Position gl_FragColor vec4 type is frequently used, the fragment shader, color provided:

gl_FragColor = vec4(1.0,0.0,0.0,1.0);

ivec2, ivec3, ivec4 and vec2, vec3, as vec4 usage, the difference is ivec parameter is of type int.
bvec2, bvec3, bvec4 and vec2, vec3, as vec4 usage, the difference is bvec parameter is the bool type.

Vector components

Get vector components in two ways:

  • Through . "" Symbol obtaining, for the vector components {x, y, z, w }, {r, g, b, a} or S {, T, R & lt, Q} . {x, y, z, w } is the position of the associated component, {r, g, b, a} is the color-related component, {s, t, r, q} related component texture coordinates, but (x, y, z, w), ( r, g, b, a) , and (s, t, r, q ) can not mix.
  • Acquired by the array subscript, e.g. v2 [0].
    Usage is as follows:
vec4 v4 = vec4(1.0, 0.0, 0.5,0.5);
float a = v4.x;
vec2 v2 = v4.xy;
vec4 v5 = vec4(v4.x,v4.y,v4.z,v4.w);
vec4 color = vec4(v4.r,v4.g,v4.b,v4.a);

mat2, mat3, mat4

Respectively 2x2,3x3,4x4 floating-point matrix. If only one parameter value as the value of the diagonal matrix, i.e. matrix, such mat4 (1.0), is a 4x4 matrix.
The matrix may be defined as follows:

mat3 m3 = mat3(1.0, 0.0, 0.0,  // 第一列
                   0.0, 1.0, 0.0,  // 第二列
                   0.0, 1.0, 1.0); // 第三列

sampler2D、samplerCube、samplerExternalOES

  • sampler2D: 2D texture, it can be used to draw pictures.
  • samplerCube: cube texture.
  • samplerExternalOES: Android terminal for rendering video, or a camera preview, using samplerExternalOES need to add in the shader #extension GL_OES_EGL_image_external head: the require, the use of the following:
#extension GL_OES_EGL_image_external : require
uniform samplerExternalOES u_Texture;

Structure

Allow statement structure, and c language type, the following statement in GLSL:

struct myStruct
{
  vec4 color;
  vec4 position;
} myVertex;

It declares a variable of type myStruct myVertex. Initialized as follows:

myVertex = myStruct(vec4(0.0, 1.0, 0.0, 0.0),vec4(0.5, 0.5,0.5, 0.5));

One correspondence to the type and structure.
Get the value of the structure as follows:

vec4 color = myVertex.color;
vec4 position = myVertex.position;

Basic Statements

if-else

if-else conditional statement is used as follows:

if (color.a < 0.25) {
	gl_FragColor = vec4(1,0,0,1);
} else {
	gl_FragColor = vec4(1,0,0,color.a);
}

for loop

When you use a for loop, loop variable must be known at compile time, used as follows:

float sum = 0;
for (int i = 0; i < 10; i++) {
	sum += i;
}

The first typical error usage:

float myArr[4];
float sum = 0;
for (int i = 0; i < 3; i++) {
	sum += myArr[i]; 
}

[] Can only be constant or uniform variables, not the number of integer variables (such as: i, j, k).

The second typical error usage:

uniform int loopIter;
for (int i = 0; i < loopIter; i++) {
	sum += i;
}

The above syntax error, the value of the loop variable loopIter must be known at compile time.

function

C language functions and usage is basically the same, can not be called recursively and must declare the return type in GLSL, if no return value is used void. Usage is as follows:

vec4 getPosition(float a){
    return vec4(0.0,0.0,0.0,a);
}

void main(){

}

Precision specifier

In GLSL into the low precision specifier (lowp), middle (mediump), high (HighP) accuracy. Can run more efficiently when using low precision, of course, if reasonable accuracy may be a problem of distortion, have encountered problems photographed black side of the project is due to accuracy problems.

Specifies default precision mode as follows:

precision mediump float;

If the precision specifier unused variables will use this default value is used as follows:

//指定精度
highp vec4 position;
//默认精度
vec4 color;

Read More:

Published 123 original articles · won praise 68 · Views 300,000 +

Guess you like

Origin blog.csdn.net/mengks1987/article/details/104104990