[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Public WebGL] After installing latest Minefield : VertexAttribPointer: index out of range



Oh, that happened while trying to translate from german to english :) 

Okay, then another try, this time, without translating. This is how I'm using it:

Shaders:

<script id="shader-fs" type="x-shader/x-fragment">

   #ifdef GL_ES
   precision highp float;
   #endif
  
   varying vec3 vLightWeighting;
   varying vec3 myColor;

   void main(void) {
    gl_FragColor = vec4(myColor * vLightWeighting, 1.0);
   }
</script>

<script id="shader-vs" type="x-shader/x-vertex">
  attribute vec3 vertexPosition;
  attribute vec3 vertexNormale;
  attribute vec3 vertexColor;
  
  uniform mat4 modelViewMatrix;
  uniform mat4 projektionsMatrix;
  uniform mat4 normalenMatrix;
  uniform vec3 ambientLight;
 
  uniform vec3 constantColor; 
  uniform bool useUniformColor;
  uniform bool selecting;

  varying vec3 vLightWeighting;
  varying vec3 myColor;

  void main(void) {

      vec4 mvPosition=modelViewMatrix*vec4(vertexPosition,1.0); 

      vec3 ambientColor = ambientLight;
      vec3 directionalLightColor = vec3(0.6, 0.6, 0.6);
      vec3 directionalVector = vec3(-0.7, -0.7, 0.9);

      
      if(selecting){
          vLightWeighting=vec3(1.0,1.0,1.0);
      }else{
          vec3 lightDirection=normalize(directionalVector - mvPosition.xyz); 
            vec4 transformedNormal = normalenMatrix * vec4(vertexNormale, 1.0);

          float directionalLightWeighting = max(dot(transformedNormal.xyz, lightDirection), 0.0);    
          vLightWeighting = ambientLight + (directionalLightColor * directionalLightWeighting);
      }  
      
      if(!useUniformColor){
       // myColor=vertexColor;   //IF ACTIVE --> chromium won't draw anything 
      }else{
         myColor=constantColor;
      }
      gl_Position = projektionsMatrix * mvPosition;

  }

</script>


And the pogram stuff: 

   var shaderProgram;
   function initShaders() {
     var fragmentShader = getShader(gl, "shader-fs");
     var vertexShader = getShader(gl, "shader-vs");

     shaderProgram = gl.createProgram();
     gl.attachShader(shaderProgram, vertexShader);
     gl.attachShader(shaderProgram, fragmentShader);
     gl.linkProgram(shaderProgram);

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

     gl.useProgram(shaderProgram);

     shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "vertexPosition");
     gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);

     shaderProgram.vertexNormalAttribute = gl.getAttribLocation(shaderProgram, "vertexNormale");
     gl.enableVertexAttribArray(shaderProgram.vertexNormalAttribute);
     shaderProgram.vertexColorAttribute = gl.getAttribLocation(shaderProgram, "vertexColor");
     //gl.enableVertexAttribArray(shaderProgram.vertexColorAttribute);


     shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, "projektionsMatrix");
     shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, "modelViewMatrix");
     shaderProgram.nMatrixUniform = gl.getUniformLocation(shaderProgram, "normalenMatrix");
     shaderProgram.ambientLight = gl.getUniformLocation(shaderProgram, "ambientLight");
     shaderProgram.useUniformColor = gl.getUniformLocation(shaderProgram, "useUniformColor");
     gl.uniform1i(shaderProgram.useUniformColor,true)
     shaderProgram.constantColor = gl.getUniformLocation(shaderProgram, "constantColor");
     shaderProgram.selecting = gl.getUniformLocation(shaderProgram, "selecting");

   }


...too bad Firefox >4.0b3 and latest Chromium not prompting any errors or warnings.


2010/10/25 Daniel Koch <daniel@transgaming.com>
Assuming this isn't a problem that arose during transcribing your code & shaders, I see the following issue:

<-- from the shader -->
  attribute vec3 vertexNormale;
...
  uniform mat4 projektionsMatrix;
  uniform mat4 normalenMatrix;

<-- from the code -->
     shaderProgram.vertexNormalAttribute = gl.getAttribLocation(shaderProgram, "vertexNormal");
...

     shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, "projectionsMatrix");
..
     shaderProgram.nMatrixUniform = gl.getUniformLocation(shaderProgram, "normalMatrix");

----

The string that is used in the GLSL code and to query from the source code must match exactly.

That is "vertexNormale" will not match "vertexNormal".  Similarly with "projektionsMatrix"  vs. "projectionsMatrix" and for "normalenMatrix" vs "normalMatrix".

That said, I'm not quite sure how this would work on any platform, so perhaps you just transcribed the code wrong.  If that's the case, please copy/paste your code *exactly* as you are trying to compile it.

Hope this helps,
Daniel


---
                        Daniel Koch -+- daniel@transgaming.com
Senior Graphics Architect -+- TransGaming Inc.  -+- www.transgaming.com