From f6e61e70cebafe59eee8bda8db37d86c029a85be Mon Sep 17 00:00:00 2001 From: hyzboy Date: Tue, 12 Mar 2024 00:42:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E4=BA=86specular=E7=AE=97?= =?UTF-8?q?=E6=B3=95=EF=BC=8C=E4=BD=86=E6=98=AF=E6=9C=89=E7=82=B9=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E3=80=82=E3=80=82=E3=80=82=E8=80=8C=E4=B8=94=E9=AB=98?= =?UTF-8?q?=E5=85=89=E5=8C=BA=E6=9C=89=E7=82=B9=E8=B1=A1=E6=98=AFVS?= =?UTF-8?q?=E7=AE=97=E7=9A=84=E4=B8=80=E6=A0=B7=EF=BC=8C=E6=80=80=E7=96=91?= =?UTF-8?q?=E6=98=AFNORMAL=E5=9C=A8VS=E7=AE=97=E7=9A=84=E5=8E=9F=E5=9B=A0?= =?UTF-8?q?=E3=80=82=E4=BD=86fs=E6=B2=A1=E6=9C=89GetLocalToWorld=E5=87=BD?= =?UTF-8?q?=E6=95=B0=EF=BC=8C=E9=9C=80=E8=A6=81=E5=B0=9D=E8=AF=95=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Std3D/BlinnPhong/SunLightPureColor.mtl | 44 +++++++++++++------ .../LightBasic/BlinnPhongDirectionLight.cpp | 4 +- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/ShaderLibrary/Std3D/BlinnPhong/SunLightPureColor.mtl b/ShaderLibrary/Std3D/BlinnPhong/SunLightPureColor.mtl index 6c09d1c1..35e23648 100644 --- a/ShaderLibrary/Std3D/BlinnPhong/SunLightPureColor.mtl +++ b/ShaderLibrary/Std3D/BlinnPhong/SunLightPureColor.mtl @@ -39,13 +39,26 @@ Output Code { + mat3 GetNormalMatrix() + { + return mat3(camera.view*GetLocalToWorld()); + } + + vec3 GetNormal(mat3 normal_matrix,vec3 normal) + { + return normalize(normal_matrix*normal); + } + void main() { + mat3 normal_matrix=GetNormalMatrix(); + + Output.Normal =GetNormal(normal_matrix,Normal); + Output.Position =GetPosition3D(); + HandoverMI(); - Output.Normal=Normal; - Output.Position=GetPosition3D(); - gl_Position=Output.Position; + gl_Position =Output.Position; } } @@ -62,30 +75,33 @@ Code MaterialInstance mi=GetMI(); //将法线归一化 - vec3 world_normal =normalize(Input.Normal); +// vec3 world_normal =normalize(Input.Normal); //对世界坐标下的灯光方法归一化 - vec3 world_light_direction =normalize(sun.direction.xyz); +// vec3 world_light_direction =normalize(sun.direction.xyz); //点乘法线和光照 - vec3 diffuse =vec3(0.5)*dot(world_light_direction.xyz,world_normal)+vec3(0.5); + float intensity=0.5*max(dot(Input.Normal,sun.direction.xyz),0.0)+0.5; //直接光颜色 - vec3 direct_color =diffuse*sun.color.rgb*mi.Color.rgb; + vec3 direct_color =intensity*sun.color.rgb*mi.Color.rgb; // #ifndef HAVE_SPECULAR // FragColor=vec4(direct_color,1.0); // #else - //归一代视角方向 - vec3 view_direction =normalize(camera.pos-Input.Position.xyz); - //世界坐标下的反射光方向 - vec3 reflect_direction =normalize(reflect(-world_light_direction,world_normal)); + vec3 spec_color=vec3(0.0); - //高光 - float specular =pow(clamp(dot(reflect_direction,view_direction),0.0,1.0),mi.Gloss); + if(intensity>0.0) + { + vec3 half_vector=normalize(sun.direction.xyz+normalize(Input.Position.xyz+camera.pos)); - FragColor =vec4(direct_color+vec3(specular),1.0); + float specular=max(dot(half_vector,Input.Normal),0.0); + + spec_color=specular*pow(specular,mi.Gloss)*sun.color.rgb; + } + + FragColor=vec4(direct_color+spec_color,1.0); // #endif//HAVE_SPECULAR } } diff --git a/example/LightBasic/BlinnPhongDirectionLight.cpp b/example/LightBasic/BlinnPhongDirectionLight.cpp index 0e085c49..5f97f94d 100644 --- a/example/LightBasic/BlinnPhongDirectionLight.cpp +++ b/example/LightBasic/BlinnPhongDirectionLight.cpp @@ -21,7 +21,7 @@ static Color4f white_color(1,1,1,1); static mtl::blinnphong::SunLight sun_light= { Vector4f(0,0,1,0), - Vector4f(1,0,0,1) + Vector4f(1,0.95,0.9,1) }; class TestApp:public SceneAppFramework @@ -137,7 +137,7 @@ private: //Sphere { - prim_sphere=CreateSphere(db,mi_sphere->GetVIL(),16); + prim_sphere=CreateSphere(db,mi_sphere->GetVIL(),32); } return(true);