Author Topic: Changing u/v coordinates on the fly  (Read 8489 times)

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Changing u/v coordinates on the fly
« on: September 12, 2010, 05:37:01 am »
I have several quads for which I want to change their textures' u/v coordinates periodically.  What is the proper way to do this in jPCT-AE?

Currently, I am calling build( false ) on the Object3D, then to change the u/v coordinates:

Code: [Select]
                tex1 = new TextureInfo(
                          TextureManager.getInstance().getTextureID( "Front" ),
                          quadUVs.u1, quadUVs.v1, quadUVs.u2, quadUVs.v2,
                          quadUVs.u3, quadUVs.v3 );
                tex2 = new TextureInfo(
                          TextureManager.getInstance().getTextureID( "Front" ),
                          quadUVs.u3, quadUVs.v3, quadUVs.u4, quadUVs.v4,
                          quadUVs.u1, quadUVs.v1 );
                gifBox.getPolygonManager().setPolygonTexture( polyIdOffset,
                                                              tex1 );
                gifBox.getPolygonManager().setPolygonTexture( polyIdOffset + 1,
                                                              tex2 );
quadUVs is just a simple class that holds 8 floats to represent the u/v coordinates of the four corners of a rectangle.  These values are changed periodically.  I pulled the code out of a previous normal jPCT project, so I'm pretty sure I'm using the correct u/v values.  However, if there is nothing obvious you can think of that I need to do to make u/v changes work, I'll try and create a simpler test-case to look at.

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: Changing u/v coordinates on the fly
« Reply #1 on: September 12, 2010, 05:53:42 am »
NVM, I figured it out.  Need to call touch() after the u/v coordinates change.

Offline gamerfan

  • int
  • **
  • Posts: 97
    • View Profile
Re: Changing u/v coordinates on the fly
« Reply #2 on: October 18, 2011, 08:52:38 am »
I am also trying to understand it. When you talk about changing UV values periodically means , what are the possible values that a texture co-ordinates can accept ? can you please elaborate.Thanks

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Changing u/v coordinates on the fly
« Reply #3 on: October 18, 2011, 11:49:37 am »
Possible values are almost -infinite to + infinite (as long as the GPU can handle it...) It depends on what you want to do...

Offline gamerfan

  • int
  • **
  • Posts: 97
    • View Profile
Re: Changing u/v coordinates on the fly
« Reply #4 on: October 18, 2011, 02:36:56 pm »
I want to create a ripple effect in water.So in this case, do I need to change all the values of the texture cordinates from - to +? As paul suggested I have created a timer task in which I am periodically changing the texture co-ordinates.You can see the following code
Code: [Select]
private void createWaterMoveEffect() {
     System.out.println(" inside the createWaterMoveEffect()....")   ;
     for(int i = 0; i<10; i++) {
       quaduv.setU1(i);
       quaduv.setV1(i + 2);
     }
     textureInfo1 = new TextureInfo(TextureManager.getInstance().getTextureID("water"), quaduv.getU1(), quaduv.getV1(),quaduv.getU2(),quaduv.getV2(), quaduv.getU3(), quaduv.getV3());
     textureInfo2 = new TextureInfo(TextureManager.getInstance().getTextureID("water"), quaduv.getU3(), quaduv.getV3(),quaduv.getU4(),quaduv.getV4(), quaduv.getU1(), quaduv.getV1());
     water.getPolygonManager().setPolygonTexture(0, textureInfo1);
     water.getPolygonManager().setPolygonTexture(1, textureInfo2);
   //  water.compile(true, true)
     water.touch();
   }
This method is getting called from a timer task.


Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Changing u/v coordinates on the fly
« Reply #5 on: October 18, 2011, 08:45:41 pm »
Might be a better idea to use a texture matrix for this (http://www.jpct.net/jpct-ae/doc/com/threed/jpct/Object3D.html#setTextureMatrix(com.threed.jpct.Matrix)), because it's easier to handle and faster to render.

Offline gamerfan

  • int
  • **
  • Posts: 97
    • View Profile
Re: Changing u/v coordinates on the fly
« Reply #6 on: October 19, 2011, 07:31:23 am »
But how to set it ? Is there any example related to this?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Changing u/v coordinates on the fly
« Reply #7 on: October 20, 2011, 06:48:28 am »
Well...i don't fully understand the question. I've already given you the link to the documention of the setter for it. It's a matrix. Everything that can be done to a matrix can be done to this one, i.e. you can translate, scale, rotate, shear...but it will be applied to the texture coordinates instead of the geometry. To move a texture, a translate of any kind should do the trick for example.

Offline gamerfan

  • int
  • **
  • Posts: 97
    • View Profile
Re: Changing u/v coordinates on the fly
« Reply #8 on: October 20, 2011, 07:32:49 am »
I was working out logic this way. Please correct me if I am wrong.
1. I am creating a translation matrix with the texture co-ordinates( ?)
2. In a timer task I will call a method that will change the co-ordinates of a translation matrix.
3. And will call Object3D.setTextureMatrix( with this matrix)
4. call touch() method on Object3D.

Also, how can I get the texture co-ordonates?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Changing u/v coordinates on the fly
« Reply #9 on: October 20, 2011, 08:19:12 am »
No, the texture coordinates don't matter. You simply translate the matrix so that in turn it translates the coordinates. You don't need to know the actual coordinates for this. I wouldn't put that in a timer task. jPCT-AE isn't thread safe. Fiddling around with the matrix while rendering in another thread is asking for trouble.

Touch is not needed for this.

Offline gamerfan

  • int
  • **
  • Posts: 97
    • View Profile
Re: Changing u/v coordinates on the fly
« Reply #10 on: October 20, 2011, 12:00:45 pm »
It seems to be working. :). This is the code that does the thing:
Code: [Select]
private void createWaterMoveEffect1() {
       System.out.println(" texture x: " + matrix.getTranslation().x);
       if(matrix.getTranslation().x > 15) {
           //SimpleVector sv = matrix.getTranslation();
           //sv.set(1.0f, 0.0f, 0.0f);
           //matrix.getTranslation().x = 1.0f;
           //matrix.translate(1.0f, 0.0f, 0.0f);
           matrix = new Matrix();
       }
       matrix.translate(1.0f, 0.0f, 0.0f);
       water.setTranslationMatrix(matrix);
   }
I am calling this method from the onDrawFrame() method.There are something here.

1.The water seems to be moving which means that entire plane object  itself is moving in the back ground.

2. At some point, this plane moves away from the screen.So I re-initialized the matrix after some units on x-axis.So it starts all over again.That is what has been done in if block of the code.Is there any better way to do this? I had tried something in the code related to this but that  is not working.The commented block.

3.When the water layer animates on the background, it cuts away some of the 3d model and it becomes deformed.Basically the object is a cylinder.The upper portion of the cylinder is getting chopped off when the water starts animating.Why this is happening like this?


Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Changing u/v coordinates on the fly
« Reply #11 on: October 20, 2011, 12:17:43 pm »
Don't move the plane, move the texture. You are calling setTranslationMatrix() where you should actually call setTextureMatrix() instead.

Offline gamerfan

  • int
  • **
  • Posts: 97
    • View Profile
Re: Changing u/v coordinates on the fly
« Reply #12 on: October 20, 2011, 01:43:56 pm »
when I use like this below,
Code: [Select]
water.setTextureMatrix(matrix);
nothing happens,the back ground texture is not moving.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Changing u/v coordinates on the fly
« Reply #13 on: October 20, 2011, 08:34:54 pm »
Are you using the OpenGL ES 2.0 renderer?

Edit: If so, please try this jar instead: http://jpct.de/download/beta/jpct-ae.jar. The 1.24 version has a flaw in the shader code that causes translations to be ignored.
« Last Edit: October 20, 2011, 08:58:06 pm by EgonOlsen »

Offline gamerfan

  • int
  • **
  • Posts: 97
    • View Profile
Re: Changing u/v coordinates on the fly
« Reply #14 on: October 21, 2011, 07:18:15 am »
No I am not using OpenGL ES 2.0 renderer.