Commit 51ad10fa authored by Maire Nicolas's avatar Maire Nicolas

Rajout d'une option d'éclairage double face pour le shader de phong.

parent 3d9eddf0
......@@ -72,6 +72,12 @@ add_executable( stage_shader stage_shader.cpp ${stage_shader_ui} ${stage_shader_
target_link_libraries( stage_shader
${CGoGN_LIBS_D} ${COMMON_LIBS} ${QT_LIBRARIES} )
QT4_WRAP_UI( stage_shader_number_two_ui stage_shader_number_two.ui )
QT4_WRAP_CPP( stage_shader_number_two_moc stage_shader_number_two.h )
add_executable( stage_shader_number_two stage_shader_number_two.cpp ${stage_shader_number_two_moc} ${stage_shader_number_two_ui})
target_link_libraries( stage_shader_number_two
${CGoGN_LIBS_D} ${NUMERICAL_LIBS} ${COMMON_LIBS} ${QT_LIBRARIES} )
#add_executable( tuto_subdivision tuto_subdivision.cpp)
#target_link_libraries( tuto_subdivision
......
......@@ -26,6 +26,7 @@
#define __CGOGN_SHADER_PHONG__
#include "Utils/GLSLShader.h"
#include "Utils/clippingShader.h"
#include "Geometry/vector_gen.h"
#include <string>
......@@ -36,7 +37,7 @@ namespace CGoGN
namespace Utils
{
class ShaderPhong : public GLSLShader
class ShaderPhong : public ClippingShader
{
protected:
// flag color per vertex or not
......@@ -71,7 +72,7 @@ protected:
void restoreUniformsAttribs();
public:
ShaderPhong();
ShaderPhong(bool doubleSided = false);
// inviduals parameter setting functions
void setAmbiant(const Geom::Vec4f& ambiant);
......
......@@ -39,10 +39,10 @@ class ShaderSimpleColor : public ClippingShader
{
protected:
// shader sources
static std::string vertexShaderText;
static std::string fragmentShaderText;
static std::string vertexShaderText;
static std::string fragmentShaderText;
// uniform locations
// uniform locations
GLuint m_unif_color;
Geom::Vec4f m_color;
......
......@@ -80,8 +80,10 @@ unsigned int ShaderColorPerVertex::setAttributeColor(VBO* vbo)
void ShaderColorPerVertex::restoreUniformsAttribs()
{
bind();
bindVA_VBO("VertexPosition", m_vboPos);
bindVA_VBO("VertexColor", m_vboCol);
unbind();
}
} // namespace Utils
......
......@@ -76,7 +76,15 @@ std::string ShaderPhong::fragmentShaderText =
" float lambertTerm = dot(N,L);\n"
" vec4 finalColor = materialAmbient;\n"
" if(lambertTerm > 0.0)\n"
" #ifdef DOUBLE_SIDED\n"
" if (lambertTerm < 0.0)\n"
" {\n"
" N = -1.0*N;\n"
" lambertTerm = -1.0*lambertTerm;\n"
" }\n"
" #else\n"
" if (lambertTerm > 0.0)\n"
" #endif\n"
" {\n"
" #ifndef WITH_COLOR\n"
" finalColor += materialDiffuse * lambertTerm;\n"
......@@ -92,19 +100,25 @@ std::string ShaderPhong::fragmentShaderText =
"}";
ShaderPhong::ShaderPhong():
ShaderPhong::ShaderPhong(bool doubleSided):
m_with_color(false),
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_lightPos(Geom::Vec3f(10.0f,10.0f,1000.0f)),
m_vboPos(NULL),
m_vboNormal(NULL),
m_vboColor(NULL)
{
// get choose GL defines (2 or 3)
// ans compile shaders
std::string glxvert(*GLSLShader::DEFINES_GL);
glxvert.append(vertexShaderText);
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());
......@@ -227,12 +241,15 @@ void ShaderPhong::unsetAttributeColor()
void ShaderPhong::restoreUniformsAttribs()
{
getLocations();
bind();
sendParams();
bindVA_VBO("VertexPosition", m_vboPos);
bindVA_VBO("VertexNormal", m_vboNormal);
if (m_vboColor)
bindVA_VBO("VertexColor", m_vboColor);
unbind();
}
unsigned int ShaderPhong::setAttributePosition(VBO* vbo)
......
......@@ -93,6 +93,7 @@ void ShaderSimpleColor::restoreUniformsAttribs()
bind();
glUniform4fv(m_unif_color, 1, m_color.data());
bindVA_VBO("VertexPosition", m_vboPos);
unbind();
}
} // namespace Utils
......
......@@ -130,9 +130,10 @@ void ShaderVectorPerVertex::restoreUniformsAttribs()
{
getLocations();
sendParams();
bind();
bindVA_VBO("VertexPosition", m_vboPos);
bindVA_VBO("VertexVector", m_vboVec);
unbind();
}
} // namespace Utils
......
......@@ -32,9 +32,11 @@ namespace Utils
ClippingShader::ClippingShader()
{
// Default values for plane clipping
m_clipPlane = Geom::Vec4f(0.5, 0.5, 0.5, 0.0);
m_unif_clipPlane = 0;
// Default values for color attenuation
m_colorAttenuationFactor = 0.0;
m_unif_colorAttenuationFactor = 0;
}
......@@ -55,6 +57,9 @@ void ClippingShader::setClippingColorAttenuationFactor(float colorAttenuationFac
void ClippingShader::addPlaneClippingToShaderSource()
{
// Shader name
std::string shaderName = m_nameVS + "/" + m_nameFS + "/" + m_nameGS;
// Verify that the shader has been well created
if (!isCreated())
{
......@@ -62,10 +67,9 @@ void ClippingShader::addPlaneClippingToShaderSource()
<< "ERROR - "
<< "ClippingShader::addPlaneClippingToShaderSource"
<< " - Could not process shader "
<< m_nameVS
<< shaderName
<< " source code : shader has not been created or has failed to compile"
<< CGoGNendl;
// TODO : add real shader name - add m_name attribute to GLSLShader Class
return;
}
......@@ -76,10 +80,9 @@ void ClippingShader::addPlaneClippingToShaderSource()
<< "ERROR - "
<< "ClippingShader::addPlaneClippingToShaderSource"
<< " - Could not process shader "
<< m_nameVS
<< shaderName
<< " source code : unable to add clipping to a shader which uses a geometry shader"
<< CGoGNendl;
// TODO : add real shader name - add m_name attribute to GLSLShader Class
return;
}
......@@ -128,7 +131,7 @@ void ClippingShader::addPlaneClippingToShaderSource()
// Use a shader mutator
ShaderMutator SM("Shader", getVertexShaderSrc(), getFragmentShaderSrc(), ""); // TODO : send the real shader name
ShaderMutator SM(shaderName, getVertexShaderSrc(), getFragmentShaderSrc(), "");
// First check if the vertex shader contains the VertexPosition attribute
if (!SM.VS_containsVariableDeclaration("VertexPosition"))
......@@ -162,6 +165,9 @@ void ClippingShader::addPlaneClippingToShaderSource()
void ClippingShader::updateClippingUniforms()
{
// Shader name
std::string shaderName = m_nameVS + "/" + m_nameFS + "/" + m_nameGS;
// Get uniforms locations
m_unif_clipPlane = glGetUniformLocation(program_handler(), "clip_ClipPlane");
if (m_unif_clipPlane == -1)
......@@ -170,10 +176,9 @@ void ClippingShader::updateClippingUniforms()
<< "ERROR - "
<< "ClippingShader::addPlaneClippingToShaderSource"
<< " - uniform 'clip_ClipPlane' not found in shader "
<< m_nameVS
<< shaderName
<< CGoGNendl;
}
// TODO : add real shader name - add m_name attribute to GLSLShader Class
m_unif_colorAttenuationFactor = glGetUniformLocation(program_handler(), "clip_ColorAttenuationFactor");
if (m_unif_colorAttenuationFactor == -1)
{
......@@ -181,10 +186,9 @@ void ClippingShader::updateClippingUniforms()
<< "ERROR - "
<< "ClippingShader::addPlaneClippingToShaderSource"
<< " - uniform 'clip_ColorAttenuationFactor' not found in shader "
<< m_nameVS
<< shaderName
<< CGoGNendl;
}
// TODO : add real shader name - add m_name attribute to GLSLShader Class
// Set uniforms values
setPlaneClippingParams(m_clipPlane);
......
......@@ -150,7 +150,7 @@ void ShaderMutator::VS_insertCodeAtMainFunctionEnd(const std::string& insertedCo
<< "Unable to insert source code in vertex shader of "
<< m_shaderName
<< ". You should check if the shader has a main function declaration "
<< "and as many '{' as '}'"
<< "and as many '{' as '}' in main"
<< CGoGNendl;
}
}
......@@ -165,7 +165,7 @@ void ShaderMutator::FS_insertCodeAtMainFunctionEnd(const std::string& insertedCo
<< "Unable to insert source code in fragment shader of "
<< m_shaderName
<< ". You should check if the shader has a main function declaration "
<< "and as many '{' as '}'"
<< "and as many '{' as '}' in main"
<< CGoGNendl;
}
}
......@@ -180,7 +180,7 @@ void ShaderMutator::GS_insertCodeAtMainFunctionEnd(const std::string& insertedCo
<< "Unable to insert source code in geometry shader of "
<< m_shaderName
<< ". You should check if the shader has a main function declaration "
<< "and as many '{' as '}'"
<< "and as many '{' as '}' in main"
<< CGoGNendl;
}
}
......
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