jPCT - a 3d engine for Java => Support => Topic started by: draga on November 27, 2009, 04:40:56 pm

Title: multi texturing
Post by: draga on November 27, 2009, 04:40:56 pm

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,
Title: Re: multi texturing
Post by: EgonOlsen 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);


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:

Title: Re: multi texturing
Post by: draga 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?

Title: Re: multi texturing
Post by: EgonOlsen 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) {

   public void decBrightness(float br) {

   public boolean containsAlpha() {
   return false;
   public void setBrightness(float 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;
         if (r>255) {
         if (g>255) {
         if (b>255) {

and apply it like so:

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

Another option is to download a very early version of jPCT 1.20 (http://www.jpct.net/download/beta/jpct.jar), 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.
Title: Re: multi texturing
Post by: draga on November 28, 2009, 11:01:41 am
thanks, it works ;)