Jump to content
  • Advertisement
Sign in to follow this  

OpenGL Scaling, UV Coords, and clipping rectangle rounding error?

This topic is 1142 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm currently working in OpenGL ES 2.0 and I came across a weird issue with my clipping rectangle.

I have this texture atlas (159 x 48):

And a clipping rectangle that is sized to get only the green square:

The clipping rectangle position / sized is at
X: 32.0f
Y: 0.0f
Width: 7.0f
Height 12.0f


Now when I try to scale this up by 2 and offset the drawing location by the clipping rectangle's center. In my current case, I'm drawing at 0,0 and offsetting by the clip rectangle center (3.5f, 6.0f) scaled by 2.


Depending on the draw position (or in my case -7.0f, -12.0f  [0,0 offset by the clip rectangle's center]), my image sometimes has a weird artifact, where extra pixels are included in the image that is rendered:

I guess the clip rectangle gets a little too big. Why is this happening? Is this do to floating point errors? Is it because I have a weird texture size (159 x 48) with a width that does not divide by 2 evenly?


This only happens when scaling up by certain values. For example scaling by 1, 3, 4, or 5 are totally fine. But when I scale by 2 or 14 the issue can be seen

My data is placed into my VBO like this:

//Position calculations
topLeftX = x;
topLeftY = y;

botLeftX = x;
botLeftY = y + (clipRect.getHeight() * scale);

botRightX = x + (clipRect.getWidth() * scale); 
botRightY = y + (clipRect.getHeight() * scale);

topRightX = x + (clipRect.getWidth() * scale);
topRightY = y;

//Calc the UV Coords
uvTopLeftX = clipRect.getX() / texture.getWidth();
uvTopLeftY = clipRect.getY() / texture.getHeight();

uvBotLeftX = clipRect.getX() / texture.getWidth();
uvBotLeftY = (clipRect.getY() + clipRect.getHeight()) / texture.getHeight();

uvBotRightX = (clipRect.getX() + clipRect.getWidth()) / texture.getWidth();
uvBotRightY = (clipRect.getY() + clipRect.getHeight()) / texture.getHeight();

uvTopRightX = (clipRect.getX() + clipRect.getWidth()) / texture.getWidth();
uvTopRightY = clipRect.getY() / texture.getHeight();

/* Code to place above calculations into the VBO buffer */

Edited by noodleBowl

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!