More DirectX teapots

Coco | Heroic Unstoppable!
 
more |
XBL: Monsieur Cocco
PSN:
Steam: Mr Coco
ID: Cocos
IP: Logged

2,588 posts
 
Don't expect anyone to care, but I don't care because I spent the last 10 hours or so figuring out how to blend texture mapping, normal mapping, and environment-map (skybox) reflections in one HLSL Phong shader, plus getting it all to work in DirectX, and it all finally works.

The texture and normal are usually used for floors, but oh well.

Texture mapping + cube-mapped reflections



Normal mapping (a little overkill)



Compared to last time, where I had

So the gibberish for the shader looks something like:
Code: [Select]
struct Mtrl
{
float4 ambient;
float4 diffuse;
float4 spec;
float  specPower;
};

struct DirLight
{
float4 ambient;
float4 diffuse;
float4 spec;
float3 dirW; 
};

uniform extern float4x4 gWorld;
uniform extern float4x4 gWorldInv;
uniform extern float4x4 gWorldInvTrans;
uniform extern float4x4 gWVP;
uniform extern Mtrl     gMtrl;
uniform extern DirLight gLight;
uniform extern float3   gEyePosW;
uniform extern texture  gTex;
uniform extern texture  gEnvMap;
uniform extern texture  gNormalMap;

uniform extern float gReflectivity;
uniform extern float gNormalPower;
uniform extern float gTexBlend;


sampler TexS = sampler_state
{
Texture = <gTex>;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU  = WRAP;
    AddressV  = WRAP;
};

sampler NormalMapS = sampler_state
{
Texture = <gNormalMap>;
MinFilter = ANISOTROPIC;
MaxAnisotropy = 8;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU  = WRAP;
    AddressV  = WRAP;
};

sampler EnvMapS = sampler_state
{
Texture = <gEnvMap>;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU  = WRAP;
    AddressV  = WRAP;
};

struct OutputVS
{
float4 posH      : POSITION0;
    float3 toEyeT    : TEXCOORD0;
    float3 lightDirT : TEXCOORD1;
    float2 tex0      : TEXCOORD2;
float3 normalW : TEXCOORD3;
float3 toEyeW : TEXCOORD4;
};

OutputVS PhongEnvNMVS( float3 posL : POSITION0,
float3 normalL : NORMAL0,
float3 tangentL : TANGENT0,
float3 binormalL : BINORMAL0,
float2 tex0 : TEXCOORD0)
{
    // Zero out the output.
OutputVS outVS = (OutputVS)0;

////////////////////////// TANGENT SPACE CALCULATIONS:
// Build Tangent Binormal-basis.
float3x3 TBN;
TBN[0] = tangentL;
TBN[1] = binormalL;
TBN[2] = normalL;

// Transform matrix from object space to tangent space
float3x3 toTangentSpace = transpose(TBN);
// Transform eye position to local space.
float3 eyePosL = mul(float4(gEyePosW, 1.0f), gWorldInv);
// Transform to-eye vector to tangent space.
float3 toEyeL = eyePosL - posL;
// Transform light direction to tangent space.
outVS.toEyeT = mul(toEyeL, toTangentSpace);
///////////////////////////////////////////////////////

float3 lightDirL = mul(float4(gLight.dirW, 0.0f), gWorldInv).xyz;
outVS.lightDirT  = mul(lightDirL, toTangentSpace);

// Transform to homogeneous clip space.
outVS.posH = mul(float4(posL, 1.0f), gWVP);

// Pass texture coordinates to be interpolated during rasterization.
outVS.tex0 = tex0;
outVS.normalW = mul(float4(normalL, 0.0f), gWorldInvTrans).xyz;
float3 posW  = mul(float4(posL, 1.0f), gWorld).xyz;
outVS.toEyeW = gEyePosW - posW;

    return outVS;
}

float4 PhongEnvNMPS( float3 toEyeT    : TEXCOORD0,
float3 lightDirT : TEXCOORD1,
float2 tex0      : TEXCOORD2,
float3 normalW : TEXCOORD3,
float3 toEyeW : TEXCOORD4) : COLOR
{
// Normalize, because interpolated normals can warp
toEyeT    = normalize(toEyeT);
lightDirT = normalize(lightDirT);
normalW = normalize(normalW);
toEyeW  = normalize(toEyeW);

// Light vector is opposite the direction of the light.
float3 lightVecT = -lightDirT;
// Sample normal map.
float3 normalT = tex2D(NormalMapS, tex0);
// Expand from [0, 1] compressed interval to true [-1, 1] interval.
    normalT = (2.0f*normalT - 1.0f) * gNormalPower;
// Make it a unit vector.
normalT = normalize(normalT);

//Compute the normal reflection vector
float rN = reflect(-lightVecT, normalT);
// Determine the diffuse light intensity that strikes the vertex.
float tN  = pow(max(dot(rN, toEyeT), 0.0f), gMtrl.specPower);
float sN = max(dot(lightVecT, normalT), 0.0f);

// If the diffuse light intensity is low, kill the specular lighting term.
// It doesn't look right to add specular light when the surface receives
// little diffuse light.
if(sN <= 0.0f)
     tN = 0.0f;

// Sample the environment map and get the reflected color + add the normal component to it
float3 envMapTex = reflect(-toEyeW, normalW) + (normalT*gNormalPower);
float3 reflectedColor = texCUBE(EnvMapS, envMapTex);

// Light vector is opposite the direction of the light.
float3 lightVecW = -gLight.dirW;
// Compute the reflection vector.
float3 r = reflect(-lightVecW, normalW);
// Determine how much (if any) specular light makes it into the eye.
float t = pow(max(dot(r, toEyeW), 0.0f), gMtrl.specPower);
float s = max(dot(lightVecW, normalW), 0.0f);


// Get the texture color, and multiply it by how blended it should be.
float4 texColor = tex2D(TexS, tex0) * gTexBlend;

// Weighted average between the reflected color, and usual
// diffuse/ambient material color modulated with the texture color.
float3 ambientMtrl = gReflectivity*reflectedColor + (1.0f-gReflectivity)*(gMtrl.ambient*texColor);
float3 diffuseMtrl = gReflectivity*reflectedColor + (1.0f-gReflectivity)*(gMtrl.diffuse*texColor);

// Compute the ambient, diffuse and specular terms separately.
float3 spec = tN*(gMtrl.spec*gLight.spec).rgb;
float3 diffuse = sN*(diffuseMtrl*gLight.diffuse.rgb);
float3 ambient = ambientMtrl*gLight.ambient;

// Compute the final color: ambient + diffuse + specular:
float3 final = (ambient + diffuse)*texColor.rgb + spec;

// Output the color and the alpha.
    return float4(final, gMtrl.diffuse.a*texColor.a);
}

technique PhongEnvNMTech
{
    pass P0
    {
        // Specify the vertex and pixel shader associated with this pass.
        vertexShader = compile vs_2_0 PhongEnvNMVS();
        pixelShader  = compile ps_2_0 PhongEnvNMPS();
    }
}


Rinev | Legendary Invincible!
 
more |
XBL: Rinev Jeqkogo
PSN:
Steam: rinevjekogo
ID: Rinev Jeqkogo
IP: Logged

3,715 posts
Feet first into fun!
That looks pretty.


Coco | Heroic Unstoppable!
 
more |
XBL: Monsieur Cocco
PSN:
Steam: Mr Coco
ID: Cocos
IP: Logged

2,588 posts
 


Epsira | Legendary Invincible!
 
more |
XBL:
PSN:
Steam: interiminitiator
ID: Epsira
IP: Logged

4,041 posts
 
This looks really neat, though I don't really know what it's about.


Coco | Heroic Unstoppable!
 
more |
XBL: Monsieur Cocco
PSN:
Steam: Mr Coco
ID: Cocos
IP: Logged

2,588 posts
 
This looks really neat, though I don't really know what it's about.

It's DirectX programming.  Essentially, I've been programming the stuff that lets you see "3D" objects on your screen, how your computer handles being in a 3D world (using linear algebra to figure out where something is positioned locally, globally, through your view, etc...). 

Since I accomplished that a few weeks ago, the code I posted is for a shader, which is basically how a graphics card colors a 3D object: before, I had it do the computations relating to handling how light and color affect the look of the model; what I posted in the OP is how the computer makes something shiny, and that warpy effect that you get from looking at something shiny from another angle.

I also did normal mapping and texture mapping, which means I wrapped this around the model (kinda like applying a coat of paint in a very specific way):


And then used this normal map:

To make the model give off the illusion of added depth/bumpiness.  The shader goes through the normal map, and the color at each pixel of the map tells the shader how light should react at that spot on the model
Last Edit: April 05, 2015, 10:18:13 PM by Cocos


Epsira | Legendary Invincible!
 
more |
XBL:
PSN:
Steam: interiminitiator
ID: Epsira
IP: Logged

4,041 posts
 
This looks really neat, though I don't really know what it's about.

It's DirectX programming.  Essentially, I've been programming the stuff that lets you see "3D" objects on your screen, how your computer handles being in a 3D world (using linear algebra to figure out where something is positioned locally, globally, through your view, etc...). 

Since I accomplished that a few weeks ago, the code I posted is for a shader, which is basically how a graphics card colors a 3D object: before, I had it do the computations relating to handling how light and color affect the look of the model; what I posted in the OP is how the computer makes something shiny, and that warpy effect that you get from looking at something shiny from another angle.

I also did normal mapping and texture mapping, which means I wrapped this around the model (kinda like applying a coat of paint in a very specific way):


And then used this normal map:

To make the model give off the illusion of added depth/bumpiness.  The shader goes through the normal map, and the color at each pixel of the map tells the shader how light should react at that spot on the model
So you're emulating reality in a sense?
Beyond cool.


 
cxfhvxgkcf-56:7
| Marty Inconceivable!
 
more |
XBL:
PSN:
Steam:
ID: SoporificSlash
IP: Logged

15,844 posts
 
This user has been blacklisted from posting on the forums. Until the blacklist is lifted, all posts made by this user have been hidden and require a Sep7agon® SecondClass Premium Membership to view.


Girl of Mystery | Mythic Unfrigginbelievable!
 
more |
XBL:
PSN:
Steam:
ID: TheBritishLemon
IP: Logged

23,485 posts
A flower which blooms on the battlefield
That's a lot of frames


Batch | Mythic Inconceivable!
 
more |
XBL: Sips
PSN: Fucking
Steam: Tea
ID: Batch
IP: Logged

8,175 posts
 
OH MY GOD

*sips tea*