From 0b1e1370326973c434a67ea37326507e9a11b975 Mon Sep 17 00:00:00 2001 From: blobt Date: Sun, 5 Jul 2020 13:24:27 +0800 Subject: [PATCH] =?UTF-8?q?=E7=82=B9=E5=85=89=E6=BA=90=E6=9E=B6=E6=9E=84?= =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vs/shader3/v14/.suo | Bin 28672 -> 23040 bytes main.cpp | 4 ++-- res/shader/pointLight.fs | 39 +++++++++++++++++++++++++++++++++++++++ res/shader/pointLight.vs | 19 +++++++++++++++++++ res/shader/specular.fs | 32 ++++++++++++++++++++++++++++++++ res/shader/specular.vs | 19 +++++++++++++++++++ res/shader/test.fs | 11 +++++++++-- res/shader/test.vs | 3 ++- 8 files changed, 122 insertions(+), 5 deletions(-) create mode 100644 res/shader/pointLight.fs create mode 100644 res/shader/pointLight.vs create mode 100644 res/shader/specular.fs create mode 100644 res/shader/specular.vs diff --git a/.vs/shader3/v14/.suo b/.vs/shader3/v14/.suo index d952caf3338a4fcb4fde391e82f6c7a6791ef032..e77638feb3233e4723e258005c8d954ca1b22016 100644 GIT binary patch delta 2936 zcmd5;ZA_C_6u$RuUkdbXDYPQrNI|Pm5!A?s0!6D}+tIfa=RPpm5d4~7fx^V8tx7b@ zzOZMBe`YeB{#h0s>2%4qAK;d3OO~+tV_%7h`?27bCHqsN7=fMhwy%_J7$p0#+dR4V zoO{l_=iGbl^EPychd$zi4lWEe>IJTWBB9XexcH7Z(E2@3XV{j&QQ;^*4&y?L$PK7~ z?_8^(p&|uBI;XT46H@(}rm0>-SY5C=;TMF=aR7_khYZa8($XVzw+V#p`+u;7WO zeP`c^*=f5Z@}w}xlhS*VbAbCgQ;-gf&VX`yT+VbR$?=WU{3&-4q2`{3;}pbWa{qhk zMpl9j6#Y`{G4I9kysz=07vAMCo#r%fnwH{blq3X9va|vf}yIy($6(+cexc#!T=R+`6_ z-sA;scXk1Huw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{pDpZ{yIRK_cQ|hLNFEl( z`Vt z0!~wnq56ZN&E~e%$}c}J8Mt(1VG;5R^T6xK46d1+9Fz_x7&?zo3T!Nn(2Ef}S!bYRqO*DN0NxFu$F*kZrfzLK1b_YIN z)KMjltB}^MtPZVgIJ2UC3v0!Zn|V-2+B)WjkLyd}m8NPi2^onp-XC}J0M5D>fV)QE z?G_o1G>?F#S=M8)oC2nH(n%)4U8kADQMZ^tHw`X@uyYjGe61zoW5Ltz6N1S{!$>=Z zJrM|*Q@+#da=!Y+6gl|hxM!~?;uz_ttr*OLk?Vq=wT1a3rhG}K#MAK$6|3tw)k*-1F=IHu(x+;j1>E+~p?8_gX;-yaYb@;%>G4$qt;*KRQW}q$o zSVVmSzj;tcTN)Mcde}BEaslRwy6&)fXL~x)#h9+=(9IDc|ID$UFAi6|`XNz6JTAlr zidO+=k3l^O%Kr#B3*7%oxGt&p^4>e4$VXKj^cTO*-yIMF*&dPp8nO{lAHkp9C#f@X z;I1@OFbH2{Bj@M%MEZBgiBW3(I5`Kmt=%GKDx;hI?mB-N-VsKW light pos + } + vec3 N = normalize(V_Normal); + float diffuseIntensity = max(0.0, dot(L,N)); + vec4 diffuseColor = U_DiffuseLightColor * U_DiffuseMaterial * diffuseIntensity; + + //specular + vec3 reflectDir = normalize(reflect(-L,N));//-L是入射光向量,通过reflect函数求到反射光方向 + vec3 viewDir = normalize(U_EyePos - V_WorldPos); + vec4 specularColor = U_SpecularLightColor * U_SpecularMaterial * pow(max(0.0, dot(viewDir,reflectDir)), 64.0); + + + gl_FragColor = ambientColor + diffuseColor + specularColor; +} \ No newline at end of file diff --git a/res/shader/pointLight.vs b/res/shader/pointLight.vs new file mode 100644 index 0000000..356d514 --- /dev/null +++ b/res/shader/pointLight.vs @@ -0,0 +1,19 @@ +attribute vec3 pos; +attribute vec2 texcoord; +attribute vec3 normal; + +uniform mat4 M; +uniform mat4 P; +uniform mat4 V; +uniform mat4 NM;//Normal Matrix 使用来修正法线的 + +varying vec3 V_Normal; +varying vec3 V_WorldPos; +void main() +{ + V_Normal = mat3(NM)*normal; + vec4 worldPos = M * vec4(pos, 1.0); + V_WorldPos = worldPos.xyz; + + gl_Position=P*V*M*vec4(pos,1.0); +} \ No newline at end of file diff --git a/res/shader/specular.fs b/res/shader/specular.fs new file mode 100644 index 0000000..3d510e2 --- /dev/null +++ b/res/shader/specular.fs @@ -0,0 +1,32 @@ +uniform vec4 U_AmbientLightColor; +uniform vec4 U_AmbientMaterial; +uniform vec3 U_LightPos; +uniform vec4 U_DiffuseLightColor; +uniform vec4 U_DiffuseMaterial; +uniform vec3 U_EyePos; +uniform vec4 U_SpecularLightColor; +uniform vec4 U_SpecularMaterial; + + +varying vec3 V_Normal; +varying vec3 V_WorldPos; + +void main() +{ + //ambient + vec4 ambientColor = U_AmbientLightColor * U_AmbientMaterial; + + //diffuse + vec3 N = normalize(V_Normal); + vec3 L = normalize(U_LightPos); + float diffuseIntensity = max(0.0, dot(L,N)); + vec4 diffuseColor = U_DiffuseLightColor * U_DiffuseMaterial * diffuseIntensity; + + //specular + vec3 reflectDir = normalize(reflect(-L,N));//-L是入射光向量,通过reflect函数求到反射光方向 + vec3 viewDir = normalize(U_EyePos - V_WorldPos); + vec4 specularColor = U_SpecularLightColor * U_SpecularMaterial * pow(max(0.0, dot(viewDir,reflectDir)), 64.0); + + + gl_FragColor = ambientColor + diffuseColor + specularColor; +} \ No newline at end of file diff --git a/res/shader/specular.vs b/res/shader/specular.vs new file mode 100644 index 0000000..356d514 --- /dev/null +++ b/res/shader/specular.vs @@ -0,0 +1,19 @@ +attribute vec3 pos; +attribute vec2 texcoord; +attribute vec3 normal; + +uniform mat4 M; +uniform mat4 P; +uniform mat4 V; +uniform mat4 NM;//Normal Matrix 使用来修正法线的 + +varying vec3 V_Normal; +varying vec3 V_WorldPos; +void main() +{ + V_Normal = mat3(NM)*normal; + vec4 worldPos = M * vec4(pos, 1.0); + V_WorldPos = worldPos.xyz; + + gl_Position=P*V*M*vec4(pos,1.0); +} \ No newline at end of file diff --git a/res/shader/test.fs b/res/shader/test.fs index 3d510e2..51e6eca 100644 --- a/res/shader/test.fs +++ b/res/shader/test.fs @@ -1,6 +1,6 @@ uniform vec4 U_AmbientLightColor; uniform vec4 U_AmbientMaterial; -uniform vec3 U_LightPos; +uniform vec4 U_LightPos; uniform vec4 U_DiffuseLightColor; uniform vec4 U_DiffuseMaterial; uniform vec3 U_EyePos; @@ -17,8 +17,15 @@ void main() vec4 ambientColor = U_AmbientLightColor * U_AmbientMaterial; //diffuse + vec3 L = vec3(0.0); + if(U_LightPos.w==0.0){ + //这是方向光 + L = normalize(U_LightPos.xyz); + } else { + //这是点光源 + //model point -> light pos + } vec3 N = normalize(V_Normal); - vec3 L = normalize(U_LightPos); float diffuseIntensity = max(0.0, dot(L,N)); vec4 diffuseColor = U_DiffuseLightColor * U_DiffuseMaterial * diffuseIntensity; diff --git a/res/shader/test.vs b/res/shader/test.vs index 5cfa86b..356d514 100644 --- a/res/shader/test.vs +++ b/res/shader/test.vs @@ -12,7 +12,8 @@ varying vec3 V_WorldPos; void main() { V_Normal = mat3(NM)*normal; - V_WorldPos = M * vec4(pos, 1.0); + vec4 worldPos = M * vec4(pos, 1.0); + V_WorldPos = worldPos.xyz; gl_Position=P*V*M*vec4(pos,1.0); } \ No newline at end of file