cocos2d-x repeating textures in 3d

751 views Asked by At

I would very much like to create a repeating texture on a 3D object.

I tried exporting from Maya to .obj. The material file (.mtl) looks like this:

newmtl lambert10SG
illum 4
Kd 0.00 0.00 0.00
Ka 0.00 0.00 0.00
Tf 1.00 1.00 1.00
map_Kd -s 0.1 0.1 grass.jpg
Ni 1.00

the line "map_Kd -s 0.1 0.1 grass.jpg" should indicate that the texture is repeating. However this doesn't work at all. The texture doesn't show until I remove "-s 0.1 0.1". Then it gets stretched.

I tried exporting to .fbx and then convert to .c3b. Same result. Texture gets stretched.

Then I tried creating my own texture. I know that in OpenGL I would have to set texture coordinates to >1 to make the texture repeat itself. These seems to be equivalent to maxS and maxT in the texture(?).

This is my texture setup:

cocos2d::Image *textImage = new (std::nothrow) cocos2d::Image();
textImage->initWithImageFile("grass.jpg");

cocos2d::Texture2D *texture = new (std::nothrow)cocos2d::Texture2D();
texture->initWithImage(textImage);

cocos2d::Texture2D::TexParams texParam;
texParam.wrapS = GL_REPEAT;
texParam.wrapT = GL_REPEAT;
texParam.minFilter = GL_LINEAR;
texParam.magFilter = GL_LINEAR;

texture->setTexParameters(texParam);
texture->setMaxS(10.0f);
texture->setMaxT(10.0f);

sprite->getMesh()->setTexture(texture);

Texture is still stretching.

From searching the internet it seems I would be able to set texture coordinates on a 2D sprite in Cocos with the setTextureRect function. However this doesn't seem to exist for sprite3D.

Any ideas will be very much appreciated!

1

There are 1 answers

0
Rasmusr On

UPDATE:

I managed to get a texture tiling by editing the .obj file manually.

Obviously the CCObjLoader doesn't understand the line in the material file (.mtl):

map_Kd -s 0.1 0.1 grass.jpg

Removing "-s 0.1 0.1" makes the loader recognize the texture (still stretched though).

After this I had to manually change all vt coordinates in the .obj file, by multiplying with 10. Still the texture didn't repeat, until I changed the texture parameters to GL_REPEAT instead of GL_CLAMP_TO_EDGE.

cocos2d::Texture2D::TexParams texParam;
texParam.wrapS = GL_REPEAT;
texParam.wrapT = GL_REPEAT;
texParam.minFilter = GL_LINEAR;
texParam.magFilter = GL_LINEAR;

sprite->getMesh()->getTexture()->setTexParameters(texParam);

This is not a solution to my problem as such, as I would need the app to recognize when a texture should repeat automatically.

I haven't yet deciphered where texture coordinates are kept in the cocos2d structure, hence haven't been able to change these after the sprite has been loaded. A solution could be to fix the objLoader, however this is not very prone to cocos updates. Or maybe make a small .obj file fixer. None of these seems to be ideal solutions...