OpenGL ES Shader 多重纹理

一、着色器创建

OpenGL ES Shader 多重纹理_第1张图片

二、顶点着色器

2.1 内建输入参数:

  • gl_Vertex
  • gl_Normal
  • gl_Color
  • gl_SecondaryColor
  • gl_TexCoordn
  • gl_FogCoord
2.2 用户输入参数:(带修饰符)

  • uniform
  • attribute
2.3 用户数出参数:(带修饰符)

  • varying
2.4 内建输出参数:

  • gl_Position
  • gl_FrontColor
  • gl_BackColor
  • gl_FrontSecondaryColor
  • gl_BackSecondaryColor
  • gl_TexCoord[n]
  • gl_FogFragColor
  • gl_PointSize
  • gl_ClipVertex
2.5 例子:

"uniform highp mat4 inMvpMatrix;\
attribute highp vec3 inVertex;\
attribete mediump vec2 inTexCoord;\
varying mediump vec2 outTexCoord;\
void main ()\
{\
   gl_Position = (mvpMatrix * vec4(inVertex, 1.0));\
}\n";

三、片段着色器

3.1 内建输入参数:

  • gl_FragCoord
  • gl_Color
  • gl_SecondaryColor
  • gl_TexCorrd[n]
  • gl_FogFragCoord
  • gl_FrontFacing
3.2 用户输入参数:(带修饰符)

  • uniform
  • varying
3.3 内建输出参数:(带修饰符)

  • gl_FragColor
  • gl_FragDepth
  • gl_FragData[n]
3.4 例子:

"uniform mediump vec4 inColor;\
uniform sampler2D inTexture;\
varying mediump vec2 outTexCoord;\
void main ()\
{\
   gl_FragColor = inColor;\
}\n";

四、代码实现匹配

4.1 用户输入参数绑定:

  • uniform修饰符采用:
mvpMatrix = glGetUniformLocation(program, "inMvpMatrix");  
color = glGetUniformLocation(program, "inColor");
texture = glGetUniformLocation(Program, "inTexture");
  • attribue修饰符采用:
定义索引值:index_vertex_array=0; index_texcoord_array=1; index_normal_array=2..... 以此类推
glBindAttribLocation(program, index_vertex_array, "inVertex");
glBindAttribLocation(program, index_texcoord_array, "inTexCoord");

4.2 纹理处理

1)纹理id绑定到纹理单元中:

glActiveTextrue(unitIndex); //纹理单元从0-7,共计8个单元

2)绑定纹理ID到激活的纹理单元里

glBindTexture(GL_TEXTURE_2D, texID);

3)指定纹理单元和shader输出参数匹配

glUniform1i(texture, unitIndex);

4.3 顶点数据传递

1)顶点数据

glVertexAttribPointer(index_vertex_array, size, type, normalized, stride, VertexPtr);

2)纹理坐标数据

glVertexAttribPointer(index_texcoord_array, size, type, normalized, stride, TexCoordPtr);

3)法向量数据

glVertexAttribPointer(index)normal_array, size, type, normalized, stride, NormalPtr);

4.4 调用绘制接口

1)vbo,ibo方式:

glDrawElements(mode, count, type, pIdx);

2)顶点数组方式:

glDrawArrays(mode, first, count);


五、实现多重纹理

1)fragment shader定义多个纹理输入参数

texture1 = glGetUniformLocation(Program, "inTexture1");
texture2 = glGetUniformLocation(Program, "inTexture2");

2)激活纹理单元,匹配对应纹理ID

glActiveTextrue(0); //纹理单元从0-7,共计8个单元
glBindTexture(GL_TEXTURE_2D, texID1);
glUniform1i(texture1, 0);

glActiveTextrue(1); //纹理单元从0-7,共计8个单元
glBindTexture(GL_TEXTURE_2D, texID2);
glUniform1i(texture2, 1);

3)vertex shader定义多个纹理坐标输入参数

//index_texcoord_array1=4; index_texcoord_array2=5;
glBindAttribLocation(program, index_texcoord_array1, "inTexCoord1");
glBindAttribLocation(program, index_texcoord_array2, "inTexCoord2");

4)传入纹理坐标(TexcoordPtr1, TexcoordPtr2)

glVertexAttribPointer(index_texcoord_array1, size, type, normalized, stride,TexcoordPtr1);
glVertexAttribPointer(index_texcoord_array2, size, type, normalized, stride,TexcoordPtr2);

5) fragment shader中对于纹理坐标进行计算或处理

你可能感兴趣的:(OpenGL)