78 lines
2.7 KiB
C++
78 lines
2.7 KiB
C++
#include "fs_classic_phong.h"
|
|
#include "asset/dvc_material.h"
|
|
#include "pipe/dvc_render_pipe.h"
|
|
#include "shader/dvc_shader_functions.h"
|
|
|
|
using namespace davinci;
|
|
|
|
const char* ClassicFragementShader_Phong::getName(void) const
|
|
{
|
|
return "classic:phong";
|
|
}
|
|
|
|
void ClassicFragementShader_Phong::begin(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material)
|
|
{
|
|
m_pipe = pipe;
|
|
const davinci::UniformBuffer& uniformBuffer = m_pipe->getUniformBuffer();
|
|
|
|
material->getParam("u_materialColor", m_materialColor);
|
|
material->getParam("u_specularShininess", m_specularShininess);
|
|
material->getParam("u_specularStrength", m_specularStrength);
|
|
|
|
if (davinci::fVector3* p_ambient_light_color = (davinci::fVector3*)(uniformBuffer.getAttribute("ambient_light:color")))
|
|
{
|
|
m_ambientLightColor = *p_ambient_light_color;
|
|
}
|
|
if (davinci::fVector3* p_light0_position = (davinci::fVector3*)(uniformBuffer.getAttribute("light0:position")))
|
|
{
|
|
m_light0Position = *p_light0_position;
|
|
}
|
|
if (davinci::fVector3* p_light0_color = (davinci::fVector3*)(uniformBuffer.getAttribute("light0:color")))
|
|
{
|
|
m_light0Color = *p_light0_color;
|
|
}
|
|
|
|
if (davinci::fVector3* p_camera_position = (davinci::fVector3*)(uniformBuffer.getAttribute("camera:eye_pos")))
|
|
{
|
|
m_cameraPosition = *p_camera_position;
|
|
}
|
|
}
|
|
|
|
void ClassicFragementShader_Phong::process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData, davinci::fVector4& color)
|
|
{
|
|
const VertexDesc::Element* v_worldpos_Element = veretexDesc.getElement(davinci::VET_POSITION_WORLD);
|
|
fVector3 v_worldpos = v_worldpos_Element ? *((const fVector3*)(pixelData + (v_worldpos_Element->offset / sizeof(float32_t)))) : fVector3::ZERO;
|
|
|
|
const VertexDesc::Element* v_normal_Element = veretexDesc.getElement(davinci::VET_NORMAL);
|
|
fVector3 v_normal = v_normal_Element ? *((const fVector3*)(pixelData + (v_normal_Element->offset / sizeof(float32_t)))) : fVector3::UNIT_Z;
|
|
|
|
fVector3 toLight = m_light0Position - v_worldpos;
|
|
toLight.normalise();
|
|
|
|
fVector3 toCamera = m_cameraPosition - v_worldpos;
|
|
toCamera.normalise();
|
|
|
|
float32_t ndotl = toLight.dotProduct(v_normal);
|
|
fVector3 vR = 2 * ndotl*v_normal - toLight;
|
|
float32_t rdotv = vR.dotProduct(toCamera);
|
|
|
|
fVector3 colAmbient = m_ambientLightColor * m_materialColor.xyz();
|
|
|
|
fVector3 colDiffuse = shader::max(0.0f, ndotl) * m_light0Color * m_materialColor.xyz();
|
|
|
|
float32_t specular = shader::step(0.f, ndotl) * m_specularStrength * shader::pow(shader::max(0.0f, rdotv), m_specularShininess);
|
|
fVector3 colSpecular = specular * m_light0Color;
|
|
|
|
fVector3 final = fVector3::BLACK;
|
|
final += colAmbient;
|
|
final += colDiffuse;
|
|
final += colSpecular;
|
|
|
|
color = fVector4(final.saturate(), 1.0f);
|
|
}
|
|
|
|
void ClassicFragementShader_Phong::end()
|
|
{
|
|
}
|
|
|