From 549167d966274404c5cd13c0d5ac12765df1c620 Mon Sep 17 00:00:00 2001 From: boustila Date: Fri, 14 Feb 2014 17:26:32 +0100 Subject: [PATCH] update VRJ --- include/Algo/Import/importObjTex.h | 2 + include/Utils/Shaders/shaderCustom.frag | 8 + include/Utils/Shaders/shaderCustom.geom | 35 ++++ include/Utils/Shaders/shaderCustom.h | 54 +++++ include/Utils/Shaders/shaderCustom.vert | 8 + include/Utils/Shaders/shaderCustomTex.frag | 13 ++ include/Utils/Shaders/shaderCustomTex.geom | 24 +++ include/Utils/Shaders/shaderCustomTex.h | 68 ++++++ include/Utils/Shaders/shaderCustomTex.vert | 16 ++ include/Utils/Shaders/shaderMatCustom.frag | 43 ++++ include/Utils/Shaders/shaderMatCustom.geom | 35 ++++ include/Utils/Shaders/shaderMatCustom.h | 95 +++++++++ include/Utils/Shaders/shaderMatCustom.vert | 37 ++++ include/Utils/gl_matrices.h | 9 +- src/Utils/Shaders/shaderCustom.cpp | 137 +++++++++++++ src/Utils/Shaders/shaderCustomTex.cpp | 167 +++++++++++++++ src/Utils/Shaders/shaderMatCustom.cpp | 228 +++++++++++++++++++++ 17 files changed, 975 insertions(+), 4 deletions(-) create mode 100644 include/Utils/Shaders/shaderCustom.frag create mode 100644 include/Utils/Shaders/shaderCustom.geom create mode 100644 include/Utils/Shaders/shaderCustom.h create mode 100644 include/Utils/Shaders/shaderCustom.vert create mode 100644 include/Utils/Shaders/shaderCustomTex.frag create mode 100644 include/Utils/Shaders/shaderCustomTex.geom create mode 100644 include/Utils/Shaders/shaderCustomTex.h create mode 100644 include/Utils/Shaders/shaderCustomTex.vert create mode 100644 include/Utils/Shaders/shaderMatCustom.frag create mode 100644 include/Utils/Shaders/shaderMatCustom.geom create mode 100644 include/Utils/Shaders/shaderMatCustom.h create mode 100644 include/Utils/Shaders/shaderMatCustom.vert create mode 100644 src/Utils/Shaders/shaderCustom.cpp create mode 100644 src/Utils/Shaders/shaderCustomTex.cpp create mode 100644 src/Utils/Shaders/shaderMatCustom.cpp diff --git a/include/Algo/Import/importObjTex.h b/include/Algo/Import/importObjTex.h index 285bec13..60114dd4 100644 --- a/include/Algo/Import/importObjTex.h +++ b/include/Algo/Import/importObjTex.h @@ -294,6 +294,8 @@ public: const Geom::BoundingBox& getGroupBB(unsigned int i) const { return m_groupBBs[i];} + Geom::BoundingBox& getGroupBB(unsigned int i) { return m_groupBBs[i];} + const std::string& objGroupName(unsigned int i) const { return m_groupNames[i];} /** diff --git a/include/Utils/Shaders/shaderCustom.frag b/include/Utils/Shaders/shaderCustom.frag new file mode 100644 index 00000000..f0c1e424 --- /dev/null +++ b/include/Utils/Shaders/shaderCustom.frag @@ -0,0 +1,8 @@ +// ShaderCustom::fragmentShaderText +VARYING_FRAG vec4 ColorFS; +VARYING_FRAG vec3 N; +void main() +{ + gl_FragData[0] = ColorFS; + gl_FragData[1] = vec4( 0.5*normalize(N)+vec3(0.5), 1.0 ); +} diff --git a/include/Utils/Shaders/shaderCustom.geom b/include/Utils/Shaders/shaderCustom.geom new file mode 100644 index 00000000..5ee21083 --- /dev/null +++ b/include/Utils/Shaders/shaderCustom.geom @@ -0,0 +1,35 @@ +// ShaderCustom::geometryShaderText + +uniform float explode; +uniform mat4 ModelViewProjectionMatrix; +uniform mat4 NormalMatrix; +uniform mat4 ModelViewMatrix; +uniform vec3 lightPosition; +uniform vec4 diffuse; +uniform vec4 ambient; +VARYING_OUT vec4 ColorFS; +VARYING_OUT vec3 N; +VARYING_OUT vec2 texCoord; +void main(void) +{ + vec3 v1 = POSITION_IN(1).xyz - POSITION_IN(0).xyz; + vec3 v2 = POSITION_IN(2).xyz - POSITION_IN(0).xyz; + N = cross(v1,v2); + N = normalize (vec3(NormalMatrix*vec4(N,0.0))); + vec3 center = POSITION_IN(0).xyz + POSITION_IN(1).xyz + POSITION_IN(2).xyz; + center /= 3.0; + vec4 newPos = ModelViewMatrix * vec4(center,0.0); + vec3 L = normalize (lightPosition - newPos.xyz); + float lambertTerm = dot(N,L); + ColorFS = ambient; + if(lambertTerm > 0.0) + ColorFS += diffuse * lambertTerm; + int i; + for(i=0; i< NBVERTS_IN; i++) + { + vec4 pos = explode * POSITION_IN(i) + (1.0-explode)* vec4(center,1.0); + gl_Position = ModelViewProjectionMatrix * pos; + EmitVertex(); + } + EndPrimitive(); +} diff --git a/include/Utils/Shaders/shaderCustom.h b/include/Utils/Shaders/shaderCustom.h new file mode 100644 index 00000000..b0636ff4 --- /dev/null +++ b/include/Utils/Shaders/shaderCustom.h @@ -0,0 +1,54 @@ +#ifndef __SHADER_CUSTOM__ +#define __SHADER_CUSTOM__ + +#include "Utils/GLSLShader.h" +#include "Geometry/vector_gen.h" +#include "Geometry/matrix.h" + + +using namespace CGoGN; + +class ShaderCustom : public Utils::GLSLShader +{ +protected: + // shader sources + static std::string vertexShaderText; + static std::string fragmentShaderText; + static std::string geometryShaderText; + + // uniform locations + CGoGNGLuint m_unif_ambiant; + CGoGNGLuint m_unif_diffuse; + CGoGNGLuint m_unif_lightPos; + CGoGNGLuint m_unif_explode; + + float m_explode; + Geom::Vec4f m_ambiant; + Geom::Vec4f m_diffuse; + Geom::Vec3f m_light_pos; + + Utils::VBO* m_vboPos; + + void getLocations(); + + void restoreUniformsAttribs(); + +public: + ShaderCustom(); + + void setExplode(float explode); + + void setAmbiant(const Geom::Vec4f& ambiant); + + void setDiffuse(const Geom::Vec4f& diffuse); + + void setLightPosition(const Geom::Vec3f& lp); + + void setParams(float explode, const Geom::Vec4f& ambiant, const Geom::Vec4f& diffuse, const Geom::Vec3f& lightPos); + + unsigned int setAttributePosition(Utils::VBO* vbo); + + void setTransformation(Geom::Matrix44f t); +}; + +#endif diff --git a/include/Utils/Shaders/shaderCustom.vert b/include/Utils/Shaders/shaderCustom.vert new file mode 100644 index 00000000..2b19d55a --- /dev/null +++ b/include/Utils/Shaders/shaderCustom.vert @@ -0,0 +1,8 @@ +// ShaderCustom::vertexShaderText +uniform mat4 TransformationMatrix; +uniform mat4 ModelViewProjectionMatrix; +ATTRIBUTE vec3 VertexPosition; +void main() +{ + gl_Position = TransformationMatrix *vec4(VertexPosition, 1.0); +} diff --git a/include/Utils/Shaders/shaderCustomTex.frag b/include/Utils/Shaders/shaderCustomTex.frag new file mode 100644 index 00000000..e2aca964 --- /dev/null +++ b/include/Utils/Shaders/shaderCustomTex.frag @@ -0,0 +1,13 @@ +// ShaderCustomTex::fragmentShaderText + +PRECISON; +uniform sampler2D textureUnit; +in vec3 N; +in vec2 fragTexCoord; +uniform vec4 ambient; +FRAG_OUT_DEF; +void main() +{ + gl_FragData[0] = ambient*texture2D(textureUnit,fragTexCoord); + gl_FragData[1] = vec4( 0.5*normalize(N)+vec3(0.5), 1.0 ); +} \ No newline at end of file diff --git a/include/Utils/Shaders/shaderCustomTex.geom b/include/Utils/Shaders/shaderCustomTex.geom new file mode 100644 index 00000000..b2b0ac6d --- /dev/null +++ b/include/Utils/Shaders/shaderCustomTex.geom @@ -0,0 +1,24 @@ +// ShaderCustomTex::geometryShaderText + +VARYING_IN vec2 texCoord[]; +VARYING_IN vec3 Normal[]; +VARYING_OUT vec2 fragTexCoord; +VARYING_OUT vec3 N; +void main(void) +{ + //vec3 v1 = POSITION_IN(1).xyz - POSITION_IN(0).xyz; + //vec3 v2 = POSITION_IN(2).xyz - POSITION_IN(0).xyz; + //N = cross(v1,v2); + //N = normalize(N); + + + int i; + for(i=0; i< NBVERTS_IN; i++) + { + gl_Position = POSITION_IN(i); + fragTexCoord = texCoord[i]; + N = Normal[i]; + EmitVertex(); + } + EndPrimitive(); +} diff --git a/include/Utils/Shaders/shaderCustomTex.h b/include/Utils/Shaders/shaderCustomTex.h new file mode 100644 index 00000000..2b1b930a --- /dev/null +++ b/include/Utils/Shaders/shaderCustomTex.h @@ -0,0 +1,68 @@ +#ifndef __SHADER_CUSTOMTEX__ +#define __SHADER_CUSTOMTEX__ + +#include "Utils/GLSLShader.h" +#include "Geometry/vector_gen.h" + +#include "Utils/clippingShader.h" +#include "Utils/textures.h" +#include "Utils/gl_def.h" +#include "Geometry/matrix.h" + +using namespace CGoGN; + +class ShaderCustomTex : public Utils::ClippingShader +{ +protected: + // shader sources + static std::string vertexShaderText; + static std::string fragmentShaderText; + static std::string geometryShaderText; + + CGoGNGLuint m_unif_unit; + int m_unit; + + Geom::Vec4f m_col; + + Utils::GTexture* m_tex_ptr; + Utils::VBO* m_vboPos; + Utils::VBO* m_vboNormal; + Utils::VBO* m_vboTexCoord; + + void restoreUniformsAttribs(); + +public: + ShaderCustomTex(); + + /** + * choose the texture unit engine to use for this texture + */ + void setTextureUnit(GLenum texture_unit); + + /** + * set the texture to use + */ + void setTexture(Utils::GTexture* tex); + + /** + * activation of texture unit with set texture + */ + void activeTexture(); + + /** + * activation of texture unit with texture id + */ + void activeTexture(CGoGNGLuint texId); + + unsigned int setAttributePosition(Utils::VBO* vbo); + + unsigned int setAttributeNormal(Utils::VBO* vbo); + + unsigned int setAttributeTexCoord(Utils::VBO* vbo); + + void setBaseColor(Geom::Vec4f col); + + void setTransformation(Geom::Matrix44f t); +}; + +#endif diff --git a/include/Utils/Shaders/shaderCustomTex.vert b/include/Utils/Shaders/shaderCustomTex.vert new file mode 100644 index 00000000..739c7698 --- /dev/null +++ b/include/Utils/Shaders/shaderCustomTex.vert @@ -0,0 +1,16 @@ +// ShaderCustomTex::vertexShaderText + +ATTRIBUTE vec3 VertexPosition, VertexNormal; +ATTRIBUTE vec2 VertexTexCoord; +uniform mat4 TransformationMatrix; +uniform mat4 ModelViewProjectionMatrix; +VARYING_VERT vec2 texCoord; +VARYING_VERT vec3 Normal; +INVARIANT_POS; +void main () +{ + Normal = vec3 (ModelViewProjectionMatrix * TransformationMatrix * vec4 (VertexNormal , 1.0)); + gl_Position = ModelViewProjectionMatrix * TransformationMatrix * vec4 (VertexPosition, 1.0); + texCoord = VertexTexCoord; +} + diff --git a/include/Utils/Shaders/shaderMatCustom.frag b/include/Utils/Shaders/shaderMatCustom.frag new file mode 100644 index 00000000..67e0014c --- /dev/null +++ b/include/Utils/Shaders/shaderMatCustom.frag @@ -0,0 +1,43 @@ +//ShaderMatCustom::fragmentShaderText + +PRECISON; +VARYING_FRAG vec3 EyeVector, Normal, LightDir; +#ifdef WITH_COLOR +VARYING_FRAG vec3 Color; +#endif +uniform vec4 materialDiffuse; +uniform vec4 materialSpecular; +uniform vec4 materialAmbient; +uniform float shininess; +FRAG_OUT_DEF; +void main() +{ + vec3 N = normalize (Normal); + vec3 L = normalize (LightDir); + //float lambertTerm = dot(N,L); + float lambertTerm = 0.15*dot(N,L)+0.80; + + vec4 finalColor = materialAmbient; + + #ifdef DOUBLE_SIDED + if (lambertTerm < 0.0) + { + N = -1.0*N; + lambertTerm = -1.0*lambertTerm; + #else + if (lambertTerm > 0.0) + { + #endif + #ifndef WITH_COLOR + finalColor += materialDiffuse * lambertTerm; + #else + finalColor += vec4((Color*lambertTerm),0.0) ; + #endif + vec3 E = normalize(EyeVector); + vec3 R = reflect(-L, N); + float specular = pow( max(dot(R, E), 0.0), shininess ); + finalColor += materialSpecular * specular; + } + gl_FragColor=finalColor; + //gl_FragColor = vec4(lambertTerm,lambertTerm,lambertTerm,0); +} diff --git a/include/Utils/Shaders/shaderMatCustom.geom b/include/Utils/Shaders/shaderMatCustom.geom new file mode 100644 index 00000000..2c666735 --- /dev/null +++ b/include/Utils/Shaders/shaderMatCustom.geom @@ -0,0 +1,35 @@ +// ShaderMatCustom::geometryShaderText + +uniform float explode; +uniform mat4 ModelViewProjectionMatrix; +uniform mat4 NormalMatrix; +uniform mat4 ModelViewMatrix; +uniform vec3 lightPosition; +uniform vec4 diffuse; +uniform vec4 ambient; +//VARYING_OUT vec4 ColorFS; +VARYING_OUT vec3 N; +VARYING_OUT vec2 texCoord; +void main(void) +{ + vec3 v1 = POSITION_IN(1).xyz - POSITION_IN(0).xyz; + vec3 v2 = POSITION_IN(2).xyz - POSITION_IN(0).xyz; + N = cross(v1,v2); + N = normalize (vec3(NormalMatrix*vec4(N,0.0))); + vec3 center = POSITION_IN(0).xyz + POSITION_IN(1).xyz + POSITION_IN(2).xyz; + center /= 3.0; + vec4 newPos = ModelViewMatrix * vec4(center,0.0); + vec3 L = normalize (lightPosition - newPos.xyz); + float lambertTerm = dot(N,L); + ColorFS = ambient; + if(lambertTerm > 0.0) + ColorFS += diffuse * lambertTerm; + int i; + for(i=0; i< NBVERTS_IN; i++) + { + vec4 pos = explode * POSITION_IN(i) + (1.0-explode)* vec4(center,1.0); + gl_Position = ModelViewProjectionMatrix * pos; + EmitVertex(); + } + EndPrimitive(); +} diff --git a/include/Utils/Shaders/shaderMatCustom.h b/include/Utils/Shaders/shaderMatCustom.h new file mode 100644 index 00000000..198ff421 --- /dev/null +++ b/include/Utils/Shaders/shaderMatCustom.h @@ -0,0 +1,95 @@ +#ifndef SHADER_MATCUST +#define SHADER_MATCUST + +#include "Utils/GLSLShader.h" +#include "Geometry/vector_gen.h" +#include "Geometry/matrix.h" + +#include + +using namespace CGoGN; + +class ShaderMatCustom : public Utils::GLSLShader +{ +protected: + // flag color per vertex or not + bool m_with_color; + // flag color per vertex or not + bool m_with_eyepos; + + // shader sources OGL3 + static std::string vertexShaderText; + static std::string fragmentShaderText; + static std::string geometryShaderText; + + // uniform locations + CGoGNGLuint m_unif_ambiant; + CGoGNGLuint m_unif_diffuse; + CGoGNGLuint m_unif_specular; + CGoGNGLuint m_unif_shininess; + CGoGNGLuint m_unif_lightPos; + CGoGNGLuint m_unif_eyePos; + + //values + Geom::Vec4f m_ambiant; + Geom::Vec4f m_diffuse; + Geom::Vec4f m_specular; + float m_shininess; + Geom::Vec3f m_lightPos; + Geom::Vec3f m_eyePos; + + Utils::VBO* m_vboPos; + Utils::VBO* m_vboNormal; + Utils::VBO* m_vboColor; + + void getLocations(); + + void sendParams(); + + void restoreUniformsAttribs(); + +public: + ShaderMatCustom(bool doubleSided = false, bool withEyePosition=false); + + // inviduals parameter setting functions + void setAmbiant(const Geom::Vec4f& ambiant); + + void setDiffuse(const Geom::Vec4f& diffuse); + + void setSpecular(const Geom::Vec4f& specular); + + void setShininess(float shininess); + + void setLightPosition(const Geom::Vec3f& lp); + + /// set eye position for VR environement + void setEyePosition(const Geom::Vec3f& ep); + + const Geom::Vec4f& getAmbiant() const { return m_ambiant; } + + const Geom::Vec4f& getDiffuse() const { return m_diffuse; } + + const Geom::Vec4f& getSpecular() const { return m_specular; } + + float getShininess() const { return m_shininess; } + + const Geom::Vec3f& getLightPosition() const { return m_lightPos; } + + /** + * set all parameter in on call (one bind also) + */ + void setParams(const Geom::Vec4f& ambiant, const Geom::Vec4f& diffuse, const Geom::Vec4f& specular, float shininess, const Geom::Vec3f& lightPos); + + // attributes + unsigned int setAttributePosition(Utils::VBO* vbo); + + unsigned int setAttributeNormal(Utils::VBO* vbo); + + // optional attributes + unsigned int setAttributeColor(::Utils::VBO* vbo); + void unsetAttributeColor(); + + void setTransformation(Geom::Matrix44f t); +}; + +#endif diff --git a/include/Utils/Shaders/shaderMatCustom.vert b/include/Utils/Shaders/shaderMatCustom.vert new file mode 100644 index 00000000..02aa7145 --- /dev/null +++ b/include/Utils/Shaders/shaderMatCustom.vert @@ -0,0 +1,37 @@ +//ShaderMatCustom::vertexShaderText + +ATTRIBUTE vec3 VertexPosition, VertexNormal; +#ifdef WITH_COLOR +ATTRIBUTE vec3 VertexColor; +#endif +uniform mat4 TransformationMatrix; +uniform mat4 ModelViewProjectionMatrix; +uniform mat4 ModelViewMatrix; +uniform mat4 NormalMatrix; +uniform vec3 lightPosition; +VARYING_VERT vec3 EyeVector, Normal, LightDir; + +#ifdef WITH_COLOR +VARYING_VERT vec3 Color; +#endif + +#ifdef WITH_EYEPOSITION +uniform vec3 eyePosition; +#endif + +INVARIANT_POS; +void main () +{ + Normal = vec3 (NormalMatrix * TransformationMatrix * vec4 (VertexNormal, 0.0)); + vec3 Position = vec3 (ModelViewMatrix * TransformationMatrix * vec4 (VertexPosition, 1.0)); + LightDir = lightPosition - Position; + #ifdef WITH_EYEPOSITION + EyeVector = eyePosition-Position; + #else + EyeVector = -Position; + #endif + #ifdef WITH_COLOR + Color = VertexColor; + #endif + gl_Position = ModelViewProjectionMatrix * (TransformationMatrix * vec4 (VertexPosition, 1.0)); +} diff --git a/include/Utils/gl_matrices.h b/include/Utils/gl_matrices.h index 15273292..a25cd7af 100644 --- a/include/Utils/gl_matrices.h +++ b/include/Utils/gl_matrices.h @@ -70,25 +70,26 @@ public: void rotate(float angle, const Geom::Vec3f& Axis) { - glm::mat4 X = glm::rotate(m_matrices[2], angle, glm::vec3(Axis[0],Axis[1],Axis[2])) * m_matrices[2]; + glm::mat4 X = glm::rotate(glm::mat4(1.f), angle, glm::vec3(Axis[0],Axis[1],Axis[2])) * m_matrices[2]; m_matrices[2] = X; } void translate(const Geom::Vec3f& P) { - glm::mat4 X = glm::translate(m_matrices[2], glm::vec3(P[0],P[1],P[2])) * m_matrices[2]; + + glm::mat4 X = glm::translate(glm::mat4(1.f), glm::vec3(P[0],P[1],P[2])) * m_matrices[2]; m_matrices[2] = X; } void scale(const Geom::Vec3f& S) { - glm::mat4 X = glm::scale(m_matrices[2], glm::vec3(S[0],S[1],S[2])) * m_matrices[2]; + glm::mat4 X = glm::scale(glm::mat4(1.f), glm::vec3(S[0],S[1],S[2])) * m_matrices[2]; m_matrices[2] = X; } void scale(float s) { - glm::mat4 X = glm::scale(m_matrices[2], glm::vec3(s,s,s)) * m_matrices[2]; + glm::mat4 X = glm::scale(glm::mat4(1.f), glm::vec3(s,s,s)) * m_matrices[2]; m_matrices[2] = X; } diff --git a/src/Utils/Shaders/shaderCustom.cpp b/src/Utils/Shaders/shaderCustom.cpp new file mode 100644 index 00000000..0226b848 --- /dev/null +++ b/src/Utils/Shaders/shaderCustom.cpp @@ -0,0 +1,137 @@ +#include +#include "Utils/Shaders/shaderCustom.h" + + +#include "shaderCustom.vert" +#include "shaderCustom.frag" +#include "shaderCustom.geom" + + +ShaderCustom::ShaderCustom() +{ + m_nameVS = "ShaderCustom_vs"; + m_nameFS = "ShaderCustom_fs"; + m_nameGS = "ShaderCustom_gs"; + + std::string glxvert(*GLSLShader::DEFINES_GL); + glxvert.append(vertexShaderText); + + std::string glxgeom = GLSLShader::defines_Geom("triangles", "triangle_strip", 3); + glxgeom.append(geometryShaderText); + + std::string glxfrag(*GLSLShader::DEFINES_GL); + glxfrag.append(fragmentShaderText); + + loadShadersFromMemory(glxvert.c_str(), glxfrag.c_str(), glxgeom.c_str(), GL_TRIANGLES, GL_TRIANGLE_STRIP, 3); + + bind(); + getLocations(); + unbind(); + + //Default values + m_explode = 1.0f; + m_ambiant = Geom::Vec4f(0.05f, 0.05f, 0.1f, 0.0f); + m_diffuse = Geom::Vec4f(0.1f, 1.0f, 0.1f, 0.0f); + m_light_pos = Geom::Vec3f(10.0f, 10.0f, 1000.0f); + + Geom::Matrix44f id; + id.identity(); + setTransformation(id); + + setParams(m_explode, m_ambiant, m_diffuse, m_light_pos); +} + +void ShaderCustom::getLocations() +{ + *m_unif_explode = glGetUniformLocation(program_handler(), "explode"); + *m_unif_ambiant = glGetUniformLocation(program_handler(), "ambient"); + *m_unif_diffuse = glGetUniformLocation(program_handler(), "diffuse"); + *m_unif_lightPos = glGetUniformLocation(program_handler(), "lightPosition"); +} + +unsigned int ShaderCustom::setAttributePosition(Utils::VBO* vbo) +{ + m_vboPos = vbo; + bind(); + unsigned int id = bindVA_VBO("VertexPosition", vbo); + unbind(); + return id; +} + +void ShaderCustom::setParams(float expl, const Geom::Vec4f& ambiant, const Geom::Vec4f& diffuse, const Geom::Vec3f& lightPos) +{ + m_explode = expl; + m_ambiant = ambiant; + m_diffuse = diffuse; + m_light_pos = lightPos; + + bind(); + + glUniform1f(*m_unif_explode, expl); + glUniform4fv(*m_unif_ambiant, 1, ambiant.data()); + glUniform4fv(*m_unif_diffuse, 1, diffuse.data()); + glUniform3fv(*m_unif_lightPos, 1, lightPos.data()); + + unbind(); +} + +void ShaderCustom::setExplode(float explode) +{ + m_explode = explode; + bind(); + glUniform1f(*m_unif_explode, explode); + unbind(); +} + +void ShaderCustom::setAmbiant(const Geom::Vec4f& ambiant) +{ + m_ambiant = ambiant; + bind(); + glUniform4fv(*m_unif_ambiant,1, ambiant.data()); + unbind(); +} + +void ShaderCustom::setDiffuse(const Geom::Vec4f& diffuse) +{ + m_diffuse = diffuse; + bind(); + glUniform4fv(*m_unif_diffuse,1, diffuse.data()); + unbind(); +} + +void ShaderCustom::setLightPosition(const Geom::Vec3f& lp) +{ + m_light_pos = lp; + bind(); + glUniform3fv(*m_unif_lightPos,1,lp.data()); + unbind(); +} + +void ShaderCustom::restoreUniformsAttribs() +{ + *m_unif_explode = glGetUniformLocation(program_handler(),"explode"); + *m_unif_ambiant = glGetUniformLocation(program_handler(),"ambient"); + *m_unif_diffuse = glGetUniformLocation(program_handler(),"diffuse"); + *m_unif_lightPos = glGetUniformLocation(program_handler(),"lightPosition"); + + bind(); + + glUniform1f (*m_unif_explode, m_explode); + glUniform4fv(*m_unif_ambiant, 1, m_ambiant.data()); + glUniform4fv(*m_unif_diffuse, 1, m_diffuse.data()); + glUniform3fv(*m_unif_lightPos, 1, m_light_pos.data()); + + bindVA_VBO("VertexPosition", m_vboPos); + + unbind(); +} + +void ShaderCustom::setTransformation(Geom::Matrix44f t) +{ + bind(); + CGoGNGLuint m_transf; + *m_transf = glGetUniformLocation(program_handler(),"TransformationMatrix"); + glUniformMatrix4fv(*m_transf, 1, false, &t(0,0)); + unbind(); +} + diff --git a/src/Utils/Shaders/shaderCustomTex.cpp b/src/Utils/Shaders/shaderCustomTex.cpp new file mode 100644 index 00000000..cae72ee6 --- /dev/null +++ b/src/Utils/Shaders/shaderCustomTex.cpp @@ -0,0 +1,167 @@ +/******************************************************************************* +* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps * +* version 0.1 * +* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg * +* * +* This library is free software; you can redistribute it and/or modify it * +* under the terms of the GNU Lesser General Public License as published by the * +* Free Software Foundation; either version 2.1 of the License, or (at your * +* option) any later version. * +* * +* This library is distributed in the hope that it will be useful, but WITHOUT * +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * +* for more details. * +* * +* You should have received a copy of the GNU Lesser General Public License * +* along with this library; if not, write to the Free Software Foundation, * +* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * +* * +* Web site: http://cgogn.unistra.fr/ * +* Contact information: cgogn@unistra.fr * +* * +*******************************************************************************/ + +#include +#include "Utils/Shaders/shaderCustomTex.h" + +#include "shaderCustomTex.vert" +#include "shaderCustomTex.frag" +#include "shaderCustomTex.geom" + +//std::string ShaderCustomTex::vertexShaderText = +// "ATTRIBUTE vec3 VertexPosition;\n" +// "ATTRIBUTE vec2 VertexTexCoord;\n" +// "uniform mat4 ModelViewProjectionMatrix;\n" +// "VARYING_VERT vec2 texCoord;\n" +// "INVARIANT_POS;\n" +// "void main ()\n" +// "{\n" +// " gl_Position = ModelViewProjectionMatrix * vec4 (VertexPosition, 1.0);\n" +// " texCoord = VertexTexCoord;\n" +// "}"; +// +// +//std::string ShaderCustomTex::fragmentShaderText = +// "PRECISON;\n" +// "VARYING_FRAG vec2 texCoord;\n" +// "uniform sampler2D textureUnit;\n" +// "FRAG_OUT_DEF;\n" +// "void main()\n" +// "{\n" +// " gl_FragColor=texture2D(textureUnit,texCoord);\n" +// "}"; + + +ShaderCustomTex::ShaderCustomTex() : +m_col(1) +{ + m_nameVS = "ShaderCustomTex_vs"; + m_nameFS = "ShaderCustomTex_fs"; + m_nameGS = "ShaderCustomTex_gs"; + + std::string glxvert(*GLSLShader::DEFINES_GL); + glxvert.append(vertexShaderText); + + std::string glxgeom = GLSLShader::defines_Geom("triangles", "triangle_strip", 3); + glxgeom.append(geometryShaderText); + + std::string glxfrag(*GLSLShader::DEFINES_GL); + glxfrag.append(fragmentShaderText); + +// loadShadersFromMemory(glxvert.c_str(), glxfrag.c_str()); + loadShadersFromMemory(glxvert.c_str(), glxfrag.c_str(), glxgeom.c_str(), GL_TRIANGLES, GL_TRIANGLE_STRIP, 3); + + m_unif_unit = glGetUniformLocation(this->program_handler(), "textureUnit"); + + setBaseColor(m_col); + + Geom::Matrix44f id; + id.identity(); + setTransformation(id); + +} + +void ShaderCustomTex::setTextureUnit(GLenum texture_unit) +{ + this->bind(); + int unit = texture_unit - GL_TEXTURE0; + glUniform1iARB(*m_unif_unit,unit); + m_unit = unit; +} + +void ShaderCustomTex::setTexture(Utils::GTexture* tex) +{ + m_tex_ptr = tex; +} + +void ShaderCustomTex::setBaseColor(Geom::Vec4f col) +{ + m_col = col; + + bind(); + CGoGNGLuint m_unif_ambiant; + *m_unif_ambiant = glGetUniformLocation(program_handler(),"ambient"); + glUniform4fv(*m_unif_ambiant, 1, m_col.data()); + unbind(); +} + +void ShaderCustomTex::setTransformation(Geom::Matrix44f t) +{ + bind(); + CGoGNGLuint m_transf; + *m_transf = glGetUniformLocation(program_handler(),"TransformationMatrix"); + glUniformMatrix4fv(*m_transf, 1, false, &t(0,0)); + unbind(); +} + +void ShaderCustomTex::activeTexture() +{ + glActiveTexture(GL_TEXTURE0 + m_unit); + m_tex_ptr->bind(); +} + +void ShaderCustomTex::activeTexture(CGoGNGLuint texId) +{ + glActiveTexture(GL_TEXTURE0 + m_unit); + glBindTexture(GL_TEXTURE_2D, *texId); +} + +unsigned int ShaderCustomTex::setAttributePosition(Utils::VBO* vbo) +{ + m_vboPos = vbo; + bind(); + unsigned int id = bindVA_VBO("VertexPosition", vbo); + unbind(); + return id; +} + +unsigned int ShaderCustomTex::setAttributeNormal(Utils::VBO* vbo) +{ + m_vboNormal = vbo; + bind(); + unsigned int id = bindVA_VBO("VertexNormal", vbo); + unbind(); + return id; +} + +unsigned int ShaderCustomTex::setAttributeTexCoord(Utils::VBO* vbo) +{ + m_vboTexCoord = vbo; + bind(); + unsigned int id = bindVA_VBO("VertexTexCoord", vbo); + unbind(); + return id; +} + +void ShaderCustomTex::restoreUniformsAttribs() +{ + m_unif_unit = glGetUniformLocation(this->program_handler(), "textureUnit"); + + bindVA_VBO("VertexPosition", m_vboPos); + bindVA_VBO("VertexTexCoord", m_vboTexCoord); + + this->bind(); + glUniform1iARB(*m_unif_unit,m_unit); + this->unbind(); +} diff --git a/src/Utils/Shaders/shaderMatCustom.cpp b/src/Utils/Shaders/shaderMatCustom.cpp new file mode 100644 index 00000000..24908714 --- /dev/null +++ b/src/Utils/Shaders/shaderMatCustom.cpp @@ -0,0 +1,228 @@ +#include +#include +#include "Utils/Shaders/shaderMatCustom.h" + +#include "shaderMatCustom.vert" +#include "shaderMatCustom.frag" +//#include "shaderMatCustom.geom" + +ShaderMatCustom::ShaderMatCustom(bool doubleSided, bool withEyePosition): + m_with_color(false), + m_with_eyepos(withEyePosition), + m_ambiant(Geom::Vec4f(0.05f,0.05f,0.1f,0.0f)), + m_diffuse(Geom::Vec4f(0.1f,1.0f,0.1f,0.0f)), + m_specular(Geom::Vec4f(1.0f,1.0f,1.0f,0.0f)), + m_shininess(100.0f), + m_lightPos(Geom::Vec3f(10.0f,10.0f,1000.0f)), + m_vboPos(NULL), + m_vboNormal(NULL), + m_vboColor(NULL) +{ + m_nameVS = "ShaderMatCustom_vs"; + m_nameFS = "ShaderMatCustom_fs"; + m_nameGS = "ShaderMatCustom_gs"; + + std::string glxvert(*GLSLShader::DEFINES_GL); + // get choose GL defines (2 or 3) + // ans compile shaders + if (m_with_eyepos) + glxvert.append("#define WITH_EYEPOSITION"); + glxvert.append(vertexShaderText); + +// std::string glxgeom = GLSLShader::defines_Geom("triangles", "triangle_strip", 3); +// glxgeom.append(geometryShaderText); + + std::string glxfrag(*GLSLShader::DEFINES_GL); + // Use double sided lighting if set + if (doubleSided) + glxfrag.append("#define DOUBLE_SIDED\n"); + glxfrag.append(fragmentShaderText); + + loadShadersFromMemory(glxvert.c_str(), glxfrag.c_str()); +// loadShadersFromMemory(glxvert.c_str(), glxfrag.c_str(), glxgeom.c_str(), GL_TRIANGLES, GL_TRIANGLE_STRIP, 3); + + // and get and fill uniforms + getLocations(); + + Geom::Matrix44f id; + id.identity(); + setTransformation(id); + + sendParams(); +} + +void ShaderMatCustom::getLocations() +{ + bind(); + *m_unif_ambiant = glGetUniformLocation(this->program_handler(), "materialAmbient"); + *m_unif_diffuse = glGetUniformLocation(this->program_handler(), "materialDiffuse"); + *m_unif_specular = glGetUniformLocation(this->program_handler(), "materialSpecular"); + *m_unif_shininess = glGetUniformLocation(this->program_handler(), "shininess"); + *m_unif_lightPos = glGetUniformLocation(this->program_handler(), "lightPosition"); + if (m_with_eyepos) + *m_unif_eyePos = glGetUniformLocation(this->program_handler(), "eyePosition"); + unbind(); +} + +void ShaderMatCustom::sendParams() +{ + bind(); + glUniform4fv(*m_unif_ambiant, 1, m_ambiant.data()); + glUniform4fv(*m_unif_diffuse, 1, m_diffuse.data()); + glUniform4fv(*m_unif_specular, 1, m_specular.data()); + glUniform1f(*m_unif_shininess, m_shininess); + glUniform3fv(*m_unif_lightPos, 1, m_lightPos.data()); + if (m_with_eyepos) + glUniform3fv(*m_unif_eyePos, 1, m_eyePos.data()); + unbind(); +} + +void ShaderMatCustom::setAmbiant(const Geom::Vec4f& ambiant) +{ + bind(); + glUniform4fv(*m_unif_ambiant,1, ambiant.data()); + m_ambiant = ambiant; + unbind(); +} + +void ShaderMatCustom::setDiffuse(const Geom::Vec4f& diffuse) +{ + bind(); + glUniform4fv(*m_unif_diffuse,1, diffuse.data()); + m_diffuse = diffuse; + unbind(); +} + +void ShaderMatCustom::setSpecular(const Geom::Vec4f& specular) +{ + bind(); + glUniform4fv(*m_unif_specular,1,specular.data()); + m_specular = specular; + unbind(); +} + +void ShaderMatCustom::setShininess(float shininess) +{ + bind(); + glUniform1f (*m_unif_shininess, shininess); + m_shininess = shininess; + unbind(); +} + +void ShaderMatCustom::setLightPosition(const Geom::Vec3f& lightPos) +{ + bind(); + glUniform3fv(*m_unif_lightPos,1,lightPos.data()); + m_lightPos = lightPos; + unbind(); +} + +void ShaderMatCustom::setEyePosition(const Geom::Vec3f& eyePos) +{ + if (m_with_eyepos) + { + bind(); + glUniform3fv(*m_unif_eyePos,1,eyePos.data()); + m_eyePos = eyePos; + unbind(); + } +} + +void ShaderMatCustom::setParams(const Geom::Vec4f& ambiant, const Geom::Vec4f& diffuse, const Geom::Vec4f& specular, float shininess, const Geom::Vec3f& lightPos) +{ + m_ambiant = ambiant; + m_diffuse = diffuse; + m_specular = specular; + m_shininess = shininess; + m_lightPos = lightPos; + sendParams(); +} + +unsigned int ShaderMatCustom::setAttributeColor(CGoGN::Utils::VBO* vbo) +{ + m_vboColor = vbo; + if (!m_with_color) + { + m_with_color=true; + // set the define and recompile shader + std::string gl3vert(*GLSLShader::DEFINES_GL); + gl3vert.append("#define WITH_COLOR 1\n"); + gl3vert.append(vertexShaderText); + std::string gl3frag(*GLSLShader::DEFINES_GL); + gl3frag.append("#define WITH_COLOR 1\n"); + gl3frag.append(fragmentShaderText); + loadShadersFromMemory(gl3vert.c_str(), gl3frag.c_str()); + + // and treat uniforms + getLocations(); + sendParams(); + } + // bind th VA with WBO + bind(); + unsigned int id = bindVA_VBO("VertexColor", vbo); + unbind(); + return id; +} + +void ShaderMatCustom::unsetAttributeColor() +{ + m_vboColor = NULL; + if (m_with_color) + { + m_with_color = false; + // unbind the VA + bind(); + unbindVA("VertexColor"); + unbind(); + // recompile shader + std::string gl3vert(*GLSLShader::DEFINES_GL); + gl3vert.append(vertexShaderText); + std::string gl3frag(*GLSLShader::DEFINES_GL); + gl3frag.append(fragmentShaderText); + loadShadersFromMemory(gl3vert.c_str(), gl3frag.c_str()); + // and treat uniforms + getLocations(); + sendParams(); + } +} + +void ShaderMatCustom::restoreUniformsAttribs() +{ + getLocations(); + sendParams(); + + bind(); + bindVA_VBO("VertexPosition", m_vboPos); + bindVA_VBO("VertexNormal", m_vboNormal); + if (m_vboColor) + bindVA_VBO("VertexColor", m_vboColor); + + unbind(); +} + +unsigned int ShaderMatCustom::setAttributePosition(CGoGN::Utils::VBO* vbo) +{ + m_vboPos = vbo; + bind(); + unsigned int id = bindVA_VBO("VertexPosition", vbo); + unbind(); + return id; +} + +unsigned int ShaderMatCustom::setAttributeNormal(CGoGN::Utils::VBO* vbo) +{ + m_vboNormal = vbo; + bind(); + unsigned int id = bindVA_VBO("VertexNormal", vbo); + unbind(); + return id; +} + +void ShaderMatCustom::setTransformation(Geom::Matrix44f t) +{ + bind(); + CGoGNGLuint m_transf; + *m_transf = glGetUniformLocation(program_handler(),"TransformationMatrix"); + glUniformMatrix4fv(*m_transf, 1, false, &t(0,0)); + unbind(); +} -- GitLab