QT OpenGL blinnphong

xiaoxiao2021-02-28  7

就是normalize(eyedir+lightdir)求一个halfwaydir

eyedir=eyepos-fragpos  lightdir=lightpos-fragpos  fragpos=a_position*model

pow(max( dot(eyedir,halfwaydir),0) ,material.shininess)=spec

specular = light.specular * spec *vec3(texture(material.specular, v_texcoord));

然后就求出来高光了

VERT

#ifdef GL_ES // Set default precision to medium precision mediump int; precision mediump float; #endif uniform mat4 mvp_matrix; uniform mat4 model; //uniform mat3 transpose_inverse_model; attribute vec4 a_position; attribute vec2 a_texcoord; attribute vec3 a_normal; varying vec2 v_texcoord; varying vec3 fragPos; varying vec3 normal; //! [0] void main() { // Calculate vertex position in screen space gl_Position = mvp_matrix * a_position; fragPos=vec3(model*a_position); normal=mat3(transpose(inverse(model)))*a_normal;//model的逆矩阵的转置的左上角3x3矩阵 // // Pass texture coordinate to fragment shader // Value will be automatically interpolated to fragments inside polygon faces v_texcoord = a_texcoord; } //! [0]

FRAG

#ifdef GL_ES // Set default precision to medium precision mediump int; precision mediump float; #endif struct Material { sampler2D diffuse; sampler2D specular; float shininess; }; struct Light { vec3 position; vec3 ambient; vec3 diffuse; vec3 specular; }; uniform Material material; uniform Light light; //uniform sampler2D texture; uniform vec3 viewPos; //uniform vec3 lightPos; //uniform vec3 lightColor; varying vec2 v_texcoord; varying vec3 fragPos; varying vec3 normal; //! [0] void main() { vec4 ans; //float ambientStrength=0.1; vec3 ambient = light.ambient *vec3(texture(material.diffuse, v_texcoord));//环境光 // diffuse vec3 norm = normalize(normal); vec3 lightDir = normalize(light.position - fragPos); float diff = max(dot(norm, lightDir), 0.0); vec3 diffuse = light.diffuse*diff * vec3(texture(material.diffuse, v_texcoord));//漫反射 // specular vec3 viewDir = normalize(viewPos - fragPos); vec3 halfwayDir=normalize(lightDir+viewDir);//blinn phong //vec3 reflectDir = reflect(-lightDir, norm);//phong float spec = pow(max(dot(viewDir, halfwayDir), 0.0), material.shininess); vec3 specular = light.specular * spec *vec3(texture(material.specular, v_texcoord)); vec3 result = ambient + diffuse + specular; //vec3 result = (ambient + diffuse)*texture(material.specular,v_texcoord).rgb; //float gl_FragColor = vec4(result,1.0); } //! [0]
转载请注明原文地址: https://www.6miu.com/read-2300244.html

最新回复(0)