Part of the bump mapped model looks segmented by contours

Anything and everything that's related to OGRE or the wider graphics field that doesn't fit into the other forums.
Post Reply
machineonpointer
Kobold
Posts: 35
Joined: Fri Dec 06, 2013 4:15 am

Part of the bump mapped model looks segmented by contours

Post by machineonpointer »

In Blender , the model looks smoothly. But on android phone, the rendered model with bump map shader applied looks like contour, not smooth, it's unacceptable. Is it possible to change the glsles shader to improve the effect?


bump map Vertex shader

Code: Select all

#version 100
precision mediump int;
precision mediump float;


void FFP_VertexFog_Linear(in mat4 mWorldViewProj, in vec4 pos, in vec4 fogParams, out float oFogFactor)
{
    vec4 vOutPos    = mWorldViewProj * pos;
    float distance  = abs(vOutPos.w);
    float fogFactor = (fogParams.z - distance) * fogParams.w;
    oFogFactor      = clamp(fogFactor, 0.0, 1.0);
}

uniform vec4 lightPosition; // object space
uniform vec3 eyePosition;   // object space
uniform mat4 worldViewProj;
uniform	vec4 gFogParams;

attribute vec4 position;
attribute vec3 normal;
attribute vec3 tangent;
attribute vec2 uv0;

varying vec2 vUV0;
varying vec3 vTSLightDir;
varying vec3 vTSHalfAngle;
varying float vEyeDist;
varying	float oFogFactor;

void main()
{
	// calculate output position
	gl_Position = worldViewProj * position;
	vUV0 = uv0;
	vEyeDist = sqrt( (position.x - eyePosition.x) * (position.x - eyePosition.x) +
	                 (position.y - eyePosition.y) * (position.y - eyePosition.y) +
                     (position.z - eyePosition.z) * (position.z - eyePosition.z) );

	/*
	value of lightPosition:
	For Point lights:       (pos.x, pos.y, pos.z, 1.0f)    =>  lightDir = lightPosition.xyz - position
	For directional lights: (-dir.x, -dir.y, -dir.z, 0.0f) =>  lightDir = lightPosition.xyz = (-dir.x, -dir.y, -dir.z)
	So the following expression to compute the lightDir is correct
	*/
	vec3 lightDir = normalize(lightPosition.xyz - (position * lightPosition.w).xyz);

	// Calculate the binormal (NB we assume both normal and tangent are already normalised)
	vec3 binormal = cross(normal, tangent);
	
	// Form a rotation matrix out of the vectors
	mat3 rotation = mat3(tangent, binormal, normal);

	// Transform the light vector according to this matrix
	//combine the tangent and normal
	vTSLightDir = rotation * lightDir;

	// Calculate half-angle in tangent space
	vec3 eyeDir = normalize(eyePosition - position.xyz);
	vec3 halfAngle = normalize(eyeDir + lightDir);
	//combine the direction of light and eye relative to the point
	vTSHalfAngle = rotation * halfAngle;

	FFP_VertexFog_Linear(worldViewProj, position, gFogParams, oFogFactor);
}

//	mat3 rotation = mat3(vec3(tangent[0], binormal[0], normal[0]),
//						vec3(tangent[1], binormal[1], normal[1]),
//						vec3(tangent[2], binormal[2], normal[2]));

bump map Fragment shader

Code: Select all

#version 100
precision mediump int;
precision mediump float;

vec3 expand(vec3 v)
{
	return (v - 0.5) * 2.0;
}

uniform vec4 lightDiffuse;
uniform vec4 lightSpecular;
uniform sampler2D diffuseMap;
uniform sampler2D normalMap;
uniform vec4 fogColour;

varying vec2 vUV0;
varying vec3 vTSLightDir;
varying vec3 vTSHalfAngle;
varying float vEyeDist;
varying	float oFogFactor;

void main()
{
	//get bump map vector, again expand from range-compressed
	vec3 normal = expand(texture2D(normalMap, vUV0).xyz);  //range: [-1, 1]

    vec4 diffuseTex = texture2D(diffuseMap, vUV0);

    //lightDir affects the diffuse part
    vec3 lightDir = normalize(vTSLightDir);
    vec4 diffusePart = lightDiffuse * clamp(dot(normal, lightDir), 0.0, 1.0);
    //float diffuseFactor = pow(clamp(dot(normal, lightDir), 0.0, 1.0), 2.0);
    //vec4 diffusePart = lightDiffuse * diffuseFactor;


    // Pre-raise the specular exponent to the eight power
    // the direction between eye direction and original light direction affects the specular part
    vec3 halfAngle = normalize(vTSHalfAngle);
	float specularFactor = pow(clamp(dot(normal, halfAngle), 0.0, 1.0), 12.0);
    vec4 specularPart = lightSpecular * specularFactor;

	// Calculate dot product for diffuse
	vec4 baseColor = vec4(diffuseTex.xyz + diffusePart.xyz + specularPart.xyz, 1);

    gl_FragColor = mix(fogColour, baseColor, oFogFactor);
	//gl_FragColor = vec4(diffuseTex.xyz + diffusePart.xyz + specularPart.xyz, 1);
}

Bump map material

Code: Select all

material redBoardSpecularBumpMaterial
{
    // This is the preferred technique which uses both vertex and
	// fragment programs, supports coloured lights
	technique
	{
		pass
		{
		    iteration once_per_light
            alpha_to_coverage off
            colour_write on
            cull_hardware clockwise
            depth_check on
            depth_func less_equal
            depth_write on
            illumination_stage
            light_clip_planes off
            light_scissor off
            lighting off
            normalise_normals off
            polygon_mode solid
            scene_blend one zero
            scene_blend_op add
            shading gouraud
            transparent_sorting on

			// Vertex program reference: BumpMappingSpecularVp.glsles
			vertex_program_ref BumpMap/BumpMapVPSpecularGLSLES
			{
				param_named_auto lightPosition light_position_object_space 0
				param_named_auto eyePosition camera_position_object_space
				param_named_auto worldViewProj worldviewproj_matrix
				param_named_auto gFogParams fog_params
			}

			// Fragment program: BumpMappingSpecularFp.glsles
			fragment_program_ref BumpMap/BumpMapFPSpecularGLSLES
			{
				param_named_auto lightDiffuse light_diffuse_colour 0
				param_named_auto lightSpecular light_specular_colour 0
				param_named_auto fogColour fog_colour
			}

            texture_unit diffuseMap
			{
				texture env_diffuse.tex
				tex_address_mode wrap
                scale 1.0 1.0
                tex_coord_set 0
                colour_op modulate
			}

			texture_unit normalmap
			{
				texture env_norm.tex
				tex_address_mode wrap
                scale 1.0 1.0
                tex_coord_set 0
                colour_op modulate
			}
		}
	}
}
Last edited by machineonpointer on Tue Jul 29, 2014 11:09 am, edited 1 time in total.
User avatar
tod
Troll
Posts: 1394
Joined: Wed Aug 02, 2006 9:41 am
Location: Bucharest
x 94
Contact:

Re: Part of the bump mapped model looks like contours

Post by tod »

What is contours?
machineonpointer
Kobold
Posts: 35
Joined: Fri Dec 06, 2013 4:15 am

Re: Part of the bump mapped model looks like contours

Post by machineonpointer »

tod wrote:What is contours?
Some area of the rendered model looks like segmented by many closed lines like contour, the colors of segmented area looks different obviously.

Image
User avatar
tod
Troll
Posts: 1394
Joined: Wed Aug 02, 2006 9:41 am
Location: Bucharest
x 94
Contact:

Re: Part of the bump mapped model looks segmented by contour

Post by tod »

What about a picture of the result?
machineonpointer
Kobold
Posts: 35
Joined: Fri Dec 06, 2013 4:15 am

Re: Part of the bump mapped model looks segmented by contour

Post by machineonpointer »

tod wrote:What about a picture of the result?
What do you mean? The result without applying bump mapping? Or a snapshot of the rendered result?
The bump map effect looks good on PC in Blender, without any contours .
User avatar
tod
Troll
Posts: 1394
Joined: Wed Aug 02, 2006 9:41 am
Location: Bucharest
x 94
Contact:

Re: Part of the bump mapped model looks segmented by contour

Post by tod »

A picture with the wrong result.
User avatar
c6burns
Beholder
Posts: 1512
Joined: Fri Feb 22, 2013 4:44 am
Location: Deep behind enemy lines
x 138

Re: Part of the bump mapped model looks segmented by contour

Post by c6burns »

It's probably an issue with tangent generation. A picture sure wouldn't hurt.
machineonpointer
Kobold
Posts: 35
Joined: Fri Dec 06, 2013 4:15 am

Re: Part of the bump mapped model looks segmented by contour

Post by machineonpointer »

c6burns wrote:It's probably an issue with tangent generation. A picture sure wouldn't hurt.
The tangent is built as following. Then should I build the tangent manually?

Code: Select all

    
buildTangentVectors ( meshPtr, Ogre::VES_TANGENT );

void buildTangentVectors(const Ogre::MeshPtr& pMesh, const Ogre::VertexElementSemantic& semantic)
    {
        unsigned short src, dest;
        if (!pMesh->suggestTangentVectorBuildParams ( semantic, src, dest ))
        {
            pMesh->buildTangentVectors ( semantic, src, dest );
            Ogre::LogManager::getSingletonPtr ()->logMessage ( "Tangent Vectors built!!!" );
        }
    }
User avatar
cybereality
Hobgoblin
Posts: 563
Joined: Wed Jul 12, 2006 5:40 pm
x 12

Re: Part of the bump mapped model looks segmented by contour

Post by cybereality »

The suspense is killing me. Where is the image?
machineonpointer
Kobold
Posts: 35
Joined: Fri Dec 06, 2013 4:15 am

Re: Part of the bump mapped model looks segmented by contour

Post by machineonpointer »

In the following code segment, the semantic is set as VES_TANGENT, and suggestTangentVectorBuildParams return true, so buildTangentVectors is not called.

Code: Select all

unsigned short src, dest;
        if (!pMesh->suggestTangentVectorBuildParams ( semantic, src, dest ))
        {
            pMesh->buildTangentVectors ( semantic, src, dest );
            Ogre::LogManager::getSingletonPtr ()->logMessage ( "Tangent Vectors built!!!" );
        }
User avatar
c6burns
Beholder
Posts: 1512
Joined: Fri Feb 22, 2013 4:44 am
Location: Deep behind enemy lines
x 138

Re: Part of the bump mapped model looks segmented by contour

Post by c6burns »

Code: Select all

        /** Ask the mesh to suggest parameters to a future buildTangentVectors call, 
            should you wish to use texture coordinates to store the tangents. 
        @remarks
            This helper method will suggest source and destination texture coordinate sets
            for a call to buildTangentVectors. It will detect when there are inappropriate
            conditions (such as multiple geometry sets which don't agree). 
            Moreover, it will return 'true' if it detects that there are aleady 3D 
            coordinates in the mesh, and therefore tangents may have been prepared already.
You can also check if tangents are there by using OgreXMLConverter, and you can generate them using OgreMeshUpgrader
machineonpointer
Kobold
Posts: 35
Joined: Fri Dec 06, 2013 4:15 am

Re: Part of the bump mapped model looks segmented by contour

Post by machineonpointer »

c6burns wrote:

Code: Select all

        /** Ask the mesh to suggest parameters to a future buildTangentVectors call, 
            should you wish to use texture coordinates to store the tangents. 
        @remarks
            This helper method will suggest source and destination texture coordinate sets
            for a call to buildTangentVectors. It will detect when there are inappropriate
            conditions (such as multiple geometry sets which don't agree). 
            Moreover, it will return 'true' if it detects that there are aleady 3D 
            coordinates in the mesh, and therefore tangents may have been prepared already.
You can also check if tangents are there by using OgreXMLConverter, and you can generate them using OgreMeshUpgrader

I generated the tangents using -t option of xmlConverter, but part of the surface of the model still looks like contours. Maybe it has nothing to do with the tangents.
Post Reply