title: OpenGL ES 2.0 for Android - gl_FragCoord and gl_FragColor
body: <p>After a few day with a lot of researching and tests I finally was able to implement the Fragment Shader that is shown is this book (an excellent book, may I say): <a href="http://arcsynthesis.org/gltut/Basics/Tutorial%2002.html" rel="nofollow">http://arcsynthesis.org/gltut/Basics/Tutorial%2002.html</a>.</p>
  
  <p>It just teaches how to render a simple triangle with a Fragment Shader that uses "gl_FragCoord.y".</p>
  
  <p>In these working days however I went throught some issues that I would like to understand a little better why them happened.</p>
  
  <p><strong>1st</strong> - In my fragment shader, if I use the code:
  <code>gl_FragColor = vec4(gl_FragCoord.x/500.0, 0.0, 1.0, 1.0);</code>
  the triangles is rendered. </p>
  
  <p>But If I use:
  <code>gl_FragColor = vec4(gl_FragCoord.x/500, 0.0, 1.0, 1.0);</code>
  it's not rendered (I got a black screen, or whatever the color is defined in the <code>GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);</code>.</p>
  
  <p>That's lead to the question: <strong>In GLSL numbers have to have decimal specification?</strong>
  Note that the only thing that changed in the code was the 500.0 number to 500.</p>
  
  <p><strong>2nd</strong> - <strong>How can I retrieve the variable values that are inside the Fragment Shader Code to display them for example in a Toast?</strong> Let me explain that question.</p>
  
  <p>With these three code lines below:</p>
  
  <p><code>float colorTwo[] = { 1.0f, 1.0f, 1.0f, 0.0f };</code></p>
  
  <p><code>int mColorHandleTwo = GLES20.glGetUniformLocation(mProgram, "vColorTwo");</code></p>
  
  <p><code>GLES20.glUniform4fv(mColorHandleTwo, 1, colorTwo, 0);</code></p>
  
  <p>I'm able to get this "colorTwo" array and used it inside the Fragment Shader Code.
  In the other hand, If I have let's say a statment inside the Fragment Shader Code like:</p>
  
  <p><code>float whatThisNumber = gl_FragCoord.y</code></p>
  
  <p>How can I get this float and display it in a Toast?</p>
  
  <p>The code below is actually the one I'm working on:</p>
  
  <pre><code>public class TheShape {
  
      ByteBuffer myByteBuffer;
      FloatBuffer positionBufferObject;    
  
      int mProgram;   
  
      public TheShape() {
  
          float vertexPositions[] = { 
  
                   0.75f,  0.75f, 0.0f, 1.0f,
                   0.75f, -0.75f, 0.0f, 1.0f,
                  -0.75f, -0.75f, 0.0f, 1.0f
  
          };
  
          myByteBuffer = ByteBuffer.allocateDirect(vertexPositions.length * 4);
          myByteBuffer.order(ByteOrder.nativeOrder());
  
          positionBufferObject = myByteBuffer.asFloatBuffer();
          positionBufferObject.put(vertexPositions);
          positionBufferObject.position(0);
  
          String vertexShaderCode =
                  "attribute vec4 vPosition;" +
                  "void main() {" +
                  "  gl_Position = vPosition;" +
                  "}";
  
          String fragmentShaderCode =
                  "precision mediump float;" +
                  "uniform vec4 vColor;" +
                  "uniform vec4 vColorTwo;" +
                  "void main() {" +
                  "gl_FragColor = vec4(gl_FragCoord.x/500.0, 0.0, 1.0, 1.0);" +
           //ok   "gl_FragColor = mix(vColorTwo, vColor, (gl_FragCoord.y)/500.0);" +
           //ok   "gl_FragColor = vec4((gl_FragCoord.x)/500.0, (gl_FragCoord.y)/500.0, 0.0, 1.0);" + 
                  "}";
  
          int myVertexShader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
          GLES20.glShaderSource(myVertexShader, vertexShaderCode);
          GLES20.glCompileShader(myVertexShader);
  
          int myFragmentShader = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
          GLES20.glShaderSource(myFragmentShader, fragmentShaderCode);
          GLES20.glCompileShader(myFragmentShader);
  
          mProgram = GLES20.glCreateProgram();
  
          GLES20.glAttachShader(mProgram, myVertexShader);        
          GLES20.glAttachShader(mProgram, myFragmentShader); 
  
          GLES20.glLinkProgram(mProgram);        
  
      }
  
      public void draw() {
  
          GLES20.glUseProgram(mProgram);
  
          GLES20.glEnableVertexAttribArray(0);
          GLES20.glVertexAttribPointer(0, 4, GLES20.GL_FLOAT, false, 0, positionBufferObject);
  
  
  
          float color[] = { 0.0f, 0.0f, 0.0f, 1.0f };  
          int mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");
          GLES20.glUniform4fv(mColorHandle, 1, color, 0);
  
  
          float colorTwo[] = { 1.0f, 1.0f, 1.0f, 0.0f };  
          int mColorHandleTwo = GLES20.glGetUniformLocation(mProgram, "vColorTwo");
          GLES20.glUniform4fv(mColorHandleTwo, 1, colorTwo, 0);
  
  
  
          GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);
  
      }
  
  }
  </code></pre>
  
  <p>I hope I was clear on my questions.</p>