基本照明OpenGl

时间:2016-12-10 09:02:04

标签: c++ opengl

我正在尝试为我的OpengGl场景添加基本灯光。我修改了片段和顶点着色器,但我不知道如何在片段着色器中组合" fragmentColour = texture(diffuseTexture,passTexture);"为了将照明应用于整个场景。我试着添加fColor = vec4(颜色,1.0f);但在这种情况下,纹理不再可见。这是我的顶点和片段着色器:

// vertexShader

#version 400

layout(location = 0) in vec3 vertexPosition;
layout(location = 1) in vec3 vertexNormal;
layout(location = 2) in vec2 textcoord;

out vec3 colour;
out vec2 passTexture; 

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
uniform mat3 normalMatrix;

//lighting
uniform vec3 lightDir;
uniform vec3 lightColor;
uniform vec3 baseColor;

vec3 ambient;
float ambientStrength = 0.2f;
vec3 diffuse;
vec3 specular;
float specularStrength = 0.5f;
float shininess = 32.0f;

void main() {
//compute ambient light
ambient = ambientStrength * lightColor;

//normalize the light's direction
vec3 lightDirN = normalize(lightDir);
//compute eye coordinates for normals (transform normals)
vec3 normalEye = normalize(normalMatrix * vertexNormal);
//compute diffuse light
diffuse = max(dot(normalEye, lightDirN), 0.0f) * lightColor;


//compute the vertex position in eye coordinates
vec4 vertPosEye = view * model * vec4(vertexPosition, 1.0f);
//compute the view (Eye) direction (in eye coordinates, the camera is at the origin)
vec3 viewDir = normalize(- vertPosEye.xyz);
//compute the light's reflection (the reflect function requires a direction pointing towards the vertex, not away
//from it)
vec3 reflectDir = normalize(reflect(-lightDir, normalEye));
//compute specular light
float specCoeff = pow(max(dot(viewDir, reflectDir), 0.0f), shininess);
specular = specularStrength * specCoeff * lightColor;


//compute final vertex color
colour = min((ambient + diffuse) * baseColor + specular, 1.0f);


passTexture = textcoord; 
    gl_Position = projection * view * model * vec4(vertexPosition, 1.0);
}

// fragmentShader

#version 400

in vec3 colour;
in vec2 passTexture; 
out vec4 fColor;

out vec4 fragmentColour;
uniform sampler2D diffuseTexture; 
void main() {

    fragmentColour = texture(diffuseTexture, passTexture); 
fColor = vec4(color, 1.0f);


}

0 个答案:

没有答案