11.15
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
add_subdirectory(core)
|
||||
add_subdirectory(utils)
|
||||
add_subdirectory(console)
|
||||
add_subdirectory(shaders)
|
||||
|
||||
|
||||
#################
|
||||
|
||||
@@ -26,6 +26,9 @@ int32_t g_canvasWidth = 1024;
|
||||
int32_t g_canvasHeight = 512;
|
||||
davinci::Device g_device;
|
||||
davinci::Scene g_scene;
|
||||
int32_t g_mousePosX = 0;
|
||||
int32_t g_mousePosY = 0;
|
||||
davinci::fVector4 g_mouseColor = davinci::fVector4::BLACK;
|
||||
|
||||
//-------------------------------------------------------------------------------------
|
||||
//int main(int argc, char* argv[])
|
||||
@@ -160,6 +163,20 @@ SDL_AppResult SDL_AppIterate(void* appstate)
|
||||
g_adapterTexture.updateTexture(g_canvasWidth, 0, g_renderTarget.getDepthBuffer());
|
||||
g_adapterTexture.draw();
|
||||
|
||||
float mousePosX, mousePosY;
|
||||
SDL_GetMouseState(&mousePosX, &mousePosY);
|
||||
|
||||
g_mousePosX = (int32_t)(mousePosX) % g_canvasWidth;
|
||||
g_mousePosY = g_screenHeight - (int32_t)(mousePosY);
|
||||
if(g_mousePosX>=0 && g_mousePosX < g_canvasWidth && g_mousePosY >=0 && g_mousePosY < g_canvasHeight)
|
||||
{
|
||||
g_mouseColor = g_renderTarget.getColorBuffer().getPixel(g_mousePosX, g_mousePosY);
|
||||
}
|
||||
else
|
||||
{
|
||||
g_mouseColor = davinci::fVector4::BLACK;
|
||||
}
|
||||
|
||||
//draw ui
|
||||
drawMainUI();
|
||||
|
||||
|
||||
@@ -25,6 +25,9 @@ extern davinci::Device g_device;
|
||||
extern davinci::Scene g_scene;
|
||||
extern davinci::RenderTarget g_renderTarget;
|
||||
extern davinci::AntiAliasingTech g_aaTech;
|
||||
extern int32_t g_mousePosX;
|
||||
extern int32_t g_mousePosY;
|
||||
extern davinci::fVector4 g_mouseColor;
|
||||
|
||||
bool initMainUI(SDL_Window* window, SDL_GLContext gl_context, const char* glsl_version)
|
||||
{
|
||||
@@ -129,6 +132,10 @@ void drawMainUI()
|
||||
|
||||
davinci_utils::dump_render_target("d:\\rendertarget", g_renderTarget);
|
||||
}
|
||||
ImGui::Separator();
|
||||
ImVec2 mousePos = ImGui::GetMousePos();
|
||||
ImGui::Text("MousePosition: %d,%d", g_mousePosX, g_mousePosY);
|
||||
ImGui::Text("MouseColor: %.3f,%.3f,%.3f,%.3f", g_mouseColor.x, g_mouseColor.y, g_mouseColor.z, g_mouseColor.w);
|
||||
ImGui::End();
|
||||
|
||||
// Rendering
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "dvc_config.h"
|
||||
#include "dvc_pre_declare.h"
|
||||
#include "math/dvc_vector4.h"
|
||||
#include "math/dvc_matrix3.h"
|
||||
|
||||
DV_CORE_BEGIN_NAMESPACE
|
||||
|
||||
@@ -13,16 +14,14 @@ public:
|
||||
{
|
||||
TextureConstPtr texture;
|
||||
int32_t texCoord = 0;
|
||||
fMatrix3 uvTransform;
|
||||
};
|
||||
|
||||
public:
|
||||
int32_t getID(void) const { return m_id; }
|
||||
|
||||
void setBaseColorTexture(const TextureConstPtr& texture, int32_t texCoord, const fVector4& baseColorfactor) {
|
||||
m_baseColorTexture.texture = texture;
|
||||
m_baseColorTexture.texCoord = texCoord;
|
||||
m_baseColorFactor = baseColorfactor;
|
||||
}
|
||||
void setBaseColorTexture(const TextureConstPtr& texture, int32_t texCoord, const fMatrix3& uvTransform,
|
||||
const fVector4& baseColorfactor);
|
||||
const TextureInfo& getBaseColorTexture() const {
|
||||
return m_baseColorTexture;
|
||||
}
|
||||
@@ -31,12 +30,8 @@ public:
|
||||
return m_baseColorFactor;
|
||||
}
|
||||
|
||||
void setMetallicRoughnessTexture(const TextureConstPtr& texture, int32_t texCoord, float32_t metallicFactor=1.f, float32_t roughnessFactor=1.f ) {
|
||||
m_metallicRoughnessTexture.texture = texture;
|
||||
m_metallicRoughnessTexture.texCoord = texCoord;
|
||||
m_metallicFactor = metallicFactor;
|
||||
m_roughnessFactor = roughnessFactor;
|
||||
}
|
||||
void setMetallicRoughnessTexture(const TextureConstPtr& texture, int32_t texCoord, const fMatrix3& uvTransform,
|
||||
float32_t metallicFactor = 1, float32_t roughnessFactor = 1);
|
||||
|
||||
const TextureInfo& getMetallicRoughnessTexture() const {
|
||||
return m_metallicRoughnessTexture;
|
||||
@@ -50,11 +45,8 @@ public:
|
||||
return m_roughnessFactor;
|
||||
}
|
||||
|
||||
void setNormalTexture(const TextureConstPtr& texture, int32_t texCoord, float32_t scale=1.f) {
|
||||
m_normalTexture.texture = texture;
|
||||
m_normalTexture.texCoord = texCoord;
|
||||
m_normalScale = scale;
|
||||
}
|
||||
void setNormalTexture(const TextureConstPtr& texture, int32_t texCoord, const fMatrix3& uvTransform,
|
||||
float32_t scale = 1);
|
||||
|
||||
const TextureInfo& getNormalTexture() const {
|
||||
return m_normalTexture;
|
||||
@@ -64,6 +56,9 @@ public:
|
||||
return m_normalScale;
|
||||
}
|
||||
|
||||
void setVertexShader(const char* szShaderFile, const char* vertexShaderName);
|
||||
void setFragementShader(const char* szShaderFile, const char* fragementShaderName);
|
||||
|
||||
VertexShaderInterface* getVertexShader() const {
|
||||
return m_vertexShader;
|
||||
}
|
||||
@@ -72,6 +67,7 @@ public:
|
||||
return m_pixelShader;
|
||||
}
|
||||
|
||||
|
||||
public:
|
||||
bool loadShader();
|
||||
|
||||
@@ -105,6 +101,12 @@ protected:
|
||||
VertexShaderInterface* m_vertexShader = nullptr;
|
||||
PixelShaderInterface* m_pixelShader = nullptr;
|
||||
|
||||
std::string m_vertexshaderFile;
|
||||
std::string m_vertexShaderName;
|
||||
|
||||
std::string m_fragementShaderFile;
|
||||
std::string m_fragementShaderName;
|
||||
|
||||
public:
|
||||
Material(int32_t id);
|
||||
~Material();
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "dvc_pre_declare.h"
|
||||
#include "math/dvc_vector2.h"
|
||||
#include "math/dvc_vector4.h"
|
||||
#include "math/dvc_matrix3.h"
|
||||
|
||||
DV_CORE_BEGIN_NAMESPACE
|
||||
|
||||
|
||||
@@ -94,6 +94,45 @@ public:
|
||||
template<typename U>
|
||||
friend TMatrix3<U> operator * (U fScalar, const TMatrix3<U>& rkMatrix);
|
||||
|
||||
public:
|
||||
static inline TMatrix3 makeTrans(T x, T y) {
|
||||
return TMatrix3(
|
||||
1, 0, x,
|
||||
0, 1, y,
|
||||
0, 0, 1);
|
||||
}
|
||||
|
||||
static inline TMatrix3 makeTrans(const TVector2<T>& pos) {
|
||||
return TMatrix3(
|
||||
1, 0, pos.x,
|
||||
0, 1, pos.y,
|
||||
0, 0, 1);
|
||||
}
|
||||
|
||||
static inline TMatrix3 makeScale(T x, T y) {
|
||||
return TMatrix3(
|
||||
x, 0, 0,
|
||||
0, y, 0,
|
||||
0, 0, 1);
|
||||
}
|
||||
|
||||
static inline TMatrix3 makeScale(const TVector2<T>& scale) {
|
||||
return TMatrix3(
|
||||
scale.x, 0, 0,
|
||||
0, scale.y, 0,
|
||||
0, 0, 1);
|
||||
}
|
||||
|
||||
//2D rotate, left multiplication
|
||||
static inline TMatrix3 makeRotate(T angle) {
|
||||
T s = sin(angle);
|
||||
T c = cos(angle);
|
||||
return TMatrix3(
|
||||
c, -s, 0,
|
||||
s, c, 0,
|
||||
0, 0, 1);
|
||||
}
|
||||
|
||||
public:
|
||||
// Utilities
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "dvc_pre_declare.h"
|
||||
#include "math/dvc_vector2.h"
|
||||
#include "math/dvc_vector4.h"
|
||||
#include "asset/dvc_material.h"
|
||||
|
||||
DV_CORE_BEGIN_NAMESPACE
|
||||
|
||||
@@ -30,7 +31,7 @@ public:
|
||||
return (a < b) ? a : b;
|
||||
}
|
||||
|
||||
static DV_CORE_API fVector4 texture2D(const RenderPipe& pipe, const char* textName, const fVector2& uv, const fVector4& defaultValue);
|
||||
static DV_CORE_API fVector4 texture2D(const RenderPipe& pipe, const Material::TextureInfo& texturePtr, const fVector2& uv, const fVector4& defaultValue);
|
||||
};
|
||||
|
||||
DV_CORE_END_NAMESPACE
|
||||
|
||||
@@ -26,8 +26,7 @@ class PixelShaderInterface
|
||||
public:
|
||||
virtual const char* getName(void) const = 0;
|
||||
|
||||
typedef std::function<size_t(const std::string&)> SetInputAttributeFunc;
|
||||
virtual void prepare(const RenderPipe* pipe, SetInputAttributeFunc setInputAttributeFunc) = 0;
|
||||
virtual void prepare(const RenderPipe* pipe, MaterialConstPtr material) = 0;
|
||||
|
||||
virtual void process(const VertexDesc& veretexDesc, const float32_t* pixelData, fVector4& color) = 0;
|
||||
};
|
||||
|
||||
@@ -12,12 +12,73 @@ Material::~Material()
|
||||
{
|
||||
}
|
||||
|
||||
void Material::setBaseColorTexture(const TextureConstPtr& texture, int32_t texCoord, const fMatrix3& uvTransform,
|
||||
const fVector4& baseColorfactor)
|
||||
{
|
||||
m_baseColorTexture.texture = texture;
|
||||
m_baseColorTexture.texCoord = texCoord;
|
||||
m_baseColorTexture.uvTransform = uvTransform;
|
||||
|
||||
m_baseColorFactor = baseColorfactor;
|
||||
}
|
||||
|
||||
void Material::setMetallicRoughnessTexture(const TextureConstPtr& texture, int32_t texCoord, const fMatrix3& uvTransform,
|
||||
float32_t metallicFactor, float32_t roughnessFactor)
|
||||
{
|
||||
m_metallicRoughnessTexture.texture = texture;
|
||||
m_metallicRoughnessTexture.texCoord = texCoord;
|
||||
m_metallicRoughnessTexture.uvTransform = uvTransform;
|
||||
|
||||
m_metallicFactor = metallicFactor;
|
||||
m_roughnessFactor = roughnessFactor;
|
||||
}
|
||||
|
||||
void Material::setNormalTexture(const TextureConstPtr& texture, int32_t texCoord, const fMatrix3& uvTransform,
|
||||
float32_t scale)
|
||||
{
|
||||
m_normalTexture.texture = texture;
|
||||
m_normalTexture.texCoord = texCoord;
|
||||
m_normalTexture.uvTransform = uvTransform;
|
||||
|
||||
m_normalScale = scale;
|
||||
}
|
||||
|
||||
void Material::setVertexShader(const char* szShaderFile, const char* vertexShaderName)
|
||||
{
|
||||
m_vertexshaderFile = szShaderFile;
|
||||
m_vertexShaderName = vertexShaderName;
|
||||
}
|
||||
|
||||
void Material::setFragementShader(const char* szShaderFile, const char* fragementShaderName)
|
||||
{
|
||||
m_fragementShaderFile = szShaderFile;
|
||||
m_fragementShaderName = fragementShaderName;
|
||||
}
|
||||
|
||||
bool Material::loadShader()
|
||||
{
|
||||
ShaderFileInterface* shaderInterface = davinci::LoadShaderFile("TestShader.dll");
|
||||
m_vertexShader = shaderInterface->getVertexShader("test");
|
||||
m_pixelShader = shaderInterface->getPixelShader("test");
|
||||
ShaderFileInterface* shaderInterface = davinci::LoadShaderFile(m_vertexshaderFile.c_str());
|
||||
if (shaderInterface == nullptr)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
m_vertexShader = shaderInterface->getVertexShader(m_vertexShaderName.c_str());
|
||||
if (!m_vertexShader)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
shaderInterface = davinci::LoadShaderFile(m_fragementShaderFile.c_str());
|
||||
if (shaderInterface == nullptr)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
m_pixelShader = shaderInterface->getPixelShader(m_fragementShaderName.c_str());
|
||||
if (!m_pixelShader)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -20,8 +20,12 @@ fVector4 Texture::sample(const fVector2& uv) const
|
||||
fVector4 color = fVector4::ZERO;
|
||||
if (!m_image) return color;
|
||||
|
||||
int32_t u = uv.x * m_image->getWidth();
|
||||
int32_t v = uv.y * m_image->getHeight();
|
||||
float uv_x = uv.x - std::floor(uv.x);
|
||||
float uv_y = uv.y - std::floor(uv.y);
|
||||
|
||||
|
||||
int32_t u = uv_x * m_image->getWidth();
|
||||
int32_t v = uv_y * m_image->getHeight();
|
||||
|
||||
return m_image->getPixel(u, v);
|
||||
}
|
||||
|
||||
@@ -26,18 +26,8 @@ void RasterizerStep::process(RenderPipe& pipe, const VertexShaderStep::NodeArray
|
||||
{
|
||||
const VertexShaderStep::Node& node = *it;
|
||||
|
||||
if (node.material->getBaseColorTexture().texture)
|
||||
{
|
||||
uniformBuffer.setAttribute("material:pbr:base_color_texture", node.material->getBaseColorTexture().texture->getID());
|
||||
}
|
||||
|
||||
if (node.material->getNormalTexture().texture)
|
||||
{
|
||||
uniformBuffer.setAttribute("material:pbr:normal_texture", node.material->getNormalTexture().texture->getID());
|
||||
}
|
||||
|
||||
PixelShaderInterface* pixelShader = node.material->getPixelShader();
|
||||
pixelShader->prepare(&pipe, nullptr);
|
||||
pixelShader->prepare(&pipe, node.material);
|
||||
|
||||
size_t vertexSize = node.vertexDesc.vertexByteSize();
|
||||
size_t POS_offset = node.vertexDesc.getElement(VET_POSITION)->offset;
|
||||
@@ -75,9 +65,14 @@ void RasterizerStep::process(RenderPipe& pipe, const VertexShaderStep::NodeArray
|
||||
pixelData[j] = MathUtil::lerp3(vertex0[j], vertex1[j], vertex2[j], percentCorrected);
|
||||
}
|
||||
|
||||
if(dot.first==570 && dot.second==405 )
|
||||
{
|
||||
int a = 0;
|
||||
}
|
||||
fVector4 color;
|
||||
pixelShader->process(node.vertexDesc, &(pixelData[0]), color);
|
||||
|
||||
|
||||
float32_t depth = MathUtil::lerp3(vertexDepth[0], vertexDepth[1], vertexDepth[2], percentCorrected);
|
||||
|
||||
int32_t x = dot.first;
|
||||
|
||||
@@ -4,15 +4,12 @@
|
||||
|
||||
DV_CORE_BEGIN_NAMESPACE
|
||||
|
||||
fVector4 shader::texture2D(const RenderPipe& pipe, const char* textName, const fVector2& uv, const fVector4& defaultValue)
|
||||
fVector4 shader::texture2D(const RenderPipe& pipe, const Material::TextureInfo& textureInfo, const fVector2& uv, const fVector4& defaultValue)
|
||||
{
|
||||
davinci::fVector4 textureColor = defaultValue;
|
||||
const int32_t* textureID = (const int32_t* )(pipe.getUniformBuffer().getAttribute(textName));
|
||||
if (textureID != nullptr)
|
||||
{
|
||||
textureColor = pipe.getDevice().getTexture(*textureID)->sample(uv);
|
||||
}
|
||||
return textureColor;
|
||||
if (!(textureInfo.texture)) return defaultValue;
|
||||
fVector3 uvTransform = textureInfo.uvTransform * fVector3(uv, 1);
|
||||
return textureInfo.texture->sample(uvTransform.xy());
|
||||
}
|
||||
|
||||
|
||||
DV_CORE_END_NAMESPACE
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
#################
|
||||
# sub dictionary
|
||||
#################
|
||||
add_subdirectory(standard)
|
||||
@@ -0,0 +1,34 @@
|
||||
add_library(dv_standard_shaders SHARED
|
||||
dll_main.cpp
|
||||
vs_standard.h
|
||||
vs_standard.inc
|
||||
fs_debug_solid_color.h
|
||||
fs_debug_solid_color.cpp
|
||||
fs_debug_base_color.h
|
||||
fs_debug_base_color.cpp
|
||||
fs_debug_normal_texture.h
|
||||
fs_debug_normal_texture.cpp
|
||||
fs_debug_texture_coordinates.h
|
||||
fs_debug_texture_coordinates.cpp
|
||||
fs_debug_geometry_normal.h
|
||||
fs_debug_geometry_normal.cpp
|
||||
)
|
||||
|
||||
add_definitions(-DDV_SHADER_EXPORTS)
|
||||
|
||||
target_include_directories(dv_standard_shaders
|
||||
PRIVATE
|
||||
${DV_AUTO_INCLUDE_PATH}
|
||||
${DV_CORE_INCLUDE_PATH}
|
||||
)
|
||||
|
||||
target_link_libraries(dv_standard_shaders
|
||||
dv_core
|
||||
)
|
||||
|
||||
#################
|
||||
# copy runtime dll(for debug)
|
||||
#################
|
||||
add_custom_command(
|
||||
TARGET dv_standard_shaders POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:dv_standard_shaders> ${CMAKE_BINARY_DIR}/source/console/$<$<CONFIG:Debug>:Debug>$<$<CONFIG:Release>:Release>/)
|
||||
@@ -0,0 +1,82 @@
|
||||
#include "dv_config.h"
|
||||
#include "shader/dvc_shader_interface.h"
|
||||
|
||||
#include "vs_standard.h"
|
||||
#include "fs_debug_solid_color.h"
|
||||
#include "fs_debug_base_color.h"
|
||||
#include "fs_debug_normal_texture.h"
|
||||
#include "fs_debug_texture_coordinates.h"
|
||||
#include "fs_debug_geometry_normal.h"
|
||||
|
||||
class StandardShaderInterface : public davinci::ShaderFileInterface
|
||||
{
|
||||
public:
|
||||
virtual davinci::VertexShaderInterface* getVertexShader(const char* szName)
|
||||
{
|
||||
auto it = m_vertexShaderMap.find(szName);
|
||||
if (it != m_vertexShaderMap.end()) return it->second;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
virtual davinci::PixelShaderInterface* getPixelShader(const char* szName)
|
||||
{
|
||||
auto it = m_fragementShaderMap.find(szName);
|
||||
if (it != m_fragementShaderMap.end()) return it->second;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
public:
|
||||
StandardShaderInterface()
|
||||
{
|
||||
registerVertexShader(new StandardVertexShader<false , false>());
|
||||
registerVertexShader(new StandardVertexShader<true , false>());
|
||||
registerVertexShader(new StandardVertexShader<false , true>());
|
||||
registerVertexShader(new StandardVertexShader<true , true>());
|
||||
|
||||
registerFragementShader(new DebugFragementShader_SolidColor());
|
||||
registerFragementShader(new DebugFragementShader_BaseColor());
|
||||
registerFragementShader(new DebugFragementShader_NormalTexture());
|
||||
registerFragementShader(new DebugFragementShader_TextureCoordinates());
|
||||
registerFragementShader(new DebugFragementShader_GeometryNormal());
|
||||
}
|
||||
|
||||
~StandardShaderInterface()
|
||||
{
|
||||
for (auto it = m_vertexShaderMap.begin(); it != m_vertexShaderMap.end(); ++it)
|
||||
{
|
||||
delete it->second;
|
||||
}
|
||||
m_vertexShaderMap.clear();
|
||||
|
||||
for (auto it = m_fragementShaderMap.begin(); it != m_fragementShaderMap.end(); ++it)
|
||||
{
|
||||
delete it->second;
|
||||
}
|
||||
m_fragementShaderMap.clear();
|
||||
}
|
||||
|
||||
private:
|
||||
void registerVertexShader(davinci::VertexShaderInterface* vertexShader)
|
||||
{
|
||||
m_vertexShaderMap.insert(std::make_pair(vertexShader->getName(), vertexShader));
|
||||
}
|
||||
void registerFragementShader(davinci::PixelShaderInterface* fragementShader)
|
||||
{
|
||||
m_fragementShaderMap.insert(std::make_pair(fragementShader->getName(), fragementShader));
|
||||
}
|
||||
|
||||
private:
|
||||
std::map<std::string, davinci::VertexShaderInterface*> m_vertexShaderMap;
|
||||
std::map<std::string, davinci::PixelShaderInterface*> m_fragementShaderMap;
|
||||
};
|
||||
|
||||
extern "C"
|
||||
{
|
||||
|
||||
__declspec(dllexport) davinci::ShaderFileInterface* ShaderFileInterface()
|
||||
{
|
||||
static StandardShaderInterface theInterface;
|
||||
return &theInterface;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#include "fs_debug_base_color.h"
|
||||
#include "device/dvc_device.h"
|
||||
#include "device/dvc_uniform_buffer.h"
|
||||
#include "asset/dvc_texture.h"
|
||||
#include "shader/dvc_shader_functions.h"
|
||||
#include "pipe/dvc_render_pipe.h"
|
||||
|
||||
const char* DebugFragementShader_BaseColor::getName(void) const
|
||||
{
|
||||
return "debug:base_color";
|
||||
}
|
||||
|
||||
void DebugFragementShader_BaseColor::prepare(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material)
|
||||
{
|
||||
m_pipe = pipe;
|
||||
m_material = material;
|
||||
}
|
||||
|
||||
void DebugFragementShader_BaseColor::process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData, davinci::fVector4& color)
|
||||
{
|
||||
const davinci::UniformBuffer& uniformBuffer = m_pipe->getUniformBuffer();
|
||||
|
||||
const davinci::VertexDesc::Element* v_uv_Element = veretexDesc.getElement("v_uv");
|
||||
const davinci::fVector2* v_uv = (const davinci::fVector2*)(pixelData + (v_uv_Element->offset / sizeof(float32_t)));
|
||||
|
||||
davinci::fVector4 u_baseColor = davinci::shader::texture2D(*m_pipe, m_material->getBaseColorTexture(), (*v_uv), davinci::fVector4::WHITE);
|
||||
|
||||
color = davinci::fVector4(u_baseColor.xyz(), 1);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include "dvc_config.h"
|
||||
#include "shader/dvc_shader_interface.h"
|
||||
|
||||
class DebugFragementShader_BaseColor : public davinci::PixelShaderInterface
|
||||
{
|
||||
public:
|
||||
virtual const char* getName(void) const;
|
||||
|
||||
virtual void prepare(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material);
|
||||
|
||||
virtual void process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData,
|
||||
davinci::fVector4& color);
|
||||
|
||||
private:
|
||||
const davinci::RenderPipe* m_pipe = nullptr;
|
||||
davinci::MaterialConstPtr m_material;
|
||||
};
|
||||
@@ -0,0 +1,30 @@
|
||||
#include "fs_debug_geometry_normal.h"
|
||||
#include "device/dvc_device.h"
|
||||
#include "device/dvc_uniform_buffer.h"
|
||||
#include "asset/dvc_texture.h"
|
||||
#include "shader/dvc_shader_functions.h"
|
||||
#include "pipe/dvc_render_pipe.h"
|
||||
|
||||
const char* DebugFragementShader_GeometryNormal::getName(void) const
|
||||
{
|
||||
return "debug:geometry_normal";
|
||||
}
|
||||
|
||||
void DebugFragementShader_GeometryNormal::prepare(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material)
|
||||
{
|
||||
m_pipe = pipe;
|
||||
m_material = material;
|
||||
}
|
||||
|
||||
void DebugFragementShader_GeometryNormal::process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData, davinci::fVector4& color)
|
||||
{
|
||||
const davinci::UniformBuffer& uniformBuffer = m_pipe->getUniformBuffer();
|
||||
|
||||
const davinci::VertexDesc::Element* v_normal_Element = veretexDesc.getElement("v_normal");
|
||||
const davinci::fVector3* v_normal = (const davinci::fVector3*)(pixelData + (v_normal_Element->offset / sizeof(float32_t)));
|
||||
|
||||
davinci::fVector3 normalColor = *v_normal;
|
||||
normalColor.normalise();
|
||||
|
||||
color = davinci::fVector4(normalColor, 1);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include "dvc_config.h"
|
||||
#include "shader/dvc_shader_interface.h"
|
||||
|
||||
class DebugFragementShader_GeometryNormal : public davinci::PixelShaderInterface
|
||||
{
|
||||
public:
|
||||
virtual const char* getName(void) const;
|
||||
|
||||
virtual void prepare(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material);
|
||||
|
||||
virtual void process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData,
|
||||
davinci::fVector4& color);
|
||||
|
||||
private:
|
||||
const davinci::RenderPipe* m_pipe = nullptr;
|
||||
davinci::MaterialConstPtr m_material;
|
||||
};
|
||||
@@ -0,0 +1,29 @@
|
||||
#include "fs_debug_normal_texture.h"
|
||||
#include "device/dvc_device.h"
|
||||
#include "device/dvc_uniform_buffer.h"
|
||||
#include "asset/dvc_texture.h"
|
||||
#include "shader/dvc_shader_functions.h"
|
||||
#include "pipe/dvc_render_pipe.h"
|
||||
|
||||
const char* DebugFragementShader_NormalTexture::getName(void) const
|
||||
{
|
||||
return "debug:normal_texture";
|
||||
}
|
||||
|
||||
void DebugFragementShader_NormalTexture::prepare(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material)
|
||||
{
|
||||
m_pipe = pipe;
|
||||
m_material = material;
|
||||
}
|
||||
|
||||
void DebugFragementShader_NormalTexture::process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData, davinci::fVector4& color)
|
||||
{
|
||||
const davinci::UniformBuffer& uniformBuffer = m_pipe->getUniformBuffer();
|
||||
|
||||
const davinci::VertexDesc::Element* v_uv_Element = veretexDesc.getElement("v_uv");
|
||||
const davinci::fVector2* v_uv = (const davinci::fVector2*)(pixelData + (v_uv_Element->offset / sizeof(float32_t)));
|
||||
|
||||
davinci::fVector4 u_baseColor = davinci::shader::texture2D(*m_pipe, m_material->getNormalTexture(), (*v_uv), davinci::fVector4::WHITE);
|
||||
|
||||
color = davinci::fVector4(u_baseColor.xyz(), 1);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include "dvc_config.h"
|
||||
#include "shader/dvc_shader_interface.h"
|
||||
|
||||
class DebugFragementShader_NormalTexture : public davinci::PixelShaderInterface
|
||||
{
|
||||
public:
|
||||
virtual const char* getName(void) const;
|
||||
|
||||
virtual void prepare(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material);
|
||||
|
||||
virtual void process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData,
|
||||
davinci::fVector4& color);
|
||||
|
||||
private:
|
||||
const davinci::RenderPipe* m_pipe = nullptr;
|
||||
davinci::MaterialConstPtr m_material;
|
||||
};
|
||||
@@ -0,0 +1,16 @@
|
||||
#include "fs_debug_solid_color.h"
|
||||
|
||||
const char* DebugFragementShader_SolidColor::getName(void) const
|
||||
{
|
||||
return "debug:solid_color";
|
||||
}
|
||||
|
||||
void DebugFragementShader_SolidColor::prepare(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void DebugFragementShader_SolidColor::process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData, davinci::fVector4& color)
|
||||
{
|
||||
color = davinci::fVector4::RED;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include "dvc_config.h"
|
||||
#include "shader/dvc_shader_interface.h"
|
||||
|
||||
class DebugFragementShader_SolidColor : public davinci::PixelShaderInterface
|
||||
{
|
||||
public:
|
||||
virtual const char* getName(void) const;
|
||||
|
||||
virtual void prepare(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material);
|
||||
|
||||
virtual void process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData,
|
||||
davinci::fVector4& color);
|
||||
};
|
||||
@@ -0,0 +1,31 @@
|
||||
#include "fs_debug_texture_coordinates.h"
|
||||
#include "device/dvc_device.h"
|
||||
#include "device/dvc_uniform_buffer.h"
|
||||
#include "asset/dvc_texture.h"
|
||||
#include "shader/dvc_shader_functions.h"
|
||||
#include "pipe/dvc_render_pipe.h"
|
||||
|
||||
const char* DebugFragementShader_TextureCoordinates::getName(void) const
|
||||
{
|
||||
return "debug:texture_coordinates";
|
||||
}
|
||||
|
||||
void DebugFragementShader_TextureCoordinates::prepare(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material)
|
||||
{
|
||||
m_pipe = pipe;
|
||||
m_material = material;
|
||||
}
|
||||
|
||||
void DebugFragementShader_TextureCoordinates::process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData, davinci::fVector4& color)
|
||||
{
|
||||
const davinci::UniformBuffer& uniformBuffer = m_pipe->getUniformBuffer();
|
||||
|
||||
const davinci::VertexDesc::Element* v_uv_Element = veretexDesc.getElement("v_uv");
|
||||
const davinci::fVector2* v_uv = (const davinci::fVector2*)(pixelData + (v_uv_Element->offset / sizeof(float32_t)));
|
||||
|
||||
davinci::fVector2 uv = *v_uv;
|
||||
float uv_x = uv.x - std::floor(uv.x);
|
||||
float uv_y = uv.y - std::floor(uv.y);
|
||||
|
||||
color = davinci::fVector4(uv_x, uv_y, 0, 1);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include "dvc_config.h"
|
||||
#include "shader/dvc_shader_interface.h"
|
||||
|
||||
class DebugFragementShader_TextureCoordinates : public davinci::PixelShaderInterface
|
||||
{
|
||||
public:
|
||||
virtual const char* getName(void) const;
|
||||
|
||||
virtual void prepare(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material);
|
||||
|
||||
virtual void process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData,
|
||||
davinci::fVector4& color);
|
||||
|
||||
private:
|
||||
const davinci::RenderPipe* m_pipe = nullptr;
|
||||
davinci::MaterialConstPtr m_material;
|
||||
};
|
||||
@@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
|
||||
#include "dvc_config.h"
|
||||
#include "shader/dvc_shader_interface.h"
|
||||
|
||||
|
||||
template<bool WithNormal, bool WithUV>
|
||||
class StandardVertexShader : public davinci::VertexShaderInterface
|
||||
{
|
||||
public:
|
||||
virtual const char* getName(void) const;
|
||||
virtual const davinci::VertexDesc& getOutputVertexDesc() const;
|
||||
virtual size_t getOutputVertexByteSize(void) const;
|
||||
virtual void prepare(const davinci::RenderPipe* pipe, SetInputAttributeFunc setInputAttributeFunc);
|
||||
virtual void process(size_t index, void* outputVertex, davinci::fVector3& vertexWorldPos);
|
||||
|
||||
private:
|
||||
const davinci::RenderPipe* m_pipe = nullptr;
|
||||
davinci::VertexDesc m_outputVertexDesc;
|
||||
|
||||
using InputBufferMembers = decltype(std::tuple_cat(
|
||||
std::tuple<davinci::DeviceBufferAccessorConstPtr>(),
|
||||
std::conditional_t<WithNormal, std::tuple<davinci::DeviceBufferAccessorConstPtr>, std::tuple<>>{},
|
||||
std::conditional_t<WithUV, std::tuple<davinci::DeviceBufferAccessorConstPtr>, std::tuple<>>{}
|
||||
));
|
||||
InputBufferMembers m_inputAccessors;
|
||||
|
||||
static constexpr int32_t iPOSITION() { return 0; };
|
||||
static constexpr int32_t iNORMAL() { return 1; };
|
||||
static constexpr int32_t iUV() { return WithNormal+1; };
|
||||
|
||||
//davinci::DeviceBufferAccessorConstPtr& getPositionAccessor();
|
||||
//davinci::DeviceBufferAccessorConstPtr& getNormalAccessor();
|
||||
//davinci::DeviceBufferAccessorConstPtr& getUVAccessor();
|
||||
|
||||
public:
|
||||
StandardVertexShader();
|
||||
virtual ~StandardVertexShader();
|
||||
};
|
||||
|
||||
#include "vs_standard.inc"
|
||||
@@ -0,0 +1,181 @@
|
||||
#include "device/dvc_buffer_accessor.h"
|
||||
#include "math/dvc_vector2.h"
|
||||
#include "math/dvc_vector3.h"
|
||||
#include "math/dvc_matrix4.h"
|
||||
#include "pipe/dvc_render_pipe.h"
|
||||
#include "device/dvc_uniform_buffer.h"
|
||||
|
||||
template<bool WithNormal, bool WithUV>
|
||||
StandardVertexShader<WithNormal, WithUV>::StandardVertexShader()
|
||||
{
|
||||
m_outputVertexDesc.addElement("v_position", davinci::VET_POSITION, davinci::CT_Float32, davinci::DT_Vec3);
|
||||
|
||||
if constexpr(WithNormal)
|
||||
m_outputVertexDesc.addElement("v_normal", davinci::VET_NORMAL, davinci::CT_Float32, davinci::DT_Vec3);
|
||||
|
||||
if constexpr(WithUV)
|
||||
m_outputVertexDesc.addElement("v_uv", davinci::VET_TEXCOORD_0, davinci::CT_Float32, davinci::DT_Vec2);
|
||||
}
|
||||
|
||||
template<bool WithNormal, bool WithUV>
|
||||
StandardVertexShader<WithNormal, WithUV>::~StandardVertexShader()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
template<std::size_t N>
|
||||
struct CompileTimeString {
|
||||
std::array<char, N> data{};
|
||||
std::size_t size = 0;
|
||||
|
||||
constexpr void append(const char* str, std::size_t len) {
|
||||
for (std::size_t i = 0; i < len; ++i) {
|
||||
data[size++] = str[i];
|
||||
}
|
||||
}
|
||||
|
||||
constexpr void append(const char* str) {
|
||||
while (*str) {
|
||||
data[size++] = *str++;
|
||||
}
|
||||
}
|
||||
|
||||
constexpr const char* c_str() const { return data.data(); }
|
||||
constexpr std::string_view view() const { return {data.data(), size}; }
|
||||
};
|
||||
|
||||
template<bool WithNormal, bool WithUV>
|
||||
constexpr auto buildNameString()
|
||||
{
|
||||
constexpr const char* prefix = "standard";
|
||||
constexpr const char* normalString = WithNormal ? "+normal" : "-normal";
|
||||
constexpr const char* uvString = WithUV ? "+uv" : "-uv";
|
||||
|
||||
constexpr std::size_t total_len = std::char_traits<char>::length(prefix)
|
||||
+ std::char_traits<char>::length(normalString)
|
||||
+ std::char_traits<char>::length(uvString);
|
||||
|
||||
CompileTimeString<total_len> result;
|
||||
result.append(prefix);
|
||||
result.append(normalString);
|
||||
result.append(uvString);
|
||||
result.append(""); // null terminator
|
||||
return result;
|
||||
}
|
||||
|
||||
template<bool WithNormal, bool WithUV>
|
||||
const char* StandardVertexShader<WithNormal, WithUV>::getName(void) const
|
||||
{
|
||||
static constexpr auto strName = buildNameString<WithNormal, WithUV>();
|
||||
const char* p = strName.c_str();
|
||||
return p;
|
||||
}
|
||||
|
||||
template<bool WithNormal, bool WithUV>
|
||||
const davinci::VertexDesc& StandardVertexShader<WithNormal, WithUV>::getOutputVertexDesc() const
|
||||
{
|
||||
return m_outputVertexDesc;
|
||||
}
|
||||
|
||||
template<bool WithNormal, bool WithUV>
|
||||
size_t StandardVertexShader<WithNormal, WithUV>::getOutputVertexByteSize(void) const
|
||||
{
|
||||
return m_outputVertexDesc.vertexByteSize();
|
||||
}
|
||||
|
||||
template<bool WithNormal, bool WithUV>
|
||||
void StandardVertexShader<WithNormal, WithUV>::prepare(const davinci::RenderPipe* pipe, SetInputAttributeFunc setInputAttributeFunc)
|
||||
{
|
||||
m_pipe = pipe;
|
||||
|
||||
std::get<iPOSITION()>(m_inputAccessors) = setInputAttributeFunc(davinci::VET_POSITION);
|
||||
|
||||
if constexpr(WithNormal)
|
||||
std::get<iNORMAL()>(m_inputAccessors) = setInputAttributeFunc(davinci::VET_NORMAL);
|
||||
|
||||
if constexpr(WithUV)
|
||||
std::get<iUV()>(m_inputAccessors) = setInputAttributeFunc(davinci::VET_TEXCOORD_0);
|
||||
|
||||
}
|
||||
/*
|
||||
template<bool WithNormal, bool WithUV>
|
||||
davinci::DeviceBufferAccessorConstPtr& StandardVertexShader<WithNormal, WithUV>::getPositionAccessor()
|
||||
{
|
||||
return std::get<0>(m_inputAccessors);
|
||||
}
|
||||
|
||||
template<bool WithNormal, bool WithUV>
|
||||
davinci::DeviceBufferAccessorConstPtr& StandardVertexShader<WithNormal, WithUV>::getNormalAccessor()
|
||||
{
|
||||
if constexpr(WithNormal)
|
||||
return std::get<1>(m_inputAccessors);
|
||||
else
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
template<bool WithNormal, bool WithUV>
|
||||
davinci::DeviceBufferAccessorConstPtr& StandardVertexShader<WithNormal, WithUV>::getUVAccessor()
|
||||
{
|
||||
if constexpr(WithUV)
|
||||
return std::get<WithNormal+1>(m_inputAccessors);
|
||||
else
|
||||
return nullptr;
|
||||
}
|
||||
*/
|
||||
template<bool WithNormal, bool WithUV>
|
||||
void StandardVertexShader<WithNormal, WithUV>::process(size_t index, void* outputVertex, davinci::fVector3& vertexWorldPos)
|
||||
{
|
||||
static const float DEFAULT_POSITION[] = {0.f, 0.f, 0.f};
|
||||
static const float32_t DEFAULT_NORMAL[] = { 0.f, 0.f, 0.f };
|
||||
static const float32_t DEFAULT_TEXCOORD0[] = { 0.f, 0.f};
|
||||
|
||||
const davinci::UniformBuffer& uniformBuffer = m_pipe->getUniformBuffer();
|
||||
|
||||
davinci::DeviceBufferAccessorConstPtr inputPosition = std::get<0>(m_inputAccessors);
|
||||
|
||||
davinci::DeviceBufferAccessorConstPtr inputNormal = nullptr;
|
||||
if constexpr(WithNormal)
|
||||
inputNormal = std::get<iNORMAL()>(m_inputAccessors);
|
||||
|
||||
davinci::DeviceBufferAccessorConstPtr inputUV = nullptr;
|
||||
if constexpr(WithUV)
|
||||
inputUV = std::get<iUV()>(m_inputAccessors);
|
||||
|
||||
davinci::fVector3 a_position(inputPosition ? (const float32_t*)(inputPosition->getElement(index)) : DEFAULT_POSITION);
|
||||
davinci::fVector3 a_normal(inputNormal ? (const float32_t*)(inputNormal->getElement(index)) : DEFAULT_NORMAL);
|
||||
davinci::fVector2 a_uv(inputUV ? (const float32_t*)(inputUV->getElement(index)) : DEFAULT_TEXCOORD0);
|
||||
|
||||
davinci::fMatrix4 mat_world = davinci::fMatrix4::IDENTITY;
|
||||
if (davinci::fMatrix4* p_mat_world = (davinci::fMatrix4*)(uniformBuffer.getAttribute("self:world_transform")))
|
||||
{
|
||||
mat_world = *p_mat_world;
|
||||
}
|
||||
|
||||
davinci::fMatrix4 mat_camera_view_proj = davinci::fMatrix4::IDENTITY;
|
||||
if (davinci::fMatrix4* p_mat_camera_view_proj = (davinci::fMatrix4*)(uniformBuffer.getAttribute("camera:view_proj_mat")))
|
||||
{
|
||||
mat_camera_view_proj = *p_mat_camera_view_proj;
|
||||
}
|
||||
|
||||
vertexWorldPos = mat_world * a_position;
|
||||
davinci::fVector3 v_position = mat_camera_view_proj * vertexWorldPos;
|
||||
|
||||
int32_t outputOffset=0;
|
||||
memcpy((char*)outputVertex + m_outputVertexDesc.getElement(outputOffset++)->offset, &v_position, sizeof(davinci::fVector3));
|
||||
|
||||
if constexpr(WithNormal)
|
||||
{
|
||||
davinci::fMatrix4 matNormal = davinci::fMatrix4::IDENTITY;
|
||||
if (davinci::fMatrix4* p_mat_normal = (davinci::fMatrix4*)(uniformBuffer.getAttribute("self:world_transform_inverse_trans")))
|
||||
{
|
||||
matNormal = *p_mat_normal;
|
||||
}
|
||||
davinci::fVector3 v_normal = (matNormal * davinci::fVector4(a_normal, 0)).xyz().normalise();
|
||||
memcpy((char*)outputVertex + m_outputVertexDesc.getElement(outputOffset++)->offset, &v_normal, sizeof(davinci::fVector3));
|
||||
}
|
||||
|
||||
if constexpr(WithUV)
|
||||
{
|
||||
memcpy((char*)outputVertex + m_outputVertexDesc.getElement(outputOffset++)->offset, &a_uv, sizeof(float32_t) * 2);
|
||||
}
|
||||
}
|
||||
@@ -43,6 +43,19 @@ set(DV_UTILS_CAMERA_SOURCE_FILES
|
||||
)
|
||||
source_group("source/camera" FILES ${DV_UTILS_CAMERA_SOURCE_FILES})
|
||||
|
||||
#################
|
||||
# standard asserts
|
||||
#################
|
||||
set(DV_UTILS_STANDARD_INCLUDE_FILES
|
||||
include/standard/dvu_standard_material.h
|
||||
)
|
||||
source_group("include/standard" FILES ${DV_UTILS_STANDARD_INCLUDE_FILES})
|
||||
|
||||
set(DV_UTILS_STANDARD_SOURCE_FILES
|
||||
source/standard/dvu_standard_material.cpp
|
||||
)
|
||||
source_group("source/standard" FILES ${DV_UTILS_STANDARD_SOURCE_FILES})
|
||||
|
||||
include_directories(
|
||||
${DV_AUTO_INCLUDE_PATH}
|
||||
${DV_CORE_INCLUDE_PATH}
|
||||
@@ -58,6 +71,8 @@ add_library(dv_utils SHARED
|
||||
${DV_UTILS_BUFFER_SOURCE_FILES}
|
||||
${DV_UTILS_CAMERA_INCLUDE_FILES}
|
||||
${DV_UTILS_CAMERA_SOURCE_FILES}
|
||||
${DV_UTILS_STANDARD_INCLUDE_FILES}
|
||||
${DV_UTILS_STANDARD_SOURCE_FILES}
|
||||
)
|
||||
|
||||
target_link_libraries(dv_utils
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include "dvu_config.h"
|
||||
#include "dvc_config.h"
|
||||
#include "dvc_pre_declare.h"
|
||||
|
||||
DV_UTILS_BEGIN_NAMESPACE
|
||||
|
||||
davinci::MaterialPtr DV_UTILS_API create_standard_material(davinci::Device* device, const char* name);
|
||||
|
||||
DV_UTILS_END_NAMESPACE
|
||||
@@ -20,6 +20,8 @@
|
||||
|
||||
#include "buffer/dvu_dump_buffer.h"
|
||||
|
||||
#include "standard/dvu_standard_material.h"
|
||||
|
||||
#include <tiny_gltf.h>
|
||||
|
||||
#include <string>
|
||||
@@ -614,6 +616,49 @@ davinci::fVector4 _getVector4FromDoubleArray(const std::vector<double>& doubleAr
|
||||
return davinci::fVector4((float32_t)doubleArray[0], (float32_t)doubleArray[1], (float32_t)doubleArray[2], (float32_t)doubleArray[3]);
|
||||
}
|
||||
|
||||
davinci::fMatrix3 _getTextureTransform(const tinygltf::ExtensionMap& gltfTextureInfoExternsionMap)
|
||||
{
|
||||
davinci::fMatrix3 textureTransform = davinci::fMatrix3::IDENTITY;
|
||||
|
||||
auto transformExternsions = gltfTextureInfoExternsionMap.find("KHR_texture_transform");
|
||||
if (transformExternsions != gltfTextureInfoExternsionMap.end())
|
||||
{
|
||||
davinci::fVector2 scale = davinci::fVector2::ONE;
|
||||
const tinygltf::Value& scaleArray = transformExternsions->second.Get("scale");
|
||||
if (scaleArray.IsArray() && scaleArray.ArrayLen()>=2 &&
|
||||
scaleArray.Get(0).IsNumber() && scaleArray.Get(1).IsNumber())
|
||||
{
|
||||
scale = davinci::fVector2(
|
||||
(float32_t)(scaleArray.Get(0).GetNumberAsDouble()),
|
||||
(float32_t)(scaleArray.Get(1).GetNumberAsDouble())
|
||||
);
|
||||
}
|
||||
|
||||
float32_t rotate = 0.f;
|
||||
const tinygltf::Value& rotationValue = transformExternsions->second.Get("rotation");
|
||||
if (rotationValue.IsNumber())
|
||||
{
|
||||
rotate = (float32_t)rotationValue.GetNumberAsDouble();
|
||||
}
|
||||
|
||||
davinci::fVector2 offset = davinci::fVector2::ZERO;
|
||||
const tinygltf::Value& offsetArray = transformExternsions->second.Get("offset");
|
||||
if (offsetArray.IsArray() && offsetArray.ArrayLen()>=2 &&
|
||||
offsetArray.Get(0).IsNumber() && offsetArray.Get(1).IsNumber())
|
||||
{
|
||||
offset = davinci::fVector2(
|
||||
(float32_t)(offsetArray.Get(0).GetNumberAsDouble()),
|
||||
(float32_t)(offsetArray.Get(1).GetNumberAsDouble())
|
||||
);
|
||||
}
|
||||
|
||||
textureTransform = davinci::fMatrix3::makeTrans(offset) *
|
||||
davinci::fMatrix3::makeRotate(rotate) * davinci::fMatrix3::makeScale(scale);
|
||||
}
|
||||
|
||||
return textureTransform;
|
||||
}
|
||||
|
||||
bool _loadMaterial(tinygltf::Model& gltfModel, davinci::Device& device, davinci::Scene& scene)
|
||||
{
|
||||
for (size_t i = 0; i < gltfModel.materials.size(); i++)
|
||||
@@ -621,26 +666,36 @@ bool _loadMaterial(tinygltf::Model& gltfModel, davinci::Device& device, davinci:
|
||||
tinygltf::Material& gltfMaterial = gltfModel.materials[i];
|
||||
|
||||
//create new material
|
||||
davinci::MaterialPtr materialPtr = device.newMaterialAsset();
|
||||
davinci::MaterialPtr materialPtr = create_standard_material(&device, "debug:geometry_normal");
|
||||
|
||||
//apply matellic roughness info
|
||||
const tinygltf::PbrMetallicRoughness& pbrMetllicRoughness = gltfMaterial.pbrMetallicRoughness;
|
||||
const tinygltf::PbrMetallicRoughness& gltfPBRMetllicRoughness = gltfMaterial.pbrMetallicRoughness;
|
||||
|
||||
//base color texture
|
||||
const tinygltf::TextureInfo& gltfBaseColorTextureInfo = gltfPBRMetllicRoughness.baseColorTexture;
|
||||
materialPtr->setBaseColorTexture(
|
||||
_getTexturePtr(gltfModel, device, pbrMetllicRoughness.baseColorTexture.index),
|
||||
pbrMetllicRoughness.baseColorTexture.texCoord,
|
||||
_getVector4FromDoubleArray(pbrMetllicRoughness.baseColorFactor)
|
||||
);
|
||||
materialPtr->setMetallicRoughnessTexture(
|
||||
_getTexturePtr(gltfModel, device, pbrMetllicRoughness.metallicRoughnessTexture.index),
|
||||
pbrMetllicRoughness.metallicRoughnessTexture.texCoord,
|
||||
pbrMetllicRoughness.metallicFactor,
|
||||
pbrMetllicRoughness.roughnessFactor
|
||||
_getTexturePtr(gltfModel, device, gltfBaseColorTextureInfo.index),
|
||||
gltfBaseColorTextureInfo.texCoord,
|
||||
_getTextureTransform(gltfBaseColorTextureInfo.extensions),
|
||||
_getVector4FromDoubleArray(gltfPBRMetllicRoughness.baseColorFactor)
|
||||
);
|
||||
|
||||
//set normal texture info
|
||||
//matallic & roughness texture
|
||||
const tinygltf::TextureInfo& gltfMetalicRoughnessTextureInfo = gltfPBRMetllicRoughness.metallicRoughnessTexture;
|
||||
materialPtr->setMetallicRoughnessTexture(
|
||||
_getTexturePtr(gltfModel, device, gltfMetalicRoughnessTextureInfo.index),
|
||||
gltfMetalicRoughnessTextureInfo.texCoord,
|
||||
_getTextureTransform(gltfMetalicRoughnessTextureInfo.extensions),
|
||||
gltfPBRMetllicRoughness.metallicFactor,
|
||||
gltfPBRMetllicRoughness.roughnessFactor
|
||||
);
|
||||
|
||||
//normal texture
|
||||
const tinygltf::NormalTextureInfo& gltfNormalTextureInfo = gltfMaterial.normalTexture;
|
||||
materialPtr->setNormalTexture(
|
||||
_getTexturePtr(gltfModel, device, gltfMaterial.normalTexture.index),
|
||||
gltfMaterial.normalTexture.texCoord,
|
||||
_getTexturePtr(gltfModel, device, gltfNormalTextureInfo.index),
|
||||
gltfNormalTextureInfo.texCoord,
|
||||
_getTextureTransform(gltfNormalTextureInfo.extensions),
|
||||
gltfMaterial.normalTexture.scale
|
||||
);
|
||||
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
#include "standard/dvu_standard_material.h"
|
||||
#include "device/dvc_device.h"
|
||||
#include "asset/dvc_material.h"
|
||||
|
||||
DV_UTILS_BEGIN_NAMESPACE
|
||||
|
||||
davinci::MaterialPtr create_standard_material(davinci::Device* device, const char* name)
|
||||
{
|
||||
if (strcmp(name, "debug:base_color")==0)
|
||||
{
|
||||
davinci::MaterialPtr materialPtr = device->newMaterialAsset();
|
||||
materialPtr->setVertexShader("dv_standard_shaders.dll", "standard-normal+uv");
|
||||
materialPtr->setFragementShader("dv_standard_shaders.dll", "debug:base_color");
|
||||
return materialPtr;
|
||||
}
|
||||
else if (strcmp(name, "debug:normal_texture")==0)
|
||||
{
|
||||
davinci::MaterialPtr materialPtr = device->newMaterialAsset();
|
||||
materialPtr->setVertexShader("dv_standard_shaders.dll", "standard-normal+uv");
|
||||
materialPtr->setFragementShader("dv_standard_shaders.dll", "debug:normal_texture");
|
||||
return materialPtr;
|
||||
}
|
||||
else if (strcmp(name, "debug:texture_coordinates")==0)
|
||||
{
|
||||
davinci::MaterialPtr materialPtr = device->newMaterialAsset();
|
||||
materialPtr->setVertexShader("dv_standard_shaders.dll", "standard-normal+uv");
|
||||
materialPtr->setFragementShader("dv_standard_shaders.dll", "debug:texture_coordinates");
|
||||
return materialPtr;
|
||||
}
|
||||
else if (strcmp(name, "debug:geometry_normal")==0)
|
||||
{
|
||||
davinci::MaterialPtr materialPtr = device->newMaterialAsset();
|
||||
materialPtr->setVertexShader("dv_standard_shaders.dll", "standard+normal-uv");
|
||||
materialPtr->setFragementShader("dv_standard_shaders.dll", "debug:geometry_normal");
|
||||
return materialPtr;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
DV_UTILS_END_NAMESPACE
|
||||
@@ -9,12 +9,7 @@ class TestShaderInterface : public davinci::ShaderFileInterface
|
||||
public:
|
||||
virtual davinci::VertexShaderInterface* getVertexShader(const char* szName)
|
||||
{
|
||||
if (strcmp(szName, "standard") == 0)
|
||||
{
|
||||
static StandardVertexShader standardVertexShader;
|
||||
return &standardVertexShader;
|
||||
}
|
||||
else if (strcmp(szName, "test") == 0)
|
||||
if (strcmp(szName, "test") == 0)
|
||||
{
|
||||
static TestVertexShader testVertexShader;
|
||||
return &testVertexShader;
|
||||
@@ -23,13 +18,8 @@ public:
|
||||
}
|
||||
virtual davinci::PixelShaderInterface* getPixelShader(const char* szName)
|
||||
{
|
||||
static SolidColorPixelShader solidColorPixelShader;
|
||||
static TestPixelShader testPixelShader;
|
||||
if(strcmp(szName, solidColorPixelShader.getName()) == 0)
|
||||
{
|
||||
return &solidColorPixelShader;
|
||||
}
|
||||
else if (strcmp(szName, testPixelShader.getName()) == 0)
|
||||
if (strcmp(szName, testPixelShader.getName()) == 0)
|
||||
{
|
||||
return &testPixelShader;
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ const char* TestPixelShader::getName(void) const
|
||||
return "test";
|
||||
}
|
||||
|
||||
void TestPixelShader::prepare(const RenderPipe* pipe, SetInputAttributeFunc setInputAttributeFunc)
|
||||
void TestPixelShader::prepare(const RenderPipe* pipe, davinci::MaterialConstPtr material)
|
||||
{
|
||||
m_pipe = pipe;
|
||||
}
|
||||
@@ -68,76 +68,41 @@ void TestPixelShader::process(const VertexDesc& veretexDesc, const float32_t* pi
|
||||
|
||||
fVector3 u_lightColor = fVector3(1.0, 1.0, 1.0);
|
||||
|
||||
fVector4 u_textureColor = shader::texture2D(*m_pipe, "material:pbr:base_color_texture", *v_uv, fVector4::WHITE);
|
||||
fVector4 u_textureColor;// = shader::texture2D(*m_pipe, "material:pbr:base_color_texture", *v_uv, fVector4::WHITE);
|
||||
|
||||
const fVector4 defaultNormal = fVector4(0.5f, 0.5f, 1.0f, 1.0f);
|
||||
fVector4 u_normalColor = shader::texture2D(*m_pipe, "material:pbr:normal_texture", *v_uv, defaultNormal);
|
||||
fVector4 u_normalColor;// = shader::texture2D(*m_pipe, "material:pbr:normal_texture", *v_uv, defaultNormal);
|
||||
|
||||
//----------------
|
||||
fVector3 vN = fVector3::UNIT_Z;// u_normalColor.xyz() * 2.0f - fVector3::ONE;
|
||||
fVector3 vN = u_normalColor.xyz() * 2.0f - fVector3::ONE;
|
||||
vN.normalise();
|
||||
|
||||
//vector vL = normalize(v_toLight);
|
||||
fVector3 vL =vLight;
|
||||
vL.normalise();
|
||||
|
||||
//vector vV = normalize(v_toCamera);
|
||||
fVector3 vV = *v_toCamera;
|
||||
vV.normalise();
|
||||
|
||||
float ndotl = vL.dotProduct(vN);
|
||||
|
||||
//vector vR = 2 * dot(vL, vN)*vN - vL;
|
||||
fVector3 vR = 2 * ndotl * vN - vL;
|
||||
|
||||
float rdotv = vR.dotProduct(vV);
|
||||
|
||||
//vector vH = normalize(vL + vV);
|
||||
//fVector3 vH = vL + vV;
|
||||
//vH.normalise();
|
||||
|
||||
|
||||
//color colAmbient = u_ambientColor*u_matColor;
|
||||
fVector3 colAmbient = u_ambientColor * u_matColor;
|
||||
|
||||
//float ndotl = max(dot(vN, vL), 0);
|
||||
//float32_t ndotl = std::max(vN.dotProduct(vL), 0.0f);
|
||||
//color colDiff = ndotl * u_matColor * u_lightColor;
|
||||
fVector3 colDiff = shader::max(0.0f, ndotl) * u_matColor* u_lightColor;
|
||||
|
||||
|
||||
//float32_t spec = std::max(vR.dotProduct(vV), 0.0f)*1.0;
|
||||
//color colSpec = step(0, ndotl) * pow(spec, 64) * u_matColor * u_lightColor;
|
||||
|
||||
const float _m = 1.0f;
|
||||
float spec = shader::step(0.0f, ndotl) * shader::pow(shader::max(0.0f, rdotv), 32.f);
|
||||
fVector3 colSpec = spec * u_matColor* u_lightColor;
|
||||
|
||||
//color final = colAmbient + colDiff + colSpec;
|
||||
fVector3 final = fVector3::BLACK;// (colAmbient + colDiff + colSpec)* u_textureColor.xyz();
|
||||
|
||||
final += colAmbient;
|
||||
fVector3 final = fVector3::BLACK;
|
||||
//final += colAmbient;
|
||||
final += colDiff;
|
||||
final += colSpec;
|
||||
|
||||
final *= u_textureColor.xyz();
|
||||
//fVector3 final = (colAmbient +colDiff +colSpec)*u_textureColor.xyz();
|
||||
//final += colSpec;
|
||||
//final *= u_textureColor.xyz();
|
||||
|
||||
//final = u_normalColor.xyz();
|
||||
//color = fVector4::RED;
|
||||
color = fVector4(final.saturate(), 1.0f);// fVector4::RED;
|
||||
}
|
||||
|
||||
const char* SolidColorPixelShader::getName(void) const
|
||||
{
|
||||
return "solid";
|
||||
}
|
||||
|
||||
void SolidColorPixelShader::prepare(const RenderPipe* pipe, SetInputAttributeFunc setInputAttributeFunc)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void SolidColorPixelShader::process(const VertexDesc& veretexDesc, const float32_t* pixelData, fVector4& color)
|
||||
{
|
||||
color = fVector4::RED;
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ class TestPixelShader : public davinci::PixelShaderInterface
|
||||
public:
|
||||
virtual const char* getName(void) const;
|
||||
|
||||
virtual void prepare(const davinci::RenderPipe* pipe, SetInputAttributeFunc setInputAttributeFunc);
|
||||
virtual void prepare(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material);
|
||||
|
||||
virtual void process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData,
|
||||
davinci::fVector4& color);
|
||||
@@ -16,13 +16,3 @@ private:
|
||||
const davinci::RenderPipe* m_pipe = nullptr;
|
||||
};
|
||||
|
||||
class SolidColorPixelShader : public davinci::PixelShaderInterface
|
||||
{
|
||||
public:
|
||||
virtual const char* getName(void) const;
|
||||
|
||||
virtual void prepare(const davinci::RenderPipe* pipe, SetInputAttributeFunc setInputAttributeFunc);
|
||||
|
||||
virtual void process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData,
|
||||
davinci::fVector4& color);
|
||||
};
|
||||
|
||||
@@ -63,7 +63,7 @@ void TestVertexShader::process(size_t index, void* outputVertex, davinci::fVecto
|
||||
static const float32_t DEFAULT_POSITION[] = {0.f, 0.f, 0.f};
|
||||
static const float32_t DEFAULT_NORMAL[] = { 0.f, 0.f, 0.f };
|
||||
static const float32_t DEFAULT_TEXCOORD0[] = { 0.f, 0.f};
|
||||
static const float32_t DEFAULT_TANGENT[] = { 1.f, 0.f, 0.f };
|
||||
static const float32_t DEFAULT_TANGENT[] = { 1.f, 0.f, 0.f, 1.f };
|
||||
|
||||
const davinci::UniformBuffer& uniformBuffer = m_pipe->getUniformBuffer();
|
||||
|
||||
@@ -73,7 +73,7 @@ void TestVertexShader::process(size_t index, void* outputVertex, davinci::fVecto
|
||||
|
||||
davinci::fVector2 a_uv(m_TEXCOORD_0 ? (const float32_t*)(m_TEXCOORD_0->getElement(index)) : DEFAULT_TEXCOORD0);
|
||||
|
||||
davinci::fVector3 a_tangent(m_TANGENT ? (const float32_t*)(m_TANGENT->getElement(index)) : DEFAULT_TANGENT);
|
||||
davinci::fVector4 a_tangent(m_TANGENT ? (const float32_t*)(m_TANGENT->getElement(index)) : DEFAULT_TANGENT);
|
||||
|
||||
davinci::fMatrix4 mat_world = davinci::fMatrix4::IDENTITY;
|
||||
if (davinci::fMatrix4* p_mat_world = (davinci::fMatrix4*)(uniformBuffer.getAttribute("self:world_transform")))
|
||||
@@ -108,8 +108,8 @@ void TestVertexShader::process(size_t index, void* outputVertex, davinci::fVecto
|
||||
matNormal = *p_mat_normal;
|
||||
}
|
||||
davinci::fVector3 v_normal = (matNormal * davinci::fVector4(a_normal, 0)).xyz().normalise();
|
||||
davinci::fVector3 v_tangent = (matNormal * davinci::fVector4(a_tangent, 0)).xyz().normalise();
|
||||
davinci::fVector3 v_binormal = v_normal.crossProduct(v_tangent).normalise();
|
||||
davinci::fVector3 v_tangent = (matNormal * davinci::fVector4(a_tangent.xyz(), 0)).xyz().normalise();
|
||||
davinci::fVector3 v_binormal = v_normal.crossProduct(v_tangent).normalise();// *a_tangent.w;
|
||||
|
||||
davinci::fMatrix3 tbnMatrix = davinci::fMatrix3(
|
||||
v_tangent.x, v_binormal.x, v_normal.x,
|
||||
|
||||
Reference in New Issue
Block a user