1

Running on iPad.

Mapping a texture that is 256x256 onto a quad. I'm trying to render it exactly the same size as the actual image. The quad looks correct (shape is right, texture mapped correctly), but it is only ~75% of the size of the actual .png.

Not sure why.

The code is characterized as follows (excerpts below):

Screen is 768x1024. Windows is 768x1024 as well.

    glViewport(0, 0, 768, 1024);  // aspect ratio 1:1.333
    glOrthof(-0.5f, 0.5f, -0.666f, 0.666f, -1.0f, 1.0f); // matching aspect ratio with 0,0 centered

    // Sets up an array of values to use as the sprite vertices.  
    //.25 of 1024 is 256 pixels so the quad (centered on 0,0) spans -0.125, 
    //-0.125 to 0.125, 0.125 (bottom left corner and upper right corner)

    GLfloat spriteVertices[] = {
       -0.125f, -0.125f,
        0.125f, -0.125f,
       -0. 125f,  0.125f,
        0.125f,  0.125f,
    };

    // Sets up an array of values for the texture coordinates.

    const GLshort spriteTexcoords[] = {
        0, 0,
        1, 0,
        0, 1,
        1, 1,
    };

followed by the appropriate calls to:

    glVertexPointer(2, GL_FLOAT, 0, spriteVertices);
    glTexCoordPointer(2, GL_SHORT, 0, spriteTexcoords);

then

    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

Why is my sprite smaller than 256x256 when rendered?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Gerald
  • 21
  • 3
  • Smaller by how much? If it's only 1 or 2 pixels in either dimension, this is no surprise. OpenGL doesn't address pixels/texels but interval sampling points. The outermost pixels of a texture don't map to coordinates 0 and 1, but to 0+0.5/dimension and 1-0.5/dimension. – datenwolf Jun 08 '11 at 07:10
  • From above: "The quad looks correct (shape is right, texture mapped correctly), but it is only ~75% of the size of the actual .png." – Gerald Jun 08 '11 at 07:25
  • @datenwolf I had problems with this once. Do you know where it is exactly in the spec ? Couldn't find it at the time. – Calvin1602 Jun 09 '11 at 08:15
  • @Calvin1602: See paragraph about texture sampling. The lines go along that the data provided are actually support samples for an interpolation in the range [0..1], where the texels are to be considered small rectangles in a grid with the outer boundaries of the whole grid being at 0, 1. – datenwolf Jun 09 '11 at 09:07

1 Answers1

3

Your output is 192x192 (approx) because your quad is the wrong size. It's 0.25x0.25 and the "unit length" direction is X which is 768 wide, so it's 0.25 * 768 = 192. If you switched your glOrthof so that top/bottom were -0.5 and +0.5 (with appropriate correction to X) it would work.

Ben Jackson
  • 90,079
  • 9
  • 98
  • 150
  • Interesting. What determines "unit length" direction? This is a new concept for me... (googling it now as well).. I used 1024 vs. 768 in my calculations. Thanks for the quick response. – Gerald Jun 08 '11 at 08:11
  • That fixed the issue. Per my previous question, "what determines unit length direction?" Is this X by default? And, is it always thus or do I need to be aware that it might change? Thanks again. – Gerald Jun 08 '11 at 08:48
  • @Gerald: I just invented the term "unit length direction" to describe the axis in `glOrthof` which you made "1.0 units" wide. I assumed you decided that one axis would be 1.0 ("unit length") and you would then scale the other to match the aspect ratio. You could have equally well made one axis 768.0 and the other 1024.0 and your quad 256.0 x 256.0 – Ben Jackson Jun 08 '11 at 19:19