glsl - Vertex and Fragment shaders for lighting effect in OpenGL ES20 -
i want add lighting effect in models via shaders. found out there vertex , fragment shaders make work on 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 working in opengl es2, because developing android app. , seems in-built variable gl_frontlightmodelproduct not available gles20, because having compilation fails in line.
my question therefore is: how can modify above shaders make them work in opengl es20 context?
gl_frontlightmodelproduct.scenecolor
gives ambient colour of scene can 0 if want area not affected light black. can replace vec4(0.0, 0.0, 0.0, 1.0);
you should remove these variables , send them uniforms.
gl_modelviewmatrix
(send uniform)gl_vertex
(read attributes)gl_normalmatrix
(send uniform)gl_normal
(read attributes)
or instead of fighting in converting opengl shader, can search simple opengl es 2.0 shaders
Comments
Post a Comment