Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8222811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:24:16+00:00 2026-06-07T14:24:16+00:00

Let’s say we have a texture (in this case 8×8 pixels) we want to

  • 0

Let’s say we have a texture (in this case 8×8 pixels) we want to use as a sprite sheet. One of the sub-images (sprite) is a subregion of 4×3 inside the texture, like in this image:

enter image description here

(Normalized texture coordinates of the four corners are shown)

Now, there are basically two ways to assign texture coordinates to a 4px x 3px-sized quad so that it effectively becomes the sprite we are looking for; The first and most straightforward is to sample the texture at the corners of the subregion:

enter image description here

// Texture coordinates

GLfloat sMin = (xIndex0                  ) / imageWidth;
GLfloat sMax = (xIndex0 + subregionWidth ) / imageWidth;
GLfloat tMin = (yIndex0                  ) / imageHeight;
GLfloat tMax = (yIndex0 + subregionHeight) / imageHeight;

Although when first implementing this method, ca. 2010, I realized the sprites looked slightly ‘distorted’. After a bit of search, I came across a post in the cocos2d forums explaining that the ‘right way’ to sample a texture when rendering a sprite is this:

enter image description here

// Texture coordinates

GLfloat sMin = (xIndex0                   + 0.5) / imageWidth;
GLfloat sMax = (xIndex0 + subregionWidth  - 0.5) / imageWidth;
GLfloat tMin = (yIndex0                   + 0.5) / imageHeight;
GLfloat tMax = (yIndex0 + subregionHeight - 0.5) / imageHeight;

…and after fixing my code, I was happy for a while. But somewhere along the way, and I believe it is around the introduction of iOS 5, I started feeling that my sprites weren’t looking good. After some testing, I switched back to the ‘blue’ method (second image) and now they seem to look good, but not always.

Am I going crazy, or something changed with iOS 5 related to GL ES texture mapping? Perhaps I am doing something else wrong? (e.g., the vertex position coordinates are slightly off? Wrong texture setup parameters?) But my code base didn’t change, so perhaps I am doing something wrong from the beginning…?

I mean, at least with my code, it feels as if the “red” method used to be correct but now the “blue” method gives better results.

Right now, my game looks OK, but I feel there is something half-wrong that I must fix sooner or later…

Any ideas / experiences / opinions?

ADDENDUM

To render the sprite above, I would draw a quad measuring 4×3 in orthographic projection, with each vertex assigned the texture coords implied in the code mentioned before, like this:

// Top-Left Vertex
{ sMin, tMin };

// Bottom-Left Vertex
{ sMin, tMax };

// Top-Right Vertex
{ sMax, tMin };

// Bottom-right Vertex
{ sMax, tMax };

The original quad is created from (-0.5, -0.5) to (+0.5, +0.5); i.e. it is a unit square at the center of the screen, then scaled to the size of the subregion (in this case, 4×3), and its center positioned at integer (x,y) coordinates. I smell this has something to do too, especially when either width, height or both are not even?

ADDENDUM 2

I also found this article, but I’m still trying to put it together (it’s 4:00 AM here)
http://www.mindcontrol.org/~hplus/graphics/opengl-pixel-perfect.html

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T14:24:17+00:00Added an answer on June 7, 2026 at 2:24 pm

    There’s slightly more to this picture than meets the eye, the texture coordinates are not the only factor in where the texture gets sampled. In your case I believe the blue is probably what want to have.

    What you ultimately want is to sample each texel in center. You don’t want to be taking samples on the boundary between two texels, because that either combines them with linear sampling, or arbitrarily chooses one or the other with nearest, depending on which way the floating point calculations round.

    Having said that, you might think that you don’t want to have your texcoords at (0,0), (1,1) and the other corners, because those are on the texel boundary. However an important thing to note is that opengl samples textures in the center of a fragment.

    For a super simple example, consider a 2 by 2 pixel monitor, with a 2 by 2 pixel texture.

    If you draw a quad from (0,0) to (2,2), this will cover 4 pixels. If you texture map this quad, it will need to take 4 samples from the texture.

    If your texture coordinates go from 0 to 1, then opengl will interpolate this and sample from the center of each pixel, with the lower left texcoord starting at the bottom left corner of the bottom left pixel. This will ultimately generate texcoord pairs of (0.25, 0.25), (0.75,0.75), (0.25, 0.75), and (0.75, 0.25). Which puts the samples right in the middle of each texel, which is what you want.

    If you offset your texcoords by a half pixel as in the red example, then it will interpolate incorrectly, and you’ll end up sampling the texture off center of the texels.

    So long story short, you want to make sure that your pixels line up correctly with your texels (don’t draw sprites at non-integer pixel locations), and don’t scale sprites by arbitrary amounts.

    If the blue square is giving you bad results, can you give an example image, or describe how you’re drawing it?

    Picture says 1000 words:

    img

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let’s say I have a number like 0x448 . In binary this is 0100
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
Let's say I have a C++ Visual Studio 2010 solution with 2 projects: one
Let's say I have this code: <p dataname=description> Hello this is a description. <a
Let say I've this URL: http://example.com/image-title/987654/ I want to insert download to the part
Let's say I have table with column 'URL' whrere I store urls like this
Let's say that I have a set of relations that looks like this: relations
Let's say I have a drive such as C:\ , and I want to
Let's say I don't have photoshop, but I want to make pattern files (.pat)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.