shader diffuse bumpmapping(Cg): Unterschied zwischen den Versionen

Aus DGL Wiki
Wechseln zu: Navigation, Suche
(Die Seite wurde neu angelegt: =Shadername= Zurück zur Shadersammlung {|{{Prettytable_B1}} width=100% !width=60%|Beschreibung !width=20%|Autor !width=20%|Version |- |Wendet diffuses Bumpmapping ...)
 
Zeile 21: Zeile 21:
  
 
==Beschreibung==
 
==Beschreibung==
''kommt noch...''
+
Um die benötigte Vertexzahl im 3D-Modellen zu minimieren, werden oftmals so genannte "Normalmaps" verwendet, die die Normalen der entfernten Vertices beinhalten. Somit kann der Eindruck eines höheren Detailgrades erreicht werden.<br>Die Figur im Screenshot zum Beispiel ist nur aus 4 Vertices (also ein Quad) zusammengesetzt.<br>
 +
Bumpmapping verändert nicht die Position der Vertices, sondern nur die Helligkeit der Pixel der Textur in Abhängigkeit zur Position der Lichtquelle. So würde in den Bildern oben der Effekt auffallen, sobald man das Quad um die X- oder Y-Achse drehen würde.
  
 
==Besondere Vorraussetzungen==
 
==Besondere Vorraussetzungen==
''kommt noch...''
+
Keine bekannt. Getestet mit Cg 1.5.
  
 
==Code==
 
==Code==
 
<cpp>
 
<cpp>
 +
//Result structure for the fragment shader
 
struct fs_res {
 
struct fs_res {
 
   float4 color : COLOR;
 
   float4 color : COLOR;
Zeile 59: Zeile 61:
 
}
 
}
  
 +
//Result structure for the vertex shader
 
struct vs_res {
 
struct vs_res {
 
   float4 position : POSITION;
 
   float4 position : POSITION;
Zeile 71: Zeile 74:
 
   float4 color : COLOR,
 
   float4 color : COLOR,
 
   float4 colortexcoords : TEXCOORD0,
 
   float4 colortexcoords : TEXCOORD0,
   uniform float4x4 modelview,
+
   uniform float4x4 modelview, //The model-view-projection matrix
   uniform float3 lightpos,
+
   uniform float3 lightpos, //Position of the light
   uniform float4 lightcolor
+
   uniform float4 lightcolor //Color of the light
 
)
 
)
 
{
 
{

Version vom 31. Oktober 2008, 16:12 Uhr

Shadername

Zurück zur Shadersammlung

Beschreibung Autor Version
Wendet diffuses Bumpmapping auf eine beliebige Oberfläche an. Igel457 1.0

Bilder

Die Originaltextur...
...und die Normalmap...
ergeben zusammen mit einer Lichtposition ein plastisches Bild.

Beschreibung

Um die benötigte Vertexzahl im 3D-Modellen zu minimieren, werden oftmals so genannte "Normalmaps" verwendet, die die Normalen der entfernten Vertices beinhalten. Somit kann der Eindruck eines höheren Detailgrades erreicht werden.
Die Figur im Screenshot zum Beispiel ist nur aus 4 Vertices (also ein Quad) zusammengesetzt.
Bumpmapping verändert nicht die Position der Vertices, sondern nur die Helligkeit der Pixel der Textur in Abhängigkeit zur Position der Lichtquelle. So würde in den Bildern oben der Effekt auffallen, sobald man das Quad um die X- oder Y-Achse drehen würde.

Besondere Vorraussetzungen

Keine bekannt. Getestet mit Cg 1.5.

Code

//Result structure for the fragment shader
struct fs_res {
  float4 color : COLOR;
};

float3 expand(float3 v)
{
  return (v - 0.5) * 2; // Expand a range-compressed vector
}

fs_res fragment_shader(
  float2 colortexcoords: TEXCOORD0,
  float3 lightdirection: TEXCOORD1,
  float4 color: COLOR,
  uniform sampler2D colormap,
  uniform sampler2D normalmap,
  uniform float  lightintensity,
  uniform float4 ambientcolor  
)
{
  fs_res OUT;
  
  float3 light = expand(lightdirection);
  
  float3 normaltex = tex2D(normalmap, colortexcoords).xyz;
  float3 normal = expand(normaltex);      
  
  OUT.color = (dot(-normal, light) * lightintensity * color + ambientcolor) * tex2D(colormap, colortexcoords);  
  
  return OUT;
}

//Result structure for the vertex shader
struct vs_res {
  float4 position : POSITION;
  float4 color    : COLOR;
  float2 colortexcoords: TEXCOORD0;
  float3 lightdirection: TEXCOORD1;
};

vs_res vertex_shader(
  float3 position : POSITION,
  float3 normal : NORMAL,
  float4 color : COLOR,
  float4 colortexcoords : TEXCOORD0,
  uniform float4x4 modelview, //The model-view-projection matrix
  uniform float3 lightpos, //Position of the light
  uniform float4 lightcolor //Color of the light
)
{
  vs_res OUT; 
  
  //Transform the vertex position to the world position
  OUT.position = mul(modelview, float4(position, 1));
  
  //Comment this line if you don't want the shader to transform the light position
  //to the world position
  float4 lp = mul(modelview, float4(lightpos, 1));  
 
  //Set the color texture coordinates
  OUT.colortexcoords = colortexcoords;
  //Set the color of the vertex
  OUT.color = color * lightcolor;
  //Stor the light direction in a second texture coordinate vector
  OUT.lightdirection = normalize(position.xyz - lightpos);
  
  return OUT;
}