www.jpct.net

jPCT-AE - a 3d engine for Android => Support => Topic started by: paulscode on September 12, 2010, 05:37:01 am

Title: Changing u/v coordinates on the fly
Post by: paulscode 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.
Title: Re: Changing u/v coordinates on the fly
Post by: paulscode on September 12, 2010, 05:53:42 am
NVM, I figured it out.  Need to call touch() after the u/v coordinates change.
Title: Re: Changing u/v coordinates on the fly
Post by: gamerfan 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
Title: Re: Changing u/v coordinates on the fly
Post by: EgonOlsen 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...
Title: Re: Changing u/v coordinates on the fly
Post by: gamerfan 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.

Title: Re: Changing u/v coordinates on the fly
Post by: EgonOlsen 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) (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.
Title: Re: Changing u/v coordinates on the fly
Post by: gamerfan on October 19, 2011, 07:31:23 am
But how to set it ? Is there any example related to this?
Title: Re: Changing u/v coordinates on the fly
Post by: EgonOlsen 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.
Title: Re: Changing u/v coordinates on the fly
Post by: gamerfan 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?
Title: Re: Changing u/v coordinates on the fly
Post by: EgonOlsen 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.
Title: Re: Changing u/v coordinates on the fly
Post by: gamerfan 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?

Title: Re: Changing u/v coordinates on the fly
Post by: EgonOlsen 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.
Title: Re: Changing u/v coordinates on the fly
Post by: gamerfan 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.
Title: Re: Changing u/v coordinates on the fly
Post by: EgonOlsen 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 (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.
Title: Re: Changing u/v coordinates on the fly
Post by: gamerfan on October 21, 2011, 07:18:15 am
No I am not using OpenGL ES 2.0 renderer.
Title: Re: Changing u/v coordinates on the fly
Post by: gamerfan on October 21, 2011, 08:28:05 am
I  tried using the latest version of the jar.But still it is not working.Also, I am not using OpenGL ES 2.0. I think  I am using old version only.
Title: Re: Changing u/v coordinates on the fly
Post by: EgonOlsen on October 21, 2011, 10:09:46 am
On which device?
Title: Re: Changing u/v coordinates on the fly
Post by: gamerfan on October 24, 2011, 06:42:53 am
Currently, I am using NetBeans emulator only for development.
Title: Re: Changing u/v coordinates on the fly
Post by: EgonOlsen on October 24, 2011, 07:00:40 am
I'm not sure if this is properly supported by the emulator. Do you have a real device to test it on?
Title: Re: Changing u/v coordinates on the fly
Post by: gamerfan on October 24, 2011, 09:47:47 am
Sorry @EgonOlsen, I do not have any device that supports android at the moment. :'( . But if this implementation will work in android devices in real time, then no problem for me. I can experience this feature later on the real device. At least in the development face, I can just ignore this.Not a problem.I am just going through its features.However, I do not have much idea about this whether this feature will support in android emulator as emulator is purely unknown from the framework related features.

Btw, I am attaching an image to this thread as it would be more clear to you what I am doing.The blue back ground is the water texture and that is the one supposed to be animated.


[attachment deleted by admin]