opengles实现点光源

opengles实现点光源_第1张图片

核心shader

       const char* vs  =   
            {
                "attribute vec3  inVertex; "
                "attribute vec3  inNormal;"
                "attribute vec2  inTexCoord;"

                "uniform mat4  MVPMatrix;"
                "uniform mat4  ModelView;"
                "uniform mat3  ModelViewIT;"
                "uniform vec3  LightPosition;"
                "uniform vec3  LightDirection;"
                "uniform vec3  LightColor;"

                "varying vec2  TexCoord;"
                "varying vec3  DiffuseLight;"
                "varying vec3  SpecularLight;"
                "const  float  cShininess = 16.0;"

                "void main(void)"
                "{"
                    "vec3 normal = normalize(ModelViewIT * inNormal);"

                    "vec3 ecPosition = vec3(ModelView * vec4(inVertex, 1.0));"

                    "DiffuseLight   =   vec3(0.0);"
                    "SpecularLight  =   vec3(0.0);"
                    "vec3 lightDir  =   -normalize(ecPosition - LightPosition);"

                    "vec3    eyeDir =   -normalize(ecPosition);"
                    "float   NdotL  =   max(dot(normal, lightDir), 0.0);"
                    "DiffuseLight   +=  NdotL * LightColor;"

                    "if (NdotL > 0.0)"
                    "{"
                        "vec3 halfVector = normalize(lightDir + eyeDir);"
                        "float NdotH = max(dot(normal, halfVector), 0.0);	"	
                        "float specular = pow(NdotH, cShininess);"
                        "SpecularLight += specular * LightColor;"
                    "}	"

                    "gl_Position    =   MVPMatrix * vec4(inVertex,1);"

                    "TexCoord       =   inTexCoord;"
                "}"

            };
            const char* ps  =   
            {
                "precision  lowp float; "
                "uniform sampler2D  sTexture;"

                "varying vec2  TexCoord;"
                "varying vec3  DiffuseLight;"
                "varying vec3  SpecularLight;"

                "void main()"
                "{"
                    "vec3 texColor  = vec3(texture2D(sTexture, TexCoord));"
                    "vec3 color = (texColor * DiffuseLight) + SpecularLight;"
                    "gl_FragColor = vec4(color, 1.0);"
                "}"
            };


你可能感兴趣的:(opengles实现点光源)