Commit 6fa0fae0 authored by Sylvain Thery's avatar Sylvain Thery

add iso lines shader

parent f08ed21b
// ShaderIsoLines::fragmentShaderText
VARYING_FRAG vec4 ColorFS;
void main()
{
gl_FragColor = ColorFS;
}
// ShaderIsoLines::geometryShaderText
uniform mat4 NormalMatrix;
uniform mat4 ModelViewMatrix;
uniform mat4 ModelViewProjectionMatrix;
uniform vec4 colorMin;
uniform vec4 colorMax;
uniform float vmin;
uniform float vmax;
uniform int vnb;
VARYING_IN float attribData[3];
VARYING_OUT vec4 ColorFS;
float bary(float x, float xmin, float xmax)
{
return (x-xmin)/(xmax-xmin);
}
/*
* warning works only with triangles
*/
void isoLine(float x)
{
ColorFS = mix(colorMin,colorMax,(x-vmin)/(vmax-vmin));
float b01 = bary(x,attribData[0],attribData[1]);
float b02 = bary(x,attribData[0],attribData[2]);
float b12 = bary(x,attribData[1],attribData[2]);
bool in01 = (b01>=0.0) && (b01<=1.0);
bool in02 = (b02>=0.0) && (b02<=1.0);
bool in12 = (b12>=0.0) && (b12<=1.0);
if (in01)
{
if (in02)
{
// line 01 - 02
vec4 pos = mix(POSITION_IN(0),POSITION_IN(1),b01);
gl_Position = ModelViewProjectionMatrix * pos;
EmitVertex();
pos = mix(POSITION_IN(0),POSITION_IN(2),b02);
gl_Position = ModelViewProjectionMatrix * pos;
EmitVertex();
EndPrimitive();
}
if (in12)
{
// line 01 - 12
vec4 pos = mix(POSITION_IN(0),POSITION_IN(1),b01);
gl_Position = ModelViewProjectionMatrix * pos;
EmitVertex();
pos = mix(POSITION_IN(1),POSITION_IN(2),b12);
gl_Position = ModelViewProjectionMatrix * pos;
EmitVertex();
EndPrimitive();
}
}
if (in02 && in12)
{
// line 12 - 02
vec4 pos = mix(POSITION_IN(0),POSITION_IN(2),b02);
gl_Position = ModelViewProjectionMatrix * pos;
EmitVertex();
pos = mix(POSITION_IN(1),POSITION_IN(2),b12);
gl_Position = ModelViewProjectionMatrix * pos;
EmitVertex();
EndPrimitive();
}
}
void main(void)
{
float inc = (vmax-vmin)/float(vnb);
for (int i=0; i<vnb; ++i)
{
float v = vmin + float(i)*inc;
isoLine(v);
}
// isoLine(0.5);
// isoLine(0.5);
// isoLine(0.75);
}
/*******************************************************************************
* 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_ISO_LINES__
#define __CGOGN_SHADER_ISO_LINES__
#include "Utils/GLSLShader.h"
#include "Geometry/vector_gen.h"
namespace CGoGN
{
namespace Utils
{
class ShaderIsoLines : public GLSLShader
{
protected:
// shader sources
static std::string vertexShaderText;
static std::string fragmentShaderText;
static std::string geometryShaderText;
// uniform locations
CGoGNGLuint m_unif_colorMin;
CGoGNGLuint m_unif_colorMax;
CGoGNGLuint m_unif_vmin;
CGoGNGLuint m_unif_vmax;
CGoGNGLuint m_unif_vnb;
Geom::Vec4f m_colorMin;
Geom::Vec4f m_colorMax;
float m_vmin;
float m_vmax;
int m_vnb;
VBO* m_vboPos;
VBO* m_vboData;
void getLocations();
// void restoreUniformsAttribs();
public:
ShaderIsoLines(int maxNbIsoPerTriangle=6);
void setColors(const Geom::Vec4f& colorMin, const Geom::Vec4f& colorMax);
void setDataBound(float attMin, float attMax);
void setNbIso(int nb);
/**
*
*/
void setAttributePosition(VBO* vbo);
/**
* Data attribute must be float
*/
void setAttributeData(VBO* vbo);
};
} // namespace Utils
} // namespace CGoGN
#endif
// ShaderIsoLines::vertexShaderText
ATTRIBUTE vec3 VertexPosition;
ATTRIBUTE float VertexData;
VARYING_VERT float attribData;
void main()
{
gl_Position = vec4(VertexPosition, 1.0);
attribData = VertexData;
}
/*******************************************************************************
* 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 *
* *
*******************************************************************************/
#include <string>
#include <GL/glew.h>
#include "Utils/Shaders/shaderIsoLines.h"
namespace CGoGN
{
namespace Utils
{
#include "shaderIsoLines.vert"
#include "shaderIsoLines.frag"
#include "shaderIsoLines.geom"
ShaderIsoLines::ShaderIsoLines(int maxNbIsoPerTriangle)
{
m_nameVS = "shaderIsoLines_vs";
m_nameFS = "shaderIsoLines_fs";
m_nameGS = "shaderIsoLines_gs";
std::string glxvert(*GLSLShader::DEFINES_GL);
glxvert.append(vertexShaderText);
std::string glxgeom = GLSLShader::defines_Geom("triangles", "line_strip", 2*maxNbIsoPerTriangle);
glxgeom.append(geometryShaderText);
std::string glxfrag(*GLSLShader::DEFINES_GL);
glxfrag.append(fragmentShaderText);
loadShadersFromMemory(glxvert.c_str(), glxfrag.c_str(), glxgeom.c_str(), GL_TRIANGLES, GL_LINE_STRIP, 2*maxNbIsoPerTriangle);
getLocations();
//Default values
setColors(Geom::Vec4f(1.0f,0.0f,0.0f,1.0f),Geom::Vec4f(0.0f,1.0f,0.0f,1.0f));
setDataBound(0.0f,1.0f);
setNbIso(32);
}
void ShaderIsoLines::getLocations()
{
*m_unif_colorMin = glGetUniformLocation(program_handler(),"colorMin");
*m_unif_colorMax = glGetUniformLocation(program_handler(),"colorMax");
*m_unif_vmin = glGetUniformLocation(program_handler(),"vmin");
*m_unif_vmax = glGetUniformLocation(program_handler(),"vmax");
*m_unif_vnb = glGetUniformLocation(program_handler(),"vnb");
}
void ShaderIsoLines::setAttributePosition(VBO* vbo)
{
m_vboPos = vbo;
bindVA_VBO("VertexPosition", vbo);
}
void ShaderIsoLines::setAttributeData(VBO* vbo)
{
m_vboData = vbo;
bindVA_VBO("VertexData", vbo);
}
void ShaderIsoLines::setColors(const Geom::Vec4f& colorMin, const Geom::Vec4f& colorMax)
{
m_colorMin = colorMin;
m_colorMax = colorMax;
bind();
glUniform4fv(*m_unif_colorMin,1, colorMin.data());
glUniform4fv(*m_unif_colorMax,1, colorMax.data());
}
void ShaderIsoLines::setDataBound(float attMin, float attMax)
{
m_vmin = attMin;
m_vmax = attMax;
bind();
glUniform1f(*m_unif_vmin, attMin);
glUniform1f(*m_unif_vmax, attMax);
}
void ShaderIsoLines::setNbIso(int nb)
{
m_vnb = nb;
bind();
glUniform1i(*m_unif_vnb, nb);
}
/*
void ShaderIsoLines::restoreUniformsAttribs()
{
*m_unif_explode = glGetUniformLocation(program_handler(),"explode");
*m_unif_ambiant = glGetUniformLocation(program_handler(),"ambient");
*m_unif_lightPos = glGetUniformLocation(program_handler(),"lightPosition");
bind();
glUniform1f (*m_unif_explode, m_explode);
glUniform4fv(*m_unif_ambiant, 1, m_ambiant.data());
glUniform3fv(*m_unif_lightPos, 1, m_light_pos.data());
bindVA_VBO("VertexPosition", m_vboPos);
bindVA_VBO("VertexColor", m_vboPos);
unbind();
}
*/
} // namespace Utils
} // namespace CGoGN
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