GLSL ---- 基础语法


变量和数据类型

//布尔类型. true,false;

bool bDone = false;

//整形数据类型

int iValue = 42;

//无符号整型 

uint uiValue = 3929u; 

//浮点型

float fValue = 3.14159f

向量数据类型

//声明向量:4分量的float类型

vec4 V1;

//声明并对向量进行构造

vec4 V2 = vec4(1,2,3,4);

//向量运算,赋值/加/标量相乘

vec4 v;

vec4 vOldPos = vec4(1,2,3,4);

vec4 vOffset = vec4(1,2,3,4); 

v = vOldPos + vOffset;

v = vNewPos;

v += vec4(10,10,10,10);

v = vOldPos * vOffset;

v *= 5;

//向量中元素获取和赋值:xyz

v1.x = 3.0f

v1.xy = vec2(3.0f,4.0f);

v1.xyz = vec3(3,0f,4,0f,5.0f);

//通过rgba赋值

v1.r = 3.0f;

v1.rgba = vec4(1.0f,1.0f,1.0f,1.0f);

//通过纹理坐标赋值 

v1.st = vec2(1.0f,0.0f); 

//错误:混值赋值

v1.st = v2.xt; //不可以 

v1.st = v2.xy; //可以,不建议

//多个元素交换

v1.rgba = v2.bgra;

v2.bgra = v1.rgba; 

//单个之间赋值

v1.r = v2.b;

v1.g = v2.g;

v1.b = v2.r;

v1.a = v2.a;

//单个之间赋值

v1.x = vOtherVerex.x +5.0f;

v1.y = vOtherVerex.y +4.0f;

v1.z = vOtherVerex.z +3.0f;

v1.xyz = vOtherVerex.xyz + vec3(5.0f,4.0f,3.0f);


矩阵

//创建

mat4 m1,m2,m3;

//运算构造

/// 单位矩阵

mat4 m2 = mat4(1.0f,0.0f,0.0f,0.0f

                             0.0f,1.0f,0.0f,0.0f,

                             0.0f,0.0f,1.0f,0.0f,

                              0.0f,0.0f,0.0f,1.0f);

mat4 m4 = mat4(1.0f); 

/// 构造

mat4 m3 = mat4(0.5,0.5,0.5,0.5,

                             0.5,0.5,0.5,0.5,

                             0.5,0.5,0.5,0.5,

                             0.5,0.5,0.5,0.5)

m1 = m2 * m3;

数组

float floatArray[4];

vec4 vecArray[2]; 

float a[4] = float[](1.0,2.0,3.0,4.0);

vec2 c[2] = vec2[2](vec2(1.0,2.0),vec2(3.0,4.0));


结构体:

 struct myStruct{

    vec4 color;

    float start;

    float end;

}fogVar;

fogVar = myStruct(vec4(1.0,0.0,0.0,1.0),0.5,2.0);

vec4 color = fogVar.color;

float start = fogVar.start;

函数:不允许递归

定义函数的3个修饰符

in:默认修饰符,传入韩式中,函数不能对其进行修改

inout:可以进行修改

out : 函数返回值,可以进行修改


//常量

Const float zero = 0.0; 

vec4 myFunc(inout float myfloa,out vec4 m1, mat4 m2){

vec4 diffuse(vec3 normal ,vec3 light, vec4 baseColor)

{

   return baseColor * dot(normal,light);

}

你可能感兴趣的:(GLSL ---- 基础语法)