Jump to content

  • Log In with Google      Sign In   
  • Create Account


Applying light effects and reflection


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Yura   Members   -  Reputation: 263

Like
0Likes
Like

Posted 11 March 2013 - 04:32 AM

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. smile.png

 

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;
}

 

 



Sponsor:

#2 Jason Z   Crossbones+   -  Reputation: 4720

Like
2Likes
Like

Posted 11 March 2013 - 05:20 AM

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.



#3 Yura   Members   -  Reputation: 263

Like
0Likes
Like

Posted 13 March 2013 - 02:35 AM

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  Capture.PNG  Capture2.PNG

 

And when I disable lines, I get unclear what : Capture3.PNG  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.



#4 TiagoCosta   Crossbones+   -  Reputation: 1880

Like
1Likes
Like

Posted 13 March 2013 - 05:41 AM

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

 

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


Tiago Costa
Aqua Engine - my DirectX 11 game "engine" - In development

#5 Yura   Members   -  Reputation: 263

Like
0Likes
Like

Posted 14 March 2013 - 07:38 AM

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...



#6 user88   Members   -  Reputation: 268

Like
1Likes
Like

Posted 14 March 2013 - 07:55 AM

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;
             }


#7 Yura   Members   -  Reputation: 263

Like
0Likes
Like

Posted 18 March 2013 - 04:59 AM

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 );
}





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS