Entry 5157

Phong Beleuchungsmodell in Cg

   

Submitted by anonymous on July 22, 2010 at 6:50 p.m.
Language: C. Code size: 1.7 KB.

struct appIn  
{ float4 VSPosition : POSITION;	// Eckpunktkoordinaten, Objekt-Koordinatensystem
  float4 VSNormal : NORMAL;	// Eckennormale, Objekt-Koordinatensystem
};
struct vertOut  
{ float4 Position : POSITION;	// Eckpunktkoordinaten, homogene Clippingkoordinaten
  float4 Color : COLOR0;	// Eckpunkt-Farbe
};

vertOut main(appIn IN,  uniform float4x4 VSModelView, uniform float4x4 VSModelViewIT, uniform float4x4 VSModelViewProj, uniform float4 VSLightPos,
             uniform float4 VSLightColor)
{ vertOut OUT;
  // Eckpunkt von Objekt- in (nicht homogene) Kamerakoordinaten
  	float4 ecP = mul(VSModelView, IN.VSPosition);
  // Normale von Objekt- in (nicht homogene) Kamerakoordinaten
 	 float3 ecN = normalize(mul(VSModelViewIT, IN.VSNormal).xyz);
  // Lichtvektor L vom Eckpunkt zur Lichtquelle (in Kamerakoordinaten)
	  float3 ecL = normalize(VSLightPos.xyz-ecP);
  // Viewingvektor V in Kamerakoordinaten
  // Augpunkt ist in Kamerakoordinaten im Ursprung!
 	 float3 ecV = normalize(-ecP);
  // Halfway-Vektor (in Kamerakoordinaten)
 	 float3 ecH = normalize(ecL+ecV);
  // diffusen Lichtbeitrag berechnen
	  float diffuseWeight = max(0.0, dot(ecL, ecN));
  // spiegelnden Lichtbeitrag berechnen
	  float specularWeight = (diffuseWeight > 0.0?pow(max(0.0,dot(ecH, ecN)),32):0.0);
   // diffuser (rot) und spiegelnder (weiß) Reflexionskoeffizient
	  float3 diffuseCoeff = float3(1.0, 0.0, 0.0);
 	 float3 specularCoeff = float3(1.0, 1.0, 1.0);	
  // Lichtanteile zusammenfassen
 	 OUT.Color.rgb = diffuseCoeff * diffuseWeight + VSLightColor.xyz * specularCoeff * specularWeight;
	  OUT.Color.a = 1.0;
  // Eckpunkt von Objekt- in (homogene) Clippingkoordinaten
	  OUT.Position = mul(VSModelViewProj, IN.VSPosition);

 	 return OUT;
}

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).