Commit 698176e3 authored by Sylvain Thery's avatar Sylvain Thery

MapHandler::setScaling + double side

parent 2c40f75b
...@@ -92,20 +92,20 @@ void Viewer::cb_initGL() ...@@ -92,20 +92,20 @@ void Viewer::cb_initGL()
m_positionVBO = new Utils::VBO() ; m_positionVBO = new Utils::VBO() ;
m_normalVBO = new Utils::VBO() ; m_normalVBO = new Utils::VBO() ;
m_phongShader = new Utils::ShaderPhong(true,false,false) ; m_phongShader = new Utils::ShaderPhong(true,false) ;
m_phongShader->setAttributePosition(m_positionVBO) ; m_phongShader->setAttributePosition(m_positionVBO) ;
m_phongShader->setAttributeNormal(m_normalVBO) ; m_phongShader->setAttributeNormal(m_normalVBO) ;
m_phongShader->setAmbiant(colClear) ; m_phongShader->setAmbiant(colClear) ;
m_phongShader->setDiffuse(colDif) ; m_phongShader->setDiffuse(colDif) ;
m_phongShader->setSpecular(colSpec) ; m_phongShader->setSpecular(colSpec) ;
m_phongShader->setShininess(shininess) ; m_phongShader->setShininess(shininess) ;
m_phongShader->setBackColor(Geom::Vec4f(0,0,0.2,0)) ; m_phongShader->setBackColor(Geom::Vec4f(0.9f, 0.0f, 0.0f, 0.0f));
m_flatShader = new Utils::ShaderSimpleFlat(true,false) ; m_flatShader = new Utils::ShaderSimpleFlat() ;
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->setBackColor(Geom::Vec4f(0,0,0.2,0)) ; m_flatShader->setBackColor(Geom::Vec4f(0.9f, 0.0f, 0.0f, 0.0f));
m_vectorShader = new Utils::ShaderVectorPerVertex() ; m_vectorShader = new Utils::ShaderVectorPerVertex() ;
...@@ -182,7 +182,7 @@ void Viewer::cb_redraw() ...@@ -182,7 +182,7 @@ void Viewer::cb_redraw()
} }
m_nbFrames++; m_nbFrames++;
if (m_nbFrames >=100) if (m_nbFrames >=500)
{ {
std::cout << 100000.0/m_frame_ch.elapsed()<< " fps"<<std::endl; std::cout << 100000.0/m_frame_ch.elapsed()<< " fps"<<std::endl;
m_nbFrames = 0; m_nbFrames = 0;
...@@ -214,6 +214,34 @@ void Viewer::cb_keyPress(int keycode) ...@@ -214,6 +214,34 @@ void Viewer::cb_keyPress(int keycode)
{ {
switch(keycode) switch(keycode)
{ {
case 'd':
{
m_phongShader->setDoubleSided(true);
m_flatShader->setDoubleSided(true);
updateGL();
}
break;
case 'D':
{
m_phongShader->setDoubleSided(false);
m_flatShader->setDoubleSided(false);
updateGL();
}
break;
case 'q':
{
Utils::Chrono ch;
ch.start();
for (int i = 0; i < 1000; ++i)
{
m_render->draw(m_flatShader, Algo::Render::GL2::TRIANGLES);
glFlush();
}
std::cout << "speed render " << 1000.0*1000.0/ch.elapsed() << " fps " << std::endl;
}
case 'n': case 'n':
m_flatShader->setNoClippingPlane(); m_flatShader->setNoClippingPlane();
m_phongShader->setNoClippingPlane(); m_phongShader->setNoClippingPlane();
......
...@@ -43,6 +43,7 @@ ...@@ -43,6 +43,7 @@
#include "Utils/Shaders/shaderPhong.h" #include "Utils/Shaders/shaderPhong.h"
#include "Utils/Shaders/shaderSimpleFlat.h" #include "Utils/Shaders/shaderSimpleFlat.h"
//#include "Utils/Shaders/shaderFlat.h"
#include "Utils/Shaders/shaderSimpleColor.h" #include "Utils/Shaders/shaderSimpleColor.h"
#include "Utils/Shaders/shaderVectorPerVertex.h" #include "Utils/Shaders/shaderVectorPerVertex.h"
#include "Utils/pointSprite.h" #include "Utils/pointSprite.h"
......
...@@ -2,19 +2,20 @@ ...@@ -2,19 +2,20 @@
uniform vec4 diffuse; uniform vec4 diffuse;
uniform vec4 diffuseBack; uniform vec4 diffuseBack;
uniform vec4 ambient; uniform vec4 ambient;
uniform bool doubleSided;
VARYING_FRAG float lambertTerm; VARYING_FRAG float lambertTerm;
FRAG_OUT_DEF; FRAG_OUT_DEF;
void main() void main()
{ {
#ifdef DOUBLE_SIDED
if (gl_FrontFacing)
FRAG_OUT = ambient + lambertTerm*diffuse;
else
FRAG_OUT = ambient - lambertTerm*diffuse;
#else
if (gl_FrontFacing) if (gl_FrontFacing)
FRAG_OUT = ambient + lambertTerm*diffuse; FRAG_OUT = ambient + lambertTerm*diffuse;
else else
{
if (!doubleSided)
discard;
FRAG_OUT = ambient - lambertTerm*diffuseBack; FRAG_OUT = ambient - lambertTerm*diffuseBack;
#endif }
} }
...@@ -5,6 +5,7 @@ uniform mat4 ModelViewProjectionMatrix; ...@@ -5,6 +5,7 @@ uniform mat4 ModelViewProjectionMatrix;
uniform mat4 NormalMatrix; uniform mat4 NormalMatrix;
uniform mat4 ModelViewMatrix; uniform mat4 ModelViewMatrix;
uniform vec3 lightPosition; uniform vec3 lightPosition;
VARYING_OUT float lambertTerm; VARYING_OUT float lambertTerm;
void main(void) void main(void)
......
...@@ -50,8 +50,9 @@ protected: ...@@ -50,8 +50,9 @@ protected:
CGoGNGLuint m_unif_diffuseback; CGoGNGLuint m_unif_diffuseback;
CGoGNGLuint m_unif_lightPos; CGoGNGLuint m_unif_lightPos;
CGoGNGLuint m_unif_explode; CGoGNGLuint m_unif_explode;
CGoGNGLuint m_unif_doubleSided;
bool m_doubleSided; int m_doubleSided;
float m_explode; float m_explode;
Geom::Vec4f m_ambiant; Geom::Vec4f m_ambiant;
Geom::Vec4f m_diffuse; Geom::Vec4f m_diffuse;
...@@ -75,7 +76,7 @@ public: ...@@ -75,7 +76,7 @@ public:
void setDiffuse(const Geom::Vec4f& diffuse); void setDiffuse(const Geom::Vec4f& diffuse);
void setDiffuseBack(const Geom::Vec4f& diffuseb); void setBackColor(const Geom::Vec4f& diffuseb);
void setLightPosition(const Geom::Vec3f& lp); void setLightPosition(const Geom::Vec3f& lp);
......
...@@ -8,43 +8,30 @@ VARYING_FRAG vec3 Color; ...@@ -8,43 +8,30 @@ VARYING_FRAG vec3 Color;
uniform vec4 materialDiffuse; uniform vec4 materialDiffuse;
uniform vec4 materialSpecular; uniform vec4 materialSpecular;
uniform vec4 materialAmbient; uniform vec4 materialAmbient;
uniform float shininess;
uniform vec4 backColor; uniform vec4 backColor;
uniform float shininess;
uniform bool doubleSided;
FRAG_OUT_DEF; FRAG_OUT_DEF;
void main() void main()
{ {
vec3 N = normalize (Normal); vec3 N = normalize (Normal);
vec3 L = normalize (LightDir); vec3 L = normalize (LightDir);
//float lambertTerm = clamp(dot(N,L),0.0,1.0);
vec4 finalColor = materialAmbient; vec4 finalColor = materialAmbient;
vec4 currentColor = materialDiffuse;
#ifdef DOUBLE_SIDED
float lambertTerm;
vec4 diffuseColor = materialDiffuse;
if (!gl_FrontFacing) if (!gl_FrontFacing)
{ {
if (!doubleSided)
discard;
N *= -1.0; N *= -1.0;
lambertTerm = clamp(dot(N,L),0.0,1.0); currentColor = backColor;
} }
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
if (gl_FrontFacing)
{
float lambertTerm = clamp(dot(N,L),0.0,1.0); float lambertTerm = clamp(dot(N,L),0.0,1.0);
#ifndef WITH_COLOR #ifndef WITH_COLOR
finalColor += materialDiffuse * lambertTerm; finalColor += currentColor * lambertTerm;
#else #else
finalColor += vec4((Color*lambertTerm),0.0) ; finalColor += vec4((Color*lambertTerm),0.0) ;
#endif #endif
...@@ -52,20 +39,5 @@ void main() ...@@ -52,20 +39,5 @@ void main()
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;
}
else
{
float lambertTerm = clamp(-dot(N,L),0.0,1.0);
#ifndef WITH_COLOR
finalColor += backColor * 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;
}
#endif
FRAG_OUT=finalColor; FRAG_OUT=finalColor;
} }
...@@ -46,7 +46,7 @@ protected: ...@@ -46,7 +46,7 @@ protected:
bool m_with_color; bool m_with_color;
// flag color per vertex or not // flag color per vertex or not
bool m_with_eyepos; bool m_with_eyepos;
bool m_doubleSided; //bool m_doubleSided;
// shader sources OGL3 // shader sources OGL3
static std::string vertexShaderText; static std::string vertexShaderText;
...@@ -62,6 +62,7 @@ protected: ...@@ -62,6 +62,7 @@ protected:
CGoGNGLuint m_unif_lightPos; CGoGNGLuint m_unif_lightPos;
CGoGNGLuint m_unif_eyePos; CGoGNGLuint m_unif_eyePos;
CGoGNGLuint m_unif_backColor; CGoGNGLuint m_unif_backColor;
CGoGNGLuint m_unif_doubleSided;
//values //values
Geom::Vec4f m_ambiant; Geom::Vec4f m_ambiant;
...@@ -71,6 +72,7 @@ protected: ...@@ -71,6 +72,7 @@ protected:
Geom::Vec3f m_lightPos; Geom::Vec3f m_lightPos;
Geom::Vec3f m_eyePos; Geom::Vec3f m_eyePos;
Geom::Vec4f m_backColor; Geom::Vec4f m_backColor;
int m_doubleSided;
VBO* m_vboPos; VBO* m_vboPos;
VBO* m_vboNormal; VBO* m_vboNormal;
...@@ -87,7 +89,7 @@ protected: ...@@ -87,7 +89,7 @@ protected:
void restoreUniformsAttribs(); void restoreUniformsAttribs();
public: public:
ShaderPhong(bool withClipping = false ,bool doubleSided = false, bool withEyePosition=false); ShaderPhong(bool withClipping = false, bool withEyePosition=false);
void setDoubleSided(bool doubleSided); void setDoubleSided(bool doubleSided);
......
...@@ -10,7 +10,7 @@ uniform vec4 materialSpecular; ...@@ -10,7 +10,7 @@ uniform vec4 materialSpecular;
uniform vec4 materialAmbient; uniform vec4 materialAmbient;
uniform float shininess; uniform float shininess;
uniform vec4 backColor; uniform vec4 backColor;
uniform bool doubleSided;
uniform vec4 planeClip; uniform vec4 planeClip;
VARYING_FRAG vec3 posClip; VARYING_FRAG vec3 posClip;
...@@ -27,29 +27,14 @@ void main() ...@@ -27,29 +27,14 @@ void main()
vec4 finalColor = materialAmbient; vec4 finalColor = materialAmbient;
#ifdef DOUBLE_SIDED
float lambertTerm;
vec4 diffuseColor = materialDiffuse;
if (!gl_FrontFacing) if (!gl_FrontFacing)
{ {
if (!doubleSided)
discard;
N *= -1.0; N *= -1.0;
lambertTerm = clamp(dot(N,L),0.0,1.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); float lambertTerm = clamp(dot(N,L),0.0,1.0);
if (gl_FrontFacing)
{
#ifndef WITH_COLOR #ifndef WITH_COLOR
finalColor += materialDiffuse * lambertTerm; finalColor += materialDiffuse * lambertTerm;
#else #else
...@@ -59,11 +44,6 @@ void main() ...@@ -59,11 +44,6 @@ void main()
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;
}
else
{
finalColor = backColor;
}
#endif
FRAG_OUT=finalColor; FRAG_OUT=finalColor;
} }
...@@ -11,6 +11,7 @@ uniform vec4 materialSpecular; ...@@ -11,6 +11,7 @@ uniform vec4 materialSpecular;
uniform vec4 materialAmbient; uniform vec4 materialAmbient;
uniform float shininess; uniform float shininess;
uniform vec4 backColor; uniform vec4 backColor;
uniform bool doubleSided;
FRAG_OUT_DEF; FRAG_OUT_DEF;
...@@ -23,39 +24,21 @@ void main() ...@@ -23,39 +24,21 @@ void main()
vec3 L = normalize (LightDir); vec3 L = normalize (LightDir);
vec4 finalColor = materialAmbient; vec4 finalColor = materialAmbient;
float lambertTerm = clamp(dot(N,L),0.0,1.0);
#ifdef DOUBLE_SIDED vec4 currentColor = materialDiffuse;
float lambertTerm;
vec4 diffuseColor = materialDiffuse;
if (!gl_FrontFacing)
lambertTerm = clamp(-dot(N,L),0.0,1.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
#else
if (gl_FrontFacing) if (!gl_FrontFacing)
{ {
float lambertTerm = clamp(dot(N,L),0.0,1.0); if (!doubleSided)
#ifndef WITH_COLOR discard;
finalColor += materialDiffuse * lambertTerm; currentColor = backColor;
#else
finalColor += vec4((Color*lambertTerm),0.0) ;
#endif
} }
else
{
float lambertTerm = clamp(-dot(N,L),0.0,1.0);
#ifndef WITH_COLOR #ifndef WITH_COLOR
finalColor += backColor * lambertTerm; finalColor += currentColor * lambertTerm;
#else #else
finalColor += vec4((Color*lambertTerm),0.0) ; finalColor += vec4((Color*lambertTerm),0.0) ;
#endif
}
#endif #endif
FRAG_OUT=finalColor; FRAG_OUT=finalColor;
} }
...@@ -46,7 +46,6 @@ protected: ...@@ -46,7 +46,6 @@ protected:
bool m_with_color; bool m_with_color;
// flag color per vertex or not // flag color per vertex or not
bool m_with_eyepos; bool m_with_eyepos;
bool m_doubleSided;
// shader sources OGL3 // shader sources OGL3
static std::string vertexShaderText; static std::string vertexShaderText;
static std::string fragmentShaderText; static std::string fragmentShaderText;
...@@ -58,12 +57,14 @@ protected: ...@@ -58,12 +57,14 @@ protected:
CGoGNGLuint m_unif_diffuse; CGoGNGLuint m_unif_diffuse;
CGoGNGLuint m_unif_lightPos; CGoGNGLuint m_unif_lightPos;
CGoGNGLuint m_unif_backColor; CGoGNGLuint m_unif_backColor;
CGoGNGLuint m_unif_doubleSided;
//values //values
Geom::Vec4f m_ambiant; Geom::Vec4f m_ambiant;
Geom::Vec4f m_diffuse; Geom::Vec4f m_diffuse;
Geom::Vec3f m_lightPos; Geom::Vec3f m_lightPos;
Geom::Vec4f m_backColor; Geom::Vec4f m_backColor;
int m_doubleSided;
VBO* m_vboPos; VBO* m_vboPos;
VBO* m_vboColor; VBO* m_vboColor;
......
...@@ -11,6 +11,7 @@ uniform vec4 materialSpecular; ...@@ -11,6 +11,7 @@ uniform vec4 materialSpecular;
uniform vec4 materialAmbient; uniform vec4 materialAmbient;
uniform float shininess; uniform float shininess;
uniform vec4 backColor; uniform vec4 backColor;
uniform bool doubleSided;
FRAG_OUT_DEF; FRAG_OUT_DEF;
...@@ -28,37 +29,21 @@ void main() ...@@ -28,37 +29,21 @@ void main()
vec3 L = normalize (LightDir); vec3 L = normalize (LightDir);
vec4 finalColor = materialAmbient;
vec4 finalColor = materialAmbient; vec4 finalColor = materialAmbient;
#ifdef DOUBLE_SIDED
float lambertTerm;
vec4 diffuseColor = materialDiffuse;
if (!gl_FrontFacing) if (!gl_FrontFacing)
{ {
if (!doubleSided)
discard;
N *= -1.0; N *= -1.0;
lambertTerm = clamp(dot(N,L),0.0,1.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
#else
float lambertTerm = clamp(dot(N,L),0.0,1.0); float lambertTerm = clamp(dot(N,L),0.0,1.0);
if (gl_FrontFacing)
{
#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
}
else
{
finalColor = backColor;
}
#endif #endif
FRAG_OUT=finalColor; FRAG_OUT=finalColor;
} }
...@@ -37,7 +37,7 @@ namespace Utils ...@@ -37,7 +37,7 @@ namespace Utils
ShaderFlat::ShaderFlat(): ShaderFlat::ShaderFlat():
m_doubleSided(false) m_doubleSided(1)
{ {
m_nameVS = "ShaderFlat_vs"; m_nameVS = "ShaderFlat_vs";
m_nameFS = "ShaderFlat_fs"; m_nameFS = "ShaderFlat_fs";
...@@ -70,27 +70,12 @@ m_doubleSided(false) ...@@ -70,27 +70,12 @@ m_doubleSided(false)
void ShaderFlat::setDoubleSided(bool doubleSided) void ShaderFlat::setDoubleSided(bool doubleSided)
{ {
if (doubleSided == m_doubleSided) m_doubleSided = doubleSided;
return;
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());
if (doubleSided)
glxfrag.append("#define DOUBLE_SIDED\n");
glxfrag.append(fragmentShaderText);
loadShadersFromMemory(glxvert.c_str(), glxfrag.c_str(), glxgeom.c_str(), GL_TRIANGLES, GL_TRIANGLE_STRIP, 3);
bind(); bind();
getLocations(); glUniform1i(*m_unif_doubleSided, m_doubleSided);
unbind(); unbind();
setParams(m_explode, m_ambiant, m_diffuse, m_diffuseBack, m_light_pos);
} }
void ShaderFlat::getLocations()