From e515e411fd452474254659ae5a3397a1af6ebd6a Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Fri, 4 May 2018 23:01:45 +0200 Subject: [PATCH] [DEV] better file of material --- data/material3D.frag | 73 ++++++++++++++++++++++++++-------------- ege/Material.cpp | 41 ++++++++++++++++++++++ ege/Material.hpp | 47 ++++++-------------------- ege/resource/MeshEmf.cpp | 5 ++- 4 files changed, 102 insertions(+), 64 deletions(-) diff --git a/data/material3D.frag b/data/material3D.frag index 8d33306..b49fdb2 100644 --- a/data/material3D.frag +++ b/data/material3D.frag @@ -12,7 +12,7 @@ struct DirectionalLight { struct Material { vec4 ambientFactor; // color of the material with a reflection factor (0.2 for example) ==> light the back of the object - vec4 diffuseFactor; // Direct exposition of the object by the light + vec4 diffuseFactor; // Direct exposition of the object by the light (object color) vec4 specularFactor; // Shining the Object with the light float shininess; }; @@ -24,29 +24,50 @@ uniform Material EW_material; varying vec3 v_ecNormal; -void main(void) { - // Normalize v_ecNormal - vec3 ecNormal = normalize(v_ecNormal); - - // Calculate ambient light - vec4 ambientLight = EW_directionalLight.ambientColor * EW_material.ambientFactor; - - float ecNormalDotLightDirection = max(0.0, dot(ecNormal, EW_directionalLight.direction)); - //float ecNormalDotLightDirection = (dot(ecNormal, EW_directionalLight.direction)+1.0) * 0.5; - - // Calculate diffuse light - vec4 diffuseLight = ecNormalDotLightDirection * EW_directionalLight.diffuseColor * EW_material.diffuseFactor; - //vec4 diffuseLight = vec4(0.0, 0.0, 0.0, 0.0); - - // Calculate specular light - vec4 specularLight = EW_directionalLight.specularColor * EW_material.specularFactor; - - float ecNormalDotLightHalfplane = dot(ecNormal, EW_directionalLight.halfplane); - if (ecNormalDotLightHalfplane > 0.0) { - specularLight = pow(ecNormalDotLightHalfplane, EW_material.shininess) * EW_directionalLight.specularColor * EW_material.specularFactor; - specularLight = EW_directionalLight.specularColor * EW_material.specularFactor; +#if 1 + // real basic version + void main(void) { + // Normalize v_ecNormal + vec3 ecNormal = normalize(v_ecNormal); + + float ecNormalDotLightDirection = (dot(ecNormal, EW_directionalLight.direction)+1.0) * 0.5; + + // Calculate diffuse light + vec4 diffuseLight = ecNormalDotLightDirection * EW_directionalLight.diffuseColor * EW_material.diffuseFactor; + + vec4 light = diffuseLight; + gl_FragColor = light; } - - vec4 light = ambientLight + diffuseLight + specularLight; - gl_FragColor = light; -} +#else + // must work but it is full white + void main(void) { + // Normalize v_ecNormal + vec3 ecNormal = normalize(v_ecNormal); + + // Calculate ambient light + vec4 ambientLight = EW_directionalLight.ambientColor * EW_material.ambientFactor; + + float ecNormalDotLightDirection = max(0.0, dot(ecNormal, EW_directionalLight.direction)); + //float ecNormalDotLightDirection = (dot(ecNormal, EW_directionalLight.direction)+1.0) * 0.5; + + // Calculate diffuse light + vec4 diffuseLight = ecNormalDotLightDirection * EW_directionalLight.diffuseColor * EW_material.diffuseFactor; + //vec4 diffuseLight = EW_material.diffuseFactor; + //vec4 diffuseLight = vec4(0.0, 0.0, 0.0, 0.0); + + // Calculate specular light + vec4 specularLight = EW_directionalLight.specularColor * EW_material.specularFactor; + + float ecNormalDotLightHalfplane = dot(ecNormal, EW_directionalLight.halfplane); + if (ecNormalDotLightHalfplane > 0.0) { + specularLight = pow(ecNormalDotLightHalfplane, EW_material.shininess) * EW_directionalLight.specularColor * EW_material.specularFactor; + specularLight = EW_directionalLight.specularColor * EW_material.specularFactor; + } + + // here, we have a white color + vec4 light = ambientLight + diffuseLight + specularLight; + // here we have color + //vec4 light = diffuseLight; + gl_FragColor = light; + } +#endif diff --git a/ege/Material.cpp b/ege/Material.cpp index dc2313c..7d0545a 100644 --- a/ege/Material.cpp +++ b/ege/Material.cpp @@ -66,6 +66,23 @@ void ege::Material::draw(ememory::SharedPtr _prog, cons EGE_VERBOSE("draw Material: ( end )"); } +void ege::Material::setAmbientFactor(const vec4& _val) { + m_ambientFactor = _val; +} + +void ege::Material::setDiffuseFactor(const vec4& _val) { + //EGE_ERROR("**************** set difuse factor:" << _val); + m_diffuseFactor = _val; +} + +void ege::Material::setSpecularFactor(const vec4& _val) { + m_specularFactor = _val; +} + +void ege::Material::setShininess(float _val) { + m_shininess = _val; +} + void ege::Material::setTexture0(const etk::String& _filename) { ivec2 tmpSize(256, 256); if (_filename != "") { @@ -104,6 +121,9 @@ void ege::Material::setTexture0Magic(const ivec2& _size) { enum gale::openGL::renderMode ege::Material::getRenderModeOpenGl() { return m_renderMode; } +enum gale::openGL::renderMode ege::Material::getRenderMode() { + return m_renderMode; +} void ege::Material::setRenderMode(enum gale::openGL::renderMode _val) { switch (_val) { @@ -145,3 +165,24 @@ void ege::Material::setRenderMode(enum gale::openGL::renderMode _val) { m_renderMode = _val; } +void ege::Material::setImageSize(const ivec2& _newSize) { + if (m_texture0 == nullptr){ + return; + } + m_texture0->setImageSize(_newSize); +} + +egami::Image* ege::Material::get() { + if (m_texture0 == nullptr){ + return nullptr; + } + return &m_texture0->get(); +} + +void ege::Material::flush() { + if (m_texture0 == nullptr){ + return; + } + m_texture0->flush(); +} + diff --git a/ege/Material.hpp b/ege/Material.hpp index 675d3d5..d43de11 100644 --- a/ege/Material.hpp +++ b/ege/Material.hpp @@ -10,6 +10,7 @@ #include #include #include +#include namespace ege { /** @@ -26,8 +27,6 @@ namespace ege { MaterialGlId(); void link(ememory::SharedPtr _prog, const etk::String& _baseName); }; - - class Material { private: // values @@ -35,7 +34,7 @@ namespace ege { vec4 m_diffuseFactor; vec4 m_specularFactor; float m_shininess; - enum gale::openGL::renderMode m_renderMode; // Select Render mode (triangle/Line/point ...) + enum gale::openGL::renderMode m_renderMode; //!< Select Render mode (triangle/Line/point ...) ememory::SharedPtr m_texture0; public: etk::Vector m_listIndexFaces; @@ -43,46 +42,20 @@ namespace ege { Material(); ~Material(); void draw(ememory::SharedPtr _prog, const ege::MaterialGlId& _glID); - void setAmbientFactor(const vec4& _val) { - m_ambientFactor = _val; - } - void setDiffuseFactor(const vec4& _val) { - m_diffuseFactor = _val; - } - void setSpecularFactor(const vec4& _val) { - m_specularFactor = _val; - } - void setShininess(float _val) { - m_shininess = _val; - } + void setAmbientFactor(const vec4& _val); + void setDiffuseFactor(const vec4& _val); + void setSpecularFactor(const vec4& _val); + void setShininess(float _val); void setRenderMode(enum gale::openGL::renderMode _val); enum gale::openGL::renderMode getRenderModeOpenGl(); - enum gale::openGL::renderMode getRenderMode() { - return m_renderMode; - } + enum gale::openGL::renderMode getRenderMode(); void setTexture0(const etk::String& _filename); void setTexture0Magic(const ivec2& _size); - - void setImageSize(const ivec2& _newSize) { - if (m_texture0 == nullptr){ - return; - } - m_texture0->setImageSize(_newSize); - }; + void setImageSize(const ivec2& _newSize); // get the reference on this image to draw nomething on it ... - egami::Image* get() { - if (m_texture0 == nullptr){ - return nullptr; - } - return &m_texture0->get(); - }; + egami::Image* get(); // flush the data to send it at the openGl system - void flush() { - if (m_texture0 == nullptr){ - return; - } - m_texture0->flush(); - }; + void flush(); }; } diff --git a/ege/resource/MeshEmf.cpp b/ege/resource/MeshEmf.cpp index 4fcc508..21977f7 100644 --- a/ege/resource/MeshEmf.cpp +++ b/ege/resource/MeshEmf.cpp @@ -485,10 +485,13 @@ bool ege::resource::Mesh::loadEMF(const etk::String& _fileName) { float tmpVal1=0; float tmpVal2=0; float tmpVal3=0; + EGE_ERROR("************************** DiffuseFactor '" << inputDataLine << "'"); + EGE_ERROR("************************** DiffuseFactor '" << &inputDataLine[3] << "'"); + sscanf(&inputDataLine[3], "%f %f %f", &tmpVal1, &tmpVal2, &tmpVal3); vec4 tmp(tmpVal1, tmpVal2, tmpVal3, 1); material->setDiffuseFactor(tmp); - EGE_VERBOSE(" DiffuseFactor " << tmp); + EGE_ERROR(" DiffuseFactor " << tmp); } else if(strncmp(inputDataLine,"Ks ",3) == 0) { float tmpVal1=0; float tmpVal2=0;