Mesh optimization

Started by zbych, May 16, 2013, 09:26:37 PM

Previous topic - Next topic

zbych

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?

EgonOlsen

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?

zbych

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.

zbych

I forgot, this is vertex shader:


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:


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);
}

EgonOlsen

You have to disable sharing before adding triangles to the object, not just before calling build().

zbych

I don't use method addTriangle(). I have all data in arrays:


        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();

EgonOlsen

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

zbych

Thank you !  :D
I works perfect now.