title: GLSL: Rendering a 2D texture
body: <p>I was following LazyFoo's tutorial on GLSL 2D texturing (<a href="http://lazyfoo.net/tutorials/OpenGL/34_glsl_texturing/index.php" rel="nofollow">http://lazyfoo.net/tutorials/OpenGL/34_glsl_texturing/index.php</a>), and I was able to get most parts working.</p>
  
  <p>However, the program renders the texture zoomed up real close. Is this an issue with the vertex, or the texture lookup? Below is the vertex shader I was using in my implementation:</p>
  
  <pre><code>texCoord = LTexCoord;  
  gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * vec4( LVertexPos2D.x, LVertexPos2D.y, 0.0, 1.0 );
  </code></pre>
  
  <p>And below is the fragment shader I was using:</p>
  
  <pre><code>gl_FragColor = texture( textureID, texCoord );
  </code></pre>
  
  <p>As for the render function, I deviate from the tutorial by using opengl's fixed pipeline matrices (don't need to update matrices ):</p>
  
  <pre><code>//If the texture exists
      if( mTextureID != 0 )
      {
          //Texture coordinates
          GLfloat texTop = 0.f;
          GLfloat texBottom = (GLfloat)mImageHeight / (GLfloat)mTextureHeight;
          GLfloat texLeft = 0.f;
          GLfloat texRight = (GLfloat)mImageWidth / (GLfloat)mTextureWidth;
  
          //Vertex coordinates
          GLfloat quadWidth = mImageWidth;
          GLfloat quadHeight = mImageHeight;
  
          //Set vertex data
          LVertexData2D vData[ 4 ];
  
          //Texture coordinates
          vData[ 0 ].texCoord.s =  texLeft; vData[ 0 ].texCoord.t =    texTop;
          vData[ 1 ].texCoord.s = texRight; vData[ 1 ].texCoord.t =    texTop;
          vData[ 2 ].texCoord.s = texRight; vData[ 2 ].texCoord.t = texBottom;
          vData[ 3 ].texCoord.s =  texLeft; vData[ 3 ].texCoord.t = texBottom;
  
          //Vertex positions
          vData[ 0 ].position.x =       0.f; vData[ 0 ].position.y =        0.f;
          vData[ 1 ].position.x = quadWidth; vData[ 1 ].position.y =        0.f;
          vData[ 2 ].position.x = quadWidth; vData[ 2 ].position.y = quadHeight;
          vData[ 3 ].position.x =       0.f; vData[ 3 ].position.y = quadHeight;
  
          glEnable(GL_TEXTURE_2D);
          glBindTexture( GL_TEXTURE_2D, mTextureID );
          glContext.textureShader-&gt;bind();
          glContext.textureShader-&gt;setTextureID( mTextureID );
          glContext.textureShader-&gt;enableVertexPointer();
          glContext.textureShader-&gt;enableTexCoordPointer();
              glBindBuffer( GL_ARRAY_BUFFER, mVBOID );
              glBufferSubData( GL_ARRAY_BUFFER, 0, 4 * sizeof(LVertexData2D), vData );
              glContext.textureShader-&gt;setTexCoordPointer( sizeof(LVertexData2D), (GLvoid*)offsetof( LVertexData2D, texCoord ) );
              glContext.textureShader-&gt;setVertexPointer( sizeof(LVertexData2D), (GLvoid*)offsetof( LVertexData2D, position ) );
              glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, mIBOID );
              glDrawElements( GL_TRIANGLE_FAN, 4, GL_UNSIGNED_INT, NULL );
          glContext.textureShader-&gt;disableVertexPointer();
          glContext.textureShader-&gt;disableTexCoordPointer();
          glContext.textureShader-&gt;unbind();
          glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
          glBindTexture( GL_TEXTURE_2D, NULL );
          glDisable(GL_TEXTURE_2D);         // disable texture 2d
  
  
      }
  }
  </code></pre>
  
  <p>In response to Koradi, the vertex and texture coordinates are instantiated as such below:</p>
  
  <pre><code>void TextureShader::setVertexPointer( GLsizei stride, const GLvoid* data )
  {
      glVertexAttribPointer( mVertexPosLocation, 2, GL_FLOAT, GL_FALSE, stride, data );
  }
  void TextureShader::setTexCoordPointer( GLsizei stride, const GLvoid* data )
  {
      glVertexAttribPointer( mTexCoordLocation, 2, GL_FLOAT, GL_FALSE, stride, data );
  }
  </code></pre>
  
  <p>It is rendered in the main loop with the following code:</p>
  
  <pre><code>glPushMatrix();
      glTranslatef( glContext.gFBOTexture-&gt;imageWidth() / -2.f, glContext.gFBOTexture-&gt;imageHeight() / -2.f, 0.f );
      glContext.gFBOTexture-&gt;render();
  glPopMatrix();
  </code></pre>
  
  <p>Is there something obvious that I am overlooking? I am new to GLSL.</p>
  
  <p>Edit: Added more code</p>