# OpenGL scaling causes artifacts

## Recommended Posts

Hello guys,

i'm working on an opengl based tile game. the game map is formed by many tiles.
The game looks fine, but somebody thought the tiles are too small.

i therefore want to enlarge the game map simply by applying a scaling matrix before drawing these tiles.

but then i started to see cracks between tiles.

this is how i draw the tiles:

[code]
[font=monospace]glMatrixMode(GL_MODELVIEW);glPushMatrix();glScalef(scale,scale,1.0);map.draw();glPopMatrix();[/font]
[/code]

this is how it looks like without scaling (scale==1.0)

[img]http://dl.dropbox.com/u/3315090/prob1.png[/img]

this is when scale=1.5

[img]http://dl.dropbox.com/u/3315090/prob2.png[/img]

as you can see between the tiles, there are cracks. cracks don't always happen, it happens when the tile's coordinates has odd number.

for example, if the tile's coordinates are (3,3), you will see the problem, but if the coordinates are (4,6) you won't see the cracks.

i first thought this problem may be a driver issue. but i have tried on both ubuntu and mac os, same problem.

what's wrong?

Thanks.

##### Share on other sites
another thing is that those cracks are all horizontal. between two vertically continues tiles. crack doesn't happen between two horizontally adjacent tiles.

these are the coordinates, width and height of two adjacent tiles. i can't see any problem with these numbers, given that they renders perfectly without scaling.

##### Share on other sites
i guess this has something todo with the opengl rasterization method.

if the position coordinate is, say, 31.5,

the opengl rasterization method may round it to 31 or 32.

##### Share on other sites
You should avoid using glScale.

Try to modify the vertex positions, thus resizing the mesh yourself.

There are two good reasons to do so:

- glScale is used every frame meaning your code is resized every frame (not very efficient)
- glScale messes up the normals of a mesh. (Alright, you're not using normals (yet) so this argument might be useless)

assainator

##### Share on other sites
In OpenGL a vertex position x in view (!) gets mapped onto pixel position y, if y < x <= y+1. The center of a pixel is hit if frac(x) == 0.5.

A problem arises at pixel borders where frac(x) must be 0! If, after the transformation (inclusive the scaling) the frac(x) lies just a hundredth above 0, then the next pixel will be used. For meshes, especially 3D meshes, the problem isn't apparent because (1) the vertices are shared, and (2) who cares whether the mesh is shifted by 1 pixel in its entirety.

Hence, for pixel perfect drawing, ensure that the vertex positions in view have a fraction close to 0.5. For example: Assume a face that ranges (in horizontal direction) from l=1.5 to r=21.5. The l gets mapped to pixel column 1, and r gets mapped to pixel column 21. Notice that this covers all pixel columns between 1 and 21 inclusive (!), what means 21 pixels. Assume that we would draw a face just to the right of the former one, i.e. it starts at21.5. This means that OpenGL would draw the pixel at column 21 once with the former face and once with the latter face. That would be a problem (flickering in dependence of the rendering order) and waste of performance. Hence OpenGL suppresses rendering of the rightmost pixel in each line and the bottommost pixel in each row when rendering faces. Hence the actual amount of pixels drawn from the former face ranges from 1 to 20, making 20 pixels.

BTW: Texels have a similar issue: If you want to map pixel-perfect texture mapping, ensure that the texel positions (when multiplied with the count of pixels in that dimension) in use will have a fractional part of 0.5, too. Doing so hits the center of the texel and hence suppresses filtering or nearest neighbour mismatch.

##### Share on other sites
[quote name='assainator' timestamp='1298103668' post='4776245']
You should avoid using glScale.
[/quote]
The artifact will probably go away when translating the tile by the magic amount of 0.5 pixel units. E.g. (using column vectors as is typical for OpenGL)
[b]T[/b]( 0.5, 0.5 ) * [b]S[/b]( s, s ) * [b]v[/b]
where [b]v[/b] is the vertex position in pixel units using whole numbers, s is a small whole-numbered scaling factor and [b]S[/b] its scaling matrix, and [b]T[/b] a translation matrix made from its 0.5 valued arguments. With [b]v[/b] and [b]S[/b] having numbers close to integer values, also [b]S[/b] * [b]v[/b] will do so. Hence adding 0.5 will result in numbers with fractional parts close to 0.5.

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627661
• Total Posts
2978490
• ### Similar Content

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• By Defend
I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
* make lots of VAO/VBO pairs and flip through them to render different objects, or
* make one big VBO and jump around its memory to render different objects.
I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?

• 10
• 12
• 22
• 13
• 33