Author Topic: VertexAttributes.update () issue  (Read 1438 times)

Offline mxar

  • int
  • **
  • Posts: 73
    • View Profile
VertexAttributes.update () issue
« on: September 01, 2015, 09:34:35 am »
Hi,

I want to render an OBJ object3d.
Part of the initialization code is:

Code: [Select]
GroupOfModels3D = Loader.loadOBJ(objStream, mtlStream, scale);
object3D = Object3D.mergeAll(GroupOfModels3D);
object3D.build();//<----- removing this line fixes the issue


GLSLShader blendingShader = new  GLSLShader( Loader.loadTextFile(res.openRawResource(R.raw.vertexshader)),Loader.loadTextFile(res.openRawResource(R.raw.fragmentshader)));

//assign vertex colors
 int  numberOfVertices = object3D.getMesh().getUniqueVertexCount();
 
     
  float[]     offScreenColorArray = new float[numberOfVertices * 4];

  for (int i = 0; i < offScreenColorArray.length; i++) {

  offScreenColorArray[i] = 0.0f;

  if (i > 0) {

  if (i % 4 == 0)
  offScreenColorArray[i] = 0; //red color

  if (i % 4 == 1)
  offScreenColorArray[i] = 0;//green color

  if (i % 4 == 2)
  offScreenColorArray[i] = 1;//blue color

  if (i % 4 == 3)
  offScreenColorArray[i] = 1; //transparency
  // 1-> opaque 0-> total
  // transparent
  }
  }
 
        vattrs = new VertexAttributes("colorinfo",offScreenColorArray,VertexAttributes.TYPE_FOUR_FLOATS);
        vattrs.setDynamic(true);
        object3D.getMesh().addVertexAttributes(vattrs);
   


At the beginning the object is rendered without using the blendingShader GLSLShader with no problem.

But later on run time i want to use the blendingShader  with a different array of color (red ) like this one:

   
Code: [Select]

int numberOfVertices = object3D.getMesh().getUniqueVertexCount();

        for(int i = 0; i < fixedColor.length; i++){
       
        fixedColor[i] = 0.0f;
           
            if(i>0){
           
              if(i%4 == 0)
              fixedColor[i] = 1;//red color
             
              if(i%4 == 1)
              fixedColor[i] = 0; //green color
             
              if(i%4 == 2)
              fixedColor[i] = 0; //blue color
             
              if(i%4 == 3)
              fixedColor[i] = 1;   //transparency
            }
           
        }
      

After i assign the new color i call the following lines:

Code: [Select]
      vattrs.update(fixedColor, 0);
       
      object3D.setShader(blendingShader);

But in this case the object3D is rendered with blue color (offScreenColorArray) instead of red color (fixedColor)


After a lot of hours i found out that if i remove the call of object3D.build() at the beginning, the object3d is rendered with red color  (fixedcolor array)



The used shaders are:

Code: [Select]
//------------------------------- vertexshader -------------------------

attribute vec4 colorinfo;
uniform mat4 modelViewProjectionMatrix;
attribute vec4 position;
varying vec4 vertexColor;





void main(void)
{
     vertexColor = colorinfo;
     gl_Position = modelViewProjectionMatrix * position;

}


Code: [Select]
//-------------------------------- fragmentshader -------------------
varying vec4 theColor;
varying vec4 vertexColor;


void main(void)
{
 gl_FragColor = vertexColor;
}

What is the problem?

Thanks in Advance
« Last Edit: September 01, 2015, 10:24:15 am by EgonOlsen »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11745
    • View Profile
    • http://www.jpct.net
Re: VertexAttributes.update () issue
« Reply #1 on: September 01, 2015, 12:29:17 pm »
That's because the object has to be compiled in dynamic mode to be aware of changes to the vertex attributes. By default, jPCT compiles an object automagically based on the information it has about the object when you are calling build() on it. In this case, if you call build() right at the beginning, it isn't aware of the presence of vertex attributes, so it compiles it in static mode. If you don't call build(), the engine does so on it's own at render time. In that case, the vertex attributes are already there, so it chooses dynamic.
You have several options to deal with this:

  • Omit build() just as you did. I don't encourage people to do this, but it's an option.
  • Delay the call to build() to after assigning the vextex attributes.
  • Leave build() where it is and add a call to Object3D.compile(true); before calling build().

Offline mxar

  • int
  • **
  • Posts: 73
    • View Profile
Re: VertexAttributes.update () issue
« Reply #2 on: September 01, 2015, 01:55:56 pm »

Thanks for your answer.

Is it possible to create and add an object3d in the world (world.add(Object3D)) while the the renderer is active ?.

In many cases I receive exception while i try to add an Object3d to the world.

Thanks in advance


Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11745
    • View Profile
    • http://www.jpct.net
Re: VertexAttributes.update () issue
« Reply #3 on: September 01, 2015, 05:40:20 pm »
Of course it is, but not from outside the rendering thread. Have a look here: http://www.jpct.net/wiki/index.php?title=Thread_safety

Offline mxar

  • int
  • **
  • Posts: 73
    • View Profile
Re: VertexAttributes.update () issue
« Reply #4 on: September 02, 2015, 04:07:04 pm »

Hi,

thanks for the answer.

When you say rendering thread what you mean?
Do you have an example code?

It is safe to manipulate jPCT related instances (like instances of World, Object3D...)
inside the onDrawFrame() method?

In my application when I create  new Object3D model I added it in a queue (Vector) and in onDrawFrame() I read the queue .
If there is a pending Object3D model (not added in the World) then I call the method world.add(Object3D ).

The reading of queue is done before the world.draw(fb).

It is safe this way?

Thanks in advance

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11745
    • View Profile
    • http://www.jpct.net
Re: VertexAttributes.update () issue
« Reply #5 on: September 02, 2015, 05:31:49 pm »
Yes, that's the rendering thread. It don't see how this can go wrong unless something is fishy with your queue. Which exception are you getting exactly?

Offline mxar

  • int
  • **
  • Posts: 73
    • View Profile
Re: VertexAttributes.update () issue
« Reply #6 on: September 02, 2015, 06:42:29 pm »
Hi,

I will try to reproduce the issue again. I think happens only if the Object3dD model has texture.

A log info when the exception happens is:


D/ARRenderer(4310): world.draw() start

08-31 20:23:38.128: W/Adreno-ES20(4310): <core_glUseProgram:1525>: GL_INVALID_VALUE

08-31 20:23:38.128: I/jPCT-AE(4310): Creating buffers...

08-31 20:23:38.128: E/jPCT-AE(4310): [ 1441041818133 ] - ERROR: before: glError 1281

08-31 20:23:38.128: D/ARRenderer(4310): ################## world.draw exception ##################

08-31 20:23:38.128: D/ARRenderer(4310): world.draw() end

08-31 20:23:38.138: D/ARRenderer(4310): onSurfaceChanged start


08-31 20:23:38.138: D/ARRenderer(4310): onSurfaceChanged fb.dispose 1


The word.draw(fb) is in try/catch and when the exception happens the " ################## world.draw exception ##################" is printed.

I will  check again the source code.

Thanks in Advance


Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11745
    • View Profile
    • http://www.jpct.net
Re: VertexAttributes.update () issue
« Reply #7 on: September 02, 2015, 08:00:12 pm »
Strange. Are you fiddling around with OpenGL calls directly in your code somewhere? Or are your textures very large?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11745
    • View Profile
    • http://www.jpct.net
Re: VertexAttributes.update () issue
« Reply #8 on: September 02, 2015, 08:02:30 pm »
If you can reproduce it, try to der the Logger to debug and post the complete log. Please wrap the log in code-tags when posting it...it will be much more readable that way.

Offline mxar

  • int
  • **
  • Posts: 73
    • View Profile
Re: VertexAttributes.update () issue
« Reply #9 on: September 02, 2015, 09:59:22 pm »

Hi,

one texture has side 2048x2048,two have 1024x512 and the 4 rest  have smaller.

But the same issue happens with smaller size textures.

I check carefully the code and i will try to reproduce it.

The application is built with vuforia library.

I noticed that the exception happens when the application has find the trucker (marker) at the same time the object3d model is added to the World.

If the application has not find the trucker (marker) at the time the model is added to World, but finds it after a few seconds, then there is no problem.

When an Augmented reality application finds a trucker (marker) then draws the object3d model.


Thanks for your time

I will post the complete log when i reproduce the exception again.


Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11745
    • View Profile
    • http://www.jpct.net
Re: VertexAttributes.update () issue
« Reply #10 on: September 02, 2015, 10:42:06 pm »
Maybe Vuforia causes some GL error that it doesn't catch. In that case, simply adding a new object triggers a check from jPCT when the object gets compiled, but that doesn't mean that the error actually happens there. GL will store such errors forever as long as nobody checks for them.
If you are sure that the app works fine otherwise, try to set Config.glIgnoreErrors to true. You might need the latest beta for that: jpct.de/download/beta/jpct_ae.jar

Offline mxar

  • int
  • **
  • Posts: 73
    • View Profile
Re: VertexAttributes.update () issue
« Reply #11 on: September 02, 2015, 11:06:49 pm »
Thanks,

perhaps some direct OpenGL calls from Vuforia cause the exception.
Part of Vuforia source code in my application calls directly some OpenGL methods

I will search deeply in my source code to address the issue.

I will inform you for the results.

Thanks for your time

Offline mxar

  • int
  • **
  • Posts: 73
    • View Profile
Re: VertexAttributes.update () issue
« Reply #12 on: September 12, 2015, 10:37:03 pm »
Hi
I finally found a way to solve the problem.

When I create a Object3D  i dont add it directly to the world calling the world.add(Object3D).

I call an AsyncTask having the following code

         @Override
         protected String doInBackground(Augmented3dModel... arg0) {
         
         
AR3dModel ar3dModel = arg0[0];
         
                    try {
                                 Thread.sleep(200);
                                 ar3dModel .shouldBeAdded = true;
                             } catch (InterruptedException e) {
                               Thread.interrupted();
                             }
                     return null;
                  }

                            In the OnDrawFrame i added the following code:


                                 if(ar3dModel .shouldBeAdded == true){
                                   world.add( ar3dModel .getObject3d());//adds the created Object3D
                                   ar3dModel .shouldBeAdded = false;
                                 }
               


AR3dModel  is a wrapper class of the Object3D.

Perhaps the delay of 200ms helps to fix the problem.


Best Regards

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11745
    • View Profile
    • http://www.jpct.net
Re: VertexAttributes.update () issue
« Reply #13 on: September 12, 2015, 10:53:25 pm »
Sounds...hacky...but if it solves the problem (I've just no idea why...)...