Commit 5ea26aa3 authored by Simon Boyé's avatar Simon Boyé

Vitelotte: Updated VGMeshRenderer to work with linear meshes.

parent 8cb3dd86
#version 410 core
uniform samplerBuffer nodes;
uniform int baseNodeIndex;
uniform bool singularTriangles;
in vec3 linearBasis;
in vec2 position;
flat in vec2 vertices[3];
flat in vec2 normEdges[3];
out vec4 out_color;
float irlerp(in vec2 vx, in vec2 v1, in vec2 v2);
vec4 quadraticInterp(in vec4 colors[6]);
int baseVxIndex = baseNodeIndex + gl_PrimitiveID * (3 + int(singularTriangles));
vec4 linearInterp(in vec4 colors[3])
{
return
colors[0] * linearBasis.x +
colors[1] * linearBasis.y +
colors[2] * linearBasis.z;
}
void main(void)
{
vec4 colorNodes[] = vec4[3](
texelFetch(nodes, baseVxIndex + 0),
texelFetch(nodes, baseVxIndex + 1),
texelFetch(nodes, baseVxIndex + 2)
);
if(singularTriangles)
{
colorNodes[0] = mix(colorNodes[0],
texelFetch(nodes, baseVxIndex + 3),
irlerp(normalize(position - vertices[0]),
normEdges[2], -normEdges[1]));
}
out_color = linearInterp(colorNodes);
}
// Generated by shader2cpp.
// 2014-11-14T17:07:59.526825
// 2014-12-15T13:54:15.035333
namespace Vitelotte
......@@ -96,6 +96,53 @@ static const char* frag_common_glsl =
"}\n"
"";
static const char* frag_linear_glsl =
"#version 410 core\n"
"\n"
"uniform samplerBuffer nodes;\n"
"uniform int baseNodeIndex;\n"
"uniform bool singularTriangles;\n"
"\n"
"in vec3 linearBasis;\n"
"in vec2 position;\n"
"flat in vec2 vertices[3];\n"
"flat in vec2 normEdges[3];\n"
"\n"
"out vec4 out_color;\n"
"\n"
"float irlerp(in vec2 vx, in vec2 v1, in vec2 v2);\n"
"vec4 quadraticInterp(in vec4 colors[6]);\n"
"\n"
"int baseVxIndex = baseNodeIndex + gl_PrimitiveID * (3 + int(singularTriangles));\n"
"\n"
"vec4 linearInterp(in vec4 colors[3])\n"
"{\n"
" return\n"
" colors[0] * linearBasis.x +\n"
" colors[1] * linearBasis.y +\n"
" colors[2] * linearBasis.z;\n"
"}\n"
"\n"
"void main(void)\n"
"{\n"
" vec4 colorNodes[] = vec4[3](\n"
" texelFetch(nodes, baseVxIndex + 0),\n"
" texelFetch(nodes, baseVxIndex + 1),\n"
" texelFetch(nodes, baseVxIndex + 2)\n"
" );\n"
"\n"
" if(singularTriangles)\n"
" {\n"
" colorNodes[0] = mix(colorNodes[0],\n"
" texelFetch(nodes, baseVxIndex + 3),\n"
" irlerp(normalize(position - vertices[0]),\n"
" normEdges[2], -normEdges[1]));\n"
" }\n"
"\n"
" out_color = linearInterp(colorNodes);\n"
"}\n"
"";
static const char* frag_quadratic_glsl =
"#version 410 core\n"
"\n"
......@@ -191,10 +238,10 @@ static const char* geom_common_glsl =
"\n"
"void main()\n"
"{\n"
" for(int i=0; i<3; ++i)\n"
" {\n"
" gl_Position = gl_in[i].gl_Position;\n"
" gl_PrimitiveID = gl_PrimitiveIDIn;\n"
" for(int i=0; i<3; ++i)\n"
" {\n"
" gl_Position = gl_in[i].gl_Position;\n"
" gl_PrimitiveID = gl_PrimitiveIDIn;\n"
" linearBasis = basis[i];\n"
" position = position_obj[i];//gl_in[i].gl_Position.xy;\n"
" for(int j=0; j<3; ++j)\n"
......
......@@ -19,21 +19,21 @@ inline Eigen::Vector4f srgbToLinear(const Eigen::Vector4f& srgb);
class VGMeshRendererShader
{
public:
enum TriangleType { Standard, Singular };
enum TriangleType {
Quadratic = 0x01,
Singular = 0x02
};
public:
inline VGMeshRendererShader() {}
virtual bool useShader(TriangleType triangleType) = 0;
virtual void setNodesTexture(int texUnit, int baseOffset) = 0;
virtual void setNodesTexture(TriangleType triangleType, int texUnit, int baseOffset) = 0;
inline GLint verticesAttibLocation() { return m_verticesLoc; }
virtual GLint verticesAttibLocation(TriangleType triangleType) = 0;
private:
VGMeshRendererShader(const VGMeshRendererShader&);
protected:
GLint m_verticesLoc;
};
......@@ -43,26 +43,38 @@ public:
inline VGMeshRendererDefaultShader();
virtual inline ~VGMeshRendererDefaultShader() {}
virtual GLint verticesAttibLocation(TriangleType triangleType);
virtual bool useShader(TriangleType triangleType);
virtual void setNodesTexture(int texUnit, int baseOffset);
virtual void setNodesTexture(TriangleType triangleType, int texUnit, int baseOffset);
inline Eigen::Matrix4f& viewMatrix() { return m_viewMatrix; }
inline const Eigen::Matrix4f& viewMatrix() const { return m_viewMatrix; }
inline void setViewMatrix(const Eigen::Matrix4f& viewMatrix)
{ m_viewMatrix = viewMatrix; }
protected:
struct Uniforms
{
GLint viewMatrixLoc;
GLint nodesLoc;
GLint baseNodeIndexLoc;
GLint singularTrianglesLoc;
};
protected:
inline void getUniforms();
inline void setupUniforms();
inline void getUniforms(PatateCommon::Shader& shader, Uniforms& uniforms);
inline void setupUniforms(const Uniforms& uniforms);
protected:
PatateCommon::Shader m_shader;
PatateCommon::Shader m_linearShader;
PatateCommon::Shader m_quadraticShader;
GLint m_viewMatrixLoc;
GLint m_nodesLoc;
GLint m_baseNodeIndexLoc;
GLint m_singularTrianglesLoc;
GLint m_verticesLocLinear;
GLint m_verticesLocQuadratic;
Uniforms m_linearUniforms;
Uniforms m_quadraticUniforms;
Eigen::Matrix4f m_viewMatrix;
int m_nodes;
......@@ -77,8 +89,10 @@ public:
inline VGMeshRendererWireframeShader();
virtual inline ~VGMeshRendererWireframeShader() {}
virtual GLint verticesAttibLocation(TriangleType triangleType);
virtual bool useShader(TriangleType triangleType);
virtual void setNodesTexture(int texUnit, int baseOffset);
virtual void setNodesTexture(TriangleType triangleType, int texUnit, int baseOffset);
inline Eigen::Matrix4f& viewMatrix() { return m_viewMatrix; }
inline const Eigen::Matrix4f& viewMatrix() const { return m_viewMatrix; }
......@@ -103,6 +117,8 @@ protected:
protected:
PatateCommon::Shader m_shader;
GLint m_verticesLoc;
GLint m_viewMatrixLoc;
GLint m_zoomLoc;
GLint m_lineWidthLoc;
......@@ -114,66 +130,6 @@ protected:
Eigen::Vector4f m_wireframeColor;
};
//class VGMeshRendererDefaultShaders : public VGMeshRendererShaders
//{
//public:
// inline VGMeshRendererDefaultShaders();
// virtual inline ~VGMeshRendererDefaultShaders() {}
// virtual GLuint useShader(TriangleType triangleType);
// virtual void setColorTexture(int texUnit, int baseOffset);
// inline Eigen::Matrix4f& viewMatrix() { return m_viewMatrix; }
// inline const Eigen::Matrix4f& viewMatrix() const { return m_viewMatrix; }
// inline bool& showWireframe() { return m_showWireframe; }
// inline const bool& showWireframe() const { return m_showWireframe; }
// inline float& zoom() { return m_zoom; }
// inline const float& zoom() const { return m_zoom; }
// inline float& pointRadius() { return m_pointRadius; }
// inline const float& pointRadius() const { return m_pointRadius; }
// inline float& lineWidth() { return m_lineWidth; }
// inline const float& lineWidth() const { return m_lineWidth; }
// inline Eigen::Vector4f& wireframeColor() { return m_wireframeColor; }
// inline const Eigen::Vector4f& wireframeColor() const { return m_wireframeColor; }
// inline Eigen::Vector4f& pointColor() { return m_pointColor; }
// inline const Eigen::Vector4f& pointColor() const { return m_pointColor; }
//protected:
// struct Uniforms
// {
// GLint viewMatrixLoc;
// GLint showWireframeLoc;
// GLint zoomLoc;
// GLint pointRadiusLoc;
// GLint halfLineWidthLoc;
// GLint wireframeColorLoc;
// GLint pointColorLoc;
// };
// inline void getUniforms(PatateCommon::Shader& shader, Uniforms& uniforms);
// inline void setupUniforms(const Uniforms& uniforms);
//protected:
// PatateCommon::Shader m_shader;
// Uniforms m_uniforms;
// Eigen::Matrix4f m_viewMatrix;
// bool m_showWireframe;
// float m_zoom;
// float m_pointRadius;
// float m_lineWidth;
// Eigen::Vector4f m_wireframeColor;
// Eigen::Vector4f m_pointColor;
//};
template < class _Mesh >
class VGMeshRenderer
......@@ -234,6 +190,7 @@ private:
IndicesVector m_indices;
bool m_quadratic;
NodesVector m_nodes;
int m_nTriangles;
int m_nSingulars;
......
......@@ -36,40 +36,68 @@ VGMeshRendererDefaultShader::VGMeshRendererDefaultShader()
}
inline
GLint
VGMeshRendererDefaultShader::verticesAttibLocation(TriangleType triangleType)
{
return (triangleType & Quadratic)? m_verticesLocQuadratic:
m_verticesLocLinear;
}
inline
bool
VGMeshRendererDefaultShader::useShader(TriangleType triangleType)
{
if(m_shader.status() == PatateCommon::Shader::Uninitialized)
bool quadratic = triangleType & Quadratic;
PatateCommon::Shader& shader = quadratic?
m_quadraticShader:
m_linearShader;
Uniforms& uniforms = quadratic?
m_quadraticUniforms:
m_linearUniforms;
if(shader.status() == PatateCommon::Shader::Uninitialized)
{
m_shader.create();
shader.create();
bool bRes = true;
bRes &= m_shader.addShader(GL_VERTEX_SHADER,
bRes &= shader.addShader(GL_VERTEX_SHADER,
shader::vert_common_glsl);
bRes &= m_shader.addShader(GL_GEOMETRY_SHADER,
bRes &= shader.addShader(GL_GEOMETRY_SHADER,
shader::geom_common_glsl);
bRes &= m_shader.addShader(GL_FRAGMENT_SHADER,
bRes &= shader.addShader(GL_FRAGMENT_SHADER,
shader::frag_common_glsl);
bRes &= m_shader.addShader(GL_FRAGMENT_SHADER,
shader::frag_quadratic_glsl);
bRes &= shader.addShader(GL_FRAGMENT_SHADER,
quadratic?
shader::frag_quadratic_glsl:
shader::frag_linear_glsl);
bRes &= m_shader.finalize();
bRes &= shader.finalize();
if(bRes)
{
getUniforms();
m_verticesLoc = glGetAttribLocation(m_shader.getShaderId(), "vx_position");
getUniforms(shader, uniforms);
if(quadratic)
{
m_verticesLocQuadratic =
glGetAttribLocation(shader.getShaderId(), "vx_position");
}
else
{
m_verticesLocLinear =
glGetAttribLocation(shader.getShaderId(), "vx_position");
}
}
}
bool ok = m_shader.status() == PatateCommon::Shader::CompilationSuccessful;
bool ok = shader.status() == PatateCommon::Shader::CompilationSuccessful;
if(ok)
{
m_shader.use();
m_singularTriangles = (triangleType == Singular);
setupUniforms();
shader.use();
m_singularTriangles = (triangleType & Singular);
setupUniforms(uniforms);
}
return ok;
......@@ -79,7 +107,7 @@ VGMeshRendererDefaultShader::useShader(TriangleType triangleType)
inline
void
VGMeshRendererDefaultShader::
setNodesTexture(int texUnit, int baseOffset)
setNodesTexture(TriangleType triangleType, int texUnit, int baseOffset)
{
m_nodes = texUnit;
m_baseNodeIndex = baseOffset;
......@@ -89,31 +117,31 @@ VGMeshRendererDefaultShader::
inline
void
VGMeshRendererDefaultShader::
getUniforms()
getUniforms(PatateCommon::Shader& shader, Uniforms& uniforms)
{
m_viewMatrixLoc = m_shader.getUniformLocation("viewMatrix");
m_nodesLoc = m_shader.getUniformLocation("nodes");
m_baseNodeIndexLoc = m_shader.getUniformLocation("baseNodeIndex");
m_singularTrianglesLoc = m_shader.getUniformLocation("singularTriangles");
uniforms.viewMatrixLoc = shader.getUniformLocation("viewMatrix");
uniforms.nodesLoc = shader.getUniformLocation("nodes");
uniforms.baseNodeIndexLoc = shader.getUniformLocation("baseNodeIndex");
uniforms.singularTrianglesLoc = shader.getUniformLocation("singularTriangles");
}
inline
void
VGMeshRendererDefaultShader::
setupUniforms()
setupUniforms(const Uniforms& uniforms)
{
if(m_viewMatrixLoc >= 0)
glUniformMatrix4fv(m_viewMatrixLoc, 1, false, m_viewMatrix.data());
if(uniforms.viewMatrixLoc >= 0)
glUniformMatrix4fv(uniforms.viewMatrixLoc, 1, false, m_viewMatrix.data());
if(m_nodesLoc >= 0)
glUniform1i(m_nodesLoc, m_nodes);
if(uniforms.nodesLoc >= 0)
glUniform1i(uniforms.nodesLoc, m_nodes);
if(m_baseNodeIndexLoc >= 0)
glUniform1i(m_baseNodeIndexLoc, m_baseNodeIndex);
if(uniforms.baseNodeIndexLoc >= 0)
glUniform1i(uniforms.baseNodeIndexLoc, m_baseNodeIndex);
if(m_singularTrianglesLoc >= 0)
glUniform1i(m_singularTrianglesLoc, m_singularTriangles);
if(uniforms.singularTrianglesLoc >= 0)
glUniform1i(uniforms.singularTrianglesLoc, m_singularTriangles);
}
......@@ -127,6 +155,14 @@ VGMeshRendererWireframeShader::VGMeshRendererWireframeShader()
}
inline
GLint
VGMeshRendererWireframeShader::verticesAttibLocation(TriangleType /*triangleType*/)
{
return m_verticesLoc;
}
inline
bool
VGMeshRendererWireframeShader::useShader(TriangleType /*triangleType*/)
......@@ -169,7 +205,7 @@ VGMeshRendererWireframeShader::useShader(TriangleType /*triangleType*/)
inline
void
VGMeshRendererWireframeShader::
setNodesTexture(int /*texUnit*/, int /*baseOffset*/)
setNodesTexture(TriangleType /*triangleType*/, int /*texUnit*/, int /*baseOffset*/)
{
}
......@@ -224,7 +260,10 @@ inline void VGMeshRenderer<_Mesh>::updateMesh()
m_nodes.clear();
if(!m_pMesh) return;
assert((m_pMesh->getAttributes() & Mesh::Quadratic) == Mesh::Quadratic);
assert(m_pMesh->getAttributes() & Mesh::VertexValue);
m_quadratic = m_pMesh->getAttributes() & Mesh::EdgeValue;
int nodePerTriangle = m_quadratic? 6: 3;
// Compute number of singular and normal triangles
m_nSingulars = m_pMesh->nSingularFaces();
......@@ -233,7 +272,8 @@ inline void VGMeshRenderer<_Mesh>::updateMesh()
// Reserve buffers
m_vertices.reserve(m_pMesh->nVertices());
m_indices.resize(m_nTriangles * 3 + m_nSingulars * 3);
m_nodes.resize(m_nTriangles * 6 + m_nSingulars * 7);
m_nodes.resize(m_nTriangles * nodePerTriangle +
m_nSingulars * (nodePerTriangle + 1));
// Push vertices positions
for(typename Mesh::VertexIterator vit = m_pMesh->verticesBegin();
......@@ -246,7 +286,7 @@ inline void VGMeshRenderer<_Mesh>::updateMesh()
unsigned triIndex = 0;
unsigned singIndex = m_nTriangles * 3;
unsigned triNodeIndex = 0;
unsigned singNodeIndex = m_nTriangles * 6;
unsigned singNodeIndex = m_nTriangles * nodePerTriangle;
for(typename Mesh::FaceIterator fit = m_pMesh->facesBegin();
fit != m_pMesh->facesEnd(); ++fit)
{
......@@ -272,21 +312,24 @@ inline void VGMeshRenderer<_Mesh>::updateMesh()
}
// Singular node is the last one
if(isSingular)
m_nodes[nodeIndex + 6] = nodeValue(m_pMesh->vertexValueNode(h));
m_nodes[nodeIndex + nodePerTriangle] = nodeValue(m_pMesh->vertexValueNode(h));
// Push edge nodes
h = m_pMesh->prevHalfedge(h);
for(int ei = 0; ei < 3; ++ei)
if(m_quadratic)
{
m_nodes[nodeIndex + 3 + ei] = nodeValue(m_pMesh->edgeValueNode(h));
h = m_pMesh->nextHalfedge(h);
// Push edge nodes
h = m_pMesh->prevHalfedge(h);
for(int ei = 0; ei < 3; ++ei)
{
m_nodes[nodeIndex + 3 + ei] = nodeValue(m_pMesh->edgeValueNode(h));
h = m_pMesh->nextHalfedge(h);
}
}
index += 3;
nodeIndex += 6 + isSingular;
nodeIndex += nodePerTriangle + isSingular;
}
assert(triIndex == m_nTriangles * 3 && singIndex == m_indices.size());
assert(triNodeIndex == m_nTriangles * 6 && singNodeIndex == m_nodes.size());
assert(triNodeIndex == m_nTriangles * nodePerTriangle && singNodeIndex == m_nodes.size());
// Create and upload buffers
createAndUploadBuffer(m_verticesBuffer, GL_ARRAY_BUFFER,
......@@ -326,14 +369,17 @@ inline void VGMeshRenderer<_Mesh>::renderTriangles(
glBindVertexArray(m_vao);
shaders.setNodesTexture(0, _singular * m_nTriangles * 6);
if(!shaders.useShader((!_singular)?
VGMeshRendererShader::Standard:
VGMeshRendererShader::Singular))
return;
VGMeshRendererShader::TriangleType triangleType =
VGMeshRendererShader::TriangleType(
(m_quadratic? VGMeshRendererShader::Quadratic: 0) |
(_singular? VGMeshRendererShader::Singular: 0));
GLint verticesLoc = shaders.verticesAttibLocation();
int nodePerTriangle = m_quadratic? 6: 3;
shaders.setNodesTexture(triangleType, 0, _singular * m_nTriangles * nodePerTriangle);
if(!shaders.useShader(triangleType))
return;
GLint verticesLoc = shaders.verticesAttibLocation(triangleType);
if(verticesLoc >= 0)
{
glEnableVertexAttribArray(verticesLoc);
......
#!/bin/sh
./bin/shader2cpp -n Vitelotte::shader -o Patate/Vitelotte/Utils/shaders.hpp Patate/Vitelotte/Shaders/*
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