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

MapHandler::setScaling + double side

parent 2c40f75b
......@@ -92,20 +92,20 @@ void Viewer::cb_initGL()
m_positionVBO = 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->setAttributeNormal(m_normalVBO) ;
m_phongShader->setAmbiant(colClear) ;
m_phongShader->setDiffuse(colDif) ;
m_phongShader->setSpecular(colSpec) ;
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->setAmbiant(colClear) ;
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() ;
......@@ -182,7 +182,7 @@ void Viewer::cb_redraw()
}
m_nbFrames++;
if (m_nbFrames >=100)
if (m_nbFrames >=500)
{
std::cout << 100000.0/m_frame_ch.elapsed()<< " fps"<<std::endl;
m_nbFrames = 0;
......@@ -214,6 +214,34 @@ void Viewer::cb_keyPress(int 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':
m_flatShader->setNoClippingPlane();
m_phongShader->setNoClippingPlane();
......
......@@ -43,6 +43,7 @@
#include "Utils/Shaders/shaderPhong.h"
#include "Utils/Shaders/shaderSimpleFlat.h"
//#include "Utils/Shaders/shaderFlat.h"
#include "Utils/Shaders/shaderSimpleColor.h"
#include "Utils/Shaders/shaderVectorPerVertex.h"
#include "Utils/pointSprite.h"
......
......@@ -2,19 +2,20 @@
uniform vec4 diffuse;
uniform vec4 diffuseBack;
uniform vec4 ambient;
uniform bool doubleSided;
VARYING_FRAG float lambertTerm;
FRAG_OUT_DEF;
void main()
{
#ifdef DOUBLE_SIDED
if (gl_FrontFacing)
FRAG_OUT = ambient + lambertTerm*diffuse;
else
FRAG_OUT = ambient - lambertTerm*diffuse;
#else
if (gl_FrontFacing)
FRAG_OUT = ambient + lambertTerm*diffuse;
else
{
if (!doubleSided)
discard;
FRAG_OUT = ambient - lambertTerm*diffuseBack;
#endif
}
}
......@@ -5,6 +5,7 @@ uniform mat4 ModelViewProjectionMatrix;
uniform mat4 NormalMatrix;
uniform mat4 ModelViewMatrix;
uniform vec3 lightPosition;
VARYING_OUT float lambertTerm;
void main(void)
......
......@@ -50,8 +50,9 @@ protected:
CGoGNGLuint m_unif_diffuseback;
CGoGNGLuint m_unif_lightPos;
CGoGNGLuint m_unif_explode;
CGoGNGLuint m_unif_doubleSided;
bool m_doubleSided;
int m_doubleSided;
float m_explode;
Geom::Vec4f m_ambiant;
Geom::Vec4f m_diffuse;
......@@ -75,7 +76,7 @@ public:
void setDiffuse(const Geom::Vec4f& diffuse);
void setDiffuseBack(const Geom::Vec4f& diffuseb);
void setBackColor(const Geom::Vec4f& diffuseb);
void setLightPosition(const Geom::Vec3f& lp);
......
......@@ -8,30 +8,30 @@ VARYING_FRAG vec3 Color;
uniform vec4 materialDiffuse;
uniform vec4 materialSpecular;
uniform vec4 materialAmbient;
uniform float shininess;
uniform vec4 backColor;
uniform float shininess;
uniform bool doubleSided;
FRAG_OUT_DEF;
void main()
{
vec3 N = normalize (Normal);
vec3 L = normalize (LightDir);
//float lambertTerm = clamp(dot(N,L),0.0,1.0);
vec4 finalColor = materialAmbient;
#ifdef DOUBLE_SIDED
float lambertTerm;
vec4 diffuseColor = materialDiffuse;
vec4 currentColor = materialDiffuse;
if (!gl_FrontFacing)
{
if (!doubleSided)
discard;
N *= -1.0;
lambertTerm = clamp(dot(N,L),0.0,1.0);
currentColor = backColor;
}
else
lambertTerm = clamp(dot(N,L),0.0,1.0);
float lambertTerm = clamp(dot(N,L),0.0,1.0);
#ifndef WITH_COLOR
finalColor += materialDiffuse * lambertTerm;
finalColor += currentColor * lambertTerm;
#else
finalColor += vec4((Color*lambertTerm),0.0) ;
#endif
......@@ -39,33 +39,5 @@ void main()
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);
#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);
#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;
}
......@@ -46,7 +46,7 @@ protected:
bool m_with_color;
// flag color per vertex or not
bool m_with_eyepos;
bool m_doubleSided;
//bool m_doubleSided;
// shader sources OGL3
static std::string vertexShaderText;
......@@ -62,6 +62,7 @@ protected:
CGoGNGLuint m_unif_lightPos;
CGoGNGLuint m_unif_eyePos;
CGoGNGLuint m_unif_backColor;
CGoGNGLuint m_unif_doubleSided;
//values
Geom::Vec4f m_ambiant;
......@@ -71,6 +72,7 @@ protected:
Geom::Vec3f m_lightPos;
Geom::Vec3f m_eyePos;
Geom::Vec4f m_backColor;
int m_doubleSided;
VBO* m_vboPos;
VBO* m_vboNormal;
......@@ -87,7 +89,7 @@ protected:
void restoreUniformsAttribs();
public:
ShaderPhong(bool withClipping = false ,bool doubleSided = false, bool withEyePosition=false);
ShaderPhong(bool withClipping = false, bool withEyePosition=false);
void setDoubleSided(bool doubleSided);
......
......@@ -10,7 +10,7 @@ uniform vec4 materialSpecular;
uniform vec4 materialAmbient;
uniform float shininess;
uniform vec4 backColor;
uniform bool doubleSided;
uniform vec4 planeClip;
VARYING_FRAG vec3 posClip;
......@@ -27,16 +27,14 @@ void main()
vec4 finalColor = materialAmbient;
#ifdef DOUBLE_SIDED
float lambertTerm;
vec4 diffuseColor = materialDiffuse;
if (!gl_FrontFacing)
{
if (!doubleSided)
discard;
N *= -1.0;
lambertTerm = clamp(dot(N,L),0.0,1.0);
}
else
lambertTerm = clamp(dot(N,L),0.0,1.0);
float lambertTerm = clamp(dot(N,L),0.0,1.0);
#ifndef WITH_COLOR
finalColor += materialDiffuse * lambertTerm;
#else
......@@ -46,24 +44,6 @@ void main()
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)
{
#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
{
finalColor = backColor;
}
#endif
FRAG_OUT=finalColor;
}
......@@ -11,6 +11,7 @@ uniform vec4 materialSpecular;
uniform vec4 materialAmbient;
uniform float shininess;
uniform vec4 backColor;
uniform bool doubleSided;
FRAG_OUT_DEF;
......@@ -23,39 +24,21 @@ void main()
vec3 L = normalize (LightDir);
vec4 finalColor = materialAmbient;
float lambertTerm = clamp(dot(N,L),0.0,1.0);
vec4 currentColor = materialDiffuse;
#ifdef DOUBLE_SIDED
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)
{
float lambertTerm = clamp(dot(N,L),0.0,1.0);
#ifndef WITH_COLOR
finalColor += materialDiffuse * lambertTerm;
#else
finalColor += vec4((Color*lambertTerm),0.0) ;
#endif
if (!doubleSided)
discard;
currentColor = backColor;
}
else
{
float lambertTerm = clamp(-dot(N,L),0.0,1.0);
#ifndef WITH_COLOR
finalColor += backColor * lambertTerm;
finalColor += currentColor * lambertTerm;
#else
finalColor += vec4((Color*lambertTerm),0.0) ;
#endif
}
finalColor += vec4((Color*lambertTerm),0.0) ;
#endif
FRAG_OUT=finalColor;
}
......@@ -46,7 +46,6 @@ protected:
bool m_with_color;
// flag color per vertex or not
bool m_with_eyepos;
bool m_doubleSided;
// shader sources OGL3
static std::string vertexShaderText;
static std::string fragmentShaderText;
......@@ -58,12 +57,14 @@ protected:
CGoGNGLuint m_unif_diffuse;
CGoGNGLuint m_unif_lightPos;
CGoGNGLuint m_unif_backColor;
CGoGNGLuint m_unif_doubleSided;
//values
Geom::Vec4f m_ambiant;
Geom::Vec4f m_diffuse;
Geom::Vec3f m_lightPos;
Geom::Vec4f m_backColor;
int m_doubleSided;
VBO* m_vboPos;
VBO* m_vboColor;
......
......@@ -11,6 +11,7 @@ uniform vec4 materialSpecular;
uniform vec4 materialAmbient;
uniform float shininess;
uniform vec4 backColor;
uniform bool doubleSided;
FRAG_OUT_DEF;
......@@ -28,37 +29,21 @@ void main()
vec3 L = normalize (LightDir);
vec4 finalColor = materialAmbient;
vec4 finalColor = materialAmbient;
#ifdef DOUBLE_SIDED
float lambertTerm;
vec4 diffuseColor = materialDiffuse;
if (!gl_FrontFacing)
{
if (!doubleSided)
discard;
N *= -1.0;
lambertTerm = clamp(dot(N,L),0.0,1.0);
}
else
lambertTerm = clamp(dot(N,L),0.0,1.0);
float 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);
if (gl_FrontFacing)
{
#ifndef WITH_COLOR
finalColor += materialDiffuse * lambertTerm;
#else
finalColor += vec4((Color*lambertTerm),0.0) ;
#endif
}
else
{
finalColor = backColor;
}
#endif
FRAG_OUT=finalColor;
}
......@@ -37,7 +37,7 @@ namespace Utils
ShaderFlat::ShaderFlat():
m_doubleSided(false)
m_doubleSided(1)
{
m_nameVS = "ShaderFlat_vs";
m_nameFS = "ShaderFlat_fs";
......@@ -70,27 +70,12 @@ m_doubleSided(false)
void ShaderFlat::setDoubleSided(bool doubleSided)
{
if (doubleSided == m_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);
m_doubleSided = doubleSided;
bind();
getLocations();
glUniform1i(*m_unif_doubleSided, m_doubleSided);
unbind();
setParams(m_explode, m_ambiant, m_diffuse, m_diffuseBack, m_light_pos);
}
void ShaderFlat::getLocations()
......@@ -100,6 +85,7 @@ void ShaderFlat::getLocations()
*m_unif_diffuse = glGetUniformLocation(program_handler(), "diffuse");
*m_unif_diffuseback = glGetUniformLocation(program_handler(), "diffuseBack");
*m_unif_lightPos = glGetUniformLocation(program_handler(), "lightPosition");
*m_unif_doubleSided = glGetUniformLocation(this->program_handler(), "doubleSided");
}
unsigned int ShaderFlat::setAttributePosition(VBO* vbo)
......@@ -153,7 +139,7 @@ void ShaderFlat::setDiffuse(const Geom::Vec4f& diffuse)
unbind();
}
void ShaderFlat::setDiffuseBack(const Geom::Vec4f& diffuseb)
void ShaderFlat::setBackColor(const Geom::Vec4f& diffuseb)
{
m_diffuseBack = diffuseb;
bind();
......@@ -176,6 +162,7 @@ void ShaderFlat::restoreUniformsAttribs()
*m_unif_diffuse = glGetUniformLocation(program_handler(),"diffuse");
*m_unif_diffuseback = glGetUniformLocation(program_handler(),"diffuseBack");
*m_unif_lightPos = glGetUniformLocation(program_handler(),"lightPosition");
*m_unif_doubleSided = glGetUniformLocation(this->program_handler(), "doubleSided");
bind();
......@@ -184,6 +171,7 @@ void ShaderFlat::restoreUniformsAttribs()
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());
glUniform1i(*m_unif_doubleSided, m_doubleSided);
bindVA_VBO("VertexPosition", m_vboPos);
......
......@@ -37,10 +37,10 @@ namespace Utils
#include "shaderPhongClip.frag"
ShaderPhong::ShaderPhong(bool withClipping, bool doubleSided, bool withEyePosition) :
ShaderPhong::ShaderPhong(bool withClipping, bool withEyePosition) :
m_with_color(false),
m_with_eyepos(withEyePosition),
m_doubleSided(doubleSided),
m_doubleSided(1),
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)),
......@@ -63,8 +63,8 @@ namespace Utils
glxvert.append("#define WITH_EYEPOSITION");
glxvert.append(vertexShaderClipText);
// Use double sided lighting if set
if (doubleSided)
glxfrag.append("#define DOUBLE_SIDED\n");
//if (doubleSided)
// glxfrag.append("#define DOUBLE_SIDED\n");
glxfrag.append(fragmentShaderClipText);
}
else
......@@ -75,8 +75,8 @@ namespace Utils
glxvert.append("#define WITH_EYEPOSITION");
glxvert.append(vertexShaderText);
// Use double sided lighting if set
if (doubleSided)
glxfrag.append("#define DOUBLE_SIDED\n");
//if (doubleSided)
// glxfrag.append("#define DOUBLE_SIDED\n");
glxfrag.append(fragmentShaderText);
}
......@@ -88,46 +88,6 @@ namespace Utils
}
void ShaderPhong::setDoubleSided(bool doubleSided)
{
if (doubleSided == m_doubleSided)
return;
m_doubleSided = doubleSided;
std::string glxvert(GLSLShader::defines_gl());
std::string glxfrag(GLSLShader::defines_gl());
if (m_nameVS == "ShaderPhongClip_vs")
{
if (m_with_eyepos)
glxvert.append("#define WITH_EYEPOSITION");
glxvert.append(vertexShaderClipText);
// Use double sided lighting if set
if (doubleSided)
glxfrag.append("#define DOUBLE_SIDED\n");
glxfrag.append(fragmentShaderClipText);
}
else
{
if (m_with_eyepos)
glxvert.append("#define WITH_EYEPOSITION");
glxvert.append(vertexShaderText);
// Use double sided lighting if set
if (doubleSided)
glxfrag.append("#define DOUBLE_SIDED\n");
glxfrag.append(fragmentShaderText);
}
loadShadersFromMemory(glxvert.c_str(), glxfrag.c_str());
// and get and fill uniforms
getLocations();
sendParams();
}
void ShaderPhong::getLocations()
{
bind();
......@@ -140,6 +100,7 @@ void ShaderPhong::getLocations()
*m_unif_eyePos = glGetUniformLocation(this->program_handler(), "eyePosition");
*m_unif_backColor = glGetUniformLocation(this->program_handler(), "backColor");
*m_unif_planeClip = glGetUniformLocation(this->program_handler(), "planeClip");
*m_unif_doubleSided = glGetUniformLocation(this->program_handler(), "doubleSided");
unbind();
}
......@@ -152,6 +113,7 @@ void ShaderPhong::sendParams()
glUniform4fv(*m_unif_specular, 1, m_specular.data());
glUniform1f(*m_unif_shininess, m_shininess);
glUniform3fv(*m_unif_lightPos, 1, m_lightPos.data());
glUniform1i(*m_unif_doubleSided, m_doubleSided);
if (m_with_eyepos)
glUniform3fv(*m_unif_eyePos, 1, m_eyePos.data());
glUniform4fv(*m_unif_backColor, 1, m_backColor.data());
......@@ -160,6 +122,16 @@ void ShaderPhong::sendParams()
unbind();
}
void ShaderPhong::setDoubleSided(bool doubleSided)
{
m_doubleSided = doubleSided;
bind();
glUniform1i(*m_unif_doubleSided, m_doubleSided);
unbind();
}
void ShaderPhong::setAmbiant(const Geom::Vec4f& ambiant)
{
bind();
......
......@@ -57,8 +57,6 @@ ShaderSimpleFlat::ShaderSimpleFlat(bool withClipping, bool doubleSided):
m_nameVS = "ShaderSimpleFlatClip_vs";
m_nameFS = "ShaderSimpleFlatClip_fs";
glxvert.append(vertexShaderClipText);
if (doubleSided)
glxfrag.append("#define DOUBLE_SIDED\n");
glxfrag.append(fragmentShaderClipText);
}
else
......@@ -68,8 +66,6 @@ ShaderSimpleFlat::ShaderSimpleFlat(bool withClipping, bool doubleSided):
// get choose GL defines (2 or 3)
// ans compile shaders
glxvert.append(vertexShaderText);
if (doubleSided)
glxfrag.append("#define DOUBLE_SIDED\n");
glxfrag.append(fragmentShaderText);
}
......@@ -81,31 +77,11 @@ ShaderSimpleFlat::ShaderSimpleFlat(bool withClipping, bool doubleSided):
void ShaderSimpleFlat::setDoubleSided(bool doubleSided)
{
if (doubleSided == m_doubleSided)
return;
m_doubleSided = doubleSided;
std::string glxvert(GLSLShader::defines_gl());
std::string glxfrag(GLSLShader::defines_gl());
if (m_nameVS == "ShaderSimpleFlatClip_vs")
{
glxvert.append(vertexShaderClipText);
if (doubleSided)
glxfrag.append("#define DOUBLE_SIDED\n");
glxfrag.append(fragmentShaderClipText);
}
else
{
glxvert.append(vertexShaderText);
if (doubleSided)
glxfrag.append("#define DOUBLE_SIDED\n");
glxfrag.append(fragmentShaderText);
}
loadShadersFromMemory(glxvert.c_str(), glxfrag.c_str());
// and get and fill uniforms
getLocations();
sendParams();
bind();
glUniform1i(*m_unif_doubleSided, m_doubleSided);
unbind();
}
......@@ -117,6 +93,7 @@ void ShaderSimpleFlat::getLocations()
*m_unif_lightPos = glGetUniformLocation(this->program_handler(), "lightPosition");
*m_unif_backColor = glGetUniformLocation(this->program_handler(), "backColor");
*m_unif_planeClip = glGetUniformLocation(this->program_handler(), "planeClip");
*m_unif_doubleSided = glGetUniformLocation(this->program_handler(), "doubleSided");
unbind();
}
......@@ -127,6 +104,7 @@ void ShaderSimpleFlat::sendParams()
glUniform4fv(*m_unif_diffuse, 1, m_diffuse.data());
glUniform3fv(*m_unif_lightPos, 1, m_lightPos.data());
glUniform4fv(*m_unif_backColor, 1, m_backColor.data());
glUniform1i(*m_unif_doubleSided, m_doubleSided);
if (*m_unif_planeClip > 0)
glUniform4fv(*m_unif_planeClip, 1, m_planeClip.data());
unbind();
......
......@@ -14,7 +14,14 @@
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="1">
<item row="16" column="0" colspan="3">
<widget class="QCheckBox" name="check_renderBoundary">
<property name="text">
<string>render boundary</string>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QComboBox" name="combo_positionVBO">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
......@@ -36,13 +43,6 @@
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Position :</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
......@@ -50,7 +50,7 @@
</property>
</widget>
</item>
<item row="1" column="1">
<item row="1" column="2">
<widget class="QComboBox" name="combo_normalVBO">
<property name="sizePolicy">