SOLVED CG shader cubemap is rotating with scenenode, why?

Problems building or running the engine, queries about how to use features etc.
Post Reply
User avatar
toglia
Gnome
Posts: 336
Joined: Sat Dec 08, 2007 4:28 am
Location: Canada

SOLVED CG shader cubemap is rotating with scenenode, why?

Post by toglia » Tue Mar 24, 2009 7:49 am

I did a recent port of the simple glass shader from rendermonkey, but the cubemap seems to be rotating when I rotate my scenenode, like they were parented.

Material

Code: Select all

vertex_program test5_vs cg
{
	source test5.cg
	entry_point main_vs
	profiles vs_1_1 arbvp1
}

fragment_program test5_ps cg
{
	source test5.cg
	entry_point main_ps
	profiles ps_2_0 arbfp1
}


material test5
{
	technique
	{
		pass
		{			
			scene_blend alpha_blend			
			depth_write off
			
			vertex_program_ref test5_vs
			{
				param_named_auto worldViewProj worldviewproj_matrix		
				param_named_auto eyePosition camera_position_object_space
			}
			
			fragment_program_ref test5_ps
			{
			}			
			
			texture_unit
			{
				cubic_texture reflection.dds combinedUVW				
				tex_address_mode clamp
			}	
		}		
	}
}
CG

Code: Select all

void main_vs(
			float4 inPosition : POSITION,
			float3 inNormal   : NORMAL,	
			
			out float4 outPosition : 	POSITION,
			out float3 outNormal :		TEXCOORD0,
			out float3 outViewVec :		TEXCOORD1,	
						
			uniform float4x4 worldViewProj,
			uniform float3 eyePosition
			)
{

	outPosition = mul(worldViewProj, inPosition);	
	outNormal = normalize(inNormal);
	outViewVec = eyePosition - inPosition;	
}



void main_ps(
		float3 inNormal: TEXCOORD0, 
		float3 inViewVec: TEXCOORD1,
		
		out float4 outColor	: COLOR,		
		
		uniform samplerCUBE ReflectTex
		)
{  	
	float v = dot(normalize(inViewVec), inNormal);

	float3 reflectionVec = reflect(-inViewVec, inNormal);
	float3 reflection = texCUBE(ReflectTex, reflectionVec);   

	outColor = float4(reflection, 1 - v);
	
}
Last edited by toglia on Wed Mar 25, 2009 9:23 am, edited 1 time in total.
0 x

User avatar
Gucman
Orc
Posts: 467
Joined: Fri Dec 15, 2006 2:09 pm
Location: Poland, Lodz

Re: CG shader cubemap is rotating with scenenode, why?

Post by Gucman » Tue Mar 24, 2009 10:35 am

Well You are doing your calculations in object space so they are actually parented :)
Calculate your outViewVec (VS) = inViewVec (PS) in world space and it will be ok.
0 x

User avatar
toglia
Gnome
Posts: 336
Joined: Sat Dec 08, 2007 4:28 am
Location: Canada

Re: CG shader cubemap is rotating with scenenode, why?

Post by toglia » Tue Mar 24, 2009 5:28 pm

I have just a couple weeks with shaders... Could you specify me how to do that change... :? , I tried playing with the eyePosition in world space but its giving worst outcomes. Thanks! :D
0 x

User avatar
Gucman
Orc
Posts: 467
Joined: Fri Dec 15, 2006 2:09 pm
Location: Poland, Lodz

Re: CG shader cubemap is rotating with scenenode, why?

Post by Gucman » Tue Mar 24, 2009 5:43 pm

Code: Select all

void main_vs(
         float4 inPosition : POSITION,
         float3 inNormal   : NORMAL,   
         
         out float4 outPosition :    POSITION,
         out float3 outNormal :      TEXCOORD0,
         out float3 outViewVec :      TEXCOORD1,   
                  
         uniform float4x4 world,
         uniform float4x4 ViewProj,
         uniform float3 eyePositionWorld   // in world space = param_named_auto eyePositionWorld camera_position
         )
{
   float4 worldPosition = mul(world,inPosition); // this is position of vertex in world space
   outPosition = mul(ViewProj, worldPosition));   
   outNormal = normalize(mul(world,inNormal)); // normal have to be in world space either
   outViewVec = eyePositionWorld - worldPosition;  // now our view vector is in world space too
}
// fragment shader is OK I think
It is not tested but should do what we want ;)
0 x

User avatar
toglia
Gnome
Posts: 336
Joined: Sat Dec 08, 2007 4:28 am
Location: Canada

Re: CG shader cubemap is rotating with scenenode, why?

Post by toglia » Tue Mar 24, 2009 7:55 pm

Gucman thanks for the help! really appreciated, but there seems to be a problem with the outNormal code (I suspect):

VS parameters:

Code: Select all

param_named_auto world world_matrix				
param_named_auto viewProj viewproj_matrix				
param_named_auto eyePositionWorld camera_position 
VS

Code: Select all

void main_vs(
			float4 inPosition:	POSITION,
			float3 inNormal:	NORMAL,
			out float4 outPosition:		POSITION,
			out float3 outNormal:		TEXCOORD0,
			out float3 outViewVec:		TEXCOORD1,
			uniform float4x4 world,			
			uniform float4x4 viewProj,			
			uniform float3 eyePositionWorld
){
	float4 worldPosition = mul(world,inPosition);	
	outPosition = mul(viewProj, worldPosition);   
	outNormal = normalize(mul(world,inNormal).xyz);	
	outViewVec = normalize(eyePositionWorld - worldPosition); 
}
PS

Code: Select all

void main_ps(
		float3 inNormal:	TEXCOORD0, 
		float3 inViewVec:	TEXCOORD1,		
		out float4 outColor	: COLOR,		
		uniform samplerCUBE ReflectTex
){  
	float v = dot(inViewVec, inNormal);
	float3 reflectionVec = reflect(-inViewVec, inNormal);
	float3 reflection = texCUBE(ReflectTex, reflectionVec);
	outColor = float4(reflection, 1 - v);
}
This is like the easiest shader of all times but I cant get it to work :x ...
0 x

User avatar
Gucman
Orc
Posts: 467
Joined: Fri Dec 15, 2006 2:09 pm
Location: Poland, Lodz

Re: CG shader cubemap is rotating with scenenode, why?

Post by Gucman » Tue Mar 24, 2009 8:01 pm

but there seems to be a problem with the outNormal code (I suspect):
What kind of problem? Could you post attachment of how it looks and description of how you would like it look?
0 x

User avatar
toglia
Gnome
Posts: 336
Joined: Sat Dec 08, 2007 4:28 am
Location: Canada

Re: CG shader cubemap is rotating with scenenode, why?

Post by toglia » Tue Mar 24, 2009 8:30 pm

Well if I leave:

Code: Select all

outNormal = normalize(mul(world,inNormal).xyz);
it turns to the ogre's default white material.

If I change it with:

Code: Select all

outNormal = inNormal;
it looks like:
Image

In rendermonkey looks like this:
Image
0 x

User avatar
Gucman
Orc
Posts: 467
Joined: Fri Dec 15, 2006 2:09 pm
Location: Poland, Lodz

Re: CG shader cubemap is rotating with scenenode, why?

Post by Gucman » Tue Mar 24, 2009 10:12 pm

Well if I leave:

Code: Select all
outNormal = normalize(mul(world,inNormal).xyz);
it turns to the ogre's default white material.
There should be an error in the log file. Did you check the log to find out wat is wrong?
Possibly it should be
outNormal = normalize(mul(float3x3(world),inNormal).xyz);
or
outNormal = normalize(mul(world,float4(inNormal,0)).xyz);
0 x

User avatar
toglia
Gnome
Posts: 336
Joined: Sat Dec 08, 2007 4:28 am
Location: Canada

Re: CG shader cubemap is rotating with scenenode, why?

Post by toglia » Wed Mar 25, 2009 9:23 am

Yeah! that did it! Thanks a lot mate! :P
0 x

User avatar
stump
Gnoblar
Posts: 1
Joined: Sat May 07, 2011 5:42 pm

Re: SOLVED CG shader cubemap is rotating with scenenode, why

Post by stump » Sat May 07, 2011 6:07 pm

great examp...but...
can't understand one thing...
i want to make cube_mapping sample but with my own cg shader. I've already made dynamic cube texture generation and used this shader...but position of object reflection is wrong and it's translates with my cam...why???
if there is any link to the solution??can't find it...
0 x

User avatar
hebdemnobad
Goblin
Posts: 274
Joined: Wed Feb 09, 2005 5:09 pm
Location: catskill escarpment, new york, usa
Contact:

Re: CG shader cubemap is rotating with scenenode, why?

Post by hebdemnobad » Wed Jan 04, 2012 7:37 pm

Gucman wrote:
Well if I leave:

Code: Select all
outNormal = normalize(mul(world,inNormal).xyz);
it turns to the ogre's default white material.
There should be an error in the log file. Did you check the log to find out wat is wrong?
Possibly it should be
outNormal = normalize(mul(float3x3(world),inNormal).xyz);
or
outNormal = normalize(mul(world,float4(inNormal,0)).xyz);
this shader works for me too...is there a way to make it transparent?
0 x

User avatar
hebdemnobad
Goblin
Posts: 274
Joined: Wed Feb 09, 2005 5:09 pm
Location: catskill escarpment, new york, usa
Contact:

Re: CG shader cubemap is rotating with scenenode, why?

Post by hebdemnobad » Thu Jan 05, 2012 7:54 pm

hebdemnobad wrote:
Gucman wrote:
Well if I leave:

Code: Select all
outNormal = normalize(mul(world,inNormal).xyz);
it turns to the ogre's default white material.
There should be an error in the log file. Did you check the log to find out wat is wrong?
Possibly it should be
outNormal = normalize(mul(float3x3(world),inNormal).xyz);
or
outNormal = normalize(mul(world,float4(inNormal,0)).xyz);
this shader works for me too...is there a way to make it transparent?
my questions was answered by changing:

outColor = float4(reflection, 1 - v)

to

outColor = float4(reflection, .2 - v)

or anything under 1 in the 1-v
0 x

Post Reply