Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cocos2dx shader 赏析 #8

Open
zhishu520 opened this issue Dec 3, 2018 · 1 comment
Open

cocos2dx shader 赏析 #8

zhishu520 opened this issue Dec 3, 2018 · 1 comment

Comments

@zhishu520
Copy link
Owner

zhishu520 commented Dec 3, 2018

GLProgram::SHADER_NAME_POSITION_GRAYSCALE

scalegray

// vert
attribute vec4 a_position;
attribute vec2 a_texCoord;
attribute vec4 a_color;

#ifdef GL_ES
varying lowp vec4 v_fragmentColor;
varying mediump vec2 v_texCoord;
#else
varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
#endif

void main()
{
    gl_Position = CC_PMatrix * a_position;
    v_fragmentColor = a_color;
    v_texCoord = a_texCoord;
}

//frag
#ifdef GL_ES
precision mediump float;
#endif

varying vec4 v_fragmentColor;
varying vec2 v_texCoord;

void main(void)
{
    vec4 c = texture2D(CC_Texture0, v_texCoord);
    c = v_fragmentColor * c;
    gl_FragColor.xyz = vec3(0.2126*c.r + 0.7152*c.g + 0.0722*c.b);
    gl_FragColor.w = c.w;
}
@zhishu520 zhishu520 reopened this Dec 8, 2018
@zhishu520 zhishu520 changed the title cocos2dx 定时器的源码分析 cocos2dx shader 赏析 Dec 9, 2018
@zhishu520
Copy link
Owner Author

zhishu520 commented Dec 9, 2018

Shader 知识

变量类型 uniform attribute varying

  • uniform变量是外部程序传递给vertex和fragment的变量, 不能被shader修改, 一般用来表示:变换矩阵,材质,光照参数和颜色等信息。 一般用函数glBindAttribLocation()来绑定每个attribute变量的位置,然后用函数glVertexAttribPointer()为每个attribute变量赋值。
  • attribute变量是只能在vertex shader中使用的变量, 一般用attribute变量来表示一些顶点的数据,如:顶点坐标,法线,纹理坐标,顶点颜色等。 在application中,一般用函数glBindAttribLocation()来绑定每个attribute变量的位置,然后用函数glVertexAttribPointer()为每个attribute变量赋值。
  • varying变量是vertex和fragment shader之间做数据传递用的。一般vertex shader修改varying变量的值,然后fragment shader使用该varying变量的值。因此varying变量在vertex和fragment shader二者之间的声明必须是一致的。application不能使用此变量。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant