GLSL教程(2)【转】

7 变量类型

 

着色程序中常用的变量是uniformattribute变量。

uniform的值只能被图元修改,不能在glBegin / glEnd 间被修改。这意味着uniform不能用作顶点属性,uniform适用于在图元, , 或整个scene都不变的量。uniform变量能在顶点和片断着色器中读。

attribute可以为顶点赋值。attribute在任何时候都可以被更新。只能用于用于顶点着色器,不能用于片断着色器。

7.1 uniform变量的应用:

下面是着色器中的定义的变量:

       uniform float specIntensity;

       uniform vec4 specColor;

       uniform float t[2];

       uniform vec4 colors[3];

 

下面是opengl中的代码部分:

       GLint loc1,loc2,loc3,loc4;

       float specIntensity = 0.98;

       float sc[4] = {0.8,0.8,0.8,1.0};

       float threshold[2] = {0.5,0.25};

       float colors[12] = {0.4,0.4,0.8,1.0,

                             0.2,0.2,0.4,1.0,

                          0.1,0.1,0.1,1.0};

 

       loc1 = glGetUniformLocation(p,"specIntensity");/*给着色程序传值*/

       glUniform1f(loc1,specIntensity);

 

       loc2 = glGetUniformLocation(p,"specColor");

       glUniform4fv(loc2,1,sc);/*1*4=41必须是着色器中声明的值*/

       /*或者是:

       loc2 = glGetUniformLocation(p,"specColor");

       glUniform4f(loc2,sc[0],sc[1],sc[2],sc[3]);

       */

 

       loc3 = glGetUniformLocation(p,"t");

       glUniform1fv(loc3,2,threshold);/*2必须是着色器中声明的值*/

 

       loc4 = glGetUniformLocation(p,"colors");

       glUniform4fv(loc4,3,colors);/*3*4=123必须是着色器中声明的值*/

 

 

注意:

GLint glUniform{1,2,3,4}f(GLint location, GLsizei count, GLfloat *v); 是用浮点数赋值

GLint glUniform{1,2,3,4}fv(GLint location, GLsizei count, GLfloat *v); 用浮点数组赋值

 

7.2 attribute变量的应用:  

glBegin(GL_TRIANGLE_STRIP);

 

       glVertexAttrib1f(loc,2.0);

       glVertex2f(-1,1);

 

       glVertexAttrib1f(loc,2.0);

       glVertex2f(1,1);

 

       glVertexAttrib1f(loc,-2.0);

       glVertex2f(-1,-1);

 

       glVertexAttrib1f(loc,-2.0);

       glVertex2f(1,-1);

 

glEnd();

 

 

下面是着色器中的定义的变量:

attribute float height;

 

下面是opengl中的代码部分:

 

loc = glGetAttribLocation(p,"height");

..........................

glBegin(GL_TRIANGLE_STRIP);

 

       glVertexAttrib1f(loc,2.0);

       glVertex2f(-1,1);

 

       glVertexAttrib1f(loc,2.0);

       glVertex2f(1,1);

 

       glVertexAttrib1f(loc,-2.0);

       glVertex2f(-1,-1);

 

       glVertexAttrib1f(loc,-2.0);

       glVertex2f(1,-1);

 

glEnd();

使用数组:

        float vertices[8] = {-1,1, 1,1, -1,-1, 1,-1};

       float heights[4] = {2,2,-2,-2};

              ...

              loc = glGetAttribLocationARB(p,"height");

 

       glEnableClientState(GL_VERTEX_ARRAY);

       glEnableVertexAttribArrayARB(loc);

 

       glVertexPointer(2,GL_FLOAT,0,vertices);

       glVertexAttribPointerARB(loc,1,GL_FLOAT,0,0,heights);

8 数据类型

GLSL支持下面数据类型:

Float bool int

vec{2,3,4} 拥有 2,3, 4 个浮点数的向量

bvec{2,3,4}

ivec{2,3,4} 整型矩阵

mat2     2*2维矩阵

mat3

mat4

sampler1D - 1D纹理使用(含sample的变量均为纹理使用)

sampler2D

sampler3D

samplerCube

sampler1DShadow

sampler2DShadow

下面是变量使用时应注意的问题:

        float a,b;     // two vector (yes, the comments are like in C)

        int c = 2;             // c is initialized with 2

        bool d = true; // d is true

.

        float b = 2;   // incorrect, there is no automatic type casting

       

        float e = (float)2;// incorrect, requires constructors for type casting

       

        int a = 2;

        float c = float(a); // correct. c is 2.0

 

        vec3 f;        // declaring f as a vec3

        vec3 g = vec3(1.0,2.0,3.0); // declaring and initializing g

       

以下为允许的使用方法

        vec2 a = vec2(1.0,2.0);

        vec2 b = vec2(3.0,4.0);

       

        vec4 c = vec4(a,b) // c = vec4(1.0,2.0,3.0,4.0);

       

        vec2 g = vec2(1.0,2.0);

        float h = 3.0;

       

        vec3 j = vec3(g,h);

        mat4 m = mat4(1.0) // initializing the diagonal of the matrix with 1.0

       

        vec2 a = vec2(1.0,2.0);

        vec2 b = vec2(3.0,4.0);

       

        mat2 n = mat2(a,b); // matrices are assigned in column major order

       

        mat2 k = mat2(1.0,0.0,1.0,0.0); // all elements are specified

The declaration and initialization of structures is demonstrated below:

        struct dirlight {              // type definition

               vec3 direction;

               vec3 color;

        };

 

        dirlight d1;

        dirlight d2 = dirlight(vec3(1.0,1.0,0.0),vec3(0.8,0.8,0.4));

        vec4 a = vec4(1.0,2.0,3.0,4.0);

       

        float posX = a.x;

        float posY = a[1];

       

        vec2 posXY = a.xy;

       

        float depth = a.w

 

http://hi.baidu.com/huguosheng

 

转自:http://hi.baidu.com/luckwxx623/blog/item/880bc2df0eb7501a632798ea.html

相关文章
相关标签/搜索