Gouraud shader

Q and A about functionality and how to solve a special task for your application.

Moderator: Support

Post Reply
VincentPix
Posts: 39
Joined: 06 Nov 2014, 15:07

Gouraud shader

Post by VincentPix » 03 Jun 2016, 16:33

Hi

Is there somewhere the template for gouraud shader in Ventuz ?
including textures, material, lights,...

I want to do a custom vertex noise for an object with gouraud or phong shading.

TVM Cracklings
Posts: 33
Joined: 30 Sep 2015, 10:49

Re: Gouraud shader

Post by TVM Cracklings » 03 Jun 2016, 18:56

This could probably help.

VincentPix
Posts: 39
Joined: 06 Nov 2014, 15:07

Re: Gouraud shader

Post by VincentPix » 03 Jun 2016, 23:31

thanks for this. :)

VincentPix
Posts: 39
Joined: 06 Nov 2014, 15:07

Re: Gouraud shader

Post by VincentPix » 06 Jun 2016, 12:56

hello

I've got a strange result with this shader (same code than in the "lit" topic with the corrections as indicated. I also delete the direct color on the shader and use instead the diffuse of the material.

Code: Select all

#define pi 3.14159265358979324

float4x4 mMVP : WORLDVIEWPROJECTION;
float4x4 mMV : WORLDVIEW;
float4x4 mIMV : WORLDVIEWINVERSE;

struct VDL{
	float3 D;
	float3 S;
	float3 A;
	float3 vDir;};

struct VPL{
	float3 D;
	float3 S;
	float3 A;
	float3 vPos;
	float3 vAtt;
	float Range;};

struct VSL{
	int Type;
	float3 D;
	float3 S;
	float3 A;
	float3 vPos;
	float3 vDir;
	float3 Att;
	float4 FalloffO2I;};

struct VL{
	int Type;
	float3 D;
	float3 S;
	float3 A;
	float3 vPos;
	float3 vDir;
	float3 vAtt;
	float4 FalloffO2I;};


VDL DL[8]<   string SasBindAddress = "Ventuz.DirectionalLight[*]";>;
VPL PL[8]<   string SasBindAddress = "Ventuz.PointLight[*]"      ;>;
VSL SL[8]<   string SasBindAddress = "Ventuz.SpotLight[*]"      ;>;
VL  L[8] <   string SasBindAddress = "Ventuz.Light[*]"         ;>;

int DLCount<   string SasBindAddress = "Ventuz.NumDirectionalLights"   ;>;
int PLCount<   string SasBindAddress = "Ventuz.NumPointLights"         ;>;
int SLCount<   string SasBindAddress = "Ventuz.NumSpotLights"         ;>;
int LCount<    string SasBindAddress = "Ventuz.NumLights"            ;>;

struct a2v{
	float4 vPos : POSITION0;
	float4 vNormal : NORMAL0;
};

struct v2f{
	float4 vPos : POSITION0;
	float4 vPos2 : TEXCOORD1;
	float4 vPosVS : TEXCOORD0;
	float4 vNormalVS : NORMAL0;
};

float Alpha <   string SasBindAddress = "Ventuz.Material.Alpha" ;>;
float sp <   string SasBindAddress = "Ventuz.Material.Sharpness" ;>;
float3 c <   string SasBindAddress = "Ventuz.Material.Diffuse" ;>; 



v2f vert(a2v Input){
	float4x4 mITMV = transpose(mIMV);
	v2f Output;
	Output.vPos = mul(Input.vPos, mMVP);
	Output.vNormalVS = mul(Input.vNormal, mITMV);
	Output.vPosVS = mul(Input.vPos, mMV);
	Output.vPos2 = Output.vPos;
	return Output;
}

float4 frag(v2f Input) : COLOR{
	//   Directional Light:

	float3 n = normalize(Input.vNormalVS.xyz);
	float3 l = -normalize(DL[0].vDir);
	float3 v = normalize(Input.vPos2.xyz);
	float3 h = (l+v)/2;
	float4 LCoef = lit(dot(n,l),dot(n,h),sp);
	return LCoef*float4(c,Alpha);
}

technique tech0{
	pass pass0{
		vertexshader = compile vs_3_0 vert();
		pixelshader = compile ps_3_0 frag();
      
		ZEnable = TRUE;
	}
}


If you try it, you'll see that it gives a picture with strange colors(Red for diffuse and a yellow specular) instead of grey levels (light and material are set to white except ambiant and emissive, set to black)

Does someone have a hint to correct this problem ?

thanks

User avatar
lerou
Posts: 331
Joined: 06 Sep 2013, 07:14
Location: Hamburg, Germany

Re: Gouraud shader

Post by lerou » 06 Jun 2016, 14:04

You're using lit() which returns several lighting coefficients in one vector. See here for details. Then you multiply the coefficients with your color:

return LCoef *float4(c,Alpha):

r * 1
g * diffuse coef
b * specular coef
a * 1

What you need to do is use the correct coef for the correct light and material component. Have a look at the light equation. Also you're ignoring all colors expect for the diffuse color anyway ;)

If you do:

return LCoef.y *float4(c,Alpha);

You'd get a gray gradient. But you're still ignoring the light color for the diffuse channel. And the other channels (ambient, specular, emissive). You should multiply material.diffuse with light.diffuse and the diffuse coef. Same for the others. Sum up the results. Well, see light equation for all the details ;)

cheers,
rou

VincentPix
Posts: 39
Joined: 06 Nov 2014, 15:07

Re: Gouraud shader

Post by VincentPix » 06 Jun 2016, 15:49

Thank you very much !!
(I feel stupid now I have the explanation)

VincentPix
Posts: 39
Joined: 06 Nov 2014, 15:07

Re: Gouraud shader

Post by VincentPix » 07 Jun 2016, 11:58

Did this
(support only Directional Lights type but can handle 8 sources. Manage only one texture for now. But the lighting fx seems quite good)

Code: Select all

float4x4 mMVP : WORLDVIEWPROJECTION;
float4x4 mMV : WORLDVIEW;
float4x4 mIMV : WORLDVIEWINVERSE;

struct VDL{
	float3 D;
	float3 S;
	float3 A;
	float3 vDir;};

struct VPL{
	float3 D;
	float3 S;
	float3 A;
	float3 vPos;
	float3 vAtt;
	float Range;};

struct VSL{
	int Type;
	float3 D;
	float3 S;
	float3 A;
	float3 vPos;
	float3 vDir;
	float3 Att;
	float4 FalloffO2I;};

struct VL{
	int Type;
	float3 D;
	float3 S;
	float3 A;
	float3 vPos;
	float3 vDir;
	float3 vAtt;
	float4 FalloffO2I;};


VDL DL[8]<   string SasBindAddress = "Ventuz.DirectionalLight[*]";>;
VPL PL[8]<   string SasBindAddress = "Ventuz.PointLight[*]"      ;>;
VSL SL[8]<   string SasBindAddress = "Ventuz.SpotLight[*]"      ;>;
VL  L[8] <   string SasBindAddress = "Ventuz.Light[*]"         ;>;

int DLCount<   string SasBindAddress = "Ventuz.NumDirectionalLights"   ;>;
int PLCount<   string SasBindAddress = "Ventuz.NumPointLights"         ;>;
int SLCount<   string SasBindAddress = "Ventuz.NumSpotLights"         ;>;
int LCount<    string SasBindAddress = "Ventuz.NumLights"            ;>;

float4 TexSize[8] <	string SasBindAddress = "Ventuz.Textures[*].Size"; >;
float4x4 TexMap[8] < string SasBindAddress = "Ventuz.Textures[*].Mapping"; >;
uint4 TexInfo[8] < string SasBindAddress = "Ventuz.Textures[*].Info"; >;
int TexCount < string SasBindAddress = "Ventuz.NumTextures"; >;


texture Tex <
	bool SasUiVisible = false;
>;
sampler Samp = sampler_state    
{
	Texture   = (Tex);          
	MipFilter = LINEAR;         
	MinFilter = LINEAR;
	MagFilter = LINEAR;
};

struct a2v{
	float4 vPos : POSITION0;
	float4 vNormal : NORMAL0;
	float4 TexCd : TEXCOORD0;
};

struct v2f{
	float4 vPos : POSITION0;
	float4 vPos2 : TEXCOORD1;
	float4 vPosVS : TEXCOORD2;
	float4 vNormalVS : NORMAL0;
	float4 TexCD : TEXCOORD0;
};

float AlphaCol <   string SasBindAddress = "Ventuz.Material.Alpha" ;>;
float SharpCol <   string SasBindAddress = "Ventuz.Material.Sharpness" ;>;
float3 DiffCol <   string SasBindAddress = "Ventuz.Material.Diffuse" ;>;
float3 SpeCol <   string SasBindAddress = "Ventuz.Material.Specular" ;>; 
float3 AmbCol <   string SasBindAddress = "Ventuz.Material.Ambient" ;>;
float3 EmiCol <   string SasBindAddress = "Ventuz.Material.Emissive" ;>;



v2f vert(a2v Input){
	float4x4 mITMV = transpose(mIMV);
	v2f Output;
	Output.vPos = mul(Input.vPos, mMVP);
	Output.vNormalVS = mul(Input.vNormal, mITMV);
	Output.vPosVS = mul(Input.vPos, mMV);
	Output.vPos2 = Output.vPos;
	Output.TexCD = Input.TexCd;
	return Output;
}

float4 frag(v2f Input) : COLOR{
	//   Directional Light:
	float3 n = normalize(Input.vNormalVS.xyz);
	float3 l;
	float3 v = normalize(Input.vPos2.xyz);
	float3 h;
	float4 LCoef = float4(0,0,0,0);
	float4 AllLCoef = float4(0,0,0,0);
	float3 DiffL = float3(0,0,0);
	float3 SpecL = float3(0,0,0);
	float3 AmbL = float3(0,0,0);
	
	for( int i = 0;i < DLCount; i++ )
	{
		l = -normalize(DL[i].vDir);
		h = l; //h = (l+v)/2;
		LCoef = lit(dot(n,l),dot(n,h),SharpCol);
		AllLCoef += LCoef;
		DiffL += LCoef.g * DL[i].D;
		SpecL += LCoef.b * DL[i].S;
		AmbL += DL[i].A;
	}
	/*
	//   Textures:
	for( int j = 0;j < TexCount; j++ )
	{
		
	}
	*/
	float4 ColTex = float4 (0,0,0,1);
	if (TexCount > 0)
	{	ColTex = tex2D(Samp, mul(Input.TexCD, TexMap[0]).xy);}
		
	
	
	float3 Amb = AmbCol + AmbL;
	float3 Diff = AllLCoef.g * DiffCol * DiffL;
	float3 Spec = AllLCoef.b * SpeCol * SpecL ;
	float4 ColOut = ColTex * float4(Amb + Diff + Spec + EmiCol , AlphaCol);
	return ColOut;
	}

technique tech0{
	pass pass0{
		vertexshader = compile vs_3_0 vert();
		pixelshader = compile ps_3_0 frag();
      
		ZEnable = TRUE;
	}
}

I'm trying now to manage all textures and mapping in this shader but I've not found for now how to do and where to do a loop on the sampler for each texture.

to be continued...

User avatar
lerou
Posts: 331
Joined: 06 Sep 2013, 07:14
Location: Hamburg, Germany

Re: Gouraud shader

Post by lerou » 07 Jun 2016, 12:13

a long time ago, we posted a sample shader. It wasn't perfect, had a few bugs, but maybe this helps.

Post Reply