texture mapping on uneven 2D terrain

General discussion about the R.U.B.E editor
Post Reply
anupgupta@inbox.com
Posts: 6
Joined: Wed Jul 10, 2013 6:02 pm

texture mapping on uneven 2D terrain

Post by anupgupta@inbox.com »

the game i am making is like hill climb racing.. my car moves ahead and the ground is uneven and i have mapped the texture on the ground which work great. but after playing the for a while my ground texture starts to stretch and keeps on stretching as i move forward.
IMG_0181.png
IMG_0181.png (20.69 KiB) Viewed 7025 times
IMG_0186.png
IMG_0186.png (19.68 KiB) Viewed 7025 times
after some debugging, i found that the texture co-ordinates in x direction become too large and the image distorts after the co-ordinates go beyond 256.

Code: Select all

//calculating texture co-ordinates
for(short i = 0; i < MAX_SEGMENTS; ++i)
{
CCPoint pt = hillBottomVertices[i];
hillBottomTexCoords[i].x = pt.x/(terrainBottomTexSize/CC_CONTENT_SCALE_FACTOR());
hillBottomTexCoords[i].y = 1.0f -  pt.y/(terrainBottomTexSize/CC_CONTENT_SCALE_FACTOR());

CCLOG("point: %f %f", hillBottomTexCoords[i].x, hillBottomTexCoords[i].y);
}
the texture coordinates values are like
point: 0.937500 0.594344
point: 0.937500 1.000000
point: 1.093750 0.565063
point: 1.093750 1.000000

point: 2.031250 0.347375
point: 2.031250 1.000000 …so on
point: 166.875000 0.280531
point: 166.875000 1.000000 … till

point: 260.937500 0.308594
point: 260.937500 1.000000 .. until the X co-ordinates goes beyond 256 which is also the size of the texture(size = 256) ..that the stretch starts to appear.

is there any solution which can be used to handle this issue. Any Suggestions
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: texture mapping on uneven 2D terrain

Post by iforce2d »

I had a similar problem recently and it seems that even though the texture parameter is given as a 32 bit float, the graphics driver stores a much lower precision variable. I think this is because usually texture coordinates are defined in the range 0 - 1 so a lower precision value is fine for most purposes.

As you know these values are supposed to be wrapped so that 1.23 will become 0.23 etc., but due to lower precision storage it seems that this will not be calculated correctly for higher values. You could try converting this value yourself, so instead of giving the value 260.9375 you would give the value 0.9375 instead.
Post Reply