title: Reflection Mapping in OpenGL ES
body: <p>I am trying to implement the reflection mapping in OpenGL ES 2.0 for 'sphere'.
  I have done the skybox.
  For sphere rendering, the reflection shaders i have used are:</p>
  
  <p>Environment mapping (Sphere) vertex shader::</p>
  
  <pre><code>precision highp float;
  uniform mat4    u_mvMatrix; // ModelView Matrix
  uniform mat4    u_mvpMatrix; // ModelViewProjection Matrix
  
  attribute vec4 a_position;
  attribute vec3 a_envmapNormal;
  
  varying vec3 v_eyecoordEyeReflection;
  
  vec3 v_eyecoordPosition;
  vec3 v_eyecoordNormal;
  
  void main()
   {
      // position and normal in model coordinates
      vec4 modelCoordPosition = a_position;
      vec3 modelCoordNormal   = a_envmapNormal;
  
      // Calculate position in eye space
      v_eyecoordPosition = vec3(u_mvMatrix * modelCoordPosition);
  
      // Calculate and normalize eye space normal
      vec3 eyecoordNormal    = vec3(u_mvMatrix * vec4(modelCoordNormal, 0.0));
      v_eyecoordNormal        = normalize(eyecoordNormal);
      // Calculate reflection vector
      v_eyecoordEyeReflection = reflect(v_eyecoordPosition, v_eyecoordNormal);
  
      gl_Position = u_mvpMatrix * a_position;
  }
  </code></pre>
  
  <p>Environment mapping (Sphere) Fragment shader</p>
  
  <pre><code>precision highp float;
  uniform lowp samplerCube baseCubeMapTexture;
  varying vec3 v_eyecoordEyeReflection;
  
  void main()
  {
      gl_FragColor  = textureCube(baseCubeMapTexture, v_eyecoordEyeReflection);
  }
  </code></pre>
  
  <p>But i am not getting correct output.
  When the sphere is rotated, the texture is not changing.</p>
  
  <p>what is the error in the shader?</p>
  
  <hr>
  
  <hr>
  
  <p>Thanks Andon...
  I used your shader code.
  But i am getting white sphere.</p>
  
  <p>Sphere Normals are calculated using:</p>
  
  <pre><code>    #define ANGLE_STEP      ((2.0f * OGLES_PI) / ((float) NUM_OF_SLICES))
      for ( iCnti = 0; iCnti &lt; NUM_OF_PARALLELS + 1; iCnti++ )    {
          for ( iCntj = 0; iCntj &lt; NUM_OF_SLICES + 1; iCntj++ )   {
              pSphereNormals[iNormalIndex + 0] = sin(ANGLE_STEP * (FLOAT) iCnti )* sin (ANGLE_STEP *(FLOAT)iCntj);
              pSphereNormals[iNormalIndex + 1] = cos(ANGLE_STEP * (FLOAT) iCnti );
              pSphereNormals[iNormalIndex + 2] = sin(ANGLE_STEP * (FLOAT) iCnti )* cos (ANGLE_STEP *(FLOAT)iCntj);
              iNormalIndex += 3;
          }
      }
  </code></pre>
  
  <p>My View Matrix "matViewMatrix" is derived from (<a href="http://www.learnopengles.com/tag/linmath-h/" rel="nofollow">http://www.learnopengles.com/tag/linmath-h/</a> mat4x4_look_at())
  MyCameraLookAt(matViewMatrix, 0.0f , 0.0f, -2.0f,  0.0f , 0.0f, -1.0f,  0.0f , 1.0f, 0.0f);</p>
  
  <p>The Inverse matrix   InvViewMat is  // inverse() function is taken from <a href="http://www.opensource.apple.com/source/WebCore/WebCore-514/platform/graphics/transforms/TransformationMatrix.cpp" rel="nofollow">http://www.opensource.apple.com/source/WebCore/WebCore-514/platform/graphics/transforms/TransformationMatrix.cpp</a></p>
  
  <pre><code>    InvViewMat[0][0] = -1.000000   InvViewMat[1][0] = -0.000000   InvViewMat[2][0] = 0.000000    InvViewMat[3][0] = -0.000000
      InvViewMat[0][1] = -0.000000   InvViewMat[1][1] = 1.000000    InvViewMat[2][1] = -0.000000   InvViewMat[3][1] = -0.000000
      InvViewMat[0][2] =  0.000000   InvViewMat[1][2] = -0.000000   InvViewMat[2][2] = -1.000000   InvViewMat[3][2] = -2.000000
      InvViewMat[0][3] = -0.000000   InvViewMat[1][3] = 0.000000    InvViewMat[2][3] = 0.000000    InvViewMat[3][3] = 1.000000
  </code></pre>
  
  <p>Is there any problem with my matrix values or any of my calculations?</p>