简体   繁体   English

顶点和片段着色器,用于OpenGL ES20中的照明效果

[英]Vertex and Fragment shaders for lighting effect in OpenGL ES20

I want to add a very simply lighting effect in my models via shaders. 我想通过着色器在我的模型中添加一个非常简单的照明效果。 I found out there a vertex and a fragment shaders that make the work on OpenGL: 我发现那里有一个顶点和一个片段着色器,它们可以在OpenGL上工作:

static const char* vertSource = {
    "uniform vec3 lightPosition;\n"
    "varying vec3 normal, eyeVec, lightDir;\n"
    "void main()\n"
    "{\n"
    "    vec4 vertexInEye = gl_ModelViewMatrix * gl_Vertex;\n"
    "    eyeVec = -vertexInEye.xyz;\n"
    "    lightDir = vec3(lightPosition - vertexInEye.xyz);\n"
    "    normal = gl_NormalMatrix * gl_Normal;\n"
    "    gl_Position = ftransform();\n"
    "}\n"
};

static const char* fragSource = {
    "uniform vec4 lightDiffuse;\n"
    "uniform vec4 lightSpecular;\n"
    "uniform float shininess;\n"
    "varying vec3 normal, eyeVec, lightDir;\n"
    "void main (void)\n"
    "{\n"
    "  vec4 finalColor = gl_FrontLightModelProduct.sceneColor;\n"
    "  vec3 N = normalize(normal);\n"
    "  vec3 L = normalize(lightDir);\n"
    "  float lambert = dot(N,L);\n"
    "  if (lambert > 0.0)\n"
    "  {\n"
    "    finalColor += lightDiffuse * lambert;\n"
    "    vec3 E = normalize(eyeVec);\n"
    "    vec3 R = reflect(-L, N);\n"
    "    float specular = pow(max(dot(R, E), 0.0), shininess);\n"
    "    finalColor += lightSpecular * specular;\n"
    "  }\n"
    "  gl_FragColor = finalColor;\n"
    "}\n"
};

The problem is that I am working in OpenGL ES2, because I am developing an Android app. 问题是我在OpenGL ES2中工作,因为我正在开发一个Android应用程序。 And it seems that the in-built variable gl_FrontLightModelProduct is not available for GLES20, because I am having compilation fails in this line. 似乎内置变量gl_FrontLightModelProduct不适用于GLES20,因为我在这一行中编译失败。

My question therefore is: How I can modify the above shaders to make them work in a OpenGL ES20 context? 因此,我的问题是:如何修改上述着色器以使其在OpenGL ES20上下文中工作?

gl_FrontLightModelProduct.sceneColor gives the Ambient colour of the scene which can be 0 if you want the area which is not affected by light to be fully black. gl_FrontLightModelProduct.sceneColor给出场景的环境颜色,如果您希望不受光线影响的区域为全黑,则可以为0。 You can replace that with a vec4(0.0, 0.0, 0.0, 1.0); 您可以将其替换为vec4(0.0, 0.0, 0.0, 1.0);

You should also remove these variables and send them as uniforms. 您还应该删除这些变量并将其作为制服发送。

  1. gl_ModelViewMatrix (send as uniform) gl_ModelViewMatrix (统一发送)
  2. gl_Vertex (read this from attributes) gl_Vertex (从属性中读取)
  3. gl_NormalMatrix (send as uniform) gl_NormalMatrix (统一发送)
  4. gl_Normal (read this from attributes) gl_Normal (从属性中读取)

Or instead of fighting in converting OpenGL shader, you can search for Simple OpenGL ES 2.0 shaders 或者,您可以搜索简单的OpenGL ES 2.0着色器,而不必为转换OpenGL着色器而战

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM