Author Topic: Mesh optimization  (Read 3169 times)

Offline zbych

  • int
  • **
  • Posts: 75
    • View Profile
Mesh optimization
« on: May 16, 2013, 09:26:37 pm »
Is there any way to disable mesh optimization, or can you add something like a switch to enable/disable it?
It might help me in 2 things:
1) speedup object creation
2) add flat shading
I wanted to create "flat shading" and I wanted to use undocumented method Mesh.addVertexAttributes(), however when I build object some vertices together with my attributes were removed from mesh (or it looks like they were removed) :(
Can you help me with this Egon please?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Mesh optimization
« Reply #1 on: May 16, 2013, 11:04:02 pm »
You can disable it on the object by using http://www.jpct.net/jpct-ae/doc/com/threed/jpct/Object3D.html#disableVertexSharing() and on the loader by using http://www.jpct.net/jpct-ae/doc/com/threed/jpct/Loader.html#setVertexOptimization(boolean). However, this will hurt performance because it increases vertex count of the resulting object.
Anyway, what are you planning to inject into the shader in these attributes just for flat shading?

Offline zbych

  • int
  • **
  • Posts: 75
    • View Profile
Re: Mesh optimization
« Reply #2 on: May 17, 2013, 11:41:29 am »
It helped, but it looks like there is problem on GPU side :(
This is screen of nine objects (every object is build from mesh of 4x4 boxes) in default shading with one light:


I have added attribute to control brightness of selected face. This screen shows when first four values of additional attribute are set to 0 - first face (box wall) of every object is black:


So far so good... but next screen shows when the attributes are set to make all front walls black:


and the same for top wall:


fail  :'(

The scene has no spot lights and ambient light is set to 255 and for every object before calling build() I call disableVertexSharing() . The only thing I can do is to decrease contrast between brighter and darker values.

Offline zbych

  • int
  • **
  • Posts: 75
    • View Profile
Re: Mesh optimization
« Reply #3 on: May 17, 2013, 11:47:07 am »
I forgot, this is vertex shader:

Code: [Select]
precision mediump float;
precision highp int;

uniform mat4 modelViewMatrix;
uniform mat4 modelViewProjectionMatrix;

uniform vec4 additionalColor;
uniform vec4 ambientColor;

uniform vec3 lightPositions[8];

attribute vec4 position;
attribute vec3 normal;
attribute vec4 tangent;
attribute vec2 texture0;
attribute float faceBrightness;

varying vec2 texCoord;
varying vec4 varVertexColor;

void main(void)
{
vec4 normal4 = vec4(normal, 0);
vec4 position4 = modelViewMatrix * position;
vec3 position3 = position4.xyz;// / position4.w;

vec4 eyeNormal4 = modelViewMatrix * normal4;

varVertexColor = vec4(faceBrightness,faceBrightness,faceBrightness,0);//vec4(1,1,1,0);

texCoord = texture0;

gl_Position = modelViewProjectionMatrix * position;
}


and fragment shader:

Code: [Select]
precision mediump float;
precision highp int;


varying vec2 texCoord;
varying vec4 varVertexColor;

uniform sampler2D textureUnit0;
uniform sampler2D textureUnit1;

uniform float alpha;


void main (void)
{
vec4 finalColor = texture2D(textureUnit0, texCoord) * varVertexColor;
gl_FragColor = finalColor;//base;//vertexColor ;//vec4(1, 0, 0, 1);
}

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Mesh optimization
« Reply #4 on: May 17, 2013, 02:51:17 pm »
You have to disable sharing before adding triangles to the object, not just before calling build().

Offline zbych

  • int
  • **
  • Posts: 75
    • View Profile
Re: Mesh optimization
« Reply #5 on: May 17, 2013, 03:23:45 pm »
I don't use method addTriangle(). I have all data in arrays:

Code: [Select]
        currentObject = new Object3D(vertices, uvs, indices, TextureManager.getInstance().getTextureID(_textureAtlas));
        VertexAttributes attribs = new VertexAttributes("faceBrightness", brightnes, VertexAttributes.TYPE_SINGLE_FLOATS);
        currentObject.getMesh().addVertexAttributes(attribs);
        currentObject.setLighting(Object3D.LIGHTING_NO_LIGHTS);
        currentObject.disableVertexSharing();
        currentObject.setShader(_flatShader);
        currentObject.build();

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Mesh optimization
« Reply #6 on: May 17, 2013, 04:42:29 pm »
Well, in that case, jPCT doesn't do anything to the mesh and disableVertexSharing will have no effect. However, the compilation process might interfere with your attributes, because it might join vertices on the GPU side which have different attributes (because it doesn't care about attributes). In that case, the assigned attribute that comes last will win.

Try this jar to see if it helps: http://jpct.de/download/beta/jpct_ae.jar

Offline zbych

  • int
  • **
  • Posts: 75
    • View Profile
Re: Mesh optimization
« Reply #7 on: May 17, 2013, 05:56:35 pm »
Thank you !  :D
I works perfect now.