Commit 3bf37922 authored by Sylvain Thery's avatar Sylvain Thery

GL3 Lines shaders

parent 116f2b83
// ShaderBold3DColorLines::fragmentShaderText
PRECISION;
VARYING_FRAG vec3 fragColor;
uniform float alpha;
uniform vec4 planeClip;
VARYING_FRAG vec3 fragClip;
FRAG_OUT_DEF;
void main()
{
if (dot(planeClip,vec4(fragClip,1.0))>0.0)
discard;
FRAG_OUT = vec4(fragColor,alpha);
}
// ShaderBold3DColorLines::geometryShaderText
uniform float lineWidth;
uniform mat4 ProjectionMatrix;
VARYING_IN vec3 vcolor[];
VARYING_OUT vec3 fragColor;
VARYING_IN vec3 posClip[];
VARYING_OUT vec3 fragClip;
void main()
{
vec4 Ap = (ProjectionMatrix*POSITION_IN(0));
vec4 Bp = (ProjectionMatrix*POSITION_IN(1));
vec3 U3 = normalize((Bp.xyz/Bp.w - Ap.xyz/Ap.w));
vec3 U = vec3(lineWidth*U3);
vec3 V = vec3(lineWidth*normalize(vec2(U[1],-U[0])), 0.0);
fragClip = posClip[0];
fragColor = vcolor[0];
gl_Position = ProjectionMatrix*vec4(POSITION_IN(0).xyz-U, 1.0);
EmitVertex();
gl_Position = ProjectionMatrix*vec4(POSITION_IN(0).xyz+V, 1.0);
EmitVertex();
gl_Position = ProjectionMatrix*vec4(POSITION_IN(0).xyz-V, 1.0);
EmitVertex();
fragClip = posClip[1];
fragColor = vcolor[1];
gl_Position = ProjectionMatrix*vec4(POSITION_IN(1).xyz+V, 1.0);
EmitVertex();
gl_Position = ProjectionMatrix*vec4(POSITION_IN(1).xyz-V, 1.0);
EmitVertex();
gl_Position = ProjectionMatrix*vec4(POSITION_IN(1).xyz+U, 1.0);
EmitVertex();
EndPrimitive();
}
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps *
* version 0.1 *
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg *
* *
* This library is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your *
* option) any later version. *
* *
* This library is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this library; if not, write to the Free Software Foundation, *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
* *
* Web site: http://cgogn.unistra.fr/ *
* Contact information: cgogn@unistra.fr *
* *
*******************************************************************************/
#ifndef __CGOGN_SHADER_BOLD3DCOLORLINES__
#define __CGOGN_SHADER_BOLD3DCOLORLINES__
#include "Utils/GLSLShader.h"
#include "Utils/clippingShader.h"
#include "Geometry/vector_gen.h"
#include "Utils/dll.h"
namespace CGoGN
{
namespace Utils
{
class CGoGN_UTILS_API ShaderBold3DColorLines : public ClippingShader
{
protected:
// shader sources
static std::string vertexShaderText;
static std::string geometryShaderText;
static std::string fragmentShaderText;
CGoGNGLuint m_uniform_lineWidth;
CGoGNGLuint m_unif_alpha;
float m_lineWidth;
VBO* m_vboPos;
VBO* m_vboCol;
float m_opacity;
/// clipping
CGoGNGLuint m_unif_planeClip;
Geom::Vec4f m_planeClip;
void getLocations();
void sendParams();
void restoreUniformsAttribs();
public:
ShaderBold3DColorLines();
void setLineWidth(float pix);
void setOpacity(float op);
unsigned int setAttributeColor(VBO* vbo);
unsigned int setAttributePosition(VBO* vbo);
void setClippingPlane(const Geom::Vec4f& plane);
inline void setNoClippingPlane() { setClippingPlane(Geom::Vec4f(0.0f,0.0f,0.0f,0.0f)); }
};
} // namespace Utils
} // namespace CGoGN
#endif
// ShaderBold3DColorLines::vertexShaderText
ATTRIBUTE vec3 VertexPosition;
ATTRIBUTE vec3 VertexColor;
uniform mat4 ModelViewMatrix;
VARYING_VERT vec3 vcolor;
VARYING_VERT vec3 posClip;
INVARIANT_POS;
void main ()
{
posClip = VertexPosition;
vcolor = VertexColor;
gl_Position = ModelViewMatrix * vec4 (VertexPosition, 1.0);
}
// ShaderBold3DLines::fragmentShaderText
PRECISION;
uniform vec4 lineColor;
uniform vec4 planeClip;
VARYING_FRAG vec3 fragClip;
FRAG_OUT_DEF;
void main()
{
if (dot(planeClip,vec4(fragClip,1.0))>0.0)
discard;
FRAG_OUT = lineColor;
}
// ShaderBold3DLines::geometryShaderText
uniform mat4 ProjectionMatrix;
uniform float lineWidth;
VARYING_IN vec3 posClip[];
VARYING_OUT vec3 fragClip;
void main()
{
vec4 Ap = (ProjectionMatrix*POSITION_IN(0));
vec4 Bp = (ProjectionMatrix*POSITION_IN(1));
vec3 U3 = normalize((Bp.xyz/Bp.w - Ap.xyz/Ap.w));
vec3 U = vec3(lineWidth*U3);
vec3 V = vec3(lineWidth*normalize(vec2(U[1],-U[0])), 0.0);
fragClip = posClip[0];
gl_Position = ProjectionMatrix*vec4(POSITION_IN(0).xyz-U, 1.0);
EmitVertex();
gl_Position = ProjectionMatrix*vec4(POSITION_IN(0).xyz+V, 1.0);
EmitVertex();
gl_Position = ProjectionMatrix*vec4(POSITION_IN(0).xyz-V, 1.0);
EmitVertex();
fragClip = posClip[1];
gl_Position = ProjectionMatrix*vec4(POSITION_IN(1).xyz+V, 1.0);
EmitVertex();
gl_Position = ProjectionMatrix*vec4(POSITION_IN(1).xyz-V, 1.0);
EmitVertex();
gl_Position = ProjectionMatrix*vec4(POSITION_IN(1).xyz+U, 1.0);
EmitVertex();
EndPrimitive();
}
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps *
* version 0.1 *
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg *
* *
* This library is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your *
* option) any later version. *
* *
* This library is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this library; if not, write to the Free Software Foundation, *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
* *
* Web site: http://cgogn.unistra.fr/ *
* Contact information: cgogn@unistra.fr *
* *
*******************************************************************************/
#ifndef __CGOGN_SHADER_BOLD3DLINES__
#define __CGOGN_SHADER_BOLD3DLINES__
#include "Utils/GLSLShader.h"
#include "Utils/clippingShader.h"
#include "Geometry/vector_gen.h"
#include "Utils/dll.h"
namespace CGoGN
{
namespace Utils
{
class CGoGN_UTILS_API ShaderBold3DLines : public ClippingShader
{
protected:
// shader sources
static std::string vertexShaderText;
static std::string geometryShaderText;
static std::string fragmentShaderText;
CGoGNGLuint m_uniform_lineWidth;
CGoGNGLuint m_uniform_color;
float m_lineWidth;
Geom::Vec4f m_color;
VBO* m_vboPos;
/// clipping
CGoGNGLuint m_unif_planeClip;
Geom::Vec4f m_planeClip;
void getLocations();
void sendParams();
void restoreUniformsAttribs();
public:
ShaderBold3DLines();
void setLineWidth(float pix);
void setColor(const Geom::Vec4f& color);
unsigned int setAttributePosition(VBO* vbo);
void setClippingPlane(const Geom::Vec4f& plane);
inline void setNoClippingPlane() { setClippingPlane(Geom::Vec4f(0.0f,0.0f,0.0f,0.0f)); }
};
} // namespace Utils
} // namespace CGoGN
#endif
// ShaderBold3DLines::vertexShaderText
ATTRIBUTE vec3 VertexPosition;
uniform mat4 ModelViewMatrix;
VARYING_VERT vec3 posClip;
INVARIANT_POS;
void main ()
{
posClip = VertexPosition;
gl_Position = ModelViewMatrix * vec4 (VertexPosition, 1.0);
}
// ShaderBoldColorLines::geometryShaderText
uniform vec2 lineWidth;
uniform vec2 lineWidths;
uniform mat4 ProjectionMatrix;
VARYING_IN vec3 vcolor[];
VARYING_OUT vec3 fragColor;
......@@ -10,30 +11,49 @@ VARYING_OUT vec3 fragClip;
void main()
{
vec4 A = POSITION_IN(0);
vec4 B = POSITION_IN(1);
vec3 A = POSITION_IN(0).xyz / abs(POSITION_IN(0).w);
vec3 B = POSITION_IN(1).xyz / abs(POSITION_IN(1).w);
// float nearZ = - ProjectionMatrix[3][2] / (ProjectionMatrix[2][2] - 1.0);
float nearZ = 1.0;
if (ProjectionMatrix[2][2] != 1.0)
nearZ = - ProjectionMatrix[3][2] / (ProjectionMatrix[2][2] - 1.0);
vec3 U = normalize(B-A);
vec3 V = cross(U,vec3(0.0,0.0,1.0));
vec3 LW = vec3(lineWidth,1.0);
if ((A.z < nearZ) || (B.z < nearZ))
{
if (A.z >= nearZ)
A = B + (A-B)*(nearZ-B.z)/(A.z-B.z);
if (B.z >= nearZ)
B = A + (B-A)*(nearZ-A.z)/(B.z-A.z);
A = ProjectionMatrix*A;
B = ProjectionMatrix*B;
A = A/A.w;
B = B/B.w;
vec2 U2 = normalize((B.xyz - A.xyz).xy);
vec3 U = vec3(lineWidths*U2,0.0);
vec3 V = vec3(lineWidths*vec2(U2[1], -U2[0]), 0.0);
U2 *= (lineWidths[0]+lineWidths[1])/4.0;
fragClip = posClip[0];
fragColor = vcolor[0];
gl_Position = vec4(A-U*LW, 1.0);
EmitVertex();
gl_Position = vec4(A+V*LW, 1.0);
A -= vec4(U2,0.0,0.0);
gl_Position = vec4(A.xyz+V, 1.0);
EmitVertex();
gl_Position = vec4(A-V*LW, 1.0);
gl_Position = vec4(A.xyz-V, 1.0);
EmitVertex();
fragColor = vcolor[1];
fragClip = posClip[1];
gl_Position = vec4(B+V*LW, 1.0);
EmitVertex();
gl_Position = vec4(B-V*LW, 1.0);
fragColor = vcolor[1];
B += vec4(U2,0.0,0.0);
gl_Position = vec4(B.xyz+V, 1.0);
EmitVertex();
gl_Position = vec4(B+U*LW, 1.0);
gl_Position = vec4(B.xyz-V, 1.0);
EmitVertex();
EndPrimitive();
}
}
......@@ -49,7 +49,6 @@ protected:
CGoGNGLuint m_unif_alpha;
Geom::Vec2f m_lineWidth;
float m_pixWidth;
VBO* m_vboPos;
VBO* m_vboCol;
......@@ -71,8 +70,6 @@ public:
void setLineWidth(float pix);
void updatePixelWidth();
void setOpacity(float op);
unsigned int setAttributeColor(VBO* vbo);
......
......@@ -2,7 +2,7 @@
ATTRIBUTE vec3 VertexPosition;
ATTRIBUTE vec3 VertexColor;
uniform mat4 ModelViewProjectionMatrix;
uniform mat4 ModelViewMatrix;
VARYING_VERT vec3 vcolor;
VARYING_VERT vec3 posClip;
......@@ -11,5 +11,5 @@ void main ()
{
posClip = VertexPosition;
vcolor = VertexColor;
gl_Position = ModelViewProjectionMatrix * vec4 (VertexPosition, 1.0);
gl_Position = ModelViewMatrix * vec4 (VertexPosition, 1.0);
}
// ShaderBoldLines::geometryShaderText
uniform vec2 lineWidth;
uniform mat4 ProjectionMatrix;
uniform vec2 lineWidths;
VARYING_IN vec3 posClip[];
VARYING_OUT vec3 fragClip;
void main()
{
vec3 A = POSITION_IN(0).xyz / abs(POSITION_IN(0).w);
vec3 B = POSITION_IN(1).xyz / abs(POSITION_IN(1).w);
vec4 A = POSITION_IN(0);
vec4 B = POSITION_IN(1);
vec3 U = normalize(B-A);
vec3 V = cross(U,vec3(0.0,0.0,1.0));
float nearZ = 1.0;
if (ProjectionMatrix[2][2] != 1.0)
nearZ = - ProjectionMatrix[3][2] / (ProjectionMatrix[2][2] - 1.0);
if ((A.z < nearZ) || (B.z < nearZ))
{
if (A.z >= nearZ)
A = B + (A-B)*(nearZ-B.z)/(A.z-B.z);
if (B.z >= nearZ)
B = A + (B-A)*(nearZ-A.z)/(B.z-A.z);
A = ProjectionMatrix*A;
B = ProjectionMatrix*B;
A = A/A.w;
B = B/B.w;
vec2 U2 = normalize((B.xyz - A.xyz).xy);
vec3 U = vec3(lineWidths*U2,0.0);
vec3 V = vec3(lineWidths*vec2(U2[1], -U2[0]), 0.0);
U2 *= (lineWidths[0]+lineWidths[1])/4.0;
vec3 LW = vec3(lineWidth,1.0);
fragClip = posClip[0];
gl_Position = vec4(A-U*LW, 1.0);
A -= vec4(U2,0.0,0.0);
gl_Position = vec4(A.xyz+V, 1.0);
EmitVertex();
gl_Position = vec4(A+V*LW, 1.0);
EmitVertex();
gl_Position = vec4(A-V*LW, 1.0);
gl_Position = vec4(A.xyz-V, 1.0);
EmitVertex();
fragClip = posClip[1];
gl_Position = vec4(B+V*LW, 1.0);
B += vec4(U2,0.0,0.0);
gl_Position = vec4(B.xyz+V, 1.0);
EmitVertex();
gl_Position = vec4(B-V*LW, 1.0);
EmitVertex();
gl_Position = vec4(B+U*LW, 1.0);
gl_Position = vec4(B.xyz-V, 1.0);
EmitVertex();
EndPrimitive();
}
}
......@@ -49,7 +49,6 @@ protected:
CGoGNGLuint m_uniform_color;
Geom::Vec2f m_lineWidth;
float m_pixWidth;
Geom::Vec4f m_color;
......@@ -70,8 +69,6 @@ public:
void setLineWidth(float pix);
void updatePixelWidth();
void setColor(const Geom::Vec4f& color);
unsigned int setAttributePosition(VBO* vbo);
......
// ShaderBoldLines::vertexShaderText
ATTRIBUTE vec3 VertexPosition;
uniform mat4 ModelViewProjectionMatrix;
uniform mat4 ModelViewMatrix;
VARYING_VERT vec3 posClip;
INVARIANT_POS;
void main ()
{
posClip = VertexPosition;
gl_Position = ModelViewProjectionMatrix * vec4 (VertexPosition, 1.0);
gl_Position = ModelViewMatrix * vec4 (VertexPosition, 1.0);
}
......@@ -30,6 +30,7 @@
#include "Geometry/vector_gen.h"
#include "Utils/Shaders/shaderColorPerVertex.h"
#include "Utils/Shaders/shaderBoldColorLines.h"
#include "Utils/Shaders/shaderBold3DColorLines.h"
namespace CGoGN { namespace Utils { class VBO; } }
namespace CGoGN { namespace Utils { namespace SVG { class SVGOut ; } } }
......@@ -71,21 +72,26 @@ protected:
float m_currentSize;
GLenum m_compile;
Utils::ShaderColorPerVertex* m_shader;
Utils::ShaderBoldColorLines* m_shader2;
Utils::ShaderBoldColorLines* m_shaderCL;
Utils::ShaderBold3DColorLines* m_shader3DCL;
int m_lineMode;
public:
/**
* constructor, init all buffers (data and OpenGL) and shader
* @param lineMode 0:simple thin Line / 1:line with possible width /2:3D Lines
* @Warning need OpenGL context
*/
Drawer();
Drawer(int lineMode=0);
/**
* release buffers and shader
*/
~Drawer();
// Utils::ShaderColorPerVertex* getShader();
std::vector<Utils::GLSLShader*> getShaders();
......
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps *
* version 0.1 *
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg *
* *
* This library is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your *
* option) any later version. *
* *
* This library is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this library; if not, write to the Free Software Foundation, *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
* *
* Web site: http://cgogn.unistra.fr/ *
* Contact information: cgogn@unistra.fr *
* *
*******************************************************************************/
#define CGoGN_UTILS_DLL_EXPORT 1
#include "Utils/Shaders/shaderBold3DColorLines.h"
#include <algorithm>
namespace CGoGN
{
namespace Utils
{
#include "ShaderBold3DColorLines.vert"
#include "ShaderBold3DColorLines.geom"
#include "ShaderBold3DColorLines.frag"
ShaderBold3DColorLines::ShaderBold3DColorLines() :
m_lineWidth(0.01f),
m_opacity(1.0f),
m_planeClip(0.0f,0.0f,0.0f,0.0f)
{
m_nameVS = "ShaderBold3DColorLines_vs";
m_nameFS = "ShaderBold3DColorLines_fs";
m_nameGS = "ShaderBold3DColorLines_gs";
std::string glxvert(GLSLShader::defines_gl());
glxvert.append(vertexShaderText);
std::string glxgeom = GLSLShader::defines_Geom("lines", "triangle_strip", 6);
glxgeom.append(geometryShaderText);
std::string glxfrag(GLSLShader::defines_gl());
glxfrag.append(fragmentShaderText);
loadShadersFromMemory(glxvert.c_str(), glxfrag.c_str(), glxgeom.c_str(), GL_LINES, GL_TRIANGLE_STRIP, 6);
// get and fill uniforms
getLocations();
sendParams();
}
void ShaderBold3DColorLines::getLocations()
{
bind();
*m_uniform_lineWidth = glGetUniformLocation(this->program_handler(), "lineWidth");
*m_unif_planeClip = glGetUniformLocation(this->program_handler(), "planeClip");
*m_unif_alpha = glGetUniformLocation(this->program_handler(), "alpha");
unbind();
}
void ShaderBold3DColorLines::sendParams()
{
bind();
glUniform1f(*m_uniform_lineWidth, m_lineWidth);
glUniform1f (*m_unif_alpha, m_opacity);
if (*m_unif_planeClip > 0)
glUniform4fv(*m_unif_planeClip, 1, m_planeClip.data());
unbind();
}
void ShaderBold3DColorLines::setLineWidth(float pix)
{
glm::i32vec4 viewport;
glGetIntegerv(GL_VIEWPORT, &(viewport[0]));
m_lineWidth = pix;
bind();
glUniform1f(*m_uniform_lineWidth, m_lineWidth);
unbind();
}
unsigned int ShaderBold3DColorLines::setAttributeColor(VBO* vbo)
{
m_vboCol = vbo;
bind();