• Advertisement

# Applying light effects and reflection

This topic is 1771 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

Hi all,

I'm trying to enable light on my 3D scene and make my primitives a little specular. A little means that they have their color and it's must be visible but they must to reflect light on the normal direction.

My problem is that rendered model is black. If I disable shader function it seems to be OK, but light does not work as well

Light light = new Light()
{
Type = LightType.Directional,
Diffuse = SharpDX.Color.White,
Direction = Vector3.Normalize(new Vector3(0, 0, 1)),
};
d3dDevice.SetLight(0, ref light);
d3dDevice.EnableLight(0, true);


Texture CubeMap;

samplerCUBE enviroMap =
sampler_state
{
Texture = <CubeMap>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};

//VertexShader function with reflection calculations
VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
VertexShaderOutput output = (VertexShaderOutput)0;

output.Position = mul(input.Position, worldViewProj);

//
float3 Normal = float3(0,1,0);

// Compute normal in camera space
float4 NormalVec = normalize( mul( worldViewProj, Normal ) );

// Obtain the reverse eye vector
float4 EyeVec = normalize( mul( output.Position, worldViewProj ) );

// Compute the reflection vector
float4 Reflect = reflect( -EyeVec, NormalVec );
Reflect = refract( EyeVec, NormalVec, 0.8f );

// Store the reflection vector in texcoord0
output.reflectionCoord = Reflect.xyz;

return output;
}

//PixelShader function
float4 PixelShaderFunction(VertexShaderOutput input, float face : VFACE) : COLOR
{
float reflectivity = 0.5f;
float4 color = reflectivity * texCUBE( enviroMap, input.reflectionCoord );
return color;
}


#### Share this post

##### Share on other sites
Advertisement

If you are using shaders, then you can't use the light API in D3D9 - that is for fixed function processing only.  Instead, you have to pass the information in directly as constants to the shader program.

#### Share this post

##### Share on other sites

If you are using shaders, then you can't use the light API in D3D9 - that is for fixed function processing only.  Instead, you have to pass the information in directly as constants to the shader program.

You are right, I've fixed a light, but my problem is still unsolved. To describe it better, I'm attaching some screenshots:

I have a model, which is empty inside  [attachment=14249:Capture.PNG] [attachment=14250:Capture2.PNG]

And when I disable lines, I get unclear what : [attachment=14251:Capture3.PNG] [attachment=14252:Capture4.PNG]

I want to make a light reflection to fix this problem, but the hlsl reflection code, which I posted above, is not what I need.

#### Share this post

##### Share on other sites

If you want specular light reflections use a light and Blinn-Phong.

Why are you trying to use reflection maps if you want lighting?

#### Share this post

##### Share on other sites

The problem is that I don't have Normal vector on each vertice, so I'm using one default (0,1,0) for all vertices, and this is not good! I don't know where to find or how to calculate normal on each vertice, so I tried to make  reflection map, but with no desired result...

#### Share this post

##### Share on other sites

I don't know where to find or how to calculate normal on each vertice, so I tried to make  reflection map, but with no desired result...

There is a lot of information conerning this in a web. I.E. here http://www.riemers.net:

            for (int i = 0; i < indices.Length / 3; i++)
{
Vector3 firstvec = vertices[indices[i*3+1]].Position-vertices[indices[i*3]].Position;
Vector3 secondvec = vertices[indices[i*3]].Position-vertices[indices[i*3+2]].Position;
Vector3 normal = Vector3.Cross(firstvec, secondvec);
normal.Normalize();
vertices[indices[i * 3]].Normal += normal;
vertices[indices[i * 3 + 1]].Normal += normal;
vertices[indices[i * 3 + 2]].Normal += normal;
}


#### Share this post

##### Share on other sites

Thanks user88! I calculated normals and all seems to be ok.

I've implemented Blinn-Phong algorithm, it is what I need! But I have a question: I have vectors EyePosition and LooAtVector and I want source light always be at the camera position. Otherwords, the side of the figure, on which I'm looking, must be lighted. Look at my source code and tell me what I'm doing wrong:

//Vertex shader
......
// Will be moved into PixelShader for deferred rendering
float3 lightDirection = LookAtPosition;
output.L = -lightDirection;	            // Light Direction
output.N = mul(input.Normal, World);    // Surface Normal [World Coordinates]
.......

float4 PixelShaderFunction(VertexShaderOutput input, float face : VFACE) : COLOR
{
//my color
float4 color = colors[round(input.texCoord * 11)];

// [Ambient Light] I = Ai * Ac
float Ambient_Intensity = 0.7f;
float4 Ambient_Colour = color;
float4 Ambient_Light = Ambient_Intensity * Ambient_Colour;

// [Diffuse Light] I = Di * Dc * N.L
float Diffuse_Intensity = 0.7f;
float4 Diffuse_Colour = color;
float NdotL = dot(normalize(input.N), normalize(input.L));
float4 Diffuse_Light = Diffuse_Intensity * color * saturate(NdotL);

//----------Blinn-Phong

// Compute the half vector Eye + Light
float3 half_vector = normalize(EyePosition + EyePosition); //in my case eye+eye, couse I want light to be the same position as light

// Compute the angle between the half vector and normal
float  HdotN = max( 0.0f, dot( half_vector, input.N ) );

// Compute the specular colour
float4 cSpecular = float4(1.0f,1.0f,1.0f,1.0f); //white
float fSpecularExponent = 5;
float3 specular = cSpecular * pow( HdotN, fSpecularExponent );

// Determine the final colour
return float4( Diffuse_Light + specular + Ambient_Light, 1.0f );
}


#### Share this post

##### Share on other sites

• Advertisement
• Advertisement
• ### Popular Tags

• Advertisement
• ### Popular Now

• 11
• 9
• 11
• 9
• 12
• Advertisement