billboard normals not facing camera

Problems building or running the engine, queries about how to use features etc.
Post Reply
SuperNess
Kobold
Posts: 39
Joined: Wed Feb 12, 2014 12:52 am
x 9

billboard normals not facing camera

Post by SuperNess »

hello,

I have some billboards and billboard sets and I expect their normals to face camera, meaning that lighting should work on them as if they were regular 3d quads rotating around (if the light is behind the camera they should be lit, as they face the camera. if the light is behind them they should be dark).
I've seen this post:

http://ogre3d.org/forums/viewtopic.php?f=5&t=63350

Where Anicka says "billboards are always facing camera. So every normal on a billboard is -(eye vector)."

So it should work right?
What I'm seeing is that all my billboards have constant normals, which appear to be (0, 0, 1). looking at them from different angle does not change the normals.

this is how I set them:

Code: Select all

        m_billboard->setMaterialName(billMat, "resources_doodads");
	m_billboard->setBillboardType(Ogre::BBT_ORIENTED_COMMON);
	m_billboard->setCommonUpVector(Vector3(0, 1,0));
	m_billboard->setCommonDirection(Vector3(0, 1, 0));
	m_billboard->setRenderingDistance(METER_TO_UNITS(FLAG_CULLING_DISTANCE_IN_METERS));
	m_billboard->setQueryFlags(RAY_QUERY_DOODADS);
	m_billboard->setDefaultDimensions(width, height);
	m_billboard->setUseAccurateFacing(true);
	m_billboard->setCullIndividually(true);
what am I doing wrong here?

thanks!
User avatar
lingfors
Hobgoblin
Posts: 525
Joined: Mon Apr 02, 2007 12:18 am
Location: Sweden
x 79

Re: billboard normals not facing camera

Post by lingfors »

Billboard vertices don't have normals?

Edit: Found the relevant part of the BillboardSet code. This is what you get in your billboard vertices:

Code: Select all

        /* Alloc positions   ( 1 or 4 verts per billboard, 3 components )
                 colours     ( 1 x RGBA per vertex )
                 indices     ( 6 per billboard ( 2 tris ) if not point rendering )
                 tex. coords ( 2D coords, 1 or 4 per billboard )
        */
SuperNess
Kobold
Posts: 39
Joined: Wed Feb 12, 2014 12:52 am
x 9

Re: billboard normals not facing camera

Post by SuperNess »

interesting, thanks.
so any idea how am I supposed to use lighting with billboards?
User avatar
lingfors
Hobgoblin
Posts: 525
Joined: Mon Apr 02, 2007 12:18 am
Location: Sweden
x 79

Re: billboard normals not facing camera

Post by lingfors »

SuperNess wrote:interesting, thanks.
so any idea how am I supposed to use lighting with billboards?
Calculate the normal you want in the shader?
Use auto_param to send a normal-like value to the shader?
Implement your own Billboard system?
Modify the Ogre code to include normals?
Kenshido
Gremlin
Posts: 153
Joined: Tue Jun 09, 2009 9:31 am
Location: Russia
x 12
Contact:

Re: billboard normals not facing camera

Post by Kenshido »

SuperNess wrote:interesting, thanks.
so any idea how am I supposed to use lighting with billboards?
This is my implementation of billboard lighting for JonhJ's PagedGeometry.

Vertex and fragment shader params:

Code: Select all

vertex_program ImpostorAlphaVP cg
{
	source impostors.cg
	entry_point impostorAlpha_vp
	profiles vs_1_1 arbvp1

	default_params
	{
		param_named_auto	worldViewProj		worldviewproj_matrix
		param_named_auto	lightPositionObj	light_position_object_space 0
		param_named_auto	normal				view_direction
		param_named_auto	tangent				view_side_vector
		param_named_auto	binormal			view_up_vector
	}
}

fragment_program ImpostorAlphaFP cg
{
	source impostors.cg
	entry_point impostorAlpha_fp
	profiles ps_2_0 arbfp1

	default_params
	{
		param_named_auto	ambientColor		ambient_light_colour
		param_named_auto	diffuseColor		light_diffuse_colour 0
	}
}
Vertex and fragment programs:

Code: Select all

// Expand a range-compressed vector
float4 expand(float4 v)	
{	
	return v * 2 - 1;	
}

// -------------------------   IMPOSTOR    -------------------------
// ------------------------- VERTEX SHADER -------------------------
void impostorAlpha_vp (	
	float4			iPosition:		POSITION,
	float3			iNormal			: NORMAL,
	float4			iColor			: COLOR,
	float2			iUV			: TEXCOORD0,

	uniform float4		lightPositionObj,
	uniform float4		cameraPositionObj,
	uniform float4x4	worldViewProj,
	uniform float3		normal,
	uniform float3		tangent,
	uniform float3		binormal,
	uniform float		uScroll,
	uniform float		vScroll,
	uniform float4		preRotatedQuad[4],

	out float4		oPosition		: POSITION,
	out float2		oUv			: TEXCOORD0,
	out float3		oTSLightDir		: TEXCOORD1,
	out float4		oColor			: COLOR,
	out float		oFog			: FOG
)	
{	
	float4 vCenter = float4(iPosition.x, iPosition.y, iPosition.z, 1.0f);
	float4 vScale = float4(iNormal.x, iNormal.y, iNormal.x, 1.0f);
	float4 positionSprite = vCenter + (preRotatedQuad[iNormal.z] * vScale);
	oPosition = mul(worldViewProj, positionSprite );

	float3 lightDir = normalize(lightPositionObj.xyz - positionSprite .xyz * lightPositionObj.w);
	float3x3 rotation = float3x3(tangent, binormal, -normal);
	oTSLightDir = mul(rotation, lightDir);

	oColor = iColor;
	oUv = iUV;
	oUv.x += uScroll;
	oUv.y += vScroll;
	oFog = oPosition.z;
}

// ------------------------- FRAGMENT SHADER -------------------------
void impostorAlpha_fp (	
	float2			iUV			: TEXCOORD0,	
	float3			iTSLightDir		: TEXCOORD1,	
	float4			iColor			: COLOR,		
	
	uniform float4		ambientColor,	
	uniform float4		diffuseColor,	
	uniform sampler2D	diffuseTexture		: register(s0),
	uniform sampler2D	normalTexture		: register(s1),
	
	out float4		oColor			: COLOR
)	
{	
	float4 diffuseTex = tex2D(diffuseTexture, iUV.xy);
	float4 normalTex = normalize(expand(tex2D(normalTexture, iUV.xy)));

	float diffuseFactor = saturate(dot(normalTex.xyz, iTSLightDir));

	oColor.rgb = diffuseTex.rgb * (diffuseColor.rgb * diffuseFactor * iColor.rgb + ambientColor.rgb);
	oColor.a = diffuseTex.a;
}
Diffuse texture
Diffuse texture
Normalmap Texture
Normalmap Texture
The Result
The Result
Last edited by Kenshido on Mon Mar 16, 2015 5:27 am, edited 1 time in total.
SuperNess
Kobold
Posts: 39
Joined: Wed Feb 12, 2014 12:52 am
x 9

Re: billboard normals not facing camera

Post by SuperNess »

thank you Kenshido and Lingfors (although I'm not sure how to interpret your excessive use of question marks...)
I was hoping to avoid shaders at this point but I guess its a good time to start working on them (I was hoping to continue with my mockup graphics with default pipeline a bit longer :))
Post Reply