title: OpenGL transformation in NDC coordinates instead of screen coordinates (2D)
body: <p>I'm having trouble figuring out how to position a 2D object in my scene using screen coordinates. At the moment I have something working (code below) but it want NDC coordinates which isn't easy to work with. I can't figure out where it's going wrong. I think I've used everything like it should be so I think I'm forgetting something.</p>
  
  <p>Here's the code that handles the drawing of the objects in my scene:</p>
  
  <pre><code>glClearDepth(1.0f);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  
  
  // RENDERING HERE
  colorProgram.bind();
  
  for (size_t t = 0; t &lt; objectsWithGraphicsComponentInThisScene.size(); ++t)
  {
      // set texture
      glActiveTexture(GL_TEXTURE0);
      glBindTexture(GL_TEXTURE_2D, objectsWithGraphicsComponentInThisScene[t]-&gt;getComponent&lt;GraphicsComponent&gt;()-&gt;getTexture());
  
      GLint texLocation = colorProgram.getUniformLocation("texSampler");
      glUniform1i(texLocation, 0);
  
      glm::mat4 trans;
      trans = glm::translate(glm::mat4x4(1.0f), glm::vec3(objectsWithGraphicsComponentInThisScene[t]-&gt;getPosition().x, objectsWithGraphicsComponentInThisScene[t]-&gt;getPosition().y, 0));
      GLint transMatLocation = colorProgram.getUniformLocation("transformMatrix");
      glUniformMatrix4fv(transMatLocation, 1, GL_FALSE, glm::value_ptr(trans));
  
      // set camera Matrix
      GLint projMatLocation = colorProgram.getUniformLocation("projectionMatrix");
      glm::mat4 cameraMatrix = camera-&gt;getCameraMatrix();
      glUniformMatrix4fv(projMatLocation, 1, GL_FALSE, glm::value_ptr(cameraMatrix));
  
      objectsWithGraphicsComponentInThisScene[t]-&gt;getComponent&lt;GraphicsComponent&gt;()-&gt;getSprite()-&gt;draw();
  
      // unbind all
      glBindTexture(GL_TEXTURE_2D, 0);
  }
  
  colorProgram.unbind();
  </code></pre>
  
  <p>where colorProgram is the shader my sprites use and <code>getPosition()</code> simply returns a value which I've set. (where the x y and z value should be given as screen coordinates). so for example, getPosition might return [100, 50, 0] but that will render the object outside of the screen (the screen is 1280x720).</p>
  
  <p>Now the code that renders the sprite (objectsWithGraphicsComponentInThisScene[t]->getComponent()->getSprite()->draw();):</p>
  
  <pre><code>void Sprite::draw()
  {
  glBindBuffer(GL_ARRAY_BUFFER, vboID);
  
  glEnableVertexAttribArray(0);
  
  // position
  glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, position));
  
  //color
  glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), (void*)offsetof(Vertex, color));
  
  // uv
  glVertexAttribPointer(2, 2, GL_FLOAT, GL_TRUE, sizeof(Vertex), (void*)offsetof(Vertex, uv));
  
  glDrawArrays(GL_TRIANGLES, 0, 6);
  
  glDisableVertexAttribArray(0);
  
  glBindBuffer(GL_ARRAY_BUFFER, 0);
  }
  </code></pre>
  
  <p>And here's the code in the shader (colorProgram):
  VERTEX SHADER:</p>
  
  <pre><code>#version 130
  // per vertex
  
  // input data from VBO
  in vec2 vertexPosition;
  in vec4 vertexColor;
  in vec2 vertexUV;
  
  // output to fragment shader
  out vec4 fragmentColor;
  out vec2 fragmentUV;
  
  uniform mat4 projectionMatrix;
  uniform mat4 transformMatrix;
  
  void main()
  {
  mat4 resultMatrix = transformMatrix * projectionMatrix;
  gl_Position.xy = (resultMatrix * vec4(vertexPosition, 0.0, 1.0)).xy;
  gl_Position.z = 0.0;
  
  // Indicate that the coordinates are normalized
  gl_Position.w = 1.0;
  
  fragmentColor = vertexColor;
  fragmentUV = vec2(vertexUV.x, 1.0 - vertexUV.y);
  }
  </code></pre>
  
  <p>FRAGMENT SHADER</p>
  
  <pre><code>#version 130
  // per pixel
  
  // input from vertex shader
  in vec4 fragmentColor;
  in vec2 fragmentUV;
  
  out vec4 color;
  
  uniform sampler2D texSampler;
  
  void main()
  {
  vec4 textureColor = texture(texSampler, fragmentUV);
  if (textureColor.a &lt; 0.5) discard;
  color = fragmentColor * textureColor;
  }
  </code></pre>
  
  <p>If you need more code I'd be happy to add more although I think this is everything that is needed.</p>