Author Topic: multi texturing  (Read 1694 times)

Offline draga

  • byte
  • *
  • Posts: 20
    • View Profile
multi texturing
« on: November 27, 2009, 04:40:56 pm »
Hello,

I have a question:
I have a terrain in obj-file and a mtl with texture info.
Now I want to add another texture over the whole terrain AND I want to make a uv-repeat by 200*200.

Is this possible? and how?

best regards,
draga

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11776
    • View Profile
    • http://www.jpct.net
Re: multi texturing
« Reply #1 on: November 27, 2009, 05:37:22 pm »
Yes, it is (when using the hardware renderer. The software one doesn't support multi texturing). But the obj-format itself doesn't support multiple texture layers, so you have to do it in code. You can use something like this:

Code: [Select]
private void reTexture(Object3D obj, String t2, float tileFactor) {
Logger.log("ReTexturing...", Logger.MESSAGE);
PolygonManager pm=obj.getPolygonManager();
int t2Id=TextureManager.getInstance().getTextureID(t2);
int end=pm.getMaxPolygonID();
for (int i=0; i<end; i++) {
int t1=pm.getPolygonTexture(i);
SimpleVector uv0=pm.getTextureUV(i, 0);
SimpleVector uv1=pm.getTextureUV(i, 1);
SimpleVector uv2=pm.getTextureUV(i, 2);
TextureInfo ti=new TextureInfo(t1, uv0.x, uv0.y, uv1.x, uv1.y, uv2.x, uv2.y);

uv0.scalarMul(tileFactor);
uv1.scalarMul(tileFactor);
uv2.scalarMul(tileFactor);

ti.add(t2Id, uv0.x, uv0.y, uv1.x, uv1.y, uv2.x, uv2.y, TextureInfo.MODE_MODULATE);
pm.setPolygonTexture(i, ti);
}
Logger.log("ReTexturing...done!", Logger.MESSAGE);
}

and call it like

Code: [Select]
reTexture(plane, "layer2", 2f);
However, this method calculates the uv-coordinates of the second layer relative to the first one's. Maybe that's sufficient for you, if you know the size of your terrain. If not, you would have to add some code that calculates the new uv-coords based on the actual coordinates of the vertices. I haven't done this for this simple example, but it should get you started.

When used on the advanced example posted in the wiki, the result looks like this:

« Last Edit: November 27, 2009, 05:39:43 pm by EgonOlsen »

Offline draga

  • byte
  • *
  • Posts: 20
    • View Profile
Re: multi texturing
« Reply #2 on: November 27, 2009, 06:54:06 pm »
yes, Thanks it works ;)

I have another question:
I want to add the 2nd texture as a detail layer, so I have tried to make TextureInfo.ADD,
but now the terrain is overlighten, what should I do ?
I want a additive Layer. With  TextureInfo.MODE_MODULATE is to dark...
Should I play with Alpha on texture?


Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11776
    • View Profile
    • http://www.jpct.net
Re: multi texturing
« Reply #3 on: November 27, 2009, 09:05:17 pm »
Alpha doesn't help here. One option is to simply darken the detail map. If you want to do this in your code, you can use a simple ITextureEffect like this one (taken from an old project of mine):

Code: [Select]
private static class BrightnessModifier implements ITextureEffect {

   private float mul=1f;

   public void init(Texture t) {
   }

   public void incBrightness(float br) {
      mul+=br;
   }

   public void decBrightness(float br) {
      mul-=br;
   }

   public boolean containsAlpha() {
   return false;
   }
   
   public void setBrightness(float br) {
      mul=br;
   }

   public void apply(int[] dest, int[] src) {
      for (int i=0; i<dest.length; i++) {
         int rgb=src[i];
         int r=rgb>>16;
         int g=(rgb>>8)&255;
         int b=rgb&255;
         r*=mul;
         g*=mul;
         b*=mul;
         if (r>255) {
            r=255;
         }
         if (g>255) {
            g=255;
         }
         if (b>255) {
            b=255;
         }
         dest[i]=(r<<16)|(g<<8)|b;
      }
   }
}


and apply it like so:

Code: [Select]
BrightnessModifier bm=new BrightnessModifier();
bm.decBrightness(0.75f);
Texture l2=TextureManager.getInstance().getTexture("layer2");
l2.setEffect(bm);
l2.applyEffect();

Another option is to download a very early version of jPCT 1.20, which adds an ADD_SIGNED-blending mode to TextureInfo, where 50% means no change, and everything higher will be added while everything lower will be subtracted.

Offline draga

  • byte
  • *
  • Posts: 20
    • View Profile
Re: multi texturing
« Reply #4 on: November 28, 2009, 11:01:41 am »
thanks, it works ;)