image Stretching

Started by
6 comments, last by anupgupta 9 years, 11 months ago

Hello

I am facing a peculiar issue in my game, i am currently making a game which is an endless car driving game using cocos2dx and box2d for iOS platform.

[attachment=21145:IMG_0176.PNG] [attachment=21146:IMG_0179.PNG]

As you can see the ground below, initially when i start the ground image is fine, but as i progress through the game driving continuously, the ground image starts stretching.

I am also attaching the reference code of the ground(terrain.zip), so that i can get some inputs as to where the problem could be.

Any Suggestions

Advertisement
void CTerrainLayer::generateTerrainTexture()
{
    //terrain top texture                                                                                              
    ccTexParams txpTop = {GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT};
    gameplaySceneRef->gameImages[IMG_GAME_TERRAINTOP]->getTexture()->setTexParameters(&txpTop);
    terrainTopTexId = gameplaySceneRef->gameImages[IMG_GAME_TERRAINTOP]->getTexture()->getName();
    terrainTopTexSize = gameplaySceneRef->gameImages[IMG_GAME_TERRAINTOP]->getContentSize().height/CC_CONTENT_SCALE_FACTOR()/2;

    //terrain bottom texture                                                                                           
    ccTexParams txpBottom = {GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT};
    gameplaySceneRef->gameImages[IMG_GAME_TERRAINBOTTOM]->getTexture()->setTexParameters(&txpBottom);
    terrainBottomTexId = gameplaySceneRef->gameImages[IMG_GAME_TERRAINBOTTOM]->getTexture()->getName();
    terrainBottomTexSize = gameplaySceneRef->gameImages[IMG_GAME_TERRAINBOTTOM]->getContentSize().height*CC_CONTENT_SCALE_FACTOR()/2;

    //CCLog("texSize:%f %f", terrainTopTexSize, terrainBottomTexSize);                                                 
}
Hmmm... I don't really understand your code but, why would you divide by CC_CONTENT_SCALE_FACTOR for the top texture and multiply by it instead for the bottom texture?


… but as i progress through the game driving continuously, the ground image starts stretching.

If the effect actually starts after already driving for a while, the problem may be due to an accumulation. Actually, there is a location in getTerrainNextPoint which is IMO problematic: Therein you continuously add 200 onto a float variable (namely the x co-ordinate of the terrain control points). I have no clue with which rate this is done, but if it happens often enough the float variable's precision will suffer. Could you check for the current value of that variable when the effect starts to be noticeable?

BTW: I would expect the problem to have an effect on the top terrain texture, too, but the hills visible in the two screen shots seem to be scaled well. However, that may be due to a difference in the values of terrainBottomTexSize and terrainTopTexSize.

..why would you divide by CC_CONTENT_SCALE_FACTOR for the top texture and multiply by it instead for the bottom texture?

my bottom texture image size was 512x512 and top texture size was 32x32 .. that i did to scale down the textures to make it look appropriate on the screen.

Actually, there is a location in getTerrainNextPoint which is IMO problematic: Therein you continuously add 200 onto a float variable (namely the x co-ordinate of the terrain control points). I have no clue with which rate this is done

just roughly what i am doing is i have calculated 10 points(MAX_HILL_POINTS) which are 200 pixels apart of each other and then divided the whole 10 points into 100 segments (MAX_SEGMENTS)..and then in the update i keep checking if the points go outside screen then delete the point from the left and then add one point to the right and recalculate the terrain and thus this gives me endless terrain. the new point which is added is also 200 pixels apart in x and random in y in a given range. and the cardinal spline algorithm gives me a smooth curve across points(100 segments). i guess the issue could be in the generatecoordinates() function..but am not sure and need help to understand it.

the 200 is added only when one point is deleted and when the next is added..thus every point added is 200 pixel apart in X direction and random in Y direction between a set range.

checking of all the 10 points is done every frame if any of the point is gone outside screen, and if it goes outside the screen then delete the point and add a new point as the next point (getTerrainNextPoint) to the hill points array and then recalculate the whole terrain again and map the texture on it. thus with only 10 points divided into 100 segments i get a constantly generated terrain which is endless.

?generally, though i have now tried to follow a tutorial for texture mapping on the terrain and i revised my code..here is the link : http://www.codeproject.com/Articles/727918/d-Textures-in-cocos-d-x-V

but the problem remains as it is..the terrain bottom texture still gets stretched even after implementing this tutorial.

Attaching the revised code ([attachment=21149:terrain.zip]) for your reference and to get some inputs for this query.

Please suggest something or if you have a better way of doing it do let me know. rolleyes.gif .. the problem i am facing is in texture mapping. i guess

[attachment=21147:IMG_0181.png] [attachment=21148:IMG_0186.png]

You should learn how to debug your code. There are many things you can do to gain an understanding of what your code is doing. For instance, you could run the program in a debugger, get to the point where the textures look wrong and then execute the code step by step, checking intermediate values to see what variables have unexpected values. Or you write out a piece of text to a file every frame, with the texture coordinates you are using, to see where they go wrong.

Debugging is not particularly fun, but it's an important skill.


Debugging is not particularly fun, but it's an important skill.

smile.png .. yes..i did try to debug it and gather much of the data i could with respect to the vertices and texture co-ordinates which are being generated, and trying to make some meaning out of the data.

though i think the code area:


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

   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.

As i move ahead the x co-ordinates keeps on increasing and the texture goes on stretching more.

My guess is that is due to decreased precision of the part of the value to the right of the decimal point as your floating point numbers get larger.

A solution would be to "reset" all your texture coordinates once they get too large, so that everything is as close to 0 as possible.

A solution would be to "reset" all your texture coordinates once they get too large, so that everything is as close to 0 as possible.

the texture co-ordinates in the x direction are being generated with respect to the terrain vertices (hillBottomVertices[i]) which are being created as the vehicle moves ahead in the x-direction

how do i make sure that the texture co-ordinates in the x direction are close to 0...or maybe reach 256 (texture size = 256) and then reset.

This topic is closed to new replies.

Advertisement