init commit
This commit is contained in:
50
src/Shader/DemoShader.cpp
Normal file
50
src/Shader/DemoShader.cpp
Normal file
@ -0,0 +1,50 @@
|
||||
//
|
||||
// Created by sfd on 25-7-28.
|
||||
//
|
||||
|
||||
#include "DemoShader.h"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
void DemoVertexShader(BlinnVaryings& varyings, const BlinnVertex& vertex, const BlinnUniform& uniform)
|
||||
{
|
||||
varyings.ClipPos = uniform.MVP * vertex.ModelPosition;
|
||||
varyings.TexCoord = vertex.TexCoord;
|
||||
varyings.WorldPos = uniform.Model * vertex.ModelPosition;
|
||||
varyings.WorldNormal = uniform.ModelNormalToWorld * Math::Vec4{ vertex.ModelNormal, 0.0f};
|
||||
}
|
||||
|
||||
Math::Vec4 DemoFragmentShader(bool& discard, const BlinnVaryings& varyings, const BlinnUniform& uniform)
|
||||
{
|
||||
discard = false;
|
||||
|
||||
const Math::Vec3 cameraPos = uniform.Camera;
|
||||
const Math::Vec3 lightPos = uniform.LightPos;
|
||||
const Math::Vec3 worldPos = varyings.WorldPos;
|
||||
|
||||
Math::Vec3 worldNormal = Math::Normalize(varyings.WorldNormal);
|
||||
const Math::Vec3 viewDir = Math::Normalize(cameraPos - worldPos);
|
||||
const Math::Vec3 lightDir = Math::Normalize(lightPos - worldPos);
|
||||
|
||||
Math::Vec3 halfDir = Math::Normalize(viewDir + lightDir);
|
||||
|
||||
Math::Vec3 ambient = uniform.LightAmbient;
|
||||
Math::Vec3 specularStrength {1.0f, 1.0f, 1.0f};
|
||||
Math::Vec3 diffColor {1.0f , 1.0f , 1.0f};
|
||||
if (uniform.Diffuse)
|
||||
{
|
||||
const Math::Vec2& texCoord = varyings.TexCoord;
|
||||
diffColor = uniform.Diffuse->Sample(texCoord);
|
||||
ambient = ambient * diffColor;
|
||||
if (uniform.Specular)
|
||||
specularStrength = uniform.Specular->Sample(texCoord);
|
||||
}
|
||||
|
||||
|
||||
Math::Vec3 diffuse = std::max(0.0f, Math::Dot(worldNormal, lightDir)) * uniform.LightDiffuse * diffColor;
|
||||
Math::Vec3 specular = (float)pow(std::max(0.0f, Math::Dot(halfDir, worldNormal)), uniform.Shininess) * uniform.LightSPecular * specularStrength;
|
||||
|
||||
Math::Vec3 result = (ambient + diffuse + specular);
|
||||
|
||||
return {result, 1.0f};
|
||||
}
|
||||
Reference in New Issue
Block a user