Open GLES 2.0 android中的漫反射光模型

时间:2017-05-23 02:02:19

标签: android opengl-es opengl-es-2.0

为什么在Openg gles 2.0中存在阶梯式而非逐渐的漫反射光照?

 String fragmentShaderCode =

        "precision highp float;" +

                "uniform vec4 u_color;" +
                "varying vec3 v_pos;" +
                "varying vec3 v_normal;" +

                "void main() {" +
                "vec3 u_camera=vec3(0.0,0.0,0.0);"+
                "vec3 u_lightPosition=vec3(0.0,0.0,0.0);"+
                "vec3 n_normal=normalize(v_normal);"+
                "vec3 lightvector = normalize(u_lightPosition - v_pos);"+


                "float ambient=0.1;"+
                "float k_diffuse=0.5;"+
                "float cos=(n_normal.x*lightvector.x+n_normal.y*lightvector.y+n_normal.z*lightvector.z)/(length(n_normal)*length(lightvector));"+
                "if(cos<0.0)cos=0.0;"+
                "float an=acos(cos)*(180.0/3.14);"+
                "float diffuse =k_diffuse* (1.0-an/90.0);"+

enter image description here                     &#34; gl_FragColor = u_color *(漫反射+环境);&#34; +

                "}";

我做了三角形,以便形成具有法线的平面(0.0,0.0,1.0)。问题是光逻辑通常是正确的,但具有陡峭的视觉效果。所以这是我的三角形:   static float triangleCoords [] = {             //按逆时针顺序:             5.0f,5.0f,0.0f,// top            -5.0f,5.0f,0.0f,             -5.0f,-5.0f,0.0f,

        5.0f,  5.0f, 0.0f,
        -5.0f,  -5.0f, 0.0f,
        5.0f,  -5.0f, 0.0f,
       // bottom right
};
static float norms[] = {
        // in counterclockwise order:
        0.0f,  0.0f, 1.0f,   // top
        0.0f,  0.0f, 1.0f,
        0.0f,  0.0f, 1.0f,

        0.0f,  0.0f, 1.0f,  // top
        0.0f,  0.0f, 1.0f,
        0.0f,  0.0f, 1.0f
};

这是我的顶点着色器:`private final String vertexShaderCode =

        "uniform mat4 prMat;" +
                "uniform mat4 camMat;" +

                "attribute vec3 a_pos;" +
                "varying vec3 v_pos;" +
                "attribute vec3 a_normal;" +
                "varying vec3 v_normal;" +



                "void main() {" +
                "vec4 ver=camMat*vec4(a_pos,1.0);"+

                "v_pos=ver.xyz;"+
                "vec4 v_n=vec4(a_normal,1.0);"+
                "v_normal=normalize(v_n.xyz);"+
                "  gl_Position = prMat*camMat*vec4(a_pos,1.0);" +
                "}";`

来自渲染的一些代码:         Matrix.translateM(camMat,0,0.0f,0.0F,-7.0f);         mTriangle.draw(mProjectionMatrix,camMat,0);`

enter code here

0 个答案:

没有答案