-2

I have variable in vertex shader "uIsTeapot".

uniform float uIsTeapot;

Vertex shader work with it very well, but fragment shader don't see it. If I use

if (uIsTeapot == 0.0) 

then an error occured

"uIsTeapot": undeclared identifier

but I defined it in vertex shader. If I define uIsTeapot in both shaders as uniform, then program say "Could not initialise shaders" since program don't pass veryfication

if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
        alert("Could not initialise shaders");
}

EDITED: I add mediump to variables and now program compiled without errors, but result is one object on screen, but I draw two object.

Vertex shader

attribute vec3 aVertexPosition;
attribute vec3 aVertexNormal;
attribute vec2 aTextureCoord;

attribute vec4 aVertexColor;

uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
uniform mat3 uNMatrix;

uniform vec3 uAmbientColor;
uniform vec3 uPointLightingLocation;
uniform vec3 uPointLightingColor;

uniform mediump float uIsTeapot;
//varying float vIsTeapot;

varying vec2 vTextureCoord;
varying vec3 vLightWeighting;

varying vec4 vColor;

void main(void) {

    if (uIsTeapot == 1.0) {
        vec4 mvPosition = uMVMatrix * vec4(aVertexPosition, 1.0);
        gl_Position = uPMatrix * mvPosition;

        vec3 lightDirection = normalize(uPointLightingLocation - mvPosition.xyz);
        vec3 transformedNormal = uNMatrix * aVertexNormal;
        float directionalLightWeighting = max(dot(transformedNormal, lightDirection), 0.0);
        directionalLightWeighting = 100.0;
        vLightWeighting = uAmbientColor + uPointLightingColor * directionalLightWeighting;
        vTextureCoord = aTextureCoord;
    }
    if (uIsTeapot == 0.0) {
        gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
        vColor = aVertexColor;
    } }

Fragment shader

precision mediump float;

varying vec2 vTextureCoord;
varying vec3 vLightWeighting;

varying vec4 vColor;

uniform sampler2D uSampler;

uniform mediump float uIsTeapot;
//varying float vIsTeapot;

void main(void) {

    if (uIsTeapot == 1.0) {
        vec4 textureColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));
        gl_FragColor = vec4(textureColor.rgb * vLightWeighting, textureColor.a);
    } else {
        gl_FragColor = vColor;
    }


}
Log
  • 433
  • 4
  • 19
  • "program don't work right" is not enough info. What error message are you getting? [Are you printing out the shader info log and program info log when the shaders fail to compile or link](http://webglfundamentals.org/webgl/lessons/webgl-boilerplate.html)? – gman Oct 11 '16 at 08:27
  • I added info where error occur – Log Oct 11 '16 at 14:25
  • Your code should say `if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) { alert(gl.getProgramInfoLog(shaderProgram)); }` and you should also have *similar* code for when you compile the shaders except you call `gl.getShaderInfoLog(shader)` and `gl.getShaderParameter(shader, gl.COMPILE_STATUS)` – gman Oct 11 '16 at 23:04

2 Answers2

2

You need to declare it in both shaders, with the same precision.

What do you mean by:

then program don't work right.

pleup
  • 917
  • 7
  • 9
2

Can you post your vertex shader and your fragment shader? I suspect you are relying on the default precision, and it is likely that you are using highp in the vertex shader and mediump in the fragment shader

Try using:

uniform mediump float uIsTeapot;

... in both vertex shader and fragment shader.

solidpixel
  • 10,688
  • 1
  • 20
  • 33