title: Resizing point sprites based on distance from the camera
body: <p>I'm writing a clone of Wolfenstein 3D using only core OpenGL 3.3 for university and I've run into a bit of a problem with the sprites, namely getting them to scale correctly based on distance.</p>
  
  <p>From what I can tell, previous versions of OGL would in fact do this for you, but that functionality has been removed, and all my attempts to reimplement it have resulted in complete failure.</p>
  
  <p>My current implementation is passable at distances, not too shabby at mid range and bizzare at close range. </p>
  
  <p>The main problem (I think) is that I have no understanding of the maths I'm using.<br>
  The target size of the sprite is slightly bigger than the viewport, so it should 'go out of the picture' as you get right up to it, but it doesn't. It gets smaller, and that's confusing me a lot.<br>
  I recorded a small video of this, in case words are not enough. (Mine is on the right)</p>
  
  <p><img src="https://i.imgur.com/bXuiJ.gif" alt="Expected Result" title="Original Wolf3D screenvid">
  <img src="https://i.imgur.com/3rf4g.gif" alt="Actual Result" title="My attempt screenvid"></p>
  
  <p>Can anyone direct me to where I'm going wrong, and explain why?</p>
  
  <p>Code:<br>
  <strong>C++</strong></p>
  
  <pre><code>// setup
  glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN, GL_LOWER_LEFT);
  glEnable(GL_PROGRAM_POINT_SIZE);
  
  // Drawing
  glUseProgram(StaticsProg);
  glBindVertexArray(statixVAO);
  glUniformMatrix4fv(uStatixMVP, 1, GL_FALSE, glm::value_ptr(MVP));
  glDrawArrays(GL_POINTS, 0, iNumSprites);
  </code></pre>
  
  <p><strong>Vertex Shader</strong></p>
  
  <pre><code>#version 330 core
  
  layout(location = 0) in vec2 pos;
  layout(location = 1) in int spriteNum_;
  
  flat out int spriteNum;
  
  uniform mat4 MVP;
  
  const float constAtten  = 0.9;
  const float linearAtten = 0.6;
  const float quadAtten   = 0.001;
  
  void main() {
      spriteNum = spriteNum_;
      gl_Position = MVP * vec4(pos.x + 1, pos.y, 0.5, 1); // Note: I have fiddled the MVP so that z is height rather than depth, since this is how I learned my vectors.
      float dist = distance(gl_Position, vec4(0,0,0,1));
      float attn = constAtten / ((1 + linearAtten * dist) * (1 + quadAtten * dist * dist));
      gl_PointSize = 768.0 * attn;
  }
  </code></pre>
  
  <p><strong>Fragment Shader</strong></p>
  
  <pre><code>#version 330 core
  
  flat in int spriteNum;
  
  out vec4 color;
  
  uniform sampler2DArray Sprites;
  
  void main() {
      color = texture(Sprites, vec3(gl_PointCoord.s, gl_PointCoord.t, spriteNum));
      if (color.a &lt; 0.2)
          discard;
  }
  </code></pre>