Changing u/v coordinates on the fly

Started by paulscode, September 12, 2010, 05:37:01 AM

Previous topic - Next topic

paulscode

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:

                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.

paulscode

NVM, I figured it out.  Need to call touch() after the u/v coordinates change.

gamerfan

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

EgonOlsen

Possible values are almost -infinite to + infinite (as long as the GPU can handle it...) It depends on what you want to do...

gamerfan

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

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.


EgonOlsen

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.

gamerfan

But how to set it ? Is there any example related to this?

EgonOlsen

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.

gamerfan

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?

EgonOlsen

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.

gamerfan

It seems to be working. :). This is the code that does the thing:

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?


EgonOlsen

Don't move the plane, move the texture. You are calling setTranslationMatrix() where you should actually call setTextureMatrix() instead.

gamerfan

when I use like this below,

water.setTextureMatrix(matrix);

nothing happens,the back ground texture is not moving.

EgonOlsen

#13
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.

gamerfan

No I am not using OpenGL ES 2.0 renderer.