Cel Shading (Cartoon like) with Purebasic & Ogre, How ?

Advanced game related topics
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Re: Cel Shading (Cartoon like) with Purebasic & Ogre, How ?

Post by djes »

Ok, I've been able to test hlsl, as with asm we're forced to use indexed parameters it was a pain.
I've nothing on screen but... I'm not desespered :)
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Re: Cel Shading (Cartoon like) with Purebasic & Ogre, How ?

Post by djes »

Not perfect, as I had to change line 61 as two parameters seems to be 0, but it seems to work. I should do a biggest object to test. We'll see tomorrow.

Example_CelShading.cg file :

Code: Select all

/* Cel shading vertex program for single-pass rendering
   In this program, we want to calculate the diffuse and specular
   ramp components, and the edge factor (for doing simple outlining)
   For the outlining to look good, we need a pretty well curved model.
*/
void main_vp(float4 position	: POSITION,
			 float3 normal		: NORMAL,
			 // outputs
			 out float4 oPosition : POSITION,
			 out float  diffuse		 : TEXCOORD0,
			 out float  specular	 : TEXCOORD1,
			 out float  edge		 : TEXCOORD2,
			 // parameters
			 uniform float3 lightPosition, // object space
			 uniform float3 eyePosition,   // object space
			 uniform float4  shininess,
			 uniform float4x4 worldViewProj)
{
	// calculate output position
	oPosition = mul(worldViewProj, position);

	// calculate light vector
	float3 N = normalize(normal);
	float3 L = normalize(lightPosition - position.xyz);
	
	// Calculate diffuse component
	diffuse = max(dot(N, L) , 0);

	// Calculate specular component
	float3 E = normalize(eyePosition - position.xyz);
	float3 H = normalize(L + E);
	specular = pow(max(dot(N, H), 0), shininess);
	// Mask off specular if diffuse is 0
	if (diffuse == 0) specular = 0;

	// Edge detection, dot eye and normal vectors
	edge = max(dot(N, E), 0);
}

void main_fp(float diffuseIn 	: TEXCOORD0,
			 float specularIn	: TEXCOORD1,
			 float edge		: TEXCOORD2,
			 
			 out float4 colour	: COLOR,
			 
			 uniform float4 diffuse,
			 uniform float4 specular,
			 
			 uniform sampler1D diffuseRamp,
			 uniform sampler1D specularRamp,
			 uniform sampler1D edgeRamp)
{
	// Step functions from textures
	diffuseIn = tex1D(diffuseRamp, diffuseIn).x;
	specularIn = tex1D(specularRamp, specularIn).x;
	edge = tex1D(edgeRamp, edge).x;

// diffuse and specular are not set?
//	colour = edge * ((diffuse * diffuseIn) + 
//					(specular * specularIn));
	colour = edge * ((2 * diffuseIn) + 
					(3 * specularIn));

}
			 
 
shadercube.material file :

Code: Select all

vertex_program Ogre/CelShadingVP hlsl
{
	source Example_CelShading.cg
	target vs_2_0
	entry_point main_vp

	default_params
	{
		param_named_auto lightPosition light_position_object_space 1
		param_named_auto eyePosition camera_position_object_space
		param_named_auto worldViewProj worldviewproj_matrix
		param_named shininess float 10 
	}
}

fragment_program Ogre/CelShadingFP hlsl
{
	source Example_CelShading.cg
	target ps_2_0
	entry_point main_fp
}


material shader
{
	technique
	{
		pass
		{
		
			vertex_program_ref Ogre/CelShadingVP
			{
				// map shininess from custom renderable param 1
				param_named_auto shininess custom 1
			}
			fragment_program_ref Ogre/CelShadingFP
			{
				// map diffuse from custom renderable param 2
				param_named_auto diffuse custom 2
				// map specular from custom renderable param 2
				param_named_auto specular custom 3
			}
			texture_unit
			{
				texture cel_shading_diffuse.png 1d
				tex_address_mode clamp
				filtering none
			}
			texture_unit
			{
				texture cel_shading_specular.png 1d
				tex_address_mode clamp
				filtering none
				tex_coord_set 1
			}
			texture_unit
			{
				texture cel_shading_edge.png 1d
				tex_address_mode clamp
				filtering none
				tex_coord_set 2
			}

		}
	}
	
}
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Re: Cel Shading (Cartoon like) with Purebasic & Ogre, How ?

Post by Rings »

@djes:
thx for your support for this problem.

If you got some freetime during weekend,
Could you provide a working example
(with gfx and PB code) with some screenshots
how it shoud look like ?
i tested with some pics, and got some results.
still not that i expected, but something's going
around in the cube :)
SPAMINATOR NR.1
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Re: Cel Shading (Cartoon like) with Purebasic & Ogre, How ?

Post by djes »

Yep, here is a sample. Download it there : http://djes.free.fr/purebasic/shader_cellshading.zip
Post Reply