機械学習基礎理論独習

誤りがあればご指摘いただけると幸いです。数式が整うまで少し時間かかります。リンクフリーです。

勉強ログです。リンクフリーです
目次へ戻る

【OpenGL】Gouraud / Phong / Blinn Phong の Shader の実装【C++】

前提

OpenGL4.6
射影行列はOrtho
Ambient / Diffuse の Matrial は Texture より取得
命名規則などは Blender の GLSL Coding Style を参考にした。

ソース

vert

#version 460 core
// in
layout(location = 0) in vec3 in_ls_pos;
layout(location = 1) in vec3 in_ls_normal;
layout(location = 2) in vec2 in_uv;
// out
layout(location = 0) out vec2 out_uv;
layout(location = 1) out vec4 out_ambient_term;
layout(location = 2) out vec4 out_diffuse_term;
layout(location = 3) out vec4 out_specular_color;
layout(location = 4) out vec3 out_vs_normal;
// uniform
layout(location = 0) uniform mat4 world_mat;
layout(location = 1) uniform mat4 view_mat;
layout(location = 2) uniform mat4 proj_mat;
layout(location = 3) uniform mat3 normal_mat;
layout(location = 4) uniform vec3 vs_light_direction;
layout(location = 5) uniform int shading_type;
layout(location = 6) uniform int shineness;

vec4 ambient_light = vec4(0.2, 0.2, 0.2, 1.0);
vec4 ambient_material = vec4(1.0, 0.0, 0.0, 1.0);
vec4 diffuse_light = vec4(0.8, 0.8, 0.8, 1.0);
vec4 diffuse_material = vec4(1.0, 0.0, 0.0, 1.0);
vec4 specular_light = vec4(0.8, 0.8, 0.8, 1.0);
vec4 specular_material = vec4(1.0, 1.0, 1.0, 1.0);

void main() {
  if(shading_type == 0) {// Gouraud
    // ワールド空間の法線
	vec3 wN = normal_mat * in_ls_normal;
	// ビュー空間の法線(ビュー行列の上位 3x3 部分だけを使う)
	vec3 vN = normalize(mat3(view_mat) * wN);

	// 光源へ向かうベクトル
	vec3 vL = vs_light_direction;
	// 内積を計算
	float lambert_term = max(dot(vN, vL), 0.0);

	// Ambient
	out_ambient_term = ambient_light;
	// Diffuse
	out_diffuse_term = diffuse_light * lambert_term;
	// Specular
	vec3 vV = vec3(0, 0, 1);
	// reflect() 関数(GLSLやHLSLにある標準関数)は
	// "入射ベクトル" を引数にもつので vL を反転させる
	vec3 vR = reflect(-vL, vN); 
	float specular = pow(max(dot(vR, vV), 0.0), shineness);
	out_specular_color = specular_light * specular_material 
					   * specular;
  }
  else {// Phong or Blinn / Phong
    // ビュー空間の法線(ビュー行列の上位 3x3 部分だけを使う)
	out_vs_normal = mat3(view_mat) * normal_mat * in_ls_normal;	
  }  

  gl_Position = proj_mat * view_mat * world_mat * vec4(in_ls_pos, 1.0);
  out_uv = in_uv;
}

fragment

#version 460 core
// in
layout(location = 0) in vec2 in_uv;
layout(location = 1) in vec4 in_ambient_term;
layout(location = 2) in vec4 in_diffuse_term;
layout(location = 3) in vec4 in_specular_color;
layout(location = 4) in vec3 in_vs_normal;
// out
out vec4 out_frag_color;
// uniform
layout(location = 0) uniform mat4 world_mat;
layout(location = 1) uniform mat4 view_mat;
layout(location = 2) uniform mat4 proj_mat;
layout(location = 3) uniform mat3 normal_mat;
layout(location = 4) uniform vec3 vs_light_direction;
layout(location = 5) uniform int shading_type;
layout(location = 6) uniform int shineness;
layout(binding = 0) uniform sampler2D tex;

vec4 ambient_light = vec4(0.2, 0.2, 0.2, 1.0);
vec4 diffuse_light = vec4(0.8, 0.8, 0.8, 1.0);
vec4 specular_light = vec4(0.8, 0.8, 0.8, 1.0);
vec4 specular_material = vec4(1.0, 1.0, 1.0, 1.0);

void main() {
  if(shading_type == 0) {// Gouraud Shading
    vec4 ambient_material = texture(tex, in_uv);
    vec4 diffuse_material = ambient_material;
    out_frag_color = in_ambient_term * ambient_material
			       + in_diffuse_term * diffuse_material
			       + in_specular_color;
  } 
  else {// Phong, Blinn-Phone
	// 線形補間されたことにより、単位ベクトルでなくなるので正規化する
	vec3 vN = normalize(in_vs_normal);

	// 光源へ向かうベクトル
	vec3 vL = vs_light_direction;
	// 内積を計算
	float lambert_term = max(dot(vN, vL), 0.0);

	// Ambient
	vec4 ambient_term = ambient_light;
	vec4 ambient_material = texture(tex, in_uv);
	// Diffuse
	vec4 diffuse_term = diffuse_light * lambert_term;
    vec4 diffuse_material = ambient_material;
	// Specular
	// Ortho なら視点が存在しないので
	// ビュー空間における視点へ向かうベクトルは (0, 0, 1) 固定。
	vec3 vV = vec3(0, 0, 1);
	float specular;
	if(shading_type == 1) {
	  // reflect() 関数(GLSLやHLSLにある標準関数)は
	  // "入射ベクトル" を引数にもつので vL を反転させる
	  // vL は光源を向いているのでいわゆる "入射ベクトル" の逆ベクトル
	  vec3 vR = normalize(reflect(-vL, vN)); 
	  specular = pow(max(dot(vR, vV), 0.0), shineness);
	} else {
	  vec3 vH = normalize(vL + vV); // ハーフベクトル
      specular = pow(max(dot(vN, vH), 0.0), shineness);
	}	
	vec4 specular_color = specular_light * specular_material 
					   * specular;
	out_frag_color = ambient_term * ambient_material
			       + diffuse_term * diffuse_material
			       + specular_color;
  }
  
}

動作

動作は確認しています。座標軸は別のシェーダーで書いています。

目次へ戻る