🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

I got a Problem in my GLSL Shader

Started by
5 comments, last by ddlox 3 years, 8 months ago

first, sorry for my bad english guys.

I made a glsl source for Phong model, but it seems not work well…

light just shines to left bottom side, and when i change color of light, all objects except same color with light just become black.

i upload sources, so plz help me find what is wrong.

Fragment Shader

#version 330 core

in vec3 FragPos;
in vec3 Normal;
in vec3 ObjectColor;

out vec4 FragColor;

uniform vec3 lightPos;
uniform vec3 viewPos;
uniform vec3 lightColor;

uniform float ambientLight;

void main()
{
	vec3 ambient;
	ambient = ambientLight * lightColor;

	vec3 normalVector = normalize(Normal);

	vec3 lightDir;
	lightDir = normalize(lightPos - FragPos);

	float diffuseLight;
	diffuseLight = max(dot(normalVector, lightDir), 0.0);

	vec3 diffuse;
	diffuse = diffuseLight * lightColor;

	int shininess = 128;

	vec3 viewDir;
	viewDir = normalize(viewPos - FragPos);

	vec3 reflectDir;
	reflectDir = reflect(-lightDir, normalVector);

	float specularLight;
	specularLight = max(dot(viewDir, reflectDir), 0.0);
	specularLight = pow(specularLight, shininess);

	vec3 specular;
	specular = specularLight * lightColor;

	vec3 result;
	result = (ambient + diffuse + specular) * ObjectColor;
	FragColor = vec4 (result, 1.0);
}

VertexShader

#version 330 core

layout (location = 0) in vec3 vPos;
layout (location = 1) in vec3 vColor;
layout (location = 2) in vec3 vNormal;

out vec3 FragPos;
out vec3 Normal;
out vec3 ObjectColor;

uniform mat4 modelTransform;
uniform mat4 viewTransform;
uniform mat4 projectionTransform;

void main()
{
	gl_Position = projectionTransform * viewTransform * modelTransform * vec4(vPos, 1.0);
	FragPos = vec3(modelTransform * vec4(vPos, 1.0));
	Normal = vNormal;
	ObjectColor = vColor;
}
Advertisement

which tutorial are you following ?

@undefined i'm studying in university in korea. tutorial is made of korean so you may cant read

ok try these:

// in FS shader

...
int shininess = 32; // 128;
...
float spec_strength = 0.5; // add this 
...
specular = spec_strength * specularLight * lightColor;




// in VS shader

FragPos = vec3(modelTransform * vec4(vPos, 1.0));
Normal = mat3(transpose(inverse(modelTransform))) * vNormal; // u must transform normals as well

gl_Position = projectionTransform * viewTransform * vec4(FragPos, 1.0);
ObjectColor = vColor;

I hope this helps ?

EDIT: if it still doesn't work, then try removing transpose( ) AND/OR inverse( )

omg thx but i found i didnt put normals into shader. i just fixed it and it works nice. thx for your help!

wonderful ?

This topic is closed to new replies.

Advertisement