Fragment Shader problem

I have Fragment Shader configure like this

#version 330
uniform vec3 LightColor;
uniform vec3 LightDirection;
uniform float LightAmbient;
in vec4 color;
in vec3 vNormal;
in vec2 texCoord;
out vec4 outputColor;
uniform sampler2D gSampler;
void main()
float LightDiffuse = max(0.0, dot(normalize(vNormal), -LightDirection));
outputColor = texture2D(gSampler, texCoord);
//outputColor = color*vec4(LightColor*(LightAmbient+LightDiffuse),1.0);

It does work but i can only choose one “outputColor” (texture, or light effect)
Can someone help me modify it so i can use light effect combined with texture. Im green at this so i would be a greatful for a streight answer.

I did find solution Thred to erase


Look at how you are combining LightAmbient, LightDiffuse, and LightColor. Think of the texture as a light. To combine you either add or multiply. Your colors are RGB and so white is (1,1,1) and black is (0,0,0). Red is (1,0,0). Each value is a percentage of how much of that color to use. If you think about adding them, it is basically going to combine them as if using both and anything over 1 is still just 1 (it doesn’t get any whiter than white). This often causes burn-out when you add colors because it doesn’t take a whole lot to add up to one and everything turns white.

When you multiply colors you tend to get the lowest common denominator. In the previous case, white times black is black. If you had a gray of (0.3,0.3,0.3) and multiply that times what you would get that shade of gray still. Multiply the same gray times black and you will get black. So, it basically combines the colors in a way that favors the darkest elements in the two colors. So, you’ll never get burn-out that way. But it’s really two different effects.

More specifically, I can show you my fragment shader as an example. It uses “InputColor” to allow for the texture color to be used or to ignore the texture and just use the vertex color.

#version 450 core

in vec2 TextureCoordinates;
in vec3 VertexNormal;
in vec4 RGBAColor;
in float FogFactor;
in vec4 PositionRelativeToCamera;
in vec3 WorldSpacePosition;

layout (location = 0) out vec4 OutputColor;

uniform vec4 AmbientLightColor;
uniform vec3 DiffuseLightDirection;
uniform vec4 DiffuseLightColor;
uniform vec3 CameraPosition;
uniform float SpecularPower;
uniform vec4 FogColor;
uniform float FogStartDistance;
uniform float FogMaxDistance;
uniform bool UseTexture;
uniform sampler2D Texture0;

vec4 BlinnSpecular(in vec3 LightDirection, in vec4 LightColor, in vec3 PixelNormal, in vec3 CameraDirection, in float SpecularPower)
	vec3 HalfwayNormal;
	vec4 SpecularLight;
	float SpecularHighlightAmount;

	HalfwayNormal = normalize(LightDirection + CameraDirection);
	SpecularHighlightAmount = pow(clamp(dot(PixelNormal, HalfwayNormal), 0.0, 1.0), SpecularPower);
	SpecularLight = SpecularHighlightAmount * LightColor; 

	return SpecularLight;

vec4 PhongSpecular(in vec3 LightDirection, in vec4 LightColor, in vec3 PixelNormal, in vec3 CameraDirection, in float SpecularPower)
	vec3 ReflectedLightDirection;	
	vec4 SpecularLight;
	float SpecularHighlightAmount;

	ReflectedLightDirection = 2.0 * PixelNormal * clamp(dot(PixelNormal, LightDirection), 0.0, 1.0) - LightDirection;
	SpecularHighlightAmount = pow(clamp(dot(ReflectedLightDirection, CameraDirection), 0.0, 1.0), SpecularPower);
	SpecularLight = SpecularHighlightAmount * LightColor; 

	return SpecularLight;

void main()
	vec3 LightDirection;
	float DiffuseLightPercentage;
	vec4 SpecularColor;
	vec3 CameraDirection;	//Float3 because the w component really doesn't belong in a 3D vector normal.
	vec4 AmbientLight;
	vec4 DiffuseLight;
	vec4 InputColor;

	if (UseTexture) 
		InputColor = texture(Texture0, TextureCoordinates);
		InputColor = RGBAColor; // vec4(0.0, 0.0, 0.0, 1.0);

	LightDirection = -normalize(DiffuseLightDirection);	//Normal must face into the light, rather than WITH the light to be lit up.
	DiffuseLightPercentage = max(dot(VertexNormal, LightDirection), 0.0);	//Percentage is based on angle between the direction of light and the vertex's normal. 
	DiffuseLight = clamp((DiffuseLightColor * InputColor) * DiffuseLightPercentage, 0.0, 1.0);	//Apply only the percentage of the diffuse color. Saturate clamps output between 0.0 and 1.0.

	CameraDirection = normalize(CameraPosition - WorldSpacePosition);	//Create a normal that points in the direction from the pixel to the camera.

	if (DiffuseLightPercentage == 0.0f) 
		SpecularColor  = vec4(0.0f, 0.0f, 0.0f, 1.0f);
		//SpecularColor = BlinnSpecular(LightDirection, DiffuseLightColor, normalize(VertexNormal), CameraDirection, SpecularPower);
		SpecularColor = PhongSpecular(LightDirection, DiffuseLightColor, normalize(VertexNormal), CameraDirection, SpecularPower);

	float FogDensity = 0.01f;
	float LOG2 = 1.442695f;
	float FogFactor = exp2(-FogDensity * FogDensity * PositionRelativeToCamera.z * PositionRelativeToCamera.z * LOG2);
	FogFactor = 1 - FogFactor;
	//float FogFactor = clamp((FogMaxDistance - PositionRelativeToCamera.z)/(FogMaxDistance - FogStartDistance), 0.0, 1.0);
	OutputColor = RGBAColor * (AmbientLightColor * InputColor) + DiffuseLight + SpecularColor;
	OutputColor = mix (OutputColor, FogColor, FogFactor);
	//OutputColor = vec4(0.0f, 0.5f, 0.0f, 1.0f);

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.