(GLSL) Dual tone car shader with fresnel and sphere mapping

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
omaremad
Competition winner
Posts: 1027
Joined: Fri Jul 15, 2005 11:30 pm
Location: Cairo,Egypt

(GLSL) Dual tone car shader with fresnel and sphere mapping

Post by omaremad »

Here is a car paint shader, i used sphere mapping(done by shader) for 2 reasons:
:D Its faster to do reatime reflections (one render to texture only)
:cry: irrlicht cant load cubemaps

rember to pass the proper variables

Vertex shader

Code: Select all

uniform vec3 fvLightPosition;
uniform vec3 fvEyePosition;

varying vec2 Texcoord;
varying vec3 ViewDirection;
varying vec3 LightDirection;
varying vec3 Normal;
//get this from irr (important for sphere mapping)
uniform mat4 matWorldInverseTranspose;
varying vec2 reflcoord;
void main( void )
{
   gl_Position = ftransform();
   Texcoord    = gl_MultiTexCoord0.xy;
    
   vec4 fvObjectPosition = gl_ModelViewMatrix * gl_Vertex;
   
   ViewDirection  = fvEyePosition - fvObjectPosition.xyz;
   vec3 ViewDirectionn=normalize(ViewDirection);
   LightDirection = fvLightPosition - fvObjectPosition.xyz;
   Normal         = gl_NormalMatrix * gl_Normal;
   vec3 normal2=vec4(Normal,1)*matWorldInverseTranspose;
   
   vec3 fin=ViewDirectionn-(2*(dot(normalize(normal2),ViewDirectionn))*normal2);
   float p=sqrt(pow(fin.x,2)+pow(fin.y,2)+pow((fin.z+1),2));
   
   
   reflcoord=vec2(((fin.x/(2*p))+1/2),((fin.y/(2*p))+1/2));
}
Fragment shader

Code: Select all

uniform vec4 fvLowTone;
uniform vec4 fvSpecular;
uniform vec4 fvHighTone;
uniform float fSpecularPower;

uniform sampler2D baseMap;

uniform sampler2D cube;
varying vec2 Texcoord;
varying vec3 ViewDirection;
varying vec3 LightDirection;
varying vec3 Normal;
varying vec2 reflcoord;
void main( void )
{
   vec3  fvLightDirection = normalize( LightDirection );
   vec3  fvNormal         = normalize( Normal );
   float fNDotL           = dot( fvNormal, fvLightDirection ); 
   
   vec3  fvReflection     = normalize( ( ( 2.0 * fvNormal ) * fNDotL ) - fvLightDirection ); 
   vec3  fvViewDirection  = normalize( ViewDirection );
   float fRDotV           = max( 0.0, dot( fvReflection, fvViewDirection ) );
   
   vec4  fvBaseColor      = texture2D( baseMap, Texcoord );
   
   vec4  fvTotalAmbient   = fvLowTone * fvBaseColor; 
   vec4  fvTotalDiffuse   = fvHighTone * (fNDotL) * fvBaseColor; 
   vec4  fvTotalSpecular  = fvSpecular * ( pow( fRDotV, fSpecularPower ) );
   float fresnel =(1/dot( Normal, fvViewDirection) )/5;
   gl_FragColor = ( fvTotalAmbient + fvTotalDiffuse + fvTotalSpecular )+(fresnel*texture2D(cube,reflcoord))+(fresnel /2);
       
}
I havent looked at the output asm yet so im trusting the compiler to optimise this.

Image
"Irrlicht is obese"

If you want modern rendering techniques learn how to make them or go to the engine next door =p
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

I gaped when I saw this, too bad my card doesnt have fragment for opengl :(

I will try to convert to HLSL :)

PS: A nice shader collection like the guy in the thread below this mentioned is a great idea.
omaremad
Competition winner
Posts: 1027
Joined: Fri Jul 15, 2005 11:30 pm
Location: Cairo,Egypt

Post by omaremad »

I got it running in irrlicht but the fresnel factor is realllly saturated so its more like a toon car shader in irrlicht. :(

Ill investigate.

Yeah i agree more shaders = prettier irrlicht.
"Irrlicht is obese"

If you want modern rendering techniques learn how to make them or go to the engine next door =p
noreg
Posts: 158
Joined: Fri Jun 23, 2006 6:01 pm
Location: continental europe

Post by noreg »

Thanks omaremad. Nice one.

I am totally fascinated by this --> shader. The dem is interactive and it can be tweaked to generate PERFECT ice , water, AND diamonds (with refraction!). It's really good. Don't forget to tweak a lot and press D key to see the effects. OPENSOURCE.

http://www.beyond3d.com/articles/shader ... ucency.zip
abraham
Posts: 30
Joined: Wed Jul 19, 2006 8:56 am

Post by abraham »

wooow looks nice
n7600gt 256mb ram 128bit
AMD 3200+
GA-K8NF-9 gigabyte
belfegor
Posts: 383
Joined: Mon Sep 18, 2006 7:22 pm
Location: Serbia

Post by belfegor »

I liked what i see on those pictures.Nice effect.
I want to use only DX.
I havent try to convert it to HLSL but if i replace
vec2 & vec3 with float2 & float3 and remove "varying"
is it going to work?Or it needs more info for HLSL?

Also i havent seen functon names to be different then in HLSL.

edit: and what is HLSL compile targets vs1.xxx ps 1.xxx or 2, 2b...3...
if it going to work?
Small FPS demo made using Irrlicht&NewtonDEMO
InfoHERE
Its at very early stage but i think im crazy enough to finish it all alone.
omaremad
Competition winner
Posts: 1027
Joined: Fri Jul 15, 2005 11:30 pm
Location: Cairo,Egypt

Post by omaremad »

varyings have to be trsanported as texcoords.

vec4 multiplied by matricies need mult(vec4*matrix)
as for compile targets it should be below 2.

as for the refreaction stuff, its nice and physically correct, but reversed cumbmaps with offsets per fragment are allot faster. The guys shader is excellent (for real physics) but it is no way suited to games especially with the per frame regeneration of normalmaps.

Even top ray tracers fake effects ( the documents on pixars website show how much they fake even if they have the most powerful computers)
"Irrlicht is obese"

If you want modern rendering techniques learn how to make them or go to the engine next door =p
Post Reply