0

I am trying to learn webGL, tried to implemement phong shading following this sample code on the link http://voxelent.com/html/beginners-guide/chapter_3/ch3_Sphere_Phong.html

I get two errors on shader compilation, and consequently there is no moon displayed, which is supposed to be display as i am following Lesson11 of GitHUB where they make sphere from rectangles , the errors i got are :

ERROR: 0:49: '*' :  wrong operand types  no operation '*' exists that takes a left-hand operand of type 'mediump 3-component vector of float' and a right operand of type 'mediump 4-component vector of float' (or there is no acceptable conversion)

And my full code is :

<script id="shader-fs" type="x-shader/x-fragment">
        precision mediump float;
        varying vec2 vTextureCoord;
        varying vec3 vLightWeighting;
        uniform sampler2D uSampler;
        uniform float uShininess;        //shininess
        uniform vec3 uLightDirection;  //light direction

        uniform vec4 uLightAmbient;      //light ambient property
        uniform vec4 uLightDiffuse;      //light diffuse property
        uniform vec4 uLightSpecular;     //light specular property

        uniform vec4 uMaterialAmbient;  //object ambient property
        uniform vec4 uMaterialDiffuse;   //object diffuse property
        uniform vec4 uMaterialSpecular;  //object specular property

        varying vec3 vNormal;
        varying vec3 vEyeVec;

        void main(void) 
        {
          vec3 L = normalize(uLightDirection);
        vec3 N = normalize(vNormal);

        //Lambert's cosine law
        float lambertTerm = dot(N,-L);

        //Ambient Term
        vec4 Ia = uLightAmbient * uMaterialAmbient;

        //Diffuse Term
        vec4 Id = vec4(0.0,0.0,0.0,1.0);

        //Specular Term
        vec4 Is = vec4(0.0,0.0,0.0,1.0);

        if(lambertTerm > 0.0) //only if lambertTerm is positive
        {
              Id = uLightDiffuse * uMaterialDiffuse * lambertTerm; //add diffuse term
              vec3 E = normalize(vEyeVec);
              vec3 R = reflect(L, N);
              float specular = pow( max(dot(R, E), 0.0), uShininess);
              Is = uLightSpecular * uMaterialSpecular * specular; //add specular term
        }
        //Final color
           vec4 finalColor =Ia + Id + Is;
           finalColor.a = 1.0;
           vec4 textureColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));
           gl_FragColor = vec4(textureColor.rgb * finalColor, textureColor.a);
        }
    </script>

    <script id="shader-vs" type="x-shader/x-vertex">
        attribute vec3 aVertexPosition;
        attribute vec3 aVertexNormal;
        attribute vec2 aTextureCoord;
        uniform mat4 uMVMatrix;
        uniform mat4 uPMatrix;
        uniform mat3 uNMatrix;
        uniform vec3 uAmbientColor;
        uniform vec3 uLightingDirection;
        uniform vec3 uDirectionalColor;
        uniform bool uUseLighting;
        varying vec2 vTextureCoord;
        varying vec3 vLightWeighting;

        varying vec3 vNormal;
        varying vec3 vEyeVec;

        void main(void) 
        {
        //Transformed vertex position
        vec4 vertex= uMVMatrix * vec4(aVertexPosition, 1.0);

        //Transformed normal position
        vNormal   = vec3(uNMatrix * vec4(aVertexNormal, 1.0));

        //Vector Eye
        vEyeVec = -vec3(vertex.xyz);
        gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
        vTextureCoord = aTextureCoord;       
        }
    </script>   

EDIT2:

  <script id="shader-fs" type="x-shader/x-fragment">
        precision mediump float;
        varying vec2 vTextureCoord;
        varying vec3 vLightWeighting;
        uniform sampler2D uSampler;
        uniform float uShininess;        //shininess
        uniform vec3 uLightDirection;  //light direction

        uniform vec4 uLightAmbient;      //light ambient property
        uniform vec4 uLightDiffuse;      //light diffuse property
        uniform vec4 uLightSpecular;     //light specular property

        uniform vec4 uMaterialAmbient;  //object ambient property
        uniform vec4 uMaterialDiffuse;   //object diffuse property
        uniform vec4 uMaterialSpecular;  //object specular property

        varying vec3 vNormal;
        varying vec3 vEyeVec;

        void main(void)
        {
        vec3 L = normalize(uLightDirection);
        vec3 N = normalize(vNormal);

        //Lambert's cosine law
        float lambertTerm = dot(N,-L);

        //Ambient Term
        vec4 Ia = uLightAmbient * uMaterialAmbient;

        //Diffuse Term
        vec4 Id = vec4(0.0,0.0,0.0,1.0);

        //Specular Term
        vec4 Is = vec4(0.0,0.0,0.0,1.0);

        if(lambertTerm > 0.0) //only if lambertTerm is positive
        {
        Id = uLightDiffuse * uMaterialDiffuse * lambertTerm; //add diffuse term
        vec3 E = normalize(vEyeVec);
        vec3 R = reflect(L, N);
        float specular = pow( max(dot(R, E), 0.0), uShininess);
        Is = uLightSpecular * uMaterialSpecular * specular; //add specular term
        }
        //Final color
        vec4 finalColor =Ia + Id + Is;
        finalColor.a = 1.0;
        vec4 textureColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));
        gl_FragColor = vec4(textureColor.rgb, textureColor.a)+finalColor;
        }
    </script>

    <script id="shader-vs" type="x-shader/x-vertex">
        attribute vec3 aVertexPosition;
        attribute vec3 aVertexNormal;
        attribute vec2 aTextureCoord;
        uniform mat4 uMVMatrix;
        uniform mat4 uPMatrix;
        uniform mat3 uNMatrix;
        uniform vec3 uAmbientColor;
        uniform vec3 uLightingDirection;
        uniform vec3 uDirectionalColor;
        uniform bool uUseLighting;
        varying vec2 vTextureCoord;
        varying vec3 vLightWeighting;

        varying vec3 vNormal;
        varying vec3 vEyeVec;

        void main(void)
        {
        //Transformed vertex position
        vec4 vertex= uMVMatrix * vec4(aVertexPosition, 1.0);

        //Transformed normal position
        vNormal   = vec3(uNMatrix * vec3(aVertexNormal));

        //Vector Eye
        vEyeVec = -vec3(vertex.xyz);
        gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
        vTextureCoord = aTextureCoord;
        }
    </script>   

How to display moon with phong shading effects. Could someone please help me ?

struggling
  • 535
  • 1
  • 10
  • 25

1 Answers1

1

The error message is unambiguous: You try to multiply a vec3 by a vec4, which obvoiusly does not make sense at all. It also tells you that the error is in line 49 of the shader:

gl_FragColor = vec4(textureColor.rgb * finalColor, textureColor.a);
//                                     ^ finalColor is a vec4!

You probably meant to tuse finalColor.rgb here.

derhass
  • 43,833
  • 2
  • 57
  • 78
  • I ahve already solved the problem, but thanks for trying to help me. – struggling Oct 20 '15 at 07:17
  • I done something liek this "" vec4 textureColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t)); gl_FragColor = vec4(textureColor.rgb, textureColor.a)+finalColor; , Also could you please check the logic of phong shading in my code ? I have no errors now in my edit2 part but it do not light very well. – struggling Oct 20 '15 at 07:20