Commit 9b1b9e57 authored by Sylvain Thery's avatar Sylvain Thery

Shader dans fichier separe mais compile dans le .o

Ajout executable Tools/shader_to_h
Modification de la compilation de utils pour generer
les fichiers a inclure  a la compilation
parent 7764757b
......@@ -522,7 +522,7 @@ void MyQT::cb_keyPress(int keycode)
if (!d_vertices.empty())
{
std::stringstream ss;
ss << "Sommet: dart: " << d_vertices[0].index << ": " << position[d_vertices[0]];
ss << "Sommet: dart: " << d_vertices[0].index << ": " << position[d_vertices[0]]<< "( id emb:"<< myMap.getEmbedding(VERTEX,d_vertices[0])<<")"<< std::endl; ;
statusMsg(ss.str().c_str());
updateGL();
......
......@@ -34,5 +34,6 @@ ELSE(WIN32)
add_subdirectory(AntTweakBar)
add_subdirectory(Numerical)
add_subdirectory(Assimp)
add_subdirectory(Tools)
ENDIF(WIN32)
cmake_minimum_required(VERSION 2.6)
project(Tools)
SET (CMAKE_BUILD_TYPE Release)
add_executable( shader_to_h shader_to_h.cpp )
target_link_libraries( shader_to_h )
#include <iostream>
#include <fstream>
#include <sstream>
int main(int argc, char **argv)
{
char buffer[512];
for (unsigned int i=1; i< argc; ++i)
{
std::string filename(argv[i]);
std::ifstream fs(filename.c_str(), std::ios::in);
if (!fs.good())
{
std::cerr << "Unable to open file " << filename << std::endl;
return 1;
}
size_t last_slash = filename.rfind('/');
if (last_slash == std::string::npos)
{
last_slash = filename.rfind('\\');
if (last_slash == std::string::npos)
last_slash = 0;
else
++last_slash;
}
else
++last_slash;
std::string outName = filename.substr(last_slash,filename.size()-last_slash);
std::stringstream ssi;
std::stringstream sso;
std::ifstream fsi(outName.c_str(),std::ios::in);
if (fsi.good())
{
while (!fsi.eof())
{
fsi.getline(buffer,512);
if (!fsi.eof())
ssi << buffer << std::endl ;
}
fsi.close();
}
// fist line
fs.getline(buffer,512);
char *sub=buffer;
while ((*sub=='/') || (*sub==' '))
++sub;
sso << "std::string "<<sub<< " =";
// text of shader
while (!fs.eof())
{
fs.getline(buffer,512);
if (!fs.eof() && (*buffer!=0))
sso << std::endl << "\"" << buffer <<"\\n\"";
};
sso << ";"<< std::endl<< std::endl;
std::string ssostr = sso.str();
if (ssostr != ssi.str())
{
std::ofstream fso(outName.c_str(),std::ios::out);
fso << ssostr;
std::cout << "Shader_to_h: "<< outName << " copy"<< std::endl;
fso.close();
}
else
std::cout << "Shader_to_h: "<< outName << " ok"<< std::endl;
fs.close();
}
return 0;
}
......@@ -19,6 +19,7 @@ ADD_DEFINITIONS(${QT_DEFINITIONS})
include_directories(
${CMAKE_CURRENT_BINARY_DIR}
/usr/include/libxml2
${CGoGN_ROOT_DIR}/include
${CGoGN_ROOT_DIR}/ThirdParty/Numerical
......@@ -66,17 +67,22 @@ add_library(
${files_algo}
)
#file(
# GLOB_RECURSE
# files_geometry
# ${CGoGN_ROOT_DIR}/src/Geometry/*.cpp
# ${CGoGN_ROOT_DIR}/include/Geometry/*.hpp
# ${CGoGN_ROOT_DIR}/include/Geometry/*.h
#)
#add_library(
# geometryD
# ${files_geometry}
#)
file(
GLOB_RECURSE
shaders_srcD
${CGoGN_ROOT_DIR}/include/Utils/*.frag
${CGoGN_ROOT_DIR}/include/Utils/*.vert
${CGoGN_ROOT_DIR}/include/Utils/*.geom
${CGoGN_ROOT_DIR}/include/Utils/Shaders*.frag
${CGoGN_ROOT_DIR}/include/Utils/Shaders*.vert
${CGoGN_ROOT_DIR}/include/Utils/Shaders*.geom
)
add_custom_target(shader_targetD ${CGoGN_ROOT_DIR}/ThirdParty/Tools/shader_to_h ${shaders_srcD}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
SOURCES ${shaders_srcD}
)
file(
......@@ -102,4 +108,5 @@ add_library(
${files_utils_withQt}
)
add_dependencies(utilsD shader_targetD )
......@@ -21,6 +21,7 @@ ADD_DEFINITIONS(${QT_DEFINITIONS})
include_directories(
${CMAKE_CURRENT_BINARY_DIR}
/usr/include/libxml2
${CGoGN_ROOT_DIR}/include
${CGoGN_ROOT_DIR}/ThirdParty/Numerical
......@@ -81,6 +82,24 @@ add_library(
#)
file(
GLOB_RECURSE
shaders_src
${CGoGN_ROOT_DIR}/include/Utils/*.frag
${CGoGN_ROOT_DIR}/include/Utils/*.vert
${CGoGN_ROOT_DIR}/include/Utils/*.geom
${CGoGN_ROOT_DIR}/include/Utils/Shaders*.frag
${CGoGN_ROOT_DIR}/include/Utils/Shaders*.vert
${CGoGN_ROOT_DIR}/include/Utils/Shaders*.geom
)
add_custom_target(shader_target ${CGoGN_ROOT_DIR}/ThirdParty/Tools/shader_to_h ${shaders_src}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
SOURCES ${shaders_src}
)
file(
GLOB_RECURSE
files_utils
......@@ -104,6 +123,7 @@ add_library(
${files_utils_withQt}
)
add_dependencies(utils shader_target )
// ShaderColorPerVertex::fragmentShaderText
PRECISON;
VARYING_FRAG vec3 color;
FRAG_OUT_DEF;
void main()
{
gl_FragColor=vec4(color,0.0);
}
// ShaderColorPerVertex::vertexShaderText
ATTRIBUTE vec3 VertexPosition;
ATTRIBUTE vec3 VertexColor;
uniform mat4 ModelViewProjectionMatrix;
VARYING_VERT vec3 color;
INVARIANT_POS;
void main ()
{
gl_Position = ModelViewProjectionMatrix * vec4 (VertexPosition, 1.0);
color = VertexColor;
}
// ShaderFlat::fragmentShaderText
VARYING_FRAG vec4 ColorFS;
void main()
{
gl_FragColor = ColorFS;
}
// ShaderFlat::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;
void main(void)
{
vec3 v1 = POSITION_IN(1).xyz - POSITION_IN(0).xyz;
vec3 v2 = POSITION_IN(2).xyz - POSITION_IN(0).xyz;
vec3 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();
}
// ShaderFlat::vertexShaderText
ATTRIBUTE vec3 VertexPosition;
void main()
{
gl_Position = vec4(VertexPosition, 1.0);
}
//ShaderPhong::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);
vec4 finalColor = materialAmbient;
if(lambertTerm > 0.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;
}
gl_FragColor=finalColor;
}
//ShaderPhong::vertexShaderText
ATTRIBUTE vec3 VertexPosition, VertexNormal;
#ifdef WITH_COLOR
ATTRIBUTE vec3 VertexColor;
#endif
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
INVARIANT_POS;
void main ()
{
Normal = vec3 (NormalMatrix * vec4 (VertexNormal, 0.0));
vec3 Position = vec3 (ModelViewMatrix * vec4 (VertexPosition, 1.0));
LightDir = lightPosition - Position;
EyeVector = -Position;
#ifdef WITH_COLOR
Color = VertexColor;
#endif
gl_Position = ModelViewProjectionMatrix * vec4 (VertexPosition, 1.0);
}
// ShaderSimpleColor::fragmentShaderText
PRECISON;
uniform vec4 color;
FRAG_OUT_DEF;
void main()
{
gl_FragColor=color;
}
// ShaderSimpleColor::vertexShaderText
ATTRIBUTE vec3 VertexPosition, VertexNormal;
uniform mat4 ModelViewProjectionMatrix;
void main ()
{
gl_Position = ModelViewProjectionMatrix * vec4 (VertexPosition, 1.0);
}
// ShaderSimpleTexture::fragmentShaderText
PRECISON;
VARYING_FRAG vec2 texCoord;
uniform sampler2D textureUnit;
FRAG_OUT_DEF;
void main()
{
gl_FragColor=texture2D(textureUnit,texCoord);
}
\ No newline at end of file
// ShaderSimpleTexture::vertexShaderText
ATTRIBUTE vec3 VertexPosition;
ATTRIBUTE vec2 VertexTexCoord;
uniform mat4 ModelViewProjectionMatrix;
VARYING_VERT vec2 texCoord;
INVARIANT_POS;
void main ()
{
gl_Position = ModelViewProjectionMatrix * vec4 (VertexPosition, 1.0);
texCoord = VertexTexCoord;
}
// ShaderTextureMask::fragmentShaderText
PRECISON;
VARYING_FRAG vec2 texCoord;
uniform sampler2D textureUnit;
uniform sampler2D textureUnitMask;
FRAG_OUT_DEF;
void main()
{
float m = texture2D(textureUnitMask,texCoord).r;
if (m < 0.5)
discard;
gl_FragColor=texture2D(textureUnit,texCoord)*m;
}
// ShaderTextureMask::vertexShaderText
ATTRIBUTE vec3 VertexPosition;
ATTRIBUTE vec2 VertexTexCoord;
uniform mat4 ModelViewProjectionMatrix;
VARYING_VERT vec2 texCoord;
INVARIANT_POS;
void main ()
{
gl_Position = ModelViewProjectionMatrix * vec4 (VertexPosition, 1.0);
texCoord = VertexTexCoord;
}
\ No newline at end of file
// ShaderVectorPerVertex::fragmentShaderText
PRECISON;
uniform vec4 vectorColor;
FRAG_OUT_DEF;
void main()
{
gl_FragColor = vectorColor;
}
// ShaderVectorPerVertex::geometryShaderText
uniform float vectorScale;
uniform mat4 ModelViewProjectionMatrix;
VARYING_IN vec3 VectorAttrib[];
void main()
{
gl_Position = ModelViewProjectionMatrix * POSITION_IN(0);
EmitVertex();
gl_Position = ModelViewProjectionMatrix * (POSITION_IN(0) + vec4(VectorAttrib[0] * vectorScale, 0.0));
EmitVertex();
EndPrimitive();
}
// ShaderVectorPerVertex::vertexShaderText
ATTRIBUTE vec3 VertexPosition;
ATTRIBUTE vec3 VertexVector;
VARYING_VERT vec3 VectorAttrib;
INVARIANT_POS;
void main ()
{
VectorAttrib = VertexVector;
gl_Position = vec4(VertexPosition, 1.0);
}
// PointSprite::fragmentShaderText
uniform sampler2D SpriteTexture;
uniform float size;
uniform vec3 color;
VARYING_FRAG vec2 texCoord;
VARYING_FRAG vec2 positionFragIn;
VARYING_FRAG vec4 mvpFragIn;
void main(void)
{
float lum = texture2D(SpriteTexture, texCoord).s;
if (lum==0.0)
discard;
vec2 v = texCoord-vec2(0.5,0.5);
float z = size * sqrt(1.0-dot(v,v));
vec2 zfrag = positionFragIn + vec2(z,0.0);
gl_FragDepth = 0.5 + 0.5 * dot(zfrag, mvpFragIn.xy) / dot(zfrag, mvpFragIn.zw);
gl_FragColor = vec4(color,0.0)*lum;
}
\ No newline at end of file
// PointSprite::geometryShaderText
uniform float size;
uniform mat4 ModelViewMatrix;
uniform mat4 ProjectionMatrix;
VARYING_OUT vec2 texCoord;
VARYING_OUT vec2 positionFragIn;
VARYING_OUT vec4 mvpFragIn;
void main()
{
vec4 posCenter = ModelViewMatrix * POSITION_IN(0);
vec4 pos = posCenter + vec4(-size, size, 0.0, 0.0);
positionFragIn = posCenter.zw;
mvpFragIn.x = ProjectionMatrix[2][2];
mvpFragIn.y = ProjectionMatrix[3][2];
mvpFragIn.z = ProjectionMatrix[2][3];
mvpFragIn.w = ProjectionMatrix[3][3];
texCoord = vec2(0.0,1.0);
gl_Position = ProjectionMatrix * pos;
EmitVertex();
pos = posCenter + vec4(-size, -size, 0.0, 0.0);
texCoord = vec2(0.0,0.0);
gl_Position = ProjectionMatrix * pos;
EmitVertex();
pos = posCenter + vec4( size, size, 0.0, 0.0);
texCoord = vec2(1.0,1.0);
gl_Position = ProjectionMatrix * pos;
EmitVertex();
pos = posCenter + vec4( size,-size, 0.0, 0.0);
texCoord = vec2(1.0,0.0);
gl_Position = ProjectionMatrix * pos;
EmitVertex();
EndPrimitive();
}
// PointSprite::vertexShaderText
ATTRIBUTE vec3 VertexPosition;
void main ()
{
gl_Position = vec4(VertexPosition,1.0);
}
//Strings3D::fragmentShaderText1
VARYING_FRAG vec2 tex_coord;
uniform sampler2D FontTexture;
uniform vec3 color;
FRAG_OUT_DEF;
void main (void)
{
float lum = texture2D(FontTexture, tex_coord).s;;
}
//Strings3D::vertexShaderText
ATTRIBUTE vec4 VertexPosition;
uniform mat4 ModelViewMatrix;
uniform mat4 ProjectionMatrix
uniform vec3 strPos;
uniform float scale;
VARYING_VERT vec2 tex_coord;
INVARIANT_POS;
void main ()
{
vec4 pos = ModelViewMatrix * vec4(strPos,1.0) + vec4(VertexPosition[0]*scale,VertexPosition[1]*scale,0.0,0.0);
tex_coord = vec2(VertexPosition[2],VertexPosition[3]);
gl_Position = ProjectionMatrix * pos;
}
\ No newline at end of file
......@@ -32,27 +32,30 @@ namespace CGoGN
namespace Utils
{
std::string ShaderColorPerVertex::vertexShaderText =
"ATTRIBUTE vec3 VertexPosition;\n"
"ATTRIBUTE vec3 VertexColor;\n"
"uniform mat4 ModelViewProjectionMatrix;\n"
"VARYING_VERT vec3 color;\n"
"INVARIANT_POS;\n"
"void main ()\n"
"{\n"
" gl_Position = ModelViewProjectionMatrix * vec4 (VertexPosition, 1.0);\n"
" color = VertexColor;\n"
"}";
std::string ShaderColorPerVertex::fragmentShaderText =
"PRECISON;\n"
"VARYING_FRAG vec3 color;\n"
"FRAG_OUT_DEF;\n"
"void main()\n"
"{\n"
" gl_FragColor=vec4(color,0.0);\n"
"}";
#include "shaderColorPerVertex.vert"
#include "shaderColorPerVertex.frag"
//std::string ShaderColorPerVertex::vertexShaderText =
// "ATTRIBUTE vec3 VertexPosition;\n"
// "ATTRIBUTE vec3 VertexColor;\n"
// "uniform mat4 ModelViewProjectionMatrix;\n"
// "VARYING_VERT vec3 color;\n"
// "INVARIANT_POS;\n"
// "void main ()\n"
// "{\n"
// " gl_Position = ModelViewProjectionMatrix * vec4 (VertexPosition, 1.0);\n"
// " color = VertexColor;\n"
// "}";
//
//
//std::string ShaderColorPerVertex::fragmentShaderText =
// "PRECISON;\n"
// "VARYING_FRAG vec3 color;\n"
// "FRAG_OUT_DEF;\n"
// "void main()\n"
// "{\n"
// " gl_FragColor=vec4(color,0.0);\n"
// "}";
ShaderColorPerVertex::ShaderColorPerVertex()
......
......@@ -31,54 +31,57 @@ namespace CGoGN
namespace Utils
{
std::string ShaderFlat::vertexShaderText =
"ATTRIBUTE vec3 VertexPosition;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(VertexPosition, 1.0);\n"
"}";
std::string ShaderFlat::geometryShaderText =
"uniform float explode;\n"
"uniform mat4 ModelViewProjectionMatrix;\n"
"uniform mat4 NormalMatrix;\n"
"uniform mat4 ModelViewMatrix;\n"
"uniform vec3 lightPosition;\n"
"uniform vec4 diffuse;\n"
"uniform vec4 ambient;\n"
"VARYING_OUT vec4 ColorFS;\n"
"void main(void)\n"
"{\n"
" vec3 v1 = POSITION_IN(1).xyz - POSITION_IN(0).xyz;\n"
" vec3 v2 = POSITION_IN(2).xyz - POSITION_IN(0).xyz;\n"
" vec3 N = cross(v1,v2);\n"
" N = normalize (vec3(NormalMatrix*vec4(N,0.0))); \n"
" vec3 center = POSITION_IN(0).xyz + POSITION_IN(1).xyz + POSITION_IN(2).xyz; \n"
" center /= 3.0;\n"
" vec4 newPos = ModelViewMatrix * vec4(center,0.0);\n"
" vec3 L = normalize (lightPosition - newPos.xyz);\n"
" float lambertTerm = dot(N,L);\n"
" ColorFS = ambient;\n"
" if(lambertTerm > 0.0)\n"
" ColorFS += diffuse * lambertTerm;\n"
" int i;\n"
" for(i=0; i< NBVERTS_IN; i++)\n"
" {\n"
" vec4 pos = explode * POSITION_IN(i) + (1.0-explode)* vec4(center,1.0);\n"
" gl_Position = ModelViewProjectionMatrix * pos;\n"
" EmitVertex();\n"
" }\n"
" EndPrimitive();\n"
"}";
std::string ShaderFlat::fragmentShaderText =
"VARYING_FRAG vec4 ColorFS; \n"
"void main()\n"
"{\n"
" gl_FragColor = ColorFS;\n"
"}";
#include "shaderFlat.vert"
#include "shaderFlat.frag"
#include "shaderFlat.geom"
//std::string ShaderFlat::vertexShaderText =
//"ATTRIBUTE vec3 VertexPosition;\n"
//"void main()\n"
//"{\n"
//" gl_Position = vec4(VertexPosition, 1.0);\n"
//"}";
//std::string ShaderFlat::geometryShaderText =
//"uniform float explode;\n"
//"uniform mat4 ModelViewProjectionMatrix;\n"
//"uniform mat4 NormalMatrix;\n"
//"uniform mat4 ModelViewMatrix;\n"
//"uniform vec3 lightPosition;\n"
//"uniform vec4 diffuse;\n"
//"uniform vec4 ambient;\n"
//"VARYING_OUT vec4 ColorFS;\n"
//"void main(void)\n"
//"{\n"
//" vec3 v1 = POSITION_IN(1).xyz - POSITION_IN(0).xyz;\n"
//" vec3 v2 = POSITION_IN(2).xyz - POSITION_IN(0).xyz;\n"
//" vec3 N = cross(v1,v2);\n"
//" N = normalize (vec3(NormalMatrix*vec4(N,0.0))); \n"
//" vec3 center = POSITION_IN(0).xyz + POSITION_IN(1).xyz + POSITION_IN(2).xyz; \n"
//" center /= 3.0;\n"
//" vec4 newPos = ModelViewMatrix * vec4(center,0.0);\n"
//" vec3 L = normalize (lightPosition - newPos.xyz);\n"
//" float lambertTerm = dot(N,L);\n"
//" ColorFS = ambient;\n"
//" if(lambertTerm > 0.0)\n"
//" ColorFS += diffuse * lambertTerm;\n"
//" int i;\n"
//" for(i=0; i< NBVERTS_IN; i++)\n"
//" {\n"
//" vec4 pos = explode * POSITION_IN(i) + (1.0-explode)* vec4(center,1.0);\n"
//" gl_Position = ModelViewProjectionMatrix * pos;\n"
//" EmitVertex();\n"
//" }\n"
//" EndPrimitive();\n"
//"}";
//std::string ShaderFlat::fragmentShaderText =
//"VARYING_FRAG vec4 ColorFS; \n"
//"void main()\n"
//"{\n"
//" gl_FragColor = ColorFS;\n"
//"}";
ShaderFlat::ShaderFlat()
......
......@@ -30,66 +30,67 @@ namespace CGoGN
namespace Utils
{
#include "shaderPhong.vert"
#include "shaderPhong.frag"
//std::string ShaderPhong::vertexShaderText =
//"ATTRIBUTE vec3 VertexPosition, VertexNormal;\n"
//"#ifdef WITH_COLOR\n"
//"ATTRIBUTE vec3 VertexColor;\n"
//"#endif\n"
//"uniform mat4 ModelViewProjectionMatrix;\n"
//"uniform mat4 ModelViewMatrix;\n"
//"uniform mat4 NormalMatrix;\n"
//"uniform vec3 lightPosition;\n"
//"VARYING_VERT vec3 EyeVector, Normal, LightDir;\n"
//"#ifdef WITH_COLOR\n"
//"VARYING_VERT vec3 Color;\n"
//"#endif\n"
//"INVARIANT_POS;\n"
//"void main ()\n"
//"{\n"
//" Normal = vec3 (NormalMatrix * vec4 (VertexNormal, 0.0));\n"
//" vec3 Position = vec3 (ModelViewMatrix * vec4 (VertexPosition, 1.0));\n"
//" LightDir = lightPosition - Position;\n"