2
有人可以幫我審查我的glsl着色器程序。我遇到的問題是,儘管光的位置,我的網格的邊緣是不應該是黑色的。GLSL着色器照明不工作
爲了說明問題,我提供這個畫面(注意:紅,綠,藍與白點是光線的位置):
VertexShader:
#version 330
struct Matrix {
mat4 mvp;
mat4 mv;
mat4 view;
mat4 projection;
};
struct Light {
vec3 position; //Supplied in eye space(camera view)
vec3 color;
vec3 direction;
float intensity;
vec3 ambient;
};
attribute vec3 inputPosition;
attribute vec3 inputNormal;
attribute vec2 inputTexture;
attribute ivec2 number_influence;
attribute ivec4 boneIDs;
attribute vec4 boneWeights;
//--------------------------------------------
// UNIFORM:INPUT Supplied Data from C++ application
//--------------------------------------------
uniform Matrix matrix;
uniform Light light;
uniform vec3 cameraPosition; //position of viewer
// Bone deformation matrices
uniform mat4 boneFinalMatrix [30];
uniform mat4 boneInvBindPosition[30];
uniform mat4 boneTranslation [30];
//Output Variables
out vec3 oFragNormal;
out vec2 oTexCoord;
out vec3 oFragPosition;
void main() {
mat3 Normal_Matrix = mat3( transpose(inverse(matrix.view)) );
//PLACE HOLDER
vec4 INPUT= vec4(inputPosition , 1.0);
vec4 vertex= vec4(0.0,0.0,0.0,0.0);
vec3 normal= vec3(0.0,0.0,0.0);
int id = 0;
mat4 im;
vec4 O;
float weight;
mat4 locTrans;
mat4 invMatrix;
mat4 transform;
mat4 iBindMatrix;
mat4 FinalTranform;
mat4 Ttrans;
for(int i = 0; i < number_influence.x; i++){
id = boneIDs[i];
weight = boneWeights[id];
if(weight <= 0.0) weight = 1.0;
invMatrix = boneInvBindPosition[id];
locTrans = boneTranslation[id];
transform = boneFinalMatrix[id] ;
FinalTranform += transform * weight;
iBindMatrix+= (invMatrix)*weight;
Ttrans += locTrans * weight;
}
vertex = ( FinalTranform )* ( iBindMatrix * (INPUT)) ;
normal = mat3(FinalTranform) * mat3(iBindMatrix) * inputNormal ;
// output the transformed vertex
gl_Position = matrix.projection * matrix.view * vertex;
oFragPosition = vec3(matrix.view * vertex);
oFragNormal = Normal_Matrix * normal;
//store the texture data
oTexCoord = inputTexture.xy;
}
FragmentShader
#version 330
const vec4 AMBIENT = vec4(0.452, 0.452, 0.479, 1.0); //0.2 for all component is a good dark value
struct Light {
vec3 position;
vec3 diffuse;
vec3 direction;
float specularExponent;
vec3 ambient;
vec3 specular;
};
//the image
uniform sampler2D textureSampler;
uniform sampler2D normalSampler;
uniform vec3 cameraPosition;
uniform vec3 materialDiffuse;
uniform vec3 materialSpecular;
uniform Light light[10];
uniform int numberLights;
out vec4 finalOutput;
in vec2 oTexCoord;
in vec3 oFragNormal;
in vec3 oFragPosition;
void main() {
int i=0;
vec3 N = normalize(oFragNormal);
//V = frag to viewier vector
vec3 V = normalize(cameraPosition - oFragPosition );
vec4 texColor = texture2D(textureSampler, oTexCoord);
vec3 mDiffuse = vec3(0.0);
vec3 mSpecular = vec3(0.0);
float N_dot_L = 0.0;
float N_dot_H = 0.0;
vec3 L;
vec3 H;
for(i= 0; i < numberLights; i++){
L = normalize(oFragPosition - light[i].position );
//Diffuse: CmaterialDiffuseColor = max(Normal * LightDir, 0) * CmaterialColor * ClightColor
N_dot_L = max (dot (N, L), 0.0 ) ;
mDiffuse += materialDiffuse * light[i].diffuse * N_dot_L ;
//Specular: CmaterialSpecularColor = max(Normal * HalfAngleLightandViewVector, 0)^exp * CmaterialColor * ClightColor
H = normalize((L + V)/2.0);
N_dot_H = max(dot (N , H), 0.0) ;
mSpecular += materialSpecular * light[i].specular * vec3(pow(N_dot_H, light[i].specularExponent));
}
finalOutput = texColor * vec4(mDiffuse,1.0) ;
finalOutput.a=1.0;
}
你沒有在任何地方添加環境,這是因爲你的漫反射顏色導致黑色所以需要 –
我是否通過添加以下內容來添加環境:finalOutput = texColor * vec4(mDiffuse,1.0)+ vec4(0.3,0.3,0.3, 1.0); – ukaku
如果白色球體是一個光線,並且它在物體後面,我的第一個問題是爲什麼表面面對用戶點亮?有些事情是非常錯誤的=) – Drop