Commit c1c330dd authored by Sylvain Thery's avatar Sylvain Thery
Browse files

bug shader

parent 26dd6409
...@@ -78,7 +78,7 @@ void Viewer::initGUI() ...@@ -78,7 +78,7 @@ void Viewer::initGUI()
void Viewer::cb_initGL() void Viewer::cb_initGL()
{ {
Utils::GLSLShader::setCurrentOGLVersion(3) ; Utils::GLSLShader::setCurrentOGLVersion(2) ;
CGoGNout << "GL VERSION = "<< glGetString(GL_VERSION)<< CGoGNendl; CGoGNout << "GL VERSION = "<< glGetString(GL_VERSION)<< CGoGNendl;
Utils::GLSLShader::areShadersSupported(); Utils::GLSLShader::areShadersSupported();
...@@ -102,6 +102,7 @@ void Viewer::cb_initGL() ...@@ -102,6 +102,7 @@ void Viewer::cb_initGL()
m_flatShader->setAttributePosition(m_positionVBO) ; m_flatShader->setAttributePosition(m_positionVBO) ;
m_flatShader->setAmbiant(colClear) ; m_flatShader->setAmbiant(colClear) ;
m_flatShader->setDiffuse(colDif) ; m_flatShader->setDiffuse(colDif) ;
m_flatShader->setDiffuseBack(Geom::Vec4f(0,0,0,0)) ;
m_flatShader->setExplode(faceShrinkage) ; m_flatShader->setExplode(faceShrinkage) ;
m_vectorShader = new Utils::ShaderVectorPerVertex() ; m_vectorShader = new Utils::ShaderVectorPerVertex() ;
......
// ShaderFlat::fragmentShaderText // ShaderFlat::fragmentShaderText
VARYING_FRAG vec4 ColorFS; uniform vec4 diffuse;
uniform vec4 diffuseBack;
uniform vec4 ambient;
VARYING_FRAG float lambertTerm;
FRAG_OUT_DEF; FRAG_OUT_DEF;
void main() void main()
{ {
gl_FragColor = ColorFS; if (gl_FrontFacing)
gl_FragColor = ambient + lambertTerm*diffuse;
else
gl_FragColor = ambient + lambertTerm*diffuseBack;
} }
...@@ -5,24 +5,20 @@ uniform mat4 ModelViewProjectionMatrix; ...@@ -5,24 +5,20 @@ uniform mat4 ModelViewProjectionMatrix;
uniform mat4 NormalMatrix; uniform mat4 NormalMatrix;
uniform mat4 ModelViewMatrix; uniform mat4 ModelViewMatrix;
uniform vec3 lightPosition; uniform vec3 lightPosition;
uniform vec4 diffuse; VARYING_OUT float lambertTerm;
uniform vec4 ambient;
VARYING_OUT vec4 ColorFS;
void main(void) void main(void)
{ {
vec3 v1 = POSITION_IN(1).xyz - POSITION_IN(0).xyz; vec3 v1 = POSITION_IN(1).xyz - POSITION_IN(0).xyz;
vec3 v2 = POSITION_IN(2).xyz - POSITION_IN(0).xyz; vec3 v2 = POSITION_IN(2).xyz - POSITION_IN(0).xyz;
vec3 N = cross(v1,v2); vec3 N = cross(v1,v2);
N = normalize (vec3(NormalMatrix*vec4(N,0.0))); N = normalize(vec3(NormalMatrix*vec4(N,0.0)));
vec3 center = POSITION_IN(0).xyz + POSITION_IN(1).xyz + POSITION_IN(2).xyz; vec3 center = POSITION_IN(0).xyz + POSITION_IN(1).xyz + POSITION_IN(2).xyz;
center /= 3.0; center /= 3.0;
vec4 newPos = ModelViewMatrix * vec4(center,1.0); vec4 newPos = ModelViewMatrix * vec4(center,1.0);
vec3 L = normalize (lightPosition - newPos.xyz); vec3 L = normalize (lightPosition - newPos.xyz);
float lambertTerm = dot(N,L); lambertTerm = clamp(dot(N,L),0.0,1.0);
ColorFS = ambient;
if(lambertTerm > 0.0)
ColorFS += diffuse * lambertTerm;
int i; int i;
for(i=0; i< NBVERTS_IN; i++) for(i=0; i< NBVERTS_IN; i++)
......
...@@ -45,12 +45,14 @@ protected: ...@@ -45,12 +45,14 @@ protected:
// uniform locations // uniform locations
CGoGNGLuint m_unif_ambiant; CGoGNGLuint m_unif_ambiant;
CGoGNGLuint m_unif_diffuse; CGoGNGLuint m_unif_diffuse;
CGoGNGLuint m_unif_diffuseback;
CGoGNGLuint m_unif_lightPos; CGoGNGLuint m_unif_lightPos;
CGoGNGLuint m_unif_explode; CGoGNGLuint m_unif_explode;
float m_explode; float m_explode;
Geom::Vec4f m_ambiant; Geom::Vec4f m_ambiant;
Geom::Vec4f m_diffuse; Geom::Vec4f m_diffuse;
Geom::Vec4f m_diffuseBack;
Geom::Vec3f m_light_pos; Geom::Vec3f m_light_pos;
VBO* m_vboPos; VBO* m_vboPos;
...@@ -68,9 +70,11 @@ public: ...@@ -68,9 +70,11 @@ public:
void setDiffuse(const Geom::Vec4f& diffuse); void setDiffuse(const Geom::Vec4f& diffuse);
void setDiffuseBack(const Geom::Vec4f& diffuseb);
void setLightPosition(const Geom::Vec3f& lp); void setLightPosition(const Geom::Vec3f& lp);
void setParams(float explode, const Geom::Vec4f& ambiant, const Geom::Vec4f& diffuse, const Geom::Vec3f& lightPos); void setParams(float explode, const Geom::Vec4f& ambiant, const Geom::Vec4f& diffuse, const Geom::Vec4f& diffuseBack, const Geom::Vec3f& lightPos);
unsigned int setAttributePosition(VBO* vbo); unsigned int setAttributePosition(VBO* vbo);
}; };
......
...@@ -14,28 +14,43 @@ void main() ...@@ -14,28 +14,43 @@ void main()
{ {
vec3 N = normalize (Normal); vec3 N = normalize (Normal);
vec3 L = normalize (LightDir); vec3 L = normalize (LightDir);
float lambertTerm = dot(N,L); //float lambertTerm = clamp(dot(N,L),0.0,1.0);
vec4 finalColor = materialAmbient; vec4 finalColor = materialAmbient;
#ifdef DOUBLE_SIDED #ifdef DOUBLE_SIDED
if (lambertTerm < 0.0) float lambertTerm;
vec4 diffuseColor = materialDiffuse;
if (!gl_FrontFacing)
{ {
N = -1.0*N; N *= -1.0;
lambertTerm = -1.0*lambertTerm; lambertTerm = clamp(dot(N,L),0.0,1.0);
#else }
if (lambertTerm > 0.0) else
lambertTerm = clamp(dot(N,L),0.0,1.0);
#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;
#else
float lambertTerm = clamp(dot(N,L),0.0,1.0);
if (gl_FrontFacing)
{ {
#endif #ifndef WITH_COLOR
#ifndef WITH_COLOR
finalColor += materialDiffuse * lambertTerm; finalColor += materialDiffuse * lambertTerm;
#else #else
finalColor += vec4((Color*lambertTerm),0.0) ; finalColor += vec4((Color*lambertTerm),0.0) ;
#endif #endif
vec3 E = normalize(EyeVector); vec3 E = normalize(EyeVector);
vec3 R = reflect(-L, N); vec3 R = reflect(-L, N);
float specular = pow( max(dot(R, E), 0.0), shininess ); float specular = pow( max(dot(R, E), 0.0), shininess );
finalColor += materialSpecular * specular; finalColor += materialSpecular * specular;
} }
#endif
gl_FragColor=finalColor; gl_FragColor=finalColor;
} }
...@@ -61,9 +61,10 @@ ShaderFlat::ShaderFlat() ...@@ -61,9 +61,10 @@ ShaderFlat::ShaderFlat()
m_explode = 1.0f; m_explode = 1.0f;
m_ambiant = Geom::Vec4f(0.05f, 0.05f, 0.1f, 0.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_diffuse = Geom::Vec4f(0.1f, 1.0f, 0.1f, 0.0f);
m_diffuseBack = m_diffuse;
m_light_pos = Geom::Vec3f(10.0f, 10.0f, 1000.0f); m_light_pos = Geom::Vec3f(10.0f, 10.0f, 1000.0f);
setParams(m_explode, m_ambiant, m_diffuse, m_light_pos); setParams(m_explode, m_ambiant, m_diffuse, m_diffuseBack ,m_light_pos);
} }
void ShaderFlat::getLocations() void ShaderFlat::getLocations()
...@@ -71,6 +72,7 @@ void ShaderFlat::getLocations() ...@@ -71,6 +72,7 @@ void ShaderFlat::getLocations()
*m_unif_explode = glGetUniformLocation(program_handler(), "explode"); *m_unif_explode = glGetUniformLocation(program_handler(), "explode");
*m_unif_ambiant = glGetUniformLocation(program_handler(), "ambient"); *m_unif_ambiant = glGetUniformLocation(program_handler(), "ambient");
*m_unif_diffuse = glGetUniformLocation(program_handler(), "diffuse"); *m_unif_diffuse = glGetUniformLocation(program_handler(), "diffuse");
*m_unif_diffuseback = glGetUniformLocation(program_handler(), "diffuseBack");
*m_unif_lightPos = glGetUniformLocation(program_handler(), "lightPosition"); *m_unif_lightPos = glGetUniformLocation(program_handler(), "lightPosition");
} }
...@@ -83,7 +85,7 @@ unsigned int ShaderFlat::setAttributePosition(VBO* vbo) ...@@ -83,7 +85,7 @@ unsigned int ShaderFlat::setAttributePosition(VBO* vbo)
return id; return id;
} }
void ShaderFlat::setParams(float expl, const Geom::Vec4f& ambiant, const Geom::Vec4f& diffuse, const Geom::Vec3f& lightPos) void ShaderFlat::setParams(float expl, const Geom::Vec4f& ambiant, const Geom::Vec4f& diffuse, const Geom::Vec4f& diffuseBack, const Geom::Vec3f& lightPos)
{ {
m_explode = expl; m_explode = expl;
m_ambiant = ambiant; m_ambiant = ambiant;
...@@ -95,6 +97,7 @@ void ShaderFlat::setParams(float expl, const Geom::Vec4f& ambiant, const Geom::V ...@@ -95,6 +97,7 @@ void ShaderFlat::setParams(float expl, const Geom::Vec4f& ambiant, const Geom::V
glUniform1f(*m_unif_explode, expl); glUniform1f(*m_unif_explode, expl);
glUniform4fv(*m_unif_ambiant, 1, ambiant.data()); glUniform4fv(*m_unif_ambiant, 1, ambiant.data());
glUniform4fv(*m_unif_diffuse, 1, diffuse.data()); glUniform4fv(*m_unif_diffuse, 1, diffuse.data());
glUniform4fv(*m_unif_diffuseback, 1, diffuseBack.data());
glUniform3fv(*m_unif_lightPos, 1, lightPos.data()); glUniform3fv(*m_unif_lightPos, 1, lightPos.data());
unbind(); unbind();
...@@ -124,6 +127,14 @@ void ShaderFlat::setDiffuse(const Geom::Vec4f& diffuse) ...@@ -124,6 +127,14 @@ void ShaderFlat::setDiffuse(const Geom::Vec4f& diffuse)
unbind(); unbind();
} }
void ShaderFlat::setDiffuseBack(const Geom::Vec4f& diffuseb)
{
m_diffuseBack = diffuseb;
bind();
glUniform4fv(*m_unif_diffuseback,1, diffuseb.data());
unbind();
}
void ShaderFlat::setLightPosition(const Geom::Vec3f& lp) void ShaderFlat::setLightPosition(const Geom::Vec3f& lp)
{ {
m_light_pos = lp; m_light_pos = lp;
...@@ -134,16 +145,18 @@ void ShaderFlat::setLightPosition(const Geom::Vec3f& lp) ...@@ -134,16 +145,18 @@ void ShaderFlat::setLightPosition(const Geom::Vec3f& lp)
void ShaderFlat::restoreUniformsAttribs() void ShaderFlat::restoreUniformsAttribs()
{ {
*m_unif_explode = glGetUniformLocation(program_handler(),"explode"); *m_unif_explode = glGetUniformLocation(program_handler(),"explode");
*m_unif_ambiant = glGetUniformLocation(program_handler(),"ambient"); *m_unif_ambiant = glGetUniformLocation(program_handler(),"ambient");
*m_unif_diffuse = glGetUniformLocation(program_handler(),"diffuse"); *m_unif_diffuse = glGetUniformLocation(program_handler(),"diffuse");
*m_unif_lightPos = glGetUniformLocation(program_handler(),"lightPosition"); *m_unif_diffuseback = glGetUniformLocation(program_handler(),"diffuseBack");
*m_unif_lightPos = glGetUniformLocation(program_handler(),"lightPosition");
bind(); bind();
glUniform1f (*m_unif_explode, m_explode); glUniform1f (*m_unif_explode, m_explode);
glUniform4fv(*m_unif_ambiant, 1, m_ambiant.data()); glUniform4fv(*m_unif_ambiant, 1, m_ambiant.data());
glUniform4fv(*m_unif_diffuse, 1, m_diffuse.data()); glUniform4fv(*m_unif_diffuse, 1, m_diffuse.data());
glUniform4fv(*m_unif_diffuseback, 1, m_diffuseBack.data());
glUniform3fv(*m_unif_lightPos, 1, m_light_pos.data()); glUniform3fv(*m_unif_lightPos, 1, m_light_pos.data());
bindVA_VBO("VertexPosition", m_vboPos); bindVA_VBO("VertexPosition", m_vboPos);
......
...@@ -49,7 +49,7 @@ ShaderPhong::ShaderPhong(bool doubleSided, bool withEyePosition): ...@@ -49,7 +49,7 @@ ShaderPhong::ShaderPhong(bool doubleSided, bool withEyePosition):
{ {
m_nameVS = "ShaderPhong_vs"; m_nameVS = "ShaderPhong_vs";
m_nameFS = "ShaderPhong_fs"; m_nameFS = "ShaderPhong_fs";
m_nameGS = "ShaderPhong_gs"; // m_nameGS = "ShaderPhong_gs";
// get choose GL defines (2 or 3) // get choose GL defines (2 or 3)
// ans compile shaders // ans compile shaders
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment